From d554946ab0a9827bcba739c129b51927567927cf Mon Sep 17 00:00:00 2001 From: Bruno Dutra Date: Thu, 10 May 2018 19:08:42 +0200 Subject: [PATCH] feat: allow skipping specific instances of html-webpack-plugin --- README.md | 3 +++ src/index.js | 4 +++- test/nohtml.test.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1bb08786..8163bebe 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,9 @@ In combination with [html-webpack-plugin](https://github.com/jantimon/html-webpa > https://github.com/jantimon/favicons-webpack-plugin/blob/master/test/fixtures/expected/html +HTML injection is skipped for a particular `html-webpack-plugin` if either `inject` or `favicons` +properties are set to `false` in its configuration object. + ## Advanced Usage ```javascript diff --git a/src/index.js b/src/index.js index 1b156c4d..775373d0 100644 --- a/src/index.js +++ b/src/index.js @@ -50,7 +50,9 @@ module.exports = class FaviconsWebpackPlugin { if (this.options.inject) { // Hook into the html-webpack-plugin processing and add the html tap(compilation, 'html-webpack-plugin-before-html-processing', 'FaviconsWebpackPlugin', (htmlPluginData, callback) => { - htmlPluginData.html = htmlPluginData.html.replace(/(<\/head>)/i, result + '$&'); + if (htmlPluginData.plugin.options.inject && htmlPluginData.plugin.options.favicons !== false) { + htmlPluginData.html = htmlPluginData.html.replace(/(<\/head>)/i, result + '$&'); + } return callback(null, htmlPluginData); }); } diff --git a/test/nohtml.test.js b/test/nohtml.test.js index dd27cf0e..338d4783 100644 --- a/test/nohtml.test.js +++ b/test/nohtml.test.js @@ -24,4 +24,36 @@ test('should allow disabling html injection', async t => { t.deepEqual(await compare(dist, path.resolve(expected, 'nohtml')), []); }); +test('should respect HtmlWebpackPlugin@inject flag', async t => { + const dist = path.join(t.context.root, 'dist'); + await generate({ + context: t.context.root, + output: { + path: dist, + }, + plugins: [ + new HtmlWebpackPlugin({inject: false}), + new WebappWebpackPlugin({logo}), + ], + }); + + t.deepEqual(await compare(dist, path.resolve(expected, 'nohtml')), []); +}); + +test('should respect HtmlWebpackPlugin@favicons flag', async t => { + const dist = path.join(t.context.root, 'dist'); + await generate({ + context: t.context.root, + output: { + path: dist, + }, + plugins: [ + new HtmlWebpackPlugin({favicons: false}), + new WebappWebpackPlugin({logo}), + ], + }); + + t.deepEqual(await compare(dist, path.resolve(expected, 'nohtml')), []); +}); + test.afterEach(t => fs.remove(t.context.root));