Skip to content

Commit

Permalink
Record chunk assets through hook
Browse files Browse the repository at this point in the history
Closes #83
  • Loading branch information
jscheid committed Sep 11, 2018
1 parent 905dfbd commit 4ede6a6
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 35 deletions.
3 changes: 3 additions & 0 deletions examples/webpack4-contenthash-issue-83/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Issue 83

Test case for issue #83
1 change: 1 addition & 0 deletions examples/webpack4-contenthash-issue-83/chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('ok');
3 changes: 3 additions & 0 deletions examples/webpack4-contenthash-issue-83/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: red;
}
1 change: 1 addition & 0 deletions examples/webpack4-contenthash-issue-83/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./index.css');
17 changes: 17 additions & 0 deletions examples/webpack4-contenthash-issue-83/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var webpackVersionComponents = require('webpack/package.json').version.split(
'.'
);
var webpackVersionMajor = Number(webpackVersionComponents[0]);
var webpackVersionMinor = Number(webpackVersionComponents[0]);
var expect = require('expect');

module.exports.skip = function skip() {
return (
webpackVersionMajor < 4 ||
(webpackVersionMajor === 4 && webpackVersionMinor < 3)
);
};

module.exports.check = function check(stats) {
expect(stats.compilation.warnings).toEqual([]);
};
37 changes: 37 additions & 0 deletions examples/webpack4-contenthash-issue-83/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const SriPlugin = require('webpack-subresource-integrity');

module.exports = {
mode: 'production',
entry: './index.js',
output: {
crossOriginLoading: 'anonymous',
chunkFilename: '[name]-[chunkhash].js',
filename: '[name]-[contenthash].js'
},
optimization: {
splitChunks: {
cacheGroups: {
styles: {
name: 'style',
chunks: 'all',
enforce: true
}
}
}
},
plugins: [
new MiniCssExtractPlugin({ filename: '[name].css' }),
new SriPlugin({
hashFuncNames: ['sha256', 'sha384']
})
],
module: {
rules: [
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
]
}
};
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ SubresourceIntegrityPlugin.prototype.processChunk = function processChunk(
// This can happen with invalid Webpack configurations
if (childChunk.files.length === 0) return;

sourcePath = util.getChunkFilename(compilation, childChunk);
sourcePath = compilation.sriChunkAssets[childChunk.id];

if (childChunk.files.indexOf(sourcePath) < 0) {
self.warnOnce(
Expand All @@ -220,6 +220,12 @@ SubresourceIntegrityPlugin.prototype.processChunk = function processChunk(
});
};

SubresourceIntegrityPlugin.prototype.chunkAsset =
function chunkAsset(compilation, chunk, asset) {
// eslint-disable-next-line no-param-reassign
compilation.sriChunkAssets[chunk.id] = asset;
};

/*
* Calculate SRI values for each chunk and replace the magic
* placeholders by the actual values.
Expand Down Expand Up @@ -329,6 +335,7 @@ SubresourceIntegrityPlugin.prototype.registerHwpHooks =
SubresourceIntegrityPlugin.prototype.thisCompilation =
function thisCompilation(compiler, compilation) {
var afterOptimizeAssets = this.afterOptimizeAssets.bind(this, compilation);
var chunkAsset = this.chunkAsset.bind(this, compilation);
var alterAssetTags = this.alterAssetTags.bind(this, compilation);
var beforeHtmlGeneration = this.beforeHtmlGeneration.bind(this, compilation);

Expand All @@ -340,15 +347,21 @@ SubresourceIntegrityPlugin.prototype.thisCompilation =

this.registerJMTP(compilation);

// FIXME: refactor into separate per-compilation state
// eslint-disable-next-line no-param-reassign
compilation.sriChunkAssets = {};

/*
* html-webpack support:
* Modify the asset tags before webpack injects them for anything with an integrity value.
*/
if (compiler.hooks) {
compilation.hooks.afterOptimizeAssets.tap('SriPlugin', afterOptimizeAssets);
compilation.hooks.chunkAsset.tap('SriPlugin', chunkAsset);
compiler.hooks.compilation.tap('HtmlWebpackPluginHooks', this.registerHwpHooks.bind(this, alterAssetTags, beforeHtmlGeneration));
} else {
compilation.plugin('after-optimize-assets', afterOptimizeAssets);
compilation.plugin('chunk-asset', chunkAsset);
compilation.plugin('html-webpack-plugin-alter-asset-tags', alterAssetTags);
compilation.plugin('html-webpack-plugin-before-html-generation', beforeHtmlGeneration);
}
Expand Down
34 changes: 0 additions & 34 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,39 +102,6 @@ function makePlaceholder(id) {
return "*-*-*-CHUNK-SRI-HASH-" + id + "-*-*-*";
}

function isInitialChunk(chunk) {
if (chunk.isOnlyInitial) {
return chunk.isOnlyInitial();
}
if (chunk.isInitial) {
return chunk.isInitial();
}
return chunk.initial;
}

function getChunkFilename(compilation, chunk) {
var filename = compilation.mainTemplate.outputOptions.filename || "bundle.js";
var chunkFilename = compilation.mainTemplate.outputOptions.chunkFilename || ("[id]." + filename);
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,
contentHash: chunk.contentHash
}
}];

if (compilation.mainTemplate.getAssetPath) {
return compilation.mainTemplate.getAssetPath.apply(compilation.mainTemplate, args);
}
args.unshift("asset-path");
return compilation.mainTemplate.applyPluginsWaterfall.apply(compilation.mainTemplate, args);
}

module.exports.computeIntegrity = computeIntegrity;
module.exports.findChunks = findChunks;
module.exports.filterTag = filterTag;
Expand All @@ -143,4 +110,3 @@ module.exports.normalizePath = normalizePath;
module.exports.getIntegrityChecksumForAsset = getIntegrityChecksumForAsset;
module.exports.isRuntimeChunk = isRuntimeChunk;
module.exports.makePlaceholder = makePlaceholder;
module.exports.getChunkFilename = getChunkFilename;

0 comments on commit 4ede6a6

Please sign in to comment.