@@ -164,13 +164,16 @@ async function runDtsBundler(entrypoint, output) {
164
164
* @property {() => void } [onWatchRebuild]
165
165
*/
166
166
function createBundler ( entrypoint , outfile , taskOptions = { } ) {
167
+ const preBabel = `${ outfile . replace ( / \. j s $ / , "" ) } .preBabel.js` ;
168
+ const postBabel = `${ outfile . replace ( / \. j s $ / , "" ) } .postBabel.js` ;
169
+
167
170
const getOptions = memoize ( async ( ) => {
168
171
/** @type {esbuild.BuildOptions } */
169
172
const options = {
170
173
entryPoints : [ entrypoint ] ,
171
174
banner : { js : await copyright ( ) } ,
172
175
bundle : true ,
173
- outfile,
176
+ outfile : preBabel ,
174
177
platform : "node" ,
175
178
target : "es2018" ,
176
179
format : "cjs" ,
@@ -206,15 +209,37 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
206
209
name : "fix-require" ,
207
210
setup : ( build ) => {
208
211
build . onEnd ( async ( ) => {
209
- let contents = await fs . promises . readFile ( outfile , "utf-8" ) ;
212
+ let contents = await fs . promises . readFile ( preBabel , "utf-8" ) ;
210
213
contents = contents . replace ( / \$ \$ r e q u i r e / g, " require" ) ;
211
- await fs . promises . writeFile ( outfile , contents ) ;
214
+ await fs . promises . writeFile ( preBabel , contents ) ;
212
215
} ) ;
213
216
} ,
214
- }
217
+ } ,
215
218
] ;
216
219
}
217
220
221
+ options . plugins = ( options . plugins ?? [ ] ) . concat ( {
222
+ name : "let-const" ,
223
+ setup : ( build ) => {
224
+ build . onEnd ( async ( ) => {
225
+ await exec ( process . execPath , [
226
+ "./node_modules/@babel/cli/bin/babel.js" ,
227
+ "--plugins" ,
228
+ "@babel/plugin-transform-block-scoping" ,
229
+ preBabel ,
230
+ "--out-file" ,
231
+ postBabel ,
232
+ ] ) ;
233
+
234
+ // Reformatting the code back to reduce the load time difference from main.
235
+ await exec ( "./node_modules/esbuild/bin/esbuild" , [
236
+ postBabel ,
237
+ `--outfile=${ outfile } ` ,
238
+ ] ) ;
239
+ } ) ;
240
+ } ,
241
+ } ) ;
242
+
218
243
return options ;
219
244
} ) ;
220
245
0 commit comments