动态的获取entry和设置html-webpack-plugin的数量

    利用glob.sync

    1. const setMPA = () => {
    2. const entry = {};
    3. const HtmlWebpackPlugins = [];
    4. const entryFiles = glob.sync(path.join(__dirname, "./src/*/index.js"));
    5. console.log("entryFiles", entryFiles);
    6. entryFiles.map((file) => {
    7. const entryFileName = file.match(/\/src\/(.*)\/index\.js/)[1];
    8. entry[entryFileName] = file;
    9. HtmlWebpackPlugins.push(
    10. new HtmlWebpackPlugin({
    11. template: `src/${entryFileName}/index.html`,
    12. filename: `${entryFileName}.html`,
    13. chunks: [entryFileName],
    14. })
    15. );
    16. });
    17. return {
    18. entry,
    19. HtmlWebpackPlugins,
    20. };
    21. };
    22. const { entry, HtmlWebpackPlugins } = setMPA();
    23. module.exports={
    24. entry,
    25. plugins: [
    26. ...HtmlWebpackPlugins,
    27. ],
    28. }