当我们导入本地组件的时候

import { Field, FieldDescription, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

或使用相对路径

import { Field, FieldDescription, FieldLabel } from "../components/ui/field";
import { Input } from "../components/ui/input";

都会报错:

Failed to fetch dynamically imported module: http://localhost:6006/src/stories/wasm/wasm.mdx?t=1777191804922

The component failed to render properly, likely due to a configuration issue in Storybook. Here are some common causes and how you can address them:

Missing Context/Providers: You can use decorators to supply specific contexts or providers, which are sometimes necessary for components to render correctly. For detailed instructions on using decorators, please visit the Decorators documentation.

Misconfigured Webpack or Vite: Verify that Storybook picks up all necessary settings for loaders, plugins, and other relevant parameters. You can find step-by-step guides for configuring Webpack or Vite with Storybook.

Missing Environment Variables: Your Storybook may require specific environment variables to function as intended. You can set up custom environment variables as outlined in the Environment Variables documentation.

TypeError: Failed to fetch dynamically imported module: http://localhost:6006/src/stories/wasm/wasm.mdx?t=1777191804922

解决方法

pnpm add -D vite-tsconfig-paths

编辑.storybook/main.ts

新增代码:

  async viteFinal(config) {
    return mergeConfig(config, {
      // 直接读取 tsconfig.json 中的别名配置
      plugins: [tsconfigPaths()],
    
    });
  },

附录:完整代码:

import type { StorybookConfig } from '@storybook/react-vite';
import { mergeConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';

const config: StorybookConfig = {
  "stories": [
    "../src/**/*.mdx",
    "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"
  ],
  "addons": [
    "@chromatic-com/storybook",
    "@storybook/addon-vitest",
    "@storybook/addon-a11y",
    "@storybook/addon-docs",
    "@storybook/addon-onboarding"
  ],
  "framework": "@storybook/react-vite",

  // 新增的 viteFinal 配置
  async viteFinal(config) {
    return mergeConfig(config, {
      // 直接读取 tsconfig.json 中的别名配置
      plugins: [tsconfigPaths()],
      // optimizeDeps: {
      //   // 强制预构建 lucide-react,避免 ESM 加载报错
      //   include: ['lucide-react'], 
      // },
    });
  },
};
export default config;