@@ -159,18 +159,16 @@ function getCopyrightHeader() {
159159 * @param {boolean } performanceMatters True if this is a bundle where performance matters, so should be optimized at the cost of build time.
160160 */
161161function esbuildTask ( entrypoint , outfile , exportIsTsObject = false , performanceMatters = false ) {
162- const preBabel = `${ outfile } .tmp.js` ;
163-
164162 /** @type {esbuild.BuildOptions } */
165163 const options = {
166164 entryPoints : [ entrypoint ] ,
167165 banner : { js : getCopyrightHeader ( ) } ,
168166 bundle : true ,
169- outfile : performanceMatters ? preBabel : outfile ,
167+ outfile,
170168 platform : "node" ,
171169 target : "es2018" , // This covers Node 10.
172170 format : "cjs" ,
173- sourcemap : true ,
171+ sourcemap : "linked" ,
174172 external : [ "./node_modules/*" ] ,
175173 conditions : [ "require" ] ,
176174 supported : {
@@ -180,6 +178,30 @@ function esbuildTask(entrypoint, outfile, exportIsTsObject = false, performanceM
180178 // legalComments: "none", // If we add copyright headers to the source files, uncomment.
181179 } ;
182180
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+
183205 if ( exportIsTsObject ) {
184206 options . format = "iife" ; // We use an IIFE so we can inject the code below.
185207 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
200222 }
201223
202224 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 ) ,
212226 clean : ( ) => del ( [ outfile , `${ outfile } .map` ] ) ,
213227 watch : ( ) => esbuild . build ( { ...options , watch : true } ) ,
214228 } ;
0 commit comments