Skip to content

Commit 0d5c0cf

Browse files
authored
Merge pull request webpack#8654 from mc-zone/fix/8626
Fix: Add missed __webpack_require__.e while importing exist module with context
2 parents 2eefbae + 7a5137d commit 0d5c0cf

File tree

6 files changed

+88
-40
lines changed

6 files changed

+88
-40
lines changed

lib/ContextModule.js

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,12 @@ function webpackContext(req) {
380380
${returnModuleObject}
381381
}
382382
function webpackContextResolve(req) {
383-
var id = map[req];
384-
if(!(id + 1)) { // check for number or string
383+
if(!__webpack_require__.o(map, req)) {
385384
var e = new Error("Cannot find module '" + req + "'");
386385
e.code = 'MODULE_NOT_FOUND';
387386
throw e;
388387
}
389-
return id;
388+
return map[req];
390389
}
391390
webpackContext.keys = function webpackContextKeys() {
392391
return Object.keys(map);
@@ -414,13 +413,12 @@ function webpackContext(req) {
414413
${returnModuleObject}
415414
}
416415
function webpackContextResolve(req) {
417-
var id = map[req];
418-
if(!(id + 1)) { // check for number or string
416+
if(!__webpack_require__.o(map, req)) {
419417
var e = new Error("Cannot find module '" + req + "'");
420418
e.code = 'MODULE_NOT_FOUND';
421419
throw e;
422420
}
423-
return id;
421+
return map[req];
424422
}
425423
webpackContext.keys = function webpackContextKeys() {
426424
return Object.keys(map);
@@ -452,13 +450,12 @@ function webpackAsyncContextResolve(req) {
452450
// Here Promise.resolve().then() is used instead of new Promise() to prevent
453451
// uncaught exception popping up in devtools
454452
return Promise.resolve().then(function() {
455-
var id = map[req];
456-
if(!(id + 1)) { // check for number or string
453+
if(!__webpack_require__.o(map, req)) {
457454
var e = new Error("Cannot find module '" + req + "'");
458455
e.code = 'MODULE_NOT_FOUND';
459456
throw e;
460457
}
461-
return id;
458+
return map[req];
462459
});
463460
}
464461
webpackAsyncContext.keys = function webpackAsyncContextKeys() {
@@ -488,13 +485,12 @@ function webpackAsyncContextResolve(req) {
488485
// Here Promise.resolve().then() is used instead of new Promise() to prevent
489486
// uncaught exception popping up in devtools
490487
return Promise.resolve().then(function() {
491-
var id = map[req];
492-
if(!(id + 1)) { // check for number or string
488+
if(!__webpack_require__.o(map, req)) {
493489
var e = new Error("Cannot find module '" + req + "'");
494490
e.code = 'MODULE_NOT_FOUND';
495491
throw e;
496492
}
497-
return id;
493+
return map[req];
498494
});
499495
}
500496
webpackAsyncContext.keys = function webpackAsyncContextKeys() {
@@ -527,13 +523,12 @@ function webpackAsyncContext(req) {
527523
}
528524
function webpackAsyncContextResolve(req) {
529525
return ${promise}.then(function() {
530-
var id = map[req];
531-
if(!(id + 1)) { // check for number or string
526+
if(!__webpack_require__.o(map, req)) {
532527
var e = new Error("Cannot find module '" + req + "'");
533528
e.code = 'MODULE_NOT_FOUND';
534529
throw e;
535530
}
536-
return id;
531+
return map[req];
537532
});
538533
}
539534
webpackAsyncContext.keys = function webpackAsyncContextKeys() {
@@ -546,59 +541,92 @@ module.exports = webpackAsyncContext;`;
546541

547542
getLazySource(blocks, id) {
548543
let hasMultipleOrNoChunks = false;
544+
let hasNoChunk = true;
549545
const fakeMap = this.getFakeMap(blocks.map(b => b.dependencies[0]));
546+
const hasFakeMap = typeof fakeMap === "object";
550547
const map = blocks
551548
.filter(block => block.dependencies[0].module)
552-
.map(block => ({
553-
dependency: block.dependencies[0],
554-
block: block,
555-
userRequest: block.dependencies[0].userRequest
556-
}))
549+
.map(block => {
550+
const chunks = block.chunkGroup ? block.chunkGroup.chunks : [];
551+
if (chunks.length > 0) {
552+
hasNoChunk = false;
553+
}
554+
if (chunks.length !== 1) {
555+
hasMultipleOrNoChunks = true;
556+
}
557+
return {
558+
dependency: block.dependencies[0],
559+
block: block,
560+
userRequest: block.dependencies[0].userRequest,
561+
chunks
562+
};
563+
})
557564
.sort((a, b) => {
558565
if (a.userRequest === b.userRequest) return 0;
559566
return a.userRequest < b.userRequest ? -1 : 1;
560567
})
561568
.reduce((map, item) => {
562-
const chunks =
563-
(item.block.chunkGroup && item.block.chunkGroup.chunks) || [];
564-
if (chunks.length !== 1) {
565-
hasMultipleOrNoChunks = true;
566-
}
567-
const arrayStart = [item.dependency.module.id];
568-
if (typeof fakeMap === "object") {
569-
arrayStart.push(fakeMap[item.dependency.module.id]);
569+
const chunks = item.chunks;
570+
571+
if (hasNoChunk && !hasFakeMap) {
572+
map[item.userRequest] = item.dependency.module.id;
573+
} else {
574+
const arrayStart = [item.dependency.module.id];
575+
if (typeof fakeMap === "object") {
576+
arrayStart.push(fakeMap[item.dependency.module.id]);
577+
}
578+
map[item.userRequest] = arrayStart.concat(
579+
chunks.map(chunk => chunk.id)
580+
);
570581
}
571-
map[item.userRequest] = arrayStart.concat(
572-
chunks.map(chunk => chunk.id)
573-
);
574582

575583
return map;
576584
}, Object.create(null));
577585

578-
const chunksStartPosition = typeof fakeMap === "object" ? 2 : 1;
579-
const requestPrefix = hasMultipleOrNoChunks
586+
const shortMode = hasNoChunk && !hasFakeMap;
587+
const chunksStartPosition = hasFakeMap ? 2 : 1;
588+
const requestPrefix = hasNoChunk
589+
? "Promise.resolve()"
590+
: hasMultipleOrNoChunks
580591
? `Promise.all(ids.slice(${chunksStartPosition}).map(__webpack_require__.e))`
581592
: `__webpack_require__.e(ids[${chunksStartPosition}])`;
582593
const returnModuleObject = this.getReturnModuleObjectSource(
583594
fakeMap,
584-
"ids[1]"
595+
shortMode ? "invalid" : "ids[1]"
585596
);
586597

587-
return `var map = ${JSON.stringify(map, null, "\t")};
598+
const webpackAsyncContext =
599+
requestPrefix === "Promise.resolve()"
600+
? `${shortMode ? "" : ""}
588601
function webpackAsyncContext(req) {
589-
var ids = map[req];
590-
if(!ids) {
602+
return Promise.resolve().then(function() {
603+
if(!__webpack_require__.o(map, req)) {
604+
var e = new Error("Cannot find module '" + req + "'");
605+
e.code = 'MODULE_NOT_FOUND';
606+
throw e;
607+
}
608+
609+
${shortMode ? "var id = map[req];" : "var ids = map[req], id = ids[0];"}
610+
${returnModuleObject}
611+
});
612+
}`
613+
: `function webpackAsyncContext(req) {
614+
if(!__webpack_require__.o(map, req)) {
591615
return Promise.resolve().then(function() {
592616
var e = new Error("Cannot find module '" + req + "'");
593617
e.code = 'MODULE_NOT_FOUND';
594618
throw e;
595619
});
596620
}
621+
622+
var ids = map[req], id = ids[0];
597623
return ${requestPrefix}.then(function() {
598-
var id = ids[0];
599624
${returnModuleObject}
600625
});
601-
}
626+
}`;
627+
628+
return `var map = ${JSON.stringify(map, null, "\t")};
629+
${webpackAsyncContext}
602630
webpackAsyncContext.keys = function webpackAsyncContextKeys() {
603631
return Object.keys(map);
604632
};

test/__snapshots__/StatsTestCases.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ Built at: Thu Jan 01 1970 00:00:00 GMT
10791079
0.js 305 bytes 0 [emitted]
10801080
1.js 314 bytes 1 [emitted]
10811081
2.js 308 bytes 2 [emitted]
1082-
entry.js 9.03 KiB 3 [emitted] entry
1082+
entry.js 9.05 KiB 3 [emitted] entry
10831083
Entrypoint entry = entry.js
10841084
[0] ./templates/bar.js 38 bytes {0} [optional] [built]
10851085
[1] ./templates/baz.js 38 bytes {1} [optional] [built]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "initialModuleDefault";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.default = "other";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "initialModuleDefault";
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
it("should resolve when import existed chunk (#8626)", function(done) {
2+
require.context("./dir-initial/");
3+
const fileName = "initialModule";
4+
import(`./dir-initial/${fileName}`).then(({default:m}) => {
5+
expect(m).toBe("initialModuleDefault");
6+
done();
7+
}).catch(done);
8+
});
9+
10+
it("should resolve when import existed chunk with fake maps", function(done) {
11+
require.context("./dir-initial-with-fake-map/");
12+
const fileName = "initialModule";
13+
import(`./dir-initial-with-fake-map/${fileName}`).then(({default:m}) => {
14+
expect(m).toBe("initialModuleDefault");
15+
done();
16+
}).catch(done);
17+
});

0 commit comments

Comments
 (0)