Skip to content

Commit

Permalink
Ignore assets removed by other plugins
Browse files Browse the repository at this point in the history
Also, make an effort to ensure our chunkAsset callback runs after
callbacks registered by other plugins.

Closes #106
  • Loading branch information
jscheid committed Sep 27, 2019
1 parent 7acf47e commit 5b43a3e
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
3 changes: 3 additions & 0 deletions examples/webpack-fix-style-only-entries/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# With webpack-fix-style-only-entries

Test case for issue #106
1 change: 1 addition & 0 deletions examples/webpack-fix-style-only-entries/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('ok');
Empty file.
13 changes: 13 additions & 0 deletions examples/webpack-fix-style-only-entries/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var expect = require('expect');
var fs = require('fs');
var path = require('path');

var webpackVersion = Number(require('webpack/package.json').version.split('.')[0]);

module.exports.skip = function skip() {
return webpackVersion < 4;
};

module.exports.check = function check(stats) {
expect(stats.compilation.warnings.length).toEqual(0);
};
43 changes: 43 additions & 0 deletions examples/webpack-fix-style-only-entries/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var SriPlugin = require('webpack-subresource-integrity');
var MiniCssExtractPlugin = require('mini-css-extract-plugin');
var WebpackAssetsManifest = require('webpack-assets-manifest');
var FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');

module.exports = {
entry: {
index: './index.js',
style: ["./style.css"],
},
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 1
}
}
]
},
],
},
output: {
crossOriginLoading: 'anonymous'
},
plugins: [
new FixStyleOnlyEntriesPlugin({
silent: true
}),
new MiniCssExtractPlugin({
filename: "[name].css",
}),
new WebpackAssetsManifest({integrity: true}),
new SriPlugin({
hashFuncNames: ['sha256', 'sha384'],
enabled: true
}),
]
};
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ 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;
if (compilation.assets[asset]) {
// eslint-disable-next-line no-param-reassign
compilation.sriChunkAssets[chunk.id] = asset;
}
};

SubresourceIntegrityPlugin.prototype.addMissingIntegrityHashes =
Expand Down Expand Up @@ -344,7 +346,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 beforeChunkAssets = this.beforeChunkAssets.bind(this, compilation);
var alterAssetTags = this.alterAssetTags.bind(this, compilation);
var beforeHtmlGeneration = this.beforeHtmlGeneration.bind(this, compilation);

Expand All @@ -366,16 +368,25 @@ SubresourceIntegrityPlugin.prototype.thisCompilation =
*/
if (compiler.hooks) {
compilation.hooks.afterOptimizeAssets.tap('SriPlugin', afterOptimizeAssets);
compilation.hooks.chunkAsset.tap('SriPlugin', chunkAsset);
compilation.hooks.beforeChunkAssets.tap('SriPlugin', beforeChunkAssets);
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('before-chunk-assets', beforeChunkAssets);
compilation.plugin('html-webpack-plugin-alter-asset-tags', alterAssetTags);
compilation.plugin('html-webpack-plugin-before-html-generation', beforeHtmlGeneration);
}
};

SubresourceIntegrityPlugin.prototype.beforeChunkAssets = function afterPlugins(compilation) {
var chunkAsset = this.chunkAsset.bind(this, compilation);
if (compilation.hooks) {
compilation.hooks.chunkAsset.tap('SriPlugin', chunkAsset);
} else {
compilation.plugin('chunk-asset', chunkAsset);
}
};

SubresourceIntegrityPlugin.prototype.afterPlugins = function afterPlugins(compiler) {
if (compiler.hooks) {
compiler.hooks.thisCompilation.tap('SriPlugin', this.thisCompilation.bind(this, compiler));
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"style-loader": "^0.18.0",
"tmp": "^0.0.31",
"webpack": "^1.12.11",
"webpack-assets-manifest": "^3.0.0"
"webpack-assets-manifest": "^3.0.0",
"webpack-fix-style-only-entries": "^0.4.0"
},
"peerDependencies": {
"html-webpack-plugin": "^2.21.0 || ~3 || >=4.0.0-alpha.2 <5",
Expand Down

0 comments on commit 5b43a3e

Please sign in to comment.