1
- import { context } from "esbuild" ;
2
- import process from "process" ;
1
+ import * as esbuild from "esbuild" ;
2
+ import * as process from "process" ;
3
+ import { readFileSync } from "fs" ;
4
+ import { umdWrapper } from "esbuild-plugin-umd-wrapper" ;
5
+ import yargs from "yargs" ;
6
+ import { hideBin } from "yargs/helpers" ;
3
7
4
- const production = process . argv . includes ( "--production" ) ;
5
- const watch = process . argv . includes ( "--watch" ) ;
8
+ const myYargs = yargs ( hideBin ( process . argv ) ) ;
9
+ myYargs
10
+ . usage ( "Usage: node esbuild.mjs [options]" )
11
+ . demandCommand ( 0 , 0 )
12
+ . example ( "node esbuild.mjs --watch" , "Bundle and watch for changes" )
13
+ . option ( "mode" , {
14
+ alias : "m" ,
15
+ describe : "Build mode" ,
16
+ choices : [ "development" , "production" ] ,
17
+ default : "production"
18
+ } )
19
+ . option ( "w" , {
20
+ alias : "watch" ,
21
+ describe : "Watch for changes" ,
22
+ type : "boolean"
23
+ } )
24
+ . help ( "h" )
25
+ . alias ( "h" , "help" )
26
+ . epilog ( "https://github.com/hpcc-systems/hpcc-js-wasm" )
27
+ ;
28
+ const argv = await myYargs . argv ;
29
+ const isDevelopment = argv . mode === "development" ;
30
+ const isProduction = ! isDevelopment ;
31
+ const isWatch = argv . watch ;
6
32
7
- /**
8
- * @type {import('esbuild').Plugin }
9
- */
10
- const esbuildProblemMatcherPlugin = {
33
+ // plugins ---
34
+ const excludeSourceMapPlugin = ( { filter } ) => ( {
35
+ name : 'excludeSourceMapPlugin' ,
36
+
37
+ setup ( build ) {
38
+ build . onLoad ( { filter } , ( args ) => {
39
+ return {
40
+ contents :
41
+ readFileSync ( args . path , 'utf8' ) +
42
+ '\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==' ,
43
+ loader : 'default' ,
44
+ } ;
45
+ } ) ;
46
+ } ,
47
+ } ) ;
48
+
49
+ const esbuildProblemMatcherPlugin = ( { filter } ) => ( {
11
50
name : "esbuild-problem-matcher" ,
12
51
13
52
setup ( build ) {
@@ -22,95 +61,97 @@ const esbuildProblemMatcherPlugin = {
22
61
console . log ( "[watch] build finished" ) ;
23
62
} ) ;
24
63
} ,
25
- } ;
64
+ } ) ;
26
65
27
- async function extension ( ) {
28
- const ctx = await context ( {
29
- entryPoints : [
30
- "src/extension.ts" ,
31
- "src/debugger.ts"
32
- ] ,
33
- bundle : true ,
34
- format : "cjs" ,
35
- minify : production ,
36
- sourcemap : ! production ,
37
- sourcesContent : false ,
38
- platform : "node" ,
39
- outdir : "dist" ,
40
- external : [ "vscode" ] ,
41
- // logLevel: "silent",
66
+ // helpers ---
67
+ function build ( config ) {
68
+ isDevelopment && console . log ( "Start: " , config . entryPoints [ 0 ] , config . outfile ) ;
69
+ return esbuild . build ( {
70
+ ...config ,
71
+ sourcemap : "linked" ,
42
72
plugins : [
43
- /* add to the end of plugins array */
44
- esbuildProblemMatcherPlugin ,
45
- ] ,
73
+ ...config . plugins ?? [ ] ,
74
+ ]
75
+ } ) . then ( ( ) => {
76
+ isDevelopment && console . log ( "Stop: " , config . entryPoints [ 0 ] , config . outfile ) ;
46
77
} ) ;
47
- if ( watch ) {
48
- await ctx . watch ( ) ;
49
- } else {
50
- await ctx . rebuild ( ) ;
51
- await ctx . dispose ( ) ;
52
- }
53
78
}
54
79
55
- async function notebook ( ) {
56
- const ctx = await context ( {
57
- entryPoints : [
58
- "src/eclwatch.tsx" ,
59
- "src/notebook/renderers/wuRenderer.tsx" ,
60
- "src/notebook/renderers/ojsRenderer.ts"
61
- ] ,
62
- bundle : true ,
63
- format : "esm" ,
64
- minify : true ,
65
- sourcemap : ! production ,
66
- sourcesContent : false ,
67
- platform : "browser" ,
68
- outdir : "dist" ,
69
- external : [ "vscode" , "fs" , "path" , "os" ] ,
70
- // logLevel: "silent",
80
+ function watch ( config ) {
81
+ return esbuild . context ( {
82
+ ...config ,
83
+ sourcemap : "linked" ,
71
84
plugins : [
72
- /* add to the end of plugins array */
73
- esbuildProblemMatcherPlugin ,
74
- ] ,
85
+ ...config . plugins ?? [ ] ,
86
+ {
87
+ name : "rebuild-notify" ,
88
+ setup ( build ) {
89
+ build . onEnd ( result => {
90
+ console . log ( `Built ${ config . outfile } ` ) ;
91
+ } ) ;
92
+ } ,
93
+ }
94
+ ]
95
+ } ) . then ( ctx => {
96
+ return ctx . watch ( ) ;
75
97
} ) ;
76
- if ( watch ) {
77
- await ctx . watch ( ) ;
78
- } else {
79
- await ctx . rebuild ( ) ;
80
- await ctx . dispose ( ) ;
81
- }
82
98
}
83
99
84
- async function web_extension ( ) {
85
- const ctx = await context ( {
86
- entryPoints : [
87
- "src/web-extension.ts"
88
- ] ,
89
- bundle : true ,
90
- format : "esm" ,
91
- minify : production ,
92
- sourcemap : ! production ,
93
- sourcesContent : false ,
100
+ function browserTpl ( input , output , format , globalName = "" , external = [ ] ) {
101
+ return ( isWatch ? watch : build ) ( {
102
+ entryPoints : [ input ] ,
103
+ outfile : `${ output } .${ format === "esm" ? "js" : "umd.js" } ` ,
94
104
platform : "browser" ,
95
- outfile : "dist-web/extension.js " ,
96
- external : [ "vscode" , "fs" , "path" , "os" ] ,
97
- // logLevel: "silent" ,
98
- plugins : [
99
- /* add to the end of plugins array */
100
- esbuildProblemMatcherPlugin ,
101
- ] ,
105
+ target : "es2022 " ,
106
+ format ,
107
+ globalName ,
108
+ bundle : true ,
109
+ minify : isProduction ,
110
+ external ,
111
+ plugins : format === "umd" ? [ umdWrapper ( ) ] : [ ]
102
112
} ) ;
103
- if ( watch ) {
104
- await ctx . watch ( ) ;
105
- } else {
106
- await ctx . rebuild ( ) ;
107
- await ctx . dispose ( ) ;
108
- }
109
113
}
110
114
111
- Promise . all ( [ extension ( ) , notebook ( ) , web_extension ( ) ] )
112
- . then ( ( ) => {
113
- } ) . catch ( e => {
114
- console . error ( e ) ;
115
- process . exit ( 1 ) ;
115
+ function browserBoth ( input , output , globalName = undefined , external = [ ] ) {
116
+ return Promise . all ( [
117
+ browserTpl ( input , output , "esm" , globalName , external ) ,
118
+ browserTpl ( input , output , "umd" , globalName , external )
119
+ ] ) ;
120
+ }
121
+
122
+ function nodeTpl ( input , output , format , external = [ ] ) {
123
+ return ( isWatch ? watch : build ) ( {
124
+ entryPoints : [ input ] ,
125
+ outfile : `${ output } .${ format === "esm" ? "mjs" : "js" } ` ,
126
+ platform : "node" ,
127
+ target : "node20" ,
128
+ format,
129
+ bundle : true ,
130
+ minify : isProduction ,
131
+ external
116
132
} ) ;
133
+ }
134
+
135
+ function nodeBoth ( input , output , external = [ ] ) {
136
+ return Promise . all ( [
137
+ nodeTpl ( input , output , "esm" , external ) ,
138
+ nodeTpl ( input , output , "cjs" , external )
139
+ ] ) ;
140
+ }
141
+
142
+ function bothTpl ( input , output , globalName = undefined , external = [ ] ) {
143
+ return Promise . all ( [
144
+ browserBoth ( input , output , globalName , external ) ,
145
+ nodeTpl ( input , output , "cjs" , external )
146
+ ] ) ;
147
+ }
148
+
149
+ // config ---
150
+ nodeTpl ( "src/extension.ts" , "dist/extension" , "cjs" , [ "vscode" ] ) ;
151
+ nodeTpl ( "src/notebook/renderers/wuRenderer.tsx" , "dist/notebook/renderers" , "cjs" , [ "vscode" , "fs" , "path" , "os" ] ) ;
152
+ nodeTpl ( "src/notebook/renderers/ojsRenderer.ts" , "dist/notebook/renderers" , "cjs" , [ "vscode" , "fs" , "path" , "os" ] ) ;
153
+
154
+ nodeTpl ( "src/debugger.ts" , "dist/debugger" , "cjs" , [ "vscode" ] ) ;
155
+
156
+ browserTpl ( "src/eclwatch.tsx" , "dist/eclwatch" , "esm" , undefined , [ "vscode" , "fs" , "path" , "os" ] ) ;
157
+ browserTpl ( "src/web-extension.ts" , "dist-web/extension.js" , "esm" , undefined , [ "vscode" , "fs" , "path" , "os" ] ) ;
0 commit comments