Skip to content

Commit afc8eae

Browse files
committed
Support building for externally shared js builtins
Initial support for loading unbundled module in `AddExternalizedBuiltin`. - Reduces downstream distribution package size (by not shipping wasm twice and not base64-encoding it) - Provides a cleaner stacktrace - Easier to patch To enable this, pass `EXTERNAL_PATH=/global/node_modules/cjs-module-lexer` to `build.js`. You shall also pass this path to `--shared-builtin-cjs_module_lexer/dist/lexer-path` in Node.js's `configure.py`, with the extra `/dist` part in the path. Signed-off-by: Zephyr Lykos <[email protected]>
1 parent 279682c commit afc8eae

File tree

6 files changed

+396
-779
lines changed

6 files changed

+396
-779
lines changed

.babelrc

Lines changed: 0 additions & 10 deletions
This file was deleted.

build.js

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,72 @@
11
const fs = require('fs');
2-
const terser = require('terser');
2+
const { buildSync } = require('esbuild');
33

4-
const MINIFY = true;
4+
const { EXTERNAL_PATH } = process.env;
5+
const MINIFY = !EXTERNAL_PATH;
56

67
try { fs.mkdirSync('./dist'); }
78
catch (e) {}
89

910
const wasmBuffer = fs.readFileSync('./lib/lexer.wasm');
10-
const jsSource = fs.readFileSync('./src/lexer.js').toString();
1111
const pjson = JSON.parse(fs.readFileSync('./package.json').toString());
1212

13-
const jsSourceProcessed = jsSource.replace('WASM_BINARY', wasmBuffer.toString('base64'));
13+
buildSync({
14+
entryPoints: ['./src/lexer.js'],
15+
outfile: './dist/lexer.mjs',
16+
bundle: true,
17+
minify: MINIFY,
18+
platform: 'node',
19+
format: 'esm',
20+
banner: {
21+
js: `/* cjs-module-lexer ${pjson.version} */`
22+
},
23+
define: EXTERNAL_PATH ? {
24+
WASM_BINARY: 'undefined',
25+
EXTERNAL_PATH: `'${EXTERNAL_PATH}'`,
26+
} : {
27+
WASM_BINARY: `'${wasmBuffer.toString('base64')}'`,
28+
EXTERNAL_PATH: 'undefined'
29+
}
30+
})
1431

15-
const minified = MINIFY && terser.minify(jsSourceProcessed, {
16-
module: true,
17-
output: {
18-
preamble: `/* cjs-module-lexer ${pjson.version} */`
32+
if (EXTERNAL_PATH) {
33+
buildSync({
34+
stdin: {
35+
contents: `'use strict';
36+
let lazy;
37+
async function init () {
38+
if (!lazy) {
39+
lazy = await import(require('node:url').pathToFileURL(require('node:module').createRequire('${EXTERNAL_PATH}/dist/lexer.js').resolve('./lexer.mjs')));
1940
}
20-
});
41+
module.exports = lazy;
42+
return lazy.init();
43+
}
44+
45+
function parse (source, name = '@') {
46+
if (!lazy)
47+
throw new Error('Not initialized');
48+
49+
return lazy.parse(source, name);
50+
}
2151
22-
if (minified.error)
23-
throw minified.error;
52+
module.exports = { init, parse };`,
53+
loader: 'js',
54+
},
55+
outfile: './dist/lexer.js',
56+
minify: MINIFY,
57+
platform: 'node',
58+
format: 'cjs',
59+
});
60+
} else {
61+
buildSync({
62+
entryPoints: ['./dist/lexer.mjs'],
63+
outfile: './dist/lexer.js',
64+
minify: MINIFY,
65+
platform: 'node',
66+
format: 'cjs',
67+
banner: {
68+
js: `/* cjs-module-lexer ${pjson.version} */`
69+
}
70+
})
71+
}
2472

25-
fs.writeFileSync('./dist/lexer.mjs', minified ? minified.code : jsSourceProcessed);

0 commit comments

Comments
 (0)