Skip to content

Commit 237b531

Browse files
committed
feat(volar): allow rootDir option
1 parent 812b566 commit 237b531

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

packages/router/src/volar/entries/sfc-typed-router.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,20 @@ import type ts from 'typescript'
1717
const plugin: VueLanguagePlugin = ({
1818
compilerOptions,
1919
modules: { typescript: ts },
20+
...options
2021
}) => {
22+
// rootDir comes from plugin options (vuejs/language-tools#5678)
23+
const optionsRootDir = (options as { rootDir?: string }).rootDir
24+
// Prioritize plugin options over tsconfig
25+
const rootDir = optionsRootDir ?? compilerOptions.rootDir
26+
27+
// Warn if no rootDir specified
28+
if (!rootDir) {
29+
console.warn(
30+
'[vue-router] No rootDir specified. Set it in the Volar plugin options or tsconfig compilerOptions.rootDir for proper typed routes.'
31+
)
32+
}
33+
2134
const RE = {
2235
DOLLAR_ROUTE: {
2336
/**
@@ -38,9 +51,7 @@ const plugin: VueLanguagePlugin = ({
3851
// TODO: Do we want to apply this to EVERY .vue file or only to components that the user wrote themselves?
3952

4053
// NOTE: this might not work if different from the root passed to VueRouter unplugin
41-
const relativeFilePath = compilerOptions.rootDir
42-
? relative(compilerOptions.rootDir, fileName)
43-
: fileName
54+
const relativeFilePath = rootDir ? relative(rootDir, fileName) : fileName
4455

4556
const useRouteNameType = `import('vue-router/auto-routes')._RouteNamesForFilePath<'${relativeFilePath}'>`
4657
const useRouteNameTypeParam = `<${useRouteNameType}>`

0 commit comments

Comments
 (0)