Skip to content

Commit 02522f1

Browse files
committed
throw an error when trying to use [chunkhash]
... when it's not allowed. I. e. with HMR enabled [chunkhash] for the entry chunk is not allowed. Now it throws an error. Also fixes a bug where [chunkhash] doesn't have to correct length. fixes webpack#1192
1 parent ba03fda commit 02522f1

8 files changed

Lines changed: 135 additions & 46 deletions

File tree

‎lib/ChunkRenderError.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function ChunkRenderError(chunk, file, error) {
6+
Error.call(this);
7+
Error.captureStackTrace(this, ChunkRenderError);
8+
this.name = "ChunkRenderError";
9+
this.error = error;
10+
this.message = error.message;
11+
this.details = error.stack;
12+
this.file = file;
13+
this.chunk = chunk;
14+
}
15+
module.exports = ChunkRenderError;
16+
17+
ChunkRenderError.prototype = Object.create(Error.prototype);

‎lib/Compilation.js‎

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var ChunkTemplate = require("./ChunkTemplate");
1717
var HotUpdateChunkTemplate = require("./HotUpdateChunkTemplate");
1818
var ModuleTemplate = require("./ModuleTemplate");
1919
var Dependency = require("./Dependency");
20+
var ChunkRenderError = require("./ChunkRenderError");
2021
var CachedSource = require("webpack-core/lib/CachedSource");
2122

