@@ -138,14 +138,17 @@ function getCopyrightHeader() {
138138 * @param {string } entrypoint
139139 * @param {string } outfile
140140 * @param {boolean } exportIsTsObject True if this file exports the TS object and should have relevant code injected.
141+ * @param {boolean } performanceMatters True if this is a bundle where performance matters, so should be optimized at the cost of build time.
141142 */
142- function esbuildTask ( entrypoint , outfile , exportIsTsObject = false ) {
143+ function esbuildTask ( entrypoint , outfile , exportIsTsObject = false , performanceMatters = false ) {
144+ const preBabel = `${ outfile } .tmp.js` ;
145+
143146 /** @type {esbuild.BuildOptions } */
144147 const options = {
145148 entryPoints : [ entrypoint ] ,
146149 banner : { js : getCopyrightHeader ( ) } ,
147150 bundle : true ,
148- outfile,
151+ outfile : performanceMatters ? preBabel : outfile ,
149152 platform : "node" ,
150153 // TODO: also specify minimal browser targets
151154 target : "node10" , // Node 10 is the oldest benchmarker.
@@ -181,7 +184,15 @@ function esbuildTask(entrypoint, outfile, exportIsTsObject = false) {
181184 }
182185
183186 return {
184- build : ( ) => esbuild . build ( options ) ,
187+ build : async ( ) => {
188+ await esbuild . build ( options ) ;
189+ if ( performanceMatters ) {
190+ // TODO(jakebailey): we could use ts.transpileModule for this, but running babel is faster to get this singular transform.
191+ // If we did use ts.transpileModule, we'd need ts.ScriptTarget.ES5, which also will downlevel arrow functions, for-of, spread, etc.
192+ 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" ] ) ;
193+ await del ( [ preBabel , `${ preBabel } .map` ] ) ;
194+ }
195+ } ,
185196 clean : ( ) => del ( [ outfile , `${ outfile } .map` ] ) ,
186197 watch : ( ) => esbuild . build ( { ...options , watch : true } ) ,
187198 } ;
@@ -209,7 +220,7 @@ cleanTasks.push(cleanDebugTools);
209220const lkgPreBuild = parallel ( generateLibs , series ( buildScripts , generateDiagnostics , buildDebugTools ) ) ;
210221
211222
212- const esbuildTsc = esbuildTask ( "./src/tsc/tsc.ts" , "./built/local/tsc.js" , /* exportIsTsObject */ true ) ;
223+ const esbuildTsc = esbuildTask ( "./src/tsc/tsc.ts" , "./built/local/tsc.js" , /* exportIsTsObject */ true , /* performanceMatters */ true ) ;
213224
214225
215226const buildTsc = ( ) => {
@@ -236,7 +247,7 @@ const localPreBuild = parallel(generateLibs, series(buildScripts, generateDiagno
236247// Pre-build steps to use based on supplied options.
237248const preBuild = cmdLineOptions . lkg ? lkgPreBuild : localPreBuild ;
238249
239- const esbuildServices = esbuildTask ( "./src/typescript/typescript.ts" , "./built/local/typescript.js" , /* exportIsTsObject */ true ) ;
250+ const esbuildServices = esbuildTask ( "./src/typescript/typescript.ts" , "./built/local/typescript.js" , /* exportIsTsObject */ true , /* performanceMatters */ true ) ;
240251
241252// TODO(jakebailey): rename this; no longer "services".
242253const buildServices = ( ) => {
@@ -266,7 +277,7 @@ task("watch-services").flags = {
266277} ;
267278
268279
269- const esbuildServer = esbuildTask ( "./src/tsserver/server.ts" , "./built/local/tsserver.js" , /* exportIsTsObject */ true ) ;
280+ const esbuildServer = esbuildTask ( "./src/tsserver/server.ts" , "./built/local/tsserver.js" , /* exportIsTsObject */ true , /* performanceMatters */ true ) ;
270281
271282const buildServer = ( ) => {
272283 if ( cmdLineOptions . bundle ) return esbuildServer . build ( ) ;
@@ -309,7 +320,7 @@ task("watch-min").flags = {
309320 " --built" : "Compile using the built version of the compiler."
310321} ;
311322
312- const esbuildLssl = esbuildTask ( "./src/tsserverlibrary/tsserverlibrary.ts" , "./built/local/tsserverlibrary.js" , /* exportIsTsObject */ true ) ;
323+ const esbuildLssl = esbuildTask ( "./src/tsserverlibrary/tsserverlibrary.ts" , "./built/local/tsserverlibrary.js" , /* exportIsTsObject */ true , /* performanceMatters */ true ) ;
313324
314325const buildLssl = ( ) => {
315326 if ( cmdLineOptions . bundle ) return esbuildLssl . build ( ) ;
0 commit comments