【发布时间】:2019-12-08 04:15:36
【问题描述】:
所以我正在为我的公司开发一个新的 webpack 配置,以让我们获得最新的功能(Webpack 4 等),但我遇到了一个障碍。我需要这个 webpack 配置来支持 CSS 模块和全局 CSS 样式,所以我一直在尝试相应地配置我的加载器。我发现我的 CSS/SCSS 模块正在编译,但我的全局样式没有。
我的 webpack 配置:
const cssLoader = (useModules) => {
const base = {
loader: 'css-loader',
options: {
importLoaders: 5
}
};
if (useModules) {
base.options.modules = {
localIdentName: '[name]__[local]__cssmod[hash:base64:5]'
}
}
return base;
};
const postCssLoader = {
loader: 'postcss-loader',
options: {
config: {
path: path.resolve(__dirname, 'postcss.config.js')
}
}
};
const config = {
mode: 'production',
entry: path.resolve(__dirname, 'src'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.js' // TODO we want a hash here
},
module: {
rules: [
{
test: /\.jsx?$/,
include: path.resolve(__dirname, 'src'),
exclude: path.resolve(__dirname, 'node_modules'),
use: {
loader: 'babel-loader'
}
},
{
test: /\.css$/,
exclude: /\.module\.css$/,
use: [
MiniCssExtractPlugin.loader,
cssLoader(false),
postCssLoader,
'resolve-url-loader'
]
},
{
test: /\.scss$/,
exclude: /\.module\.scss$/,
use: [
MiniCssExtractPlugin.loader,
cssLoader(false),
postCssLoader,
'resolve-url-loader',
'sass-loader'
]
},
{
test: /\.module\.css$/,
use: [
MiniCssExtractPlugin.loader,
cssLoader(true),
postCssLoader,
'resolve-url-loader'
]
},
{
test: /\.module\.scss$/,
use: [
MiniCssExtractPlugin.loader,
cssLoader(true),
postCssLoader,
'resolve-url-loader',
'sass-loader'
]
}
]
},
optimization: {
usedExports: true
},
plugins: [
new MiniCssExtractPlugin({
disable: false,
filename: 'app.css' // TODO we want a hash here
}),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: 'react-project',
noScriptMessage: 'This requires JavaScript',
inject: false,
filename: 'index.html',
template: path.resolve(__dirname, 'src/index-template.html'),
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
})
]
};
我不明白这里可能出了什么问题。
附言。如果我注释掉 CSS/SCSS 模块的两条规则,那么全局样式会很好地捆绑在一起,而模块样式则会被忽略。也许这意味着什么?
【问题讨论】:
标签: webpack