webpack的exract-text-webpack-plugin插件的疑问

//webpack.config.js
const extractCSS = new ExtractTextPlugin('[name]-one.css');
module: {
        rules: [
            
            {
                test: /\.css$/,
                 use: extractCSS.extract([ 'css-loader', 'postcss-loader' ]) //疑问
            }
        ]
    },
    
plugins: [
    extractCSS
  ]

刚学习webpack,请问注释行的代码是什么意思,查了官方文档没看明白。

官方实例:

Multiple Instances There is also an extract function on the instance.
You should use this if you have more than one instance of ExtractTextPlugin.

const ExtractTextPlugin = require('extract-text-webpack-plugin');
 
// Create multiple instances 
const extractCSS = new ExtractTextPlugin('stylesheets/[name]-one.css');
 
module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: extractCSS.extract([ 'css-loader', 'postcss-loader' ])
      }
    ]
  },
  plugins: [
    extractCSS
  ]
};

这是官方对extract方法的参数说明:

extract

ExtractTextPlugin.extract(options: loader | object)
Creates an extracting loader from an existing loader. Supports loaders of type { loader: [name]-loader -> {String}, options: {} -> {Object} }.

  1. options.use
    Loader(s) that should be used for converting the resource to a CSS exporting module (required)
  2. options.fallback
    loader(e.g 'style-loader') that should be used when the CSS is not extracted (i.e. in an additional chunk when allChunks: false)
  3. options.publicPath
    Override the publicPath setting for this loader
阅读 2.1k
1 个回答

你这是webpack1.x吧?
首先webpack在页面中引用某css比如require("a.css"); 如果没有配置项的话是css in js形式,
如果配置了style-loader是可以把css文件提取到style标签里边然后添加到head标签内的,如果在此之上配置了ExtractTextPlugin插件,就可以把这样式提取到文件中,不知道LZ了解了吗,毕竟单纯的理论有些晦涩(less sass等同理,只是多了一些配置)?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题