Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using webpack to prepend variables for SCSS

Webpack amateur here... I'm trying to incorporate a theme.scss file to customize the theme used by React Toolbox by following the directions specified here, namely:

If you are using Webpack as module bundler, you are probably using sass-loader as well. What we want to do is to prepend to each SASS file compilation a bunch of variables to override and this can be done with the data option. For example:

sassLoader: { data: '@import "' + path.resolve(__dirname, 'theme/_theme.scss') + '";' }

In this case we have are prepending the theme import to each SASS compilation so the primary color will be changed in every single stylesheet.

I'm having trouble implementing this instruction with my current webpack configuration, which looks like this:

const webpack = require('webpack');
const path = require('path');
let ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    context: path.join(__dirname, 'client'),
    entry: [
        './main.js',
    ],
    output: {
        path: path.join(__dirname, 'www'),
        filename: 'bundle.js',
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: [
                    'babel-loader',
                ],
            },
            {
                test: /\.css$/,
                use: [
                    "style-loader",
                    {
                        loader: "css-loader",
                        options: {
                            modules: true,
                            sourceMap: true,
                            importLoaders: 1,
                            localIdentName: "[name]--[local]--[hash:base64:8]"
                        }
                    },
                    "postcss-loader" // has separate config, see postcss.config.js nearby
                ]
            },
            {
                test: /\.scss$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: [
                        {
                            loader: 'css-loader', options: {
                                sourceMap: true,
                                data: '@import "' + path.resolve(__dirname, 'theme.scss') + '";'
                            }
                        },
                        'postcss-loader',
                        {
                            loader: 'sass-loader', options: {
                                sourceMap: true,
                                data: '@import "' + path.resolve(__dirname, 'theme.scss') + '";'
                            }
                        },
                    ],
                })
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin({
            filename: 'style.css',
            allChunks: true
        }),
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
        }),
    ],
    resolve: {
        modules: [
            path.join(__dirname, 'node_modules'),
        ],
    },
};

I don't get an error, but it seems like the data option is being entirely ignored because my file does not get imported.

Here is my theme.scss file (located in client/theme.scss):

@import "~react-toolbox/lib/colors";

$color-primary: $palette-red-500;
$color-primary-dark: $palette-red-700;

body {
  background-color: black; //testing
}

I feel like I must be doing something stupid here, but I'm driving myself crazy. I have tried messing with the path of the theme.scss file (changing the data attribute to data: '@import "' + path.resolve(__dirname, 'client/theme.scss') + '";') but that doesn't make a difference. I'm surprised I'm not getting an error of some kind.

Any suggestions?

like image 900
user1498814 Avatar asked Jun 03 '17 16:06

user1498814


1 Answers

The below configuration worked for me

  {
    test: /\.scss$/,
    include: /client/,
    use: ExtractTextPlugin.extract({
      fallback: 'style-loader',
      loader: [
        {
          loader: 'css-loader',
          query: {
            modules: true,
            sourceMap: false,
            localIdentName: '[name]_[local]_[hash:base64:5]',
          },
        },
        'postcss-loader',
        {
          loader: 'sass-loader',
          query: {
            sourceMap: false,
            data: `@import "${path.resolve(__dirname, 'client/_theme.scss')}";`
          }
        }
      ],
    }),
  },

and client/_theme.scss file

@import "react-toolbox/lib/colors.css";

$color-primary: var(--palette-blue-500);
$color-primary-dark: var(--palette-blue-700);

I checked the colors.css file in the react-toolbox library and used the same variable names. i.e --palette-blue-500, not $palette-blue-500.

like image 90
Venugopal Avatar answered Nov 15 '22 10:11

Venugopal