5

Following is my folder structure & Files for React project which is working fine but I am unable to add CSS through SCSS via Webpack using extract-text-webpack-plugin. Let me know what I am doing wrong with the configuration.

Folder Structure -

Folder Structure

Webpack.config.js File -

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: './app/index.html',
    filename: 'index.html',
    inject: 'body'
});
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ExtractTextPluginConfig = new ExtractTextPlugin('main.css',{
    allChunks: true
});

module.exports = {
    entry: './app/app.jsx',
    output: {
        path: path.resolve('dist'),
        filename: 'bundle.js'
    },
    devtool: 'source-map',
    module: {
        loaders: [
            {test: /.js$/, loader: 'babel-loader', exclude: /node_modules/},
            {test: /.jsx$/, loader: 'babel-loader', exclude: /node_modules/},
            {test: /\.scss$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader")}
        ]
    },
    plugins: [HtmlWebpackPluginConfig, ExtractTextPluginConfig]
};

Package.json -

{
  "name": "reactyarn",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "webpack-dev-server --hot"
  },
  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "css-loader": "^0.28.4",
    "extract-text-webpack-plugin": "^3.0.0",
    "html-webpack-plugin": "^2.29.0",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.18.2",
    "webpack": "^3.3.0",
    "webpack-dev-server": "^2.5.1"
  },
  "dependencies": {
    "path": "^0.12.7",
    "react": "^15.6.1",
    "react-dom": "^15.6.1"
  }
}

FYI -

I am not getting any JS error in console, so I believe its only the configuration which is not working.

Console

1 Answer 1

3

You appear to be missing one of the loaders (sass-loader) and setting them up in your modules incorrectly.

Try the example below:

module.exports = {
    entry: './app/app.jsx',
    output: {
        path: path.resolve('dist'),
        filename: 'bundle.js'
    },
    devtool: 'source-map',
    module: {
        loaders: [
            {test: /.js$/, loader: 'babel-loader', exclude: /node_modules/},
            {test: /.jsx$/, loader: 'babel-loader', exclude: /node_modules/},
            {test: /\.scss$/, loaders: ['style-loader', 'css-loader', 'sass-loader'] // <-- this is new
        ]
    },
    sassLoader: {
      includePaths: [path.resolve(__dirname, 'relative/path/to/scss')]
    }, // <--- this is new
    plugins: [HtmlWebpackPluginConfig, ExtractTextPluginConfig]
};

By using ExtractPlugin.extract you're referencing the means to do this in Webpack 2 (using rules and use) but your Webpack config file appears to be geared toward Webpack 1.

Sign up to request clarification or add additional context in comments.

2 Comments

I made the changes as per your comment but now I am facing error in cmd prompt ..File - pastebin.com/5gW7eRXU Error - i.imgur.com/4jTZITW.png
Check documentation here: github.com/webpack-contrib/sass-loader/tree/archive/webpack-1 - Also, ensure you've installed node-sass via npm i -D node-sass

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.