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.

Storybook vuejs scss global scss helper file not available via webpack

See original GitHub issue

Describe the bug There seems to be an issue in rendering the vue component, as I get alsway ‘Undefined variable’ for my scss variable from the style tag included in the vue file. This variable comes from a global helpers.scss, which is loaded in the vue app from webpack as loader scss data.

I have a vuejs setup done withe the vue-cli. I have added storybook to it manually, done like in the tutorial from learnstorybook.com

The vue-cli-plugin-storybook did also not helped me.

To Reproduce Steps to reproduce the behavior: Project setup: |src |—|components |—|—|Component1 |—|—|—|Component1.vue |—|—|—|Component1.story.js |—|sass |—|—|utils |—|—|—|utils1.scss |—|—|—|utils2.scss |—|—|helpers.scss

My components are vue single file components. I have also some webpack custom settings like aliases and the global loader of the helpers.scss, so I don’t have to include it in every component. 2. run $ npm run storybook 3. see the error:

ERROR in ./src/components/Error/Error.vue?vue&type=style&index=0&id=395d98a1&lang=scss&scoped=true& (./node_modules/css-loader!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib??vue-loader-options!./src/components/Error/Error.vue?vue&type=style&index=0&id=395d98a1&lang=scss&scoped=true&)
Module build failed (from ./node_modules/sass-loader/lib/loader.js):

        background-color: $colorRedNewsletterBtn;
                     ^
      Undefined variable: "$colorRedNewsletterBtn".
      in /Users/cojok/Sites/checkout/src/components/Error/Error.vue (line 23, column 23)
 @ ./src/components/Error/Error.vue?vue&type=style&index=0&id=395d98a1&lang=scss&scoped=true& (./node_modules/style-loader!./node_modules/css-loader!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib??vue-loader-options!./src/components/Error/Error.vue?vue&type=style&index=0&id=395d98a1&lang=scss&scoped=true&) 2:14-316 21:1-42:3 22:19-321
 @ ./src/components/Error/Error.vue?vue&type=style&index=0&id=395d98a1&lang=scss&scoped=true&
 @ ./src/components/Error/Error.vue
 @ ./src/components/Error/Error.stories.js
 @ ./src/components sync \.stories\.js$
 @ ./.storybook/config.js
 @ multi ./node_modules/@storybook/core/dist/server/common/polyfills.js ./node_modules/@storybook/core/dist/server/preview/globals.js ./.storybook/config.js ./node_modules/webpack-hot-middleware/client.js?reload=true

Expected behavior I expected that storybook would render the component as in the normal setup. As i don’t need to rewrite the components to manually add the helpers.scss in each one of them.

Code snippets .storybook/webpack.config.js

const path = require('path')
const alias = {
  '@': path.join(__dirname, 'src'),
  'utils': path.join(__dirname, 'src/_utils'),
  'components': path.join(__dirname, 'src/components')
}
module.exports = async ({ config, mode }) => {
  config.resolve.alias = {
    ...config.resolve.alias,
    ...alias
  }
  config.module.rules.push({
    test: /\.scss$/,
    loaders: ['style-loader', 'css-loader', 'sass-loader'],
    include: path.resolve(__dirname, "../")
  });

  return config
}

.storybook/config.js

import { configure } from '@storybook/vue';
// // automatically import all files ending in *.stories.js
const req = require.context('../src/components', true, /\.stories\.js$/);
function loadStories() {
  req.keys().forEach(filename => req(filename));
}

configure(loadStories, module);

vue.config.js

const path = require('path')
module.exports = {
  css: {
    loaderOptions: {
      // pass options to sass-loader
      sass: {
        // @/ is an alias to src/
        // so this assumes you have a file named `src/variables.scss`
        data: `@import "@/sass/helpers.scss";`
      }
    }
  },
  configureWebpack: {
    resolve: {
      alias: {
        '@': path.join(__dirname, 'src'),
        'utils': path.join(__dirname, 'src/_utils'),
        'components': path.join(__dirname, 'src/components')
      }
    }
  }
}

System:

  • OS: MacOS 10.13.6 (High Sierra)
  • Device: iMac (Retina 4K, 21.5-inch, 2017)
  • Browser: not relevant
  • Frameworks: @storybook/vue v5.0.11 vue v2.6.10

Thanks.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:13

github_iconTop GitHub Comments

13reactions
ieromanovcommented, May 9, 2019

Hi!

This is my config and it works for me. I think this is not the best option, but others did not work for me.

.storybook\webpack.config.js

const path = require('path')
const rootPath = path.resolve(__dirname, '../src')

module.exports = ({ config }) => {
	config.module.rules.push({
		test: /\.scss$/,
		use: [
			'vue-style-loader',
			'css-loader',
			{
				loader: 'sass-loader',
				options: {
					data: `
						@import "@/scss/common/_functions.scss";
						@import "@/scss/variables/_global.scss";
						@import "@/scss/variables/_media.scss";
						@import "@/scss/common/_mixins.scss";
						@import "@/scss/common/_media.scss";
					`
				}
			}
		],
	})

	config.module.rules.push({
		test: /\.pug$/,
		loader: 'pug-plain-loader'
	})

	config.module.rules.push({
		test: /\.svg$/,
		use: [
			'svg-sprite-loader',
			'svgo-loader'
		]
	})

	config.resolve.alias['@'] = rootPath
	config.resolve.alias['~'] = rootPath

	return config
}
4reactions
js-wi5commented, Apr 21, 2021

Thanks @davestewart that’s much simpler than the other solutions and worked for me too.

It should be noted that if your version of sass-loader is v10.* (as on my project) then the above will work when data: is replaced with additionalData:.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't compile sass while using Vue Storybook - Stack Overflow
The issue is from the webpack config. If you're using SASS, your webpack.config.js file in your .storybook folder should look like this:
Read more >
Getting started with Storybook in Vue 3 - LogRocket Blog
storybook /main.js file to help it process SCSS by extending a WebPack loader to process your styles. Sometimes your fonts are not imported; ......
Read more >
Setting up Tailwind with Vue.js | Sanity.io guide
In this guide, you will add Tailwind to a Vue application.
Read more >
Configuring Storybook: 6 Tips You Can't Miss | by Zheng Li
Tip 1: The Storybook has its default webpack configuration, when trying to import your current webpack config or add your own rules, DO...
Read more >
Install Storybook
Storybook needs to be installed into a project that is already set up with a framework. It will not work on an empty...
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