Skip to content

Commit 5119230

Browse files
authored
Enable Node.js/v8 compile caching via entrypoint shims (#59720)
1 parent 25ab551 commit 5119230

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

Herebyfile.mjs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ let printedWatchWarning = false;
303303
* @param {string} options.srcEntrypoint
304304
* @param {string} options.builtEntrypoint
305305
* @param {string} options.output
306+
* @param {boolean} [options.enableCompileCache]
306307
* @param {Task[]} [options.mainDeps]
307308
* @param {BundlerTaskOptions} [options.bundlerOptions]
308309
*/
@@ -313,7 +314,37 @@ function entrypointBuildTask(options) {
313314
run: () => buildProject(options.project),
314315
});
315316

316-
const bundler = createBundler(options.srcEntrypoint, options.output, options.bundlerOptions);
317+
const mainDeps = options.mainDeps?.slice(0) ?? [];
318+
319+
let output = options.output;
320+
if (options.enableCompileCache) {
321+
const originalOutput = output;
322+
output = path.join(path.dirname(output), "_" + path.basename(output));
323+
324+
const compileCacheShim = task({
325+
name: `shim-compile-cache-${options.name}`,
326+
run: async () => {
327+
const outDir = path.dirname(originalOutput);
328+
await fs.promises.mkdir(outDir, { recursive: true });
329+
const moduleSpecifier = path.relative(outDir, output);
330+
const lines = [
331+
`// This file is a shim which defers loading the real module until the compile cache is enabled.`,
332+
`try {`,
333+
` const { enableCompileCache } = require("node:module");`,
334+
` if (enableCompileCache) {`,
335+
` enableCompileCache();`,
336+
` }`,
337+
`} catch {}`,
338+
`module.exports = require("./${moduleSpecifier.replace(/[\\/]/g, "/")}");`,
339+
];
340+
await fs.promises.writeFile(originalOutput, lines.join("\n") + "\n");
341+
},
342+
});
343+
344+
mainDeps.push(compileCacheShim);
345+
}
346+
347+
const bundler = createBundler(options.srcEntrypoint, output, options.bundlerOptions);
317348

318349
// If we ever need to bundle our own output, change this to depend on build
319350
// and run esbuild on builtEntrypoint.
@@ -336,14 +367,13 @@ function entrypointBuildTask(options) {
336367
const shim = task({
337368
name: `shim-${options.name}`,
338369
run: async () => {
339-
const outDir = path.dirname(options.output);
370+
const outDir = path.dirname(output);
340371
await fs.promises.mkdir(outDir, { recursive: true });
341372
const moduleSpecifier = path.relative(outDir, options.builtEntrypoint);
342-
await fs.promises.writeFile(options.output, `module.exports = require("./${moduleSpecifier.replace(/[\\/]/g, "/")}")`);
373+
await fs.promises.writeFile(output, `module.exports = require("./${moduleSpecifier.replace(/[\\/]/g, "/")}")`);
343374
},
344375
});
345376

346-
const mainDeps = options.mainDeps?.slice(0) ?? [];
347377
if (cmdLineOptions.bundle) {
348378
mainDeps.push(bundle);
349379
if (cmdLineOptions.typecheck) {
@@ -392,6 +422,7 @@ const { main: tsc, watch: watchTsc } = entrypointBuildTask({
392422
builtEntrypoint: "./built/local/tsc/tsc.js",
393423
output: "./built/local/tsc.js",
394424
mainDeps: [generateLibs],
425+
enableCompileCache: true,
395426
});
396427
export { tsc, watchTsc };
397428

@@ -429,6 +460,7 @@ const { main: tsserver, watch: watchTsserver } = entrypointBuildTask({
429460
output: "./built/local/tsserver.js",
430461
mainDeps: [generateLibs, services],
431462
bundlerOptions: { usePublicAPI: true },
463+
enableCompileCache: true,
432464
});
433465
export { tsserver, watchTsserver };
434466

@@ -589,6 +621,7 @@ const { main: typingsInstaller, watch: watchTypingsInstaller } = entrypointBuild
589621
output: "./built/local/typingsInstaller.js",
590622
mainDeps: [services],
591623
bundlerOptions: { usePublicAPI: true },
624+
enableCompileCache: true,
592625
});
593626

594627
const { main: watchGuard, watch: watchWatchGuard } = entrypointBuildTask({
@@ -885,12 +918,15 @@ export const produceLKG = task({
885918
const expectedFiles = [
886919
"built/local/cancellationToken.js",
887920
"built/local/tsc.js",
921+
"built/local/_tsc.js",
888922
"built/local/tsserver.js",
923+
"built/local/_tsserver.js",
889924
"built/local/tsserverlibrary.js",
890925
"built/local/tsserverlibrary.d.ts",
891926
"built/local/typescript.js",
892927
"built/local/typescript.d.ts",
893928
"built/local/typingsInstaller.js",
929+
"built/local/_typingsInstaller.js",
894930
"built/local/watchGuard.js",
895931
].concat(libs().map(lib => lib.target));
896932
const missingFiles = expectedFiles

scripts/produceLKG.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@ async function copyTypesMap() {
5050
async function copyScriptOutputs() {
5151
await copyFromBuiltLocal("cancellationToken.js");
5252
await copyFromBuiltLocal("tsc.js");
53+
await copyFromBuiltLocal("_tsc.js");
5354
await copyFromBuiltLocal("tsserver.js");
55+
await copyFromBuiltLocal("_tsserver.js");
5456
await copyFromBuiltLocal("tsserverlibrary.js");
5557
await copyFromBuiltLocal("typescript.js");
5658
await copyFromBuiltLocal("typingsInstaller.js");
59+
await copyFromBuiltLocal("_typingsInstaller.js");
5760
await copyFromBuiltLocal("watchGuard.js");
5861
}
5962

0 commit comments

Comments
 (0)