PostCSS and Webpack configuration

I am really discouraged because I cannot find a helpful resource on this subject.

I just want to look at my .css files, use post css plugins to convert them, and finally export them to my / public folder as I already do with my .jsx files.

Here's my webpack config

    const path = require('path');
const webpack = require('webpack');

var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: path.resolve('src/index.jsx'),
    output: {
        path: path.resolve('public'),
        filename: 'bundle.js'
    },
    resolve: {
        extensions: ['*', '.js', '.jsx']
    },
    devServer: {
        publicPath: "/",
        contentBase: "./public"
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                exclude: /node_modules/,
                loader: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: [
                        {
                            loader: 'css-loader',
                            options: {
                                modules: true,
                                localIdentName: '[name]__[local]___[hash:base64:5]'
                            }
                        }, {
                            loader: 'postcss-loader',
                            options: {
                                plugins: function() {
                                    return [require('lost'), require('postcss-cssnext'), require('postcss-import')]
                                }
                            }
                        }
                    ]
                })
            }, {
                test: /\.jsx?$/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: ['es2015', 'react']
                        }
                    }
                ],
                exclude: /(node_modules|bower_components)/
            }
        ]
    },
    plugins: [new ExtractTextPlugin("main.css")]
}

      

+3


source to share


2 answers


I am assuming you are using webpack2

If you want your css file to be dumped separately, you will need ExtractTextPlugin. Here is my css loader that works



I define post css plugins right in the webpack config because then it stays in one place. Hope this helps:

var ExtractTextPlugin = require('extract-text-webpack-plugin');
...
...
module.exports = {
...
...
    module: {
        rules: [
...
...
            {
                test: /\.css$/,
                exclude: /node_modules/,
                loader: ExtractTextPlugin.extract(
                    { fallback: 'style-loader',
                    use: [{
                        loader: 'css-loader',
                        options: {
                            modules: true,
                            localIdentName:'[name]__[local]___[hash:base64:5]'
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            plugins: function() {
                                return [
                                    require('autoprefixer')
                                ]
                            }
                        }
                    },
                ]
            })
        },
}

      

+4


source


Your plugins may not have been created correctly.

Try

return [require('lost')(), require('postcss-cssnext')(), require('postcss-import')()]

      



(Note the () to trigger plugin creation).

Also do you use import / require () to include your css? If you shouldn't, no magic will glob your css :)

0


source







All Articles