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

Commit 05e9fc5

Browse files
authored
support html-webpack-plugin v4 (#131)
1 parent 0c13d8b commit 05e9fc5

11 files changed

Lines changed: 31 additions & 60 deletions

File tree

src/compat.js

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

17+
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+
;
23+
};
24+
1725
/* istanbul ignore next */
1826
module.exports.trigger = (tappable, hook, arg, callback) => (
1927
tappable.hooks /* Webpack >= 4.0 */

src/compiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports.run = ({prefix, favicons: options, logo, cache}, context, compila
5050

5151
delete compilation.assets[output];
5252

53-
trigger(compilation, 'webapp-webpack-plugin-before-emit', result, (error, {html = '', assets = []} = {}) => {
53+
trigger(compilation, 'webapp-webpack-plugin-before-emit', result, (error, {tags = [], assets = []} = {}) => {
5454
if (error) {
5555
return reject(error);
5656
}
@@ -62,7 +62,7 @@ module.exports.run = ({prefix, favicons: options, logo, cache}, context, compila
6262
};
6363
}
6464

65-
return resolve(html);
65+
return resolve(tags);
6666
});
6767
});
6868
});

src/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const assert = require('assert');
22
const child = require('./compiler');
33
const Oracle = require('./oracle');
4-
const {tap} = require('./compat');
4+
const {tap, tapHtml} = require('./compat');
55

