Webpack 和 PostCSS 函数混合不可见
Webpack & PostCSS Function Mixins are not visible
当前项目有react、webpack、postcss。这里的目的是创建全局混合。我确实尝试了几种方法并得出了以下结果。
初衷
var postCSSConfig = [
require('postcss-import'),
// require('precss'),
require('postcss-mixins')({
aloha: {
color: 'red'
}
}),
require('postcss-cssnext')({
browsers: ['last 2 versions', '> 5%'],
}),
]
module.exports = postCSSConfig;
因此跨项目使用 @mixin aloha
甚至尚未定义的 mixins 都不会影响样式表,也不会给出错误。
二意。
module.exports = {
plugins: {
'postcss-import': {},
'postcss-mixins': {
aloha: {
color: 'red'
}
},
'precss': {},
'postcss-cssnext': {
},
},
};
此时会抛出@mixin aloha
未定义的错误。
Webpack 配置
const path = require('path'),
webpack = require('webpack'),
HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: [
'react-hot-loader/patch'
'webpack-dev-server/client?http://localhost:8090',
'webpack/hot/only-dev-server',
'./index.js'
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'build'),
publicPath: '/'
},
context: path.resolve(__dirname, 'logic'),
devtool: 'inline-source-map',
devServer: {
hot: true,
contentBase: path.resolve(__dirname, 'build'),
publicPath: '/',
port: 8090
},
module: {
rules: [
{
test: /\.js$/,
use: [
'babel-loader',
],
exclude: /node_modules/
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader?modules',
'postcss-loader',
],
},
{
test: /\.(jpg|png|svg)$/,
loader: 'url-loader',
options: {
limit: 25000,
},
},
{
test: /\.(ttf|eot|woff|woff2)$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]',
},
}
],
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
template: './index.template.html'
})
],
}
你能告诉我我哪里错了吗?
postcss.config.js
中的导出需要是一个带有 plugins
属性 的对象,它包含一个插件数组(您需要导入),如 postcss-loader
- Usage.
而 postcss-mixins
接受一个带有 属性 mixin
的对象,它本身是一个带有 mixins 的对象,但是你只是直接给它 mixins,见 postcss-mixins
- Function Mixin(mixin 可以是函数或对象)。
因此您的 postcss.config.js
应该是:
module.exports = {
plugins: [
require('postcss-import'),
// require('precss'),
require('postcss-mixins')({
mixins: {
aloha: {
color: 'red'
}
}
}),
require('postcss-cssnext')({
browsers: ['last 2 versions', '> 5%'],
})
]
};
当前项目有react、webpack、postcss。这里的目的是创建全局混合。我确实尝试了几种方法并得出了以下结果。
初衷
var postCSSConfig = [
require('postcss-import'),
// require('precss'),
require('postcss-mixins')({
aloha: {
color: 'red'
}
}),
require('postcss-cssnext')({
browsers: ['last 2 versions', '> 5%'],
}),
]
module.exports = postCSSConfig;
因此跨项目使用 @mixin aloha
甚至尚未定义的 mixins 都不会影响样式表,也不会给出错误。
二意。
module.exports = {
plugins: {
'postcss-import': {},
'postcss-mixins': {
aloha: {
color: 'red'
}
},
'precss': {},
'postcss-cssnext': {
},
},
};
此时会抛出@mixin aloha
未定义的错误。
Webpack 配置
const path = require('path'),
webpack = require('webpack'),
HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: [
'react-hot-loader/patch'
'webpack-dev-server/client?http://localhost:8090',
'webpack/hot/only-dev-server',
'./index.js'
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'build'),
publicPath: '/'
},
context: path.resolve(__dirname, 'logic'),
devtool: 'inline-source-map',
devServer: {
hot: true,
contentBase: path.resolve(__dirname, 'build'),
publicPath: '/',
port: 8090
},
module: {
rules: [
{
test: /\.js$/,
use: [
'babel-loader',
],
exclude: /node_modules/
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader?modules',
'postcss-loader',
],
},
{
test: /\.(jpg|png|svg)$/,
loader: 'url-loader',
options: {
limit: 25000,
},
},
{
test: /\.(ttf|eot|woff|woff2)$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]',
},
}
],
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new HtmlWebpackPlugin({
template: './index.template.html'
})
],
}
你能告诉我我哪里错了吗?
postcss.config.js
中的导出需要是一个带有 plugins
属性 的对象,它包含一个插件数组(您需要导入),如 postcss-loader
- Usage.
而 postcss-mixins
接受一个带有 属性 mixin
的对象,它本身是一个带有 mixins 的对象,但是你只是直接给它 mixins,见 postcss-mixins
- Function Mixin(mixin 可以是函数或对象)。
因此您的 postcss.config.js
应该是:
module.exports = {
plugins: [
require('postcss-import'),
// require('precss'),
require('postcss-mixins')({
mixins: {
aloha: {
color: 'red'
}
}
}),
require('postcss-cssnext')({
browsers: ['last 2 versions', '> 5%'],
})
]
};