diff --git a/README.md b/README.md index 3b54d4c..5c0fbc1 100644 --- a/README.md +++ b/README.md @@ -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 * diff --git a/README.zh-CN.md b/README.zh-CN.md index f500b9a..010ce1d 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -169,6 +169,16 @@ export interface PluginOptions { */ resolvers?: Resolver[], + /** + * 解析 tsconfig.json 的 `paths` 作为别名 + * + * 注意,这些别名仅用在类型文件中使用 + * + * @default true + * @remarks 只使用每个路径的第一个替换 + */ + pathsToAliases?: boolean, + /** * 设置在转换别名时哪些路径需要排除 * diff --git a/examples/ts/src/alias/type.ts b/examples/ts/src/alias/type.ts new file mode 100644 index 0000000..2704b87 --- /dev/null +++ b/examples/ts/src/alias/type.ts @@ -0,0 +1,4 @@ +export interface AliasType { + count: number, + name: string +} diff --git a/examples/ts/src/index.ts b/examples/ts/src/index.ts index 47f89be..7d638eb 100644 --- a/examples/ts/src/index.ts +++ b/examples/ts/src/index.ts @@ -17,3 +17,4 @@ export { ParametersTest, test, method } from './test' export { data } export type { User } from './types' +export type { AliasType } from '@alias/type' diff --git a/examples/ts/tsconfig.json b/examples/ts/tsconfig.json index 90cfc00..602c84b 100644 --- a/examples/ts/tsconfig.json +++ b/examples/ts/tsconfig.json @@ -19,7 +19,8 @@ ], "baseUrl": ".", "paths": { - "@/*": ["src/*"] + "@/*": ["src/*"], + "@alias/*": ["src/alias/*"] }, "lib": ["esnext", "dom"] }, diff --git a/examples/ts/vite.config.ts b/examples/ts/vite.config.ts index 295b6bd..f0d7e88 100644 --- a/examples/ts/vite.config.ts +++ b/examples/ts/vite.config.ts @@ -26,7 +26,7 @@ export default defineConfig({ exclude: ['src/ignore'], // aliasesExclude: [/^@components/], staticImport: true, - // rollupTypes: true, + rollupTypes: true, insertTypesEntry: true }) ] diff --git a/examples/vue/alias/type.ts b/examples/vue/alias/type.ts new file mode 100644 index 0000000..2704b87 --- /dev/null +++ b/examples/vue/alias/type.ts @@ -0,0 +1,4 @@ +export interface AliasType { + count: number, + name: string +} diff --git a/examples/vue/src/index.ts b/examples/vue/src/index.ts index fc32657..fd72a54 100644 --- a/examples/vue/src/index.ts +++ b/examples/vue/src/index.ts @@ -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, diff --git a/examples/vue/tsconfig.json b/examples/vue/tsconfig.json index f1e19c6..2f07344 100644 --- a/examples/vue/tsconfig.json +++ b/examples/vue/tsconfig.json @@ -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"] } diff --git a/src/plugin.ts b/src/plugin.ts index bdfaf92..be5b240 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -72,6 +72,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { insertTypesEntry = false, rollupTypes = false, bundledPackages = [], + pathsToAliases = true, aliasesExclude = [], copyDtsFiles = false, strictOutput = true, @@ -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) ) ) } @@ -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 diff --git a/src/types.ts b/src/types.ts index c3370de..b1ad936 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 *