From 8b807a5a405b5eadc596825e9ace1a88c3e0fb23 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 28 Apr 2024 11:13:12 -0700 Subject: [PATCH] feat: support external wasm binary --- build.js | 46 +++++++++++++++++++++++++++------------------- package.json | 6 ++++-- src/lexer.js | 5 ++--- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/build.js b/build.js index da024d1..6b0098f 100755 --- a/build.js +++ b/build.js @@ -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); \ No newline at end of file diff --git a/package.json b/package.json index 7e150f3..1173fc5 100755 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/lexer.js b/src/lexer.js index 3143fe5..958ca13 100755 --- a/src/lexer.js +++ b/src/lexer.js @@ -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; })();