@@ -159,18 +159,16 @@ function getCopyrightHeader() {
159
159
* @param {boolean } performanceMatters True if this is a bundle where performance matters, so should be optimized at the cost of build time.
160
160
*/
161
161
function esbuildTask ( entrypoint , outfile , exportIsTsObject = false , performanceMatters = false ) {
162
- const preBabel = `${ outfile } .tmp.js` ;
163
-
164
162
/** @type {esbuild.BuildOptions } */
165
163
const options = {
166
164
entryPoints : [ entrypoint ] ,
167
165
banner : { js : getCopyrightHeader ( ) } ,
168
166
bundle : true ,
169
- outfile : performanceMatters ? preBabel : outfile ,
167
+ outfile,
170
168
platform : "node" ,
171
169
target : "es2018" , // This covers Node 10.
172
170
format : "cjs" ,
173
- sourcemap : true ,
171
+ sourcemap : "linked" ,
174
172
external : [ "./node_modules/*" ] ,
175
173
conditions : [ "require" ] ,
176
174
supported : {
@@ -180,6 +178,30 @@ function esbuildTask(entrypoint, outfile, exportIsTsObject = false, performanceM
180
178
// legalComments: "none", // If we add copyright headers to the source files, uncomment.
181
179
} ;
182
180
181
+ if ( performanceMatters ) {
182
+ const preBabel = `${ outfile } .tmp.js` ;
183
+ options . outfile = preBabel ;
184
+ options . sourcemap = "inline" ;
185
+ options . plugins = [
186
+ {
187
+ name : "babel" ,
188
+ setup : ( build ) => {
189
+ build . onEnd ( async ( ) => {
190
+ await exec ( process . execPath , [
191
+ "./node_modules/@babel/cli/bin/babel.js" ,
192
+ preBabel ,
193
+ "--out-file" , outfile ,
194
+ "--plugins" , "@babel/plugin-transform-block-scoping,./scripts/build/removeEsbuildRequire" ,
195
+ "--compact" , "false" ,
196
+ "--source-maps" ]
197
+ ) ;
198
+ await del ( preBabel ) ;
199
+ } ) ;
200
+ } ,
201
+ }
202
+ ] ;
203
+ }
204
+
183
205
if ( exportIsTsObject ) {
184
206
options . format = "iife" ; // We use an IIFE so we can inject the code below.
185
207
options . globalName = "ts" ; // Name the variable ts, matching our old big bundle and so we can use the code below.
@@ -200,15 +222,7 @@ function esbuildTask(entrypoint, outfile, exportIsTsObject = false, performanceM
200
222
}
201
223
202
224
return {
203
- build : async ( ) => {
204
- await esbuild . build ( options ) ;
205
- if ( performanceMatters ) {
206
- // TODO(jakebailey): we could use ts.transpileModule for this, but running babel is faster to get this singular transform.
207
- // If we did use ts.transpileModule, we'd need ts.ScriptTarget.ES5, which also will downlevel arrow functions, for-of, spread, etc.
208
- await exec ( process . execPath , [ "./node_modules/@babel/cli/bin/babel.js" , preBabel , "--out-file" , outfile , "--plugins" , "@babel/plugin-transform-block-scoping" , "--compact" , "false" , "--source-maps" ] ) ;
209
- await del ( [ preBabel , `${ preBabel } .map` ] ) ;
210
- }
211
- } ,
225
+ build : ( ) => esbuild . build ( options ) ,
212
226
clean : ( ) => del ( [ outfile , `${ outfile } .map` ] ) ,
213
227
watch : ( ) => esbuild . build ( { ...options , watch : true } ) ,
214
228
} ;
0 commit comments