Skip to content

Commit

Permalink
feat: support parse paths of tsconfig.json to aliases (#236)
Browse files Browse the repository at this point in the history
* feat: support parse paths of tsconfig.json to aliases

* chore: update example

* docs: update apis
  • Loading branch information
qmhc committed Jul 8, 2023
1 parent 3b5a945 commit 68fc12a
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 10 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -169,6 +169,16 @@ export interface PluginOptions {
*/
resolvers?: Resolver[],

/**
* Parsing `paths` of tsconfig.json to aliases
*
* Note that these aliases only use for declaration files
*
* @default true
* @remarks Only use first replacement of each path
*/
pathsToAliases?: boolean,

/**
* Set which paths should be excluded when transforming aliases
*
Expand Down
10 changes: 10 additions & 0 deletions README.zh-CN.md
Expand Up @@ -169,6 +169,16 @@ export interface PluginOptions {
*/
resolvers?: Resolver[],

/**
* 解析 tsconfig.json 的 `paths` 作为别名
*
* 注意,这些别名仅用在类型文件中使用
*
* @default true
* @remarks 只使用每个路径的第一个替换
*/
pathsToAliases?: boolean,

/**
* 设置在转换别名时哪些路径需要排除
*
Expand Down
4 changes: 4 additions & 0 deletions examples/ts/src/alias/type.ts
@@ -0,0 +1,4 @@
export interface AliasType {
count: number,
name: string
}
1 change: 1 addition & 0 deletions examples/ts/src/index.ts
Expand Up @@ -17,3 +17,4 @@ export { ParametersTest, test, method } from './test'
export { data }

export type { User } from './types'
export type { AliasType } from '@alias/type'
3 changes: 2 additions & 1 deletion examples/ts/tsconfig.json
Expand Up @@ -19,7 +19,8 @@
],
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
"@/*": ["src/*"],
"@alias/*": ["src/alias/*"]
},
"lib": ["esnext", "dom"]
},
Expand Down
2 changes: 1 addition & 1 deletion examples/ts/vite.config.ts
Expand Up @@ -26,7 +26,7 @@ export default defineConfig({
exclude: ['src/ignore'],
// aliasesExclude: [/^@components/],
staticImport: true,
// rollupTypes: true,
rollupTypes: true,
insertTypesEntry: true
})
]
Expand Down
4 changes: 4 additions & 0 deletions examples/vue/alias/type.ts
@@ -0,0 +1,4 @@
export interface AliasType {
count: number,
name: string
}
2 changes: 2 additions & 0 deletions examples/vue/src/index.ts
Expand Up @@ -24,6 +24,8 @@ export { Decorator } from './decorator'
export type { User } from './types'
export type { DtsType } from './dts-types'

export type { AliasType } from '@alias/type'

export {
BothScripts,
JsTest,
Expand Down
5 changes: 3 additions & 2 deletions examples/vue/tsconfig.json
Expand Up @@ -21,9 +21,10 @@
"baseUrl": ".",
"paths": {
"@/*": ["./*"],
"@components/*": ["./src/components/*"]
"@components/*": ["./src/components/*"],
"@alias/*": ["./alias/*"]
},
"lib": ["esnext", "dom"]
},
"include": ["src", "components", "*.d.ts"]
"include": ["src", "components", "alias", "*.d.ts"]
}
33 changes: 27 additions & 6 deletions src/plugin.ts
Expand Up @@ -72,6 +72,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
insertTypesEntry = false,
rollupTypes = false,
bundledPackages = [],
pathsToAliases = true,
aliasesExclude = [],
copyDtsFiles = false,
strictOutput = true,
Expand Down Expand Up @@ -135,13 +136,13 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
aliases = aliases.filter(
({ find }) =>
!aliasesExclude.some(
alias =>
alias &&
aliasExclude =>
aliasExclude &&
(isRegExp(find)
? find.toString() === alias.toString()
: isRegExp(alias)
? find.match(alias)?.[0]
: find === alias)
? find.toString() === aliasExclude.toString()
: isRegExp(aliasExclude)
? find.match(aliasExclude)?.[0]
: find === aliasExclude)
)
)
}
Expand Down Expand Up @@ -252,6 +253,26 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
: [ensureAbsolute(content?.raw.compilerOptions?.outDir || 'dist', root)]
}

const { baseUrl, paths } = compilerOptions

if (pathsToAliases && baseUrl && paths) {
const basePath = ensureAbsolute(baseUrl, configPath ? dirname(configPath) : root)
const existsFinds = new Set(
aliases.map(alias => alias.find).filter(find => typeof find === 'string')
)

for (const [findWithAsterisk, replacements] of Object.entries(paths)) {
const find = findWithAsterisk.replace('/*', '')

if (!replacements.length || existsFinds.has(find)) continue

aliases.push({
find,
replacement: ensureAbsolute(replacements[0].replace('/*', ''), basePath)
})
}
}

include = ensureArray(options.include ?? content?.raw.include ?? '**/*').map(normalizeGlob)
exclude = ensureArray(options.exclude ?? content?.raw.exclude ?? 'node_modules/**').map(
normalizeGlob
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Expand Up @@ -88,6 +88,16 @@ export interface PluginOptions {
*/
resolvers?: Resolver[],

/**
* Parsing `paths` of tsconfig.json to aliases
*
* Note that these aliases only use for declaration files
*
* @default true
* @remarks Only use first replacement of each path
*/
pathsToAliases?: boolean,

/**
* Set which paths should be excluded when transforming aliases
*
Expand Down

0 comments on commit 68fc12a

Please sign in to comment.