From 5dd739ef17c416574c59e415ed5beecbc03fa00d Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Fri, 6 Dec 2019 22:45:01 +1300 Subject: [PATCH 01/26] Webpack 5 compatibility --- .travis.yml | 1 + README.md | 18 ++- examples/disabled/webpack.config.js | 14 +- examples/etp-content-hash/test.js | 8 ++ examples/etp-path-join/test.js | 8 ++ examples/hwp-custom-template/test.js | 8 ++ examples/hwp-integrity-attributes/test.js | 8 ++ examples/hwp-output-parent-dir/test.js | 5 +- examples/hwp-public-path/test.js | 4 +- examples/hwp-subdirectories/test.js | 4 +- examples/mini-css-extract-plugin/test.js | 2 +- examples/no-error-invalid-config/test.js | 8 ++ .../no-error-invalid-config/webpack.config.js | 1 - index.js | 34 +++-- jmtp.js | 122 ++++++++++-------- 15 files changed, 160 insertions(+), 85 deletions(-) diff --git a/.travis.yml b/.travis.yml index 878c310..d3c2ab8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ env: - WEBPACK_SUFFIX="@3" ETWP_SUFFIX="@3" HWP_SUFFIX="@2" - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3" - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2" + - WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2" matrix: exclude: - node_js: 4 diff --git a/README.md b/README.md index b57debe..8d94f94 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ manipulation. ## Features -- Optional integration with [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin) -- Automatic support for code splitting (integrity for lazy-loaded chunks) -- Compatible with Webpack 1.x, 2.x, 3.x and 4.x +- Optional integration with [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin). +- Automatic support for dynamic imports (also known as code splitting.) +- Compatible with all major Webpack versions, up to and including Webpack 5. ## Installation @@ -21,6 +21,10 @@ manipulation. npm install webpack-subresource-integrity --save-dev ``` +```shell +yarn add --dev webpack-subresource-integrity +``` + ### Webpack Configuration Example ```javascript @@ -81,9 +85,11 @@ template as follows: #### Without HtmlWebpackPlugin The correct value for the `integrity` attribute can be retrieved from -the `integrity` property of Webpack assets. However, that property is -not copied over by Webpack's `stats` module so you'll have to access -the "original" asset on the `compilation` object. For example: +the `integrity` property of Webpack assets. + +Note that with Webpack versions before 5, that property is not copied over by +Webpack's `stats` module so you'll have to access the "original" asset on the +`compilation` object. For example: ```javascript compiler.plugin("done", stats => { diff --git a/examples/disabled/webpack.config.js b/examples/disabled/webpack.config.js index 45c0037..899d1c2 100644 --- a/examples/disabled/webpack.config.js +++ b/examples/disabled/webpack.config.js @@ -1,21 +1,9 @@ var SriPlugin = require('webpack-subresource-integrity'); -var ExtractTextPlugin = require('extract-text-webpack-plugin'); -var createExtractTextLoader = require('../utils').createExtractTextLoader; -var webpackVersion = Number( - require('webpack/package.json').version.split('.')[0] -); module.exports = { entry: './index.js', output: { filename: 'bundle.js' }, - module: - webpackVersion > 1 - ? { rules: [{ test: /\.css$/, use: createExtractTextLoader() }] } - : { loaders: [{ test: /\.css$/, loader: createExtractTextLoader() }] }, - plugins: [ - new ExtractTextPlugin('styles.css'), - new SriPlugin({ hashFuncNames: ['sha256'], enabled: false }) - ] + plugins: [new SriPlugin({ hashFuncNames: ['sha256'], enabled: false })] }; diff --git a/examples/etp-content-hash/test.js b/examples/etp-content-hash/test.js index bbd399a..43d566e 100644 --- a/examples/etp-content-hash/test.js +++ b/examples/etp-content-hash/test.js @@ -1,4 +1,12 @@ var expect = require('expect'); +var webpackVersion = Number( + require('webpack/package.json').version.split('.')[0] +); + +module.exports.skip = function skip() { + // Can't use extract-text-webpack-plugin with Webpack > 4. + return webpackVersion > 4; +}; module.exports.check = function check(stats) { expect(stats.compilation.warnings).toEqual([]); diff --git a/examples/etp-path-join/test.js b/examples/etp-path-join/test.js index bbd399a..43d566e 100644 --- a/examples/etp-path-join/test.js +++ b/examples/etp-path-join/test.js @@ -1,4 +1,12 @@ var expect = require('expect'); +var webpackVersion = Number( + require('webpack/package.json').version.split('.')[0] +); + +module.exports.skip = function skip() { + // Can't use extract-text-webpack-plugin with Webpack > 4. + return webpackVersion > 4; +}; module.exports.check = function check(stats) { expect(stats.compilation.warnings).toEqual([]); diff --git a/examples/hwp-custom-template/test.js b/examples/hwp-custom-template/test.js index df068a9..90f9f14 100644 --- a/examples/hwp-custom-template/test.js +++ b/examples/hwp-custom-template/test.js @@ -2,6 +2,14 @@ var select = require('soupselect').select; var expect = require('expect'); var htmlparser = require('htmlparser'); var fs = require('fs'); +var webpackVersion = Number( + require('webpack/package.json').version.split('.')[0] +); + +module.exports.skip = function skip() { + // Can't use extract-text-webpack-plugin with Webpack > 4. + return webpackVersion > 4; +}; module.exports.check = function check(stats) { var jsIntegrity; diff --git a/examples/hwp-integrity-attributes/test.js b/examples/hwp-integrity-attributes/test.js index c915cef..1c980ea 100644 --- a/examples/hwp-integrity-attributes/test.js +++ b/examples/hwp-integrity-attributes/test.js @@ -2,6 +2,14 @@ var select = require('soupselect').select; var expect = require('expect'); var htmlparser = require('htmlparser'); var fs = require('fs'); +var webpackVersion = Number( + require('webpack/package.json').version.split('.')[0] +); + +module.exports.skip = function skip() { + // Can't use extract-text-webpack-plugin with Webpack > 4. + return webpackVersion > 4; +}; module.exports.check = function check(stats) { var jsIntegrity; diff --git a/examples/hwp-output-parent-dir/test.js b/examples/hwp-output-parent-dir/test.js index 80e6d55..d28e897 100644 --- a/examples/hwp-output-parent-dir/test.js +++ b/examples/hwp-output-parent-dir/test.js @@ -7,7 +7,10 @@ module.exports.check = function check(stats) { var jsIntegrity; expect(stats.compilation.warnings).toEqual([]); - jsIntegrity = stats.compilation.assets['bundle.js'].integrity; + + jsIntegrity = + stats.toJson().assets.find(asset => asset.name === 'bundle.js').integrity || + stats.compilation.assets['bundle.js'].integrity; expect(jsIntegrity).toMatch(/^sha/); return new Promise((resolve, reject) => { diff --git a/examples/hwp-public-path/test.js b/examples/hwp-public-path/test.js index 17b6eb9..dec2f23 100644 --- a/examples/hwp-public-path/test.js +++ b/examples/hwp-public-path/test.js @@ -7,7 +7,9 @@ module.exports.check = function check(stats) { var jsIntegrity; expect(stats.compilation.warnings).toEqual([]); - jsIntegrity = stats.compilation.assets['bundle.js'].integrity; + jsIntegrity = + stats.toJson().assets.find(asset => asset.name === 'bundle.js').integrity || + stats.compilation.assets['bundle.js'].integrity; expect(jsIntegrity).toMatch(/^sha/); return new Promise((resolve, reject) => { diff --git a/examples/hwp-subdirectories/test.js b/examples/hwp-subdirectories/test.js index f100937..acf4112 100644 --- a/examples/hwp-subdirectories/test.js +++ b/examples/hwp-subdirectories/test.js @@ -4,7 +4,9 @@ var htmlparser = require('htmlparser'); var fs = require('fs'); module.exports.check = function check(stats) { - var jsIntegrity = stats.compilation.assets['subdir/bundle.js'].integrity; + var jsIntegrity = + stats.toJson().assets.find(asset => asset.name === 'subdir/bundle.js') + .integrity || stats.compilation.assets['subdir/bundle.js'].integrity; expect(jsIntegrity).toMatch(/^sha/); return new Promise((resolve, reject) => { diff --git a/examples/mini-css-extract-plugin/test.js b/examples/mini-css-extract-plugin/test.js index 1a5fdd3..3751289 100644 --- a/examples/mini-css-extract-plugin/test.js +++ b/examples/mini-css-extract-plugin/test.js @@ -4,5 +4,5 @@ var webpackVersion = Number( module.exports.skip = function skip() { // mini-css-extract-plugin needs Webpack 4 - return webpackVersion < 4; + return webpackVersion !== 4; }; diff --git a/examples/no-error-invalid-config/test.js b/examples/no-error-invalid-config/test.js index e450ee5..94c8df1 100644 --- a/examples/no-error-invalid-config/test.js +++ b/examples/no-error-invalid-config/test.js @@ -1,5 +1,13 @@ var expect = require('expect'); var ChunkRenderError = require('webpack/lib/ChunkRenderError'); +var webpackVersion = Number( + require('webpack/package.json').version.split('.')[0] +); + +module.exports.skip = function skip() { + // Error doesn't get triggered with Webpack > 4. + return webpackVersion > 4; +}; module.exports.ignoreErrors = true; diff --git a/examples/no-error-invalid-config/webpack.config.js b/examples/no-error-invalid-config/webpack.config.js index 5a63543..9416156 100644 --- a/examples/no-error-invalid-config/webpack.config.js +++ b/examples/no-error-invalid-config/webpack.config.js @@ -1,5 +1,4 @@ var SriPlugin = require('webpack-subresource-integrity'); -var HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: './index.js', diff --git a/index.js b/index.js index e85f09c..ce97f3d 100644 --- a/index.js +++ b/index.js @@ -41,6 +41,8 @@ function SubresourceIntegrityPlugin(options) { Object.assign(this.options, useOptions); this.emittedMessages = {}; + + this.assetIntegrity = new Map(); } SubresourceIntegrityPlugin.prototype.emitMessage = function emitMessage(messages, message) { @@ -203,13 +205,14 @@ SubresourceIntegrityPlugin.prototype.processChunk = function processChunk( Array.from(util.findChunks(chunk)).reverse().forEach(childChunk => { var sourcePath; + var files = Array.isArray(childChunk.files) ? new Set(childChunk.files) : childChunk.files; // This can happen with invalid Webpack configurations - if (childChunk.files.length === 0) return; + if (files.size === 0) return; sourcePath = compilation.sriChunkAssets[childChunk.id]; - if (childChunk.files.indexOf(sourcePath) < 0) { + if (!files.has(sourcePath)) { self.warnOnce( compilation, 'Cannot determine asset for chunk ' + childChunk.id + ', computed="' + sourcePath + @@ -217,15 +220,13 @@ SubresourceIntegrityPlugin.prototype.processChunk = function processChunk( 'along with your Webpack configuration at ' + 'https://github.com/waysact/webpack-subresource-integrity/issues/new' ); - sourcePath = childChunk.files[0]; + sourcePath = Array.from(files)[0]; } self.warnIfHotUpdate(compilation, assets[sourcePath].source()); - newAsset = self.replaceAsset( - assets, - hashByChunkId, - sourcePath); + newAsset = self.replaceAsset(assets, hashByChunkId, sourcePath); hashByChunkId.set(childChunk.id, newAsset.integrity); + this.assetIntegrity.set(sourcePath, newAsset.integrity); }); }; @@ -237,6 +238,16 @@ SubresourceIntegrityPlugin.prototype.chunkAsset = } }; +SubresourceIntegrityPlugin.prototype.statsFactory = + function stats(compilation, statsFactory) { + statsFactory.hooks.extract.for("asset").tap('SriPlugin', (object, asset) => { + if (this.assetIntegrity.has(asset.name)) { + // eslint-disable-next-line no-param-reassign + object.integrity = String(this.assetIntegrity.get(asset.name)); + } + }); + }; + SubresourceIntegrityPlugin.prototype.addMissingIntegrityHashes = function addMissingIntegrityHashes(assets) { var self = this; @@ -256,7 +267,7 @@ SubresourceIntegrityPlugin.prototype.afterOptimizeAssets = function afterOptimizeAssets(compilation, assets) { var self = this; - compilation.chunks.filter(util.isRuntimeChunk).forEach(function forEachChunk(chunk) { + Array.from(compilation.chunks).filter(util.isRuntimeChunk).forEach(function forEachChunk(chunk) { self.processChunk(chunk, compilation, assets); }); @@ -390,6 +401,13 @@ SubresourceIntegrityPlugin.prototype.beforeChunkAssets = function afterPlugins(c SubresourceIntegrityPlugin.prototype.afterPlugins = function afterPlugins(compiler) { if (compiler.hooks) { compiler.hooks.thisCompilation.tap('SriPlugin', this.thisCompilation.bind(this, compiler)); + + compiler.hooks.compilation.tap("DefaultStatsFactoryPlugin", compilation => { + if (compilation.hooks.statsFactory) { + compilation.hooks.statsFactory.tap('SriPlugin', this.statsFactory.bind(this, compilation)); + } + }); + } else { compiler.plugin('this-compilation', this.thisCompilation.bind(this, compiler)); } diff --git a/jmtp.js b/jmtp.js index 3da48b7..1c392af 100644 --- a/jmtp.js +++ b/jmtp.js @@ -5,81 +5,97 @@ * LICENSE file in the root directory of this source tree. */ +var webpack = require('webpack'); var Template = require('webpack/lib/Template'); var util = require('./util'); +function computeSriHashes(chunk, hashFuncNames) { + var allChunks = util.findChunks(chunk); + var includedChunks = new Set( + (chunk.getAllAsyncChunks + ? Array.from(chunk.getAllAsyncChunks()).map(c => c.id) + : Object.keys(chunk.getChunkMaps().hash) + ).map(id => id.toString()) + ); + + return Array.from(allChunks).reduce(function chunkIdReducer( + sriHashes, + depChunk + ) { + if (includedChunks.has(depChunk.id.toString())) { + // eslint-disable-next-line no-param-reassign + sriHashes[depChunk.id] = util.makePlaceholder(hashFuncNames, depChunk.id); + } + return sriHashes; + }, {}); +} + +function getCompilationHooks(compilation, mainTemplate) { + if (webpack.web && + webpack.web.JsonpTemplatePlugin && + webpack.web.JsonpTemplatePlugin.getCompilationHooks) { + return webpack.web.JsonpTemplatePlugin.getCompilationHooks( + compilation + ); + } + return mainTemplate.hooks; +} + function WebIntegrityJsonpMainTemplatePlugin(sriPlugin, compilation) { this.sriPlugin = sriPlugin; this.compilation = compilation; } -WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes = - function addSriHashes(mainTemplate, source, chunk) { - var allChunks = util.findChunks(chunk); - var includedChunks = chunk.getChunkMaps().hash; - var hashFuncNames = this.sriPlugin.options.hashFuncNames; - - if (allChunks.size > 0) { - return (Template.asString || mainTemplate.asString)([ - source, - 'var sriHashes = ' + - JSON.stringify( - Array.from(allChunks).reduce(function chunkIdReducer( - sriHashes, - depChunk - ) { - if (includedChunks[depChunk.id.toString()]) { - // eslint-disable-next-line no-param-reassign - sriHashes[depChunk.id] = util.makePlaceholder(hashFuncNames, depChunk.id); - } - return sriHashes; - }, - {}) - ) + - ';' - ]); - } - - return source; - }; - /* * Patch jsonp-script code to add the integrity attribute. */ -WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute = - function addAttribute(mainTemplate, elName, source) { - if (!mainTemplate.outputOptions.crossOriginLoading) { - this.sriPlugin.errorOnce( - this.compilation, - 'webpack option output.crossOriginLoading not set, code splitting will not work!' - ); - } - return (Template.asString || mainTemplate.asString)([ - source, - elName + '.integrity = sriHashes[chunkId];', - elName + '.crossOrigin = ' + JSON.stringify(mainTemplate.outputOptions.crossOriginLoading) + ';', - ]); - }; +WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute = function addAttribute( + mainTemplate, + elName, + source, + chunk +) { + var hashes = computeSriHashes(chunk, this.sriPlugin.options.hashFuncNames); + + var outputOptions = this.compilation.outputOptions || mainTemplate.outputOptions; + + if (!outputOptions.crossOriginLoading) { + this.sriPlugin.errorOnce( + this.compilation, + 'webpack option output.crossOriginLoading not set, code splitting will not work!' + ); + } + + return (Template.asString || mainTemplate.asString)([ + source, + elName === "script" ? ('var sriHashes = ' + JSON.stringify(hashes) + ';') : '', + elName + '.integrity = sriHashes[chunkId];', + elName + '.crossOrigin = ' + + JSON.stringify(outputOptions.crossOriginLoading) + + ';' + ]); +}; WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply( mainTemplate ) { var jsonpScriptPlugin = this.addAttribute.bind(this, mainTemplate, "script"); var linkPreloadPlugin = this.addAttribute.bind(this, mainTemplate, "link"); - var addSriHashes = this.addSriHashes.bind(this, mainTemplate); + var compilationHooks = getCompilationHooks(this.compilation, mainTemplate); - if (!mainTemplate.hooks) { - mainTemplate.plugin('jsonp-script', jsonpScriptPlugin); - mainTemplate.plugin('local-vars', addSriHashes); - } else if (mainTemplate.hooks.jsonpScript && mainTemplate.hooks.localVars) { - mainTemplate.hooks.jsonpScript.tap('SriPlugin', jsonpScriptPlugin); - mainTemplate.hooks.linkPreload.tap('SriPlugin', linkPreloadPlugin); - mainTemplate.hooks.localVars.tap('SriPlugin', addSriHashes); - } else { + if (this.compilation.compiler.options.target !== 'web') { this.sriPlugin.warnOnce( this.compilation, 'This plugin is not useful for non-web targets.' ); + return; + } + + if (!mainTemplate.hooks) { + mainTemplate.plugin('jsonp-script', jsonpScriptPlugin); + } else { + compilationHooks.jsonpScript.tap('SriPlugin', jsonpScriptPlugin); + compilationHooks.linkPreload.tap('SriPlugin', linkPreloadPlugin); } }; From 9fc191fb3b9025b88f40ff6d58524a1ca605c529 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Fri, 6 Dec 2019 23:02:20 +1300 Subject: [PATCH 02/26] Fix CI - Don't run Webpack 5 tests on Node 4 or 6 - Don't run Karma in CI - Test with Node 12 --- .travis.yml | 5 +++++ appveyor.yml | 40 ++++++++++++++++++++++++++++++++++++++++ package.json | 4 ++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index d3c2ab8..79ea393 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ node_js: - 6 - 8 - 10 + - 12 addons: firefox: latest env: @@ -19,6 +20,10 @@ matrix: env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3" - node_js: 4 env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2" + - node_js: 4 + env: WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2" + - node_js: 6 + env: WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2" install: - npm install || true - npm rm webpack extract-text-webpack-plugin eslint diff --git a/appveyor.yml b/appveyor.yml index 1a7f45c..8151b36 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -66,6 +66,11 @@ environment: ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' HWP_SUFFIX: '@4.0.0-alpha.2' + - nodejs_version: 8 + WEBPACK_SUFFIX: '@5.0.0-beta.7' + ETWP_SUFFIX: '@4.0.0-alpha.0' + FL_SUFFIX: '@1' + HWP_SUFFIX: '@4.0.0-alpha.2' - nodejs_version: 10 WEBPACK_SUFFIX: '@1' ETWP_SUFFIX: '@1' @@ -91,6 +96,41 @@ environment: ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' HWP_SUFFIX: '@4.0.0-alpha.2' + - nodejs_version: 10 + WEBPACK_SUFFIX: '@5.0.0-beta.7' + ETWP_SUFFIX: '@4.0.0-alpha.0' + FL_SUFFIX: '@1' + HWP_SUFFIX: '@4.0.0-alpha.2' + - nodejs_version: 12 + WEBPACK_SUFFIX: '@1' + ETWP_SUFFIX: '@1' + FL_SUFFIX: '@0' + HWP_SUFFIX: '@2' + - nodejs_version: 12 + WEBPACK_SUFFIX: '@2' + ETWP_SUFFIX: '@2' + FL_SUFFIX: '@1' + HWP_SUFFIX: '@2' + - nodejs_version: 12 + WEBPACK_SUFFIX: '@3' + ETWP_SUFFIX: '@3' + FL_SUFFIX: '@1' + HWP_SUFFIX: '@2' + - nodejs_version: 12 + WEBPACK_SUFFIX: '@4' + ETWP_SUFFIX: '@4.0.0-alpha.0' + FL_SUFFIX: '@1' + HWP_SUFFIX: '@3' + - nodejs_version: 12 + WEBPACK_SUFFIX: '@4' + ETWP_SUFFIX: '@4.0.0-alpha.0' + FL_SUFFIX: '@1' + HWP_SUFFIX: '@4.0.0-alpha.2' + - nodejs_version: 12 + WEBPACK_SUFFIX: '@5.0.0-beta.7' + ETWP_SUFFIX: '@4.0.0-alpha.0' + FL_SUFFIX: '@1' + HWP_SUFFIX: '@4.0.0-alpha.2' # Install scripts. (runs after repo cloning) install: diff --git a/package.json b/package.json index 7600d5e..df86764 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,8 @@ "scripts": { "codeclimate": "docker run --interactive --tty --rm --env CODECLIMATE_CODE=\"$PWD\" --volume \"$PWD\":/code --volume /var/run/docker.sock:/var/run/docker.sock --volume /tmp/cc:/tmp/cc codeclimate/codeclimate", "coverage": "nyc $(npm bin)/mocha --exit --timeout 20000", - "test": "karma start --single-run && mocha --exit --timeout 20000", - "mocha": "mocha --exit --timeout 20000", + "karma": "karma start --single-run", + "test": "mocha --exit --timeout 20000", "lint": "eslint .", "prettier": "prettier --write '**/*.js'" }, From 1875c0b8d412cdb7acec34eb996c1c0373f7d3f1 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Fri, 6 Dec 2019 23:05:29 +1300 Subject: [PATCH 03/26] Declare compatibility with Webpack 5.0.0 beta --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index df86764..a280e7c 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ "soupselect": "^0.2.0", "style-loader": "^0.18.0", "tmp": "^0.0.31", - "webpack": "^1.12.11", + "webpack": "^1.12.11 || >= 5.0.0-beta.7 < 6.0", "webpack-assets-manifest": "^3.0.0", "webpack-fix-style-only-entries": "^0.4.0" }, From 6fb9390dd5d0c42f5ce03642e70f69de5416b739 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Fri, 6 Dec 2019 23:21:33 +1300 Subject: [PATCH 04/26] More CI fixes --- .travis.yml | 1 - appveyor.yml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 79ea393..b14f0e4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,7 +35,6 @@ before_script: services: - xvfb script: - - ./node_modules/karma/bin/karma start --single-run --browsers Firefox - $(npm bin)/nyc $(npm bin)/mocha --exit --timeout 20000 && $(npm bin)/nyc report --reporter=text-lcov | $(npm bin)/coveralls notifications: webhooks: diff --git a/appveyor.yml b/appveyor.yml index 8151b36..ced7cc7 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -151,7 +151,7 @@ test_script: - node --version - npm --version # run tests - - npm run mocha + - npm run test # Don't actually build. build: off From a76916ae6047a91d34ebafdd8039c23bb90a3a9d Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Fri, 6 Dec 2019 23:39:34 +1300 Subject: [PATCH 05/26] Run with latest html-webpack-plugin beta --- .travis.yml | 10 +++++----- appveyor.yml | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index b14f0e4..babcf17 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,18 +12,18 @@ env: - WEBPACK_SUFFIX="@2" ETWP_SUFFIX="@2" HWP_SUFFIX="@2" - WEBPACK_SUFFIX="@3" ETWP_SUFFIX="@3" HWP_SUFFIX="@2" - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3" - - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2" - - WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2" + - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" + - WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" matrix: exclude: - node_js: 4 env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3" - node_js: 4 - env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2" + env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" - node_js: 4 - env: WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2" + env: WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" - node_js: 6 - env: WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-alpha.2" + env: WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" install: - npm install || true - npm rm webpack extract-text-webpack-plugin eslint diff --git a/appveyor.yml b/appveyor.yml index ced7cc7..6554450 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -40,7 +40,7 @@ environment: WEBPACK_SUFFIX: '@4' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-alpha.2' + HWP_SUFFIX: '@4.0.0-beta.11' - nodejs_version: 8 WEBPACK_SUFFIX: '@1' ETWP_SUFFIX: '@1' @@ -65,12 +65,12 @@ environment: WEBPACK_SUFFIX: '@4' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-alpha.2' + HWP_SUFFIX: '@4.0.0-beta.11' - nodejs_version: 8 WEBPACK_SUFFIX: '@5.0.0-beta.7' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-alpha.2' + HWP_SUFFIX: '@4.0.0-beta.11' - nodejs_version: 10 WEBPACK_SUFFIX: '@1' ETWP_SUFFIX: '@1' @@ -95,12 +95,12 @@ environment: WEBPACK_SUFFIX: '@4' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-alpha.2' + HWP_SUFFIX: '@4.0.0-beta.11' - nodejs_version: 10 WEBPACK_SUFFIX: '@5.0.0-beta.7' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-alpha.2' + HWP_SUFFIX: '@3' - nodejs_version: 12 WEBPACK_SUFFIX: '@1' ETWP_SUFFIX: '@1' @@ -125,12 +125,12 @@ environment: WEBPACK_SUFFIX: '@4' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-alpha.2' + HWP_SUFFIX: '@4.0.0-beta.11' - nodejs_version: 12 WEBPACK_SUFFIX: '@5.0.0-beta.7' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-alpha.2' + HWP_SUFFIX: '@3' # Install scripts. (runs after repo cloning) install: From be5a5eb625131917845a992d86f40cee1255344b Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Sat, 7 Dec 2019 08:03:54 +1300 Subject: [PATCH 06/26] Fix webpack dependency spec --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a280e7c..35e93f4 100644 --- a/package.json +++ b/package.json @@ -71,13 +71,13 @@ "soupselect": "^0.2.0", "style-loader": "^0.18.0", "tmp": "^0.0.31", - "webpack": "^1.12.11 || >= 5.0.0-beta.7 < 6.0", + "webpack": "^1.12.11", "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", - "webpack": "^1.12.11 || ~2 || ~3 || ~4" + "webpack": "^1.12.11 || ~2 || ~3 || ~4 || >= 5.0.0-beta.7 <6" }, "peerDependenciesMeta": { "html-webpack-plugin": { From 25c785c9c940e6ce6bab815464a407f377686ad5 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Sun, 8 Dec 2019 17:28:49 +1300 Subject: [PATCH 07/26] Fix Appveyor configuration --- appveyor.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 6554450..a6b2bfc 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -100,7 +100,7 @@ environment: WEBPACK_SUFFIX: '@5.0.0-beta.7' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@3' + HWP_SUFFIX: '@4.0.0-beta.11' - nodejs_version: 12 WEBPACK_SUFFIX: '@1' ETWP_SUFFIX: '@1' @@ -130,7 +130,7 @@ environment: WEBPACK_SUFFIX: '@5.0.0-beta.7' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@3' + HWP_SUFFIX: '@4.0.0-beta.11' # Install scripts. (runs after repo cloning) install: From ee4974e19bda2f72bf58d94697c35c6f3a3200e6 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Sun, 5 Jul 2020 09:07:23 +1200 Subject: [PATCH 08/26] Compatibility with webpack@5.0.0-beta20 --- .travis.yml | 4 +- examples/sourcemap-code-splitting/test.js | 2 +- jmtp.js | 112 +++++++++++----------- 3 files changed, 59 insertions(+), 59 deletions(-) diff --git a/.travis.yml b/.travis.yml index babcf17..591882d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,9 +21,9 @@ matrix: - node_js: 4 env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" - node_js: 4 - env: WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" + env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" - node_js: 6 - env: WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" + env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" install: - npm install || true - npm rm webpack extract-text-webpack-plugin eslint diff --git a/examples/sourcemap-code-splitting/test.js b/examples/sourcemap-code-splitting/test.js index 698ba06..5b2ac25 100644 --- a/examples/sourcemap-code-splitting/test.js +++ b/examples/sourcemap-code-splitting/test.js @@ -17,6 +17,6 @@ module.exports.check = function check() { } var sriHashesInSource = findAndStripSriHashString('dist/index.js', 'sha256-', -10); - var sriHashesInMap = findAndStripSriHashString('dist/index.js.map', 'var sriHashes = '); + var sriHashesInMap = findAndStripSriHashString('dist/index.js.map', '__webpack_require__.sriHashes = '); expect(sriHashesInSource.length).toEqual(sriHashesInMap.length); }; diff --git a/jmtp.js b/jmtp.js index 1c392af..a6dbfc6 100644 --- a/jmtp.js +++ b/jmtp.js @@ -9,26 +9,58 @@ var webpack = require('webpack'); var Template = require('webpack/lib/Template'); var util = require('./util'); -function computeSriHashes(chunk, hashFuncNames) { - var allChunks = util.findChunks(chunk); - var includedChunks = new Set( - (chunk.getAllAsyncChunks - ? Array.from(chunk.getAllAsyncChunks()).map(c => c.id) - : Object.keys(chunk.getChunkMaps().hash) - ).map(id => id.toString()) - ); +function WebIntegrityJsonpMainTemplatePlugin(sriPlugin, compilation) { + this.sriPlugin = sriPlugin; + this.compilation = compilation; +} + +WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes = + function addSriHashes(mainTemplate, source, chunk) { + var allChunks = util.findChunks(chunk); + var includedChunks = chunk.getChunkMaps().hash; + var hashFuncNames = this.sriPlugin.options.hashFuncNames; - return Array.from(allChunks).reduce(function chunkIdReducer( - sriHashes, - depChunk - ) { - if (includedChunks.has(depChunk.id.toString())) { - // eslint-disable-next-line no-param-reassign - sriHashes[depChunk.id] = util.makePlaceholder(hashFuncNames, depChunk.id); + if (Object.keys(includedChunks).length > 0) { + return (Template.asString || mainTemplate.asString)([ + source, + '__webpack_require__.sriHashes = ' + + JSON.stringify( + Array.from(allChunks).reduce(function chunkIdReducer( + sriHashes, + depChunk + ) { + if (includedChunks[depChunk.id.toString()]) { + // eslint-disable-next-line no-param-reassign + sriHashes[depChunk.id] = util.makePlaceholder(hashFuncNames, depChunk.id); + } + return sriHashes; + }, + {}) + ) + + ';' + ]); } - return sriHashes; - }, {}); -} + + return source; + }; + +/* + * Patch jsonp-script code to add the integrity attribute. + */ +WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute = + function addAttribute(mainTemplate, elName, source) { + if (!mainTemplate.outputOptions.crossOriginLoading) { + this.sriPlugin.errorOnce( + this.compilation, + 'webpack option output.crossOriginLoading not set, code splitting will not work!' + ); + } + return (Template.asString || mainTemplate.asString)([ + source, + elName + '.integrity = __webpack_require__.sriHashes[chunkId];', + elName + '.crossOrigin = ' + JSON.stringify(mainTemplate.outputOptions.crossOriginLoading) + ';', + ]); + }; function getCompilationHooks(compilation, mainTemplate) { if (webpack.web && @@ -41,46 +73,12 @@ function getCompilationHooks(compilation, mainTemplate) { return mainTemplate.hooks; } -function WebIntegrityJsonpMainTemplatePlugin(sriPlugin, compilation) { - this.sriPlugin = sriPlugin; - this.compilation = compilation; -} - -/* - * Patch jsonp-script code to add the integrity attribute. - */ -WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute = function addAttribute( - mainTemplate, - elName, - source, - chunk -) { - var hashes = computeSriHashes(chunk, this.sriPlugin.options.hashFuncNames); - - var outputOptions = this.compilation.outputOptions || mainTemplate.outputOptions; - - if (!outputOptions.crossOriginLoading) { - this.sriPlugin.errorOnce( - this.compilation, - 'webpack option output.crossOriginLoading not set, code splitting will not work!' - ); - } - - return (Template.asString || mainTemplate.asString)([ - source, - elName === "script" ? ('var sriHashes = ' + JSON.stringify(hashes) + ';') : '', - elName + '.integrity = sriHashes[chunkId];', - elName + '.crossOrigin = ' + - JSON.stringify(outputOptions.crossOriginLoading) + - ';' - ]); -}; - WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply( mainTemplate ) { var jsonpScriptPlugin = this.addAttribute.bind(this, mainTemplate, "script"); var linkPreloadPlugin = this.addAttribute.bind(this, mainTemplate, "link"); + var addSriHashes = this.addSriHashes.bind(this, mainTemplate); var compilationHooks = getCompilationHooks(this.compilation, mainTemplate); if (this.compilation.compiler.options.target !== 'web') { @@ -91,11 +89,13 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply( return; } - if (!mainTemplate.hooks) { - mainTemplate.plugin('jsonp-script', jsonpScriptPlugin); - } else { + if (compilationHooks) { compilationHooks.jsonpScript.tap('SriPlugin', jsonpScriptPlugin); compilationHooks.linkPreload.tap('SriPlugin', linkPreloadPlugin); + mainTemplate.hooks.localVars.tap('SriPlugin', addSriHashes); + } else { + mainTemplate.plugin('jsonp-script', jsonpScriptPlugin); + mainTemplate.plugin('local-vars', addSriHashes); } }; From 90c13776740539bc631684099c078814d085232e Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Sun, 5 Jul 2020 10:01:51 +1200 Subject: [PATCH 09/26] Fix travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 591882d..4adb137 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ env: - WEBPACK_SUFFIX="@3" ETWP_SUFFIX="@3" HWP_SUFFIX="@2" - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3" - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" - - WEBPACK_SUFFIX="@5.0.0-beta.7" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" + - WEBPACK_SUFFIX="@5.0.0-next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" matrix: exclude: - node_js: 4 From aaeaf568a295f125cbe61d7448fdd960b6915343 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Sun, 5 Jul 2020 13:02:30 +1200 Subject: [PATCH 10/26] Another fix for travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4adb137..555e609 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ env: - WEBPACK_SUFFIX="@3" ETWP_SUFFIX="@3" HWP_SUFFIX="@2" - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3" - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" - - WEBPACK_SUFFIX="@5.0.0-next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" + - WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" matrix: exclude: - node_js: 4 From c2de3da0a370c1cc4d7919a63eefe15b057d9b36 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Sun, 5 Jul 2020 15:13:14 +1200 Subject: [PATCH 11/26] Support for LoadScriptRuntimeModule --- jmtp.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/jmtp.js b/jmtp.js index a6dbfc6..e2c1e36 100644 --- a/jmtp.js +++ b/jmtp.js @@ -8,6 +8,17 @@ var webpack = require('webpack'); var Template = require('webpack/lib/Template'); var util = require('./util'); +var LoadScriptRuntimeModule; + +try { + // eslint-disable-next-line global-require + LoadScriptRuntimeModule = require('webpack/lib/runtime/LoadScriptRuntimeModule'); +} catch (e) { + // File doesn't exist in Webpack <5 + if (e.code !== 'MODULE_NOT_FOUND') { + throw e; + } +} function WebIntegrityJsonpMainTemplatePlugin(sriPlugin, compilation) { this.sriPlugin = sriPlugin; @@ -48,7 +59,7 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes = * Patch jsonp-script code to add the integrity attribute. */ WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute = - function addAttribute(mainTemplate, elName, source) { + function addAttribute(mainTemplate, elName, source, chunk) { if (!mainTemplate.outputOptions.crossOriginLoading) { this.sriPlugin.errorOnce( this.compilation, @@ -57,7 +68,7 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute = } return (Template.asString || mainTemplate.asString)([ source, - elName + '.integrity = __webpack_require__.sriHashes[chunkId];', + elName + '.integrity = __webpack_require__.sriHashes[' + (chunk ? `'${chunk.id}'` : 'chunkId') + '];', elName + '.crossOrigin = ' + JSON.stringify(mainTemplate.outputOptions.crossOriginLoading) + ';', ]); }; @@ -77,6 +88,7 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply( mainTemplate ) { var jsonpScriptPlugin = this.addAttribute.bind(this, mainTemplate, "script"); + var createScriptPlugin = this.addAttribute.bind(this, mainTemplate, "script"); var linkPreloadPlugin = this.addAttribute.bind(this, mainTemplate, "link"); var addSriHashes = this.addSriHashes.bind(this, mainTemplate); var compilationHooks = getCompilationHooks(this.compilation, mainTemplate); @@ -97,6 +109,13 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply( mainTemplate.plugin('jsonp-script', jsonpScriptPlugin); mainTemplate.plugin('local-vars', addSriHashes); } + + if (LoadScriptRuntimeModule) { + LoadScriptRuntimeModule + .getCompilationHooks(this.compilation) + .createScript + .tap('SriPlugin', createScriptPlugin); + } }; module.exports = WebIntegrityJsonpMainTemplatePlugin; From 44f124e5a6947e85c3c476af64acd61e9211513d Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Sun, 5 Jul 2020 16:13:18 +1200 Subject: [PATCH 12/26] Test against latest html-webpack-plugin@4 --- .travis.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 555e609..a7a5e87 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,18 +12,18 @@ env: - WEBPACK_SUFFIX="@2" ETWP_SUFFIX="@2" HWP_SUFFIX="@2" - WEBPACK_SUFFIX="@3" ETWP_SUFFIX="@3" HWP_SUFFIX="@2" - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3" - - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" - - WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" + - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" + - WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" matrix: exclude: - node_js: 4 env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3" - node_js: 4 - env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" + env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" - node_js: 4 - env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" + env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" - node_js: 6 - env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4.0.0-beta.11" + env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" install: - npm install || true - npm rm webpack extract-text-webpack-plugin eslint From ab9b84a7217ae83f7f26c50228f1fd2173382b95 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Sun, 5 Jul 2020 16:23:33 +1200 Subject: [PATCH 13/26] Exclude webpack@next on Node 8 from testing --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index a7a5e87..cb2ae81 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,6 +24,8 @@ matrix: env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" - node_js: 6 env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" + - node_js: 8 + env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" install: - npm install || true - npm rm webpack extract-text-webpack-plugin eslint From fb72eee8963a974bae1c7ec8597ede81fc8d177e Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 24 Aug 2020 10:21:37 +1200 Subject: [PATCH 14/26] Compatibility with webpack@5.0.0-beta28 --- index.js | 14 +++++++++++--- jmtp.js | 38 +++----------------------------------- 2 files changed, 14 insertions(+), 38 deletions(-) diff --git a/index.js b/index.js index ce97f3d..f1e11f8 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,8 @@ var crypto = require('crypto'); var path = require('path'); -var ReplaceSource = require('webpack-sources/lib/ReplaceSource'); +var webpack = require('webpack'); +var ReplaceSource = (webpack.sources || require('webpack-sources')).ReplaceSource; var util = require('./util'); var WebIntegrityJsonpMainTemplatePlugin = require('./jmtp'); var HtmlWebpackPlugin; @@ -216,7 +217,7 @@ SubresourceIntegrityPlugin.prototype.processChunk = function processChunk( self.warnOnce( compilation, 'Cannot determine asset for chunk ' + childChunk.id + ', computed="' + sourcePath + - '", available=' + childChunk.files[0] + '. Please report this full error message ' + + '", available=' + Array.from(childChunk.files)[0] + '. Please report this full error message ' + 'along with your Webpack configuration at ' + 'https://github.com/waysact/webpack-subresource-integrity/issues/new' ); @@ -378,7 +379,14 @@ SubresourceIntegrityPlugin.prototype.thisCompilation = * Modify the asset tags before webpack injects them for anything with an integrity value. */ if (compiler.hooks) { - compilation.hooks.afterOptimizeAssets.tap('SriPlugin', afterOptimizeAssets); + if (compilation.hooks.processAssets) { + compilation.hooks.processAssets.tap({ + name: 'SriPlugin', + stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH + }, afterOptimizeAssets); + } else { + compilation.hooks.afterOptimizeAssets.tap('SriPlugin', afterOptimizeAssets); + } compilation.hooks.beforeChunkAssets.tap('SriPlugin', beforeChunkAssets); compiler.hooks.compilation.tap('HtmlWebpackPluginHooks', this.registerHwpHooks.bind(this, alterAssetTags, beforeHtmlGeneration)); } else { diff --git a/jmtp.js b/jmtp.js index e2c1e36..d8af14e 100644 --- a/jmtp.js +++ b/jmtp.js @@ -5,20 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -var webpack = require('webpack'); var Template = require('webpack/lib/Template'); var util = require('./util'); -var LoadScriptRuntimeModule; - -try { - // eslint-disable-next-line global-require - LoadScriptRuntimeModule = require('webpack/lib/runtime/LoadScriptRuntimeModule'); -} catch (e) { - // File doesn't exist in Webpack <5 - if (e.code !== 'MODULE_NOT_FOUND') { - throw e; - } -} function WebIntegrityJsonpMainTemplatePlugin(sriPlugin, compilation) { this.sriPlugin = sriPlugin; @@ -73,25 +61,12 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute = ]); }; -function getCompilationHooks(compilation, mainTemplate) { - if (webpack.web && - webpack.web.JsonpTemplatePlugin && - webpack.web.JsonpTemplatePlugin.getCompilationHooks) { - return webpack.web.JsonpTemplatePlugin.getCompilationHooks( - compilation - ); - } - return mainTemplate.hooks; -} - WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply( mainTemplate ) { var jsonpScriptPlugin = this.addAttribute.bind(this, mainTemplate, "script"); - var createScriptPlugin = this.addAttribute.bind(this, mainTemplate, "script"); var linkPreloadPlugin = this.addAttribute.bind(this, mainTemplate, "link"); var addSriHashes = this.addSriHashes.bind(this, mainTemplate); - var compilationHooks = getCompilationHooks(this.compilation, mainTemplate); if (this.compilation.compiler.options.target !== 'web') { this.sriPlugin.warnOnce( @@ -101,21 +76,14 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply( return; } - if (compilationHooks) { - compilationHooks.jsonpScript.tap('SriPlugin', jsonpScriptPlugin); - compilationHooks.linkPreload.tap('SriPlugin', linkPreloadPlugin); + if (mainTemplate.hooks) { + mainTemplate.hooks.jsonpScript.tap('SriPlugin', jsonpScriptPlugin); + mainTemplate.hooks.linkPreload.tap('SriPlugin', linkPreloadPlugin); mainTemplate.hooks.localVars.tap('SriPlugin', addSriHashes); } else { mainTemplate.plugin('jsonp-script', jsonpScriptPlugin); mainTemplate.plugin('local-vars', addSriHashes); } - - if (LoadScriptRuntimeModule) { - LoadScriptRuntimeModule - .getCompilationHooks(this.compilation) - .createScript - .tap('SriPlugin', createScriptPlugin); - } }; module.exports = WebIntegrityJsonpMainTemplatePlugin; From db7f85d2e951dfeb19106fc341dc6cc593e64967 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 24 Aug 2020 11:05:37 +1200 Subject: [PATCH 15/26] Fixate compatible Webpack version There have been too many breaking changes on the Webpack Beta line for us to assume that future Beta releases will work. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 35e93f4..8dd8be4 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ }, "peerDependencies": { "html-webpack-plugin": "^2.21.0 || ~3 || >=4.0.0-alpha.2 <5", - "webpack": "^1.12.11 || ~2 || ~3 || ~4 || >= 5.0.0-beta.7 <6" + "webpack": "^1.12.11 || ~2 || ~3 || ~4 || 5.0.0-beta.28 <6" }, "peerDependenciesMeta": { "html-webpack-plugin": { From cf4c60c7631a3d5d63e1057014e768c7d4b16e3f Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 24 Aug 2020 12:55:20 +1200 Subject: [PATCH 16/26] Run Appveyor against latest Webpack beta --- appveyor.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index a6b2bfc..e284fd5 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -67,7 +67,7 @@ environment: FL_SUFFIX: '@1' HWP_SUFFIX: '@4.0.0-beta.11' - nodejs_version: 8 - WEBPACK_SUFFIX: '@5.0.0-beta.7' + WEBPACK_SUFFIX: '@next' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' HWP_SUFFIX: '@4.0.0-beta.11' @@ -97,7 +97,7 @@ environment: FL_SUFFIX: '@1' HWP_SUFFIX: '@4.0.0-beta.11' - nodejs_version: 10 - WEBPACK_SUFFIX: '@5.0.0-beta.7' + WEBPACK_SUFFIX: '@next' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' HWP_SUFFIX: '@4.0.0-beta.11' @@ -127,7 +127,7 @@ environment: FL_SUFFIX: '@1' HWP_SUFFIX: '@4.0.0-beta.11' - nodejs_version: 12 - WEBPACK_SUFFIX: '@5.0.0-beta.7' + WEBPACK_SUFFIX: '@next' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' HWP_SUFFIX: '@4.0.0-beta.11' From f98477130509b43044bfde49878d7240f626d3f3 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 24 Aug 2020 15:26:34 +1200 Subject: [PATCH 17/26] Fix Appveyor build --- appveyor.yml | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index e284fd5..701e5a3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -40,7 +40,7 @@ environment: WEBPACK_SUFFIX: '@4' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-beta.11' + HWP_SUFFIX: '@4' - nodejs_version: 8 WEBPACK_SUFFIX: '@1' ETWP_SUFFIX: '@1' @@ -65,12 +65,7 @@ environment: WEBPACK_SUFFIX: '@4' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-beta.11' - - nodejs_version: 8 - WEBPACK_SUFFIX: '@next' - ETWP_SUFFIX: '@4.0.0-alpha.0' - FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-beta.11' + HWP_SUFFIX: '@4' - nodejs_version: 10 WEBPACK_SUFFIX: '@1' ETWP_SUFFIX: '@1' @@ -95,12 +90,12 @@ environment: WEBPACK_SUFFIX: '@4' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-beta.11' + HWP_SUFFIX: '@4' - nodejs_version: 10 WEBPACK_SUFFIX: '@next' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-beta.11' + HWP_SUFFIX: '@4' - nodejs_version: 12 WEBPACK_SUFFIX: '@1' ETWP_SUFFIX: '@1' @@ -125,18 +120,19 @@ environment: WEBPACK_SUFFIX: '@4' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-beta.11' + HWP_SUFFIX: '@4' - nodejs_version: 12 WEBPACK_SUFFIX: '@next' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' - HWP_SUFFIX: '@4.0.0-beta.11' + HWP_SUFFIX: '@4' # Install scripts. (runs after repo cloning) install: # Get the latest stable version of Node.js or io.js - ps: Install-Product node $env:nodejs_version # install modules + - npm clean cache --force - appveyor-retry npm install --global npm@3 - npm install || true - npm rm webpack extract-text-webpack-plugin file-loader eslint From b9949ab343c80ca389105e35b3aad434ceb9f4e0 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 24 Aug 2020 21:12:00 +1200 Subject: [PATCH 18/26] Fix npm cache clean invocation --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 701e5a3..9fcc34a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -132,7 +132,7 @@ install: # Get the latest stable version of Node.js or io.js - ps: Install-Product node $env:nodejs_version # install modules - - npm clean cache --force + - npm cache clean --force - appveyor-retry npm install --global npm@3 - npm install || true - npm rm webpack extract-text-webpack-plugin file-loader eslint From c84e17ad84726a6d22b556db556ee52a17e32ef2 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Tue, 15 Sep 2020 22:16:41 +1200 Subject: [PATCH 19/26] Another attempt at fixing Appveyor failure --- appveyor.yml | 2 +- package.json | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 9fcc34a..26c05cf 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -133,7 +133,7 @@ install: - ps: Install-Product node $env:nodejs_version # install modules - npm cache clean --force - - appveyor-retry npm install --global npm@3 + - appveyor-retry npm install --global npm@6 - npm install || true - npm rm webpack extract-text-webpack-plugin file-loader eslint - npm install "webpack%WEBPACK_SUFFIX%" --ignore-scripts --loglevel=error || true diff --git a/package.json b/package.json index 8dd8be4..d143d75 100644 --- a/package.json +++ b/package.json @@ -100,5 +100,8 @@ }, "dependencies": { "webpack-sources": "^1.3.0" + }, + "resolutions": { + "graceful-fs": "^4.2.4" } } From 5f552111e73d2b7380c23b39cf94eb963c9c62c7 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Fri, 9 Oct 2020 17:57:49 +1300 Subject: [PATCH 20/26] Stop testing Node 4 on Windows --- appveyor.yml | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 26c05cf..b889b96 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,21 +1,6 @@ # Test against this version of Node.js environment: matrix: - - nodejs_version: 4 - WEBPACK_SUFFIX: '@1' - ETWP_SUFFIX: '@1' - FL_SUFFIX: '@0' - HWP_SUFFIX: '@2' - - nodejs_version: 4 - WEBPACK_SUFFIX: '@2' - ETWP_SUFFIX: '@2' - FL_SUFFIX: '@1' - HWP_SUFFIX: '@2' - - nodejs_version: 4 - WEBPACK_SUFFIX: '@3' - ETWP_SUFFIX: '@3' - FL_SUFFIX: '@1' - HWP_SUFFIX: '@2' - nodejs_version: 6 WEBPACK_SUFFIX: '@1' ETWP_SUFFIX: '@1' From 878afff9b518443562926e395df8cfd9e2a2913f Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 12 Oct 2020 10:56:15 +1300 Subject: [PATCH 21/26] Test against Webpack 5 --- .travis.yml | 2 +- appveyor.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index cb2ae81..f7974e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ env: - WEBPACK_SUFFIX="@3" ETWP_SUFFIX="@3" HWP_SUFFIX="@2" - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@3" - WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" - - WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" + - WEBPACK_SUFFIX="@5" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" matrix: exclude: - node_js: 4 diff --git a/appveyor.yml b/appveyor.yml index b889b96..bc55b7b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -77,7 +77,7 @@ environment: FL_SUFFIX: '@1' HWP_SUFFIX: '@4' - nodejs_version: 10 - WEBPACK_SUFFIX: '@next' + WEBPACK_SUFFIX: '@5' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' HWP_SUFFIX: '@4' @@ -107,7 +107,7 @@ environment: FL_SUFFIX: '@1' HWP_SUFFIX: '@4' - nodejs_version: 12 - WEBPACK_SUFFIX: '@next' + WEBPACK_SUFFIX: '@5' ETWP_SUFFIX: '@4.0.0-alpha.0' FL_SUFFIX: '@1' HWP_SUFFIX: '@4' From 695be87724d284c1f2eb6b5be077b51296b28452 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 12 Oct 2020 11:10:25 +1300 Subject: [PATCH 22/26] Fix Webpack 5 deprecation warning --- jmtp.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jmtp.js b/jmtp.js index d8af14e..79c99c8 100644 --- a/jmtp.js +++ b/jmtp.js @@ -48,7 +48,8 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes = */ WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute = function addAttribute(mainTemplate, elName, source, chunk) { - if (!mainTemplate.outputOptions.crossOriginLoading) { + const outputOptions = this.compilation.outputOptions || mainTemplate.outputOptions; + if (!outputOptions.crossOriginLoading) { this.sriPlugin.errorOnce( this.compilation, 'webpack option output.crossOriginLoading not set, code splitting will not work!' @@ -57,7 +58,7 @@ WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute = return (Template.asString || mainTemplate.asString)([ source, elName + '.integrity = __webpack_require__.sriHashes[' + (chunk ? `'${chunk.id}'` : 'chunkId') + '];', - elName + '.crossOrigin = ' + JSON.stringify(mainTemplate.outputOptions.crossOriginLoading) + ';', + elName + '.crossOrigin = ' + JSON.stringify(outputOptions.crossOriginLoading) + ';', ]); }; From 9c5ca816e8a5749a5f15297199c4fe92c44dc530 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 12 Oct 2020 11:19:53 +1300 Subject: [PATCH 23/26] Fix Travis exclusions --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index f7974e5..9ba57f9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,11 +21,11 @@ matrix: - node_js: 4 env: WEBPACK_SUFFIX="@4" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" - node_js: 4 - env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" + env: WEBPACK_SUFFIX="@5" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" - node_js: 6 - env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" + env: WEBPACK_SUFFIX="@5" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" - node_js: 8 - env: WEBPACK_SUFFIX="@next" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" + env: WEBPACK_SUFFIX="@5" ETWP_SUFFIX="@4.0.0-alpha.0" HWP_SUFFIX="@4" install: - npm install || true - npm rm webpack extract-text-webpack-plugin eslint From 3cc27aae0bdf7f0b238571eae806e46b564c2bb9 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 12 Oct 2020 11:43:48 +1300 Subject: [PATCH 24/26] Bump version to 1.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d143d75..743028b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack-subresource-integrity", - "version": "1.4.1", + "version": "1.5.0", "description": "Webpack plugin for enabling Subresource Integrity", "engines": { "node": ">=4" From 00dd2bc58468db9a12cfbd32adc9feb1b1607fcc Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 12 Oct 2020 14:21:33 +1300 Subject: [PATCH 25/26] Refactor hook setup --- index.js | 107 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 38 deletions(-) diff --git a/index.js b/index.js index f1e11f8..99517ba 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ var crypto = require('crypto'); var path = require('path'); var webpack = require('webpack'); +// eslint-disable-next-line global-require var ReplaceSource = (webpack.sources || require('webpack-sources')).ReplaceSource; var util = require('./util'); var WebIntegrityJsonpMainTemplatePlugin = require('./jmtp'); @@ -355,47 +356,77 @@ SubresourceIntegrityPlugin.prototype.registerHwpHooks = } }; -SubresourceIntegrityPlugin.prototype.thisCompilation = - function thisCompilation(compiler, compilation) { - var afterOptimizeAssets = this.afterOptimizeAssets.bind(this, compilation); - var beforeChunkAssets = this.beforeChunkAssets.bind(this, compilation); - var alterAssetTags = this.alterAssetTags.bind(this, compilation); - var beforeHtmlGeneration = this.beforeHtmlGeneration.bind(this, compilation); +SubresourceIntegrityPlugin.prototype.thisCompilation = function thisCompilation( + compiler, + compilation +) { + this.validateOptions(compilation); - this.validateOptions(compilation); + if (!this.options.enabled) { + return; + } - if (!this.options.enabled) { - return; - } + this.registerJMTP(compilation); - 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) { - if (compilation.hooks.processAssets) { - compilation.hooks.processAssets.tap({ - name: 'SriPlugin', - stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH - }, afterOptimizeAssets); - } else { - compilation.hooks.afterOptimizeAssets.tap('SriPlugin', afterOptimizeAssets); - } - 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('before-chunk-assets', beforeChunkAssets); - compilation.plugin('html-webpack-plugin-alter-asset-tags', alterAssetTags); - compilation.plugin('html-webpack-plugin-before-html-generation', beforeHtmlGeneration); - } - }; + // 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) { + this.setupHooks(compiler, compilation); + } else { + this.setupLegacyHooks(compiler, compilation); + } +}; + +SubresourceIntegrityPlugin.prototype.setupHooks = function setupHooks( + compiler, + compilation +) { + var afterOptimizeAssets = this.afterOptimizeAssets.bind(this, compilation); + var beforeChunkAssets = this.beforeChunkAssets.bind(this, compilation); + var alterAssetTags = this.alterAssetTags.bind(this, compilation); + var beforeHtmlGeneration = this.beforeHtmlGeneration.bind(this, compilation); + + if (compilation.hooks.processAssets) { + compilation.hooks.processAssets.tap( + { + name: 'SriPlugin', + stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH + }, + afterOptimizeAssets + ); + } else { + compilation.hooks.afterOptimizeAssets.tap('SriPlugin', afterOptimizeAssets); + } + compilation.hooks.beforeChunkAssets.tap('SriPlugin', beforeChunkAssets); + compiler.hooks.compilation.tap( + 'HtmlWebpackPluginHooks', + this.registerHwpHooks.bind(this, alterAssetTags, beforeHtmlGeneration) + ); +}; + +SubresourceIntegrityPlugin.prototype.setupLegacyHooks = function setupLegacyHooks( + compiler, + compilation +) { + var afterOptimizeAssets = this.afterOptimizeAssets.bind(this, compilation); + var beforeChunkAssets = this.beforeChunkAssets.bind(this, compilation); + var alterAssetTags = this.alterAssetTags.bind(this, compilation); + var beforeHtmlGeneration = this.beforeHtmlGeneration.bind(this, compilation); + + compilation.plugin('after-optimize-assets', afterOptimizeAssets); + 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); From 1631ca6bb227546c62dd686dbca8e877b57d3e01 Mon Sep 17 00:00:00 2001 From: Julian Scheid Date: Mon, 12 Oct 2020 20:07:03 +1300 Subject: [PATCH 26/26] Clean up dependency version ranges --- package.json | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 743028b..0ba6065 100644 --- a/package.json +++ b/package.json @@ -76,8 +76,8 @@ "webpack-fix-style-only-entries": "^0.4.0" }, "peerDependencies": { - "html-webpack-plugin": "^2.21.0 || ~3 || >=4.0.0-alpha.2 <5", - "webpack": "^1.12.11 || ~2 || ~3 || ~4 || 5.0.0-beta.28 <6" + "html-webpack-plugin": ">= 2.21.0 < 5", + "webpack": ">= 1.12.11 < 6" }, "peerDependenciesMeta": { "html-webpack-plugin": { @@ -93,15 +93,17 @@ "jmtp.js" ], "nyc": { - "exclude": ["coverage/**", "test/**", "examples/**", "**/node_modules/**"] + "exclude": [ + "coverage/**", + "test/**", + "examples/**", + "**/node_modules/**" + ] }, "prettier": { "singleQuote": true }, "dependencies": { "webpack-sources": "^1.3.0" - }, - "resolutions": { - "graceful-fs": "^4.2.4" } }