Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of contenthash along with templatehash #1065

Merged
merged 2 commits into from Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions index.js
Expand Up @@ -99,6 +99,13 @@ class HtmlWebpackPlugin {
this.options.filename = path.relative(compiler.options.output.path, filename);
}

// `contenthash` is introduced in webpack v4.3
// which conflicts with the plugin's existing `contenthash` method,
// hence it is renamed to `templatehash` to avoid conflicts
this.options.filename = this.options.filename.replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (match) => {
return match.replace('contenthash', 'templatehash');
});

// Check if webpack is running in production mode
// @see https://github.com/webpack/webpack/blob/3366421f1784c449f415cda5930a8e445086f688/lib/WebpackOptionsDefaulter.js#L12-L14
const isProductionLikeMode = compiler.options.mode === 'production' || !compiler.options.mode;
Expand Down
32 changes: 32 additions & 0 deletions spec/basic.spec.js
Expand Up @@ -626,6 +626,38 @@ describe('HtmlWebpackPlugin', () => {
}, ['<script src="index_bundle.js"'], /test-\S+\.html$/, done);
});

it('should allow filename in the format of [<hashType>:contenthash:<digestType>:<length>]', done => {
testHtmlPlugin({
mode: 'production',
entry: {
index: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [
new HtmlWebpackPlugin({filename: 'index.[sha256:contenthash:base32:32].html'})
]
}, [], /index\.[a-z0-9]{32}\.html/, done);
});

it('will replace [contenthash] in the filename with a content hash of 32 hex characters', done => {
testHtmlPlugin({
mode: 'production',
entry: {
index: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [
new HtmlWebpackPlugin({filename: 'index.[contenthash].html'})
]
}, [], /index\.[a-f0-9]{32}\.html/, done);
});

it('will replace [templatehash] in the filename with a content hash of 32 hex characters', done => {
testHtmlPlugin({
mode: 'production',
Expand Down