Skip to content

Commit eeb3004

Browse files
fix(deps): guard the lazy barrel walk on callable methods, not present keys (#21998)
* fix(deps): guard the lazy barrel walk on callable methods, not present keys A custom dependency carrying `isLazy` or `getLazyUntil` as anything but a function passed the `in` check and threw when called. Also trims the `resolveOptions` changeset under the 80-character limit. * test(deps): cover a barrel dependency that spells isLazy as a flag The walk called every dependency carrying the name, so a foreign type holding a plain flag under it ended the build with a TypeError. The case hands a barrel one and fails on the previous guard.
1 parent e5a0d99 commit eeb3004

9 files changed

Lines changed: 107 additions & 2 deletions

File tree

‎.changeset/015-cached-module-resolve-options.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Refresh a cached module's `resolveOptions` from the factory instead of the persistent cache pack.
5+
Refresh a cached module's `resolveOptions` from the factory, not the pack.
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+
Skip a foreign dependency whose `isLazy` is a flag when walking a lazy barrel.

‎lib/dependencies/HarmonyImportDependency.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ const getDeferredExports = (module) => {
114114
for (const dep of module.dependencies) {
115115
// TODO remove in webpack 6
116116
// These may be missing on custom dependency types not extending the base Dependency
117-
if (!("getLazyUntil" in dep) || !("isLazy" in dep)) continue;
117+
if (
118+
typeof dep.getLazyUntil !== "function" ||
119+
typeof dep.isLazy !== "function"
120+
) {
121+
continue;
122+
}
118123
if (!dep.isLazy()) continue;
119124
const until = dep.getLazyUntil();
120125
if (until === Dependency.LAZY_UNTIL_FALLBACK) return true;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { missing, value } from "./lib";
2+
3+
it("should walk a barrel carrying a foreign dependency that spells isLazy as a flag", () => {
4+
expect(value).toBe(1);
5+
expect(missing).toBe(undefined);
6+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { value } from "./value.js";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "flagged-lib",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"sideEffects": false
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 1;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
module.exports = [[/export 'missing'/]];
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"use strict";
2+
3+
const webpack = require("../../../../");
4+
const makeSerializable = require("../../../../lib/util/makeSerializable");
5+
6+
/**
7+
* @import {
8+
* ObjectDeserializerContext,
9+
* ObjectSerializerContext
10+
* } from "../../../../lib/serialization/ObjectMiddleware"
11+
*/
12+
13+
const { NullDependency } = webpack.dependencies;
14+
15+
// A dependency type of webpack's own spells `isLazy` as a method; a foreign one may
16+
// spell the same name as a plain flag, which the lazy-barrel walk must not call.
17+
class LazyFlagDependency extends NullDependency {
18+
constructor() {
19+
super();
20+
/** @type {{ isLazy: boolean }} */ (/** @type {unknown} */ (this)).isLazy =
21+
true;
22+
}
23+
24+
/**
25+
* Serializes this instance into the provided serializer context.
26+
* @param {ObjectSerializerContext} context context
27+
*/
28+
serialize(context) {
29+
context.write(this.isLazy);
30+
super.serialize(context);
31+
}
32+
33+
/**
34+
* Restores this instance from the provided deserializer context.
35+
* @param {ObjectDeserializerContext} context context
36+
*/
37+
deserialize(context) {
38+
/** @type {{ isLazy: boolean }} */ (/** @type {unknown} */ (this)).isLazy =
39+
/** @type {boolean} */ (context.read());
40+
super.deserialize(context);
41+
}
42+
}
43+
44+
makeSerializable(
45+
LazyFlagDependency,
46+
"test/configCases/lazy-barrel/custom-dependency-lazy-flag/webpack.config.js",
47+
"LazyFlagDependency"
48+
);
49+
50+
/** @type {import("../../../../").Configuration} */
51+
module.exports = {
52+
mode: "production",
53+
target: "web",
54+
devtool: false,
55+
optimization: {
56+
sideEffects: true,
57+
providedExports: true,
58+
usedExports: true,
59+
moduleIds: "named",
60+
minimize: false,
61+
concatenateModules: false
62+
},
63+
plugins: [
64+
(compiler) => {
65+
compiler.hooks.compilation.tap("Test", (compilation) => {
66+
compilation.dependencyTemplates.set(
67+
LazyFlagDependency,
68+
new NullDependency.Template()
69+
);
70+
compilation.hooks.succeedModule.tap("Test", (module) => {
71+
if (/lib[/\\]index\.js$/.test(module.identifier())) {
72+
module.addDependency(new LazyFlagDependency());
73+
}
74+
});
75+
});
76+
}
77+
]
78+
};

0 commit comments

Comments
 (0)