diff --git a/lib/config/html.js b/lib/config/html.js index a2ac212..57dee4b 100644 --- a/lib/config/html.js +++ b/lib/config/html.js @@ -16,6 +16,19 @@ const ReInlinePlaceholder = /\?__inline$/i; const ReUrlPlaceholder = /\?__url$/i; const ReUrl = /^(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/; +function isType(s, typeString) { + return {}.toString.call(s) === `[object ${typeString}]`; +} +function isObject(s) { + return isType(s, 'Object'); +} +function isFunction(s) { + return isType(s, 'Function'); +} +function isRegExp(s) { + return isType(s, 'RegExp'); +} + // 从源码提取扩展,支持模拟数据渲染 HtmlWebpackPlugin.prototype.executeTemplate = function (templateFunction, chunks, assets, compilation) { @@ -304,10 +317,23 @@ module.exports = function (options, webpackConfig) { matchBase: true }); + let htmlConfig = isObject(webpackConfig.html) ? webpackConfig.html : {}; + let result = []; if (htmlFiles.length) { htmlFiles.forEach(function (file) { + let filterResult = true; + if (isFunction(htmlConfig.filter)) { + filterResult = htmlConfig.filter(path.join(htmlDirCwd, file)); + } else if (isRegExp(htmlConfig.filter)) { + filterResult = htmlConfig.filter.test(path.join(htmlDirCwd, file)); + } + + if (filterResult === false) { + return; + } + let info = path.parse(file); let renderDataPath = path.join(webpackConfig.context, 'src', 'mock', info.name); let renderData = {}; @@ -341,11 +367,17 @@ module.exports = function (options, webpackConfig) { } }; - if (typeof webpackConfig.html === 'function') { - HtmlWebpackPluginConfig = webpackConfig.html(HtmlWebpackPluginConfig); + if (isFunction(htmlConfig.beforeInitialization)) { + htmlConfig.beforeInitialization(HtmlWebpackPluginConfig); + } + + let obj = new HtmlWebpackPlugin(HtmlWebpackPluginConfig); + + if (isFunction(htmlConfig.afterInitialization)) { + obj = htmlConfig.afterInitialization(obj); } - result.push(new HtmlWebpackPlugin(HtmlWebpackPluginConfig)); + result.push(obj); }); }