Skip to content

Commit

Permalink
fix: remove "force" inject option
Browse files Browse the repository at this point in the history
BREAKING CHANGE: remove "force" inject option
  • Loading branch information
jantimon committed Jan 2, 2020
1 parent 395fc09 commit 15bab27
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 657 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ plugins: [
// * boolean
// `false`: disables injection
// `true`: enables injection if that is not disabled in html-webpack-plugin
// * string
// `'force'`: enables injection even if that is disabled in html-webpack-plugin
// * function
// any predicate that takes an instance of html-webpack-plugin and returns either
// `true` or `false` to control the injection of html metadata for the html files
Expand Down
46 changes: 23 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,41 +58,41 @@ class FaviconsWebpackPlugin {
assert(typeof this.options.logo === 'string', 'Could not find `logo.png` for the current webpack context');
}

const allowInjection = typeof this.options.inject === 'function'
? this.options.inject :
htmlPlugin => Boolean(
this.options.inject === 'force' ||
htmlPlugin.options.favicons !== false && htmlPlugin.options.inject && this.options.inject
);

// Hook into the webpack compilation
// to start the favicon generation
compiler.hooks.make.tapPromise('FaviconsWebpackPlugin', async compilation => {
const faviconCompilation = this.generateFavicons(compilation);

// Hook into the html-webpack-plugin processing and add the html
const htmlWebpackPlugin = compiler.options.plugins
const HtmlWebpackPlugin = compiler.options.plugins
.map(({ constructor }) => constructor)
.find(({ name }) => name === 'HtmlWebpackPlugin');

if (htmlWebpackPlugin) {
assert(typeof htmlWebpackPlugin.getHooks !== 'undefined',
.find(( constructor ) => constructor && constructor.name === 'HtmlWebpackPlugin');
if (HtmlWebpackPlugin && this.options.inject) {
assert(typeof HtmlWebpackPlugin.getHooks !== 'undefined',
'This FaviconsWebpackPlugin is not compatible with your current HtmlWebpackPlugin version.\n' +
'Please upgrade to HtmlWebpackPlugin >= 4 OR downgrade to FaviconsWebpackPlugin 2.x'
);
htmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync('FaviconsWebpackPlugin', (htmlPluginData, htmlWebpackPluginCallback) => {
HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync('FaviconsWebpackPlugin', (htmlPluginData, htmlWebpackPluginCallback) => {
// Skip if a custom injectFunction returns false or if
// the htmlWebpackPlugin optuons includes a `favicons: false` flag
const isInjectionAllowed = typeof this.options.inject === 'function'
? this.options.inject(htmlPluginData.plugin)
: htmlPluginData.plugin.options.favicons !== false;
if (isInjectionAllowed === false) {
return htmlWebpackPluginCallback(null, htmlPluginData);;
}

faviconCompilation.then((tags) => {

if (allowInjection(htmlPluginData.plugin)) {
htmlPluginData.plugin.options.inject = true;
htmlPluginData.assetTags.meta.push(
...tags.map(tag => parse5.parseFragment(tag).childNodes[0]).map(({ tagName, attrs }) => ({
tagName,
voidTag: true,
attributes: attrs.reduce((obj, { name, value }) => Object.assign(obj, { [name]: value }), {}),
})),
);
}
htmlPluginData.assetTags.meta.push(
...tags.map(tag => parse5.parseFragment(tag).childNodes[0]).map(({ tagName, attrs }) => ({
tagName,
voidTag: true,
attributes: attrs.reduce((obj, { name, value }) => Object.assign(obj, { [name]: value }), {}),
})),
);

htmlWebpackPluginCallback(null, htmlPluginData);
}).catch(htmlWebpackPluginCallback);
});
Expand Down
3 changes: 1 addition & 2 deletions src/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export interface FaviconWebpackPlugionOptions {
* any predicate that takes an instance of html-webpack-plugin and returns either
* `true` or `false` to control the injection of html metadata for the html files
* generated by this instance.
* - `'force'`: enables injection even if that is disabled in
*/
inject?: boolean | 'force' | ((htmlWebpackPlugin: any) => boolean),
inject?: boolean | ((htmlWebpackPlugin: any) => boolean),
/**
* Favicons configuration option
* @see https://github.com/itgalaxy/favicons
Expand Down
16 changes: 0 additions & 16 deletions test/html.true.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ test('should work together with the html-webpack-plugin', async t => {
snapshotCompilationAssets(t, compilationStats);
});

test('should inject html regardless of HtmlWebpackPlugin@inject flag with inject force', async t => {
const dist = path.join(t.context.root, 'dist');
const compilationStats = await generate({
context: t.context.root,
output: {
path: dist,
},
plugins: [
new HtmlWebpackPlugin({ inject: false }),
new FaviconsWebpackPlugin({ logo, inject: 'force' }),
],
});

snapshotCompilationAssets(t, compilationStats);
});

test('should work together with the html-webpack-plugin with no <head></head> tags', async t => {
const dist = path.join(t.context.root, 'dist');
const compilationStats = await generate({
Expand Down
Binary file modified test/snapshots/html.false.test.js.snap
Binary file not shown.
Loading

0 comments on commit 15bab27

Please sign in to comment.