question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SCSS webpack loader example

See original GitHub issue

React-styleguidist has very specific opinions about where component source, style, and documentation should go. This is very useful (and preferable), but makes it a little bit difficult to figure out how to add things like an SCSS loader. An example of this would be a big help, as I can’t get SCSS working with the docs as is.

Here is my styleguide.config.js webpack config:

  components: './lib/components/**/*.jsx',
  updateWebpackConfig: function(webpackConfig, env) {
    // Your source files folder or array of folders, should not include node_modules
    let dir = path.join(__dirname, 'lib');
    webpackConfig.module.loaders.push(
      // Babel loader will use your project’s .babelrc
      {
        test: /\.jsx?$/,
        include: dir,
        exclude: 'node_modules/',
        loader: 'babel-loader'
      },
      // Other loaders that is needed for your components
      {
        test: /\.css$/,
        include: dir,
        loader: 'style!css?modules&importLoaders=1'
      },
      {
        test: /\.scss$/,
        include: dir,
        loader: 'sass-loader'
      }
    );
    return webpackConfig;
  }

SCSS files are named like the other files in existing examples: Component.jsx, Component.scss, Readme.md.

Package.json dependencies:

"devDependencies": {
    "babel-plugin-react-transform": "^2.0.2",
    "babel-preset-react-hmre": "^1.1.1",
    "node-sass": "^3.4.2",
    "react-styleguidist": "^2.1.0",
    "react-transform-hmr": "^1.0.4",
    "sass-loader": "^3.2.0"
  },
  "dependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.7.2",
    "babel-loader": "^6.2.4",
    "babel-plugin-react-transform": "^2.0.2",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "babel-preset-react-hmre": "^1.1.1",
    "babel-preset-stage-0": "^6.5.0",
    "core-decorators": "^0.11.0",
    "react": "^0.14.7",
    "react-dom": "^0.14.7",
    "react-router": "^2.0.1"
  }

I tried a number of different things in the loader string:

  • loader: 'style-loader!style!css!sass'
  • loader: 'style!css!sass'
  • loader: 'style-loader!style!css!sass'

None of these result in any output from styleguidist at the terminal, even with a Component.scss like body: { background-color: #00F !important; }. The components just load with no styling beyond the default.

Related issues:

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mik01ajcommented, Apr 21, 2016

I do it too. I’ll just paste the relevant part here, maybe it will be helpful for someone:

updateWebpackConfig: function (webpackConfig) {

    webpackConfig.entry.push('./app/scripts/globalConfig.js');

    webpackConfig.entry.push('./app/styles/bootstrap.scss');
    webpackConfig.entry.push('./app/styles/application.scss');
    webpackConfig.entry.push('./app/styles/styleguide-overrides.scss');

    webpackConfig.module.loaders = webpackConfig.module.loaders.concat([
        {
            test: /\.scss$/,
            exclude: /node_modules/,
            loader: 'style!css!postcss!sass',
        },
        {
            test: /\.(png|jpg|eot|woff|woff2|ttf|svg)$/,
            include: /.*/, // Because styleguidist requires either include or exclude.
            loader: 'url?limit=8192',
        },
        {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel',
            // We omit the ignores because of https://github.com/webpack/webpack/issues/1759
            query: _.omit(require('./babel.config'), 'ignore'),
        },
    ]);

    webpackConfig.postcss = [
        autoprefixer({browsers: ['last 2 versions', '> 0.5%']}),
    ];

    webpackConfig.output.pathinfo = true;
    webpackConfig.resolve.extensions = ['', '.js', '.jsx'];

    return webpackConfig;
},

Maybe it’s not the cleanest possible config, but it works for me 😃

1reaction
drewdecarmecommented, Jul 13, 2017

I was having the same issues as well with compiling imported .scss files into each component using a derivation of CSS Modules called Babel Plugin React CSS Modules. I read @AoDev suggestion and realized that I didn’t have an exclude in my Style Loader for Webpack 2. Here was my solution

{
        test: /\.(css|scss)$/,
        exclude: /node_modules/,  // originally didn't have it and added
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoader: 1,
              modules: true,
              localIdentName: '[local]___[hash:base64:5]'
            }
          },
          'sass-loader',
          'postcss-loader'
        ]
      },
Read more comments on GitHub >

github_iconTop Results From Across the Web

sass-loader - webpack - JS.ORG
Webpack provides an advanced mechanism to resolve files. The sass-loader uses Sass's custom importer feature to pass all queries to the Webpack resolving...
Read more >
How to configure SCSS modules for Webpack
module.scss or .module.sass file extensions. First, SCSS is converted to CSS ( sass-loader ), then run through ...
Read more >
Setting up CSS and Sass with Webpack!! - DEV Community ‍ ‍
node-sass provides binding Node.js to LibSass (The C version of Sass). sass-loader document says "The sass-loader requires you to install either ...
Read more >
How to SASS with Webpack 5 - Setup Tutorial - Robin Wieruch
This tutorial is part 3 of 4 in 'Webpack with Style'-series. ... And second, you can use the SASS loader for all CSS...
Read more >
Sass with Webpack - LearnHowToProgram.com
The first loader, style-loader , turns all our Sass into one big string. The second, css-loader , puts this big string into our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found