gpt4 book ai didi

webpack - Webpack 中的规则与加载器 - 有什么区别?

转载 作者:行者123 更新时间:2023-12-03 05:29:33 25 4
gpt4 key购买 nike

在一些 Webpack 示例中,您会看到对“rules”数组的引用:

module.exports = {
module: {
rules: [
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
//resolve-url-loader may be chained before sass-loader if necessary
use: ['css-loader', 'sass-loader']
})
}
]
},
plugins: [
new ExtractTextPlugin('style.css')
//if you want to pass in options, you can do so:
//new ExtractTextPlugin({
// filename: 'style.css'
//})
]
}

( https://github.com/webpack-contrib/extract-text-webpack-plugin )

另外,还有一个加载器数组:

var ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
module: {
loaders: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
loader: "css-loader"
})
},
{ test: /\.png$/, loader: "file-loader" }
]
},
plugins: [
new ExtractTextPlugin({
filename: "style.css",
allChunks: true
})
]
};

( https://github.com/webpack/webpack/tree/master/examples/css-bundle )

有什么区别?应该使用哪个?

最佳答案

Loaders 用于 Webpack 1
规则用于Webpack 2+

根据migrating docs在官方 Webpack 站点。

module.loaders is now module.rules

The old loader configuration was superseded by a more powerful rules system, which allows configuration of loaders and more. For compatibility reasons, the old module.loaders syntax is still valid and the old names are parsed. The new naming conventions are easier to understand and are a good reason to upgrade the configuration to using module.rules.

Delta 示例

  module: {
- loaders: [
+ rules: [
{
test: /\.css$/,
- loaders: [
- "style-loader",
- "css-loader?modules=true"
+ use: [
+ {
+ loader: "style-loader"
+ },
+ {
+ loader: "css-loader",
+ options: {
+ modules: true
+ }
+ }
]
},
{
test: /\.jsx$/,
loader: "babel-loader", // Do not use "use" here
options: {
// ...
}
}
]
}

关于webpack - Webpack 中的规则与加载器 - 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43002099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com