Skip to content

Commit 13fd5ce

Browse files
committed
Compute esbuild options lazily
1 parent bb0f23b commit 13fd5ce

File tree

1 file changed

+63
-62
lines changed

1 file changed

+63
-62
lines changed

Herebyfile.mjs

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -185,75 +185,76 @@ async function runDtsBundler(entrypoint, output) {
185185
* @param {boolean} exportIsTsObject True if this file exports the TS object and should have relevant code injected.
186186
*/
187187
function esbuildTask(entrypoint, outfile, exportIsTsObject = false) {
188-
// Note: we do not use --minify, as that would hide function names from user backtraces
189-
// (we don't ship our sourcemaps), and would break consumers like monaco which modify
190-
// typescript.js for their own needs. Also, using --sourcesContent=false doesn't help,
191-
// as even though it's a smaller source map that could be shipped to users for better
192-
// stack traces via names, the maps are bigger than the actual source files themselves.
193-
/** @type {esbuild.BuildOptions} */
194-
const options = {
195-
entryPoints: [entrypoint],
196-
banner: { js: getCopyrightHeader() },
197-
bundle: true,
198-
outfile,
199-
platform: "node",
200-
target: "es2018", // Covers Node 10.
201-
format: "cjs",
202-
sourcemap: "linked",
203-
external: ["./node_modules/*"],
204-
conditions: ["require"],
205-
supported: {
206-
// "const-and-let": false, // https://github.com/evanw/esbuild/issues/297
207-
"object-rest-spread": false, // Performance enhancement, see: https://github.com/evanw/esbuild/releases/tag/v0.14.46
208-
},
209-
// legalComments: "none", // If we add copyright headers to the source files, uncomment.
210-
plugins: [
211-
{
212-
name: "fix-require",
213-
setup: (build) => {
214-
build.onEnd(async () => {
215-
// esbuild converts calls to "require" to "__require"; this function
216-
// calls the real require if it exists, or throws if it does not (rather than
217-
// throwing an error like "require not defined"). But, since we want typescript
218-
// to be consumable by other bundlers, we need to convert these calls back to
219-
// require so our imports are visible again.
220-
//
221-
// Note that this step breaks source maps, but only for lines that reference
222-
// "__require", which is an okay tradeoff for the performance of not running
223-
// the output through transpileModule/babel/etc.
224-
//
225-
// See: https://github.com/evanw/esbuild/issues/1905
226-
let contents = await fs.promises.readFile(outfile, "utf-8");
227-
contents = contents.replace(/__require\(/g, "require(");
228-
await fs.promises.writeFile(outfile, contents);
229-
});
188+
return {
189+
build: async () => {
190+
// Note: we do not use --minify, as that would hide function names from user backtraces
191+
// (we don't ship our sourcemaps), and would break consumers like monaco which modify
192+
// typescript.js for their own needs. Also, using --sourcesContent=false doesn't help,
193+
// as even though it's a smaller source map that could be shipped to users for better
194+
// stack traces via names, the maps are bigger than the actual source files themselves.
195+
/** @type {esbuild.BuildOptions} */
196+
const options = {
197+
entryPoints: [entrypoint],
198+
banner: { js: getCopyrightHeader() },
199+
bundle: true,
200+
outfile,
201+
platform: "node",
202+
target: "es2018", // Covers Node 10.
203+
format: "cjs",
204+
sourcemap: "linked",
205+
external: ["./node_modules/*"],
206+
conditions: ["require"],
207+
supported: {
208+
// "const-and-let": false, // https://github.com/evanw/esbuild/issues/297
209+
"object-rest-spread": false, // Performance enhancement, see: https://github.com/evanw/esbuild/releases/tag/v0.14.46
230210
},
231-
}
232-
]
233-
};
234-
235-
if (exportIsTsObject) {
236-
options.format = "iife"; // We use an IIFE so we can inject the code below.
237-
options.globalName = "ts"; // Name the variable ts, matching our old big bundle and so we can use the code below.
238-
options.footer = {
239-
// These snippets cannot appear in the actual source files, otherwise they will be rewritten
240-
// to things like exports or requires.
241-
js: `
211+
// legalComments: "none", // If we add copyright headers to the source files, uncomment.
212+
plugins: [
213+
{
214+
name: "fix-require",
215+
setup: (build) => {
216+
build.onEnd(async () => {
217+
// esbuild converts calls to "require" to "__require"; this function
218+
// calls the real require if it exists, or throws if it does not (rather than
219+
// throwing an error like "require not defined"). But, since we want typescript
220+
// to be consumable by other bundlers, we need to convert these calls back to
221+
// require so our imports are visible again.
222+
//
223+
// Note that this step breaks source maps, but only for lines that reference
224+
// "__require", which is an okay tradeoff for the performance of not running
225+
// the output through transpileModule/babel/etc.
226+
//
227+
// See: https://github.com/evanw/esbuild/issues/1905
228+
let contents = await fs.promises.readFile(outfile, "utf-8");
229+
contents = contents.replace(/__require\(/g, "require(");
230+
await fs.promises.writeFile(outfile, contents);
231+
});
232+
},
233+
}
234+
]
235+
};
236+
237+
if (exportIsTsObject) {
238+
options.format = "iife"; // We use an IIFE so we can inject the code below.
239+
options.globalName = "ts"; // Name the variable ts, matching our old big bundle and so we can use the code below.
240+
options.footer = {
241+
// These snippets cannot appear in the actual source files, otherwise they will be rewritten
242+
// to things like exports or requires.
243+
js: `
242244
if (typeof module !== "undefined" && module.exports) {
243-
// If we are in a CJS context, export the ts namespace.
244-
module.exports = ts;
245+
// If we are in a CJS context, export the ts namespace.
246+
module.exports = ts;
245247
}
246248
if (ts.server) {
247-
// If we are in a server bundle, inject the dynamicImport function.
248-
ts.server.dynamicImport = id => import(id);
249+
// If we are in a server bundle, inject the dynamicImport function.
250+
ts.server.dynamicImport = id => import(id);
249251
}`
250-
};
251-
}
252+
};
253+
}
252254

253-
return {
254-
build: () => esbuild.build(options),
255+
await esbuild.build(options);
256+
},
255257
clean: () => del([outfile, `${outfile}.map`]),
256-
watch: () => esbuild.build({ ...options, watch: true }),
257258
};
258259
}
259260

0 commit comments

Comments
 (0)