Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not modify index.ejs with default viewport meta tag #1693

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
478 changes: 478 additions & 0 deletions examples/default-template-location/dist/webpack-5/bundle.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: snow;
}
22 changes: 22 additions & 0 deletions examples/default-template-location/webpack.config.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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