-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.mjs
54 lines (46 loc) · 1.29 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
51
52
53
54
import path from 'path';
import glob from 'fast-glob';
import { fileURLToPath } from 'url';
import esbuild from "esbuild";
// we need to change up how __dirname is used for ES6 purposes
const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function main() {
// Absolute path to package directory
const basePath = __dirname;
// Get all .ts as input files
const entryPoints = glob.sync(path.resolve(basePath, "src", "**", "**.ts")
.replace(/\\/g, '/')); // windows support
const outdir = path.join(basePath, 'build');
// CommonJS output
console.log("Generating CJS build...");
esbuild.build({
entryPoints,
outdir,
format: "cjs",
target: "es2017",
sourcemap: "external",
platform: "node",
});
// ESM output
console.log("Generating ESM build...");
esbuild.build({
entryPoints,
outdir,
target: "esnext",
format: "esm",
bundle: true,
sourcemap: "external",
platform: "node",
outExtension: { '.js': '.mjs', },
plugins: [{
name: 'add-mjs',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (args.importer) return { path: args.path.replace(/^\.(.*)\.js$/, '.$1.mjs'), external: true }
})
},
}]
});
console.log("Done!");
}
export default await main();