6

尝试使用所有最新版本设置 react-app。

Github 回购链接

尝试使用导入的 sass 文件运行故事书将导致以下错误。尝试在不导入样式的情况下运行,故事书有效。

相同的代码在运行时可以正常工作npm start run,没有警告和错误。

我已经使用@dr.pogodin/babel-plugin-react-css-modules和 sass、webpack 5、react 17 和最新包配置了 css 模块。

ERROR in ./src/assets/stylesheets/app.scss 1:0
Module parse failed: Unexpected character '@' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> @import "./base.scss";
| @import "./generics/font.scss";
| @import "./generics/spacing.scss";
 @ ./stories/index.js 5:0-44 8:2-10:4 8:58-10:3 9:4-49
 @ ./src/components/atoms/button/stories.js

babel.config.js

module.exports = {
  presets: ["@babel/preset-env", "@babel/preset-react"],
  plugins: [
    [
      "@dr.pogodin/babel-plugin-react-css-modules",
      {
        webpackHotModuleReloading: true,
        autoResolveMultipleImports: true,
        filetypes: {
          ".scss": {
            syntax: "postcss-scss",
          },
        },
        generateScopedName: "[name]__[local]___[hash:base64:5]",
      },
    ],
  ],
};

用于 css 的webpack.config.js(包含部分代码)

{
        test: /\.(css|sass|scss)$/,
        exclude: /node_modules/,
        use: [
          isDev ? "style-loader" : MiniCssExtractPlugin.loader,
          {
            loader: "css-loader",
            options: {
              modules: {
                auto: (resourcePath) =>
                  resourcePath.indexOf("assets/stylesheets") === -1,
                localIdentName:"[name]__[local]___[hash:base64:5]",
              },
              sourceMap: true,
            },
          },
          "sass-loader",
        ],
      }

故事书/webpack.config.js文件

const custom = require('../webpack.config.js');

module.exports = {
  // stories: ['../src/components/**/*.stories.js'],
  webpackFinal: (config) => {
    return {
      ...config,
      module: {
        rules: custom.module.rules,
      },
      resolve: {
        ...config.resolve,
        ...custom.resolve,
      }
    };
  },
};

在此处输入图像描述

4

2 回答 2

4

我不知道你对你的配置做了什么,但你会在里面定义配置的东西.storybook/main.js。并且对于全局样式 css 应该包含在preview.js文件中。

简而言之,您必须做以下几件事:

  • 删除您的.storybook/config.js并添加.storybook/main.js以下内容:
const custom = require('../webpack.config.js');

module.exports = {
  stories: [
    '../src/**/stories.js', // The name should have a prefix for component name like `button.stories.js` instead of `stories.js` like you've done. As you renamed, you can remove this pattern
    "../src/**/*.stories.@(js|jsx|ts|tsx)"
  ],
  webpackFinal: (config) => {
    return {
      ...config,
      module: {
        rules: custom.module.rules,
      },
      resolve: {
        ...config.resolve,
        ...custom.resolve,
      }
    };
  },
};
  • 创建.storybook/preview.js以导入您的全局样式:
import "../src/assets/stylesheets/app.scss";
于 2020-12-27T12:24:07.903 回答
3

有些人在使用 Storybook 6.2.0 和 Webpack 5 时遇到了一些 scss 预设的问题。我建议不要使用预设,而是如上所述在 main.js 中配置 Webpack 配置。这是 Sass 的 Storybook Webpack 配置的相关部分:

module: {
        ...config.module,
        rules: [
          ...config.module.rules,
          {
            test: /\.(scss)$/,
            use: [
              {
                loader: 'style-loader',
              },
              {
                loader: 'css-loader',
              },
              {
                loader: 'postcss-loader',
                options: {
                  postcssOptions: {
                    plugins: function () {
                      return [require('precss'), require('autoprefixer')];
                    },
                  },
                },
              },
              {
                loader: require.resolve('sass-loader'),
                options: {
                  implementation: require('sass'),
                },
              },
            ],
          },
        ],
      },

我已经写了更多关于使用 Webpack 5 启动 Storybook(以及修改 Storybook Webpack 配置)文章。

于 2021-03-26T22:27:50.140 回答