简体   繁体   English

在演示页面中安装和使用 Storybook

[英]Install and use Storybook in a demo page

I am creating a personal project in TypeScript.我正在 TypeScript 中创建一个个人项目。 It should be a library that exports React components and TypeScript functions.它应该是一个导出 React 组件和 TypeScript 函数的库。 The idea is therefore to publish this library on npm in the future.因此,这个想法是将来在 npm 上发布这个库。 There is also a demo page within the project and this is where I would like to use Storybook to test React components.项目中还有一个演示页面,这是我想使用 Storybook 测试 React 组件的地方。

This is the structure of the project:这是项目的结构:

.
├── demo/              # demo page
│   └── Home.tsx       # where I would like to use Storybook
│   └── index.html
│   └── index.tss
│   └── style.css
├── dist/              # distributable version of app built using Parcel
├── node_modules/      # npm managed libraries
├── src/               # project source code
│   └── lib/           # folder for your library
│      └── myFuncion.ts # function to export
│      └── MyComponent.tsx # react component to export
│   └── index.ts    # app entry point (it simply contains the exports of myFunction and myComponent)
├── .eslintrc.js
├── .gitignore
├── package.json
├── tsconfig.json
├── ...

I have read the Storybook documentation and it recommends to install Storybook by running npx sb init .我已阅读 Storybook 文档,它建议通过运行npx sb init来安装 Storybook。 I tried but the problem is that the stories are put in the project src directory, not in the demo page:我试过了,但问题是stories放在项目 src 目录中,而不是在演示页面中:

.
├── demo/              # demo page
│   └── Home.tsx       # where I would like to use Storybook
│   └── index.html
│   └── index.tss
│   └── style.css
├── dist/              # distributable version of app built using Parcel
├── node_modules/      # npm managed libraries
├── src/               # project source code
│   └── lib/           # folder for your library
│      └── myFuncion.ts # function to export
│      └── MyComponent.tsx # react component to export
│   └── stories/    # Storybook <<---
│   └── index.ts    # app entry point (it simply contains the exports of myFunction and myComponent)
├── .eslintrc.js
├── .gitignore
├── package.json
├── tsconfig.json
├── ...

And the storybook script that is created is this:创建的故事书脚本是这样的:

"scripts": {
    "storybook": "start-storybook -p 6006",
    "build-storybook": "build-storybook"
},

but I would like something like:但我想要类似的东西:

"scripts": {
    "storybook:demo": "start-storybook -p 6006",
    "build-storybook:demo": "build-storybook"
},

So how can I install and use Storybook only on the demo page?那么我怎样才能只在演示页面上安装和使用 Storybook?

Looks like you're ultimately trying to have multiple source directories.看起来您最终要尝试拥有多个源目录。 This is supported by both TypeScript and Storybook, it just needs a bit of configuration. TypeScript 和 Storybook 都支持这一点,它只需要一些配置。

tsconfig.json should have the include option set to: tsconfig.json应该将include选项设置为:

"include": [ "src", "demo" ]

This tells TypeScript (or its Babel loader) to compile files in src and demo .这告诉 TypeScript(或其 Babel 加载器)编译srcdemo中的文件。

.storybook/main.js should have the stories option set to: .storybook/main.js应该将stories选项设置为:

stories: [
  '../demo/**/*.stories.mdx',
  '../demo/**/*.stories.@(js|jsx|ts|tsx)',
],

This specifies which files should be interpreted as stories and in our case it would load *.stories.mdx / js / jsx / ts / tsx recursively from the demo folder.这指定了哪些文件应该被解释为故事,在我们的例子中,它将从demo文件夹中递归加载*.stories.mdx / js / jsx / ts / tsx

Also note that the stories folder is just an example folder created by Storybook and you can safely delete it.另请注意, stories文件夹只是 Storybook 创建的示例文件夹,您可以安全地删除它。 Stories can be in any of the directories processed by TypeScript as long as it matches the patterns specified in .storybook/main.js .故事可以位于 TypeScript 处理的任何目录中,只要它与.storybook/main.js中指定的模式匹配即可。

You can even have multiple Storybooks with multiple configs in a single project, but that may not be what you're after.您甚至可以在一个项目中拥有多个具有多个配置的故事书,但这可能不是您所追求的。 Just in case, though, the command would be start-storybook -p 6006 -c path/to/config/.storybook不过,以防万一,命令将是start-storybook -p 6006 -c path/to/config/.storybook

If I understood correctly, you want to build a components library and have demo app for your components.如果我理解正确,您想构建一个组件库并为您的组件提供演示应用程序。

I don't think there is a way to use Storybook in an existing app.我认为没有办法在现有应用程序中使用 Storybook。 This would mean you would have to build your demo app and use some components from Storybook to show case components in your app.这意味着您必须构建您的演示应用程序并使用 Storybook 中的一些组件来展示您的应用程序中的案例组件。 To my knowledge this is not possible.据我所知,这是不可能的。 It might be, but it seems complicated and I don't know of any docs on this.可能是这样,但它似乎很复杂,我不知道这方面的任何文档。

I think the Storybook app is (or should be) your demo app.我认为 Storybook 应用程序(或应该是)您的演示应用程序。 Storybook can render mdx files so you can add any content to it and get a demo app. Storybook 可以渲染mdx文件,因此您可以向其中添加任何内容并获得演示应用程序。

What you could try:你可以尝试什么:

  1. Move your demo app content and component stories to the demo folder将您的演示应用内容和组件故事移动到demo文件夹
  2. Migrate Home.tsx to a mdx fileHome.tsx迁移到mdx文件
  3. Change Storybook's config to load stories from '/demo`更改 Storybook 的配置以从“/demo”加载故事

To a degree, you can change Storybook's styling and "make it your own" and this can become your demo app.在某种程度上,您可以更改 Storybook 的样式“使其成为您自己的” ,这可以成为您的演示应用程序。


Until I discovered Storybook I used a home-made components show case app with react-live .在我发现 Storybook 之前,我使用了一个带有react-live的自制组件展示案例应用程序。 Might want to take a look at it but I think Storybook is is better and easier to maintain.可能想看看它,但我认为 Storybook 更好,更容易维护。

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

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