-
Notifications
You must be signed in to change notification settings - Fork 24
/
build.js
executable file
·72 lines (63 loc) · 1.56 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const fs = require('fs');
const { buildSync } = require('esbuild');
const { EXTERNAL_PATH } = process.env;
const MINIFY = !EXTERNAL_PATH;
try { fs.mkdirSync('./dist'); }
catch (e) {}
const wasmBuffer = fs.readFileSync('./lib/lexer.wasm');
const pjson = JSON.parse(fs.readFileSync('./package.json').toString());
buildSync({
entryPoints: ['./src/lexer.js'],
outfile: './dist/lexer.mjs',
bundle: true,
minify: MINIFY,
platform: 'node',
format: 'esm',
banner: {
js: `/* cjs-module-lexer ${pjson.version} */`
},
define: EXTERNAL_PATH ? {
WASM_BINARY: 'undefined',
EXTERNAL_PATH: `'${EXTERNAL_PATH}'`,
} : {
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);
}
module.exports = { init, parse };`,
loader: 'js',
},
outfile: './dist/lexer.js',
minify: MINIFY,
platform: 'node',
format: 'cjs',
});
} else {
buildSync({
entryPoints: ['./dist/lexer.mjs'],
outfile: './dist/lexer.js',
minify: MINIFY,
platform: 'node',
format: 'cjs',
banner: {
js: `/* cjs-module-lexer ${pjson.version} */`
}
})
}