简体   繁体   English

如何在生产中为 Docs 插件的 React Storybook 启用 prop-types

[英]How to enable prop-types in production for a React Storybook for the Docs addon

By default prop-types do not run in production for a react app.默认情况下,prop-types 不会在 react 应用程序的生产环境中运行。 I realize this is a good thing to improve performance.我意识到这是提高性能的一件好事。 However, we have a Storybook that we have built and are deploying it to a static site.但是,我们已经构建了一本Storybook ,并将其部署到 static 站点。 Storybook has an addon called Docs that detects the prop-types for components and creates a table of the prop-types for easy to read documentation. Storybook 有一个名为 Docs 的插件,它检测组件的 prop-types 并创建一个 prop-types 表以便于阅读文档。

When running the storybook locally, everything works perfectly.在本地运行故事书时,一切正常。 The prop-types are detected and this table is generated.检测到道具类型并生成此表。

SpinningLoader.propTypes = {
  color: PropTypes.string,
  size: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
};

在此处输入图像描述

However, since prop-types are disabled in production by default.但是,由于 prop-types 在生产中默认是禁用的。 They cannot be detected when the storybook is deployed to a static site.当故事书部署到 static 站点时,无法检测到它们。

在此处输入图像描述

Is there a way to enable prop-types in production?有没有办法在生产中启用道具类型? Or some other workaround?还是其他一些解决方法?

It's a little difficult to know without seeing more of your setup.如果不查看更多设置,就很难知道。 If you're building it with the default storybook commands without and additional configuration it should "just work"...as far as I can tell.如果您使用默认的故事书命令构建它而没有其他配置,它应该“正常工作”......据我所知。

As mentioned in a comment, Storybook has a specific build command you can add to your package.json to export it as a static app:如评论中所述, Storybook 有一个特定的构建命令,您可以将其添加到package.json以将其导出为 static 应用程序:

"scripts": {
  "build-storybook": "build-storybook -c .storybook -o .out"
}

If you're using that command and it's still not working, are you using any custom webpack/build workflow, and can you post those as well?如果您正在使用该命令但它仍然无法正常工作,您是否使用任何自定义 webpack/build 工作流程,您也可以发布这些吗?

I've built a minimal repository for reference, which may be helpful in comparing your setup.我已经构建了一个最小存储库以供参考,这可能有助于比较您的设置。 Besides the packages in package.json it's really only 3 files;除了package.json中的包之外,它实际上只有 3 个文件; Storybook config, a React component, and a Component Story: Storybook 配置、一个 React 组件和一个组件故事:

.storybook/main.js .storybook/main.js

module.exports = {
  stories: ['../src/**/*.stories.[tj]s'],
  addons: ['@storybook/addon-docs'],
};

src/components/message/message.js src/components/message/message.js

import React from 'react'
import PropTypes from 'prop-types'

const Message = function Message({ text, color }) {
  return (<div style={{ color }}>{text}</div>)
}

Message.propTypes = {
  text: PropTypes.string.isRequired,
  color: PropTypes.string.isRequired,
}

export default Message

src/components/message/message.stories.js src/components/message/message.stories.js

import React from 'react'
import Message from './message'

export default { title: 'Message', component: Message }

export const withText = () => <Message text="Hello World" color="green" />

If I run the build-storybook command, cd.out , and then npx live-server , I see the static-built storybook site, with my Message component, and the 'Docs' tab that includes the prop-types:如果我运行build-storybook命令cd.out ,然后运行 npx npx live-server ,我会看到静态构建的故事书站点,以及我的Message组件,以及包含 prop-types 的“Docs”选项卡:

在此处输入图像描述

Full repository for reference完整存储库供参考

https://github.com/BenjaminWFox/react-storybook https://github.com/BenjaminWFox/react-storybook

  1. A workaround would be to manually specify the information you want to display in the table for each component using ArgTypes: https://storybook.js.org/docs/react/api/argtypes.一种解决方法是使用 ArgTypes 手动指定要在表中显示的每个组件的信息: https://storybook.js.org/docs/react/api/argtypes。 Then you can continue with the documentation with that approach.然后,您可以使用该方法继续文档。

  2. Another option would be to complete andpublish the storybook while the app is still in development.另一种选择是在应用程序仍在开发中时完成并发布故事书 This way you will have the prop-types detected and the table generated for you, then you can later build your app for production.这样,您将检测到道具类型并为您生成表格,然后您可以稍后构建您的应用程序以进行生产。

This is how you would declare the argTypes in the first option这就是您在第一个选项中声明 argTypes 的方式

// Button.stories.js

export default {
  title: 'Button',
  component: Button,
  argTypes: {
    label: {
      description: 'overwritten description',
      table: {
        type: { 
            summary: 'something short', 
            detail: 'something really really long' 
        },
       defaultValue: { summary: 'default-label' }
      },
      control: {
        type: 'text',
      },
    },
  },
};

This is the result这是结果在此处输入图像描述

In case anyone runs into this issue again, setting NODE_ENV to development , as suggested here https://github.com/storybookjs/storybook/issues/8140#issuecomment-621314565 , solved our problems如果有人再次遇到此问题, NODE_ENV设置为development ,如此处建议https://github.com/storybookjs/storybook/issues/8140#issuecomment-621314565 ,解决了我们的问题

The issue was ultimately caused by including the transform-react-remove-prop-types plugin in our babel.config.js production environment.这个问题最终是由于在我们的babel.config.js生产环境中包含了transform-react-remove-prop-types插件引起的。 Without propTypes to read, there's nothing to display.如果没有要读取的 propTypes,就没有可显示的内容。

'propTypes' is a useful feature through which we can validate typechecking of props in React but it also unnecessarily creates runtime overhead. 'propTypes' 是一个有用的功能,通过它我们可以验证 React 中 props 的类型检查,但它也不必要地产生了运行时开销。 It downgrades the apps performance.它会降低应用程序的性能。

That is the reason it is NOT available in production.这就是它在生产中不可用的原因。

It has been made to help developers especially in a team, to find out if there is any wrong type of props been passed to the component, while writing code during the development environment.它旨在帮助开发人员,尤其是团队中的开发人员,在开发环境中编写代码时找出是否有任何错误类型的道具被传递给组件。 It does not add any extra functionality.它不会添加任何额外的功能。 It will also add extra lines of code unnecessarily.它还会不必要地添加额外的代码行。

By keeping it in the production flow it will defeat the whole purpose.通过将其保留在生产流程中,它将破坏整个目的。

Whether you also use Flow/typescript for typechecking, there purpose are all same.无论您是否也使用 Flow/typescript 进行类型检查,目的都是一样的。 refer: https://reactjs.org/docs/typechecking-with-proptypes.html参考: https://reactjs.org/docs/typechecking-with-proptypes.html

Now, your issue is similar to the below known issue, kindly refer below: https://github.com/storybookjs/storybook/issues/1661现在,您的问题类似于以下已知问题,请参考以下内容: https://github.com/storybookjs/storybook/issues/1661

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM