Skip to content

Commit 602b267

Browse files
committed
fix yield
- handle in ignore for AliasPlugin - handle in ignore for AliasFieldPlugin - handle for unsafe cache - handle yield for custom plugins - use real fs in yield tests - add tests for AliasField and Alias in yield
1 parent b6468e0 commit 602b267

File tree

12 files changed

+472
-44
lines changed

12 files changed

+472
-44
lines changed

lib/AliasFieldPlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ module.exports = class AliasFieldPlugin {
6060
...request,
6161
path: false
6262
};
63+
if (typeof resolveContext.yield === "function") {
64+
resolveContext.yield(ignoreObj);
65+
return callback(null, null);
66+
}
6367
return callback(null, ignoreObj);
6468
}
6569
const obj = {

lib/AliasPlugin.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const forEachBail = require("./forEachBail");
99

1010
/** @typedef {import("./Resolver")} Resolver */
11+
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
1112
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
1213
/** @typedef {{alias: string|Array<string>|false, name: string, onlyModule?: boolean}} AliasOption */
1314

@@ -45,10 +46,15 @@ module.exports = class AliasPlugin {
4546
const remainingRequest = innerRequest.substr(item.name.length);
4647
const resolveWithAlias = (alias, callback) => {
4748
if (alias === false) {
49+
/** @type {ResolveRequest} */
4850
const ignoreObj = {
4951
...request,
5052
path: false
5153
};
54+
if (typeof resolveContext.yield === "function") {
55+
resolveContext.yield(ignoreObj);
56+
return callback(null, null);
57+
}
5258
return callback(null, ignoreObj);
5359
}
5460
if (

lib/Resolver.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,24 @@ class Resolver {
274274
};
275275

276276
let yield_;
277-
let yieldCalled = false;
277+
let ignoredObj;
278+
let finishYield;
279+
let yieldCalled = 0;
278280
if (typeof resolveContext.yield === "function") {
279281
const old = resolveContext.yield;
280282
yield_ = obj => {
281-
yieldCalled = true;
282-
old(obj);
283+
if (obj.path === false) {
284+
if (ignoredObj) return;
285+
ignoredObj = obj;
286+
} else {
287+
old(obj);
288+
}
289+
yieldCalled++;
290+
};
291+
finishYield = result => {
292+
if (result) yield_(result);
293+
if (yieldCalled === 1 && ignoredObj) old(ignoredObj);
294+
callback(null);
283295
};
284296
}
285297

@@ -329,8 +341,8 @@ class Resolver {
329341
(err, result) => {
330342
if (err) return callback(err);
331343

344+
if (yieldCalled || (result && yield_)) return finishYield(result);
332345
if (result) return finishResolved(result);
333-
if (yieldCalled) return callback(null);
334346

335347
return finishWithoutResolve(log);
336348
}
@@ -353,8 +365,8 @@ class Resolver {
353365
(err, result) => {
354366
if (err) return callback(err);
355367

368+
if (yieldCalled || (result && yield_)) return finishYield(result);
356369
if (result) return finishResolved(result);
357-
if (yieldCalled) return callback(null);
358370

359371
// log is missing for the error details
360372
// so we redo the resolving for the log info
@@ -372,11 +384,11 @@ class Resolver {
372384
yield: yield_,
373385
stack: resolveContext.stack
374386
},
375-
err => {
387+
(err, result) => {
376388
if (err) return callback(err);
377389

378390
// In a case that there is a race condition and yield will be called
379-
if (yieldCalled) return callback(null);
391+
if (yieldCalled || (result && yield_)) return finishYield(result);
380392

381393
return finishWithoutResolve(log);
382394
}

lib/UnsafeCachePlugin.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
1111
/** @typedef {{[k: string]: any}} Cache */
1212

13-
function getCacheId(request, withContext) {
13+
function getCacheId(type, request, withContext) {
1414
return JSON.stringify({
15+
type,
1516
context: withContext ? request.context : "",
1617
path: request.path,
1718
query: request.query,
@@ -46,18 +47,49 @@ module.exports = class UnsafeCachePlugin {
4647
.getHook(this.source)
4748
.tapAsync("UnsafeCachePlugin", (request, resolveContext, callback) => {
4849
if (!this.filterPredicate(request)) return callback();
49-
const cacheId = getCacheId(request, this.withContext);
50+
const isYield = typeof resolveContext.yield === "function";
51+
const cacheId = getCacheId(
52+
isYield ? "yield" : "default",
53+
request,
54+
this.withContext
55+
);
5056
const cacheEntry = this.cache[cacheId];
5157
if (cacheEntry) {
58+
if (isYield) {
59+
const yield_ = /** @type {Function} */ (resolveContext.yield);
60+
if (Array.isArray(cacheEntry)) {
61+
for (const result of cacheEntry) yield_(result);
62+
} else {
63+
yield_(cacheEntry);
64+
}
65+
return callback(null, null);
66+
}
5267
return callback(null, cacheEntry);
5368
}
69+
70+
let yieldFn;
71+
let yield_;
72+
const yieldResult = [];
73+
if (isYield) {
74+
yieldFn = resolveContext.yield;
75+
yield_ = result => {
76+
yieldResult.push(result);
77+
};
78+
}
79+
5480
resolver.doResolve(
5581
target,
5682
request,
5783
null,
58-
resolveContext,
84+
yield_ ? { ...resolveContext, yield: yield_ } : resolveContext,
5985
(err, result) => {
6086
if (err) return callback(err);
87+
if (isYield) {
88+
if (result) yieldResult.push(result);
89+
for (const result of yieldResult) yieldFn(result);
90+
this.cache[cacheId] = yieldResult;
91+
return callback(null, null);
92+
}
6193
if (result) return callback(null, (this.cache[cacheId] = result));
6294
callback();
6395
}

test/fixtures/yield/a/foo-2/b

Whitespace-only changes.

test/fixtures/yield/a/foo-2/c

Whitespace-only changes.

test/fixtures/yield/a/foo/a

Whitespace-only changes.

test/fixtures/yield/a/foo/b

Whitespace-only changes.

test/fixtures/yield/b/foo/a

Whitespace-only changes.

test/fixtures/yield/c/foo/a

Whitespace-only changes.

0 commit comments

Comments
 (0)