Skip to content

Commit

Permalink
fix: Use src/index.ejs by default if present (#1167)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Use src/index.ejs if no template option is set.
  • Loading branch information
jantimon committed Mar 22, 2019
1 parent d4eb1c7 commit c27e5e4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -142,8 +142,8 @@ Allowed values are as follows
|:--:|:--:|:-----:|:----------|
|**`title`**|`{String}`|`Webpack App`|The title to use for the generated HTML document|
|**`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`)|
|**`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|
|**[`templateParameters`](#)**|`{Boolean\|Object\|Function}`|``| Allows to overwrite the parameters used in the template |
|**`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|
|**`templateParameters`**|`{Boolean\|Object\|Function}`|``| Allows to overwrite the parameters used in the template |
|**`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|
|**`favicon`**|`{String}`|``|Adds the given favicon path to the output HTML|
|**`meta`**|`{Object}`|`{}`|Allows to inject `meta`-tags. E.g. `meta: {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`|
Expand Down
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -38,7 +38,7 @@ class HtmlWebpackPlugin {
// Default options
/** @type {ProcessedHtmlWebpackOptions} */
const defaultOptions = {
template: path.join(__dirname, 'default_index.ejs'),
template: 'auto',
templateContent: false,
templateParameters: templateParametersGenerator,
filename: 'index.html',
Expand Down Expand Up @@ -908,6 +908,12 @@ class HtmlWebpackPlugin {
* The webpack base resolution path for relative paths e.g. process.cwd()
*/
getFullTemplatePath (template, context) {
if (template === 'auto') {
template = path.resolve(context, 'src/index.ejs');
if (!fs.existsSync(template)) {
template = path.join(__dirname, 'default_index.ejs');
}
}
// If the template doesn't use a loader use the lodash template loader
if (template.indexOf('!') === -1) {
template = require.resolve('./lib/loader.js') + '!' + path.resolve(context, template);
Expand Down
16 changes: 16 additions & 0 deletions spec/basic.spec.js
Expand Up @@ -214,6 +214,22 @@ describe('HtmlWebpackPlugin', () => {
['<script src="app_bundle.js', 'Some unique text'], null, done);
});

it('picks up src/index.ejs by default', done => {
testHtmlPlugin({
mode: 'production',
context: path.join(__dirname, 'fixtures'),
entry: {
app: './index.js'
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [new HtmlWebpackPlugin()]
},
['<script src="app_bundle.js', 'src/index.ejs'], null, done);
});

it('allows you to inject the assets into a given html file', done => {
testHtmlPlugin({
mode: 'production',
Expand Down
9 changes: 9 additions & 0 deletions spec/fixtures/src/index.ejs
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>src/index.ejs</title>
</head>
<body>
</body>
</html>

0 comments on commit c27e5e4

Please sign in to comment.