我正在尝试使用 webpack 4 min-css-extract-plugin 和 splitChunks 插件生成多个 css。
这是我的 webpack 配置。
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require("path");
module.exports = [{
entry: './index.js',
output: {
filename: '[name].js',
path: path.resolve(path.join(__dirname, "./dist")),
},
optimization: {
splitChunks: {
cacheGroups: {
vendor: false,
commons: {
name: 'commons',
test: /.styl$/,
chunks: 'all',
enforce: true,
minChunks: 1,
},
englishStyle: {
name: 'styles_en',
test: (c) => {
return c.type.match(/mini-css-extract-plugin/) && c._identifier.indexOf('_ar') === -1;
},
chunks: 'all',
priority: 1,
enforce: true,
},
arabicStyles: {
name: 'styles_ar',
test: (c) => {
return c.type.match(/mini-css-extract-plugin/) && c._identifier.indexOf('_ar') !== -1;
},
priority: 1,
chunks: 'all',
enforce: true,
}
}
}
},
module: {
rules: [
{
test: /\.styl$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"stylus-loader"
],
},
]
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[name].css"
})
]
}]
这是我的css文件结构。
common.styl
...
style.styl
@import common.styl
...
style_ar.styl
@import common.styl
...
index.js
import styles from style.styl
import styles from style_ar.styl
上面的配置只生成了styles_ar.css和style.css两个文件。在两个文件中都有共同的内容。
如何为公共文件生成单独的文件?
如果我优先考虑commons cacheGroup 只会生成一个文件commons.styl。