Skip to content

Commit b597322

Browse files
committed
fix behavior and add a warning when trying to load an
initial chunk on demand
1 parent 5433b8c commit b597322

File tree

6 files changed

+62
-14
lines changed

6 files changed

+62
-14
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Sean Larkin @thelarkinn
4+
*/
5+
"use strict";
6+
7+
const WebpackError = require("./WebpackError");
8+
9+
module.exports = class AsyncDependencyToInitialChunkWarning extends WebpackError {
10+
constructor(chunkName, module, loc) {
11+
super();
12+
13+
this.name = "AsyncDependencyToInitialChunkWarning";
14+
this.message = `It's not allowed to load an initial chunk on demand. The chunk name "${chunkName}" is already used by an entrypoint.`;
15+
this.module = module;
16+
this.origin = module;
17+
this.originLoc = loc;
18+
19+
Error.captureStackTrace(this, this.constructor);
20+
}
21+
};

lib/Compilation.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const HotUpdateChunkTemplate = require("./HotUpdateChunkTemplate");
2020
const ModuleTemplate = require("./ModuleTemplate");
2121
const Dependency = require("./Dependency");
2222
const ChunkRenderError = require("./ChunkRenderError");
23+
const AsyncDependencyToInitialChunkWarning = require("./AsyncDependencyToInitialChunkWarning");
2324
const CachedSource = require("webpack-sources").CachedSource;
2425
const Stats = require("./Stats");
2526
const Semaphore = require("./util/Semaphore");
@@ -885,12 +886,19 @@ class Compilation extends Tapable {
885886
// but only once (blockChunks map)
886887
let c = blockChunks.get(b);
887888
if(c === undefined) {
888-
c = this.addChunk(b.chunkName, b.module, b.loc);
889-
blockChunks.set(b, c);
890-
allCreatedChunks.add(c);
891-
// We initialize the chunks property
892-
// this is later filled with the chunk when needed
893-
b.chunks = [];
889+
c = this.namedChunks[b.chunkName];
890+
if(c && c.isInitial()) {
891+
// TODO webpack 4: convert this to an error
892+
this.warnings.push(new AsyncDependencyToInitialChunkWarning(b.chunkName, b.module, b.loc));
893+
c = chunk;
894+
} else {
895+
c = this.addChunk(b.chunkName, b.module, b.loc);
896+
blockChunks.set(b, c);
897+
allCreatedChunks.add(c);
898+
// We initialize the chunks property
899+
// this is later filled with the chunk when needed
900+
b.chunks = [];
901+
}
894902
}
895903

896904
// 2. We store the Block+Chunk mapping as dependency for the chunk

lib/Stats.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,22 @@ class Stats {
199199
text += e.message;
200200
if(showErrorDetails && e.details) text += `\n${e.details}`;
201201
if(showErrorDetails && e.missing) text += e.missing.map(item => `\n[${item}]`).join("");
202-
if(showModuleTrace && e.dependencies && e.origin) {
202+
if(showModuleTrace && e.origin) {
203203
text += `\n @ ${e.origin.readableIdentifier(requestShortener)}`;
204-
e.dependencies.forEach(dep => {
205-
if(!dep.loc) return;
206-
if(typeof dep.loc === "string") return;
207-
const locInfo = formatLocation(dep.loc);
208-
if(!locInfo) return;
209-
text += ` ${locInfo}`;
210-
});
204+
if(typeof e.originLoc === "object") {
205+
const locInfo = formatLocation(e.originLoc);
206+
if(locInfo)
207+
text += ` ${locInfo}`;
208+
}
209+
if(e.dependencies) {
210+
e.dependencies.forEach(dep => {
211+
if(!dep.loc) return;
212+
if(typeof dep.loc === "string") return;
213+
const locInfo = formatLocation(dep.loc);
214+
if(!locInfo) return;
215+
text += ` ${locInfo}`;
216+
});
217+
}
211218
let current = e.origin;
212219
while(current.issuer) {
213220
current = current.issuer;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
it("should handle reference to entry chunk correctly", function(done) {
2+
import(/* webpackChunkName: "main" */"./module-a").then(function(result) {
3+
result.default.should.be.eql("ok");
4+
done();
5+
}).catch(function(e) {
6+
done(e);
7+
});
8+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "ok";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
[/It's not allowed to load an initial chunk on demand\. The chunk name "main" is already used by an entrypoint\./],
3+
];

0 commit comments

Comments
 (0)