Skip to content
const { merge } = require('webpack-merge'); //合并
//通过link标签引入css
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// 压缩css
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const baseConfig = require('./webpack.base.js');

module.exports = merge(baseConfig, {
  mode: 'production',
  devtool: 'hidden-source-map',
  plugins: [
    new MiniCssExtractPlugin({
      //自定义命名,剥离出单独css文件的名称,通过入口文件index.js引入
      filename: 'style/[name].[contenthash:8].css',
      chunkFilename: 'style/[name].[contenthash:8].css'
    }),
    //压缩css  mini-css-extract-plugin结合optimize-css-assets-webpack-plugin使用
    new OptimizeCSSAssetsPlugin({})
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader, //配合MiniCssExtractPlugin插件
          'css-loader', //先处理css语法  如@import / url() require('./index.css')
          'postcss-loader' //兼容css3写法,添加浏览器前缀
        ]
      },
      {
        test: /\.less$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader', //兼容css3写法,添加浏览器前缀
          'less-loader' //处理less
        ]
      },
      {
        test: /\.s(a|c)ss$/i,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader', //兼容css3写法,添加浏览器前缀
          'sass-loader'
        ]
      }
    ]
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

MIT Licensed