module解析失败:使用 Storybook 6.1.11、Webpack 5.11.0、React 17.0.1 出现意外字符“@”(1:0)

IT技术 reactjs webpack sass storybook webpack-5
2021-05-13 12:59:48

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

Github 回购链接

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

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

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

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,
      }
    };
  },
};

在此处输入图片说明

2个回答

我不知道你对你的配置做了什么,但你会在里面定义配置的东西.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";

有些人在使用带有 Webpack 5 的 Storybook 6.2.0 时遇到了一些 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(和修改故事书的WebPack配置)在这里