尝试使用所有最新版本设置 react-app。
尝试使用导入的 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,
}
};
},
};