Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit f673015

Browse files
authored
avoid failing if html-webpack-plugin isn't installed (#153)
1 parent 4a7c639 commit f673015

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

src/compat.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ module.exports.tap = (tappable, hook, name, plugin) => (
1414
: tappable.plugin(hook, plugin)
1515
);
1616

17+
/* istanbul ignore next */
1718
module.exports.tapHtml = (tappable, name, plugin) => {
18-
const HtmlWebpackPlugin = require('html-webpack-plugin');
19-
return HtmlWebpackPlugin.getHooks /* HtmlWebpackPlugin >= 4.0 */
20-
? HtmlWebpackPlugin.getHooks(tappable).afterTemplateExecution.tapAsync(name, plugin)
21-
: module.exports.tap(tappable, 'html-webpack-plugin-before-html-processing', name, plugin)
22-
;
19+
try {
20+
const HtmlWebpackPlugin = require('html-webpack-plugin');
21+
return HtmlWebpackPlugin.getHooks /* HtmlWebpackPlugin >= 4.0 */
22+
? HtmlWebpackPlugin.getHooks(tappable).afterTemplateExecution.tapAsync(name, plugin)
23+
: module.exports.tap(tappable, 'html-webpack-plugin-before-html-processing', name, plugin)
24+
;
25+
} catch (_) {
26+
// ignore
27+
}
2328
};
2429

2530
/* istanbul ignore next */

src/index.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@ module.exports = class WebappWebpackPlugin {
4141
// Generate favicons
4242
child.run(this.options, compiler.context, compilation)
4343
.then(tags => {
44-
if (this.options.inject) {
45-
// Hook into the html-webpack-plugin processing and add the html
46-
tapHtml(compilation, 'WebappWebpackPlugin', (htmlPluginData, callback) => {
47-
const htmlPluginDataInject = htmlPluginData.plugin.options.inject && htmlPluginData.plugin.options.favicons !== false;
48-
if (htmlPluginDataInject || this.options.inject === 'force') {
49-
const idx = (htmlPluginData.html + '</head>').search(/<\/head>/i);
50-
htmlPluginData.html = [htmlPluginData.html.slice(0, idx), ...tags, htmlPluginData.html.slice(idx)].join('');
51-
}
52-
return callback(null, htmlPluginData);
53-
});
54-
}
44+
// Hook into the html-webpack-plugin processing and add the html
45+
tapHtml(compilation, 'WebappWebpackPlugin', (htmlPluginData, callback) => {
46+
const htmlPluginDataInject = htmlPluginData.plugin.options.inject && htmlPluginData.plugin.options.favicons !== false;
47+
if (htmlPluginDataInject && this.options.inject || this.options.inject === 'force') {
48+
const idx = (htmlPluginData.html + '</head>').search(/<\/head>/i);
49+
htmlPluginData.html = [htmlPluginData.html.slice(0, idx), ...tags, htmlPluginData.html.slice(idx)].join('');
50+
}
51+
return callback(null, htmlPluginData);
52+
});
5553
return callback();
5654
})
5755
.catch(callback)

0 commit comments

Comments
 (0)