错误在./~/css-loader!./~/postcss-loader/lib!./~/rc-range-slider/lib/Slider/slider.css

时间:2017-05-30 14:58:31

标签: webpack postcss

我将rc-range-slider包添加到我的项目中,当我尝试使用Webpack ^ 2.5.1编译我的react项目时,开始获取下一段中列出的错误消息。此滑块的NPM站点中建议的webpack配置与以前版本的webpack兼容。下面最后一段中的Webpack配置是我尝试将其纳入新的Webpack配置约束。但我收到以下错误消息:

请参阅Github

上的演示
ERROR in ./~/css-loader!./~/postcss-loader/lib!./~/rc-range-slider/lib/Slider/slider.css
Module build failed: Error: No PostCSS Config found in: \js\node_modules\rc-range-slider\lib\Slider
    at Error (native)
    at \js\node_modules\postcss-load-config\index.js:51:26
 @ ./~/style-loader!./~/css-loader!./~/postcss-loader/lib!./~/rc-range-slider/lib/Slider/slider.css 4:14-105 18:2-22:4 19:20-111
 @ ./~/rc-range-slider/lib/Slider/slider.css
 @ ./~/rc-range-slider/lib/Slider/index.js
 @ ./~/rc-range-slider/lib/index.js
 @ ./src/search-component.jsx
 @ ./src/search-component.jsx
 @ ./src/search-container.js
 @ ./src/routes.js
 @ ./src/index.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server babel-polyfill webpack-dev-server/client?http://localhost:8080 webpack/hot/only-dev-server ./src/index.js

ERROR in ./~/css-loader!./~/postcss-loader/lib!./~/rc-tipso/lib/Tipso/BaseTipso/base_tipso.css
Module build failed: Error: No PostCSS Config found in: \js\node_modules\rc-tipso\lib\Tipso\BaseTipso
    at Error (native)
    at \js\node_modules\postcss-load-config\index.js:51:26
 @ ./~/style-loader!./~/css-loader!./~/postcss-loader/lib!./~/rc-tipso/lib/Tipso/BaseTipso/base_tipso.css 4:14-115 18:2-22:4 19:20-121
 @ ./~/rc-tipso/lib/Tipso/BaseTipso/base_tipso.css
 @ ./~/rc-tipso/lib/Tipso/BaseTipso/index.js
 @ ./~/rc-tipso/lib/Tipso/index.js
 @ ./~/rc-tipso/lib/index.js
 @ ./~/rc-range-slider/lib/Slider/Dragger.js
 @ ./~/rc-range-slider/lib/Slider/index.js
 @ ./~/rc-range-slider/lib/index.js
 @ ./src/search-component.jsx
 @ ./src/search-component.jsx
 @ ./src/search-container.js
 @ ./src/routes.js
 @ ./src/index.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server babel-polyfill webpack-dev-server/client?http://localhost:8080 webpack/hot/only-dev-server ./src/index.js
webpack: Failed to compile.

以下是我的webpack配置:

var webpack = require( 'webpack' ),
    merge = require( 'lodash/merge' ),
    ExtractTextPlugin = require( 'extract-text-webpack-plugin' ),
    hotModuleReplacement = new webpack.HotModuleReplacementPlugin(),
    constants = {
            process:{
                env: {
                    NODE_ENV: JSON.stringify( 'development' )
                }
            }
        },
    extractSass = new ExtractTextPlugin({
            allChunks: true,
            disable: constants.process.env.NODE_ENV !== 'development',
            filename: 'public/main.css'
        }),
    getStylesheets = function( ...names ){
            const options = {
                    data: '$env: ' + constants.process.env.NODE_ENV + ';',
                    includePaths: [ 'res/main', 'node_modules' ],
                    sourceMap: true
                  },
                  specifics = {
                    css: {
                        modules: true,
                        importLoaders: 1,
                        localIdentName: '[name]__[local]___[hash:base64:5]',
                    },
                    postcss: {
                        postcss: () => [ 
                            require( 'postcss-import' )({ 
                                    addDependencyTo: webpack 
                                }),
                            require( 'postcss-url' )(),
                            require( 'postcss-cssnext' )(),
                                  // add your <plugins> here
                                  // ...
                                  // and if you want to compress,
                                  // just use css-loader option that already use cssnano under the hood
                            require( 'postcss-browser-reporter' )(),
                            require( 'postcss-reporter' )()
                        ]
                    },
                  };
            return names.map( 
                    constants.process.env.NODE_ENV ?
                    n => ({ loader: n + '-loader' }) : 
                    n => ({ loader: n + '-loader', options: merge( {}, options, ( specifics[ n ] | {} ))}));
        },
    getJSX = ( ...names ) => 
        names.map( n => ({
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: n + '-loader',
            query: {
                plugins: [ 'transform-decorators-legacy', 'transform-runtime' ],
                presets: [ 'latest', 'react', 'airbnb' ]
            }
        }));

module.exports = {
    entry: [
        'babel-polyfill',
        'webpack-dev-server/client?http://localhost:8080',
        'webpack/hot/only-dev-server',
        './src/index.js'
    ],
    devtool: 'source-map',
    module: {
        rules:[ 
            ...getJSX( 'react-hot', 'babel' ),
            {
                test: /\.css$/,
                use: extractSass.extract({ 
                    use: getStylesheets( 'style', 'css', 'postcss' ),
                    fallbackLoader: 'style-loader'
                })
            }
        ],
    },
    resolve: {
        extensions: ['.jsx', '.js', '.json', 'css']
    },
    output: {
        path: __dirname,
        publicPath: '/',
        filename: 'main.js'
    },
    devServer: {
        historyApiFallback: true,
        contentBase: './webpack_html',
        hot: true
    },
    plugins: [ hotModuleReplacement, extractSass, new webpack.DefinePlugin( constants )]
};

1 个答案:

答案 0 :(得分:0)

所有魔法都在 getStylesheets 中。 这里

 return names.map( 
                    constants.process.env.NODE_ENV ?
                    n => ({ loader: n + '-loader' }) : 
                    n => ({ loader: n + '-loader', options: merge( {}, options, ( specifics[ n ] | {} ))}));

你有

constants = {
            process:{
                env: {
                    NODE_ENV: JSON.stringify( 'development' )
                }
            }
        }

constants.process.env.NODE_ENV 将始终为 true ,因为只有空字符串才会返回false。因此,您将始终映射

n => ({ loader: n + '-loader' })

你错过了一行

  

(具体[n] | | {})

工作示例。

return names.map(
        constants.process.env.NODE_ENV !== '"development"' ?
                n => ({ loader: n + '-loader' }) :
    n => ({ loader: n + '-loader', options: merge( {}, options, ( specifics[ n ] || {} ))}));

此外,演示

中没有 postcss-browser-reporter