Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support external wasm binary #97

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
const fs = require('fs');
const terser = require('terser');

const MINIFY = true;

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

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

const jsSourceProcessed = jsSource.replace('WASM_BINARY', wasmBuffer.toString('base64'));

const minified = MINIFY && terser.minify(jsSourceProcessed, {
module: true,
output: {
preamble: `/* cjs-module-lexer ${pjson.version} */`
function buildCjsModuleLexer(filename, inline, minify) {
try { fs.mkdirSync('./dist'); }
catch (e) {}

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

let jsSourceProcessed;
if (inline) {
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')}")`);
} else {
jsSourceProcessed = jsSource.replace('WASM_BINARY', `(await import('node:fs')).readFileSync(new URL(import.meta.resolve('../lib/lexer.wasm')))`);
}
});

if (minified.error)
throw minified.error;
const minified = minify && terser.minify(jsSourceProcessed, {
module: true,
output: {
preamble: `/* cjs-module-lexer ${pjson.version} */`
}
});

if (minified.error)
throw minified.error;

fs.writeFileSync(`./dist/${filename}.mjs`, minified ? minified.code : jsSourceProcessed);
}

fs.writeFileSync('./dist/lexer.mjs', minified ? minified.code : jsSourceProcessed);
buildCjsModuleLexer('lexer', true, true);
buildCjsModuleLexer('lexer-external', false, false);
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"test-wasm": "cross-env WASM=1 mocha -b -u tdd test/*.js",
"test": "npm run test-wasm && npm run test-js",
"bench": "node --expose-gc bench/index.mjs",
"build": "node build.js ; babel dist/lexer.mjs -o dist/lexer.js ; terser dist/lexer.js -o dist/lexer.js",
"build-wasm": "make lib/lexer.wasm && node build.js",
"build": "npm run build-wasm ; npm run build-cjs ; npm run build-js",
"build-js": "terser dist/lexer.js -o dist/lexer.js",
"build-cjs": "babel dist/*.mjs --out-dir dist",
"build-wasm": "make lib/lexer.wasm ; node build.js",
"prepublishOnly": "make && npm run build",
"footprint": "npm run build && cat dist/lexer.js | gzip -9f | wc -c"
},
Expand Down
5 changes: 2 additions & 3 deletions src/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ export function init () {
return initPromise;
return initPromise = (async () => {
const compiled = await WebAssembly.compile(
(binary => typeof window !== 'undefined' && typeof atob === 'function' ? Uint8Array.from(atob(binary), x => x.charCodeAt(0)) : Buffer.from(binary, 'base64'))
('WASM_BINARY')
)
WASM_BINARY
);
const { exports } = await WebAssembly.instantiate(compiled);
wasm = exports;
})();
Expand Down
Loading