Skip to content

Commit

Permalink
Fix contenthash placeholder for Webpack 4.3+
Browse files Browse the repository at this point in the history
Closes #78
  • Loading branch information
jscheid committed Sep 8, 2018
1 parent f81b052 commit 8663bae
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/webpack4-contenthash/README.md
@@ -0,0 +1,3 @@
# Ensure contenthash path variable works in Webpack 4.3+

Test case for issue #78
1 change: 1 addition & 0 deletions examples/webpack4-contenthash/chunk.js
@@ -0,0 +1 @@
console.log('ok');
3 changes: 3 additions & 0 deletions examples/webpack4-contenthash/index.js
@@ -0,0 +1,3 @@
require.ensure([], require => {
require('./chunk.js');
});
12 changes: 12 additions & 0 deletions examples/webpack4-contenthash/test.js
@@ -0,0 +1,12 @@
var webpackVersionComponents = require('webpack/package.json').version.split(
'.'
);
var webpackVersionMajor = Number(webpackVersionComponents[0]);
var webpackVersionMinor = Number(webpackVersionComponents[0]);

module.exports.skip = function skip() {
return (
webpackVersionMajor < 4 ||
(webpackVersionMajor === 4 && webpackVersionMinor < 3)
);
};
18 changes: 18 additions & 0 deletions examples/webpack4-contenthash/webpack.config.js
@@ -0,0 +1,18 @@
var SriPlugin = require("webpack-subresource-integrity");
var HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
entry: "./index.js",
mode: "production",
output: {
filename: "[name].[contenthash].js",
chunkFilename: "[name].[contenthash].js",
crossOriginLoading: "anonymous"
},
plugins: [
new HtmlWebpackPlugin(),
new SriPlugin({
hashFuncNames: ["sha256", "sha384"]
})
]
};
4 changes: 3 additions & 1 deletion util.js
Expand Up @@ -118,11 +118,13 @@ function getChunkFilename(compilation, chunk) {
var args = [isInitialChunk(chunk) ? filename : chunkFilename, {
hash: compilation.hash.substr(0, compilation.mainTemplate.outputOptions.hashDigestLength),
hashWithLength: length => compilation.hash.substr(0, length),
contentHashType: "javascript",
chunk: {
id: chunk.id,
hash: chunk.hash.substr(0, compilation.mainTemplate.outputOptions.hashDigestLength),
hashWithLength: length => chunk.hash.substr(0, length),
name: chunk.name
name: chunk.name,
contentHash: chunk.contentHash
}
}];

Expand Down

0 comments on commit 8663bae

Please sign in to comment.