sass-loader with Webpack doesn't generate CSS

550 views Asked by At

How to use sass-loader to generate a css file from scss and embed it in the html using express.js. I am also using react-hot-loader

Below is my config file

var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var HTMLWebpackPlugin = require('html-webpack-plugin')
var HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
            template: __dirname + '/app/index.html',
            filename: 'index.html',
            inject: 'body'
        });


const path = require('path');

module.exports = {

    entry: ['react-hot-loader/patch',
            'webpack-hot-middleware/client?path=http://localhost:3000/__what',
            'webpack/hot/only-dev-server', //<- doesn’t reload the browser upon syntax errors. This is recommended for React apps because it keeps the state
            //'webpack/hot/dev-server', //<- To perform HMR in the browser reloads if HMR update fails.
            __dirname + '/app/index.js'],

    context: __dirname,
    module:{
        loaders:[
            {
                test:/\.js$/,
                exclude: /node_modules/,
                loader:'babel-loader',
                query:{
                    presets:["react","es2015","stage-2"],
                    plugins: ["react-hot-loader/babel"]
                }
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                loader: ExtractTextPlugin.extract({fallback:'style-loader' ,use:['css-loader','sass-loader']}),
                include:path.resolve(__dirname, '/app/scss') 
            }
        ]
    },
    output:{
        filename:'payload-min.js',
        path: __dirname + '/build',
        publicPath: __dirname + '/build'
    },
    plugins:[HTMLWebpackPluginConfig ,
             new ExtractTextPlugin(__dirname + '/build/payload.css', {
                                  allChunks: true
             }),
             new webpack.HotModuleReplacementPlugin(),
             new webpack.NoEmitOnErrorsPlugin()]

};

index.js

...
require('css-loader!sass-loader!./scss/payload.scss');

I never see the below plugin in action

new ExtractTextPlugin(__dirname + '/build/payload.css', {
                                      allChunks: true
                 }),

execute

webpack -d or webpack doesn't generate the payload.css file under /build

index.html

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

My Directory structure

.
├── app
│   ├── actions
│   │   └── index.js
│   ├── common
│   │   └── constants.js
│   ├── components
│   │   ├── App.js
│   │   ├── DetailsSection.js
│   │   ├── Device.js
│   │   ├── FirmwareImageDetails.js
│   │   ├── Menu.js
│   │   ├── Terminal.js
│   │   └── ViewJson.js
│   ├── index2.html
│   ├── index.html
│   ├── index.js
│   ├── reducers
│   │   ├── index.js
│   │   └── localStorage.js
│   └── scss
│       └── payload.scss
├── build
├── mock_payload.json
├── package.json
├── package-lock.json
├── payload_schema.json
├── README.md
├── server.js
├── webpack.config.js
└── webpack.server.config.js
1

There are 1 answers

0
Srikan On

The problem is with

include:path.resolve(__dirname, '/app/scss') 

in the loader removing this line generated the css file.