Skip to content

Commit

Permalink
feat: template hook
Browse files Browse the repository at this point in the history
 - add `html.filter` `html.beforeInitialization` `html.afterInitialization`
  • Loading branch information
khronosleung committed Jul 20, 2017
1 parent 64bdc9f commit 34247ec
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions lib/config/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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);
});
}

Expand Down

0 comments on commit 34247ec

Please sign in to comment.