Skip to content

Commit

Permalink
Compatibility with html-webpack-plugin 4
Browse files Browse the repository at this point in the history
  • Loading branch information
jscheid committed Sep 8, 2018
1 parent 3afe070 commit 6e1bb1c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -10,6 +10,7 @@ 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"
install:
- npm install || true
- npm rm webpack
Expand Down
15 changes: 15 additions & 0 deletions appveyor.yml
Expand Up @@ -21,6 +21,11 @@ environment:
ETWP_SUFFIX: "@4.0.0-alpha.0"
FL_SUFFIX: "@1"
HWP_SUFFIX: "@3"
- nodejs_version: 6
WEBPACK_SUFFIX: "@4"
ETWP_SUFFIX: "@4.0.0-alpha.0"
FL_SUFFIX: "@1"
HWP_SUFFIX: "@4.0.0-alpha.2"
- nodejs_version: 8
WEBPACK_SUFFIX: "@1"
ETWP_SUFFIX: "@1"
Expand All @@ -41,6 +46,11 @@ environment:
ETWP_SUFFIX: "@4.0.0-alpha.0"
FL_SUFFIX: "@1"
HWP_SUFFIX: "@3"
- nodejs_version: 8
WEBPACK_SUFFIX: "@4"
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"
Expand All @@ -61,6 +71,11 @@ environment:
ETWP_SUFFIX: "@4.0.0-alpha.0"
FL_SUFFIX: "@1"
HWP_SUFFIX: "@3"
- nodejs_version: 10
WEBPACK_SUFFIX: "@4"
ETWP_SUFFIX: "@4.0.0-alpha.0"
FL_SUFFIX: "@1"
HWP_SUFFIX: "@4.0.0-alpha.2"

# Install scripts. (runs after repo cloning)
install:
Expand Down
7 changes: 7 additions & 0 deletions examples/warn-checksum/test.js
@@ -1,4 +1,11 @@
var expect = require('expect');
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports.skip = function skip() {
// Not sure how to provoke this warning with HWP 4
// Consider removing this test altogether.
return HtmlWebpackPlugin.version >= 4;
};

module.exports.check = function check(stats) {
expect(stats.compilation.warnings.length).toEqual(1);
Expand Down
71 changes: 50 additions & 21 deletions index.js
Expand Up @@ -10,10 +10,20 @@ var path = require('path');
var ReplaceSource = require('webpack-core/lib/ReplaceSource');
var util = require('./util');
var WebIntegrityJsonpMainTemplatePlugin = require('./jmtp');
var HtmlWebpackPlugin;

// https://www.w3.org/TR/2016/REC-SRI-20160623/#cryptographic-hash-functions
var standardHashFuncNames = ['sha256', 'sha384', 'sha512'];

try {
// eslint-disable-next-line global-require
HtmlWebpackPlugin = require('html-webpack-plugin');
} catch (e) {
if (!(e instanceof Error) || e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}

function SubresourceIntegrityPlugin(options) {
var useOptions;
if (options === null || typeof options === 'undefined') {
Expand Down Expand Up @@ -231,30 +241,31 @@ SubresourceIntegrityPlugin.prototype.afterOptimizeAssets =
});
};

SubresourceIntegrityPlugin.prototype.processTag =
function processTag(compilation, tag) {
var src = this.hwpAssetPath(util.getTagSrc(tag));
var checksum = util.getIntegrityChecksumForAsset(compilation.assets, src);
if (!checksum) {
this.warnOnce(
compilation,
'Cannot determine hash for asset \'' +
src + '\', the resource will be unprotected.');
return;
}
// Add integrity check sums

/* eslint-disable no-param-reassign */
tag.attributes.integrity = checksum;
tag.attributes.crossorigin = compilation.compiler.options.output.crossOriginLoading || 'anonymous';
/* eslint-enable no-param-reassign */
};

SubresourceIntegrityPlugin.prototype.alterAssetTags =
function alterAssetTags(compilation, pluginArgs, callback) {
/* html-webpack-plugin has added an event so we can pre-process the html tags before they
inject them. This does the work.
*/
var self = this;
function processTag(tag) {
var src = self.hwpAssetPath(util.getTagSrc(tag));
var checksum = util.getIntegrityChecksumForAsset(compilation.assets, src);
if (!checksum) {
self.warnOnce(
compilation,
'Cannot determine hash for asset \'' +
src + '\', the resource will be unprotected.');
return;
}
// Add integrity check sums

/* eslint-disable no-param-reassign */
tag.attributes.integrity = checksum;
tag.attributes.crossorigin = compilation.compiler.options.output.crossOriginLoading || 'anonymous';
/* eslint-enable no-param-reassign */
}

var processTag = this.processTag.bind(this, compilation);
pluginArgs.head.filter(util.filterTag).forEach(processTag);
pluginArgs.body.filter(util.filterTag).forEach(processTag);
callback(null, pluginArgs);
Expand Down Expand Up @@ -290,8 +301,26 @@ SubresourceIntegrityPlugin.prototype.registerJMTP = function registerJMTP(compil

SubresourceIntegrityPlugin.prototype.registerHwpHooks =
function registerHwpHooks(alterAssetTags, beforeHtmlGeneration, hwpCompilation) {
if (hwpCompilation.hooks.htmlWebpackPluginAlterAssetTags &&
hwpCompilation.hooks.htmlWebpackPluginBeforeHtmlGeneration) {
var self = this;
if (HtmlWebpackPlugin && HtmlWebpackPlugin.getHooks) {
// HtmlWebpackPlugin >= 4
HtmlWebpackPlugin.getHooks(hwpCompilation).beforeAssetTagGeneration.tapAsync(
'sri',
this.beforeHtmlGeneration.bind(this, hwpCompilation)
);

HtmlWebpackPlugin.getHooks(hwpCompilation).alterAssetTags.tapAsync(
'sri',
function cb(data, callback) {
var processTag = self.processTag.bind(self, hwpCompilation);
data.assetTags.scripts.filter(util.filterTag).forEach(processTag);
data.assetTags.styles.filter(util.filterTag).forEach(processTag);
callback(null, data);
}
);
} else if (hwpCompilation.hooks.htmlWebpackPluginAlterAssetTags &&
hwpCompilation.hooks.htmlWebpackPluginBeforeHtmlGeneration) {
// HtmlWebpackPlugin 3
hwpCompilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('SriPlugin', alterAssetTags);
hwpCompilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapAsync('SriPlugin', beforeHtmlGeneration);
}
Expand Down

0 comments on commit 6e1bb1c

Please sign in to comment.