Skip to content

Commit 3797cba

Browse files
committed
Using contenthash instead of chunkhash
1 parent 9a4030c commit 3797cba

File tree

7 files changed

+42
-27
lines changed

7 files changed

+42
-27
lines changed

index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ class Encore {
993993
* file is generated.
994994
*
995995
* Encore.configureFilenames({
996-
* js: '[name].[chunkhash].js',
996+
* js: '[name].[contenthash].js',
997997
* css: '[name].[contenthash].css',
998998
* images: 'images/[name].[hash:8].[ext]',
999999
* fonts: 'fonts/[name].[hash:8].[ext]'
@@ -1003,8 +1003,7 @@ class Encore {
10031003
* will be used for any file types not passed.
10041004
*
10051005
* If you are using Encore.enableVersioning()
1006-
* make sure that your "js" filenames contain
1007-
* "[chunkhash]" and your "css" filenames contain
1006+
* make sure that your "js" and "css" filenames contain
10081007
* "[contenthash]".
10091008
*
10101009
* @param {object} filenames

lib/WebpackConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,10 @@ class WebpackConfig {
614614
}
615615
}
616616

617+
if (typeof configuredFilenames.js !== 'undefined' && configuredFilenames.js.includes('[chunkhash')) {
618+
logger.deprecation('Using the [chunkhash] placeholder in any filenames is deprecated: use [contenthash] instead.');
619+
}
620+
617621
this.configuredFilenames = configuredFilenames;
618622
}
619623

lib/config-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class ConfigGenerator {
184184

185185
buildOutputConfig() {
186186
// Default filename can be overridden using Encore.configureFilenames({ js: '...' })
187-
let filename = this.webpackConfig.useVersioning ? '[name].[chunkhash:8].js' : '[name].js';
187+
let filename = this.webpackConfig.useVersioning ? '[name].[contenthash:8].js' : '[name].js';
188188
if (this.webpackConfig.configuredFilenames.js) {
189189
filename = this.webpackConfig.configuredFilenames.js;
190190
}

lib/plugins/versioning.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const PluginPriorities = require('./plugin-priorities');
2121
module.exports = function(plugins, webpackConfig) {
2222

2323
/*
24-
* With versioning, the "chunkhash" used in the filenames and
24+
* With versioning, the "contenthash" used in the filenames and
2525
* the module ids (i.e. the internal names of modules that
2626
* are required) become important. Specifically:
2727
*
@@ -32,19 +32,14 @@ module.exports = function(plugins, webpackConfig) {
3232
*
3333
* 2) Similarly, if the final contents of a file don't change,
3434
* then we also don't want that file to have a new filename.
35-
* The WebpackChunkHash() handles this, by making sure that
36-
* the chunkhash is based off of the file contents.
37-
*
38-
* Even in the webpack community, the ideal setup seems to be
39-
* a bit of a mystery:
40-
* * https://github.com/webpack/webpack/issues/1315
41-
* * https://github.com/webpack/webpack.js.org/issues/652#issuecomment-273324529
42-
* * https://webpack.js.org/guides/caching/#deterministic-hashes
35+
* The "contenthash" handles this.
4336
*/
4437
if (webpackConfig.isProduction()) {
4538
// shorter, and obfuscated module ids (versus named modules)
4639
// makes the final assets *slightly* larger, but prevents contents
4740
// from sometimes changing when nothing really changed
41+
// Note: Should not be needed in Webpack 5:
42+
// https://github.com/webpack/webpack/pull/8276
4843
plugins.push({
4944
plugin: new webpack.HashedModuleIdsPlugin(),
5045
priority: PluginPriorities.HashedModuleIdsPlugin
@@ -55,7 +50,7 @@ module.exports = function(plugins, webpackConfig) {
5550
}
5651

5752
if (webpackConfig.useVersioning) {
58-
// enables the [chunkhash] ability
53+
// enables the [chunkhash] ability, which is deprecated
5954
plugins.push({
6055
plugin: new WebpackChunkHash(),
6156
priority: PluginPriorities.WebpackChunkHash

test/WebpackConfig.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,14 +983,14 @@ describe('WebpackConfig object', () => {
983983
it('Calling method sets it', () => {
984984
const config = createConfig();
985985
config.configureFilenames({
986-
js: '[name].[chunkhash].js',
986+
js: '[name].[contenthash].js',
987987
css: '[name].[contenthash].css',
988988
images: 'images/[name].[hash:8].[ext]',
989989
fonts: 'fonts/[name].[hash:8].[ext]'
990990
});
991991

992992
expect(config.configuredFilenames).to.deep.equals({
993-
js: '[name].[chunkhash].js',
993+
js: '[name].[contenthash].js',
994994
css: '[name].[contenthash].css',
995995
images: 'images/[name].[hash:8].[ext]',
996996
fonts: 'fonts/[name].[hash:8].[ext]'
@@ -1014,6 +1014,23 @@ describe('WebpackConfig object', () => {
10141014
});
10151015
}).to.throw('"foo" is not a valid key');
10161016
});
1017+
1018+
it('Using chunkhash is deprecated', () => {
1019+
logger.reset();
1020+
logger.quiet();
1021+
1022+
after(() => {
1023+
logger.quiet(false);
1024+
});
1025+
1026+
const config = createConfig();
1027+
1028+
config.configureFilenames({
1029+
js: 'file.[chunkhash:16].js'
1030+
});
1031+
1032+
expect(logger.getMessages().deprecation).to.not.be.empty;
1033+
});
10171034
});
10181035

10191036
describe('configureUrlLoader', () => {

test/config-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('The config-generator function', () => {
199199
config.useVersioning = true;
200200

201201
const actualConfig = configGenerator(config);
202-
expect(actualConfig.output.filename).to.equal('[name].[chunkhash:8].js');
202+
expect(actualConfig.output.filename).to.equal('[name].[contenthash:8].js');
203203

204204
const miniCssPlugin = findPlugin(MiniCssExtractPlugin, actualConfig.plugins);
205205

test/functional.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,11 @@ describe('Functional tests using webpack', function() {
356356
testSetup.runWebpack(config, (webpackAssert) => {
357357
expect(config.outputPath).to.be.a.directory()
358358
.with.files([
359-
'main.f1e0a935.js',
359+
'main.89eb104b.js',
360360
'styles.8ec31654.css',
361361
'manifest.json',
362362
'entrypoints.json',
363-
'runtime.d41d8cd9.js',
363+
'runtime.3d179b24.js',
364364
]);
365365

366366
webpackAssert.assertOutputFileContains(
@@ -387,7 +387,7 @@ describe('Functional tests using webpack', function() {
387387
config.enableVersioning(true);
388388
config.configureFilenames({
389389
js: '[name].js?[chunkhash:16]',
390-
css: '[name].css?[contenthash:16]'
390+
css: '[name].css?[chunkhash:16]'
391391
});
392392

393393
testSetup.runWebpack(config, (webpackAssert) => {
@@ -403,7 +403,7 @@ describe('Functional tests using webpack', function() {
403403
);
404404
webpackAssert.assertManifestPath(
405405
'styles.css',
406-
'/styles.css?8ec316547cc77b39'
406+
'/styles.css?91597a40238e0e66'
407407
);
408408

409409
done();
@@ -457,14 +457,14 @@ describe('Functional tests using webpack', function() {
457457
testSetup.runWebpack(config, (webpackAssert) => {
458458
expect(config.outputPath).to.be.a.directory()
459459
.with.files([
460-
'0.8256b1ad.js', // chunks are also versioned
460+
'0.590a68c7.js', // chunks are also versioned
461461
'0.8ec31654.css',
462-
'main.ba427376.js',
462+
'main.4a5effdb.js',
463463
'h1.8ec31654.css',
464464
'bg.0ec2735b.css',
465465
'manifest.json',
466466
'entrypoints.json',
467-
'runtime.d41d8cd9.js',
467+
'runtime.b84a9b43.js',
468468
]);
469469

470470
expect(path.join(config.outputPath, 'images')).to.be.a.directory()
@@ -1591,8 +1591,8 @@ module.exports = {
15911591
expect(config.outputPath).to.be.a.directory()
15921592
.with.files([
15931593
'entrypoints.json',
1594-
'runtime.d41d8cd9.js',
1595-
'main.1172d977.js',
1594+
'runtime.21aa1db9.js',
1595+
'main.22bad391.js',
15961596
'manifest.json',
15971597
'symfony_logo.ea1ca6f7.png',
15981598
'symfony_logo_alt.f27119c2.png',
@@ -1605,7 +1605,7 @@ module.exports = {
16051605

16061606
webpackAssert.assertManifestPath(
16071607
'build/main.js',
1608-
'/build/main.1172d977.js'
1608+
'/build/main.22bad391.js'
16091609
);
16101610

16111611
webpackAssert.assertManifestPath(

0 commit comments

Comments
 (0)