Skip to content

Commit 8f92788

Browse files
fix: don't add extra meta tag if it exists (#1802)
1 parent 432e6a0 commit 8f92788

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class HtmlWebpackPlugin {
7676
// Default metaOptions if no template is provided
7777
if (!userOptions.template && options.templateContent === false && options.meta) {
7878
const defaultMeta = {
79-
// From https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
79+
// TODO remove in the next major release
80+
// From https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
8081
viewport: 'width=device-width, initial-scale=1'
8182
};
8283
options.meta = Object.assign({}, options.meta, defaultMeta, userOptions.meta);
@@ -966,8 +967,15 @@ function hookIntoCompiler (compiler, options, plugin) {
966967
const htmlRegExp = /(<html[^>]*>)/i;
967968
const headRegExp = /(<\/head\s*>)/i;
968969
const bodyRegExp = /(<\/body\s*>)/i;
970+
const metaViewportRegExp = /<meta[^>]+name=["']viewport["'][^>]*>/i;
969971
const body = assetTags.bodyTags.map((assetTagObject) => htmlTagObjectToString(assetTagObject, options.xhtml));
970-
const head = assetTags.headTags.map((assetTagObject) => htmlTagObjectToString(assetTagObject, options.xhtml));
972+
const head = assetTags.headTags.filter((item) => {
973+
if (item.tagName === 'meta' && item.attributes && item.attributes.name === 'viewport' && metaViewportRegExp.test(html)) {
974+
return false;
975+
}
976+
977+
return true;
978+
}).map((assetTagObject) => htmlTagObjectToString(assetTagObject, options.xhtml));
971979

972980
if (body.length) {
973981
if (bodyRegExp.test(html)) {

spec/basic.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,21 @@ describe('HtmlWebpackPlugin', () => {
19581958
}, [/<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">/], null, done);
19591959
});
19601960

1961+
it('avoid duplicate meta tags for default template', done => {
1962+
testHtmlPlugin({
1963+
mode: 'production',
1964+
entry: path.join(__dirname, 'fixtures/index.js'),
1965+
context: path.join(__dirname, 'fixtures'),
1966+
output: {
1967+
path: OUTPUT_DIR,
1968+
filename: 'index_bundle.js'
1969+
},
1970+
plugins: [
1971+
new HtmlWebpackPlugin()
1972+
]
1973+
}, [/<head><meta charset="utf-8"\/><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,viewport-fit=cover"><title>src\/index\.ejs<\/title><script defer="defer" src="index_bundle.js"><\/script><\/head>/], null, done);
1974+
});
1975+
19611976
it('adds a meta tag with short notation', done => {
19621977
testHtmlPlugin({
19631978
mode: 'production',

spec/fixtures/src/index.ejs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html>
33
<head>
44
<meta charset="utf-8"/>
5+
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,viewport-fit=cover">
56
<title>src/index.ejs</title>
67
</head>
78
<body>

0 commit comments

Comments
 (0)