-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-cjs.js
57 lines (52 loc) · 1.35 KB
/
build-cjs.js
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
import * as esbuild from "esbuild";
import path from "node:path";
import { platform } from "node:process";
const entry = "./src/index.ts";
let jsEntry = path.resolve(
"dist/node",
path.basename(entry).replace(".ts", ".js")
);
let outfile = jsEntry.replace(".js", ".cjs");
await esbuild.build({
entryPoints: ["./dist/node/index.js"],
bundle: true,
platform: "node",
format: "cjs",
target: "es2021",
resolveExtensions: [".node.js", ".ts", ".js"],
allowOverwrite: true,
plugins: [makeNodeModulesExternal(), makeJsooExternal()],
allowOverwrite: true,
outfile,
dropLabels: ["ESM"],
minify: false,
});
function makeNodeModulesExternal() {
let isNodeModule = /^[^./\\]|^\.[^./\\]|^\.\.[^/\\]/;
return {
name: "plugin-external",
setup(build) {
build.onResolve({ filter: isNodeModule }, ({ path }) => ({
path,
external: !(platform === "win32" && path.endsWith("index.js")),
}));
},
};
}
function makeJsooExternal() {
let isJsoo = /(bc.cjs|plonk_wasm.cjs)$/;
return {
name: "plugin-external",
setup(build) {
build.onResolve({ filter: isJsoo }, ({ path: filePath, resolveDir }) => ({
path:
"./" +
path.relative(
path.resolve(".", "dist/node"),
path.resolve(resolveDir, filePath)
),
external: true,
}));
},
};
}