webpack配置 抽离css样式

webpack抽离css样式

  •   安装插件:npm i mini-css-extract-plugin -D
  •   在webpack.config.js文件中引入 const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  • 代码片段
​const MiniCssExtractPlugin = require('mini-css-extract-plugin');//引入插件
  
  module: {
        rules: [   
            {     
               
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,//使用 MiniCssExtractPlugin.loader
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                          ident: 'postcss',
                          plugins: [
                            require('autoprefixer')(),
                           
                          ]
                        }
                      }//先于css-loader被处理
                  
                ]
            },
            {
                test: /\.less$/,
                use: [
                    MiniCssExtractPlugin.loader,//使用 MiniCssExtractPlugin.loader
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                          ident: 'postcss',
                          plugins: [
                            require('autoprefixer')(),
                           
                          ]
                        }
                      },
                     'less-loader'
                    ]
            }
           
        ]
    },
plugins: [
      
        new MiniCssExtractPlugin({
            filename: 'css/main.css',//产出目录
            
        }),
    ]

​
发布了21 篇原创文章 · 获赞 0 · 访问量 328

猜你喜欢

转载自blog.csdn.net/weixin_46337813/article/details/104620667