简体   繁体   English

故事书 - 使用自定义 webpack/babel 的打字稿项目中没有出现任何故事

[英]Storybook - no stories showing up in typescript project with custom webpack / babel

I am trying to set up Storybook in a project.我正在尝试在一个项目中设置 Storybook。 My project is runing on react@^16, and I'm using typescript, with a custom babel and webpack setup for development and build.我的项目在 react@^16 上运行,我正在使用 typescript,带有用于开发和构建的自定义 babel 和 webpack 设置。 To set up storybook, I did为了设置故事书,我做了

npx sb init

This installs everything needed.这将安装所需的一切。 It puts a .storybook folder in the root folder, and a stories folder in my src folder with some prefab components and stories in tsx format (which is what I want):它把一个.storybook在根文件夹的文件夹,和stories在我的文件夹src与一些预制组件和故事文件夹tsx格式(这就是我想要的):

在此处输入图片说明

在此处输入图片说明

The .storybook/main.js file seems fine: .storybook/main.js文件看起来不错:

module.exports = {
  "stories": [
    "../src/**/*.stories.mdx",
    "../src/**/*.stories.@(js|jsx|ts|tsx)"
  ],
  "addons": [
    "@storybook/addon-links",
    "@storybook/addon-essentials"
  ]
}

And the average .stories.js file automatically installed by npx sb init also seems fine:由 npx npx sb init自动安装的平均 .stories.js 文件似乎也很好:

import React from 'react';
// also exported from '@storybook/react' if you can deal with breaking changes in 6.1
import { Story, Meta } from '@storybook/react/types-6-0';

import { Header, HeaderProps } from './Header';

export default {
  title: 'Example/Header',
  component: Header,
} as Meta;

const Template: Story<HeaderProps> = (args) => <Header {...args} />;

export const LoggedIn = Template.bind({});
LoggedIn.args = {
  user: {},
};

export const LoggedOut = Template.bind({});
LoggedOut.args = {};

But when I run npm run storybook , the storybook landing page has no stories.但是当我运行npm run storybook ,故事书登陆页面没有故事。 Even though it had installed some default stories to start playing with.即使它已经安装了一些默认的故事来开始玩。 It says:它说:

Oh no! Your Storybook is empty. Possible reasons why:
The glob specified in main.js isn't correct.
No stories are defined in your story files.

As requested, here is a link to the repo so you can dig a bit deeper into the structure, weback config, etc. Note I have not committed the npx sb init changes yet, so you won't see the files there, only my starting point just before running the sb init .根据要求,这里是 repo 的链接,因此您可以更深入地了解结构、webback 配置等。 注意我还没有提交 npx npx sb init更改,所以您不会在那里看到文件,只有我的运行sb init之前的起点。

I haven't had any issues getting npx sb init to work with a standard create-react-app, but with my custom webpack build and typescript, its just empty.我没有任何问题让npx sb init与标准的 create-react-app 一起工作,但是使用我的自定义 webpack 构建和打字稿,它只是空的。 What's going wrong?怎么了?

Edit: Additional detail编辑:附加细节

I realize that just running npx sb init , then npm run storybook throws this error:我意识到只要运行npx sb init ,然后npm run storybook抛出这个错误:

ERROR in ./.storybook/preview.js-generated-config-entry.js
Module not found: Error: Can't resolve 'core-js/modules/es.array.filter'

Based on this thread , installing core-js@3 solves the problem and storybook runs, though with no stories.基于此线程,安装core-js@3解决了问题并且故事书运行,但没有故事。

It seems like the babel plugin transform-es2015-modules-amd doesn't fit right with storybook since sb still uses your babel configuration.似乎 babel 插件transform-es2015-modules-amd不适合storybook因为sb仍然使用您的 babel 配置。

You might need to remove it then it would work:您可能需要删除它然后它会工作:

{
  "plugins": [
     // "transform-es2015-modules-amd", // Remove this plugin
   ]
}

If you want to have a special babel configuration for storybook, place it .storybook/.babelrc so the configuration would be simple like this:如果你想为 storybook 设置一个特殊的 babel 配置,把它.storybook/.babelrc这样配置就很简单了:

.storybook/.babelrc : .storybook/.babelrc :

{
  "presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"]
}

NOTE: You might miss to forget install @babel/preset-typescript to help you transform your typescript code.注意:您可能会忘记安装@babel/preset-typescript来帮助您转换打字稿代码。

In case of dealing with arcgis-js-api in sb , you have to declare @arcgis/webpack-plugin in storybook's webpack configuration by adding to its config.如果在sb中处理arcgis-js-api ,你必须在 storybook 的 webpack 配置中声明@arcgis/webpack-plugin通过添加到它的配置。

Here are a few steps you have to do:以下是您必须执行的几个步骤:

  • Add webpackFinal property in .storybook/main.js with following content:.storybook/main.js添加webpackFinal属性,内容如下:
const ArcGISPlugin = require('@arcgis/webpack-plugin');

module.exports = {
    // ...
    webpackFinal: (config) => {
        // Add your plugin
        config.plugins.push(
            new ArcGISPlugin(),
        );
        
        // Since this package has used some node's API so you might have to stop using it as client side
        config.node = {
            ...config.node,
            process: false,
            fs: "empty"
        };

        return config;
    }
};

  • One more thing to be aware of, some components are importing scss files, so you might need to support it by adding a scss addon '@storybook/preset-scss'需要注意的另一件事是,某些组件正在导入scss文件,因此您可能需要通过添加 scss 插件'@storybook/preset-scss'来支持它
// Install
npm i -D  @storybook/preset-scss css-loader sass-loader style-loader


// Add to your current addons
{
  addons: ['@storybook/addon-links', '@storybook/addon-essentials', '@storybook/preset-scss'],
}

Maybe you have problems with the stories path, try to save only "../src/**/*.stories.js" in your config to see if its the reason也许你的故事路径有问题,试着在你的配置中只保存“../src/**/*.stories.js”,看看是不是原因

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

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

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