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