Skip to content

Commit 8d1e46e

Browse files
committed
handle error/callback result in ResolverCachePlugin with yield
1 parent 93d4484 commit 8d1e46e

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

lib/ContextModule.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const makeSerializable = require("./util/makeSerializable");
6161

6262
/**
6363
* @typedef {Object} ContextModuleOptionsExtras
64-
* @property {string|string[]} resource
64+
* @property {false|string|string[]} resource
6565
* @property {string=} resourceQuery
6666
* @property {string=} resourceFragment
6767
* @property {TODO} resolveOptions
@@ -170,8 +170,9 @@ class ContextModule extends Module {
170170
_createIdentifier() {
171171
let identifier =
172172
this.context ||
173-
(typeof this.options.resource === "string"
174-
? this.options.resource
173+
(typeof this.options.resource === "string" ||
174+
this.options.resource === false
175+
? `${this.options.resource}`
175176
: this.options.resource.join("|"));
176177
if (this.options.resourceQuery) {
177178
identifier += `|${this.options.resourceQuery}`;
@@ -240,8 +241,11 @@ class ContextModule extends Module {
240241
let identifier;
241242
if (this.context) {
242243
identifier = requestShortener.shorten(this.context) + "/";
243-
} else if (typeof this.options.resource === "string") {
244-
identifier = requestShortener.shorten(this.options.resource) + "/";
244+
} else if (
245+
typeof this.options.resource === "string" ||
246+
this.options.resource === false
247+
) {
248+
identifier = requestShortener.shorten(`${this.options.resource}`) + "/";
245249
} else {
246250
identifier = this.options.resource
247251
.map(r => requestShortener.shorten(r) + "/")
@@ -310,6 +314,8 @@ class ContextModule extends Module {
310314
this.options.resource,
311315
options.associatedObjectForCache
312316
);
317+
} else if (this.options.resource === false) {
318+
identifier = "false";
313319
} else {
314320
const arr = [];
315321
for (const res of this.options.resource) {
@@ -484,14 +490,16 @@ class ContextModule extends Module {
484490
);
485491
return;
486492
}
493+
if (!this.context || !this.options.resource) return callback();
494+
487495
compilation.fileSystemInfo.createSnapshot(
488496
startTime,
489497
null,
490498
this.context
491499
? [this.context]
492500
: typeof this.options.resource === "string"
493501
? [this.options.resource]
494-
: this.options.resource,
502+
: /** @type {string[]} */ (this.options.resource),
495503
null,
496504
SNAPSHOT_OPTIONS,
497505
(err, snapshot) => {
@@ -519,6 +527,8 @@ class ContextModule extends Module {
519527
contextDependencies.add(this.context);
520528
} else if (typeof this.options.resource === "string") {
521529
contextDependencies.add(this.options.resource);
530+
} else if (this.options.resource === false) {
531+
return;
522532
} else {
523533
for (const res of this.options.resource) contextDependencies.add(res);
524534
}

lib/ContextModuleFactory.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,12 @@ module.exports = class ContextModuleFactory extends ModuleFactory {
217217
contextDependencies
218218
});
219219
}
220-
const [contextResult, loaderResult] = result;
220+
let [contextResult, loaderResult] = result;
221+
if (contextResult.length > 1) {
222+
const first = contextResult[0];
223+
contextResult = contextResult.filter(r => r.path);
224+
if (contextResult.length === 0) contextResult.push(first);
225+
}
221226
this.hooks.afterResolve.callAsync(
222227
{
223228
addon:

lib/cache/ResolverCachePlugin.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ class ResolverCachePlugin {
129129
contextDependencies: new LazySet()
130130
};
131131
let yieldResult;
132+
let withYield = false;
132133
if (typeof newResolveContext.yield === "function") {
133134
yieldResult = [];
135+
withYield = true;
134136
newResolveContext.yield = obj => yieldResult.push(obj);
135137
}
136138
const propagate = key => {
@@ -160,7 +162,10 @@ class ResolverCachePlugin {
160162
snapshotOptions,
161163
(err, snapshot) => {
162164
if (err) return callback(err);
163-
const resolveResult = result || yieldResult;
165+
const resolveResult = withYield ? yieldResult : result;
166+
// since we intercept resolve hook
167+
// we still can get result in callback
168+
if (withYield && result) yieldResult.push(result);
164169
if (!snapshot) {
165170
if (resolveResult) return callback(null, resolveResult);
166171
return callback();
@@ -242,11 +247,15 @@ class ResolverCachePlugin {
242247
yields = undefined;
243248
callbacks = false;
244249
} else {
245-
for (let i = 0; i < callbacks.length; i++) {
246-
const cb = callbacks[i];
247-
const yield_ = yields[i];
248-
if (result) for (const r of result) yield_(r);
249-
cb(null, null);
250+
if (err) {
251+
for (const cb of callbacks) cb(err);
252+
} else {
253+
for (let i = 0; i < callbacks.length; i++) {
254+
const cb = callbacks[i];
255+
const yield_ = yields[i];
256+
if (result) for (const r of result) yield_(r);
257+
cb(null, null);
258+
}
250259
}
251260
activeRequestsWithYield.delete(identifier);
252261
yields = undefined;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const id = () => Math.random();
2+
3+
it("should compile", () => {
4+
expect(/* webpackMode: "lazy" */ import(`foo/${id()}`)).rejects.toBeTruthy();
5+
expect(/* webpackMode: "lazy" */ import(`foo/${id()}`)).rejects.toBeTruthy();
6+
expect(/* webpackMode: "lazy" */ import(`foo/${id()}`)).rejects.toBeTruthy();
7+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/** @type {import("../../../../").Configuration[]} */
2+
module.exports = [
3+
{
4+
cache: true,
5+
resolve: {
6+
alias: {
7+
foo: false
8+
},
9+
unsafeCache: true
10+
}
11+
},
12+
{
13+
resolve: {
14+
alias: {
15+
foo: false
16+
},
17+
unsafeCache: true
18+
}
19+
}
20+
];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy