动态的获取entry和设置html-webpack-plugin的数量
利用glob.sync
const setMPA = () => {
const entry = {};
const HtmlWebpackPlugins = [];
const entryFiles = glob.sync(path.join(__dirname, "./src/*/index.js"));
console.log("entryFiles", entryFiles);
entryFiles.map((file) => {
const entryFileName = file.match(/\/src\/(.*)\/index\.js/)[1];
entry[entryFileName] = file;
HtmlWebpackPlugins.push(
new HtmlWebpackPlugin({
template: `src/${entryFileName}/index.html`,
filename: `${entryFileName}.html`,
chunks: [entryFileName],
})
);
});
return {
entry,
HtmlWebpackPlugins,
};
};
const { entry, HtmlWebpackPlugins } = setMPA();
module.exports={
entry,
plugins: [
...HtmlWebpackPlugins,
],
}