在自学webpack的时候,配置postcss后,运行webpack一直报错。
下面是我的配置文件
var precss = require('precss');
var autoprefixer = require('autoprefixer');
module.exports = {
devtool: 'eval-source-map',
entry: __dirname + '/app/main.js', //唯一入口文件
output: {
path: __dirname + '/public', //打包目录
filename: 'bundle.js' //打包后的文件名
},
module: {
loaders: [{
test: /\.json$/,
loader: "json"
}, {
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel', //在webpack的module部分的loaders里进行配置即可
query: {
presets: ['es2015', 'react']
}
}, {
test: /\.css$/,
loader: 'style-loader!css-loader!postcss-loader' //添加对样式表的处理
}]
},
postcss: function () {
return [precss, autoprefixer];
},
devServer: {
contentBase: "./public", //本地服务器所加载的页面所在的目录
colors: true, //终端中输出结果为彩色
historyApiFallback: true, //不跳转
inline: true //实时刷新
}
}
下面是报错信息
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration has an unknown property 'postcss'. These properties are valid:
object { amd?, bail?, cache?, context?, dependencies?, devServer?, devtool?, entry, externals?, loader?, module?, name?, node?, output?, plugins?, profile?, recordsInputPath?, recordsOutputPath?, recordsPath?, resolve?, resolveLoader?, stats?, target?, watch?, watchOptions? }
For typos: please correct them.
For loader options: webpack 2 no longer allows custom properties in configuration.
Loaders should be updated to allow passing options via loader options in module.rules.
Until loaders are updated one can use the LoaderOptionsPlugin to pass these options to the loader:
plugins: {
new webpack.LoaderOptionsPlugin({
// test: /\.xxx$/, // may apply this only for some modules
options: {
postcss: ...
}
})
}
我去github上查了postcss的配置,没看出来这是什么问题。
求大神帮忙看看
原因:Webpack 2.1.0-beta23 之后的config里不能直接包含自定义配置项
解决:将
postcss
和devServer
替换成以下写法即可参考:https://github.com/webpack/webpack/pull/2974#issuecomment-245857168