@@ -4,60 +4,64 @@ import http from 'http';
44
55let serve = false ;
66let prod = false ;
7- let pkg = true ;
87
98if ( process . argv . some ( ( x ) => x === '--serve' ) ) {
109 serve = true ;
11- pkg = false ;
1210}
1311
1412if ( process . argv . some ( ( x ) => x === '--prod' ) ) {
1513 prod = true ;
1614}
1715
18- function build ( input : string , output : string , platform : esbuild . Platform , minify : boolean ) {
16+ function build ( input : string , options : any ) {
1917 return esbuild . context ( {
2018 assetNames : 'assets/[name]-[hash]' ,
2119 bundle : true ,
2220 entryPoints : [ input ] ,
23- external : [ 'worker_threads' , 'path' , 'fs' , 'ws' ] ,
21+ external : [ 'worker_threads' , 'path' , 'fs' , 'ws' , 'url' , 'child_process' , 'http' , 'https' , 'crypto' ] ,
2422 loader : {
2523 '.jpg' : 'file' ,
2624 '.png' : 'file' ,
2725 '.gif' : 'file' ,
2826 } ,
2927 mainFields : [ 'main' , 'module' ] ,
30- minify : minify ,
31- outfile : output ,
32- platform : platform ,
3328 plugins : [
3429 cssModulesPlugin ( {
3530 inject : ( cssContent , digest ) => `console.log("${ cssContent } ", "${ digest } ")` ,
3631 } )
3732 ] ,
3833 sourcemap : true ,
39- target : [ 'es2020' , 'node12' ] ,
4034 define : {
4135 'process.env.NODE_ENV' : prod ? '"production"' : '"development"' ,
4236 } ,
37+ ...options ,
4338 } ) ;
4439}
4540
46- const outputs = {
47- browser : [
48- build ( 'repl/App.tsx' , '../dist/repl.mjs' , 'browser' , prod ) ,
49- build ( 'webR/webr-worker.ts' , '../dist/webr-worker.js' , 'node' , true ) ,
50- build ( 'webR/webr-main.ts' , '../dist/webr.mjs' , 'neutral' , prod ) ,
51- ] ,
52- npm : [
53- build ( 'webR/webr-worker.ts' , './dist/webr-worker.js' , 'node' , true ) ,
54- build ( 'webR/webr-main.ts' , './dist/webr.cjs' , 'node' , prod ) ,
55- build ( 'webR/webr-main.ts' , './dist/webr.mjs' , 'neutral' , prod ) ,
56- ]
57- } ;
58- const allOutputs = outputs . browser . concat ( pkg ? outputs . npm : [ ] ) ;
41+ const outputs = [
42+ build ( 'repl/App.tsx' , { outfile : '../dist/repl.js' , platform : 'browser' , format : 'iife' , target : [ 'es2022' ] , minify : prod } ) , // browser, script
43+ build ( 'webR/webr-main.ts' , { outfile : '../dist/webr.mjs' , platform : 'neutral' , format : 'esm' , target : [ 'es2022' ] , minify : prod } ) , // browser, script, type="module"
44+ build ( 'webR/webr-worker.ts' , { outfile : '../dist/webr-worker.js' , platform : 'neutral' , format : 'iife' , minify : prod } ) , // browser, worker
45+ build ( 'webR/webr-main.ts' , { outfile : './dist/webr.cjs' , platform : 'node' , format : 'cjs' , minify : prod } ) , // node, cjs
46+ build ( 'webR/webr-worker.ts' , { outfile : './dist/webr-worker.js' , platform : 'node' , format : 'cjs' , minify : prod } ) , // node, worker
47+ build ( 'webR/webr-main.ts' , { // node, esm
48+ outfile : './dist/webr.mjs' ,
49+ platform : 'node' ,
50+ format : 'esm' ,
51+ banner : {
52+ js : `import { createRequire } from 'module';
53+ import { fileURLToPath as urlESMPluginFileURLToPath } from "url";
54+ import { dirname as pathESMPluginDirname} from "path";
55+ const require = createRequire(import.meta.url);
56+ var __filename = urlESMPluginFileURLToPath(import.meta.url);
57+ var __dirname = pathESMPluginDirname(urlESMPluginFileURLToPath(import.meta.url));
58+ `
59+ } ,
60+ minify : prod
61+ } ) ,
62+ ] ;
5963
60- allOutputs . forEach ( ( build ) => {
64+ outputs . forEach ( ( build ) => {
6165 build
6266 . then ( async ( context ) => {
6367 await context . rebuild ( ) ;
@@ -70,7 +74,7 @@ allOutputs.forEach((build) => {
7074} ) ;
7175
7276if ( serve ) {
73- outputs . browser [ 0 ]
77+ outputs [ 0 ]
7478 . then ( async ( context ) => {
7579 await context . serve ( { servedir : '../dist' , port : 8001 } ) . then ( ( ) => {
7680 http
@@ -101,7 +105,7 @@ if (serve) {
101105 throw new Error ( 'A problem occurred serving webR distribution with esbuild' ) ;
102106 } ) ;
103107} else {
104- allOutputs . forEach ( build => {
108+ outputs . forEach ( build => {
105109 build
106110 . then ( async ( context ) => {
107111 await context . dispose ( ) ;
0 commit comments