webpack4--MiniCssExtractPlugin(extract-text-webpack-plugin)

在使用webpack打包时,将css代码从bundle.js中抽离出来,单独出一个模块,需要用到extract-text-webpack-plugin插件

webpack版本不同,相对于的插件也不同,如下:

# for webpack 4
npm install --save-dev mini-css-extract-plugin 
# for webpack 3
npm install --save-dev extract-text-webpack-plugin
# for webpack 2
npm install --save-dev 
# for webpack 1
npm install --save-dev

之前版本使用方法,可参照:https://github.com/webpack-contrib/extract-text-webpack-plugin

webpack4中,用mini-css-extract-plugin替代。

webpack.config.js:

const MiniCssExtractPlugin = require(‘mini-css-extract-plugin‘);
 
module.exports = {
  plugins: [new MiniCssExtractPlugin(
    filename:‘./css/[name].css‘
  )],
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              publicPath: ‘../‘,         hmr: process.env.NODE_ENV === ‘development‘,  
            },
          },
          ‘css-loader‘,
        ],
      },
    ],
  },
};

更多mini-css-extract-plugin的使用方法请查看:https://www.npmjs.com/package/mini-css-extract-plugin

相关推荐