Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack throwing error on SCSS @import SCSS

I'm trying to process multiple SCSS files into a single external CSS file in Webpack (3.6.0) except I'm encountering issues around the parsing of the @import statements.

Entry index.js contains:

import './styles/main.scss';

Entry SCSS:

@import 'reset.scss';
@import 'global.scss';
@import 'fonts.scss';
@import 'system.scss';

Current webpack:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
    entry: {
        app: './src/index.js'
    },
    context: path.resolve(__dirname),

    output: {
        path: path.resolve(__dirname, 'public'),
        filename: 'bundle.js',
    },

    plugins: [
        new CleanWebpackPlugin(['app']),
        new HtmlWebpackPlugin({
            title: 'Minimum-Viable',
            filename: 'index.html',
            template: './public/index.html',
        }),
    ],

    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            'es2015',
                            'react',
                        ],
                        plugins: ['transform-class-properties'],
                    },
                },
            },
            {
                test: /\.scss$/,
                include: [
                    path.resolve(__dirname, 'styles')
                ],
                use: [
                    {loader:'style-loader'},
                    {loader:'css-loader'},
                    {loader:'sass-loader'}
                ]
            },
        ],
    },
    resolve: {
        extensions: ['.js','.scss']
    }
};

The Error being thrown is:

ERROR in ./src/styles/main.scss Module parse failed: Unexpected character '@' (1:0)

You may need an appropriate loader to handle this file type. | @import 'reset.scss';

Any pointers gratefully received.

like image 624
Rich Avatar asked Nov 14 '17 17:11

Rich


2 Answers

I managed to reproduce the error and it seems to come from:

include: [
    path.resolve(__dirname, 'styles')
],

The path is the issue cause the loader searches for /styles in ./styles but looking at your entry point:

entry: {
    app: './src/index.js'
},

It should actually be ./src/styles so:

include: [
    path.resolve(__dirname, 'src', 'styles')
],
like image 177
U Rogel Avatar answered Nov 15 '22 05:11

U Rogel


with:

include: [
    path.resolve(__dirname, 'styles')
],

you are instructing webpack to use your loader chain only on files residing in your styles folder.

reset.scss looks like a node dependency usually stored in your node_module, which is excluded from SASS processing by the include option.

Try to remove your SASS include option or to extend it in order to include node_folder or the specific module imported by your styles.

like image 28
Andrea Carraro Avatar answered Nov 15 '22 05:11

Andrea Carraro