66
module.exports = class WebappWebpackPlugin {
77
constructor(args) {
@@ -40,15 +40,14 @@ module.exports = class WebappWebpackPlugin {
4040
tap(compiler, 'make', 'WebappWebpackPlugin', (compilation, callback) =>
4141
// Generate favicons
4242
child.run(this.options, compiler.context, compilation)
43-
.then(result => {
43+
.then(tags => {
4444
if (this.options.inject) {
4545
// Hook into the html-webpack-plugin processing and add the html
46-
tap(compilation, 'html-webpack-plugin-before-html-processing', 'WebappWebpackPlugin', (htmlPluginData, callback) => {
47-
const htmlPluginDataInject = htmlPluginData.plugin.options.inject && htmlPluginData.plugin.options.favicons !== false;
46+
tapHtml(compilation, 'WebappWebpackPlugin', (htmlPluginData, callback) => {
47+
const htmlPluginDataInject = htmlPluginData.plugin.options.inject && htmlPluginData.plugin.options.favicons !== false;
4848
if ( htmlPluginDataInject || this.options.inject === 'force') {
49-
let position = htmlPluginData.html.search(/<\/head>/i);
50-
position = position === -1 ? htmlPluginData.html.length : position;
51-
htmlPluginData.html = [htmlPluginData.html.slice(0, position), result, htmlPluginData.html.slice(position)].join('');
49+
const idx = (htmlPluginData.html + '</head>').search(/<\/head>/i);
50+
htmlPluginData.html = [htmlPluginData.html.slice(0, idx), ...tags, htmlPluginData.html.slice(idx)].join('');
5251
}
5352
return callback(null, htmlPluginData);
5453
});

src/loader.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ module.exports = function (content) {
2020

2121
// Generate icons
2222
return favicons(content, Object.assign(query.options, {path: url.resolve(path, prefix)}))
23-
.then(result => {
24-
const html = result.html.join('');
25-
const assets = [...result.images, ...result.files].map(({name, contents}) => ({name: prefix + name, contents}));
26-
27-
return callback(null, 'module.exports = ' + JSON.stringify(msgpack.encode({html, assets}).toString('base64')));
23+
.then(({html: tags, images, files}) => {
24+
const assets = [...images, ...files].map(({name, contents}) => ({name: prefix + name, contents}));
25+
return callback(null, 'module.exports = ' + JSON.stringify(msgpack.encode({tags, assets}).toString('base64')));
2826
})
2927
.catch(callback);
3028
};
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
<!DOCTYPE html>
2-
<html>
3-
<head>
4-
<meta charset="UTF-8">
5-
<title>Webpack App</title>
6-
<link rel="shortcut icon" href="/assets/favicon.ico"><link rel="icon" type="image/png" sizes="16x16" href="/assets/favicon-16x16.png"><link rel="icon" type="image/png" sizes="32x32" href="/assets/favicon-32x32.png"><link rel="manifest" href="/assets/manifest.json"><meta name="mobile-web-app-capable" content="yes"><meta name="theme-color" content="#fff"><meta name="application-name"><link rel="apple-touch-icon" sizes="57x57" href="/assets/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/assets/apple-touch-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/assets/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/assets/apple-touch-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/assets/apple-touch-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/assets/apple-touch-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/assets/apple-touch-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/assets/apple-touch-icon-152x152.png"><link rel="apple-touch-icon" sizes="180x180" href="/assets/apple-touch-icon-180x180.png"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"><meta name="apple-mobile-web-app-title"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 1)" href="/assets/apple-touch-startup-image-320x460.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)" href="/assets/apple-touch-startup-image-640x920.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)" href="/assets/apple-touch-startup-image-640x1096.png"><link rel="apple-touch-startup-image" media="(device-width: 375px) and (device-height: 667px) and (-webkit-device-pixel-ratio: 2)" href="/assets/apple-touch-startup-image-750x1294.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 3)" href="/assets/apple-touch-startup-image-1182x2208.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 3)" href="/assets/apple-touch-startup-image-1242x2148.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 1)" href="/assets/apple-touch-startup-image-748x1024.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 1)" href="/assets/apple-touch-startup-image-768x1004.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)" href="/assets/apple-touch-startup-image-1496x2048.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)" href="/assets/apple-touch-startup-image-1536x2008.png"><link rel="icon" type="image/png" sizes="228x228" href="/assets/coast-228x228.png"><meta name="msapplication-TileColor" content="#fff"><meta name="msapplication-TileImage" content="/assets/mstile-144x144.png"><meta name="msapplication-config" content="/assets/browserconfig.xml"><link rel="yandex-tableau-widget" href="/assets/yandex-browser-manifest.json"></head>
7-
<body>
8-
</body>
9-
</html>
1+
<!doctype html><html><head><link rel="shortcut icon" href="/assets/favicon.ico"><link rel="icon" type="image/png" sizes="16x16" href="/assets/favicon-16x16.png"><link rel="icon" type="image/png" sizes="32x32" href="/assets/favicon-32x32.png"><link rel="manifest" href="/assets/manifest.json"><meta name="mobile-web-app-capable" content="yes"><meta name="theme-color" content="#fff"><meta name="application-name"><link rel="apple-touch-icon" sizes="57x57" href="/assets/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/assets/apple-touch-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/assets/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/assets/apple-touch-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/assets/apple-touch-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/assets/apple-touch-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/assets/apple-touch-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/assets/apple-touch-icon-152x152.png"><link rel="apple-touch-icon" sizes="180x180" href="/assets/apple-touch-icon-180x180.png"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"><meta name="apple-mobile-web-app-title"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 1)" href="/assets/apple-touch-startup-image-320x460.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)" href="/assets/apple-touch-startup-image-640x920.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)" href="/assets/apple-touch-startup-image-640x1096.png"><link rel="apple-touch-startup-image" media="(device-width: 375px) and (device-height: 667px) and (-webkit-device-pixel-ratio: 2)" href="/assets/apple-touch-startup-image-750x1294.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 3)" href="/assets/apple-touch-startup-image-1182x2208.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 3)" href="/assets/apple-touch-startup-image-1242x2148.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 1)" href="/assets/apple-touch-startup-image-748x1024.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 1)" href="/assets/apple-touch-startup-image-768x1004.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)" href="/assets/apple-touch-startup-image-1496x2048.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)" href="/assets/apple-touch-startup-image-1536x2008.png"><link rel="icon" type="image/png" sizes="228x228" href="/assets/coast-228x228.png"><meta name="msapplication-TileColor" content="#fff"><meta name="msapplication-TileImage" content="/assets/mstile-144x144.png"><meta name="msapplication-config" content="/assets/browserconfig.xml"><link rel="yandex-tableau-widget" href="/assets/yandex-browser-manifest.json"></head><body></body></html>
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
<!DOCTYPE html>
2-
<html>
3-
<head>
4-
<meta charset="UTF-8">
5-
<title>Webpack App</title>
6-
</head>
7-
<body>
8-
</body>
9-
</html>
1+
<!doctype html><html><head></head><body></body></html>
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
<!DOCTYPE html>
2-
<html>
3-
<head>
4-
<meta charset="UTF-8">
5-
<title>Webpack App</title>
6-
<link rel="shortcut icon" href="/custom/prefix/5026ef56/favicon.ico"><link rel="icon" type="image/png" sizes="16x16" href="/custom/prefix/5026ef56/favicon-16x16.png"><link rel="icon" type="image/png" sizes="32x32" href="/custom/prefix/5026ef56/favicon-32x32.png"><link rel="manifest" href="/custom/prefix/5026ef56/manifest.json"><meta name="mobile-web-app-capable" content="yes"><meta name="theme-color" content="#fff"><meta name="application-name"><link rel="apple-touch-icon" sizes="57x57" href="/custom/prefix/5026ef56/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/custom/prefix/5026ef56/apple-touch-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/custom/prefix/5026ef56/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/custom/prefix/5026ef56/apple-touch-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/custom/prefix/5026ef56/apple-touch-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/custom/prefix/5026ef56/apple-touch-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/custom/prefix/5026ef56/apple-touch-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/custom/prefix/5026ef56/apple-touch-icon-152x152.png"><link rel="apple-touch-icon" sizes="180x180" href="/custom/prefix/5026ef56/apple-touch-icon-180x180.png"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"><meta name="apple-mobile-web-app-title"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 1)" href="/custom/prefix/5026ef56/apple-touch-startup-image-320x460.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)" href="/custom/prefix/5026ef56/apple-touch-startup-image-640x920.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)" href="/custom/prefix/5026ef56/apple-touch-startup-image-640x1096.png"><link rel="apple-touch-startup-image" media="(device-width: 375px) and (device-height: 667px) and (-webkit-device-pixel-ratio: 2)" href="/custom/prefix/5026ef56/apple-touch-startup-image-750x1294.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 3)" href="/custom/prefix/5026ef56/apple-touch-startup-image-1182x2208.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 3)" href="/custom/prefix/5026ef56/apple-touch-startup-image-1242x2148.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 1)" href="/custom/prefix/5026ef56/apple-touch-startup-image-748x1024.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 1)" href="/custom/prefix/5026ef56/apple-touch-startup-image-768x1004.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)" href="/custom/prefix/5026ef56/apple-touch-startup-image-1496x2048.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)" href="/custom/prefix/5026ef56/apple-touch-startup-image-1536x2008.png"><link rel="icon" type="image/png" sizes="228x228" href="/custom/prefix/5026ef56/coast-228x228.png"><meta name="msapplication-TileColor" content="#fff"><meta name="msapplication-TileImage" content="/custom/prefix/5026ef56/mstile-144x144.png"><meta name="msapplication-config" content="/custom/prefix/5026ef56/browserconfig.xml"><link rel="yandex-tableau-widget" href="/custom/prefix/5026ef56/yandex-browser-manifest.json"></head>
7-
<body>
8-
</body>
9-
</html>
1+
<!doctype html><html><head><link rel="shortcut icon" href="/custom/prefix/5026ef56/favicon.ico"><link rel="icon" type="image/png" sizes="16x16" href="/custom/prefix/5026ef56/favicon-16x16.png"><link rel="icon" type="image/png" sizes="32x32" href="/custom/prefix/5026ef56/favicon-32x32.png"><link rel="manifest" href="/custom/prefix/5026ef56/manifest.json"><meta name="mobile-web-app-capable" content="yes"><meta name="theme-color" content="#fff"><meta name="application-name"><link rel="apple-touch-icon" sizes="57x57" href="/custom/prefix/5026ef56/apple-touch-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/custom/prefix/5026ef56/apple-touch-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/custom/prefix/5026ef56/apple-touch-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/custom/prefix/5026ef56/apple-touch-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/custom/prefix/5026ef56/apple-touch-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/custom/prefix/5026ef56/apple-touch-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/custom/prefix/5026ef56/apple-touch-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/custom/prefix/5026ef56/apple-touch-icon-152x152.png"><link rel="apple-touch-icon" sizes="180x180" href="/custom/prefix/5026ef56/apple-touch-icon-180x180.png"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"><meta name="apple-mobile-web-app-title"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 1)" href="/custom/prefix/5026ef56/apple-touch-startup-image-320x460.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)" href="/custom/prefix/5026ef56/apple-touch-startup-image-640x920.png"><link rel="apple-touch-startup-image" media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)" href="/custom/prefix/5026ef56/apple-touch-startup-image-640x1096.png"><link rel="apple-touch-startup-image" media="(device-width: 375px) and (device-height: 667px) and (-webkit-device-pixel-ratio: 2)" href="/custom/prefix/5026ef56/apple-touch-startup-image-750x1294.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 3)" href="/custom/prefix/5026ef56/apple-touch-startup-image-1182x2208.png"><link rel="apple-touch-startup-image" media="(device-width: 414px) and (device-height: 736px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 3)" href="/custom/prefix/5026ef56/apple-touch-startup-image-1242x2148.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 1)" href="/custom/prefix/5026ef56/apple-touch-startup-image-748x1024.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 1)" href="/custom/prefix/5026ef56/apple-touch-startup-image-768x1004.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)" href="/custom/prefix/5026ef56/apple-touch-startup-image-1496x2048.png"><link rel="apple-touch-startup-image" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)" href="/custom/prefix/5026ef56/apple-touch-startup-image-1536x2008.png"><link rel="icon" type="image/png" sizes="228x228" href="/custom/prefix/5026ef56/coast-228x228.png"><meta name="msapplication-TileColor" content="#fff"><meta name="msapplication-TileImage" content="/custom/prefix/5026ef56/mstile-144x144.png"><meta name="msapplication-config" content="/custom/prefix/5026ef56/browserconfig.xml"><link rel="yandex-tableau-widget" href="/custom/prefix/5026ef56/yandex-browser-manifest.json"></head><body></body></html>

0 commit comments

Comments
 (0)