5

Using React with webpack. Ran through some articles, but most of them suggest calling individual scss file for each component. But I would like to precompile all css into single file for entire application like we do using grunt/gulp.

enter image description here

2 Answers 2

4

You can use the webpack-text-extract-pluggin that is in charge of compiling all css files and bundling them in an index.css file.

Also note that you'll need to install sass-loader too in order to compile the scss.

In the webpack config:

var ExtractTextPlugin = require('extract-text-webpack-plugin');
config = {
    ..., 
    plugins: [
        ...,
        new ExtractTextPlugin('index.css')
    ],
    module: {
        loaders: [
            ...
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract('style','css')
            },
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract('style', 'css!sass')
            }
        ]
    }
}

In index.html:

<link rel="stylesheet" type="text/css" href="/index.css">

In any Javascript file that gets through webpack:

require("./styles/my-custom-file.scss");
Sign up to request clarification or add additional context in comments.

Comments

3

You could take a look at the extract-text-webpack-plugin.

After requiring this in your webpack.config.js:

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

You can rewrite your sass loader to this:

module: {
    loaders: [
        {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
        {test: /\.scss$/, loader: ExtractTextPlugin.extract('style', 'css', 'sass')}
    ]
},
plugins: [
    new ExtractTextPlugin('bundle.css')
]

For more options and usage check the link above.

1 Comment

Thanks @erik-Martijn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.