【问题标题】:Can I use CSS Modules with Storybook 5 (React flavour)?我可以将 CSS 模块与 Storybook 5(React 风格)一起使用吗?
【发布时间】:2019-08-02 19:18:06
【问题描述】:

我正在尝试在我的项目中使用 Storybook v5.0,并且我正在使用 React + Webpack4 + CSS Modules。 (我没有使用 CreateReactApp)

我的设置非常简单,我可以在没有 CSS 模块的情况下设置 Storybook。

但是,如果我尝试编辑自定义故事书 webpack 配置以支持 CSS 模块,则会出错。

当我运行npm run storybook 时,我得到的错误是:

ERROR in ./src/components/Test/index.css (./node_modules/css-loader/dist/cjs.js??ref--6-1!./node_modules/postcss-loader/src??postcss!./node_modules/style-loader!./node_modules/css-loader/dist/cjs.js??ref--9-1!./node_modules/postcss-loader/src!./src/components/Test/index.css)
Module build failed (from ./node_modules/postcss-loader/src/index.js):
SyntaxError

(2:1) Unknown word

  1 |
> 2 | var content = require("!!../../../node_modules/css-loader/dist/cjs.js??ref--9-1!../../../node_modules/postcss-loader/src/index.js!./index.css");
    | ^
  3 |
  4 | if(typeof content === 'string') content = [[module.id, content, '']];

在我的package.json:

"@storybook/addon-actions": "^5.0.0",
"@storybook/addons": "^5.0.0",
"@storybook/react": "^5.0.0",

我的.storybook/webpack.config.js 遵循他们网站上的示例,如下所示:

const path = require("path");

const stylesLoaders = [
  "style-loader",
  {
    loader: "css-loader",
    options: {
      modules: true,
      localIdentName: "[path]__[local]--[hash:base64:5]"
    }
  },
  "postcss-loader"
];

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        loaders: stylesLoaders,
        include: path.resolve(__dirname, "../")
      }
    ]
  }
};

任何帮助将不胜感激,因为我想使用最新版本的 Storybook!

【问题讨论】:

  • 嘿,我也在为同样的问题苦苦挣扎。有什么解决办法吗? @Saldog
  • 如果您想使用完全控制模式,您可能必须删除一些与您的冲突的默认规则。在此处遵循此示例和整个对话:github.com/storybooks/storybook/issues/…

标签: webpack css-modules storybook react-css-modules


【解决方案1】:

我对新故事书 ( v5 ) 也有同样的问题。最后,在我不得不在 .storybook/webpack.config.js 中传播 config 和 config.module 之后,我让它工作了

根据 Storybook 的文档,我使用 webpack 的函数而不是对象:https://storybook.js.org/docs/configurations/custom-webpack-config/

这是一个例子:

module.exports = ({ config }) => ({
  ...config,
  module: {
    ...config.module,
    rules: [
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        use:[
          {
            loader: require.resolve("babel-loader"),
            options: {
            presets: [["react-app", { flow: false, typescript: true }]]
            }
          }
        ],
        include: path.resolve(__dirname, "../")
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: "style-loader",
            options: { sourceMap: true }
          },
          {
            loader: "css-loader",
            options: {
              importLoaders: 1,
              modules: true,
              localIdentName: "[name]__[local]__[hash:base64:5]"
            }
          },
          {
            loader: require.resolve("postcss-loader"),
            options: {
              ident: "postcss",
              plugins: () => [
                require("postcss-flexbugs-fixes"),
                require("postcss-inline-svg"),
                require("postcss-svgo"),
                autoprefixer({
                  browsers: [
                    ">1%",
                    "last 4 versions",
                    "Firefox ESR",
                    "not ie < 9"
                  ],
                  flexbox: "no-2009"
                })
              ]
            }
          }
        ],
        include: path.resolve(__dirname, "../")
      }
    ]
  }
});

希望对你有帮助:)

【讨论】:

  • 欢迎来到 SO。请不要发布问题作为答案。由于您可能没有足够的声誉来发表评论,因此您可以投票赞成这个问题
  • 看起来帖子未正确编辑为答案。请不要投反对票,因为这是一个可能的有效答案,如官方故事书更改日志中所述:github.com/storybooks/storybook/blob/next/…
【解决方案2】:

这对我有用:

// ./.storybook/main.js
module.exports = {
  //
  webpackFinal: (config, {configType}) => {
    const path  = require('path');

    config.module.rules.push({
      test: /\.css$/,
      use: ['style-loader', 'css-loader?modules=true'],
      include: path.resolve(__dirname, '../'),
    });
    
    return config;
  }
  //
}

与

重命名我的 CSS 文件从 &lt;filename&gt;.css 到 &lt;filename&gt;.module.css。

祝你好运……

【讨论】:

  • css-loader?modules=true 是一个很好的配置选项的快捷方式。感谢分享!
【解决方案3】:

我对新的故事书 (v6) 也有同样的问题。最后,当我应用此解决方案时,我能够解决错误

https://github.com/GVanderLugt/storybook-next-antd-less-typescript-config/blob/master/.storybook/next-preset.js

非常感谢他!

【讨论】:

    猜你喜欢
    • 2018-12-10
    • 2021-09-16
    • 2017-01-05
    • 2023-03-24
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多