forked from nodejs/cjs-module-lexer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement externalized WASM build, fix nodejs#96
- Loading branch information
Showing
3 changed files
with
65 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,69 @@ | ||
const fs = require('fs'); | ||
const { buildSync } = require('esbuild'); | ||
const path = require('path/posix') | ||
const esbuild = require('esbuild'); | ||
|
||
const { EXTERNAL_PATH } = process.env; | ||
const MINIFY = !EXTERNAL_PATH; | ||
let minify = true; | ||
let cjsBuild = true; | ||
let esmBuild = true; | ||
|
||
try { fs.mkdirSync('./dist'); } | ||
catch (e) {} | ||
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()); | ||
|
||
buildSync({ | ||
/** @type {esbuild.BuildOptions} */ | ||
const buildOptions = { | ||
entryPoints: ['./src/lexer.js'], | ||
outfile: './dist/lexer.mjs', | ||
bundle: true, | ||
minify: MINIFY, | ||
minify, | ||
platform: 'node', | ||
format: 'esm', | ||
banner: { | ||
js: `/* cjs-module-lexer ${pjson.version} */` | ||
js: `/* cjs-module-lexer ${pjson.version} */`, | ||
}, | ||
define: EXTERNAL_PATH ? { | ||
WASM_BINARY: 'undefined', | ||
EXTERNAL_PATH: `'${path.join(EXTERNAL_PATH, 'lib/lexer.wasm')}'`, | ||
} : { | ||
outfile: './dist/lexer.mjs', | ||
format: 'esm', | ||
define: { | ||
WASM_BINARY: `'${wasmBuffer.toString('base64')}'`, | ||
EXTERNAL_PATH: 'undefined' | ||
} | ||
}) | ||
|
||
if (EXTERNAL_PATH) { | ||
buildSync({ | ||
stdin: { | ||
contents: `'use strict'; | ||
let lazy; | ||
async function init () { | ||
if (!lazy) { | ||
lazy = await import(require('node:url').pathToFileURL(require('node:module').createRequire('${EXTERNAL_PATH}/dist/lexer.js').resolve('./lexer.mjs'))); | ||
} | ||
module.exports = lazy; | ||
return lazy.init(); | ||
} | ||
function parse (source, name = '@') { | ||
if (!lazy) | ||
throw new Error('Not initialized'); | ||
}, | ||
}; | ||
|
||
return lazy.parse(source, name); | ||
if (esmBuild) { | ||
// ESM builds are used when importing from npm. | ||
// Assume lib/lexer.wasm exists. | ||
esbuild.buildSync({ | ||
...buildOptions, | ||
define: { | ||
WASM_BINARY: 'undefined', | ||
}, | ||
}); | ||
} | ||
|
||
module.exports = { init, parse };`, | ||
loader: 'js', | ||
}, | ||
if (cjsBuild) { | ||
// CJS builds are used for libnode inlined builtins. | ||
esbuild.buildSync({ | ||
...buildOptions, | ||
outfile: './dist/lexer.js', | ||
minify: MINIFY, | ||
platform: 'node', | ||
format: 'cjs', | ||
logOverride: { | ||
'empty-import-meta': 'silent' | ||
}, | ||
}); | ||
} else { | ||
buildSync({ | ||
entryPoints: ['./dist/lexer.mjs'], | ||
outfile: './dist/lexer.js', | ||
minify: MINIFY, | ||
platform: 'node', | ||
format: 'cjs', | ||
banner: { | ||
js: `/* cjs-module-lexer ${pjson.version} */` | ||
} | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters