Skip to content

Commit c27e5e4

Browse files
authoredMar 22, 2019
fix: Use src/index.ejs by default if present (#1167)
BREAKING CHANGE: Use src/index.ejs if no template option is set.
1 parent d4eb1c7 commit c27e5e4

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ Allowed values are as follows
142142
|:--:|:--:|:-----:|:----------|
143143
|**`title`**|`{String}`|`Webpack App`|The title to use for the generated HTML document|
144144
|**`filename`**|`{String}`|`'index.html'`|The file to write the HTML to. Defaults to `index.html`. You can specify a subdirectory here too (eg: `assets/admin.html`)|
145-
|**`template`**|`{String}`|``|`webpack` require path to the template. Please see the [docs](https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md) for details|
146-
|**[`templateParameters`](#)**|`{Boolean\|Object\|Function}`|``| Allows to overwrite the parameters used in the template |
145+
|**`template`**|`{String}`|``|`webpack` relative or absolute path to the template. By default it will use `src/index.ejs` if it exists. Please see the [docs](https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md) for details|
146+
|**`templateParameters`**|`{Boolean\|Object\|Function}`|``| Allows to overwrite the parameters used in the template |
147147
|**`inject`**|`{Boolean\|String}`|`true`|`true \|\| 'head' \|\| 'body' \|\| false` Inject all assets into the given `template` or `templateContent`. When passing `true` or `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element|
148148
|**`favicon`**|`{String}`|``|Adds the given favicon path to the output HTML|
149149
|**`meta`**|`{Object}`|`{}`|Allows to inject `meta`-tags. E.g. `meta: {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`|

‎index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class HtmlWebpackPlugin {
3838
// Default options
3939
/** @type {ProcessedHtmlWebpackOptions} */
4040
const defaultOptions = {
41-
template: path.join(__dirname, 'default_index.ejs'),
41+
template: 'auto',
4242
templateContent: false,
4343
templateParameters: templateParametersGenerator,
4444
filename: 'index.html',
@@ -908,6 +908,12 @@ class HtmlWebpackPlugin {
908908
* The webpack base resolution path for relative paths e.g. process.cwd()
909909
*/
910910
getFullTemplatePath (template, context) {
911+
if (template === 'auto') {
912+
template = path.resolve(context, 'src/index.ejs');
913+
if (!fs.existsSync(template)) {
914+
template = path.join(__dirname, 'default_index.ejs');
915+
}
916+
}
911917
// If the template doesn't use a loader use the lodash template loader
912918
if (template.indexOf('!') === -1) {
913919
template = require.resolve('./lib/loader.js') + '!' + path.resolve(context, template);

‎spec/basic.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,22 @@ describe('HtmlWebpackPlugin', () => {
214214
['<script src="app_bundle.js', 'Some unique text'], null, done);
215215
});
216216

217+
it('picks up src/index.ejs by default', done => {
218+
testHtmlPlugin({
219+
mode: 'production',
220+
context: path.join(__dirname, 'fixtures'),
221+
entry: {
222+
app: './index.js'
223+
},
224+
output: {
225+
path: OUTPUT_DIR,
226+
filename: '[name]_bundle.js'
227+
},
228+
plugins: [new HtmlWebpackPlugin()]
229+
},
230+
['<script src="app_bundle.js', 'src/index.ejs'], null, done);
231+
});
232+
217233
it('allows you to inject the assets into a given html file', done => {
218234
testHtmlPlugin({
219235
mode: 'production',

‎spec/fixtures/src/index.ejs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>src/index.ejs</title>
6+
</head>
7+
<body>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.