简体   繁体   中英

Can't access global SCSS variables in Storybook with Webpack + React

Trying to get access to global SCSS variables like I have in the rest of my app. I've taken it so far but I'm currently getting this error with the .storybook.scss test file;

The .storybook directory and the src directory are siblings.

-/.storybook
  |-config.ts
  |-storybook.scss
  |-webpack.config.js
-/src
  |-/components/
  |-/scss/
    |-global.scss
ERROR in ./src/components/Global/Dialog/dialog.scss (./node_modules/react-scripts/node_modules/css-loader??ref--8-1!./node_modules/postcss-loader/src??postcss!./node_modules/sass-loader/lib/loader.js??ref--8-3!./node_modules/style-loader!./node_modules/css-loader/dist/cjs.js!./node_modules/sass-loader/lib/loader.js!./src/components/Global/Dialog/dialog.scss)
Module build failed (from ./node_modules/sass-loader/lib/loader.js):

.modal {
^
Invalid CSS after "": expected 1 selector or at-rule, was "var content = requi"

in /Users/hghazni/Sites/eat/src/components/Global/Dialog/dialog.scss (line 1, column 1)

I've attempted to use many different webpack.config.js and config.ts configurations and followed the official documentation on the Storybook website but had no luck.

webpack.config.js

const { resolve } = require('path');

module.exports = {
    module: {
        rules: [
            {
                test: /\.scss$/,
                loaders: ['style-loader', 'css-loader', 'sass-loader'],
                include: path.resolve(__dirname, '../src/scss')
            },
            {
                test: /\.css$/,
                loader: 'style-loader!css-loader',
                include: __dirname
            }
        ]
    }
}

config.ts

import { configure } from "@storybook/react";
require('./storybook.scss');

const req = require.context('../src/', true, /.stories.tsx$/);

function loadStories() {
    req.keys().forEach((filename) => req(filename))
}

configure(loadStories, module);

.storybook.scss

body {
    background: $color-primary;
}

Dialog.tsx

import React from 'react'
import './dialog.scss';

type DialogType = {
  onClose: any;
  isOpen: any;
}

const Dialog: React.SFC<any> = props => {
  let dialog: any = (
    <div className={"dialog " + props.dialogClass}>
      <button className={"dialogClose"} onClick={props.onClose}>X</button>
      <div>{props.children}</div>
    </div>
  );

  if (!props.isOpen) {
    dialog = null;
  }

  return (
    <div className="modal">
      {dialog}
    </div>
  )
}

export default Dialog;

dialog.scss

.modal {
  position: absolute;
  top: 20%;
  left: 50%;
  -webkit-transform: translate(-50%);
  transform: translate(-50%);
  width: 95%;

  @include media-query(min, $desk-start) {
    width: 70%;
    max-width: $desk-start;
  }
}

.dialog {
  border: 4px dashed rgba(47, 144, 189, 0.411);
  border-radius: $half-spacing;
  padding: $base-spacing;
  background: $color-base;
}

.dialogClose {
  position: absolute;
  top: 0;
  right: 0;
  padding: $half-spacing;
  background: $color-base;
  border: 1px solid rgba(47, 144, 189, 0.411);
  color: rgba(47, 144, 189, 1);
  border-radius: 50px;
  padding: $half-spacing 12.5px;

  &:hover {
    cursor: pointer;
  }
}

I'm looking for any support/guidance on how to get storybook to be able to load my SCSS variables located in ../src/scss/global.scss .

So what Storybook or the documentation doesn't explicitly tell you is that having two webpack configs isn't ideal. You'll need to basically cater for a lot of the things needed in your main webpack config if you're trying to mirror your components.

Though there is a way to have to one webpack config with all the options separated so you can reuse it across the site as many times as you need. I even wrote an article about how to do it and built a boiler plate template for you with it already setup if you want to check it out.

https://levelup.gitconnected.com/a-killer-storybook-webpack-config-a0fd05dc70a4

Maybe you should ask webpack to compile .scss files also.. 😉

Try

test: /\.(s*)css$/

Had same kind of issue, then found that Storybook supports scss configuration out of the box, I removed all rules related to sass/scss from storybook webpack config override and everything worked. However npm i node-sass was needed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM