aleclarson / vite-tsconfig-paths

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

Use wrong tsconfig when exists multiple tsconfig files #115

Closed Singloo closed 1 year ago

Singloo commented 1 year ago

I'm using it in an nx mono repo containing many tsconfig files. When running storybook, I saw vite-tsconfig-paths scanned all config files but used the wrong one, which excluded *.stories.tsx files.

Is there a way to specify which tsconfig file to use?

tsconfig for storybook

{
"extends": "../tsconfig.json",
"compilerOptions": {
"emitDecoratorMetadata": true,
"outDir": ""
},
"files": [
"../../../node_modules/@nx/react/typings/styled-jsx.d.ts",
"../../../node_modules/@nx/react/typings/cssmodule.d.ts",
"../../../node_modules/@nx/react/typings/image.d.ts"
],
"exclude": [
"../**/*.spec.ts",
"../**/*.spec.js",
"../**/*.spec.tsx",
"../**/*.spec.jsx"
],
"include": [
"../src/**/*.stories.ts",
"../src/**/*.stories.js",
"../src/**/*.stories.jsx",
"../src/**/*.stories.tsx",
"../src/**/*.stories.mdx",
"*.js",
"preview.jsx"
]
}

tsconfig.lib.json, used for bundling, generated by nx

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "types": ["node", "vite/client"]
  },
  "files": [
    "../../node_modules/@nx/react/typings/cssmodule.d.ts",
    "../../node_modules/@nx/react/typings/image.d.ts"
  ],
  "exclude": [
    "**/*.spec.ts",
    "**/*.test.ts",
    "**/*.spec.tsx",
    "**/*.test.tsx",
    "**/*.spec.js",
    "**/*.test.js",
    "**/*.spec.jsx",
    "**/*.test.jsx",
    "**/*.stories.ts",
    "**/*.stories.js",
    "**/*.stories.jsx",
    "**/*.stories.tsx"
  ],
  "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}
aleclarson commented 1 year ago

Are you not in control of vite-tsconfig-paths options in your NX project?

You could try setting the projects option if you are.

o-alexandrov commented 1 year ago

Same issue. projects option doesn't help.

I haven't browsed the code in this pkg, but if the comment left in code is how it works, then the extends is buggy:

The extends property is supported.

aleclarson commented 1 year ago

Please include debug logs and a reproduction in your bug report.

DEBUG=vite-tsconfig-paths vite
o-alexandrov commented 1 year ago

Thank you for the reply. I think the use of a plugin could be completely replaced w/ a function below, it works for my needs:

export const getResolveConfig = (tsconfig: TsConfigJson) => {
  const resolveConfig = {
    alias: []
  }

  const paths = tsconfig.compilerOptions?.paths;
  if (!paths) return resolveConfig;

  const tsconfigAliasPaths = Object.keys(paths).map((key): Alias => {
    const value = paths[key][0];
    return {
      find: key.replace('/*', ''),
      replacement: resolve(value.replace('/*', '')),
    };
  });
  resolveConfig.alias = [...resolveConfig.alias, ...tsconfigAliasPaths];

  return resolveConfig;
};
aleclarson commented 1 year ago

There was a similar comment from @matthew-dean in #116. My response can be seen here: https://github.com/aleclarson/vite-tsconfig-paths/issues/116#issuecomment-1571068640

If you could repost your function in that thread, it wouldn't be off-topic. 👍

Singloo commented 1 year ago

Are you not in control of vite-tsconfig-paths options in your NX project?

You could try setting the projects option if you are.

Thanks for your reply. projects is not working for my case. Resolved by overwriting vite config alias