vite-tsconfig-paths v5.0.1
👋 Check out Radashi, my latest endeavor.
vite-tsconfig-paths
Give vite the ability to resolve imports using TypeScript's path mapping.
Usage
Install as dev dependency
Ensure the project either has
"type": "module"set or that the Vite config is renamed tovite.config.mjs/vite.config.mtsdepending on whether TypeScript is usedInject
vite-tsconfig-pathsin the Vite configimport { defineConfig } from 'vite' import tsconfigPaths from 'vite-tsconfig-paths' export default defineConfig({ plugins: [tsconfigPaths()], })
Note: You need to restart Vite when you update your paths mappings.
Options
root: string
The directory to search fortsconfig.jsonfiles.The default value of this option depends on whether
projectsis defined. If it is, then the Vite project root is used. Otherwise, Vite'ssearchForWorkspaceRootfunction is used.projects: string[]
If you have an esoteric setup, you might need this option to specify where your tsconfig files are located. The paths within are relative to therootoption.If defined, the
rootdirectory won't be searched for tsconfig files. You should always try using just therootoption first, because this option is more brittle.loose: boolean
Disable strictness that limits path resolution to TypeScript and JavaScript importers.Useful if you want imports in Vue templates to be resolved, but don't want to use
allowJsin your tsconfig, for example.In other words, when
loose: trueis used, any file that gets transpiled into JavaScript will have its imports resolved by this plugin.parseNative: boolean
Enable use of thetsconfck.parseNativefunction, which delegates the loading of tsconfig files to the TypeScript compiler. You'll probably never need this, but I added it just in case.⚠️ This option can slow down Vite's startup time by as much as 600ms, due to the size of the TypeScript compiler. Only use it when necessary.
ignoreConfigErrors: boolean
When true, parsing errors encountered while loading tsconfig files will be ignored. This is useful if you have a monorepo with multiple tsconfig files, and you don't want to see errors for the ones that aren't relevant to the current project.
allowJs
If your tsconfig file has "allowJs": true in it, path resolution will be expanded beyond TypeScript importers. The following extensions will have their imports resolved by this plugin: .vue, .svelte, .mdx, .mjs, .js, .jsx
baseUrl
If the baseUrl is defined, it gets prepended to all bare imports, and its resolution will take precedence over node_modules. This is also how TypeScript does it.
Say the baseUrl is ../root and you import react. This plugin will use ../root/react if it exists. If not found, then react is resolved normally. The baseUrl is relative to the project root (where tsconfig.json lives).
include/exclude
The include and exclude tsconfig options are respected.
Internally, globrex is used for glob matching.
Troubleshooting
The DEBUG environment variable can be used to figure out why this plugin isn't working as you may have expected.
DEBUG=vite-tsconfig-paths yarn viteAlso, check out the Troubleshooting wiki page for more guidance.
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago