Skip to content

Commit

Permalink
fix: avoid failing if html-webpack-plugin isn't installed
Browse files Browse the repository at this point in the history
  • Loading branch information
brunocodutra authored and jantimon committed Aug 14, 2019
1 parent ccfea37 commit 1c0ee82
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
15 changes: 10 additions & 5 deletions src/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ module.exports.tap = (tappable, hook, name, plugin) => (
: tappable.plugin(hook, plugin)
);

/* istanbul ignore next */
module.exports.tapHtml = (tappable, name, plugin) => {
const HtmlWebpackPlugin = require('html-webpack-plugin');
return HtmlWebpackPlugin.getHooks /* HtmlWebpackPlugin >= 4.0 */
? HtmlWebpackPlugin.getHooks(tappable).afterTemplateExecution.tapAsync(name, plugin)
: module.exports.tap(tappable, 'html-webpack-plugin-before-html-processing', name, plugin)
;
try {
const HtmlWebpackPlugin = require('html-webpack-plugin');
return HtmlWebpackPlugin.getHooks /* HtmlWebpackPlugin >= 4.0 */
? HtmlWebpackPlugin.getHooks(tappable).afterTemplateExecution.tapAsync(name, plugin)
: module.exports.tap(tappable, 'html-webpack-plugin-before-html-processing', name, plugin)
;
} catch (_) {
// ignore
}
};

/* istanbul ignore next */
Expand Down
20 changes: 9 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,15 @@ module.exports = class FaviconsWebpackPlugin {
// Generate favicons
child.run(this.options, compiler.context, compilation)
.then(tags => {
if (this.options.inject) {
// Hook into the html-webpack-plugin processing and add the html
tapHtml(compilation, 'FaviconsWebpackPlugin', (htmlPluginData, callback) => {
const htmlPluginDataInject = htmlPluginData.plugin.options.inject && htmlPluginData.plugin.options.favicons !== false;
if (htmlPluginDataInject || this.options.inject === 'force') {
const idx = (htmlPluginData.html + '</head>').search(/<\/head>/i);
htmlPluginData.html = [htmlPluginData.html.slice(0, idx), ...tags, htmlPluginData.html.slice(idx)].join('');
}
return callback(null, htmlPluginData);
});
}
// Hook into the html-webpack-plugin processing and add the html
tapHtml(compilation, 'FaviconsWebpackPlugin', (htmlPluginData, callback) => {
const htmlPluginDataInject = htmlPluginData.plugin.options.inject && htmlPluginData.plugin.options.favicons !== false;
if (htmlPluginDataInject && this.options.inject || this.options.inject === 'force') {
const idx = (htmlPluginData.html + '</head>').search(/<\/head>/i);
htmlPluginData.html = [htmlPluginData.html.slice(0, idx), ...tags, htmlPluginData.html.slice(idx)].join('');
}
return callback(null, htmlPluginData);
});
return callback();
})
.catch(callback)
Expand Down

0 comments on commit 1c0ee82

Please sign in to comment.