Skip to content

Commit 9576473

Browse files
committed
Convert __require back to require for external bundlers
1 parent 5dd52e5 commit 9576473

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
@@ -158,18 +158,16 @@ function getCopyrightHeader() {
158158
* @param {boolean} performanceMatters True if this is a bundle where performance matters, so should be optimized at the cost of build time.
159159
*/
160160
function esbuildTask(entrypoint, outfile, exportIsTsObject = false, performanceMatters = false) {
161-
const preBabel = `${outfile}.tmp.js`;
162-
163161
/** @type {esbuild.BuildOptions} */
164162
const options = {
165163
entryPoints: [entrypoint],
166164
banner: { js: getCopyrightHeader() },
167165
bundle: true,
168-
outfile: performanceMatters ? preBabel : outfile,
166+
outfile,
169167
platform: "node",
170168
target: "es2018", // This covers Node 10.
171169
format: "cjs",
172-
sourcemap: true,
170+
sourcemap: "linked",
173171
external: ["./node_modules/*"],
174172
conditions: ["require"],
175173
supported: {
@@ -179,6 +177,30 @@ function esbuildTask(entrypoint, outfile, exportIsTsObject = false, performanceM
179177
// legalComments: "none", // If we add copyright headers to the source files, uncomment.
180178
};
181179

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

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

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)