From d2112bf67c73ec03ae40b3bcfdcd2ddae89bb4ba Mon Sep 17 00:00:00 2001 From: LI JUN Date: Thu, 12 Dec 2019 14:49:22 +0800 Subject: [PATCH 1/9] Support RegExp and Function `options.chunks` and` options.excludeChunks` support RegExp and Function --- index.js | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 2442e373..45048430 100644 --- a/index.js +++ b/index.js @@ -27,6 +27,26 @@ const getHtmlWebpackPluginHooks = require('./lib/hooks.js').getHtmlWebpackPlugin const fsStatAsync = promisify(fs.stat); const fsReadFileAsync = promisify(fs.readFile); +function dataType(obj){ + let str = Object.prototype.toString.call(obj); + let type = (str.substring(str.indexOf(" ") + 1, str.indexOf("]"))).toLowerCase(); + + return type; +} +function isRegExp(obj){ + return "regexp" === dataType(obj); +} +function isFunction(obj){ + return "function" === dataType(obj); +} +function isArray(obj){ + if(Array.isArray){ + return Array.isArray(obj); + } + + return "array" === dataType(obj) +} + class HtmlWebpackPlugin { /** * @param {HtmlWebpackOptions} [options] @@ -505,13 +525,28 @@ class HtmlWebpackPlugin { 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) { - return false; - } + if(isArray(includedChunks)){ // chunks: Array + return includedChunks.indexOf(chunkName) !== -1; + }else if(isRegExp(includedChunks)){ // chunks: RegExp + return includedChunks.test(chunkName); + }else if(isFunction(includedChunks)){ // 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) { - return false; - } + if(isArray(excludedChunks)){ // chunks: Array + return excludedChunks.indexOf(chunkName) === -1; + }else if(isRegExp(excludedChunks)){ // chunks: RegExp + return excludedChunks.test(chunkName) + }else if(isFunction(excludedChunks)){ // chunks: Function + return excludedChunks(chunkName); + } + // if (Array.isArray(excludedChunks) && excludedChunks.indexOf(chunkName) !== -1) { + // return false; + // } // Add otherwise return true; }); From 6b46eacb643365b76dfb2f103fe62f91883531a3 Mon Sep 17 00:00:00 2001 From: LI JUN Date: Thu, 12 Dec 2019 15:14:01 +0800 Subject: [PATCH 2/9] update filterChunks() comment add RegExp and Function parameter type --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 45048430..5f312bcc 100644 --- a/index.js +++ b/index.js @@ -519,8 +519,8 @@ class HtmlWebpackPlugin { /** * 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[]|RegExp|Function|'all'} includedChunks + * @param {string[]|RegExp|Function} excludedChunks */ filterChunks (chunks, includedChunks, excludedChunks) { return chunks.filter(chunkName => { From 16d60ce1bb8c47a46e2279b76a0c4b0d747f3e33 Mon Sep 17 00:00:00 2001 From: LI JUN Date: Thu, 12 Dec 2019 15:38:23 +0800 Subject: [PATCH 3/9] fix: eslint fix: eslint error --- index.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 5f312bcc..24383cf1 100644 --- a/index.js +++ b/index.js @@ -27,24 +27,24 @@ const getHtmlWebpackPluginHooks = require('./lib/hooks.js').getHtmlWebpackPlugin const fsStatAsync = promisify(fs.stat); const fsReadFileAsync = promisify(fs.readFile); -function dataType(obj){ - let str = Object.prototype.toString.call(obj); - let type = (str.substring(str.indexOf(" ") + 1, str.indexOf("]"))).toLowerCase(); +function dataType (obj) { + let str = Object.prototype.toString.call(obj); + let type = (str.substring(str.indexOf(' ') + 1, str.indexOf(']'))).toLowerCase(); - return type; + return type; } -function isRegExp(obj){ - return "regexp" === dataType(obj); +function isRegExp (obj) { + return 'regexp' === dataType(obj); } -function isFunction(obj){ - return "function" === dataType(obj); +function isFunction (obj) { + return 'function' === dataType(obj); } -function isArray(obj){ - if(Array.isArray){ +function isArray (obj) { + if (Array.isArray) { return Array.isArray(obj); } - return "array" === dataType(obj) + return 'array' === dataType(obj) } class HtmlWebpackPlugin { @@ -525,11 +525,11 @@ class HtmlWebpackPlugin { filterChunks (chunks, includedChunks, excludedChunks) { return chunks.filter(chunkName => { // Skip if the chunks should be filtered and the given chunk was not added explicity - if(isArray(includedChunks)){ // chunks: Array + if (isArray(includedChunks)) { // chunks: Array return includedChunks.indexOf(chunkName) !== -1; - }else if(isRegExp(includedChunks)){ // chunks: RegExp + } else if (isRegExp(includedChunks)) { // chunks: RegExp return includedChunks.test(chunkName); - }else if(isFunction(includedChunks)){ // chunks: Function + } else if (isFunction(includedChunks)) { // chunks: Function return includedChunks(chunkName); } // if (Array.isArray(includedChunks) && includedChunks.indexOf(chunkName) === -1) { @@ -537,11 +537,11 @@ class HtmlWebpackPlugin { // } // Skip if the chunks should be filtered and the given chunk was excluded explicity - if(isArray(excludedChunks)){ // chunks: Array + if (isArray(excludedChunks)) { // chunks: Array return excludedChunks.indexOf(chunkName) === -1; - }else if(isRegExp(excludedChunks)){ // chunks: RegExp + } else if (isRegExp(excludedChunks)) { // chunks: RegExp return excludedChunks.test(chunkName) - }else if(isFunction(excludedChunks)){ // chunks: Function + } else if (isFunction(excludedChunks)) { // chunks: Function return excludedChunks(chunkName); } // if (Array.isArray(excludedChunks) && excludedChunks.indexOf(chunkName) !== -1) { From 5e79421e9383da4aa26fcc107fedb2afe35fbf76 Mon Sep 17 00:00:00 2001 From: LI JUN Date: Thu, 12 Dec 2019 15:46:42 +0800 Subject: [PATCH 4/9] fix eslint fix eslint error --- index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 24383cf1..eabcabf3 100644 --- a/index.js +++ b/index.js @@ -34,17 +34,17 @@ function dataType (obj) { return type; } function isRegExp (obj) { - return 'regexp' === dataType(obj); + return dataType(obj) === 'regexp'; } function isFunction (obj) { - return 'function' === dataType(obj); + return dataType(obj) === 'function'; } function isArray (obj) { - if (Array.isArray) { - return Array.isArray(obj); - } + if (Array.isArray) { + return Array.isArray(obj); + } - return 'array' === dataType(obj) + return dataType(obj) === 'array'; } class HtmlWebpackPlugin { @@ -531,19 +531,19 @@ class HtmlWebpackPlugin { return includedChunks.test(chunkName); } else if (isFunction(includedChunks)) { // 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 (isArray(excludedChunks)) { // chunks: Array return excludedChunks.indexOf(chunkName) === -1; } else if (isRegExp(excludedChunks)) { // chunks: RegExp - return excludedChunks.test(chunkName) + return excludedChunks.test(chunkName); } else if (isFunction(excludedChunks)) { // chunks: Function return excludedChunks(chunkName); - } + } // if (Array.isArray(excludedChunks) && excludedChunks.indexOf(chunkName) !== -1) { // return false; // } From dc320b735a96b6b836faecb3f988c682c6726af0 Mon Sep 17 00:00:00 2001 From: LI JUN Date: Thu, 12 Dec 2019 15:50:54 +0800 Subject: [PATCH 5/9] fix travis-cli job error --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index eabcabf3..b80be431 100644 --- a/index.js +++ b/index.js @@ -531,7 +531,7 @@ class HtmlWebpackPlugin { return includedChunks.test(chunkName); } else if (isFunction(includedChunks)) { // chunks: Function return includedChunks(chunkName); - } + } // if (Array.isArray(includedChunks) && includedChunks.indexOf(chunkName) === -1) { // return false; // } From 4837ebf5480c4021266d8c0d3658fbd2e13be484 Mon Sep 17 00:00:00 2001 From: LI JUN Date: Thu, 12 Dec 2019 17:36:24 +0800 Subject: [PATCH 6/9] fix: resolve test suites --- index.js | 42 +++++++++++------------------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index b80be431..0ca432e3 100644 --- a/index.js +++ b/index.js @@ -27,26 +27,6 @@ const getHtmlWebpackPluginHooks = require('./lib/hooks.js').getHtmlWebpackPlugin const fsStatAsync = promisify(fs.stat); const fsReadFileAsync = promisify(fs.readFile); -function dataType (obj) { - let str = Object.prototype.toString.call(obj); - let type = (str.substring(str.indexOf(' ') + 1, str.indexOf(']'))).toLowerCase(); - - return type; -} -function isRegExp (obj) { - return dataType(obj) === 'regexp'; -} -function isFunction (obj) { - return dataType(obj) === 'function'; -} -function isArray (obj) { - if (Array.isArray) { - return Array.isArray(obj); - } - - return dataType(obj) === 'array'; -} - class HtmlWebpackPlugin { /** * @param {HtmlWebpackOptions} [options] @@ -519,17 +499,17 @@ class HtmlWebpackPlugin { /** * Return all chunks from the compilation result which match the exclude and include filters * @param {any} chunks - * @param {string[]|RegExp|Function|'all'} includedChunks - * @param {string[]|RegExp|Function} excludedChunks + * @param {string[]|{test(chunkName: string): boolean}|((chunkName: string) => boolean)|'all'} includedChunks + * @param {string[]|{test(chunkName: string): boolean}|((chunkName: string) => boolean)} excludedChunks */ filterChunks (chunks, includedChunks, excludedChunks) { return chunks.filter(chunkName => { // Skip if the chunks should be filtered and the given chunk was not added explicity - if (isArray(includedChunks)) { // chunks: Array - return includedChunks.indexOf(chunkName) !== -1; - } else if (isRegExp(includedChunks)) { // chunks: RegExp + 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 (isFunction(includedChunks)) { // chunks: Function + } else if (typeof includedChunks === 'function') { // chunks: Function return includedChunks(chunkName); } // if (Array.isArray(includedChunks) && includedChunks.indexOf(chunkName) === -1) { @@ -537,11 +517,11 @@ class HtmlWebpackPlugin { // } // Skip if the chunks should be filtered and the given chunk was excluded explicity - if (isArray(excludedChunks)) { // chunks: Array - return excludedChunks.indexOf(chunkName) === -1; - } else if (isRegExp(excludedChunks)) { // chunks: RegExp - return excludedChunks.test(chunkName); - } else if (isFunction(excludedChunks)) { // chunks: Function + 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) { From 33ae453e1a568e679abfd8e6e29ddb877d17c8e9 Mon Sep 17 00:00:00 2001 From: LI JUN Date: Thu, 12 Dec 2019 17:37:01 +0800 Subject: [PATCH 7/9] fix: resolve test suites --- typings.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/typings.d.ts b/typings.d.ts index baf1b553..9c241309 100644 --- a/typings.d.ts +++ b/typings.d.ts @@ -27,7 +27,7 @@ declare namespace HtmlWebpackPlugin { /** * List all entries which should be injected */ - chunks: "all" | string[]; + chunks: "all" | string[] | RegExp | (((chunkName: string) => boolean)); /** * Allows to control how chunks should be sorted before they are included to the html. * Default: `'auto'`. @@ -39,7 +39,7 @@ declare namespace HtmlWebpackPlugin { /** * List all entries which should not be injeccted */ - excludeChunks: string[]; + excludeChunks: string[] | RegExp | (((chunkName: string) => boolean)); /** * Path to the favicon icon */ From 0e04a7800556802f60f75af7ff9e97cc6d367949 Mon Sep 17 00:00:00 2001 From: LI JUN Date: Thu, 12 Dec 2019 17:37:42 +0800 Subject: [PATCH 8/9] resolve test suites --- spec/basic.spec.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/spec/basic.spec.js b/spec/basic.spec.js index 2092c5dc..71ce7366 100644 --- a/spec/basic.spec.js +++ b/spec/basic.spec.js @@ -315,6 +315,46 @@ describe('HtmlWebpackPlugin', () => { }, ['