【发布时间】:2019-01-05 03:24:33
【问题描述】:
我正在使用 Webpack 4。
我的webpack.config.js 中有两个入口点,我使用mini-css-extract-plugin 为每个入口点生成一个单独的CSS 文件。
const webpack = require('webpack')
const path = require('path')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = {
mode: 'development',
entry: {
foo: './foo.js',
bar: './bar.js'
},
output: {
path: path.resolve(__dirname, 'src/'),
filename: 'js/[name].js'
},
plugins: [
new MiniCssExtractPlugin({
filename: "css/[name].css"
})
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
"css-loader", "postcss-loader"
]
}
]
}
}
我也在使用 postcss-cssnext。
在我的根文件夹中的postcss.config.js 文件中,我在customProperties 中定义了一些CSS 变量,如下所示:
module.exports = {
plugins: {
'postcss-cssnext': {
features: {
customProperties: {
variables: {
color1: '#333',
color2: '#53565A',
baseFont: 'Helvetica, sans-serif'
}
}
}
}
}
}
如果我希望我的 CSS 变量对我的两个入口点中的每一个都有不同的值,我将如何做呢?
例如,假设在 foo.css 中我希望 var(--color1) 等于 #333,但在 bar.css 中我希望它等于 #860000。
【问题讨论】:
-
答案与postcss-loader options.config.context有关。这允许您使您的 postcss.config.js 文件动态化。如果您使用的是 webpack,则需要在 webpack.config.js 中包含 multiple configurations,每个入口点一个
标签: javascript css webpack postcss cssnext