Skip to content

Commit 8b807a5

Browse files
committed
feat: support external wasm binary
1 parent 7c74749 commit 8b807a5

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

build.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
const fs = require('fs');
22
const terser = require('terser');
33

4-
const MINIFY = true;
5-
6-
try { fs.mkdirSync('./dist'); }
7-
catch (e) {}
8-
9-
const wasmBuffer = fs.readFileSync('./lib/lexer.wasm');
10-
const jsSource = fs.readFileSync('./src/lexer.js').toString();
11-
const pjson = JSON.parse(fs.readFileSync('./package.json').toString());
12-
13-
const jsSourceProcessed = jsSource.replace('WASM_BINARY', wasmBuffer.toString('base64'));
14-
15-
const minified = MINIFY && terser.minify(jsSourceProcessed, {
16-
module: true,
17-
output: {
18-
preamble: `/* cjs-module-lexer ${pjson.version} */`
4+
function buildCjsModuleLexer(filename, inline, minify) {
5+
try { fs.mkdirSync('./dist'); }
6+
catch (e) {}
7+
8+
const wasmBuffer = fs.readFileSync('./lib/lexer.wasm');
9+
const jsSource = fs.readFileSync('./src/lexer.js').toString();
10+
const pjson = JSON.parse(fs.readFileSync('./package.json').toString());
11+
12+
let jsSourceProcessed;
13+
if (inline) {
14+
jsSourceProcessed = jsSource.replace('WASM_BINARY', `(binary => typeof window !== 'undefined' && typeof atob === 'function' ? Uint8Array.from(atob(binary), x => x.charCodeAt(0)) : Buffer.from(binary, 'base64'))("${wasmBuffer.toString('base64')}")`);
15+
} else {
16+
jsSourceProcessed = jsSource.replace('WASM_BINARY', `(await import('node:fs')).readFileSync(new URL(import.meta.resolve('../lib/lexer.wasm')))`);
1917
}
20-
});
2118

22-
if (minified.error)
23-
throw minified.error;
19+
const minified = minify && terser.minify(jsSourceProcessed, {
20+
module: true,
21+
output: {
22+
preamble: `/* cjs-module-lexer ${pjson.version} */`
23+
}
24+
});
25+
26+
if (minified.error)
27+
throw minified.error;
28+
29+
fs.writeFileSync(`./dist/${filename}.mjs`, minified ? minified.code : jsSourceProcessed);
30+
}
2431

25-
fs.writeFileSync('./dist/lexer.mjs', minified ? minified.code : jsSourceProcessed);
32+
buildCjsModuleLexer('lexer', true, true);
33+
buildCjsModuleLexer('lexer-external', false, false);

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
"test-wasm": "cross-env WASM=1 mocha -b -u tdd test/*.js",
1717
"test": "npm run test-wasm && npm run test-js",
1818
"bench": "node --expose-gc bench/index.mjs",
19-
"build": "node build.js ; babel dist/lexer.mjs -o dist/lexer.js ; terser dist/lexer.js -o dist/lexer.js",
20-
"build-wasm": "make lib/lexer.wasm && node build.js",
19+
"build": "npm run build-wasm ; npm run build-cjs ; npm run build-js",
20+
"build-js": "terser dist/lexer.js -o dist/lexer.js",
21+
"build-cjs": "babel dist/*.mjs --out-dir dist",
22+
"build-wasm": "make lib/lexer.wasm ; node build.js",
2123
"prepublishOnly": "make && npm run build",
2224
"footprint": "npm run build && cat dist/lexer.js | gzip -9f | wc -c"
2325
},

src/lexer.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ export function init () {
9696
return initPromise;
9797
return initPromise = (async () => {
9898
const compiled = await WebAssembly.compile(
99-
(binary => typeof window !== 'undefined' && typeof atob === 'function' ? Uint8Array.from(atob(binary), x => x.charCodeAt(0)) : Buffer.from(binary, 'base64'))
100-
('WASM_BINARY')
101-
)
99+
WASM_BINARY
100+
);
102101
const { exports } = await WebAssembly.instantiate(compiled);
103102
wasm = exports;
104103
})();

0 commit comments

Comments
 (0)