Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: Use src/index.ejs by default if present #1167

Merged
merged 1 commit into from
Mar 22, 2019
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
jantimon marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>src/index.ejs</title>
</head>
<body>
</body>
</html>