Skip to content

Commit 02653a1

Browse files
fix: honor resolve.fileSystem instead of overwriting it
WebpackOptionsApply assigned the compiler's input filesystem over whatever the configuration supplied, for each of the three resolvers, so a build with resolve.fileSystem set to an object with no methods still succeeded: the value never reached enhanced-resolve. It is now the default rather than an override, which is how the sibling resolve.resolver already behaves. Also gives module.exprContextCritical the 'Deprecated: moved to ...' sentence its eleven deprecated siblings carry; it was the only one without it.
1 parent be915c3 commit 02653a1

10 files changed

Lines changed: 110 additions & 22 deletions

File tree

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+
Honor `resolve.fileSystem` and say where `module.exprContextCritical` moved.

‎declarations/WebpackOptions.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,7 @@ export interface ModuleOptions {
18521852
defaultRules?: RuleSetRules;
18531853
/**
18541854
* @deprecated
1855-
* Enable warnings for full dynamic dependencies.
1855+
* Enable warnings for full dynamic dependencies. Deprecated: This option has moved to 'module.parser.javascript.exprContextCritical'.
18561856
*/
18571857
exprContextCritical?: boolean;
18581858
/**

‎lib/WebpackOptionsApply.js‎

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const { NODE_MODULES_REGEXP } = require("./util/identifier");
8585
/** @import { WebpackOptionsInterception } from "./config/normalization" */
8686
/** @import Compiler from "./Compiler" */
8787
/** @import { InputFileSystem, IntermediateFileSystem } from "./util/fs" */
88+
/** @import { ResolveOptionsWithDependencyType } from "./ResolverFactory" */
8889

8990
const CLASS_NAME = "WebpackOptionsApply";
9091

@@ -1243,34 +1244,41 @@ class WebpackOptionsApply extends OptionsApply {
12431244
if (!compiler.inputFileSystem) {
12441245
throw new Error("No input filesystem provided");
12451246
}
1246-
compiler.resolverFactory.hooks.resolveOptions
1247-
.for("normal")
1248-
.tap(CLASS_NAME, (resolveOptions) => {
1249-
resolveOptions = cleverMerge(options.resolve, resolveOptions);
1247+
/**
1248+
* Supplies the compiler's input filesystem as the default for a resolver.
1249+
* A filesystem set in the configuration is kept, so that `resolve.fileSystem`
1250+
* reaches enhanced-resolve the way `resolve.resolver` already does.
1251+
* @param {ResolveOptionsWithDependencyType} resolveOptions merged resolve options
1252+
* @returns {ResolveOptionsWithDependencyType} the same options
1253+
*/
1254+
const withFileSystem = (resolveOptions) => {
1255+
if (!resolveOptions.fileSystem) {
12501256
resolveOptions.fileSystem =
12511257
/** @type {InputFileSystem} */
12521258
(compiler.inputFileSystem);
1253-
return resolveOptions;
1254-
});
1259+
}
1260+
return resolveOptions;
1261+
};
1262+
1263+
compiler.resolverFactory.hooks.resolveOptions
1264+
.for("normal")
1265+
.tap(CLASS_NAME, (resolveOptions) =>
1266+
withFileSystem(cleverMerge(options.resolve, resolveOptions))
1267+
);
12551268
compiler.resolverFactory.hooks.resolveOptions
12561269
.for("context")
12571270
.tap(CLASS_NAME, (resolveOptions) => {
1258-
resolveOptions = cleverMerge(options.resolve, resolveOptions);
1259-
resolveOptions.fileSystem =
1260-
/** @type {InputFileSystem} */
1261-
(compiler.inputFileSystem);
1271+
resolveOptions = withFileSystem(
1272+
cleverMerge(options.resolve, resolveOptions)
1273+
);
12621274
resolveOptions.resolveToContext = true;
12631275
return resolveOptions;
12641276
});
12651277
compiler.resolverFactory.hooks.resolveOptions
12661278
.for("loader")
1267-
.tap(CLASS_NAME, (resolveOptions) => {
1268-
resolveOptions = cleverMerge(options.resolveLoader, resolveOptions);
1269-
resolveOptions.fileSystem =
1270-
/** @type {InputFileSystem} */
1271-
(compiler.inputFileSystem);
1272-
return resolveOptions;
1273-
});
1279+
.tap(CLASS_NAME, (resolveOptions) =>
1280+
withFileSystem(cleverMerge(options.resolveLoader, resolveOptions))
1281+
);
12741282
compiler.hooks.afterResolvers.call(compiler);
12751283
return options;
12761284
}

‎schemas/WebpackOptions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3556,7 +3556,7 @@
35563556
]
35573557
},
35583558
"exprContextCritical": {
3559-
"description": "Enable warnings for full dynamic dependencies.",
3559+
"description": "Enable warnings for full dynamic dependencies. Deprecated: This option has moved to 'module.parser.javascript.exprContextCritical'.",
35603560
"type": "boolean",
35613561
"deprecated": true
35623562
},

