Skip to content

Commit d804da9

Browse files
committed
Convert __require back to require for external bundlers
1 parent 1f08f8c commit d804da9

File tree

4 files changed

+134
-13
lines changed

4 files changed

+134
-13
lines changed

Gulpfile.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,16 @@ function getCopyrightHeader() {
159159
* @param {boolean} performanceMatters True if this is a bundle where performance matters, so should be optimized at the cost of build time.
160160
*/
161161
function esbuildTask(entrypoint, outfile, exportIsTsObject = false, performanceMatters = false) {
162-
const preBabel = `${outfile}.tmp.js`;
163-
164162
/** @type {esbuild.BuildOptions} */
165163
const options = {
166164
entryPoints: [entrypoint],
167165
banner: { js: getCopyrightHeader() },
168166
bundle: true,
169-
outfile: performanceMatters ? preBabel : outfile,
167+
outfile,
170168
platform: "node",
171169
target: "es2018", // This covers Node 10.
172170
format: "cjs",
173-
sourcemap: true,
171+
sourcemap: "linked",
174172
external: ["./node_modules/*"],
175173
conditions: ["require"],
176174
supported: {
@@ -180,6 +178,30 @@ function esbuildTask(entrypoint, outfile, exportIsTsObject = false, performanceM
180178
// legalComments: "none", // If we add copyright headers to the source files, uncomment.
181179
};
182180

181+
if (performanceMatters) {
182+
const preBabel = `${outfile}.tmp.js`;
183+
options.outfile = preBabel;
184+
options.sourcemap = "inline";
185+
options.plugins = [
186+
{
187+
name: "babel",
188+
setup: (build) => {
189+
build.onEnd(async () => {
190+
await exec(process.execPath, [
191+
"./node_modules/@babel/cli/bin/babel.js",
192+
preBabel,
193+
"--out-file", outfile,
194+
"--plugins", "@babel/plugin-transform-block-scoping,./scripts/build/removeEsbuildRequire",
195+
"--compact", "false",
196+
"--source-maps"]
197+
);
198+
await del(preBabel);
199+
});
200+
},
201+
}
202+
];
203+
}
204+
183205
if (exportIsTsObject) {
184206
options.format = "iife"; // We use an IIFE so we can inject the code below.
185207
options.globalName = "ts"; // Name the variable ts, matching our old big bundle and so we can use the code below.
@@ -200,15 +222,7 @@ function esbuildTask(entrypoint, outfile, exportIsTsObject = false, performanceM
200222
}
201223

202224
return {
203-
build: async () => {
204-
await esbuild.build(options);
205-
if (performanceMatters) {
206-
// TODO(jakebailey): we could use ts.transpileModule for this, but running babel is faster to get this singular transform.
207-
// If we did use ts.transpileModule, we'd need ts.ScriptTarget.ES5, which also will downlevel arrow functions, for-of, spread, etc.
208-
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"]);
209-
await del([preBabel, `${preBabel}.map`]);
210-
}
211-
},
225+
build: () => esbuild.build(options),
212226
clean: () => del([outfile, `${outfile}.map`]),
213227
watch: () => esbuild.build({ ...options, watch: true }),
214228
};

package-lock.json

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@babel/core": "^7.19.3",
4444
"@babel/plugin-transform-block-scoping": "^7.18.9",
4545
"@octokit/rest": "latest",
46+
"@types/babel__core": "^7.1.19",
4647
"@types/chai": "latest",
4748
"@types/fs-extra": "^9.0.13",
4849
"@types/glob": "latest",

scripts/build/removeEsbuildRequire.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = () => {
2+
/** @type {import("@babel/core").PluginObj} */
3+
const plugin = {
4+
name: "remove-esbuild-require",
5+
visitor: {
6+
VariableDeclarator: (path) => {
7+
const id = path.node.id;
8+
if (id.type === "Identifier" && id.name === "__require") {
9+
path.remove();
10+
}
11+
},
12+
CallExpression: (path) => {
13+
// esbuild will transform require to __require, but that may trip up bundlers
14+
// run on our own output, so convert them back,
15+
const callee = path.node.callee;
16+
if (callee.type === "Identifier" && callee.name === "__require") {
17+
callee.name = "require";
18+
}
19+
},
20+
}
21+
};
22+
return plugin;
23+
};

0 commit comments

Comments
 (0)