1. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
    2. const TerserJSPlugin = require('terser-webpack-plugin')
    3. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
    4. moduel.exports = {
    5. /* ...... */
    6. module: {
    7. rules: [
    8. {
    9. // css的抽离
    10. test: /\.css$/,
    11. loader: [
    12. // 这里不使用style-css
    13. MiniCssExtractPlugin.loader,
    14. 'css-loader',
    15. 'postcss-plugin'
    16. ]
    17. },
    18. {
    19. // less的抽离
    20. test: /\.less$/,
    21. loader: [
    22. MiniCssExtractPlugin.loader,
    23. 'css-loader',
    24. 'less-loader',
    25. 'postcss-loader'
    26. ]
    27. }
    28. ],
    29. /* ..... */
    30. plugins: [
    31. /* .... */
    32. // 抽离css文件
    33. new MiniCssExtractPlugin({
    34. filename: 'css/main.[contentHash:8].css'
    35. })
    36. ],
    37. optimization: {
    38. // 压缩css
    39. minimzer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})]
    40. }
    41. }
    42. }