Skip to content

Commit

Permalink
feat: Add default viewport meta tag for default template
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The default template has now a predefined viewport meta tag

fix #897, fix #978
  • Loading branch information
jantimon committed Jul 6, 2018
1 parent bbc07a3 commit 8650546
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/default/dist/webpack-4/index.html
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Webpack App</title>
</head>
<meta name="viewport" content="width=device-width, initial-scale=1"></head>
<body>
<script src="bundle.js"></script></body>
</html>
2 changes: 1 addition & 1 deletion examples/favicon/dist/webpack-4/favicon.html
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>HtmlWebpackPlugin example</title>
<link rel="shortcut icon" href="favicon.ico"><link href="styles.css" rel="stylesheet"></head>
<meta name="viewport" content="width=device-width, initial-scale=1"><link rel="shortcut icon" href="favicon.ico"><link href="styles.css" rel="stylesheet"></head>
<body>
<script src="bundle.js"></script></body>
</html>
25 changes: 22 additions & 3 deletions index.js
Expand Up @@ -31,6 +31,9 @@ class HtmlWebpackPlugin {
* @param {Partial<HtmlWebpackPluginOptions>} options
*/
constructor (options) {
/** @type {Partial<HtmlWebpackPluginOptions>} */
const userOptions = options || {};

// Default options
/** @type {HtmlWebpackPluginOptions} */
const defaultOptions = {
Expand All @@ -52,8 +55,19 @@ class HtmlWebpackPlugin {
title: 'Webpack App',
xhtml: false
};

/** @type {HtmlWebpackPluginOptions} */
this.options = Object.assign(defaultOptions, options);
this.options = Object.assign(defaultOptions, userOptions);

// Default metaOptions if no template is provided
if (!userOptions.template && this.options.templateContent === false && this.options.meta) {
const defaultMeta = {
// From https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
viewport: 'width=device-width, initial-scale=1'
};
this.options.meta = Object.assign({}, this.options.meta, defaultMeta, userOptions.meta);
}

// Instance variables to keep caching information
// for multiple builds
this.childCompilerHash = undefined;
Expand Down Expand Up @@ -506,16 +520,21 @@ class HtmlWebpackPlugin {
// Make tags self-closing in case of xhtml
// Turn { "viewport" : "width=500, initial-scale=1" } into
// [{ name:"viewport" content:"width=500, initial-scale=1" }]
const metaTagAttributeObjects = Object.keys(metaOptions).map((metaName) => {
const metaTagAttributeObjects = Object.keys(metaOptions)
.map((metaName) => {
const metaTagContent = metaOptions[metaName];
return (typeof metaTagContent === 'string') ? {
name: metaName,
content: metaTagContent
} : metaTagContent;
});
})
.filter((attribute) => attribute !== false);
// Turn [{ name:"viewport" content:"width=500, initial-scale=1" }] into
// the html-webpack-plugin tag structure
return metaTagAttributeObjects.map((metaTagAttributes) => {
if (metaTagAttributes === false) {
throw new Error('Invalid meta tag');
}
return {
tagName: 'meta',
voidTag: true,
Expand Down
2 changes: 1 addition & 1 deletion typings.d.ts
Expand Up @@ -72,7 +72,7 @@ interface HtmlWebpackPluginOptions {
*/
meta: false // Disable injection
| {
[name: string]: string // name content pair e.g. {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`
[name: string]: string|false // name content pair e.g. {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`
| {[attributeName: string]: string|boolean} // custom properties e.g. { name:"viewport" content:"width=500, initial-scale=1" }
},
/**
Expand Down

0 comments on commit 8650546

Please sign in to comment.