Skip to content

Commit 26a3704

Browse files
committed
fix: do not modify index.ejs with default viewport meta tag
Take into account `src/index.ejs` existence while deciding to add default meta-tags. Closes: #1387
1 parent d5ce5a8 commit 26a3704

File tree

9 files changed

+537
-4
lines changed

9 files changed

+537
-4
lines changed

examples/default-template-location/dist/webpack-5/bundle.js

+478
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# default template location
2+
3+
in this example a template is placed in the default location
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require('./main.css');
2+
var h1 = document.createElement('h1');
3+
h1.innerHTML = 'Hello world!';
4+
document.body.appendChild(h1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title><%= htmlWebpackPlugin.options.title %></title>
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<meta name="MobileOptimized" content="width">
8+
</head>
9+
<body>
10+
<h1>Template</h1>
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: snow;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var path = require('path');
2+
var HtmlWebpackPlugin = require('../..');
3+
var webpackMajorVersion = require('webpack/package.json').version.split('.')[0];
4+
5+
module.exports = {
6+
context: __dirname,
7+
entry: './src/example.js',
8+
output: {
9+
path: path.join(__dirname, 'dist/webpack-' + webpackMajorVersion),
10+
publicPath: '',
11+
filename: 'bundle.js'
12+
},
13+
module: {
14+
rules: [
15+
{ test: /\.css$/, use: ['style-loader', 'css-loader'] },
16+
{ test: /\.png$/, type: 'asset/resource' }
17+
]
18+
},
19+
plugins: [
20+
new HtmlWebpackPlugin()
21+
]
22+
};

index.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HtmlWebpackPlugin {
3636
}
3737

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

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

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

155+
function isTemplateInDefaultLocation(context) {
156+
const template = path.resolve(context, 'src/index.ejs');
157+
return fs.existsSync(template);
158+
}
159+
155160
/**
156161
* connect the html-webpack-plugin to the webpack compiler lifecycle hooks
157162
*
@@ -1018,8 +1023,9 @@ function hookIntoCompiler (compiler, options, plugin) {
10181023
*/
10191024
function getFullTemplatePath (template, context) {
10201025
if (template === 'auto') {
1021-
template = path.resolve(context, 'src/index.ejs');
1022-
if (!fs.existsSync(template)) {
1026+
if (isTemplateInDefaultLocation(context)) {
1027+
template = path.resolve(context, 'src/index.ejs');
1028+
} else {
10231029
template = path.join(__dirname, 'default_index.ejs');
10241030
}
10251031
}

spec/example.spec.js

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ describe('HtmlWebpackPlugin Examples', () => {
6767
runExample('default', done);
6868
});
6969

70+
it('default template location example', done => {
71+
runExample('default-template-location', done);
72+
});
73+
7074
it('favicon example', done => {
7175
runExample('favicon', done);
7276
});

0 commit comments

Comments
 (0)