Skip to content

Commit 302e39e

Browse files
committedJul 10, 2018
feat: Add default viewport meta tag for default template
BREAKING CHANGE: The default template has now a predefined viewport meta tag fix #897, fix #978
1 parent d7ec407 commit 302e39e

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed
 

‎examples/default/dist/webpack-4/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8">
55
<title>Webpack App</title>
6-
</head>
6+
<meta name="viewport" content="width=device-width, initial-scale=1"></head>
77
<body>
88
<script src="bundle.js"></script></body>
99
</html>

‎examples/favicon/dist/webpack-4/favicon.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8">
55
<title>HtmlWebpackPlugin example</title>
6-
<link rel="shortcut icon" href="favicon.ico"><link href="styles.css" rel="stylesheet"></head>
6+
<meta name="viewport" content="width=device-width, initial-scale=1"><link rel="shortcut icon" href="favicon.ico"><link href="styles.css" rel="stylesheet"></head>
77
<body>
88
<script src="bundle.js"></script></body>
99
</html>

‎index.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class HtmlWebpackPlugin {
3030
* @param {Partial<HtmlWebpackPluginOptions>} [options]
3131
*/
3232
constructor (options) {
33+
/** @type {Partial<HtmlWebpackPluginOptions>} */
34+
const userOptions = options || {};
35+
3336
// Default options
3437
/** @type {HtmlWebpackPluginOptions} */
3538
const defaultOptions = {
@@ -51,8 +54,19 @@ class HtmlWebpackPlugin {
5154
title: 'Webpack App',
5255
xhtml: false
5356
};
57+
5458
/** @type {HtmlWebpackPluginOptions} */
55-
this.options = Object.assign(defaultOptions, options);
59+
this.options = Object.assign(defaultOptions, userOptions);
60+
61+
// Default metaOptions if no template is provided
62+
if (!userOptions.template && this.options.templateContent === false && this.options.meta) {
63+
const defaultMeta = {
64+
// From https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
65+
viewport: 'width=device-width, initial-scale=1'
66+
};
67+
this.options.meta = Object.assign({}, this.options.meta, defaultMeta, userOptions.meta);
68+
}
69+
5670
// Instance variables to keep caching information
5771
// for multiple builds
5872
this.childCompilerHash = undefined;
@@ -502,16 +516,21 @@ class HtmlWebpackPlugin {
502516
// Make tags self-closing in case of xhtml
503517
// Turn { "viewport" : "width=500, initial-scale=1" } into
504518
// [{ name:"viewport" content:"width=500, initial-scale=1" }]
505-
const metaTagAttributeObjects = Object.keys(metaOptions).map((metaName) => {
519+
const metaTagAttributeObjects = Object.keys(metaOptions)
520+
.map((metaName) => {
506521
const metaTagContent = metaOptions[metaName];
507522
return (typeof metaTagContent === 'string') ? {
508523
name: metaName,
509524
content: metaTagContent
510525
} : metaTagContent;
511-
});
526+
})
527+
.filter((attribute) => attribute !== false);
512528
// Turn [{ name:"viewport" content:"width=500, initial-scale=1" }] into
513529
// the html-webpack-plugin tag structure
514530
return metaTagAttributeObjects.map((metaTagAttributes) => {
531+
if (metaTagAttributes === false) {
532+
throw new Error('Invalid meta tag');
533+
}
515534
return {
516535
tagName: 'meta',
517536
voidTag: true,

‎typings.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ interface HtmlWebpackPluginOptions {
7272
*/
7373
meta: false // Disable injection
7474
| {
75-
[name: string]: string // name content pair e.g. {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`
75+
[name: string]: string|false // name content pair e.g. {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`
7676
| {[attributeName: string]: string|boolean} // custom properties e.g. { name:"viewport" content:"width=500, initial-scale=1" }
7777
},
7878
/**

0 commit comments

Comments
 (0)
Please sign in to comment.