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