diff --git a/README.md b/README.md index 8080d1ae..d010ea18 100644 --- a/README.md +++ b/README.md @@ -157,9 +157,9 @@ Allowed values are as follows: |**`hash`**|`{Boolean}`|`false`|If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files. This is useful for cache busting| |**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed| |**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page| -|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)| -|**`chunksSortMode`**|`{String\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \| 'auto' \| 'manual' \| {Function}`| -|**`excludeChunks`**|`{Array.}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)| +|**`chunks`**|`{Array.\|RegExp\|Function\|'all'}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)| +|**[`chunksSortMode`]**|`{String\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \| 'auto' \| 'dependency' \| 'manual' \| {Function}`| +|**`excludeChunks`**|`{Array.\|RegExp\|Function}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)| |**`xhtml`**|`{Boolean}`|`false`|If `true` render the `link` tags as self-closing (XHTML compliant)| Here's an example webpack config illustrating how to use these options diff --git a/index.js b/index.js index de9a6294..40274c75 100644 --- a/index.js +++ b/index.js @@ -553,19 +553,34 @@ function hookIntoCompiler (compiler, options, plugin) { /** * Return all chunks from the compilation result which match the exclude and include filters * @param {any} chunks - * @param {string[]|'all'} includedChunks - * @param {string[]} excludedChunks + * @param {string[]|{test(chunkName: string): boolean}|((chunkName: string) => boolean)|'all'} includedChunks + * @param {string[]|{test(chunkName: string): boolean}|((chunkName: string) => boolean)} excludedChunks */ function filterChunks (chunks, includedChunks, excludedChunks) { return chunks.filter(chunkName => { // Skip if the chunks should be filtered and the given chunk was not added explicity - if (Array.isArray(includedChunks) && includedChunks.indexOf(chunkName) === -1) { + if (Array.isArray(includedChunks) && includedChunks.indexOf(chunkName) === -1) { // chunks: Array return false; + } else if (includedChunks instanceof RegExp) { // chunks: RegExp + return includedChunks.test(chunkName); + } else if (typeof includedChunks === 'function') { // chunks: Function + return includedChunks(chunkName); } + // if (Array.isArray(includedChunks) && includedChunks.indexOf(chunkName) === -1) { + // return false; + // } + // Skip if the chunks should be filtered and the given chunk was excluded explicity - if (Array.isArray(excludedChunks) && excludedChunks.indexOf(chunkName) !== -1) { + if (Array.isArray(excludedChunks) && excludedChunks.indexOf(chunkName) !== -1) { // chunks: Array return false; + } else if (excludedChunks instanceof RegExp) { // chunks: RegExp + return !excludedChunks.test(chunkName); + } else if (typeof excludedChunks === 'function') { // chunks: Function + return excludedChunks(chunkName); } + // if (Array.isArray(excludedChunks) && excludedChunks.indexOf(chunkName) !== -1) { + // return false; + // } // Add otherwise return true; }); diff --git a/spec/basic.spec.js b/spec/basic.spec.js index b325f7cd..8c64b117 100644 --- a/spec/basic.spec.js +++ b/spec/basic.spec.js @@ -365,6 +365,46 @@ describe('HtmlWebpackPlugin', () => { }, ['