Skip to content

Commit

Permalink
fix: do not modify index.ejs with default viewport meta tag
Browse files Browse the repository at this point in the history
Take into account `src/index.ejs` existence while deciding to add default meta-tags.

Closes: jantimon#1387
  • Loading branch information
Den-dp committed Jun 4, 2022
1 parent d5ce5a8 commit 26a3704
Show file tree
Hide file tree
Showing 9 changed files with 537 additions and 4 deletions.
478 changes: 478 additions & 0 deletions examples/default-template-location/dist/webpack-5/bundle.js

Large diffs are not rendered by default.

@@ -0,0 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Webpack App</title><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="MobileOptimized" content="width"><script defer="defer" src="bundle.js"></script></head><body><h1>Template</h1></body></html>
3 changes: 3 additions & 0 deletions examples/default-template-location/readme.md
@@ -0,0 +1,3 @@
# default template location

in this example a template is placed in the default location
4 changes: 4 additions & 0 deletions examples/default-template-location/src/example.js
@@ -0,0 +1,4 @@
require('./main.css');
var h1 = document.createElement('h1');
h1.innerHTML = 'Hello world!';
document.body.appendChild(h1);
12 changes: 12 additions & 0 deletions examples/default-template-location/src/index.ejs
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><%= htmlWebpackPlugin.options.title %></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="MobileOptimized" content="width">
</head>
<body>
<h1>Template</h1>
</body>
</html>
3 changes: 3 additions & 0 deletions examples/default-template-location/src/main.css
@@ -0,0 +1,3 @@
body {
background: snow;
}
22 changes: 22 additions & 0 deletions examples/default-template-location/webpack.config.js
@@ -0,0 +1,22 @@
var path = require('path');
var HtmlWebpackPlugin = require('../..');
var webpackMajorVersion = require('webpack/package.json').version.split('.')[0];

module.exports = {
context: __dirname,
entry: './src/example.js',
output: {
path: path.join(__dirname, 'dist/webpack-' + webpackMajorVersion),
publicPath: '',
filename: 'bundle.js'
},
module: {
rules: [
{ test: /\.css$/, use: ['style-loader', 'css-loader'] },
{ test: /\.png$/, type: 'asset/resource' }
]
},
plugins: [
new HtmlWebpackPlugin()
]
};
14 changes: 10 additions & 4 deletions index.js
Expand Up @@ -36,7 +36,7 @@ class HtmlWebpackPlugin {
}

apply (compiler) {
// Wait for configuration preset plugions to apply all configure webpack defaults
// Wait for configuration preset plugins to apply all configure webpack defaults
compiler.hooks.initialize.tap('HtmlWebpackPlugin', () => {
const userOptions = this.userOptions;

Expand Down Expand Up @@ -74,7 +74,7 @@ class HtmlWebpackPlugin {
assert(options.inject === true || options.inject === false || options.inject === 'head' || options.inject === 'body', 'inject needs to be set to true, false, "head" or "body');

// Default metaOptions if no template is provided
if (!userOptions.template && options.templateContent === false && options.meta) {
if (!userOptions.template && !isTemplateInDefaultLocation(compiler.context) && options.templateContent === false && options.meta) {
const defaultMeta = {
// From https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
viewport: 'width=device-width, initial-scale=1'
Expand Down Expand Up @@ -152,6 +152,11 @@ class HtmlWebpackPlugin {
}
}

function isTemplateInDefaultLocation(context) {
const template = path.resolve(context, 'src/index.ejs');
return fs.existsSync(template);
}

/**
* connect the html-webpack-plugin to the webpack compiler lifecycle hooks
*
Expand Down Expand Up @@ -1018,8 +1023,9 @@ function hookIntoCompiler (compiler, options, plugin) {
*/
function getFullTemplatePath (template, context) {
if (template === 'auto') {
template = path.resolve(context, 'src/index.ejs');
if (!fs.existsSync(template)) {
if (isTemplateInDefaultLocation(context)) {
template = path.resolve(context, 'src/index.ejs');
} else {
template = path.join(__dirname, 'default_index.ejs');
}
}
Expand Down
4 changes: 4 additions & 0 deletions spec/example.spec.js
Expand Up @@ -67,6 +67,10 @@ describe('HtmlWebpackPlugin Examples', () => {
runExample('default', done);
});

it('default template location example', done => {
runExample('default-template-location', done);
});

it('favicon example', done => {
runExample('favicon', done);
});
Expand Down

0 comments on commit 26a3704

Please sign in to comment.