-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
50 lines (41 loc) · 1.22 KB
/
build.mjs
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
import fs from "fs/promises";
import swc from "@swc/core";
import * as esbuild from "esbuild";
const files = [
{ name: "patches", npm: false },
{ name: "require", npm: true },
];
try {
await fs.mkdir("dist");
} catch (e) {}
for (const file of files) {
let start = Date.now();
/**
* @type {esbuild.BuildOptions}
*/
let esbuildOptions = {
entryPoints: [`${file.name}/index.js`],
bundle: true,
write: false,
legalComments: "none",
keepNames: true,
};
if (file.npm) {
esbuildOptions = {
...esbuildOptions,
globalName: "__requireCache",
define: { global: "globalThis" },
};
}
const build = await esbuild.build(esbuildOptions);
console.log(`esbuild: '${file.name}' finished in ${Date.now() - start}ms`);
const outputFile = build.outputFiles[0];
if (!file.npm) {
await fs.writeFile(`dist/${file.name}.js`, outputFile.contents);
} else {
start = Date.now();
const transform = await swc.transform(outputFile.text);
console.log(`swc: '${file.name}' finished in ${Date.now() - start}ms`);
await fs.writeFile(`dist/${file.name}.js`, transform.code);
}
}