• 动态获取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

        1. |-index

        |-index.html |-index.js |-index.css

        1. |-search

        |-static |-index.html |-index.js |-search.css

    1. // webpack.prod.js
    2. 'use strict';
    3. const path = require('path');
    4. const glob = require('glob');
    5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
    6. const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
    7. const HtmlWebpackPlugin = require("html-webpack-plugin");
    8. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
    9. const setMPA = () => {
    10. const entry = {};
    11. const htmlWebpackPlugins = [];
    12. const entryFiles = glob.sync(path.join(__dirname, './src/*/index.js'));
    13. Object.keys(entryFiles).map(item => {
    14. const entryFile = entryFiles[item];
    15. const match = entryFile.match(/.src\/(.*)\/index.js/);
    16. const pageName = match && match[1];
    17. entry[pageName] = entryFile;
    18. htmlWebpackPlugins.push(
    19. new HtmlWebpackPlugin({
    20. template: path.join(__dirname, `src/${pageName}/index.html`),
    21. filename: `${pageName}.html`,
    22. // chunks决定了html会引用的js
    23. chunks: [pageName],
    24. inject: true,
    25. minify: {
    26. html5: true,
    27. collapseWhitespace: true,
    28. preserveLineBreaks: false,
    29. minifyCSS: true,
    30. minifyJS: true,
    31. removeComments: false
    32. }
    33. })
    34. )
    35. })
    36. return {
    37. entry,
    38. htmlWebpackPlugins,
    39. }
    40. }
    41. const { entry, htmlWebpackPlugins } = setMPA();
    42. module.exports = {
    43. entry: entry,
    44. output: {
    45. path: path.join(__dirname, 'dist'),
    46. filename: '[name]_[chunkhash:8].js',
    47. },
    48. module: {
    49. rules: [
    50. {
    51. test: /\.js$/,
    52. use: 'babel-loader',
    53. },
    54. {
    55. test: /\.css$/,
    56. use: [MiniCssExtractPlugin.loader, 'css-loader']
    57. },
    58. {
    59. test: /\.less$/,
    60. use: [
    61. MiniCssExtractPlugin.loader,
    62. 'css-loader',
    63. 'less-loader',
    64. {
    65. loader: 'postcss-loader',
    66. options: {
    67. postcssOptions: { plugins: [['postcss-preset-env']] },
    68. }
    69. },
    70. {
    71. loader: 'px2rem-loader',
    72. options: {
    73. remUnit: 75,
    74. remPrecision: 8
    75. }
    76. }
    77. ],
    78. },
    79. {
    80. test: /\.(scss|sass)$/,
    81. use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
    82. },
    83. {
    84. test: /\.(png|jpg|gif|jpeg|svg)$/,
    85. use: [{
    86. loader: 'file-loader',
    87. options: {
    88. name: 'img/[name]_[hash:8].[ext]'
    89. }
    90. }]
    91. },
    92. {
    93. test: /\.(woff|woff2|eot|ttf|otf)$/,
    94. use: [{
    95. loader: 'file-loader',
    96. options: {
    97. name: 'fonts/[name]_[hash:8].[ext]'
    98. }
    99. }]
    100. }
    101. ]
    102. },
    103. plugins: [
    104. new MiniCssExtractPlugin({
    105. filename: '[name]_[contenthash:8].css'
    106. }),
    107. new CssMinimizerPlugin(),
    108. new CleanWebpackPlugin(),
    109. ].concat(htmlWebpackPlugins),
    110. mode: 'production',
    111. }