From c5beb4bd16e4916b5355c300abebf9d7d3c587da Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Thu, 29 Oct 2020 23:32:20 +0100 Subject: [PATCH] feat: filename can now be a function --- README.md | 2 +- index.js | 23 +++++++++++++++-------- spec/basic.spec.js | 34 ++++++++++++++++++++++++++++++++++ typings.d.ts | 5 ++++- 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 24694980..48d0be11 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ Allowed values are as follows: |Name|Type|Default|Description| |:--:|:--:|:-----:|:----------| |**`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`)| +|**`filename`**|`{String\|Function}`|`'index.html'`|The file to write the HTML to. Defaults to `index.html`. You can specify a subdirectory here too (eg: `assets/admin.html`). The `[name]` placeholder will be replaced with the entry name. Can also be a function e.g. `(entryName) => entryName + '.html'`. | |**`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| |**`templateContent`**|`{string\|Function\|false}`|false| Can be used instead of `template` to provide an inline template - please read the [Writing Your Own Templates](https://github.com/jantimon/html-webpack-plugin#writing-your-own-templates) section | |**`templateParameters`**|`{Boolean\|Object\|Function}`| `false`| Allows to overwrite the parameters used in the template - see [example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/template-parameters) | diff --git a/index.js b/index.js index 7d66a141..a775835f 100644 --- a/index.js +++ b/index.js @@ -84,16 +84,23 @@ class HtmlWebpackPlugin { options.meta = Object.assign({}, options.meta, defaultMeta, userOptions.meta); } - // Get all filenNames to support [name] - const multipleOptions = options.filename.includes('[name]') - ? Object.keys(compiler.options.entry).map((entryName) => ({ - ...options, - filename: options.filename.replace(/\[name\]/g, entryName) - })) - : [options]; + // entryName to fileName conversion + const filenameFunction = typeof options.filename === 'function' + ? options.filename + // Replace '[name]' with entry name + : (entryName) => options.filename.replace(/\[name\]/g, entryName); + + /** output filenames for the given entry names */ + const outputFileNames = new Set(Object.keys(compiler.options.entry).map(filenameFunction)); + + /** Option for every entry point */ + const entryOptions = Array.from(outputFileNames).map((filename) => ({ + ...options, + filename + })); // Hook all options into the webpack compiler - multipleOptions.forEach((instanceOptions) => { + entryOptions.forEach((instanceOptions) => { hookIntoCompiler(compiler, instanceOptions, this); }); }); diff --git a/spec/basic.spec.js b/spec/basic.spec.js index f693f32a..c8467ad1 100644 --- a/spec/basic.spec.js +++ b/spec/basic.spec.js @@ -240,6 +240,40 @@ describe('HtmlWebpackPlugin', () => { ['