Skip to content

Commit 1a42515

Browse files
committed
Run babel over output for let/const to var
1 parent a89f416 commit 1a42515

File tree

3 files changed

+1514
-97
lines changed

3 files changed

+1514
-97
lines changed

Herebyfile.mjs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,16 @@ async function runDtsBundler(entrypoint, output) {
164164
* @property {() => void} [onWatchRebuild]
165165
*/
166166
function createBundler(entrypoint, outfile, taskOptions = {}) {
167+
const preBabel = `${outfile.replace(/\.js$/, "")}.preBabel.js`;
168+
const postBabel = `${outfile.replace(/\.js$/, "")}.postBabel.js`;
169+
167170
const getOptions = memoize(async () => {
168171
/** @type {esbuild.BuildOptions} */
169172
const options = {
170173
entryPoints: [entrypoint],
171174
banner: { js: await copyright() },
172175
bundle: true,
173-
outfile,
176+
outfile: preBabel,
174177
platform: "node",
175178
target: "es2018",
176179
format: "cjs",
@@ -206,15 +209,37 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
206209
name: "fix-require",
207210
setup: (build) => {
208211
build.onEnd(async () => {
209-
let contents = await fs.promises.readFile(outfile, "utf-8");
212+
let contents = await fs.promises.readFile(preBabel, "utf-8");
210213
contents = contents.replace(/\$\$require/g, " require");
211-
await fs.promises.writeFile(outfile, contents);
214+
await fs.promises.writeFile(preBabel, contents);
212215
});
213216
},
214-
}
217+
},
215218
];
216219
}
217220

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+
218243
return options;
219244
});
220245

0 commit comments

Comments
 (0)