Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React storybook sass-loader import node_modules

I'm using sass-loader to load my scss files inside my project and also using the node_modules resolver eg: @import '~styleguide/src/vars.scss'

When i'm trying to load react-storybook i'm passing this to webpack.config.js

const path = require('path');
const autoprefixer = require('autoprefixer');

// load the default config generator.
var genDefaultConfig = require('@kadira/storybook/dist/server/config/defaults/webpack.config.js');

module.exports = function(config, env) {
  var config = genDefaultConfig(config, env);

  config.module.loaders.push({
    test: /\.html$/, loader: 'raw'
  });
  config.module.loaders.push({
    test: /\.scss$/,
    loaders: ['style', 'css?sourceMap', 'postcss-loader', 'sass?config=sassLoader']
  });

  config.sassLoader = {
    includePaths: [
      path.resolve(__dirname, '..', 'src/scss')
    ],
    sourceMap: true
  }

  config.postcss = function() {
    return [autoprefixer];
  }

 return config;
};

From what i can read from sass-loader the node_module resolve should be automatic but it appears is not working:

here's the error message

ERROR in ./~/css-loader?sourceMap!./~/postcss-loader!./~/sass-loader?config=sassLoader!./src/component.scss
Module build failed:
@import '~styleguide/src/vars';
^
      File to import not found or unreadable: ~styleguide/src/vars
Parent style sheet: stdin

Anyone has an idea why this is happening?

like image 612
Fabrizio Fortunato Fortune Avatar asked Nov 08 '22 07:11

Fabrizio Fortunato Fortune


1 Answers

This is working for us:

    /* eslint-env node */
    const path = require('path');

    const include = path.resolve(__dirname, '..', 'src');

    module.exports = {
        module: {
            rules: [
                {
                    test: /\.scss$/,
                    loaders: ["style-loader", "css-loader", "sass-loader"],
                    include
                }
            ]
        }
    };
like image 187
malix Avatar answered Nov 25 '22 02:11

malix