Skip to content

Commit

Permalink
added HashedChunkIdsPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
smelukov committed Nov 13, 2018
1 parent 63e15da commit 9f6a56f
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 29 deletions.
4 changes: 2 additions & 2 deletions declarations/WebpackOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,9 +796,9 @@ export interface OptimizationOptions {
*/
checkWasmTypes?: boolean;
/**
* Define the algorithm to choose chunk ids (named: readable ids for better debugging, size: numeric ids focused on minimal initial download size, total-size: numeric ids focused on minimal total download size, false: no algorithm used, as custom one can be provided via plugin)
* Define the algorithm to choose chunk ids (named: readable ids for better debugging, hashed: short hashes as ids for better long term caching, size: numeric ids focused on minimal initial download size, total-size: numeric ids focused on minimal total download size, false: no algorithm used, as custom one can be provided via plugin)
*/
chunkIds?: "natural" | "named" | "size" | "total-size" | false;
chunkIds?: "natural" | "named" | "hashed" | "size" | "total-size" | false;
/**
* Concatenate modules when possible to generate less modules, more efficient code and enable more optimizations by the minimizer
*/
Expand Down
4 changes: 4 additions & 0 deletions lib/WebpackOptionsApply.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const WarnDeprecatedOptionPlugin = require("./WarnDeprecatedOptionPlugin");
const WarnNoModeSetPlugin = require("./WarnNoModeSetPlugin");

const DeterministicModuleIdsPlugin = require("./ids/DeterministicModuleIdsPlugin");
const HashedChunkIdsPlugin = require("./ids/HashedChunkIdsPlugin");
const HashedModuleIdsPlugin = require("./ids/HashedModuleIdsPlugin");
const NamedChunkIdsPlugin = require("./ids/NamedChunkIdsPlugin");
const NamedModuleIdsPlugin = require("./ids/NamedModuleIdsPlugin");
Expand Down Expand Up @@ -416,6 +417,9 @@ class WebpackOptionsApply extends OptionsApply {
case "named":
new NamedChunkIdsPlugin().apply(compiler);
break;
case "hashed":
new HashedChunkIdsPlugin().apply(compiler);
break;
case "size":
new OccurrenceChunkIdsPlugin({
prioritiseInitial: true
Expand Down
2 changes: 1 addition & 1 deletion lib/WebpackOptionsDefaulter.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
return "natural";
});
this.set("optimization.chunkIds", "make", options => {
if (isProductionLikeMode(options)) return "total-size";
if (isProductionLikeMode(options)) return "hashed";
if (options.mode === "development") return "named";
return "natural";
});
Expand Down
45 changes: 45 additions & 0 deletions lib/ids/HashedChunkIdsPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/

"use strict";

const createHash = require("../util/createHash");
const NamedChunkIdsPlugin = require("./NamedChunkIdsPlugin");

class HashedChunkIdsPlugin extends NamedChunkIdsPlugin {
constructor(options) {
const nameResolver = NamedChunkIdsPlugin.createDefaultNameResolver({});
const usedIds = new Set();
const resolver = chunk => {
const options = this.options;
const name = nameResolver(chunk);

if (name) {
const hash = createHash(options.hashFunction);
hash.update(name);
const hashId = hash.digest(options.hashDigest);
let len = options.hashDigestLength;
while (usedIds.has(hashId.substr(0, len))) len++;
return hashId.substr(0, len);
}

return null;
};

super(resolver);

this.options = Object.assign(
{
context: null,
hashFunction: "md4",
hashDigest: "base64",
hashDigestLength: 4
},
options
);
}
}

module.exports = HashedChunkIdsPlugin;
4 changes: 2 additions & 2 deletions schemas/WebpackOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@
"type": "boolean"
},
"chunkIds": {
"description": "Define the algorithm to choose chunk ids (named: readable ids for better debugging, size: numeric ids focused on minimal initial download size, total-size: numeric ids focused on minimal total download size, false: no algorithm used, as custom one can be provided via plugin)",
"enum": ["natural", "named", "size", "total-size", false]
"description": "Define the algorithm to choose chunk ids (named: readable ids for better debugging, hashed: short hashes as ids for better long term caching, size: numeric ids focused on minimal initial download size, total-size: numeric ids focused on minimal total download size, false: no algorithm used, as custom one can be provided via plugin)",
"enum": ["natural", "named", "hashed", "size", "total-size", false]
},
"concatenateModules": {
"description": "Concatenate modules when possible to generate less modules, more efficient code and enable more optimizations by the minimizer",
Expand Down
24 changes: 19 additions & 5 deletions test/__snapshots__/StatsTestCases.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,20 @@ chunk {5} b.js (b) 179 bytes <{2}> >{4}< [rendered]
import() ./module-b [3] ./module-a.js 1:0-47"
`;

exports[`StatsTestCases should print correct stats for hashed-chunks-plugin 1`] = `
"Hash: a89a2840fa410a1e5944
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
entry.js 6.44 KiB 3Kiy [emitted] entry
vendor.js 267 bytes 4CCq [emitted] vendor
Entrypoint entry = vendor.js entry.js
[./entry.js] 72 bytes {3Kiy} [built]
[./modules/a.js] 22 bytes {4CCq} [built]
[./modules/b.js] 22 bytes {4CCq} [built]
[./modules/c.js] 22 bytes {3Kiy} [built]"
`;

exports[`StatsTestCases should print correct stats for import-context-filter 1`] = `
"Hash: bfa222a7ac46a2081a5c
Time: Xms
Expand Down Expand Up @@ -1519,23 +1533,23 @@ Entrypoint entry = entry.js
`;

exports[`StatsTestCases should print correct stats for no-emit-on-errors-plugin-with-child-error 1`] = `
"Hash: d34416d2e6d74425b231
"Hash: b08e095fa9c38d70de41
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
bundle.js 3.58 KiB 0 main
bundle.js 3.58 KiB ugHj main
child.js 3.58 KiB
Entrypoint main = bundle.js
[967] ./index.js 0 bytes {0} [built]
[967] ./index.js 0 bytes {ugHj} [built]
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/
Child child:
Asset Size Chunks Chunk Names
child.js 3.58 KiB 0 child
child.js 3.58 KiB fGJ1 child
Entrypoint child = child.js
[967] ./index.js 0 bytes {0} [built]
[967] ./index.js 0 bytes {fGJ1} [built]
ERROR in forced error"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {

var bundleDetects = [
options.amd.expectedChunkFilenameLength && {
regex: new RegExp("^\\d+.bundle" + i, "i"),
regex: new RegExp("^\\w+.bundle" + i, "i"),
expectedNameLength: options.amd.expectedChunkFilenameLength
},
{
Expand Down
36 changes: 18 additions & 18 deletions test/configCases/hash-length/output-filename/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = [
},
amd: {
expectedFilenameLength: 17,
expectedChunkFilenameLength: 19
expectedChunkFilenameLength: 22
}
},
{
Expand All @@ -21,7 +21,7 @@ module.exports = [
},
amd: {
expectedFilenameLength: 31,
expectedChunkFilenameLength: 33
expectedChunkFilenameLength: 36
}
},
{
Expand All @@ -32,7 +32,7 @@ module.exports = [
},
amd: {
expectedFilenameLength: 19,
expectedChunkFilenameLength: 21
expectedChunkFilenameLength: 24
}
},
{
Expand All @@ -43,7 +43,7 @@ module.exports = [
},
amd: {
expectedFilenameLength: 31,
expectedChunkFilenameLength: 33
expectedChunkFilenameLength: 36
}
},
{
Expand All @@ -54,7 +54,7 @@ module.exports = [
},
amd: {
expectedFilenameLength: 31,
expectedChunkFilenameLength: 21
expectedChunkFilenameLength: 24
}
},
{
Expand All @@ -65,7 +65,7 @@ module.exports = [
},
amd: {
expectedFilenameLength: 17,
expectedChunkFilenameLength: 21
expectedChunkFilenameLength: 24
}
},
{
Expand All @@ -76,7 +76,7 @@ module.exports = [
},
amd: {
expectedFilenameLength: 31,
expectedChunkFilenameLength: 20
expectedChunkFilenameLength: 23
},
plugins: [new webpack.HotModuleReplacementPlugin()]
},
Expand All @@ -89,7 +89,7 @@ module.exports = [
target: "node",
amd: {
expectedFilenameLength: 18,
expectedChunkFilenameLength: 20
expectedChunkFilenameLength: 23
}
},
{
Expand All @@ -101,7 +101,7 @@ module.exports = [
target: "node",
amd: {
expectedFilenameLength: 31,
expectedChunkFilenameLength: 20
expectedChunkFilenameLength: 23
}
},
{
Expand All @@ -113,7 +113,7 @@ module.exports = [
target: "node",
amd: {
expectedFilenameLength: 31,
expectedChunkFilenameLength: 20
expectedChunkFilenameLength: 23
}
},
{
Expand All @@ -125,7 +125,7 @@ module.exports = [
target: "node",
amd: {
expectedFilenameLength: 32,
expectedChunkFilenameLength: 34
expectedChunkFilenameLength: 37
}
},
{
Expand All @@ -137,7 +137,7 @@ module.exports = [
target: "node",
amd: {
expectedFilenameLength: 9 + 7 + 3,
expectedChunkFilenameLength: 2 + 9 + 7 + 3
expectedChunkFilenameLength: 5 + 9 + 7 + 3
}
},
{
Expand All @@ -149,7 +149,7 @@ module.exports = [
target: "async-node",
amd: {
expectedFilenameLength: 32,
expectedChunkFilenameLength: 34
expectedChunkFilenameLength: 37
}
},
{
Expand All @@ -161,7 +161,7 @@ module.exports = [
target: "async-node",
amd: {
expectedFilenameLength: 9 + 7 + 3,
expectedChunkFilenameLength: 2 + 9 + 7 + 3
expectedChunkFilenameLength: 5 + 9 + 7 + 3
}
},
{
Expand All @@ -175,7 +175,7 @@ module.exports = [
target: "web",
amd: {
expectedFilenameLength: 32,
expectedChunkFilenameLength: 34
expectedChunkFilenameLength: 37
}
},
{
Expand All @@ -189,7 +189,7 @@ module.exports = [
target: "web",
amd: {
expectedFilenameLength: 9 + 7 + 3,
expectedChunkFilenameLength: 2 + 9 + 7 + 3
expectedChunkFilenameLength: 5 + 9 + 7 + 3
}
},
{
Expand All @@ -203,7 +203,7 @@ module.exports = [
target: "webworker",
amd: {
expectedFilenameLength: 32,
expectedChunkFilenameLength: 34
expectedChunkFilenameLength: 37
}
},
{
Expand All @@ -217,7 +217,7 @@ module.exports = [
target: "webworker",
amd: {
expectedFilenameLength: 9 + 7 + 3,
expectedChunkFilenameLength: 2 + 9 + 7 + 3
expectedChunkFilenameLength: 5 + 9 + 7 + 3
}
}
];
Expand Down
3 changes: 3 additions & 0 deletions test/configCases/records/issue-2991/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module.exports = {
__dirname,
"../../../js/config/records/issue-2991/records.json"
),
optimization: {
chunkIds: "total-size"
},
target: "node",
node: {
__dirname: false
Expand Down
3 changes: 3 additions & 0 deletions test/configCases/records/issue-7339/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module.exports = {
__dirname,
"../../../js/config/records/issue-7339/records.json"
),
optimization: {
chunkIds: "total-size"
},
target: "node",
node: {
__dirname: false
Expand Down
3 changes: 3 additions & 0 deletions test/statsCases/hashed-chunks-plugin/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require("./modules/a");
require("./modules/b");
require("./modules/c");
1 change: 1 addition & 0 deletions test/statsCases/hashed-chunks-plugin/modules/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "a";
1 change: 1 addition & 0 deletions test/statsCases/hashed-chunks-plugin/modules/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "b";
1 change: 1 addition & 0 deletions test/statsCases/hashed-chunks-plugin/modules/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "c";
20 changes: 20 additions & 0 deletions test/statsCases/hashed-chunks-plugin/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
mode: "production",
entry: {
entry: "./entry"
},
optimization: {
moduleIds: "named",
chunkIds: "hashed",
splitChunks: {
cacheGroups: {
vendor: {
name: "vendor",
test: /modules[\\/][ab]/,
chunks: "all",
enforce: true
}
}
}
}
};

0 comments on commit 9f6a56f

Please sign in to comment.