From b93c12566af6006fce9c8d54e0dd3476b6837af1 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 22 Aug 2024 10:02:37 -0700 Subject: [PATCH 1/6] Add support for module.enableCompileCache --- Herebyfile.mjs | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index 2fb6b02a33ffe..2d00360ffda41 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -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] */ @@ -313,7 +314,31 @@ 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)); + + 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 = [ + `require("node:module").enableCompileCache?.();`, + `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. @@ -336,14 +361,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) { @@ -392,6 +416,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 }; @@ -429,6 +454,7 @@ const { main: tsserver, watch: watchTsserver } = entrypointBuildTask({ output: "./built/local/tsserver.js", mainDeps: [generateLibs, services], bundlerOptions: { usePublicAPI: true }, + enableCompileCache: true, }); export { tsserver, watchTsserver }; @@ -589,6 +615,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({ From 809c051274db8342430ed0c5d8836f84f859809b Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 22 Aug 2024 10:17:22 -0700 Subject: [PATCH 2/6] Use plain if statement --- Herebyfile.mjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index 2d00360ffda41..331ac7d2c11bc 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -328,7 +328,10 @@ function entrypointBuildTask(options) { await fs.promises.mkdir(outDir, { recursive: true }); const moduleSpecifier = path.relative(outDir, output); const lines = [ - `require("node:module").enableCompileCache?.();`, + `const m = require("node:module");`, + `if (m.enableCompileCache) {`, + ` m.enableCompileCache();`, + `}`, `module.exports = require("./${moduleSpecifier.replace(/[\\/]/g, "/")}");`, ]; await fs.promises.writeFile(originalOutput, lines.join("\n") + "\n"); From 4aa0aa0084b877d8ee8374b04ce8bc66516e759d Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 22 Aug 2024 10:47:57 -0700 Subject: [PATCH 3/6] Fix LKG --- Herebyfile.mjs | 3 +++ scripts/produceLKG.mjs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index 331ac7d2c11bc..e9ffa107b2445 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -915,12 +915,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 diff --git a/scripts/produceLKG.mjs b/scripts/produceLKG.mjs index 3a71e99011312..2f654d668a0a5 100644 --- a/scripts/produceLKG.mjs +++ b/scripts/produceLKG.mjs @@ -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"); } From d9bb92143b561c32e1770bfb52a357de371affd8 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Thu, 22 Aug 2024 14:19:38 -0700 Subject: [PATCH 4/6] Use recommended form --- Herebyfile.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index e9ffa107b2445..d4a6b40c40019 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -328,9 +328,9 @@ function entrypointBuildTask(options) { await fs.promises.mkdir(outDir, { recursive: true }); const moduleSpecifier = path.relative(outDir, output); const lines = [ - `const m = require("node:module");`, - `if (m.enableCompileCache) {`, - ` m.enableCompileCache();`, + `const { enableCompileCache } = require("node:module");`, + `if (enableCompileCache) {`, + ` enableCompileCache();`, `}`, `module.exports = require("./${moduleSpecifier.replace(/[\\/]/g, "/")}");`, ]; From f288c917a5c9a08bf64af87b1124b07433653028 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Sun, 8 Sep 2024 16:26:14 -0700 Subject: [PATCH 5/6] Add comment --- Herebyfile.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index d4a6b40c40019..b43ae6187be31 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -328,6 +328,7 @@ function entrypointBuildTask(options) { 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.`, `const { enableCompileCache } = require("node:module");`, `if (enableCompileCache) {`, ` enableCompileCache();`, From cb268bb89117e32ed295cdf76906bfc8c7020d2f Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Sun, 8 Sep 2024 16:28:34 -0700 Subject: [PATCH 6/6] Try catch --- Herebyfile.mjs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index b43ae6187be31..402f7917cf34a 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -329,10 +329,12 @@ function entrypointBuildTask(options) { 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.`, - `const { enableCompileCache } = require("node:module");`, - `if (enableCompileCache) {`, - ` enableCompileCache();`, - `}`, + `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");