-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_tools.ts
180 lines (165 loc) · 5.51 KB
/
build_tools.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/** some build specific utility functions */
import { ensureDir } from "https://deno.land/[email protected]/fs/mod.ts"
import { join as pathJoin } from "https://deno.land/[email protected]/path/mod.ts"
import { BuildOptions, PackageJson } from "https://deno.land/x/[email protected]/mod.ts"
import {
BuildOptions as ESBuildOptions,
OutputFile as ESOutputFile,
TransformOptions as ESTransformOptions,
build as esbuild, stop as esstop, transform as estransform
} from "https://deno.land/x/[email protected]/mod.js"
import { denoPlugins } from "https://deno.land/x/[email protected]/mod.ts"
export { ensureDir, ensureFile, walk as walkDir } from "https://deno.land/[email protected]/fs/mod.ts"
export { join as pathJoin, relative as relativePath } from "https://deno.land/[email protected]/path/mod.ts"
export { stop as esstop } from "https://deno.land/x/[email protected]/mod.js"
export type {
BuildOptions as ESBuildOptions,
OutputFile as ESOutputFile,
TransformOptions as ESTransformOptions
} from "https://deno.land/x/[email protected]/mod.js"
export const mainEntrypoint: string = "./src/mod.ts"
export const subEntrypoints: string[] = [
"./src/array2d.ts",
"./src/binder.ts",
"./src/browser.ts",
"./src/builtin_aliases_deps.ts",
"./src/builtin_aliases.ts",
"./src/collections.ts",
"./src/crypto.ts",
"./src/devdebug.ts",
"./src/dotkeypath.ts",
"./src/eightpack.ts",
"./src/eightpack_varint.ts",
"./src/formattable.ts",
"./src/image.ts",
"./src/lambda.ts",
"./src/lambdacalc.ts",
"./src/mapper.ts",
"./src/numericarray.ts",
"./src/numericmethods.ts",
"./src/stringman.ts",
"./src/struct.ts",
"./src/typedbuffer.ts",
"./src/typedefs.ts",
]
export interface LeftoverArtifacts {
cleanup: () => Promise<void>
}
export interface TemporaryFiles extends LeftoverArtifacts {
dir: string
files: string[]
}
interface NPM_Artifacts extends TemporaryFiles {
files: ["package.json", "tsconfig.json"]
}
let deno_json: { [key: string]: any }
export const getDenoJson = async (base_dir: string = "./") => {
deno_json ??= JSON.parse(await Deno.readTextFile(pathJoin(base_dir, "./deno.json")))
return deno_json
}
export const createPackageJson = async (deno_json_dir: string = "./", overrides: Partial<PackageJson> = {}): Promise<PackageJson> => {
const { name, version, description, author, license, repository, bugs, devDependencies } = await getDenoJson(deno_json_dir)
return {
name: name ?? "",
version: version ?? "0.0.0",
description, author, license, repository, bugs, devDependencies,
...overrides
}
}
export const createTSConfigJson = async (deno_json_dir: string = "./", overrides: Partial<{ compilerOptions: BuildOptions["compilerOptions"] }> = {}): Promise<{ "$schema": string, compilerOptions: BuildOptions["compilerOptions"] }> => {
const { compilerOptions } = await getDenoJson(deno_json_dir)
// remove "deno.ns" from compiler options, as it breaks `dnt` (I think)
compilerOptions.lib = (compilerOptions.lib as string[]).filter((v) => v.toLowerCase() !== "deno.ns")
Object.assign(compilerOptions,
{
target: "ESNext",
forceConsistentCasingInFileNames: true,
skipLibCheck: true,
moduleResolution: "nodenext",
},
overrides.compilerOptions,
)
delete overrides.compilerOptions
return {
"$schema": "https://json.schemastore.org/tsconfig",
...overrides,
compilerOptions,
}
}
export const createNPMFiles = async (
deno_json_dir: string = "./",
npm_base_dir: string = "./",
package_json_overrides: Partial<PackageJson> = {},
tsconfig_json_overrides: any = {}
): Promise<NPM_Artifacts> => {
const
package_json_path = pathJoin(npm_base_dir, "package.json"),
tsconfig_json_path = pathJoin(npm_base_dir, "tsconfig.json")
await ensureDir(npm_base_dir)
await Promise.all([
createPackageJson(deno_json_dir, package_json_overrides)
.then((package_json_output) => Deno.writeTextFile(
pathJoin(npm_base_dir, "package.json"),
JSON.stringify(package_json_output)
)),
createTSConfigJson(deno_json_dir, tsconfig_json_overrides)
.then((tsconfig_json_output) => Deno.writeTextFile(
pathJoin(npm_base_dir, "tsconfig.json"),
JSON.stringify(tsconfig_json_output)
)),
])
return {
dir: npm_base_dir,
files: ["package.json", "tsconfig.json"],
cleanup: async () => {
await Promise.all([
Deno.remove(package_json_path, { recursive: true }),
Deno.remove(tsconfig_json_path, { recursive: true }),
])
}
}
}
export const doubleCompileFiles = async (
compile_file_path: string,
out_dir: string,
override_bundle_options: ESBuildOptions = {},
override_minify_options: ESTransformOptions = {},
stop: boolean = true
) => {
let t0 = performance.now(), t1: number
const bundled_code = await esbuild({
entryPoints: [compile_file_path],
outdir: out_dir,
bundle: true,
minifySyntax: true,
platform: "neutral",
format: "esm",
target: "esnext",
plugins: [...denoPlugins()],
...override_bundle_options,
write: false,
})
const bundled_files = await Promise.all(bundled_code.outputFiles.map(
async ({ text, path }, file_number): Promise<ESOutputFile> => {
const
js_text = (await estransform(text, {
minify: true,
platform: "browser",
format: "esm",
target: "esnext",
...override_minify_options
})).code,
js_text_uint8 = (new TextEncoder()).encode(js_text)
console.log("bundled file", file_number, "\n\t", "output path:", path, "\n\t", "binary size:", js_text_uint8.byteLength / 1024, "kb")
return {
path,
text: js_text,
contents: js_text_uint8
}
}
))
if (stop) { esstop() }
t1 = performance.now()
console.log("bundling time:", t1 - t0, "ms")
return bundled_files
}