Skip to content

Commit

Permalink
fix: don't add extra meta tag if it exists (#1802)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jun 10, 2023
1 parent 432e6a0 commit 8f92788
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class HtmlWebpackPlugin {
// Default metaOptions if no template is provided
if (!userOptions.template && options.templateContent === false && options.meta) {
const defaultMeta = {
// From https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
// TODO remove in the next major release
// From https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
viewport: 'width=device-width, initial-scale=1'
};
options.meta = Object.assign({}, options.meta, defaultMeta, userOptions.meta);
Expand Down Expand Up @@ -966,8 +967,15 @@ function hookIntoCompiler (compiler, options, plugin) {
const htmlRegExp = /(<html[^>]*>)/i;
const headRegExp = /(<\/head\s*>)/i;
const bodyRegExp = /(<\/body\s*>)/i;
const metaViewportRegExp = /<meta[^>]+name=["']viewport["'][^>]*>/i;
const body = assetTags.bodyTags.map((assetTagObject) => htmlTagObjectToString(assetTagObject, options.xhtml));
const head = assetTags.headTags.map((assetTagObject) => htmlTagObjectToString(assetTagObject, options.xhtml));
const head = assetTags.headTags.filter((item) => {
if (item.tagName === 'meta' && item.attributes && item.attributes.name === 'viewport' && metaViewportRegExp.test(html)) {
return false;
}

return true;
}).map((assetTagObject) => htmlTagObjectToString(assetTagObject, options.xhtml));

if (body.length) {
if (bodyRegExp.test(html)) {
Expand Down
15 changes: 15 additions & 0 deletions spec/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,21 @@ describe('HtmlWebpackPlugin', () => {
}, [/<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">/], null, done);
});

it('avoid duplicate meta tags for default template', done => {
testHtmlPlugin({
mode: 'production',
entry: path.join(__dirname, 'fixtures/index.js'),
context: path.join(__dirname, 'fixtures'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [
new HtmlWebpackPlugin()
]
}, [/<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);
});

it('adds a meta tag with short notation', done => {
testHtmlPlugin({
mode: 'production',
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/src/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<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>
</head>
<body>
Expand Down

0 comments on commit 8f92788

Please sign in to comment.