我为样式表设置了相当复杂的加载程序:
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract("style",
"css?sourceMap&localIdentName=[path][name]__[local]__[hash:base64:5]!sass?outputStyle=expanded&" +
"includePaths[]=" + other stuff)
)
}
哪个有用,但有些要求我想将modules
选项添加到css-loader中,所以它看起来像这样:
require('./foo.scss!css?modules&sourceMap&localIdentName=[path][name]__[local]__[hash...');
但我无法在整个地方做到这一点。
如何配置它以便我可以在某些需要时启用css-loader模块标志,同时保持其余部分相同?
也许像装载机'别名',例如require('./foo.scss!my-scss-with-modules-flag-alias')
?
我能想到的唯一解决方案是编写一个加载器,它使用语法转换将加载器配置内联到某些需要的调用中......但这很脆弱且复杂。
答案 0 :(得分:16)
resolveLoader.alias将在这里工作。即
resolveLoader: {
alias: {
'with-modules': 'loader definition goes here',
}
}
使用此配置,您只需
即可require('with-modules!./foo.scss');
代码。
答案 1 :(得分:1)
resolveLoader.alias可能在给定的情况下有效,因为您使用插件作为加载器。 但是,如果您需要使用一系列加载器,它将无法工作。 (还有一个功能请求:https://github.com/webpack/webpack/issues/2772)。
相反,您可以在require语句
中向文件添加参数var styles = require('./foo.scss?modules');
并创建一个新的加载器规则:
module: {
loaders: [
...
{ test: /\.scss$/, loader: 'style!css!postcss!sass' },
{ test: /\.scss\?modules$/, loader: 'style!css?modules!postcss!sass' }
]
}