|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const fs = require("node:fs"); |
| 4 | +const { enableCompileCache } = require("node:module"); |
| 5 | +const os = require("node:os"); |
| 6 | +const path = require("node:path"); |
| 7 | +const { run } = require("../../utils/test-utils"); |
| 8 | + |
| 9 | +/** |
| 10 | + * Runs the CLI on a configuration that reports the compile cache directory of the |
| 11 | + * child process and the worker thread it starts. |
| 12 | + * @param {Record<string, string>} env variables to set on top of a clean environment |
| 13 | + * @returns {Promise<{ variable: string | null, child: string | null, worker: string | null }>} what it reported |
| 14 | + */ |
| 15 | +const report = async (env) => { |
| 16 | + const cleanEnv = Object.fromEntries( |
| 17 | + Object.entries(process.env).filter( |
| 18 | + ([name]) => !name.startsWith("NODE_COMPILE_CACHE") && name !== "NODE_DISABLE_COMPILE_CACHE", |
| 19 | + ), |
| 20 | + ); |
| 21 | + const { exitCode, stdout } = await run(__dirname, [], { |
| 22 | + env: { ...cleanEnv, ...env }, |
| 23 | + extendEnv: false, |
| 24 | + }); |
| 25 | + |
| 26 | + expect(exitCode).toBe(0); |
| 27 | + |
| 28 | + return JSON.parse(stdout.slice(stdout.indexOf("REPORT") + 6).split("\n")[0]); |
| 29 | +}; |
| 30 | + |
| 31 | +describe("compile cache", () => { |
| 32 | + // `module.enableCompileCache` is only available on Node.js >= 22.8.0 |
| 33 | + if (!enableCompileCache) { |
| 34 | + it("should hand nothing on without a compile cache", async () => { |
| 35 | + expect(await report({})).toEqual({ variable: null, child: null, worker: null }); |
| 36 | + }); |
| 37 | + |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + it("should hand the compile cache to child processes and worker threads", async () => { |
| 42 | + const { variable, child, worker } = await report({}); |
| 43 | + |
| 44 | + expect(typeof variable).toBe("string"); |
| 45 | + expect(child.startsWith(variable)).toBe(true); |
| 46 | + expect(worker.startsWith(variable)).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should keep a directory the environment already names", async () => { |
| 50 | + const directory = fs.mkdtempSync(path.join(os.tmpdir(), "webpack-cli-compile-cache-")); |
| 51 | + |
| 52 | + try { |
| 53 | + const { variable, child, worker } = await report({ NODE_COMPILE_CACHE: directory }); |
| 54 | + |
| 55 | + expect(variable).toBe(directory); |
| 56 | + expect(child.startsWith(directory)).toBe(true); |
| 57 | + expect(worker.startsWith(directory)).toBe(true); |
| 58 | + } finally { |
| 59 | + fs.rmSync(directory, { recursive: true, force: true }); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it("should hand nothing on where the compile cache is disabled", async () => { |
| 64 | + expect(await report({ NODE_DISABLE_COMPILE_CACHE: "1" })).toEqual({ |
| 65 | + variable: null, |
| 66 | + child: null, |
| 67 | + worker: null, |
| 68 | + }); |
| 69 | + }); |
| 70 | +}); |
0 commit comments