- 动态获取entry和设置html-webpack-plugin数量
 - 利用
glob.sync- 下载包:
npm i glob -D entry: glob.sync(path,join(__dirname,'./src/*/index.js'))文件夹目录 |-app |-node_modules |-.babelrc |-package.json |-webpack.prod.js |-src
|-index
|-index.html |-index.js |-index.css
|-search
|-static |-index.html |-index.js |-search.css
 - 下载包:
 
// webpack.prod.js'use strict';const path = require('path');const glob = require('glob');const MiniCssExtractPlugin = require('mini-css-extract-plugin');const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");const HtmlWebpackPlugin = require("html-webpack-plugin");const { CleanWebpackPlugin } = require('clean-webpack-plugin');const setMPA = () => {const entry = {};const htmlWebpackPlugins = [];const entryFiles = glob.sync(path.join(__dirname, './src/*/index.js'));Object.keys(entryFiles).map(item => {const entryFile = entryFiles[item];const match = entryFile.match(/.src\/(.*)\/index.js/);const pageName = match && match[1];entry[pageName] = entryFile;htmlWebpackPlugins.push(new HtmlWebpackPlugin({template: path.join(__dirname, `src/${pageName}/index.html`),filename: `${pageName}.html`,// chunks决定了html会引用的jschunks: [pageName],inject: true,minify: {html5: true,collapseWhitespace: true,preserveLineBreaks: false,minifyCSS: true,minifyJS: true,removeComments: false}}))})return {entry,htmlWebpackPlugins,}}const { entry, htmlWebpackPlugins } = setMPA();module.exports = {entry: entry,output: {path: path.join(__dirname, 'dist'),filename: '[name]_[chunkhash:8].js',},module: {rules: [{test: /\.js$/,use: 'babel-loader',},{test: /\.css$/,use: [MiniCssExtractPlugin.loader, 'css-loader']},{test: /\.less$/,use: [MiniCssExtractPlugin.loader,'css-loader','less-loader',{loader: 'postcss-loader',options: {postcssOptions: { plugins: [['postcss-preset-env']] },}},{loader: 'px2rem-loader',options: {remUnit: 75,remPrecision: 8}}],},{test: /\.(scss|sass)$/,use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],},{test: /\.(png|jpg|gif|jpeg|svg)$/,use: [{loader: 'file-loader',options: {name: 'img/[name]_[hash:8].[ext]'}}]},{test: /\.(woff|woff2|eot|ttf|otf)$/,use: [{loader: 'file-loader',options: {name: 'fonts/[name]_[hash:8].[ext]'}}]}]},plugins: [new MiniCssExtractPlugin({filename: '[name]_[contenthash:8].css'}),new CssMinimizerPlugin(),new CleanWebpackPlugin(),].concat(htmlWebpackPlugins),mode: 'production',}
