@@ -138,14 +138,17 @@ function getCopyrightHeader() {
138
138
* @param {string } entrypoint
139
139
* @param {string } outfile
140
140
* @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.
141
142
*/
142
- function esbuildTask ( entrypoint , outfile , exportIsTsObject = false ) {
143
+ function esbuildTask ( entrypoint , outfile , exportIsTsObject = false , performanceMatters = false ) {
144
+ const preBabel = `${ outfile } .tmp.js` ;
145
+
143
146
/** @type {esbuild.BuildOptions } */
144
147
const options = {
145
148
entryPoints : [ entrypoint ] ,
146
149
banner : { js : getCopyrightHeader ( ) } ,
147
150
bundle : true ,
148
- outfile,
151
+ outfile : performanceMatters ? preBabel : outfile ,
149
152
platform : "node" ,
150
153
// TODO: also specify minimal browser targets
151
154
target : "node10" , // Node 10 is the oldest benchmarker.
@@ -181,7 +184,15 @@ function esbuildTask(entrypoint, outfile, exportIsTsObject = false) {
181
184
}
182
185
183
186
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
+ } ,
185
196
clean : ( ) => del ( [ outfile , `${ outfile } .map` ] ) ,
186
197
watch : ( ) => esbuild . build ( { ...options , watch : true } ) ,
187
198
} ;
@@ -209,7 +220,7 @@ cleanTasks.push(cleanDebugTools);
209
220
const lkgPreBuild = parallel ( generateLibs , series ( buildScripts , generateDiagnostics , buildDebugTools ) ) ;
210
221
211
222
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 ) ;
213
224
214
225
215
226
const buildTsc = ( ) => {
@@ -236,7 +247,7 @@ const localPreBuild = parallel(generateLibs, series(buildScripts, generateDiagno
236
247
// Pre-build steps to use based on supplied options.
237
248
const preBuild = cmdLineOptions . lkg ? lkgPreBuild : localPreBuild ;
238
249
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 ) ;
240
251
241
252
// TODO(jakebailey): rename this; no longer "services".
242
253
const buildServices = ( ) => {
@@ -266,7 +277,7 @@ task("watch-services").flags = {
266
277
} ;
267
278
268
279
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 ) ;
270
281
271
282
const buildServer = ( ) => {
272
283
if ( cmdLineOptions . bundle ) return esbuildServer . build ( ) ;
@@ -309,7 +320,7 @@ task("watch-min").flags = {
309
320
" --built" : "Compile using the built version of the compiler."
310
321
} ;
311
322
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 ) ;
313
324
314
325
const buildLssl = ( ) => {
315
326
if ( cmdLineOptions . bundle ) return esbuildLssl . build ( ) ;
0 commit comments