From 5b43a3eb41243687930187485d03ef697e028891 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Fri, 27 Sep 2019 20:48:24 +1200 Subject: [PATCH] Ignore assets removed by other plugins Also, make an effort to ensure our chunkAsset callback runs after callbacks registered by other plugins. Closes #106 --- .../webpack-fix-style-only-entries/README.md | 3 ++ .../webpack-fix-style-only-entries/index.js | 1 + .../webpack-fix-style-only-entries/style.css | 0 .../webpack-fix-style-only-entries/test.js | 13 ++++++ .../webpack.config.js | 43 +++++++++++++++++++ index.js | 21 ++++++--- package.json | 3 +- 7 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 examples/webpack-fix-style-only-entries/README.md create mode 100644 examples/webpack-fix-style-only-entries/index.js create mode 100644 examples/webpack-fix-style-only-entries/style.css create mode 100644 examples/webpack-fix-style-only-entries/test.js create mode 100644 examples/webpack-fix-style-only-entries/webpack.config.js diff --git a/examples/webpack-fix-style-only-entries/README.md b/examples/webpack-fix-style-only-entries/README.md new file mode 100644 index 0000000..8e66da2 --- /dev/null +++ b/examples/webpack-fix-style-only-entries/README.md @@ -0,0 +1,3 @@ +# With webpack-fix-style-only-entries + +Test case for issue #106 diff --git a/examples/webpack-fix-style-only-entries/index.js b/examples/webpack-fix-style-only-entries/index.js new file mode 100644 index 0000000..3710888 --- /dev/null +++ b/examples/webpack-fix-style-only-entries/index.js @@ -0,0 +1 @@ +console.log('ok'); diff --git a/examples/webpack-fix-style-only-entries/style.css b/examples/webpack-fix-style-only-entries/style.css new file mode 100644 index 0000000..e69de29 diff --git a/examples/webpack-fix-style-only-entries/test.js b/examples/webpack-fix-style-only-entries/test.js new file mode 100644 index 0000000..de2d9cb --- /dev/null +++ b/examples/webpack-fix-style-only-entries/test.js @@ -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); +}; diff --git a/examples/webpack-fix-style-only-entries/webpack.config.js b/examples/webpack-fix-style-only-entries/webpack.config.js new file mode 100644 index 0000000..258987a --- /dev/null +++ b/examples/webpack-fix-style-only-entries/webpack.config.js @@ -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 + }), + ] +}; diff --git a/index.js b/index.js index b4f2634..e85f09c 100644 --- a/index.js +++ b/index.js @@ -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 = @@ -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); @@ -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)); diff --git a/package.json b/package.json index 0ef549d..30df527 100644 --- a/package.json +++ b/package.json @@ -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",