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

Module parse failed: Unexpected character '@' (1:0) with Storybook 6.1.11, Webpack 5.11.0, React 17.0.1

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

Github Repo Link

尝试使用 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]",
      },
    ],
  ],
};

webpack.config.js for css(包含部分代码)

{
        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",
        ],
      }

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

我不知道您对配置做了什么,但您会在 .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 时 running into problems 一些 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 配置)的文章over here

发生这种情况的另一个原因是:如果您正在向您的应用添加新组件并且为您的 sass-loader 定义的路径不再匹配。

例如如果你的 .storybook/main.js:

中有这个
  webpackFinal: async config => {
    // Add SASS support
    // https://storybook.js.org/docs/configurations/custom-webpack-config/#examples
    config.module.rules.push({
      test: /\.scss$/,
      use: [
        "style-loader",
        {
          loader: "css-loader",
          options: {
            modules: {
              compileType: "icss",
            },
          },
        },
        "sass-loader",
      ],
      include: path.resolve(__dirname, "../"),
    }) 

更新或完全删除 include 路径。