2223
function Compilation(compiler) {
@@ -814,39 +815,45 @@ Compilation.prototype.createChunkAssets = function createChunkAssets() {
814815
var filenameTemplate = chunk.filenameTemplate ? chunk.filenameTemplate :
815816
chunk.initial ? filename :
816817
chunkFilename;
817-
var usedHash = !chunk.entry || (this.mainTemplate.useChunkHash && this.mainTemplate.useChunkHash(chunk)) ? chunkHash : this.fullHash;
818-
var argumentedChunk = Object.create(chunk);
819-
argumentedChunk.renderedHash = usedHash;
820-
if(this.cache && this.cache["c" + chunk.id] && this.cache["c" + chunk.id].hash === usedHash) {
821-
source = this.cache["c" + chunk.id].source;
822-
} else {
823-
if(chunk.entry) {
824-
source = this.mainTemplate.render(this.hash, chunk, this.moduleTemplate, this.dependencyTemplates);
818+
try {
819+
var useChunkHash = !chunk.entry || (this.mainTemplate.useChunkHash && this.mainTemplate.useChunkHash(chunk));
820+
var usedHash = useChunkHash ? chunkHash : this.fullHash;
821+
if(this.cache && this.cache["c" + chunk.id] && this.cache["c" + chunk.id].hash === usedHash) {
822+
source = this.cache["c" + chunk.id].source;
825823
} else {
826-
source = this.chunkTemplate.render(chunk, this.moduleTemplate, this.dependencyTemplates);
827-
}
828-
if(this.cache) {
829-
this.cache["c" + chunk.id] = {
830-
hash: usedHash,
831-
source: source = (source instanceof CachedSource ? source : new CachedSource(source))
832-
};
824+
if(chunk.entry) {
825+
source = this.mainTemplate.render(this.hash, chunk, this.moduleTemplate, this.dependencyTemplates);
826+
} else {
827+
source = this.chunkTemplate.render(chunk, this.moduleTemplate, this.dependencyTemplates);
828+
}
829+
if(this.cache) {
830+
this.cache["c" + chunk.id] = {
831+
hash: usedHash,
832+
source: source = (source instanceof CachedSource ? source : new CachedSource(source))
833+
};
834+
}
833835
}
834-
}
835-
this.assets[
836-
file = this.getPath(filenameTemplate, {
837-
chunk: argumentedChunk
838-
})
839-
] = source;
840-
chunk.files.push(file);
841-
this.applyPlugins("chunk-asset", chunk, file);
842-
if(chunk.id !== 0 && namedChunkFilename && chunk.name) {
843836
this.assets[
844-
file = this.getPath(namedChunkFilename, {
845-
chunk: argumentedChunk
837+
file = this.getPath(filenameTemplate, {
838+
noChunkHash: !useChunkHash,
839+
chunk: chunk
846840
})
847841
] = source;
848842
chunk.files.push(file);
849843
this.applyPlugins("chunk-asset", chunk, file);
844+
file = undefined;
845+
if(chunk.id !== 0 && namedChunkFilename && chunk.name) {
846+
this.assets[
847+
file = this.getPath(namedChunkFilename, {
848+
noChunkHash: !useChunkHash,
849+
chunk: chunk
850+
})
851+
] = source;
852+
chunk.files.push(file);
853+
this.applyPlugins("chunk-asset", chunk, file);
854+
}
855+
} catch(err) {
856+
this.errors.push(new ChunkRenderError(chunk, file || filenameTemplate, err));
850857
}
851858
}
852859
};

‎lib/Stats.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,15 @@ Stats.prototype.toJson = function toJson(options, forToString) {
7676
var text = "";
7777
if(typeof e === "string")
7878
e = {message: e};
79+
if(e.chunk) {
80+
text += "chunk " + (e.chunk.name || e.chunk.id) +
81+
(e.chunk.entry ? " [entry]" : e.chunk.initial ? " [initial]" : "") + "\n";
82+
}
83+
if(e.file) {
84+
text += e.file + "\n";
85+
}
7986
if(e.module && e.module.readableIdentifier && typeof e.module.readableIdentifier === "function") {
8087
text += e.module.readableIdentifier(requestShortener) + "\n";
81-
} else if(e.file) {
82-
text += e.file + "\n";
8388
}
8489
text += e.message;
8590
if(showErrorDetails && e.details) text += "\n" + e.details;

‎lib/TemplatedPathPlugin.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ function replacePathVariables(path, data) {
6262
var chunkHash = chunk && (chunk.renderedHash || chunk.hash);
6363
var chunkHashWithLength = chunk && chunk.hashWithLength;
6464

65+
if(data.noChunkHash && REGEXP_CHUNKHASH_FOR_TEST.test(path)) {
66+
throw new Error("Cannot use [chunkhash] for chunk in '" + path + "' (use [hash] instead)");
67+
}
68+
6569
return path
6670
.replace(REGEXP_HASH, withHashLength(getReplacer(data.hash), data.hashWithLength))
6771
.replace(REGEXP_CHUNKHASH, withHashLength(getReplacer(chunkHash), chunkHashWithLength))

‎test/Errors.test.js‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,30 @@ describe("Errors", function() {
125125
lines[0].should.match(/non-entry/);
126126
done();
127127
});
128-
129128
});
130-
});
129+
it("should throw an error when trying to use [chunkhash] when it's invalid", function(done) {
130+
getErrors({
131+
entry: {
132+
a: "./entry-point",
133+
b: "./entry-point",
134+
c: "./entry-point"
135+
},
136+
output: {
137+
filename: "[chunkhash].js"
138+
},
139+
plugins: [
140+
new webpack.HotModuleReplacementPlugin()
141+
]
142+
}, function(errors, warnings) {
143+
errors.length.should.be.eql(3);
144+
warnings.length.should.be.eql(0);
145+
errors.forEach(function(error) {
146+
var lines = error.split("\n");
147+
lines[0].should.match(/chunk (a|b|c)/);
148+
lines[2].should.match(/\[chunkhash\].js/);
149+
lines[2].should.match(/use \[hash\] instead/);
150+
});
151+
done();
152+
});
153+
});
154+
});

‎test/configCases/hash-length/output-filename/index.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
it("should compile and run the test", function () {});
1+
it("should compile and run the test " + NAME, function () {});
22

