webpack 報錯 No PostCSS Config found 解決方案。


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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM