Skip to content

Commit e06423b

Browse files
authored
fix(virtual-url-plugin): sanitize paths for Windows compatibility (#20424)
1 parent 0e4907e commit e06423b

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Fix VirtualUrlPlugin Windows compatibility by sanitizing cache keys and filenames. Cache keys now use `toSafePath` to replace colons (`:`) with double underscores (`__`) and sanitize other invalid characters, ensuring compatibility with Windows filesystem restrictions.

lib/schemes/VirtualUrlPlugin.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,27 @@ class VirtualUrlPlugin {
126126
compiler.hooks.compilation.tap(
127127
PLUGIN_NAME,
128128
(compilation, { normalModuleFactory }) => {
129+
compilation.hooks.assetPath.tap(
130+
{ name: PLUGIN_NAME, before: "TemplatedPathPlugin" },
131+
(path, data) => {
132+
if (data.filename && this.modules[data.filename]) {
133+
/**
134+
* @param {string} str path
135+
* @returns {string} safe path
136+
*/
137+
const toSafePath = (str) =>
138+
`__${str
139+
.replace(/:/g, "__")
140+
.replace(/^[^a-z0-9]+|[^a-z0-9]+$/gi, "")
141+
.replace(/[^a-z0-9._-]+/gi, "_")}`;
142+
143+
// filename: virtual:logo.svg -> __virtual__logo.svg
144+
data.filename = toSafePath(data.filename);
145+
}
146+
return path;
147+
}
148+
);
149+
129150
normalModuleFactory.hooks.resolveForScheme
130151
.for(scheme)
131152
.tap(PLUGIN_NAME, (resourceData) => {

test/configCases/plugins/virtual-url-plugin/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ it("should correctly load virtual modules with custom loader.", (done) => {
4040

4141
it("should correctly load virtual modules with the asset/resource type.", (done) => {
4242
const fs = __non_webpack_require__("fs");
43+
// windows doesn't allow : in file names
44+
expect(Hammer).not.toContain(":");
45+
expect(Hammer).toContain("__");
4346
expect(fs.readFileSync(__dirname + "/" + Hammer, "utf-8")).toContain("</svg>");
4447
done();
4548
});

0 commit comments

Comments
 (0)