aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.24k stars 43 forks source link

tsconfig absolute paths not working in storybook #65

Closed alirezaaminii closed 1 year ago

alirezaaminii commented 1 year ago

tsconfig.json:

{
  "include": ["src", "vite.config.ts"],
  "compilerOptions": {
    ...
    "baseUrl": "./src",
    "paths": {
      "~/*": ["./*"]
    }
  },
}

vite.config.ts: plugins: [react(), tsconfigPaths()],

code: import {icon} from '~/components/icons/icon';

Error: TypeError: Failed to fetch dynamically imported module: http://localhost:6006/src/components/button/index.stories.tsx

aleclarson commented 1 year ago

Have you reported this to Storybook?

alirezaaminii commented 1 year ago

@aleclarson

Have you reported this to Storybook?

No, but I will. Thanks for noticing.

Update: I opened an issue for storybook here

dqbd commented 1 year ago

For anyone who stumbles here, this .storybook/main.js fixed the issue for me:

const { mergeConfig } = require("vite")
const { default: tsconfigPaths } = require("vite-tsconfig-paths")

module.exports = {
  stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@storybook/addon-interactions",
  ],
  framework: "@storybook/react",
  core: {
    builder: "@storybook/builder-vite",
  },
  async viteFinal(config) {
    return mergeConfig(config, {
      plugins: [tsconfigPaths()],
    })
  },
}