Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
apply(compiler) {
if ('hooks' in compiler) {
// webpack 4 or higher
const name = this.constructor.name;
if (HtmlWebpackPlugin.version >= 4) {
// HtmlWebpackPlugin 4 or higher
compiler.hooks.compilation.tap(name, compilation => {
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
name, // Set a meaningful name here for stack traces
(data, cb) => {
this._process(compilation, data, cb);
}
);
});
} else {
// HtmlWebpackPlugin 3 or lower
compiler.hooks.compilation.tap(name, compilation => {
// if htmlWebpackPlugin is not exist, just do nothing
if (compilation.hooks.htmlWebpackPluginAfterHtmlProcessing) {
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(
name,
compiler.hooks.compilation.tap('AddAssetHtmlPlugin', compilation => {
let beforeGenerationHook;
let alterAssetTagsHook;
if (HtmlWebpackPlugin.version === 4) {
const hooks = HtmlWebpackPlugin.getHooks(compilation);
beforeGenerationHook = hooks.beforeAssetTagGeneration;
alterAssetTagsHook = hooks.alterAssetTags;
} else {
const { hooks } = compilation;
beforeGenerationHook = hooks.htmlWebpackPluginBeforeHtmlGeneration;
alterAssetTagsHook = hooks.htmlWebpackPluginAlterAssetTags;
}
beforeGenerationHook.tapPromise('AddAssetHtmlPlugin', htmlPluginData =>
this.addAllAssetsToCompilation(compilation, htmlPluginData),
);
alterAssetTagsHook.tap('AddAssetHtmlPlugin', htmlPluginData => {