Skip to content
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
329 changes: 329 additions & 0 deletions test/e2e/write-to-disk-clean.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,329 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, it } from "node:test";
import { expect } from "expect";
import { Volume, createFsFromVolume } from "memfs";
import webpack from "webpack";
import webpackDevMiddleware from "webpack-dev-middleware";
import Server from "../../lib/Server.js";
import compile from "../helpers/compile.js";
import portsMap from "../ports-map.js";

const [standalonePort, pluginPort] = portsMap["write-to-disk-clean"];

// `output.clean` cleans what `devMiddleware.writeToDisk` writes to disk once
// the middleware gives webpack a file system writing to and cleaning both the
// served one and the disk, instead of the served one itself.
// TODO in the next major, once the webpack-dev-middleware version required
// does so, remove this check and the test for the other case.
const supportsDiskClean = (() => {
const compiler = webpack({ entry: "./index.js", infrastructureLogging: {} });
const { context } = webpackDevMiddleware(
compiler,
{ writeToDisk: () => true },
true,
);

return compiler.outputFileSystem !== context.outputFileSystem;
})();

const bundle = expect.stringMatching(/^main\.[0-9a-f]+\.js$/);

describe("devMiddleware.writeToDisk with output.clean", () => {
/** @type {string} */
let directory;
/** @type {string} */
let dist;

beforeEach(() => {
directory = fs.mkdtempSync(path.join(os.tmpdir(), "wds-clean-"));
dist = path.join(directory, "dist");
fs.writeFileSync(path.join(directory, "index.js"), "console.log(1);");
fs.mkdirSync(path.join(directory, "public", "images"), { recursive: true });
fs.writeFileSync(path.join(directory, "public", "robots.txt"), "v1");
fs.writeFileSync(
path.join(directory, "public", "images", "logo.svg"),
"<svg/>",
);
fs.mkdirSync(dist);
// Left from an earlier session, as webpack would find it.
fs.writeFileSync(path.join(dist, "stale.js"), "old");
});

afterEach(() => {
fs.rmSync(directory, { recursive: true, force: true });
});

/**
* @returns {import("webpack").Compiler} a compiler copying `public` beside the bundle
*/
function createCompiler() {
return webpack({
mode: "development",
devtool: false,
context: directory,
entry: "./index.js",
output: {
path: dist,
filename: "main.[contenthash].js",
clean: true,
copy: [{ from: "public" }],
},
infrastructureLogging: { level: "error" },
stats: "none",
});
}

/**
* @returns {string[]} the files in `dist`, relative to it
*/
function listDist() {
return fs
.readdirSync(dist, { recursive: true, encoding: "utf8" })
.filter((file) => fs.statSync(path.join(dist, file)).isFile())
.map((file) => file.replaceAll("\\", "/"))
.toSorted();
}

/**
* Change a source and wait for the build the watcher starts for it.
* @param {import("webpack").Compiler} compiler compiler
* @param {() => void} change the change
* @returns {Promise<void>} once rebuilt
*/
async function rebuild(compiler, change) {
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. `watchRun` is called again when a build
// is restarted, which keeps its `startTime`, so that is not compared.
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;
}

/**
* Change the entry, so the bundle gets a new hash.
*/
function changeEntry() {
fs.writeFileSync(path.join(directory, "index.js"), "console.log(2);");
}

/**
* Remove a copied file from `public`.
*/
function removeCopiedFile() {
fs.rmSync(path.join(directory, "public", "robots.txt"));
}

/**
* @param {import("memfs").IFs} memory a file system in memory
* @returns {string[]} the files in `dist` on it, relative to it
*/
function listMemory(memory) {
/** @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();
}

const options = {
hot: false,
liveReload: false,
static: false,
// The copied image is served from memory, never written to disk.
devMiddleware: { writeToDisk: (filePath) => !filePath.endsWith(".svg") },
};

const modes = [
{
name: "as a server of its own",
port: standalonePort,
/**
* @param {import("webpack").Compiler} compiler compiler
* @param {import("webpack-dev-middleware").Options=} devMiddleware options of the middleware
* @returns {Promise<() => Promise<void>>} a way to stop it
*/
async start(compiler, devMiddleware = options.devMiddleware) {
const server = new Server(
{ ...options, devMiddleware, port: standalonePort },
compiler,
);

await server.start();
await new Promise((resolve) => {
server.middleware.waitUntilValid(() => resolve());
});

return () => server.stop();
},
},
{
name: "as a plugin",
port: pluginPort,
/**
* @param {import("webpack").Compiler} compiler compiler
* @param {import("webpack-dev-middleware").Options=} devMiddleware options of the middleware
* @returns {Promise<() => Promise<void>>} a way to stop it
*/
async start(compiler, devMiddleware = options.devMiddleware) {
const server = new Server({
...options,
devMiddleware,
port: pluginPort,
});

server.apply(compiler);

await compile(compiler, pluginPort);

// Closing the compiler stops its watching and, through `shutdown`, the server.
return () =>
new Promise((resolve) => {
compiler.close(() => resolve());
});
},
},
];

for (const mode of modes) {
describe(mode.name, () => {
it(
"writes to disk what the function takes and cleans the disk across rebuilds",
{ skip: !supportsDiskClean },
async () => {
const compiler = createCompiler();
const stop = await mode.start(compiler);

try {
// The stale file cleaned, the image left off disk.
expect(listDist()).toEqual([bundle, "robots.txt"]);

const response = await fetch(
`http://127.0.0.1:${mode.port}/images/logo.svg`,
);

expect(response.status).toBe(200);
expect(await response.text()).toBe("<svg/>");

const [firstBundle] = listDist();

await rebuild(compiler, changeEntry);

// One bundle, the earlier one cleaned.
expect(listDist()).toEqual([bundle, "robots.txt"]);
expect(listDist()[0]).not.toBe(firstBundle);

await rebuild(compiler, removeCopiedFile);

// A file no longer copied is cleaned from disk.
expect(listDist()).toEqual([bundle]);
} finally {
await stop();
}
},
);

for (const [where, setUp] of [
[
"in `devMiddleware.outputFileSystem`",
(compiler, memory) => ({
writeToDisk: true,
outputFileSystem: memory,
}),
],
[
"on the compiler",
(compiler, memory) => {
compiler.outputFileSystem = memory;

return { writeToDisk: true };
},
],
]) {
it(
`writes to disk and to a file system of your own ${where}, and cleans both`,
{ skip: !supportsDiskClean },
async () => {
const compiler = createCompiler();
const memory = createFsFromVolume(new Volume());
const stop = await mode.start(compiler, setUp(compiler, memory));
const all = ["images/logo.svg", bundle, "robots.txt"];

try {
expect(listDist()).toEqual(all);
expect(listMemory(memory)).toEqual(all);

const response = await fetch(
`http://127.0.0.1:${mode.port}/robots.txt`,
);

expect(await response.text()).toBe("v1");

const [, firstBundle] = listDist();

await rebuild(compiler, changeEntry);

expect(listDist()).toEqual(all);
expect(listDist()[1]).not.toBe(firstBundle);
expect(listMemory(memory)).toEqual(listDist());
} finally {
await stop();
}
},
);
}

it(
"writes to disk what the function takes without cleaning the disk with an earlier webpack-dev-middleware",
{ skip: supportsDiskClean },
async () => {
const compiler = createCompiler();
const stop = await mode.start(compiler);

try {
expect(listDist()).toEqual([bundle, "robots.txt", "stale.js"]);
} finally {
await stop();
}
},
);
});
}
});
1 change: 1 addition & 0 deletions test/ports-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const listOfTests = {
"allowed-origins": 2,
"cli-allowed-origins": 1,
"static-deny": 1,
"write-to-disk-clean": 2,
};

let startPort = 8089;
Expand Down
Loading