Skip to content

Commit e6caf32

Browse files
fix: resolve configuration path for build dependencies
1 parent fd02100 commit e6caf32

File tree

8 files changed

+220
-2
lines changed

8 files changed

+220
-2
lines changed

.changeset/wild-papers-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack-cli": patch
3+
---
4+
5+
Resolve configuration path for cache build dependencies.

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,12 +2597,20 @@ class WebpackCLI {
25972597
configuration.cache.buildDependencies.defaultConfig = [];
25982598
}
25992599

2600+
const normalizeConfigPath = (configPath: string) =>
2601+
configPath.startsWith("file://") ? fileURLToPath(configPath) : path.resolve(configPath);
2602+
26002603
if (Array.isArray(configPath)) {
26012604
for (const oneOfConfigPath of configPath) {
2602-
configuration.cache.buildDependencies.defaultConfig.push(oneOfConfigPath);
2605+
configuration.cache.buildDependencies.defaultConfig.push(
2606+
normalizeConfigPath(oneOfConfigPath),
2607+
);
26032608
}
26042609
} else {
2605-
configuration.cache.buildDependencies.defaultConfig.push(configPath);
2610+
configuration.cache.buildDependencies.defaultConfig.push(
2611+
// TODO fix `file:` support on webpack side and remove it in the next major release
2612+
normalizeConfigPath(configPath),
2613+
);
26062614
}
26072615
}
26082616
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
name: "base-override",
3+
};

test/build/cache/base.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const path = require("node:path");
2+
3+
module.exports = {
4+
mode: "development",
5+
cache: {
6+
type: "filesystem",
7+
buildDependencies: {
8+
config: [__filename],
9+
},
10+
},
11+
infrastructureLogging: {
12+
debug: /cache/,
13+
},
14+
entry: {
15+
app: "./src/main.js",
16+
},
17+
output: {
18+
filename: "[name].bundle.js",
19+
chunkFilename: "[name].bundle.js",
20+
path: path.resolve(__dirname, "dist"),
21+
publicPath: "/",
22+
},
23+
};

test/build/cache/cache.test.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require("node:fs");
44
const path = require("node:path");
5+
const url = require("node:url");
56
const { isWindows, processKill, run, runWatch } = require("../../utils/test-utils");
67

