9
votes

CLI Tool: Storybook Framework: Vue/ Nuxt

Issue: I'm trying to pull in global SCSS variables to Storybook Stories so they run the components the same way as they do in Nuxt, I've tried the custom webpack config with sass-resources-loader but had no luck, just wanted to check if anyone else has already solved this problem

5
Did you ever figure this out? I have the same problem. None of the answers on this thread are applicable at all, because I can get Storybook to read SCSS - that's not the problem. Storybook doesn't know how to read my global SCSS file that contains all my variables and mixins. You can't import them into main.js or preview.js either. - keyboard-warrior
@keyboard-warrior I have EXACTLY the same problem. - Nadir Abbas

5 Answers

2
votes

It seems to be an issue with Storybook handling multiple rules.

I solved it by a work around.

You can read the blog i wrote for detailed explaination here.

Below is my webpack config - main.js :

webpackFinal: async (config, { configType }) => {
    config.module.rules.map((rule) => {
      if (rule.oneOf) {
        rule.oneOf = rule.oneOf.slice().map((subRule) => {
          if (subRule.test instanceof RegExp && subRule.test.test('.scss')) {
            return {
              ...subRule,
              use: [
                ...subRule.use,
                {
                  loader: require.resolve('sass-resources-loader'),
                  options: {
                    resources: [
                      path.resolve(__dirname, '../src/styles/_common.scss')
                    ]
                  }
                }
              ],
            }
          }
          return subRule;
        });
      }
      return rule;
    });
    return config;
  },

Hope this helps someone!

1
votes

I encountered the issue where global SASS variables were causing Storybook for Vue to fail.

For me, creating a webpack.config.js file in the .storybook folder with the below configuration solved my problem:

module.exports = (storybookBaseConfig, configType, defaultConfig) => {

  defaultConfig.module.rules.push(
    {
      resourceQuery: /module/,
      use: [
        {
          loader: 'vue-style-loader',
          options: {
            sourceMap: false,
            shadowMode: false
          }
        },
        {
          loader: 'css-loader',
          options: {
            sourceMap: false,
            importLoaders: 2,
            modules: true,
            localIdentName: '[name]_[local]_[hash:base64:5]'
          }
        },
        {
          loader: 'postcss-loader',
          options: {
            sourceMap: false
          }
        },
        {
          loader: 'sass-loader',
          options: {
            sourceMap: false,
            indentedSyntax: true,
            data: '@import "@/sass/_variables.scss";'
          }
        }
      ]
    }
  );

  return defaultConfig;
};

Note the line data: '@import "@/sass/_variables.scss";' needs to match the file path for the SASS file with variables in your project.

This section of config was retrieved from Vue CLI 3 by running vue inspect > output.js and then copying the config for the rule test: /\.sass$/.

0
votes

You need to add the scss rule in your .storybook/webpack.config.js for storybook to parse scss.

const path = require('path');
const scss = {
  test: /\.scss$/,
  use: [
    'vue-style-loader',
    'css-loader',
    'sass-loader'
  ],
};
module.exports = (storybookBaseConfig, configType, defaultConfig) => {
  defaultConfig.module.rules.push(scss);

  return defaultConfig;
};

You may also need to install the appropriate loaders:

yarn add -D vue-style-loader sass-loader css-loader

0
votes

For anybody who can actually get Storybook to read SCSS files but can't get it to read the global variables file, do this in your custom webpack config:

module: {
  rules: [
    // Apply loader
    {
      test: /\.scss$/,
      loaders: [
        'style-loader',
        'css-loader',
        {
          loader: 'sass-loader',
          options: {
            prependData: '@import "path/to/global.scss";',
          },
        },
      ],
    },
  ],
}
-2
votes

If your components do not get styles applied when run in the Storybook component explorer UI, just import SASS styles in your main Storybook config/storybook/config.js (in previous versions was by default at storybook/config.js) like so:

// Import Styles
import '../../src/assets/styles/index.scss';

Usually you'd have your styles and plugins imported in your src/main.js / src/main.ts but you also need to do this in Storybook config, as when running Storybook it's not running the whole Vue app but just those individual components.