From e8827cb6c8be1406e4e3a9a24639b7b54ca20d53 Mon Sep 17 00:00:00 2001 From: qmhc <40221744+qmhc@users.noreply.github.com> Date: Mon, 15 Jan 2024 16:14:43 +0800 Subject: [PATCH] fix: improve aliases replacement logic fix #294 --- examples/vue/src/index.ts | 2 +- examples/vue/tsconfig.json | 2 +- src/plugin.ts | 17 +++++++++++------ src/transform.ts | 5 ++++- tests/transform.spec.ts | 7 ++++++- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/examples/vue/src/index.ts b/examples/vue/src/index.ts index fd72a54..f76058a 100644 --- a/examples/vue/src/index.ts +++ b/examples/vue/src/index.ts @@ -24,7 +24,7 @@ export { Decorator } from './decorator' export type { User } from './types' export type { DtsType } from './dts-types' -export type { AliasType } from '@alias/type' +export type { AliasType } from '$alias/type' export { BothScripts, diff --git a/examples/vue/tsconfig.json b/examples/vue/tsconfig.json index 2f07344..cfda8b6 100644 --- a/examples/vue/tsconfig.json +++ b/examples/vue/tsconfig.json @@ -22,7 +22,7 @@ "paths": { "@/*": ["./*"], "@components/*": ["./src/components/*"], - "@alias/*": ["./alias/*"] + "$alias/*": ["./alias/*"] }, "lib": ["esnext", "dom"] }, diff --git a/src/plugin.ts b/src/plugin.ts index 27d6294..fe6171e 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -68,6 +68,9 @@ const fixedCompilerOptions: ts.CompilerOptions = { const noop = () => {} const extPrefix = (file: string) => (mtjsRE.test(file) ? 'm' : ctjsRE.test(file) ? 'c' : '') +const regexpSymbolRE = /([$.\\+?()[\]!<=|{}^,])/g +const asteriskRE = /[*]+/g + export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { const { tsconfigPath, @@ -283,18 +286,20 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { 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('/*', '') + const find = new RegExp( + findWithAsterisk.replace(regexpSymbolRE, '\\$1').replace(asteriskRE, '(.+)') + ) - if (!replacements.length || existsFinds.has(find)) continue + let index = 1 aliases.push({ find, - replacement: ensureAbsolute(replacements[0].replace('/*', ''), basePath) + replacement: ensureAbsolute( + replacements[0].replace(asteriskRE, () => `$${index++}`), + basePath + ) }) } } diff --git a/src/transform.ts b/src/transform.ts index 5ca983e..ca480f8 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -128,7 +128,10 @@ export function transformAliasImport( ? normalizePath(relative(dir, matchedAlias.replacement)) : normalizePath(matchedAlias.replacement) - const endSlash = matchResult[1].match(matchedAlias.find)![0].endsWith('/') + const endSlash = + typeof matchedAlias.find === 'string' + ? matchedAlias.find.endsWith('/') + : matchResult[1].match(matchedAlias.find)![0].endsWith('/') const truthPath = matchResult[1].replace( matchedAlias.find, replacement + (endSlash ? '/' : '') diff --git a/tests/transform.spec.ts b/tests/transform.spec.ts index d774d79..8240c87 100644 --- a/tests/transform.spec.ts +++ b/tests/transform.spec.ts @@ -64,7 +64,8 @@ describe('transform tests', () => { const aliases: Alias[] = [ { find: /^@\/(.+)/, replacement: resolve(__dirname, '../$1') }, { find: /^@components\/(.+)/, replacement: resolve(__dirname, '../src/components/$1') }, - { find: /^~\//, replacement: resolve(__dirname, '../src/') } + { find: /^~\//, replacement: resolve(__dirname, '../src/') }, + { find: '$src', replacement: resolve(__dirname, '../src') } ] const filePath = resolve(__dirname, '../src/index.ts') @@ -98,6 +99,10 @@ describe('transform tests', () => { expect( transformAliasImport(filePath, 'import type { TestBase } from "~/test";\n', aliases) ).toEqual("import type { TestBase } from './test';\n") + + expect( + transformAliasImport(filePath, 'import type { TestBase } from "$src/test"', aliases) + ).toEqual("import type { TestBase } from './test'") }) it('test: removePureImport', () => {