diff --git a/.changeset/write-to-disk-output-clean.md b/.changeset/write-to-disk-output-clean.md new file mode 100644 index 000000000..019e39a0d --- /dev/null +++ b/.changeset/write-to-disk-output-clean.md @@ -0,0 +1,5 @@ +--- +"webpack-dev-middleware": patch +--- + +Clean the files `writeToDisk` writes when `output.clean` is set. webpack wrote the build to memory and cleaned only that, so the copies on disk were never cleaned: the previous session's files stayed, and every rebuild left another `main.[contenthash].js` behind. The middleware now gives webpack a file system that writes to memory and, for the files `writeToDisk` takes, to disk, and that removes and lists files on both, so `output.clean` cleans the disk too, honoring `clean.keep` and `clean.dry`, in plugin mode as in standalone mode, with every webpack 5 version and with Rspack. `writeToDisk: true` with a file system of your own, in `outputFileSystem` or on the compiler, now writes to disk too: it wrote nothing there before. diff --git a/.cspell.json b/.cspell.json index 4b3899365..7b0c058e1 100644 --- a/.cspell.json +++ b/.cspell.json @@ -49,7 +49,8 @@ "valuenow", "webworker", "wildcarded", - "Fapp" + "Fapp", + "gracefulify" ], "ignorePaths": [ "CHANGELOG.md", diff --git a/README.md b/README.md index e10afcc9e..018d010f6 100644 --- a/README.md +++ b/README.md @@ -293,13 +293,15 @@ middleware(compiler, { }); ``` +With either value, [`output.clean`](https://webpack.js.org/configuration/output/#outputclean) cleans the files on disk as it cleans webpack's output: the directory on the first build, the files a rebuild no longer emits after that, with `clean.keep` and `clean.dry` honored. The files the function leaves out stay off disk, and a copy of one left there by an earlier session is removed. Links, permissions and times are written to disk as webpack writes them. With a file system of your own, given in `outputFileSystem` or set on the compiler, the files are written to it and to disk, and both are cleaned; one lacking methods webpack does without, such as `symlink`, works too. This works with every webpack 5 version and with Rspack. An asset named with a query string, such as `[name].js?[contenthash]`, is left on disk once it is gone, as it is without the middleware, until webpack's `output.clean` matches such a name to its file. + ### outputFileSystem Type: `Object` Default: [memfs](https://github.com/streamich/memfs) Set the default file system which will be used by webpack as primary destination of generated files. -This option isn't affected by the [writeToDisk](#writeToDisk) option. +This option isn't affected by the [writeToDisk](#writetodisk) option. With `writeToDisk` set, files are written to it as before and also to disk — with `true` as with a function. You have to provide `.join()` and `mkdirp` method to the `outputFileSystem` instance manually for compatibility with `webpack@4`. diff --git a/package-lock.json b/package-lock.json index 10db8bf9c..140e1605f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,6 +36,7 @@ "babel-loader": "^10.1.1", "babel-plugin-istanbul": "^8.0.0", "connect": "^3.7.0", + "copy-webpack-plugin": "^14.0.0", "cspell": "^10.2.0", "deepmerge": "^4.3.1", "del-cli": "^8.0.1", @@ -9445,6 +9446,43 @@ "node": ">= 0.8" } }, + "node_modules/copy-webpack-plugin": { + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/copy-webpack-plugin/-/copy-webpack-plugin-14.0.0.tgz", + "integrity": "sha512-3JLW90aBGeaTLpM7mYQKpnVdgsUZRExY55giiZgLuX/xTQRUs1dOCwbBnWnvY6Q6rfZoXMNwzOQJCSZPppfqXA==", + "dev": true, + "license": "MIT", + "dependencies": { + "glob-parent": "^6.0.1", + "normalize-path": "^3.0.0", + "schema-utils": "^4.2.0", + "serialize-javascript": "^7.0.3", + "tinyglobby": "^0.2.12" + }, + "engines": { + "node": ">= 20.9.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/webpack" + }, + "peerDependencies": { + "webpack": "^5.1.0" + } + }, + "node_modules/copy-webpack-plugin/node_modules/glob-parent": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", + "integrity": "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==", + "dev": true, + "license": "ISC", + "dependencies": { + "is-glob": "^4.0.3" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/core-js-compat": { "version": "3.50.0", "resolved": "https://registry.npmjs.org/core-js-compat/-/core-js-compat-3.50.0.tgz", @@ -18993,6 +19031,16 @@ "url": "https://opencollective.com/express" } }, + "node_modules/serialize-javascript": { + "version": "7.1.4", + "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-7.1.4.tgz", + "integrity": "sha512-FqlEuoTq/zRmA4Lg6aOqyH3lCHbUtCB0bmHCeFniX2GBdDqMJM3FwdvEhlE5Pim1dlt2FfBdVPmUNNiGhhKqpw==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=20.0.0" + } + }, "node_modules/serve-static": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-2.2.1.tgz", diff --git a/package.json b/package.json index f5f698ab2..b3982a284 100644 --- a/package.json +++ b/package.json @@ -115,6 +115,7 @@ "babel-loader": "^10.1.1", "babel-plugin-istanbul": "^8.0.0", "connect": "^3.7.0", + "copy-webpack-plugin": "^14.0.0", "cspell": "^10.2.0", "deepmerge": "^4.3.1", "del-cli": "^8.0.1", diff --git a/src/index.js b/src/index.js index a3c2fc4c9..3bc051c84 100644 --- a/src/index.js +++ b/src/index.js @@ -21,6 +21,7 @@ const noop = () => {}; /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ /** @typedef {import("webpack").MultiCompiler} MultiCompiler */ +/** @typedef {import("webpack").Compilation} Compilation */ /** @typedef {import("webpack").Configuration} Configuration */ /** @typedef {import("webpack").Stats} Stats */ /** @typedef {import("webpack").MultiStats} MultiStats */ @@ -522,66 +523,284 @@ function printStats(stats, context) { const PLUGIN_NAME = "DevMiddleware"; /** + * Whether a file system is the disk: Node.js `fs`, as Rspack's output file + * system, or `graceful-fs` wrapping it, as webpack's. + * @param {OutputFileSystem} fileSystem a file system + * @returns {boolean} true when it writes to disk + */ +function isDiskFileSystem(fileSystem) { + return ( + /** @type {unknown} */ (fileSystem) === fs || + typeof (/** @type {EXPECTED_ANY} */ (fileSystem).gracefulify) === "function" + ); +} + +// TODO in the next major release replace this file system with one of: +// - a webpack hook for additional outputs, where the middleware adds the disk +// with `writeToDisk` as its filter and webpack writes and cleans both itself; +// - a file system helper webpack exports, mirroring one file system to another; +// - `writeToDisk: true` only, with the disk as webpack's only output and files +// served from it; deprecate the function form first, as it needs this layer. +/** + * The output file system webpack is given when `writeToDisk` writes to disk + * besides the file system files are served from. Writes go to that one and, + * for the paths `writeToDisk` takes, to disk; removals reach both, and + * listings and `lstat` see the disk too. So webpack's own `output.clean` + * cleans the disk as it cleans its output. What webpack reads to decide + * whether to write a file comes from the served file system alone. * @template {IncomingMessage} Request * @template {ServerResponse} Response - * @param {Compiler} compiler compiler + * @param {OutputFileSystem} outputFileSystem the file system files are served from * @param {WithOptional, "watching" | "outputFileSystem">} context context + * @param {Compiler[]} compilers the compilers given this file system + * @returns {OutputFileSystem} the file system writing to both */ -function hookForWriteToDisk(compiler, context) { - compiler.hooks.emit.tap(PLUGIN_NAME, () => { - // @ts-expect-error - if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) { - return; +function createDiskWritingFileSystem(outputFileSystem, context, compilers) { + /** @type {EXPECTED_ANY} */ + const served = outputFileSystem; + /** @type {WeakMap} */ + const compilations = new WeakMap(); + + for (const compiler of compilers) { + compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { + compilations.set(compiler, compilation); + }); + } + + /** + * @param {string} targetPath absolute path of a file + * @returns {boolean} true when it is written to disk + */ + const takes = (targetPath) => { + const { writeToDisk } = context.options; + + return typeof writeToDisk === "function" ? writeToDisk(targetPath) : true; + }; + + /** + * Whether `output.clean` keeps a file in a compiler's output, decided as + * `CleanPlugin` decides it: its `keep` hook first, then `clean.keep`. + * @param {Compiler} compiler compiler + * @param {string} file path relative to the output, with `/` separators + * @returns {boolean} true when it is kept + */ + const isKept = (compiler, file) => { + const { clean } = compiler.options.output; + + if (!clean || (typeof clean === "object" && clean.dry)) { + return true; } - compiler.hooks.assetEmitted.tapAsync( - PLUGIN_NAME, - (file, info, callback) => { - const { targetPath, content } = info; - const { writeToDisk: filter } = context.options; - const allowWrite = - filter && typeof filter === "function" ? filter(targetPath) : true; + const compilation = compilations.get(compiler); + const { CleanPlugin } = compiler.webpack; + const byHook = + compilation && CleanPlugin && CleanPlugin.getCompilationHooks + ? CleanPlugin.getCompilationHooks(compilation).keep.call(file) + : undefined; - if (!allowWrite) { - return callback(); + if (byHook !== undefined) { + return byHook; + } + + const keep = typeof clean === "object" ? clean.keep : undefined; + + if (keep === undefined) { + return false; + } + + if (typeof keep === "string") { + return file.startsWith(keep); + } + + if (keep instanceof RegExp) { + return keep.test(file); + } + + if (typeof keep === "function") { + return Boolean(keep(file)); + } + + // A glob condition is not matched here, so what it might keep is kept. + return true; + }; + + /** + * Whether `output.clean` would remove a disk copy, left by an earlier + * session, of a file `writeToDisk` leaves out: one webpack counts as current. + * @param {string} targetPath absolute path of a file + * @returns {boolean} true when it is cleaned + */ + const isCleaned = (targetPath) => + compilers.some((compiler) => { + const file = path.relative(compiler.outputPath, targetPath); + + return ( + !file.startsWith("..") && + !path.isAbsolute(file) && + !isKept(compiler, file.replaceAll("\\", "/")) + ); + }); + + /** + * A write on the served file system, repeated on disk for the paths + * `writeToDisk` takes once it succeeded there. + * @param {"writeFile" | "symlink" | "chmod" | "utimes"} name the method + * @param {number} pathIndex which argument is the path written + * @returns {(...args: EXPECTED_ANY[]) => void} the method + */ + const mirror = + (name, pathIndex) => + (...args) => { + const callback = args.pop(); + const targetPath = args[pathIndex]; + + served[name](...args, (/** @type {Error | null=} */ err) => { + if (err) { + return callback(err); + } + + if (!takes(targetPath)) { + return isCleaned(targetPath) + ? fs.unlink(targetPath, () => callback()) + : callback(); } const dir = path.dirname(targetPath); - const name = compiler.options.name - ? `Child "${compiler.options.name}": ` - : ""; + const write = () => + /** @type {EXPECTED_ANY} */ (fs)[name]( + ...args, + (/** @type {Error | null} */ writeError) => { + if (writeError) { + context.logger.error( + `Unable to write "${targetPath}" asset to disk:\n${writeError}`, + ); + + return callback(writeError); + } + + context.logger.log(`Asset written to disk: "${targetPath}"`); - return fs.mkdir(dir, { recursive: true }, (mkdirError) => { + callback(); + }, + ); + + fs.mkdir(dir, { recursive: true }, (mkdirError) => { if (mkdirError) { context.logger.error( - `${name}Unable to write "${dir}" directory to disk:\n${mkdirError}`, + `Unable to write "${dir}" directory to disk:\n${mkdirError}`, ); return callback(mkdirError); } - return fs.writeFile(targetPath, content, (writeFileError) => { - if (writeFileError) { - context.logger.error( - `${name}Unable to write "${targetPath}" asset to disk:\n${writeFileError}`, - ); - - return callback(writeFileError); - } + // A link replaces whatever was there, as webpack replaces it. + if (name === "symlink") { + return fs.unlink(targetPath, write); + } - context.logger.log(`${name}Asset written to disk: "${targetPath}"`); + write(); + }); + }); + }; - return callback(); + /** + * A removal on both, done once either removed it. + * @param {"unlink" | "rmdir"} name the method + * @returns {(targetPath: string, callback: (err?: Error | null) => void) => void} the method + */ + const removeFromBoth = (name) => (targetPath, callback) => { + const run = (/** @type {EXPECTED_ANY} */ fileSystem) => + new Promise((resolve) => { + if (fileSystem[name]) { + fileSystem[name](targetPath, (/** @type {Error | null} */ err) => { + resolve(err || null); }); - }); - }, - ); + } else { + resolve(undefined); + } + }); - // @ts-expect-error - compiler.hasWebpackDevMiddlewareAssetEmittedCallback = true; - }); + Promise.all([run(served), run(fs)]).then((errors) => { + callback(errors.includes(null) ? null : errors.find(Boolean)); + }); + }; + + // What is not here, such as `readFile`, `stat` and `mkdir`, reaches the + // served file system alone; `lstat` and `readdir` see the disk too, which is + // how `output.clean` finds what an earlier session left there. + /** @type {Record} */ + const methods = { + writeFile: mirror("writeFile", 0), + unlink: removeFromBoth("unlink"), + rmdir: removeFromBoth("rmdir"), + lstat: ( + /** @type {string} */ targetPath, + /** @type {(err: Error | null, stats?: EXPECTED_ANY) => void} */ callback, + ) => + (served.lstat || served.stat).call( + served, + targetPath, + ( + /** @type {NodeJS.ErrnoException | null} */ err, + /** @type {EXPECTED_ANY} */ stats, + ) => + err && err.code === "ENOENT" + ? fs.lstat(targetPath, callback) + : callback(err, stats), + ), + readdir: ( + /** @type {string} */ targetPath, + /** @type {(err: Error | null, entries?: string[]) => void} */ callback, + ) => { + /** + * @param {Error | null} servedError error from the served file system + * @param {string[]=} servedEntries entries on the served file system + * @returns {void} + */ + const withDisk = (servedError, servedEntries) => + fs.readdir(targetPath, (diskError, diskEntries) => + servedError && diskError + ? callback(servedError) + : callback(null, [ + ...new Set([...(servedEntries || []), ...(diskEntries || [])]), + ]), + ); + + if (!served.readdir) { + return withDisk(null, []); + } + + served.readdir(targetPath, withDisk); + }, + }; + + // Methods webpack can do without stay out where the served file system lacks them. + for (const [name, pathIndex] of /** @type {const} */ ([ + ["symlink", 1], + ["chmod", 0], + ["utimes", 0], + ])) { + if (served[name]) { + methods[name] = mirror(name, pathIndex); + } + } + + return /** @type {OutputFileSystem} */ ( + Object.create( + outputFileSystem, + Object.fromEntries( + Object.entries(methods).map(([name, value]) => [name, { value }]), + ), + ) + ); } +// TODO in the next major release remove the standalone mode, where the +// middleware starts `compiler.watch()` and prints stats itself, and keep only +// the plugin one, where the host (webpack-cli, webpack-dev-server) owns +// watching and stats, as webpack-dev-server uses it: drop `isPlugin` here and +// `usePlugin` from the framework wrappers, with the watching, `close()` and +// `printStats` paths that exist only for standalone use. /** * @template {IncomingMessage} [RequestInternal=IncomingMessage] * @template {ServerResponse} [ResponseInternal=ServerResponse] @@ -695,12 +914,6 @@ function wdm(compiler, options = {}, isPlugin = false) { ); } - if (typeof options.writeToDisk === "function") { - for (const compiler of compilersToModify) { - hookForWriteToDisk(compiler, context); - } - } - // Modify output file system /** @type {OutputFileSystem} */ let outputFileSystem; @@ -728,11 +941,24 @@ function wdm(compiler, options = {}, isPlugin = false) { context.outputFileSystem = outputFileSystem; + // Written to disk besides the served file system unless that is the disk + // itself: with `true` and a file system of your own, on the compiler or in + // `outputFileSystem`, nothing was written to disk before. + const compilerOutputFileSystem = + options.writeToDisk && !isDiskFileSystem(outputFileSystem) + ? createDiskWritingFileSystem( + outputFileSystem, + context, + compilersToModify, + ) + : outputFileSystem; + for (const compiler of compilersToModify) { - compiler.outputFileSystem = outputFileSystem; + compiler.outputFileSystem = compilerOutputFileSystem; } // Start watching, but only for standalone usage, for plugin usage stats will be printed by external code, for example - webpack-cli + // TODO in the next major release remove, with the standalone mode (see `wdm`). if (!isPlugin) { /** * @param {Error | null} err err diff --git a/test/write-to-disk-clean.test.js b/test/write-to-disk-clean.test.js new file mode 100644 index 000000000..70e6dd816 --- /dev/null +++ b/test/write-to-disk-clean.test.js @@ -0,0 +1,1321 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +import CopyPlugin from "copy-webpack-plugin"; +import { Volume, createFsFromVolume } from "memfs"; +import request from "supertest"; +import webpack from "webpack"; + +import middleware from "../src"; + +jest.spyOn(globalThis.console, "log").mockImplementation(); + +// Links and permissions need privileges or mean nothing on Windows. +const describeWithLinksAndPermissions = + os.platform() === "win32" ? describe.skip : describe; + +// `output.clean` with `writeToDisk`: the files on disk are cleaned as webpack +// cleans its output, in standalone mode and in plugin mode alike — with +// `writeToDisk: true` webpack does it itself, as the real file system is its +// output file system; with a function, webpack writes through a file system +// writing to memory and to disk, and cleans both. +describe("output.clean with writeToDisk", () => { + /** @type {string} */ + let directory; + /** @type {string} */ + let dist; + + beforeEach(() => { + directory = fs.mkdtempSync(path.join(os.tmpdir(), "wdm-clean-")); + dist = path.join(directory, "dist"); + fs.writeFileSync(path.join(directory, "index.js"), "console.log(1);"); + fs.mkdirSync(path.join(dist, "nested"), { recursive: true }); + // Left from an earlier session, as webpack would find them. + fs.writeFileSync(path.join(dist, "stale.js"), "old"); + fs.writeFileSync(path.join(dist, "nested", "stale.css"), "old"); + }); + + afterEach(() => { + fs.rmSync(directory, { recursive: true, force: true }); + }); + + /** + * @param {import("webpack").Configuration["output"]} output output options + * @param {import("webpack").Configuration["plugins"]=} plugins plugins + * @returns {import("webpack").Compiler} compiler + */ + function createCompiler(output, plugins = []) { + return webpack({ + mode: "development", + devtool: false, + context: directory, + entry: "./index.js", + output: { path: dist, filename: "main.[contenthash].js", ...output }, + plugins, + infrastructureLogging: { level: "error" }, + stats: "none", + }); + } + + /** + * @returns {string[]} the files in `dist`, relative to it + */ + function listDist() { + // Paths as strings rather than `Dirent`s: `Dirent#parentPath` is newer + // than the oldest Node.js this package supports. + return fs + .readdirSync(dist, { recursive: true, encoding: "utf8" }) + .filter((file) => fs.statSync(path.join(dist, file)).isFile()) + .map((file) => file.replaceAll("\\", "/")) + .toSorted(); + } + + /** + * Start the middleware, standalone or as a plugin with the host watching. + * @param {import("webpack").Compiler} compiler compiler + * @param {import("../src").Options} options options + * @param {boolean} isPlugin plugin mode + * @returns {{ instance: import("../src").API, close: () => Promise }} the middleware and a way to stop it + */ + function start(compiler, options, isPlugin) { + const instance = middleware(compiler, options, isPlugin); + const watching = isPlugin ? compiler.watch({}, () => {}) : undefined; + + return { + instance, + close: () => + new Promise((resolve) => { + if (watching) { + watching.close(resolve); + } else { + instance.close(resolve); + } + }), + }; + } + + /** + * @param {import("../src").API} instance middleware + * @returns {Promise} once the build is done + */ + function built(instance) { + return new Promise((resolve) => { + instance.waitUntilValid(() => resolve()); + }); + } + + /** + * Change the source and wait for the build the watcher starts for it. + * @param {import("webpack").Compiler} compiler compiler + * @param {() => void=} change the change, the entry's source by default + * @returns {Promise} once rebuilt + */ + async function rebuild( + compiler, + change = () => + fs.writeFileSync(path.join(directory, "index.js"), "console.log(2);"), + ) { + let changed = false; + let rebuilding = false; + // The build run after the change: the watcher may run one more for the files + // the test wrote before it started, as on macOS. `watchRun` is called again + // when a build is restarted, which keeps its `startTime`, so that is not used. + compiler.hooks.watchRun.tap("test", () => { + rebuilding = changed; + }); + const done = new Promise((resolve) => { + compiler.hooks.done.tap("test", () => { + if (rebuilding) { + resolve(); + } + }); + }); + + // Past the watcher's start, so the change is seen as one. + await new Promise((resolve) => { + setTimeout(resolve, 500); + }); + change(); + changed = true; + + await done; + // The middleware writes to disk as the build emits; let it finish. + await new Promise((resolve) => { + setTimeout(resolve, 100); + }); + } + + /** + * @param {string} file a file in `dist` + * @returns {string} its path + */ + function inDist(file) { + return path.join(dist, file); + } + + /** + * @param {string} file a file in `dist` + * @returns {string} its content + */ + function readDist(file) { + return fs.readFileSync(inDist(file), "utf8"); + } + + /** + * @param {string} file a source file + * @param {string} content its content + */ + function writeSource(file, content) { + fs.writeFileSync(path.join(directory, file), content); + } + + /** + * @param {string} file a file in `public` + * @returns {string} its path + */ + function inPublic(file) { + return path.join(directory, "public", file); + } + + /** + * @param {string} file a file in `public` + * @param {string=} content its new content, or none to remove it + */ + function changePublic(file, content) { + const target = inPublic(file); + + if (typeof content === "string") { + fs.writeFileSync(target, content); + } else { + fs.rmSync(target); + } + } + + /** + * Create `public`, for copy-webpack-plugin or `output.copy` to copy to the output. + */ + function createPublic() { + fs.mkdirSync(path.join(directory, "public", "images"), { recursive: true }); + changePublic("robots.txt", "v1"); + changePublic("images/logo.svg", ""); + } + + const copiers = [ + [ + "copy-webpack-plugin", + (patterns) => + createCompiler({ clean: true }, [new CopyPlugin({ patterns })]), + ], + [ + "output.copy", + (patterns) => createCompiler({ clean: true, copy: patterns }), + ], + ]; + + const writeToDiskValues = [ + ["true", true], + ["a function", () => true], + ]; + + for (const [mode, isPlugin] of [ + ["standalone", false], + ["plugin", true], + ]) { + describe(`in ${mode} mode`, () => { + it('cleans the disk on the first build and on a rebuild with "writeToDisk" true, as webpack writes there itself', async () => { + const compiler = createCompiler({ clean: true }); + const { instance, close } = start( + compiler, + { writeToDisk: true }, + isPlugin, + ); + + try { + await built(instance); + + const first = listDist(); + + expect(first).toEqual([ + expect.stringMatching(/^main\.[0-9a-f]+\.js$/), + ]); + + await rebuild(compiler); + + const second = listDist(); + + expect(second).toEqual([ + expect.stringMatching(/^main\.[0-9a-f]+\.js$/), + ]); + expect(second[0]).not.toBe(first[0]); + } finally { + await close(); + } + }); + + describe("when writing to disk besides the served file system", () => { + it('cleans the disk on the first build and on a rebuild with "writeToDisk" a function', async () => { + const writeToDisk = () => true; + const compiler = createCompiler({ clean: true }); + const { instance, close } = start( + compiler, + { writeToDisk }, + isPlugin, + ); + + try { + await built(instance); + + const first = listDist(); + + expect(first).toHaveLength(1); + expect(first[0]).toMatch(/^main\.[0-9a-f]+\.js$/); + + await rebuild(compiler); + + const second = listDist(); + + expect(second).toHaveLength(1); + expect(second[0]).toMatch(/^main\.[0-9a-f]+\.js$/); + expect(second[0]).not.toBe(first[0]); + } finally { + await close(); + } + }); + + it('writes to disk, and cleans it, with "writeToDisk" true and an "outputFileSystem" of your own', async () => { + const compiler = createCompiler({ clean: true }); + const { instance, close } = start( + compiler, + { + writeToDisk: true, + outputFileSystem: createFsFromVolume(new Volume()), + }, + isPlugin, + ); + + try { + await built(instance); + + // Before, nothing reached the disk at all: only the stale files. + expect(listDist()).toEqual([ + expect.stringMatching(/^main\.[0-9a-f]+\.js$/), + ]); + } finally { + await close(); + } + }); + + it("keeps what `clean.keep` keeps on disk", async () => { + const compiler = createCompiler({ clean: { keep: /\.css$/ } }); + const { instance, close } = start( + compiler, + { writeToDisk: () => true }, + isPlugin, + ); + + try { + await built(instance); + + expect(listDist()).toEqual([ + expect.stringMatching(/^main\.[0-9a-f]+\.js$/), + "nested/stale.css", + ]); + } finally { + await close(); + } + }); + + it("removes nothing from disk with `clean.dry`", async () => { + const compiler = createCompiler({ clean: { dry: true } }); + const { instance, close } = start( + compiler, + { writeToDisk: () => true }, + isPlugin, + ); + + try { + await built(instance); + + expect(listDist()).toEqual([ + expect.stringMatching(/^main\.[0-9a-f]+\.js$/), + "nested/stale.css", + "stale.js", + ]); + } finally { + await close(); + } + }); + + it("removes nothing from disk without `output.clean`", async () => { + const compiler = createCompiler({}); + const { instance, close } = start( + compiler, + { writeToDisk: () => true }, + isPlugin, + ); + + try { + await built(instance); + await rebuild(compiler); + + // Both builds' bundles, and the earlier session's files. + expect(listDist()).toHaveLength(4); + } finally { + await close(); + } + }); + + it("writes to disk a chunk named with a query string under its file name", async () => { + writeSource( + "index.js", + 'import(/* webpackChunkName: "lazy" */ "./lazy.js");', + ); + writeSource("lazy.js", "console.log(1);"); + + const compiler = createCompiler({ + clean: true, + chunkFilename: "[name].js?[contenthash]", + }); + const { instance, close } = start( + compiler, + { writeToDisk: (targetPath) => targetPath.endsWith(".js") }, + isPlugin, + ); + + try { + await built(instance); + + // Cleaning it once it is gone is up to webpack's `output.clean`, + // which has to match such a name to its file. + expect(listDist()).toEqual([ + "lazy.js", + expect.stringMatching(/^main\.[0-9a-f]+\.js$/), + ]); + } finally { + await close(); + } + }); + + for (const [copier, createCopyingCompiler] of copiers) { + describe(`with ${copier}`, () => { + beforeEach(createPublic); + + for (const [label, writeToDisk] of writeToDiskValues) { + it(`writes the copied files to disk and keeps them clean with "writeToDisk" ${label}`, async () => { + const compiler = createCopyingCompiler([{ from: "public" }]); + const { instance, close } = start( + compiler, + { writeToDisk }, + isPlugin, + ); + const bundle = expect.stringMatching(/^main\.[0-9a-f]+\.js$/); + + try { + await built(instance); + + // The copied files beside the bundle, the stale ones gone. + expect(listDist()).toEqual([ + "images/logo.svg", + bundle, + "robots.txt", + ]); + + // A rebuild of the bundle leaves what is still copied. + const [, firstBundle] = listDist(); + + await rebuild(compiler); + + expect(listDist()).toEqual([ + "images/logo.svg", + bundle, + "robots.txt", + ]); + expect(listDist()[1]).not.toBe(firstBundle); + + // A changed file is copied again. + await rebuild(compiler, () => + changePublic("robots.txt", "v2"), + ); + + expect(readDist("robots.txt")).toBe("v2"); + + // One no longer copied is cleaned. + await rebuild(compiler, () => + changePublic("images/logo.svg"), + ); + + expect(listDist()).toEqual([bundle, "robots.txt"]); + } finally { + await close(); + } + }); + } + + it("leaves the copied files off disk when the function does not write them", async () => { + // One of them on disk from an earlier session, to be cleaned. + fs.writeFileSync(inDist("robots.txt"), "old"); + + const compiler = createCopyingCompiler([{ from: "public" }]); + const { instance, close } = start( + compiler, + { writeToDisk: (targetPath) => targetPath.endsWith(".js") }, + isPlugin, + ); + + try { + await built(instance); + + // Served from memory, never written, and the stale files cleaned, the + // earlier copy of one the function skips too. + expect(listDist()).toEqual([ + expect.stringMatching(/^main\.[0-9a-f]+\.js$/), + ]); + expect( + instance.context.outputFileSystem + .readFileSync(inDist("robots.txt")) + .toString(), + ).toBe("v1"); + } finally { + await close(); + } + }); + }); + } + + describeWithLinksAndPermissions( + "with output.copy, links and permissions", + () => { + beforeEach(createPublic); + + it("writes a copied link to disk as a link, and the permissions it keeps", async () => { + fs.chmodSync(inPublic("robots.txt"), 0o755); + fs.symlinkSync("robots.txt", inPublic("link.txt")); + + const compiler = createCompiler({ + clean: true, + copy: [ + { + from: "public", + globOptions: { followSymlinks: false }, + preservePermissions: true, + }, + ], + }); + const { instance, close } = start( + compiler, + { writeToDisk: () => true }, + isPlugin, + ); + + try { + await built(instance); + + expect(fs.lstatSync(inDist("link.txt")).isSymbolicLink()).toBe( + true, + ); + expect(fs.readlinkSync(inDist("link.txt"))).toBe("robots.txt"); + expect(fs.statSync(inDist("robots.txt")).mode & 0o777).toBe( + 0o755, + ); + } finally { + await close(); + } + }); + }, + ); + }); + }); + } + + /** + * @returns {import("memfs").IFs} a file system in memory + */ + function createMemoryFileSystem() { + return createFsFromVolume(new Volume()); + } + + /** + * @param {import("memfs").IFs} memory a file system in memory + * @returns {string[]} the files in `dist` on it, relative to it + */ + function listMemory(memory) { + if (!memory.existsSync(dist)) { + return []; + } + + /** @type {string[]} */ + const files = []; + /** + * @param {string} directory a directory in `dist`, relative to it + */ + const walk = (directory) => { + // One level at a time: memfs lists recursively with the full path on Windows. + for (const name of /** @type {string[]} */ ( + memory.readdirSync(path.join(dist, directory), { encoding: "utf8" }) + )) { + const file = directory ? `${directory}/${name}` : name; + + if (memory.statSync(path.join(dist, file)).isDirectory()) { + walk(file); + } else { + files.push(file); + } + } + }; + + walk(""); + + return files.toSorted(); + } + + /** + * A file system of your own, with only the methods named, as webpack can be + * given one lacking those it does without. + * @param {import("memfs").IFs} memory the file system in memory behind it + * @param {string[]} names the methods it has + * @returns {import("../src").OutputFileSystem} the file system + */ + function withMethods(memory, names) { + /** @type {Record} */ + const fileSystem = { join: path.join, dirname: path.dirname }; + + for (const name of names) { + fileSystem[name] = memory[name].bind(memory); + } + + return /** @type {import("../src").OutputFileSystem} */ (fileSystem); + } + + /** + * @param {import("../src").API} instance middleware + * @returns {import("supertest").Agent} requests to it + */ + function requestTo(instance) { + return request((req, res) => { + instance(req, res, () => { + res.statusCode = 404; + res.end(); + }); + }); + } + + /** + * @param {import("webpack").Compiler} compiler compiler + * @returns {Promise} the error a build fails with, as a failed emit does + */ + function failure(compiler) { + return new Promise((resolve) => { + compiler.hooks.failed.tap("test", resolve); + }); + } + + const bundle = expect.stringMatching(/^main\.[0-9a-f]+\.js$/); + + /** + * @param {string[]} names the names of the compilers, each writing to its own directory in `dist` + * @param {import("webpack").Configuration["output"]=} output output options + * @returns {import("webpack").MultiCompiler} a multi-compiler + */ + function createMultiCompiler(names, output = {}) { + return webpack( + names.map((name) => ({ + name, + mode: "development", + devtool: false, + context: directory, + entry: "./index.js", + output: { + path: path.join(dist, name), + filename: "main.[contenthash].js", + clean: true, + ...output, + }, + infrastructureLogging: { level: "error" }, + stats: "none", + })), + ); + } + + for (const [mode, isPlugin] of [ + ["standalone", false], + ["plugin", true], + ]) { + describe(`with a file system of your own, in ${mode} mode`, () => { + describe("on the compiler", () => { + it('writes to disk and to it, and cleans both, with "writeToDisk" true', async () => { + const memory = createMemoryFileSystem(); + + memory.mkdirSync(inDist("."), { recursive: true }); + memory.writeFileSync(inDist("stale-in-memory.js"), "old"); + + const compiler = createCompiler({ clean: true }); + + compiler.outputFileSystem = memory; + + const { instance, close } = start( + compiler, + { writeToDisk: true }, + isPlugin, + ); + + try { + await built(instance); + + // Served from it, and the disk written too: before, nothing was. + expect(instance.context.outputFileSystem).toBe(memory); + expect(listDist()).toEqual([bundle]); + expect(listMemory(memory)).toEqual(listDist()); + + const [first] = listDist(); + + await rebuild(compiler); + + expect(listDist()).toEqual([bundle]); + expect(listDist()[0]).not.toBe(first); + expect(listMemory(memory)).toEqual(listDist()); + } finally { + await close(); + } + }); + + it("does not write what was in it alone to disk", async () => { + const memory = createMemoryFileSystem(); + + memory.mkdirSync(inDist("only"), { recursive: true }); + memory.writeFileSync(inDist("only/in-memory.js"), "old"); + + const compiler = createCompiler({ clean: { keep: /in-memory/ } }); + + compiler.outputFileSystem = memory; + + const { instance, close } = start( + compiler, + { writeToDisk: true }, + isPlugin, + ); + + try { + await built(instance); + + expect(listMemory(memory)).toEqual([bundle, "only/in-memory.js"]); + // Kept where it was, not copied to disk; what was on disk is cleaned. + expect(listDist()).toEqual([bundle]); + } finally { + await close(); + } + }); + + it("writes the disk once when it is Node.js `fs`, as Rspack's", async () => { + const compiler = createCompiler({ clean: true }); + + compiler.outputFileSystem = fs; + + const { instance, close } = start( + compiler, + { writeToDisk: true }, + isPlugin, + ); + + try { + await built(instance); + + expect(compiler.outputFileSystem).toBe(fs); + expect(listDist()).toEqual([bundle]); + + const [first] = listDist(); + + await rebuild(compiler); + + expect(listDist()).toEqual([bundle]); + expect(listDist()[0]).not.toBe(first); + } finally { + await close(); + } + }); + + it('keeps the file system of webpack, which is the disk, with "writeToDisk" true', async () => { + const compiler = createCompiler({ clean: true }); + const { outputFileSystem } = compiler; + const { instance, close } = start( + compiler, + { writeToDisk: true }, + isPlugin, + ); + + try { + await built(instance); + + expect(compiler.outputFileSystem).toBe(outputFileSystem); + expect(instance.context.outputFileSystem).toBe(outputFileSystem); + expect(listDist()).toEqual([bundle]); + } finally { + await close(); + } + }); + }); + + describe('in "outputFileSystem"', () => { + for (const [label, writeToDisk] of writeToDiskValues) { + it(`writes to disk and to it, and cleans both, with "writeToDisk" ${label}`, async () => { + const memory = createMemoryFileSystem(); + const compiler = createCompiler({ clean: true }); + const { instance, close } = start( + compiler, + { writeToDisk, outputFileSystem: memory }, + isPlugin, + ); + + try { + await built(instance); + + expect(instance.context.outputFileSystem).toBe(memory); + expect(listDist()).toEqual([bundle]); + expect(listMemory(memory)).toEqual([bundle]); + + const [first] = listDist(); + + // What is on disk is what is served. + expect(readDist(first)).toBe( + memory.readFileSync(inDist(first), "utf8"), + ); + + await rebuild(compiler); + + expect(listDist()).toEqual([bundle]); + expect(listDist()[0]).not.toBe(first); + expect(listMemory(memory)).toEqual(listDist()); + } finally { + await close(); + } + }); + } + + it("cleans what was left in it and what was left on disk, without writing the one to the other", async () => { + const memory = createMemoryFileSystem(); + + memory.mkdirSync(inDist("old"), { recursive: true }); + memory.writeFileSync(inDist("old/stale-in-memory.js"), "old"); + + const compiler = createCompiler({ clean: true }); + const { instance, close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem: memory }, + isPlugin, + ); + + try { + await built(instance); + + expect(listMemory(memory)).toEqual([bundle]); + expect(listDist()).toEqual([bundle]); + // The directories emptied are removed too. + expect(fs.existsSync(inDist("nested"))).toBe(false); + expect(memory.existsSync(inDist("old"))).toBe(false); + } finally { + await close(); + } + }); + + it("writes to disk only what the function takes, and serves the rest from it", async () => { + createPublic(); + + const memory = createMemoryFileSystem(); + const compiler = createCompiler({ + clean: true, + copy: [{ from: "public" }], + }); + const { instance, close } = start( + compiler, + { + writeToDisk: (targetPath) => !targetPath.endsWith(".svg"), + outputFileSystem: memory, + }, + isPlugin, + ); + + try { + await built(instance); + + expect(listDist()).toEqual([bundle, "robots.txt"]); + expect(listMemory(memory)).toEqual([ + "images/logo.svg", + bundle, + "robots.txt", + ]); + + const response = await requestTo(instance).get("/images/logo.svg"); + + expect(response.statusCode).toBe(200); + expect(response.body.toString()).toBe(""); + + await rebuild(compiler, () => changePublic("images/logo.svg")); + + expect(listMemory(memory)).toEqual([bundle, "robots.txt"]); + expect(listDist()).toEqual([bundle, "robots.txt"]); + expect( + (await requestTo(instance).get("/images/logo.svg")).statusCode, + ).toBe(404); + } finally { + await close(); + } + }); + + it("keeps on disk and in it what `clean.keep` keeps", async () => { + const memory = createMemoryFileSystem(); + + memory.mkdirSync(inDist("."), { recursive: true }); + memory.writeFileSync(inDist("kept.css"), "old"); + memory.writeFileSync(inDist("removed.js"), "old"); + + const compiler = createCompiler({ clean: { keep: /\.css$/ } }); + const { instance, close } = start( + compiler, + { writeToDisk: true, outputFileSystem: memory }, + isPlugin, + ); + + try { + await built(instance); + + expect(listDist()).toEqual([bundle, "nested/stale.css"]); + expect(listMemory(memory)).toEqual(["kept.css", bundle]); + } finally { + await close(); + } + }); + + it("removes nothing from disk or from it with `clean.dry`", async () => { + const memory = createMemoryFileSystem(); + + memory.mkdirSync(inDist("."), { recursive: true }); + memory.writeFileSync(inDist("stale-in-memory.js"), "old"); + + const compiler = createCompiler({ clean: { dry: true } }); + const { instance, close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem: memory }, + isPlugin, + ); + + try { + await built(instance); + + expect(listDist()).toEqual([ + bundle, + "nested/stale.css", + "stale.js", + ]); + expect(listMemory(memory)).toEqual([bundle, "stale-in-memory.js"]); + } finally { + await close(); + } + }); + + it("serves and writes to disk the same content after a rebuild", async () => { + const memory = createMemoryFileSystem(); + const compiler = createCompiler({ clean: true }); + const { instance, close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem: memory }, + isPlugin, + ); + + try { + await built(instance); + await rebuild(compiler); + + const [file] = listDist(); + const response = await requestTo(instance).get(`/${file}`); + + expect(response.statusCode).toBe(200); + expect(response.text).toContain("console.log(2)"); + expect(readDist(file)).toBe(response.text); + } finally { + await close(); + } + }); + + for (const [label, getFileSystem] of [ + ["Node.js `fs`", () => fs], + [ + "the file system of webpack", + (compiler) => compiler.outputFileSystem, + ], + ]) { + it(`writes the disk once when it is ${label}`, async () => { + const compiler = createCompiler({ clean: true }); + const outputFileSystem = getFileSystem(compiler); + const { instance, close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem }, + isPlugin, + ); + + try { + await built(instance); + + // Given as it is, so nothing is written or removed twice. + expect(compiler.outputFileSystem).toBe(outputFileSystem); + expect(listDist()).toEqual([bundle]); + + const [first] = listDist(); + + await rebuild(compiler); + + expect(listDist()).toEqual([bundle]); + expect(listDist()[0]).not.toBe(first); + } finally { + await close(); + } + }); + } + + it("writes to disk, and cleans it, with one lacking what webpack does without", async () => { + const memory = createMemoryFileSystem(); + // No `lstat`, `readlink`, `symlink`, `chmod` or `utimes`. + const outputFileSystem = withMethods(memory, [ + "mkdir", + "writeFile", + "stat", + "readFile", + "readdir", + "unlink", + "rmdir", + "statSync", + "readFileSync", + "createReadStream", + ]); + const compiler = createCompiler({ clean: true }); + const { instance, close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem }, + isPlugin, + ); + + try { + await built(instance); + + expect(listDist()).toEqual([bundle]); + expect(listMemory(memory)).toEqual([bundle]); + + const [first] = listDist(); + + await rebuild(compiler); + + expect(listDist()).toEqual([bundle]); + expect(listDist()[0]).not.toBe(first); + expect(listMemory(memory)).toEqual(listDist()); + } finally { + await close(); + } + }); + + it("cleans the disk with one that cannot list or remove files", async () => { + const memory = createMemoryFileSystem(); + const outputFileSystem = withMethods(memory, [ + "mkdir", + "writeFile", + "stat", + "readFile", + ]); + const compiler = createCompiler({ clean: true }); + const { instance, close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem }, + isPlugin, + ); + + try { + await built(instance); + + expect(listDist()).toEqual([bundle]); + + const [first] = listDist(); + + await rebuild(compiler); + + expect(listDist()).toEqual([bundle]); + expect(listDist()[0]).not.toBe(first); + } finally { + await close(); + } + }); + + for (const [label, clean] of [ + ["`clean.keep` a string", { keep: "robots" }], + ["`clean.keep` a RegExp", { keep: /^robots/ }], + [ + "`clean.keep` a function", + { keep: (file) => file === "robots.txt" }, + ], + ["`clean.keep` a glob", { keep: { glob: "robots.*" } }], + ["`clean.dry`", { dry: true }], + ]) { + it(`keeps the copy on disk of a file the function leaves out with ${label}`, async () => { + createPublic(); + fs.writeFileSync(inDist("robots.txt"), "old"); + + const memory = createMemoryFileSystem(); + const compiler = createCompiler({ + clean, + copy: [{ from: "public" }], + }); + const { instance, close } = start( + compiler, + { + writeToDisk: (targetPath) => targetPath.endsWith(".js"), + outputFileSystem: memory, + }, + isPlugin, + ); + + try { + await built(instance); + + expect(readDist("robots.txt")).toBe("old"); + expect(memory.readFileSync(inDist("robots.txt"), "utf8")).toBe( + "v1", + ); + } finally { + await close(); + } + }); + } + + it("keeps the copy on disk of a file the function leaves out when a plugin keeps it through the `keep` hook", async () => { + createPublic(); + fs.writeFileSync(inDist("robots.txt"), "old"); + + const memory = createMemoryFileSystem(); + const keeper = { + apply(compiler) { + compiler.hooks.thisCompilation.tap("test", (compilation) => { + webpack.CleanPlugin.getCompilationHooks(compilation).keep.tap( + "test", + (file) => (file === "robots.txt" ? true : undefined), + ); + }); + }, + }; + const compiler = createCompiler( + { clean: true, copy: [{ from: "public" }] }, + [keeper], + ); + const { instance, close } = start( + compiler, + { + writeToDisk: (targetPath) => targetPath.endsWith(".js"), + outputFileSystem: memory, + }, + isPlugin, + ); + + try { + await built(instance); + + expect(readDist("robots.txt")).toBe("old"); + expect(memory.readFileSync(inDist("robots.txt"), "utf8")).toBe( + "v1", + ); + } finally { + await close(); + } + }); + + it("keeps the copy on disk of a file the function leaves out by `clean.keep` alone where `CleanPlugin` has no hooks", async () => { + createPublic(); + fs.writeFileSync(inDist("robots.txt"), "old"); + fs.writeFileSync(inDist("humans.txt"), "old"); + changePublic("humans.txt", "v1"); + + const memory = createMemoryFileSystem(); + const compiler = createCompiler({ + clean: { keep: /^robots/ }, + copy: [{ from: "public" }], + }); + + // As a compiler whose `CleanPlugin` offers no `keep` hook to ask. + compiler.webpack = { ...compiler.webpack, CleanPlugin: undefined }; + + const { instance, close } = start( + compiler, + { + writeToDisk: (targetPath) => targetPath.endsWith(".js"), + outputFileSystem: memory, + }, + isPlugin, + ); + + try { + await built(instance); + + expect(readDist("robots.txt")).toBe("old"); + expect(fs.existsSync(inDist("humans.txt"))).toBe(false); + } finally { + await close(); + } + }); + + it("removes from disk the copies of what the function leaves out, for each compiler of a multi-compiler", async () => { + for (const name of ["a", "b"]) { + fs.mkdirSync(inDist(name), { recursive: true }); + fs.writeFileSync(inDist(`${name}/main.js`), "old"); + } + + const memory = createMemoryFileSystem(); + const compiler = createMultiCompiler(["a", "b"], { + filename: "main.js", + }); + const { instance, close } = start( + compiler, + { writeToDisk: () => false, outputFileSystem: memory }, + isPlugin, + ); + + try { + await built(instance); + + expect(listDist()).toEqual(["nested/stale.css", "stale.js"]); + expect(listMemory(memory)).toEqual(["a/main.js", "b/main.js"]); + } finally { + await close(); + } + }); + + it("keeps the times of a copied file on disk", async () => { + createPublic(); + + const time = new Date("2020-01-01T00:00:00Z"); + + fs.utimesSync(inPublic("robots.txt"), time, time); + + const memory = createMemoryFileSystem(); + const compiler = createCompiler({ + clean: true, + copy: [{ from: "public", preserveTimestamps: true }], + }); + const { instance, close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem: memory }, + isPlugin, + ); + + try { + await built(instance); + + expect(fs.statSync(inDist("robots.txt")).mtime).toEqual(time); + expect(memory.statSync(inDist("robots.txt")).mtime).toEqual(time); + } finally { + await close(); + } + }); + + it("fails the build, writing nothing to disk, when it cannot write a file", async () => { + const memory = createMemoryFileSystem(); + const outputFileSystem = Object.create(memory, { + writeFile: { + value: (file, content, callback) => + callback(new Error(`Unable to write "${file}"`)), + }, + }); + const compiler = createCompiler({ clean: true }); + const failed = failure(compiler); + const { close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem }, + isPlugin, + ); + + try { + expect((await failed).message).toMatch(/Unable to write ".+main\./); + expect(listDist()).toEqual([]); + } finally { + await close(); + } + }); + + it("fails the build when it cannot create a directory on disk", async () => { + fs.writeFileSync(inDist("blocked"), "a file"); + + const memory = createMemoryFileSystem(); + const compiler = createCompiler({ + clean: true, + path: inDist("blocked/out"), + }); + const failed = failure(compiler); + const { close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem: memory }, + isPlugin, + ); + + try { + expect((await failed).code).toMatch(/^E/); + expect(readDist("blocked")).toBe("a file"); + } finally { + await close(); + } + }); + + it("fails the build when it cannot write a file to disk", async () => { + // A directory where the bundle goes. + fs.mkdirSync(inDist("main.js")); + + const memory = createMemoryFileSystem(); + const compiler = createCompiler({ + clean: { keep: "main.js" }, + filename: "main.js", + }); + const failed = failure(compiler); + const { close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem: memory }, + isPlugin, + ); + + try { + expect((await failed).code).toMatch(/^E/); + expect(fs.statSync(inDist("main.js")).isDirectory()).toBe(true); + } finally { + await close(); + } + }); + + it("cleans the disk of each compiler of a multi-compiler", async () => { + const memory = createMemoryFileSystem(); + + for (const name of ["a", "b"]) { + fs.mkdirSync(inDist(name), { recursive: true }); + fs.writeFileSync(inDist(`${name}/stale.js`), "old"); + } + + const compiler = createMultiCompiler(["a", "b"]); + const { instance, close } = start( + compiler, + { writeToDisk: () => true, outputFileSystem: memory }, + isPlugin, + ); + + try { + await built(instance); + + const inA = expect.stringMatching(/^a\/main\.[0-9a-f]+\.js$/); + const inB = expect.stringMatching(/^b\/main\.[0-9a-f]+\.js$/); + + // The earlier session's files in `dist` itself are no compiler's. + expect(listDist()).toEqual([ + inA, + inB, + "nested/stale.css", + "stale.js", + ]); + expect(listMemory(memory)).toEqual([inA, inB]); + } finally { + await close(); + } + }); + }); + }); + } +}); diff --git a/types/index.d.ts b/types/index.d.ts index 995090792..ee2b698d7 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -23,6 +23,7 @@ declare namespace wdm { Schema, Compiler, MultiCompiler, + Compilation, Configuration, Stats, MultiStats, @@ -139,6 +140,7 @@ declare function honoWrapper< type Schema = import("schema-utils/declarations/validate").Schema; type Compiler = import("webpack").Compiler; type MultiCompiler = import("webpack").MultiCompiler; +type Compilation = import("webpack").Compilation; type Configuration = import("webpack").Configuration; type Stats = import("webpack").Stats; type MultiStats = import("webpack").MultiStats;