shorif2000
shorif2000

Reputation: 2654

react module parse failed on scss

I am trying to implement this to see how it works but i cannot get it to build

https://ashiknesin.com/blog/build-custom-sidebar-component-react/

In my webpack.config.js I have the following

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

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

/*const extractSass = new ExtractTextPlugin({
    filename: "[name].[contenthash].css",
    disable: process.env.NODE_ENV === "development"
});
*/
module.exports = {
    entry : __dirname + '/app/index.js',
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
            }
        ],
        rules: [
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                  fallback: "style-loader",
                  use: "css-loader"
                })
            }
        ]
    },
    output: {
        filename: 'transformed.js',
        path: __dirname + '/dist'
    },
    plugins: [
        HTMLWebpackPluginConfig,
        new ExtractTextPlugin("styles.css")
    ]
}

When I try to build it errors with

ERROR in ./app/index.js
Module parse failed: Unexpected token (20:12)
You may need an appropriate loader to handle this file type.
|     render() {
|         return (
|             <div>
|                 <SideBar />
|                 <Header />
Child html-webpack-plugin for "index.html":
     1 asset
       [0] ./node_modules/html-webpack-plugin/lib/loader.js!./app/index.html 6.41 kB {0} [built]
       [2] (webpack)/buildin/global.js 509 bytes {0} [built]
       [3] (webpack)/buildin/module.js 517 bytes {0} [built]
        + 1 hidden module
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /opt/SP/wwwadm/home/.npm/_logs/2017-12-29T11_17_11_139Z-debug.log

I have tried looking at the following posts but they don't seem to work. https://github.com/webpack-contrib/extract-text-webpack-plugin

https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/580

here is my webpack file

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

module.exports = {
    entry : __dirname + '/app/index.js',
    module: {
         rules: [{
            test: /\.scss$/,
            use: [{
                loader: "style-loader"
            }, {
                loader: "css-loader"
            }, {
                loader: "sass-loader",
                options: {
                    includePaths: ["absolute/path/a", "absolute/path/b"]
                }
            }]
        }],
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
            }
        ]
    },
    output: {
        filename: 'transformed.js',
        path: __dirname + '/dist'
    },
    plugins: [
        HTMLWebpackPluginConfig
    ]
}

here is my package file

{
  "name": "ccp-react",
  "version": "1.0.0",
  "license": "MIT",
  "scripts": {
    "start": "webpack-dev-server",
    "build": "webpack"
  },
  "private": true,
  "dependencies": {
    "babel-plugin-lodash": "^3.3.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-1": "^6.24.1",
    "bootstrap": "^3.3.7",
    "classnames": "^2.2.5",
    "core-js": "^2.5.3",
    "create-react-class": "^15.6.2",
    "cross-env": "^5.1.3",
    "gulp": "^3.9.1",
    "hammerjs": "^2.0.8",
    "lodash": "^4.17.4",
    "ng": "0.0.0-rc6",
    "ng-cli": "^0.7.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "react-sidenav": "^2.1.2",
    "rxjs": "^5.5.6",
    "semantic": "0.0.1",
    "semantic-ui": "^2.2.13",
    "semantic-ui-react": "^0.77.1",
    "systemjs": "^0.20.19",
    "web-animations-js": "^2.3.1",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "^6.0.95",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-react": "^6.24.1",
    "css-loader": "^0.28.7",
    "extract-text-webpack-plugin": "^3.0.2",
    "html-webpack-plugin": "^2.30.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.3.3",
    "karma-jasmine": "^1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "node-sass": "^4.7.2",
    "protractor": "~5.1.2",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.19.1",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.4.2",
    "webpack": "^3.10.0",
    "webpack-bundle-analyzer": "^2.8.2",
    "webpack-dev-server": "^2.9.7"
  }
}

Here is my index.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

import Sidebar from './components/sidebar';
import Header from './components/header';
import HomeContent from './components/home';

class App extends Component {
    constructor(props){
        super(props);

        this.state = { nav: '/' };

    }

    render() {
        return (
            <div>
                <Header />
                <Sidebar />
                <HomeContent />
            </div>
        );
    }
};

ReactDOM.render(<App/>,  document.getElementById("app"));

my folder structure is

app/index.js
app/index.html
app/components/sidebar.js
app/components/home.js
app/components/header.js
dist/
docs/
package.json
webpack.config.js
.babelrc
node_modules

sidebar.js contains https://ashiknesin.com/blog/build-custom-sidebar-component-react/ , If I comment out the import css and remove the css loaders from webpack it builds fine.

Upvotes: 3

Views: 3260

Answers (1)

margaretkru
margaretkru

Reputation: 2781

If you are using Sass in your project, you need to tell webpack how to handle scss files, so you need to install sass-loader and include it in your webpack configuration. The very minimum is this:

module: {
    rules: [{
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
            use: [{
                loader: "css-loader"
            }, {
                loader: "sass-loader"
            }],
            fallback: "style-loader"
        })
    }]
},

Installation guide and other examples of webpack configurations with sass-loader are provided on sass-loader github homepage. In order to install sass-loader, run npm install sass-loader node-sass --save-dev, more info here.

Here is the edited version of your webpack config (removed loaders since they are not supported in webpack 3 and replaced with a rule for for handling js files with babel-loader):

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

module.exports = {
    entry : __dirname + '/app/index.js',
    module: {
         rules: [{ 
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader'
            } 
        },
        {
            test: /\.scss$/,
            use: [{
                loader: "style-loader"
            }, {
                loader: "css-loader"
            }, {
                loader: "sass-loader",
                options: {
                    includePaths: ["absolute/path/a", "absolute/path/b"]
                }
            }]
        }]
    },
    output: {
        filename: 'transformed.js',
        path: __dirname + '/dist'
    },
    plugins: [
        HTMLWebpackPluginConfig
    ]
}

Upvotes: 1

Related Questions