11const fs = require ( 'fs' ) ;
22const terser = require ( 'terser' ) ;
33
4- const MINIFY = true ;
5-
6- try { fs . mkdirSync ( './dist' ) ; }
7- catch ( e ) { }
8-
9- const wasmBuffer = fs . readFileSync ( './lib/lexer.wasm' ) ;
10- const jsSource = fs . readFileSync ( './src/lexer.js' ) . toString ( ) ;
11- const pjson = JSON . parse ( fs . readFileSync ( './package.json' ) . toString ( ) ) ;
12-
13- const jsSourceProcessed = jsSource . replace ( 'WASM_BINARY' , wasmBuffer . toString ( 'base64' ) ) ;
14-
15- const minified = MINIFY && terser . minify ( jsSourceProcessed , {
16- module : true ,
17- output : {
18- preamble : `/* cjs-module-lexer ${ pjson . version } */`
4+ function buildCjsModuleLexer ( filename , inline , minify ) {
5+ try { fs . mkdirSync ( './dist' ) ; }
6+ catch ( e ) { }
7+
8+ const wasmBuffer = fs . readFileSync ( './lib/lexer.wasm' ) ;
9+ const jsSource = fs . readFileSync ( './src/lexer.js' ) . toString ( ) ;
10+ const pjson = JSON . parse ( fs . readFileSync ( './package.json' ) . toString ( ) ) ;
11+
12+ let jsSourceProcessed ;
13+ if ( inline ) {
14+ 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' ) } ")` ) ;
15+ } else {
16+ jsSourceProcessed = jsSource . replace ( 'WASM_BINARY' , `(await import('node:fs')).readFileSync(new URL(import.meta.resolve('../lib/lexer.wasm')))` ) ;
1917 }
20- } ) ;
2118
22- if ( minified . error )
23- throw minified . error ;
19+ const minified = minify && terser . minify ( jsSourceProcessed , {
20+ module : true ,
21+ output : {
22+ preamble : `/* cjs-module-lexer ${ pjson . version } */`
23+ }
24+ } ) ;
25+
26+ if ( minified . error )
27+ throw minified . error ;
28+
29+ fs . writeFileSync ( `./dist/${ filename } .mjs` , minified ? minified . code : jsSourceProcessed ) ;
30+ }
2431
25- fs . writeFileSync ( './dist/lexer.mjs' , minified ? minified . code : jsSourceProcessed ) ;
32+ buildCjsModuleLexer ( 'lexer' , true , true ) ;
33+ buildCjsModuleLexer ( 'lexer-external' , false , false ) ;
0 commit comments