|
1 | 1 | const fs = require('fs'); |
2 | | -const { buildSync } = require('esbuild'); |
3 | | -const path = require('path/posix') |
| 2 | +const esbuild = require('esbuild'); |
4 | 3 |
|
5 | | -const { EXTERNAL_PATH } = process.env; |
6 | | -const MINIFY = !EXTERNAL_PATH; |
| 4 | +let minify = true; |
| 5 | +let cjsBuild = true; |
| 6 | +let esmBuild = true; |
7 | 7 |
|
8 | | -try { fs.mkdirSync('./dist'); } |
9 | | -catch (e) {} |
| 8 | +for (const arg of process.argv) { |
| 9 | + switch (arg) { |
| 10 | + case '--without-cjs': { |
| 11 | + cjsBuild = false; |
| 12 | + break; |
| 13 | + } |
| 14 | + case '--without-esm': { |
| 15 | + esmBuild = false; |
| 16 | + break; |
| 17 | + } |
| 18 | + case '--no-minify': { |
| 19 | + minify = false; |
| 20 | + break; |
| 21 | + } |
| 22 | + default: |
| 23 | + continue; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fs.mkdirSync('./dist', { recursive: true }); |
10 | 28 |
|
11 | 29 | const wasmBuffer = fs.readFileSync('./lib/lexer.wasm'); |
12 | 30 | const pjson = JSON.parse(fs.readFileSync('./package.json').toString()); |
13 | 31 |
|
14 | | -buildSync({ |
| 32 | +/** @type {esbuild.BuildOptions} */ |
| 33 | +const buildOptions = { |
15 | 34 | entryPoints: ['./src/lexer.js'], |
16 | | - outfile: './dist/lexer.mjs', |
17 | 35 | bundle: true, |
18 | | - minify: MINIFY, |
| 36 | + minify, |
19 | 37 | platform: 'node', |
20 | | - format: 'esm', |
21 | 38 | banner: { |
22 | | - js: `/* cjs-module-lexer ${pjson.version} */` |
| 39 | + js: `/* cjs-module-lexer ${pjson.version} */`, |
23 | 40 | }, |
24 | | - define: EXTERNAL_PATH ? { |
25 | | - WASM_BINARY: 'undefined', |
26 | | - EXTERNAL_PATH: `'${path.join(EXTERNAL_PATH, 'lib/lexer.wasm')}'`, |
27 | | - } : { |
| 41 | + outfile: './dist/lexer.mjs', |
| 42 | + format: 'esm', |
| 43 | + define: { |
28 | 44 | WASM_BINARY: `'${wasmBuffer.toString('base64')}'`, |
29 | | - EXTERNAL_PATH: 'undefined' |
30 | | - } |
31 | | -}) |
32 | | - |
33 | | -if (EXTERNAL_PATH) { |
34 | | - buildSync({ |
35 | | - stdin: { |
36 | | - contents: `'use strict'; |
37 | | -let lazy; |
38 | | -async function init () { |
39 | | - if (!lazy) { |
40 | | - lazy = await import(require('node:url').pathToFileURL(require('node:module').createRequire('${EXTERNAL_PATH}/dist/lexer.js').resolve('./lexer.mjs'))); |
41 | | - } |
42 | | - module.exports = lazy; |
43 | | - return lazy.init(); |
44 | | -} |
45 | | -
|
46 | | -function parse (source, name = '@') { |
47 | | - if (!lazy) |
48 | | - throw new Error('Not initialized'); |
| 45 | + }, |
| 46 | +}; |
49 | 47 |
|
50 | | - return lazy.parse(source, name); |
| 48 | +if (esmBuild) { |
| 49 | + esbuild.buildSync({ |
| 50 | + ...buildOptions, |
| 51 | + define: { |
| 52 | + WASM_BINARY: 'undefined', |
| 53 | + }, |
| 54 | + }); |
51 | 55 | } |
52 | 56 |
|
53 | | -module.exports = { init, parse };`, |
54 | | - loader: 'js', |
55 | | - }, |
| 57 | +if (cjsBuild) { |
| 58 | + esbuild.buildSync({ |
| 59 | + ...buildOptions, |
56 | 60 | outfile: './dist/lexer.js', |
57 | | - minify: MINIFY, |
58 | | - platform: 'node', |
59 | 61 | format: 'cjs', |
| 62 | + logOverride: { |
| 63 | + 'empty-import-meta': 'silent' |
| 64 | + }, |
60 | 65 | }); |
61 | | -} else { |
62 | | - buildSync({ |
63 | | - entryPoints: ['./dist/lexer.mjs'], |
64 | | - outfile: './dist/lexer.js', |
65 | | - minify: MINIFY, |
66 | | - platform: 'node', |
67 | | - format: 'cjs', |
68 | | - banner: { |
69 | | - js: `/* cjs-module-lexer ${pjson.version} */` |
70 | | - } |
71 | | - }) |
72 | 66 | } |
73 | | - |
0 commit comments