‎test/__snapshots__/Cli.basictest.js.snap‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,13 +1569,13 @@ Object {
15691569
"module-expr-context-critical": Object {
15701570
"configs": Array [
15711571
Object {
1572-
"description": "Enable warnings for full dynamic dependencies.",
1572+
"description": "Enable warnings for full dynamic dependencies. Deprecated: This option has moved to 'module.parser.javascript.exprContextCritical'.",
15731573
"multiple": false,
15741574
"path": "module.exprContextCritical",
15751575
"type": "boolean",
15761576
},
15771577
],
1578-
"description": "Enable warnings for full dynamic dependencies.",
1578+
"description": "Enable warnings for full dynamic dependencies. Deprecated: This option has moved to 'module.parser.javascript.exprContextCritical'.",
15791579
"multiple": false,
15801580
"simpleType": "boolean",
15811581
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "dep";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should resolve through the configured file systems", () => {
2+
expect(require("./dep")).toBe("dep");
3+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").LoaderDefinition} */
4+
module.exports = function loader(source) {
5+
return source;
6+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
/** @typedef {{ calls: number }} CallCounter */
7+
8+
/**
9+
* Counts every call the resolver makes, so `done` can tell whether webpack
10+
* resolved through this filesystem or replaced it with the compiler's own.
11+
* @param {CallCounter} counter box holding the call count
12+
* @returns {typeof fs} a counting view of the real filesystem
13+
*/
14+
const countingFileSystem = (counter) =>
15+
new Proxy(fs, {
16+
get(target, key, receiver) {
17+
const value = Reflect.get(target, key, receiver);
18+
19+
if (typeof value !== "function") return value;
20+
21+
return function countingCall(/** @type {EXPECTED_ANY[]} */ ...args) {
22+
counter.calls += 1;
23+
return value.apply(target, args);
24+
};
25+
}
26+
});
27+
28+
/** @type {CallCounter} */
29+
const moduleCounter = { calls: 0 };
30+
/** @type {CallCounter} */
31+
const loaderCounter = { calls: 0 };
32+
33+
/** @type {import("../../../../").Configuration} */
34+
module.exports = {
35+
module: {
36+
rules: [
37+
{
38+
test: /dep\.js$/,
39+
use: "./loader"
40+
}
41+
]
42+
},
43+
resolve: {
44+
fileSystem: countingFileSystem(moduleCounter)
45+
},
46+
resolveLoader: {
47+
modules: [path.resolve(__dirname)],
48+
fileSystem: countingFileSystem(loaderCounter)
49+
},
50+
plugins: [
51+
{
52+
apply(compiler) {
53+
compiler.hooks.done.tap("TestPlugin", () => {
54+
if (moduleCounter.calls === 0) {
55+
throw new Error("resolve.fileSystem was not used");
56+
}
57+
58+
if (loaderCounter.calls === 0) {
59+
throw new Error("resolveLoader.fileSystem was not used");
60+
}
61+
});
62+
}
63+
}
64+
]
65+
};

‎types.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18348,7 +18348,7 @@ declare interface ModuleOptions {
1834818348
defaultRules?: (undefined | null | false | "" | 0 | RuleSetRule | "...")[];
1834918349

1835018350
/**
18351-
* Enable warnings for full dynamic dependencies.
18351+
* Enable warnings for full dynamic dependencies. Deprecated: This option has moved to 'module.parser.javascript.exprContextCritical'.
1835218352
* @deprecated
1835318353
*/
1835418354
exprContextCritical?: boolean;

0 commit comments

Comments
 (0)