78
describe("cache", () => {
@@ -219,6 +220,145 @@ describe("cache", () => {
219220
expect(stdout).toBeTruthy();
220221
});
221222

223+
it("should work with relative path to configuration without build dependencies", async () => {
224+
fs.rmSync(
225+
path.join(
226+
__dirname,
227+
"../../../node_modules/.cache/webpack/no-build-dependencies-development",
228+
),
229+
{ recursive: true, force: true },
230+
);
231+
232+
let { exitCode, stderr, stdout } = await run(__dirname, [
233+
"-c",
234+
"./no-build-dependencies.config.js",
235+
]);
236+
237+
expect(exitCode).toBe(0);
238+
expect(stderr.match(/No pack exists at/g)).toHaveLength(1);
239+
expect(stderr.match(/Stored pack/g)).toHaveLength(1);
240+
expect(stderr).toBeTruthy();
241+
expect(stdout).toBeTruthy();
242+
243+
({ exitCode, stderr, stdout } = await run(__dirname, [
244+
"-c",
245+
"./no-build-dependencies.config.js",
246+
]));
247+
248+
expect(exitCode).toBe(0);
249+
expect(stderr.match(/restore cache container:/g)).toHaveLength(1);
250+
expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1);
251+
expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1);
252+
expect(stderr).toBeTruthy();
253+
expect(stdout).toBeTruthy();
254+
});
255+
256+
it("should work with file path to configuration without build dependencies", async () => {
257+
fs.rmSync(
258+
path.join(
259+
__dirname,
260+
"../../../node_modules/.cache/webpack/no-build-dependencies-file-development",
261+
),
262+
{ recursive: true, force: true },
263+
);
264+
265+
let { exitCode, stderr, stdout } = await run(__dirname, [
266+
"-c",
267+
path.resolve(__dirname, "./no-build-dependencies-file.config.js"),
268+
]);
269+
270+
expect(exitCode).toBe(0);
271+
expect(stderr.match(/No pack exists at/g)).toHaveLength(1);
272+
expect(stderr.match(/Stored pack/g)).toHaveLength(1);
273+
expect(stderr).toBeTruthy();
274+
expect(stdout).toBeTruthy();
275+
276+
({ exitCode, stderr, stdout } = await run(__dirname, [
277+
"-c",
278+
path.resolve(__dirname, "./no-build-dependencies-file.config.js"),
279+
]));
280+
281+
expect(exitCode).toBe(0);
282+
expect(stderr.match(/restore cache container:/g)).toHaveLength(1);
283+
expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1);
284+
expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1);
285+
expect(stderr).toBeTruthy();
286+
expect(stdout).toBeTruthy();
287+
});
288+
289+
it("should work with absolute path to configuration without build dependencies", async () => {
290+
fs.rmSync(
291+
path.join(
292+
__dirname,
293+
"../../../node_modules/.cache/webpack/no-build-dependencies-absolute-development",
294+
),
295+
{ recursive: true, force: true },
296+
);
297+
298+
let { exitCode, stderr, stdout } = await run(__dirname, [
299+
"-c",
300+
url
301+
.pathToFileURL(path.resolve(__dirname, "./no-build-dependencies-absolute.config.js"))
302+
.toString(),
303+
]);
304+
305+
expect(exitCode).toBe(0);
306+
expect(stderr.match(/No pack exists at/g)).toHaveLength(1);
307+
expect(stderr.match(/Stored pack/g)).toHaveLength(1);
308+
expect(stderr).toBeTruthy();
309+
expect(stdout).toBeTruthy();
310+
311+
({ exitCode, stderr, stdout } = await run(__dirname, [
312+
"-c",
313+
url
314+
.pathToFileURL(path.resolve(__dirname, "./no-build-dependencies-absolute.config.js"))
315+
.toString(),
316+
]));
317+
318+
expect(exitCode).toBe(0);
319+
expect(stderr.match(/restore cache container:/g)).toHaveLength(1);
320+
expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1);
321+
expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1);
322+
expect(stderr).toBeTruthy();
323+
expect(stdout).toBeTruthy();
324+
});
325+
326+
it("should work with multiple configuration and merge", async () => {
327+
fs.rmSync(
328+
path.join(__dirname, "../../../node_modules/.cache/webpack/base-override-development"),
329+
{ recursive: true, force: true },
330+
);
331+
332+
let { exitCode, stderr, stdout } = await run(__dirname, [
333+
"-c",
334+
"./base.config.js",
335+
"-c",
336+
url.pathToFileURL(path.resolve(__dirname, "./base-override.config.js")).toString(),
337+
"--merge",
338+
]);
339+
340+
expect(exitCode).toBe(0);
341+
expect(stderr.match(/No pack exists at/g)).toHaveLength(1);
342+
expect(stderr.match(/Stored pack/g)).toHaveLength(1);
343+
expect(stderr).toBeTruthy();
344+
expect(stdout).toBeTruthy();
345+
346+
({ exitCode, stderr, stdout } = await run(__dirname, [
347+
"-c",
348+
"./base.config.js",
349+
"-c",
350+
url.pathToFileURL(path.resolve(__dirname, "./base-override.config.js")).toString(),
351+
"--merge",
352+
]));
353+
354+
expect(exitCode).toBe(0);
355+
expect(stderr.match(/restore cache container:/g)).toHaveLength(1);
356+
expect(stderr.match(/restore cache content metadata:/g)).toHaveLength(1);
357+
expect(stderr.match(/restore cache content \d+ \(.+\):/g)).toHaveLength(1);
358+
expect(stderr).toBeTruthy();
359+
expect(stdout).toBeTruthy();
360+
});
361+
222362
if (!isWindows) {
223363
it("should graceful shutdown", async () => {
224364
fs.rmSync(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
name: "no-build-dependencies-absolute",
3+
entry: {
4+
app: "./src/main.js",
5+
},
6+
mode: "development",
7+
cache: {
8+
type: "filesystem",
9+
},
10+
infrastructureLogging: {
11+
debug: /cache/,
12+
},
13+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
name: "no-build-dependencies-file",
3+
entry: {
4+
app: "./src/main.js",
5+
},
6+
mode: "development",
7+
cache: {
8+
type: "filesystem",
9+
},
10+
infrastructureLogging: {
11+
debug: /cache/,
12+
},
13+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
name: "no-build-dependencies",
3+
entry: {
4+
app: "./src/main.js",
5+
},
6+
mode: "development",
7+
cache: {
8+
type: "filesystem",
9+
},
10+
infrastructureLogging: {
11+
debug: /cache/,
12+
},
13+
};

0 commit comments

Comments
 (0)