1

I am new to ReactJS. I am trying to use scss in react using sass-loaders

My webpack config looks like this-

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

var APP_DIR = path.resolve(__dirname + 'react-js/src/');
var BUILD_DIR = path.resolve(__dirname, 'react-js/dist/');

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

module.exports = {
    entry: './react-js/src/index.jsx',
    output: {
        path: BUILD_DIR,
        publicPath: '/dist',
        filename: 'bundle.js'
    },
    resolve: {
        extensions: ['.js', '.jsx']
    },
    module: {
        loaders: [
            { 
                test: /\.css$/,
                loader: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: 'css-loader',
                    publicPath: '/dist'
                })
            },
            { 
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: 'css-loader!sass-loader',
                    publicPath: '/dist'
                })
            },
            { 
                test: /\.(js|jsx)$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                  presets: ['react', 'es2015']
                }
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin('styles.css'),
    ]
}

All these configurations were from there respective documentations.

.

When I run webpack-dev-server it shows following output-

WDS starting

This is the screen when I save a scss file - Note - I have removed the ExtractTextPlugin from my webpack config file.

WDS on scss file save

Directory structure -

enter image description here

I just don't realise how it's not imported to the rendered react app. I thought importing the scss file in index.jsx like shown below will make the css file it's dependency, but it doesn't work.

import React from 'react';
import ReactDOM from 'react-dom';

import App from './app';

import style from '../../scss/main.scss';

ReactDOM.render(
    <App />, 
    document.getElementById('root')
);

Why?

Edited

Package.json

{
  "name": "skippo-vendor-admin-webui",
  "version": "1.0.0",
  "description": "",
  "main": "./react-js/src/index.jsx",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "./node_modules/.bin/webpack -d --progress --colors",
    "prod": "./node_modules/.bin/webpack -p --progress --colors",
    "watch": "./node_modules/.bin/webpack -d --progress --colors --watch",
    "start": "./node_modules/.bin/webpack-dev-server --progress --colors --hot --inline --contentBase './react-js/'"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "chai": "^3.5.0",
    "css-loader": "^0.28.1",
    "extract-text-webpack-plugin": "^2.1.0",
    "karma": "^1.6.0",
    "karma-chai": "^0.1.0",
    "karma-chrome-launcher": "^2.1.1",
    "karma-mocha": "^1.3.0",
    "karma-sinon": "^1.0.5",
    "karma-webpack": "^2.0.3",
    "mocha": "^3.3.0",
    "node-sass": "^4.5.2",
    "sass-loader": "^6.0.3",
    "sinon": "^2.2.0",
    "style-loader": "^0.17.0"
  },
  "dependencies": {
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-redux": "^5.0.4",
    "react-router": "^4.1.1"
  }
}
Ankush
  • 173
  • 2
  • 14

2 Answers2

2

The ExtractTextPlugin should only be used for your production build, since it will create a separate compiled css file that can be served without any special treatment.

For development, you can use this rule instead:

{
  test: /\.s?css$/,
  use: ['style-loader', 'css-loader', 'sass-loader'],
}

Install the 3 loaders as a dev dependency if you don't have them.

Also you don't need to specify a name when you import a scss file, just import it.

import  '../../scss/main.scss';
Preview
  • 35,317
  • 10
  • 92
  • 112
  • Thanks bro!! That works for webpack -d command. But it doesn't work for webpack-dev-server. Why is that? Are the config for WDSis wrong? I have updated the question with the package.json file. It includes the command for WDS. Thanks again – Ankush May 06 '17 at 17:18
  • Can't you just use webpack with your `watch` script? You might have to add the `hot` option in your webpack config though. – Preview May 06 '17 at 18:46
  • Yes I can do that. But point is that I am learning stuff now.. So I don't wanna skip over **webpack-dev-server** and **ExtractTextPlugin** only because I can't figure out how to set it up for my environment. @Apercu – Ankush May 06 '17 at 18:50
  • 1
    Yeah ok, what's the behavior? Any error or warning? Is the style processing logged in the console? Are you sure your config is picked up by wds? And I never said to skip the ExtractTextPlugin, just don't use it for dev. – Preview May 06 '17 at 19:01
  • Great!! See I have updated the question with 2 screenshots of WDS. I am confused that even when I have removed the ExtractTextPlugin it is still shown in WDS css build. – Ankush May 06 '17 at 19:41
1

Change this line

import style from '../../scss/main.scss';

With

import  '../../scss/main.scss';
Jorawar Singh
  • 7,463
  • 4
  • 26
  • 39