webpack 報錯 No PostCSS Config found 這個問題我在百度上找了好久,也沒有找到解決方案,最后沒有辦法只能自己去啃官方文檔,本案例在本人的webpack 學習感悟中已經寫過,但是發現很多人還是沒有找到答案,所以就將本問題整理為獨立版本。本着互聯網分享精神,現將本篇文件分享給大家。
本案例經過本人實測絕對好使。
文件地址 http://pan.baidu.com/s/1mhEUtk8
安裝:
npm install postcss-import autoprefixer cssnano style-loader postcss-loader --save-dev
webpackconfig.js配置
var htmlWebpackPlugin = require('html-webpack-plugin'); var path = require('path'); module.exports = { entry: './src/app.js', output: { path: path.resolve(__dirname, './dist/js'), filename: 'js/[name].bundle.js' }, module: { loaders: [ { test: /\.css$/, use: [ 'style-loader', { loader: 'css-loader', options: {importLoaders: 1} //這里可以簡單理解為,如果css文件中有import 進來的文件也進行處理 }, { loader: 'postcss-loader', options: { // 如果沒有options這個選項將會報錯 No PostCSS Config found plugins: (loader) => [ require('postcss-import')({root: loader.resourcePath}), require('autoprefixer')(), //CSS瀏覽器兼容 require('cssnano')() //壓縮css ] } } ] } ] }, plugins: [ new htmlWebpackPlugin({ filename: 'index.html', template: 'index.html', inject: 'body' //將js文件插入body文件內 }), ] };
入口文件app.js內容
import './css/common.css'; const App = function () { }; new App();