3-
it("should load additional chunks", function (done) {
3+
it("should load additional chunks in " + NAME, function (done) {
44
require(['./chunk'], function () {
55
done();
66
});

‎test/configCases/hash-length/output-filename/test.config.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
var fs = require("fs");
2+
require("should");
23

34
module.exports = {
45
findBundle: function(i, options) {
56
var files = fs.readdirSync(options.output.path);
6-
var hashParamMatches = options.output.filename.match(/:(\d+)/);
7-
var hashLength = hashParamMatches && hashParamMatches[1];
7+
var expectedNameLength = options.output.expectedFilenameLength;
88
var bundleDetect = new RegExp("^bundle" + i, "i");
9-
for (var i = 0, file; i < files.length; i++) {
10-
file = files[i];
9+
for(var j = 0, file; j < files.length; j++) {
10+
file = files[j];
1111
if (bundleDetect.test(file)) {
12+
file.should.match(new RegExp("^.{" + expectedNameLength + "}$"));
1213
return "./" + file;
1314
}
1415
}
Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,62 @@
1+
var webpack = require("../../../../");
12
module.exports = [{
3+
name: "hash with length in publicPath",
24
output: {
35
publicPath: "/[hash:6]/",
4-
filename: 'bundle0.[hash:6].js'
6+
filename: "bundle0.[hash:6].js",
7+
chunkFilename: "[id].bundle1.[hash:6].js",
8+
expectedFilenameLength: 17
59
}
610
}, {
11+
name: "hash in publicPath",
712
output: {
813
publicPath: "/[hash]/",
9-
filename: 'bundle1.[hash].js'
14+
filename: "bundle1.[hash].js",
15+
chunkFilename: "[id].bundle1.[hash].js",
16+
expectedFilenameLength: 31
1017
}
1118
}, {
19+
name: "chunkhash with length",
1220
output: {
13-
filename: 'bundle2.[chunkhash:8].js',
14-
chunkFilename: '[id].bundle.[chunkhash:8].js'
21+
filename: "bundle2.[chunkhash:8].js",
22+
chunkFilename: "[id].bundle2.[chunkhash:8].js",
23+
expectedFilenameLength: 19
1524
}
1625
}, {
26+
name: "chunkhash",
1727
output: {
18-
filename: 'bundle3.[chunkhash].js',
19-
chunkFilename: '[id].bundle.[chunkhash].js'
28+
filename: "bundle3.[chunkhash].js",
29+
chunkFilename: "[id].bundle3.[chunkhash].js",
30+
expectedFilenameLength: 31
2031
}
2132
}, {
33+
name: "hash with and without length",
2234
output: {
23-
filename: 'bundle4.[hash].js',
24-
chunkFilename: '[id].bundle.[hash:8].js'
35+
filename: "bundle4.[hash].js",
36+
chunkFilename: "[id].bundle4.[hash:8].js",
37+
expectedFilenameLength: 31
2538
}
2639
}, {
40+
name: "hash with length",
2741
output: {
28-
filename: 'bundle5.[hash:6].js',
29-
chunkFilename: '[id].bundle.[hash:8].js'
42+
filename: "bundle5.[hash:6].js",
43+
chunkFilename: "[id].bundle5.[hash:8].js",
44+
expectedFilenameLength: 17
3045
}
46+
}, {
47+
name: "chunkhash in chunkFilename ",
48+
output: {
49+
filename: "bundle6.[hash].js",
50+
chunkFilename: "[id].bundle6.[chunkhash:7].js",
51+
expectedFilenameLength: 31
52+
},
53+
plugins: [
54+
new webpack.HotModuleReplacementPlugin()
55+
]
3156
}];
57+
module.exports.forEach(function(options) {
58+
options.plugins = options.plugins || [];
59+
options.plugins.push(new webpack.DefinePlugin({
60+
NAME: JSON.stringify(options.name)
61+
}));
62+
});

0 commit comments

Comments
 (0)