Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Node.js/v8 compile caching via entrypoint shims #59720

Merged
merged 8 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions Herebyfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ let printedWatchWarning = false;
* @param {string} options.srcEntrypoint
* @param {string} options.builtEntrypoint
* @param {string} options.output
* @param {boolean} [options.enableCompileCache]
* @param {Task[]} [options.mainDeps]
* @param {BundlerTaskOptions} [options.bundlerOptions]
*/
Expand All @@ -313,7 +314,37 @@ function entrypointBuildTask(options) {
run: () => buildProject(options.project),
});

const bundler = createBundler(options.srcEntrypoint, options.output, options.bundlerOptions);
const mainDeps = options.mainDeps?.slice(0) ?? [];

let output = options.output;
if (options.enableCompileCache) {
const originalOutput = output;
output = path.join(path.dirname(output), "_" + path.basename(output));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just an expedient name choice; I can also remove the extension and then make it like tsc_.js which may sort better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say that _tsc tab-completes better on the command-line, though, because it doesn't interfere with types<TAB> -- but there's so much stuff in built/local that it doesn't actually make a difference.


const compileCacheShim = task({
name: `shim-compile-cache-${options.name}`,
run: async () => {
const outDir = path.dirname(originalOutput);
await fs.promises.mkdir(outDir, { recursive: true });
const moduleSpecifier = path.relative(outDir, output);
const lines = [
`// This file is a shim which defers loading the real module until the compile cache is enabled.`,
`try {`,
` const { enableCompileCache } = require("node:module");`,
` if (enableCompileCache) {`,
` enableCompileCache();`,
` }`,
`} catch {}`,
`module.exports = require("./${moduleSpecifier.replace(/[\\/]/g, "/")}");`,
];
await fs.promises.writeFile(originalOutput, lines.join("\n") + "\n");
},
});

mainDeps.push(compileCacheShim);
}

const bundler = createBundler(options.srcEntrypoint, output, options.bundlerOptions);

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

const mainDeps = options.mainDeps?.slice(0) ?? [];
if (cmdLineOptions.bundle) {
mainDeps.push(bundle);
if (cmdLineOptions.typecheck) {
Expand Down Expand Up @@ -392,6 +422,7 @@ const { main: tsc, watch: watchTsc } = entrypointBuildTask({
builtEntrypoint: "./built/local/tsc/tsc.js",
output: "./built/local/tsc.js",
mainDeps: [generateLibs],
enableCompileCache: true,
});
export { tsc, watchTsc };

Expand Down Expand Up @@ -429,6 +460,7 @@ const { main: tsserver, watch: watchTsserver } = entrypointBuildTask({
output: "./built/local/tsserver.js",
mainDeps: [generateLibs, services],
bundlerOptions: { usePublicAPI: true },
enableCompileCache: true,
});
export { tsserver, watchTsserver };

Expand Down Expand Up @@ -589,6 +621,7 @@ const { main: typingsInstaller, watch: watchTypingsInstaller } = entrypointBuild
output: "./built/local/typingsInstaller.js",
mainDeps: [services],
bundlerOptions: { usePublicAPI: true },
enableCompileCache: true,
});

const { main: watchGuard, watch: watchWatchGuard } = entrypointBuildTask({
Expand Down Expand Up @@ -885,12 +918,15 @@ export const produceLKG = task({
const expectedFiles = [
"built/local/cancellationToken.js",
"built/local/tsc.js",
"built/local/_tsc.js",
"built/local/tsserver.js",
"built/local/_tsserver.js",
"built/local/tsserverlibrary.js",
"built/local/tsserverlibrary.d.ts",
"built/local/typescript.js",
"built/local/typescript.d.ts",
"built/local/typingsInstaller.js",
"built/local/_typingsInstaller.js",
"built/local/watchGuard.js",
].concat(libs().map(lib => lib.target));
const missingFiles = expectedFiles
Expand Down
3 changes: 3 additions & 0 deletions scripts/produceLKG.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ async function copyTypesMap() {
async function copyScriptOutputs() {
await copyFromBuiltLocal("cancellationToken.js");
await copyFromBuiltLocal("tsc.js");
await copyFromBuiltLocal("_tsc.js");
await copyFromBuiltLocal("tsserver.js");
await copyFromBuiltLocal("_tsserver.js");
await copyFromBuiltLocal("tsserverlibrary.js");
await copyFromBuiltLocal("typescript.js");
await copyFromBuiltLocal("typingsInstaller.js");
await copyFromBuiltLocal("_typingsInstaller.js");
await copyFromBuiltLocal("watchGuard.js");
}

Expand Down