-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pass vite alias paths to vue-docgen-api
- Loading branch information
1 parent
7100ab4
commit 064f527
Showing
6 changed files
with
52 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import type { InlineConfig } from 'vite'; | ||
|
||
function resolveReplacementPath(replacement: string, root?: string) { | ||
const isAbsolutePath = path.isAbsolute(replacement); | ||
|
||
if (isAbsolutePath && !fs.existsSync(replacement) && root) { | ||
const possiblePath = path.join(root, replacement); | ||
if (fs.existsSync(possiblePath)) return possiblePath; | ||
} | ||
|
||
return replacement; | ||
} | ||
|
||
/** | ||
* Resolve the alias from the vite config when the replacement path is relate to the root of vite server to generate an absolute path relate to the system | ||
* @example { '@': '/src' } => { '@': '/home/projects/vue3-vite/src' } | ||
*/ | ||
export function resolveAlias(config: InlineConfig) { | ||
const resolvedAlias: Record<string, string> = {}; | ||
const alias = config.resolve?.alias || {}; | ||
|
||
if (Array.isArray(alias)) { | ||
alias.forEach((item) => { | ||
resolvedAlias[item.find] = resolveReplacementPath(item.replacement, config.root); | ||
}); | ||
} else { | ||
Object.entries(alias).forEach(([find, replacement]) => { | ||
resolvedAlias[find] = resolveReplacementPath(replacement, config.root); | ||
}); | ||
} | ||
|
||
return resolvedAlias; | ||
} |