forked from nodejs/cjs-module-lexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·69 lines (62 loc) · 1.34 KB
/
build.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
58
59
60
61
62
63
64
65
66
67
68
69
const fs = require('fs');
const esbuild = require('esbuild');
let minify = true;
let cjsBuild = true;
let esmBuild = true;
for (const arg of process.argv) {
switch (arg) {
case '--without-cjs': {
cjsBuild = false;
break;
}
case '--without-esm': {
esmBuild = false;
break;
}
case '--no-minify': {
minify = false;
break;
}
default:
continue;
}
}
fs.mkdirSync('./dist', { recursive: true });
const wasmBuffer = fs.readFileSync('./lib/lexer.wasm');
const pjson = JSON.parse(fs.readFileSync('./package.json').toString());
/** @type {esbuild.BuildOptions} */
const buildOptions = {
entryPoints: ['./src/lexer.js'],
bundle: true,
minify,
platform: 'node',
banner: {
js: `/* cjs-module-lexer ${pjson.version} */`,
},
outfile: './dist/lexer.mjs',
format: 'esm',
define: {
WASM_BINARY: `'${wasmBuffer.toString('base64')}'`,
},
};
if (esmBuild) {
// ESM builds are used when importing from npm.
// Assume lib/lexer.wasm exists.
esbuild.buildSync({
...buildOptions,
define: {
WASM_BINARY: 'undefined',
},
});
}
if (cjsBuild) {
// CJS builds are used for libnode inlined builtins.
esbuild.buildSync({
...buildOptions,
outfile: './dist/lexer.js',
format: 'cjs',
logOverride: {
'empty-import-meta': 'silent'
},
});
}