webpack(七) 动态打包多入口文件

2021-01-20  本文已影响0人  Viewwei
npm install global  html-webpack-plugin -D
const setMpa = () => {
  const entry = {};
  const htmlwebpackplugins = [];

  const entryFiles = glob.sync(path.join(__dirname, "./src/*/index.js"));

  entryFiles.map((item, index) => {
    const entryFile = entryFiles[index];
    const match = entryFile.match(/src\/(.*)\/index\.js$/);
    const pageName = match[1];
    entry[pageName] = entryFile;
    htmlwebpackplugins.push(
      new htmlWebpackPlugin({
        template: `./src/${pageName}/index.html`,
        filename: `${pageName}.html`,
        chunks: [pageName], //表示:只会把这个chunk的信息添加到这段html中
      })
    );
  });

  return {
    entry,
    htmlwebpackplugins,
  };
};
const { entry, htmlwebpackplugins } = setMpa(); //把entry htmlwebpackplugins 分别配置到entry和plugins中就可以了
上一篇下一篇

猜你喜欢

热点阅读