storybook [Bug]: 构建预览 SB_BUILDER-WEBPACK5_0002 (WebpackInvocationError) 失败:找不到模块:错误:无法解析 './Typography' 在 './components/Typography'

w1jd8yoj  于 6个月前  发布在  Webpack
关注(0)|答案(1)|浏览(117)

描述问题

我正在尝试将我的storybook上传到github pages,遇到了这个问题,有人能帮忙解决吗?
更多信息:
node版本:v20.15.1
storybook: "^8.2.5"
next: "14.2.5",

components/Typography.tsx

'use client'

import React from 'react'
import { THEMES } from '@/config/themes'
type TVariantsAndThemesTypography = {
  theme: 'theme1' | 'theme2' | 'theme3'
  variant: 'h1' | 'h2' | 'h3' | 'body-1' | 'body-2' | 'caption' | 'overline'
}

export const Typography: React.FC<
  TVariantsAndThemesTypography & { children: React.ReactNode }
> = ({ children, ...props }) => {
  const { theme, variant } = props

  switch (variant) {
    case 'h1':
      return <h1 className={THEMES[theme].h1}>{children}</h1>
    case 'h2':
      return <h2 className={THEMES[theme].h2}>{children}</h2>
    case 'h3':
      return <h3 className={THEMES[theme].h3}>{children}</h3>
    case 'body-1':
      return <p className={THEMES[theme]['body-1']}>{children}</p>
    case 'body-2':
      return <p className={THEMES[theme]['body-2']}>{children}</p>
    case 'caption':
      return <p className={THEMES[theme].caption}>{children}</p>
    case 'overline':
      return <p className={THEMES[theme].overline}>{children}</p>
    default:
      return <p className={THEMES[theme]['body-1']}>{children}</p>
  }
}

Typography.displayName = 'Typography'

components/Typography.stories.tsx

import type { Meta, StoryObj } from '@storybook/react'
import { Typography } from './Typography'

export default {
  title: 'Componentes/Tipografias',
  component: Typography,
  args: {
    children:
      'Lorem ipsum, dolor sit amet consectetur adipisicing elit. Porro reprehenderit est veniam? Rem recusandae sint perspiciatis debitis corrupti. Deserunt neque exercitationem illo voluptatum laudantium culpa suscipit accusantium blanditiis voluptas numquam.',
  },
} as Meta

type Story = StoryObj<typeof Typography>

export const h1: Story = {
  args: {
    variant: 'h1',
    theme: 'theme1',
  },
}

export const h2: Story = {
  args: {
    variant: 'h2',
    theme: 'theme1',
  },
}

export const h3: Story = {
  args: {
    variant: 'h3',
    theme: 'theme1',
  },
}

export const Body1: Story = {
  args: {
    variant: 'body-1',
    theme: 'theme1',
  },
}

export const Body2: Story = {
  args: {
    variant: 'body-2',
    theme: 'theme1',
  },
}

export const Caption: Story = {
  args: {
    variant: 'caption',
    theme: 'theme1',
  },
}

export const Overline: Story = {
  args: {
    variant: 'overline',
    theme: 'theme1',
  },
}

storybook/main.ts

import type { StorybookConfig } from '@storybook/nextjs'

const config: StorybookConfig = {
  stories: [
    '../components/**/*.mdx',
    '../components/**/*.stories.@(js|jsx|mjs|ts|tsx)',
    '../stories/**/*.mdx',
  ],
  addons: [
    '@storybook/addon-onboarding',
    '@storybook/addon-links',
    '@storybook/addon-essentials',
    '@chromatic-com/storybook',
    '@storybook/addon-interactions',
    '@storybook/addon-themes',
  ],
  framework: {
    name: '@storybook/nextjs',
    options: {},
  },
  staticDirs: ['../public'],
}
export default config

storybook/preview.ts

import type { Preview } from '@storybook/react'
import '../app/globals.css'

import { withThemeByClassName } from '@storybook/addon-themes'

const preview: Preview = {
 parameters: {
   backgrounds: {
     default: 'dark',
   },
   controls: {
     matchers: {
       color: /(background|color)$/i,
       date: /Date$/i,
     },
   },
 },
 tags: ['autodocs'],
 decorators: [
   withThemeByClassName({
     themes: {
       theme1: 'theme1',
       theme2: 'theme2',
       theme3: 'theme3',
     },
     defaultTheme: 'theme1',
   }),
 ],
}

export default preview

.github/workflows/deploy-github-pages.yaml

name: Build and Publish Storybook to GitHub Pages

on:
  push:
    branches:
      - 'design-system' 

permissions:
  contents: read
  pages: write
  id-token: write

jobs:
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.build-publish.outputs.page_url }}
    runs-on: ubuntu-latest
    steps:
    - id: build-publish
      uses: bitovi/github-actions-storybook-to-github-pages@v1.0.3
      with:
        path: storybook-static 
        install_command: npm install
        build_command: npm run build-storybook

复现链接

https://stackblitz.com/edit/github-kcfh3e?file=.storybook%2Fpreview.ts

复现步骤

根据stackblitz链接中的信息,尝试在github pages中构建和部署storybook,你将会遇到与我面临的相同的错误。

ncgqoxb0

ncgqoxb01#

免责声明
由于信息是自动生成的,可能存在不准确的情况。
为了解决这个问题,请确保在您的 Storybook 配置中正确导入 Typography 组件。更新 .storybook/main.ts 中的 webpackFinal 字段,以包含 tsconfig-paths-webpack-plugin 以解析 TypeScript 路径:

import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';

const config: StorybookConfig = {
  // ... other configurations
  webpackFinal: async (config) => {
    config.resolve.plugins = [
      ...(config.resolve.plugins || []),
      new TsconfigPathsPlugin({
        extensions: config.resolve.extensions,
      }),
    ];
    return config;
  },
};

export default config;

请确保您的故事中 Typography 组件路径正确:

import { Typography } from '../components/Typography';

这应该可以解决模块未找到的错误。

参考资料

/code/core/src/server-errors.ts
/docs/configure/webpack.mdx
/code/frameworks/web-components-webpack5
/code/addons/docs/template/stories/docspage
/code/builders/builder-webpack5
/docs/_snippets/web-components-webpack5-add-framework.md
/code/core/src/csf-tools/CsfFile.test.ts
/code/addons/interactions/template/stories
/code/builders/builder-webpack5/src/preview
/.github/DISCUSSION_TEMPLATE/help.yml
/code/addons/docs/template/stories
/test-storybooks/portable-stories-kitchen-sink/react/.storybook
/code/frameworks/server-webpack5
/code/core/src/preview
/code/frameworks/nextjs/template/cli
/.github/comments/invalid-link.md
/test-storybooks/portable-stories-kitchen-sink/nextjs/.storybook
/code/lib/cli/src/automigrate/fixes/prompt-remove-react.ts
/code/addons/interactions/template
/.github/DISCUSSION_TEMPLATE/ideas.yml
/code/builders/builder-webpack5/src
/code/core/src/preview-errors.ts
/code/frameworks/html-webpack5
/code/addons/docs/template/stories/docs2
/code/presets/server-webpack/src/index.ts

关于 Greptile

此响应为您提供了研究的起点,而不是精确的解决方案。
帮助我们改进!如果这有帮助,请留下一个 👍,如果无关紧要,请留下一个 👎。

相关问题