gpt4 book ai didi

reactjs - 模块解析失败 : Unexpected character '@' (1:0) with Storybook 6. 1.11、Webpack 5.11.0、React 17.0.1

转载 作者:行者123 更新时间:2023-12-04 11:23:14 24 4
gpt4 key购买 nike

尝试使用所有最新版本设置 react-app。
Github Repo Link
尝试使用导入的 sass 文件运行 storybook 将导致以下错误。尝试在不导入样式的情况下运行,故事书有效。
当它作为 npm start run 运行时,相同的代码可以正常工作没有警告和错误。
我已经使用 配置了 css 模块@dr.pogodin/babel-plugin-react-css-modules 使用 sass、webpack 5、react 17 和最新的软件包。

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 对于 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",
],
}
故事书/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,
}
};
},
};
enter image description here

最佳答案

我不知道您对配置做了什么,但您可以在 .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";

    关于reactjs - 模块解析失败 : Unexpected character '@' (1:0) with Storybook 6. 1.11、Webpack 5.11.0、React 17.0.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65464491/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com