Skip to content

Commit

Permalink
fix: improve aliases replacement logic
Browse files Browse the repository at this point in the history
fix #294
  • Loading branch information
qmhc committed Jan 15, 2024
1 parent 9262d01 commit e8827cb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"paths": {
"@/*": ["./*"],
"@components/*": ["./src/components/*"],
"@alias/*": ["./alias/*"]
"$alias/*": ["./alias/*"]
},
"lib": ["esnext", "dom"]
},
Expand Down
17 changes: 11 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? '/' : '')
Expand Down
7 changes: 6 additions & 1 deletion tests/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit e8827cb

Please sign in to comment.