Make esbuild require hack look better in output (#56060)

This commit is contained in:
Jake Bailey 2023-11-03 09:08:39 -07:00 committed by GitHub
parent e1ef69d99e
commit c5a8636a79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -217,18 +217,22 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
// to be consumable by other bundlers, we need to convert these calls back to
// require so our imports are visible again.
//
// The leading spaces are to keep the offsets the same within the files to keep
// source maps working (though this only really matters for the line the require is on).
// To fix this, we redefine "require" to a name we're unlikely to use with the
// same length as "require", then replace it back to "require" after bundling,
// ensuring that source maps still work.
//
// See: https://github.com/evanw/esbuild/issues/1905
options.define = { require: "$$require" };
const require = "require";
const fakeName = "Q".repeat(require.length);
const fakeNameRegExp = new RegExp(fakeName, "g");
options.define = { [require]: fakeName };
options.plugins = [
{
name: "fix-require",
setup: build => {
build.onEnd(async () => {
let contents = await fs.promises.readFile(outfile, "utf-8");
contents = contents.replace(/\$\$require/g, " require");
contents = contents.replace(fakeNameRegExp, require);
await fs.promises.writeFile(outfile, contents);
});
},