Skip to content

Commit

Permalink
fix behavior and add a warning when trying to load an
Browse files Browse the repository at this point in the history
initial chunk on demand
  • Loading branch information
sokra committed Oct 17, 2017
1 parent 5433b8c commit b597322
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 14 deletions.
21 changes: 21 additions & 0 deletions lib/AsyncDependencyToInitialChunkWarning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Sean Larkin @thelarkinn
*/
"use strict";

const WebpackError = require("./WebpackError");

module.exports = class AsyncDependencyToInitialChunkWarning extends WebpackError {
constructor(chunkName, module, loc) {
super();

this.name = "AsyncDependencyToInitialChunkWarning";
this.message = `It's not allowed to load an initial chunk on demand. The chunk name "${chunkName}" is already used by an entrypoint.`;
this.module = module;
this.origin = module;
this.originLoc = loc;

Error.captureStackTrace(this, this.constructor);
}
};
20 changes: 14 additions & 6 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const HotUpdateChunkTemplate = require("./HotUpdateChunkTemplate");
const ModuleTemplate = require("./ModuleTemplate");
const Dependency = require("./Dependency");
const ChunkRenderError = require("./ChunkRenderError");
const AsyncDependencyToInitialChunkWarning = require("./AsyncDependencyToInitialChunkWarning");
const CachedSource = require("webpack-sources").CachedSource;
const Stats = require("./Stats");
const Semaphore = require("./util/Semaphore");
Expand Down Expand Up @@ -885,12 +886,19 @@ class Compilation extends Tapable {
// but only once (blockChunks map)
let c = blockChunks.get(b);
if(c === undefined) {
c = this.addChunk(b.chunkName, b.module, b.loc);
blockChunks.set(b, c);
allCreatedChunks.add(c);
// We initialize the chunks property
// this is later filled with the chunk when needed
b.chunks = [];
c = this.namedChunks[b.chunkName];
if(c && c.isInitial()) {
// TODO webpack 4: convert this to an error
this.warnings.push(new AsyncDependencyToInitialChunkWarning(b.chunkName, b.module, b.loc));
c = chunk;
} else {
c = this.addChunk(b.chunkName, b.module, b.loc);
blockChunks.set(b, c);
allCreatedChunks.add(c);
// We initialize the chunks property
// this is later filled with the chunk when needed
b.chunks = [];
}
}

// 2. We store the Block+Chunk mapping as dependency for the chunk
Expand Down
23 changes: 15 additions & 8 deletions lib/Stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,22 @@ class Stats {
text += e.message;
if(showErrorDetails && e.details) text += `\n${e.details}`;
if(showErrorDetails && e.missing) text += e.missing.map(item => `\n[${item}]`).join("");
if(showModuleTrace && e.dependencies && e.origin) {
if(showModuleTrace && e.origin) {
text += `\n @ ${e.origin.readableIdentifier(requestShortener)}`;
e.dependencies.forEach(dep => {
if(!dep.loc) return;
if(typeof dep.loc === "string") return;
const locInfo = formatLocation(dep.loc);
if(!locInfo) return;
text += ` ${locInfo}`;
});
if(typeof e.originLoc === "object") {
const locInfo = formatLocation(e.originLoc);
if(locInfo)
text += ` ${locInfo}`;
}
if(e.dependencies) {
e.dependencies.forEach(dep => {
if(!dep.loc) return;
if(typeof dep.loc === "string") return;
const locInfo = formatLocation(dep.loc);
if(!locInfo) return;
text += ` ${locInfo}`;
});
}
let current = e.origin;
while(current.issuer) {
current = current.issuer;
Expand Down
8 changes: 8 additions & 0 deletions test/cases/chunks/weird-reference-to-entry/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
it("should handle reference to entry chunk correctly", function(done) {
import(/* webpackChunkName: "main" */"./module-a").then(function(result) {
result.default.should.be.eql("ok");
done();
}).catch(function(e) {
done(e);
});
});
1 change: 1 addition & 0 deletions test/cases/chunks/weird-reference-to-entry/module-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "ok";
3 changes: 3 additions & 0 deletions test/cases/chunks/weird-reference-to-entry/warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = [
[/It's not allowed to load an initial chunk on demand\. The chunk name "main" is already used by an entrypoint\./],
];

0 comments on commit b597322

Please sign in to comment.