Skip to content

Commit 00dd2bc

Browse files
committed
Refactor hook setup
1 parent 3cc27aa commit 00dd2bc

File tree

1 file changed

+69
-38
lines changed

1 file changed

+69
-38
lines changed

index.js

+69-38
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
var crypto = require('crypto');
99
var path = require('path');
1010
var webpack = require('webpack');
11+
// eslint-disable-next-line global-require
1112
var ReplaceSource = (webpack.sources || require('webpack-sources')).ReplaceSource;
1213
var util = require('./util');
1314
var WebIntegrityJsonpMainTemplatePlugin = require('./jmtp');
@@ -355,47 +356,77 @@ SubresourceIntegrityPlugin.prototype.registerHwpHooks =
355356
}
356357
};
357358

358-
SubresourceIntegrityPlugin.prototype.thisCompilation =
359-
function thisCompilation(compiler, compilation) {
360-
var afterOptimizeAssets = this.afterOptimizeAssets.bind(this, compilation);
361-
var beforeChunkAssets = this.beforeChunkAssets.bind(this, compilation);
362-
var alterAssetTags = this.alterAssetTags.bind(this, compilation);
363-
var beforeHtmlGeneration = this.beforeHtmlGeneration.bind(this, compilation);
359+
SubresourceIntegrityPlugin.prototype.thisCompilation = function thisCompilation(
360+
compiler,
361+
compilation
362+
) {
363+
this.validateOptions(compilation);
364364

365-
this.validateOptions(compilation);
365+
if (!this.options.enabled) {
366+
return;
367+
}
366368

367-
if (!this.options.enabled) {
368-
return;
369-
}
369+
this.registerJMTP(compilation);
370370

371-
this.registerJMTP(compilation);
372-
373-
// FIXME: refactor into separate per-compilation state
374-
// eslint-disable-next-line no-param-reassign
375-
compilation.sriChunkAssets = {};
376-
377-
/*
378-
* html-webpack support:
379-
* Modify the asset tags before webpack injects them for anything with an integrity value.
380-
*/
381-
if (compiler.hooks) {
382-
if (compilation.hooks.processAssets) {
383-
compilation.hooks.processAssets.tap({
384-
name: 'SriPlugin',
385-
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH
386-
}, afterOptimizeAssets);
387-
} else {
388-
compilation.hooks.afterOptimizeAssets.tap('SriPlugin', afterOptimizeAssets);
389-
}
390-
compilation.hooks.beforeChunkAssets.tap('SriPlugin', beforeChunkAssets);
391-
compiler.hooks.compilation.tap('HtmlWebpackPluginHooks', this.registerHwpHooks.bind(this, alterAssetTags, beforeHtmlGeneration));
392-
} else {
393-
compilation.plugin('after-optimize-assets', afterOptimizeAssets);
394-
compilation.plugin('before-chunk-assets', beforeChunkAssets);
395-
compilation.plugin('html-webpack-plugin-alter-asset-tags', alterAssetTags);
396-
compilation.plugin('html-webpack-plugin-before-html-generation', beforeHtmlGeneration);
397-
}
398-
};
371+
// FIXME: refactor into separate per-compilation state
372+
// eslint-disable-next-line no-param-reassign
373+
compilation.sriChunkAssets = {};
374+
375+
/*
376+
* html-webpack support:
377+
* Modify the asset tags before webpack injects them for anything with an integrity value.
378+
*/
379+
if (compiler.hooks) {
380+
this.setupHooks(compiler, compilation);
381+
} else {
382+
this.setupLegacyHooks(compiler, compilation);
383+
}
384+
};
385+
386+
SubresourceIntegrityPlugin.prototype.setupHooks = function setupHooks(
387+
compiler,
388+
compilation
389+
) {
390+
var afterOptimizeAssets = this.afterOptimizeAssets.bind(this, compilation);
391+
var beforeChunkAssets = this.beforeChunkAssets.bind(this, compilation);
392+
var alterAssetTags = this.alterAssetTags.bind(this, compilation);
393+
var beforeHtmlGeneration = this.beforeHtmlGeneration.bind(this, compilation);
394+
395+
if (compilation.hooks.processAssets) {
396+
compilation.hooks.processAssets.tap(
397+
{
398+
name: 'SriPlugin',
399+
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH
400+
},
401+
afterOptimizeAssets
402+
);
403+
} else {
404+
compilation.hooks.afterOptimizeAssets.tap('SriPlugin', afterOptimizeAssets);
405+
}
406+
compilation.hooks.beforeChunkAssets.tap('SriPlugin', beforeChunkAssets);
407+
compiler.hooks.compilation.tap(
408+
'HtmlWebpackPluginHooks',
409+
this.registerHwpHooks.bind(this, alterAssetTags, beforeHtmlGeneration)
410+
);
411+
};
412+
413+
SubresourceIntegrityPlugin.prototype.setupLegacyHooks = function setupLegacyHooks(
414+
compiler,
415+
compilation
416+
) {
417+
var afterOptimizeAssets = this.afterOptimizeAssets.bind(this, compilation);
418+
var beforeChunkAssets = this.beforeChunkAssets.bind(this, compilation);
419+
var alterAssetTags = this.alterAssetTags.bind(this, compilation);
420+
var beforeHtmlGeneration = this.beforeHtmlGeneration.bind(this, compilation);
421+
422+
compilation.plugin('after-optimize-assets', afterOptimizeAssets);
423+
compilation.plugin('before-chunk-assets', beforeChunkAssets);
424+
compilation.plugin('html-webpack-plugin-alter-asset-tags', alterAssetTags);
425+
compilation.plugin(
426+
'html-webpack-plugin-before-html-generation',
427+
beforeHtmlGeneration
428+
);
429+
};
399430

400431
SubresourceIntegrityPlugin.prototype.beforeChunkAssets = function afterPlugins(compilation) {
401432
var chunkAsset = this.chunkAsset.bind(this, compilation);

0 commit comments

Comments
 (0)