Skip to content

Commit

Permalink
fix: add resolution step if alias is wildcard at root. Closes qmhc#330
Browse files Browse the repository at this point in the history
  • Loading branch information
lachieh committed Dec 12, 2024
1 parent 26d42db commit a5978c0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dirname, isAbsolute, relative, resolve } from 'node:path'

import MagicString from 'magic-string'
import ts from 'typescript'
import { isRegExp, normalizePath } from './utils'
import { importResolves, isAliasGlobal, isRegExp, normalizePath } from './utils'

import type { Alias } from 'vite'

Expand Down Expand Up @@ -71,9 +71,13 @@ function transformAlias(
matchedAlias.find,
replacement + (endsWithSlash ? '/' : '')
)
const normalizedPath = normalizePath(relative(dir, resolve(dir, truthPath)))

return normalizedPath.startsWith('.') ? normalizedPath : `./${normalizedPath}`
const absolutePath = resolve(dir, truthPath)
const normalizedPath = normalizePath(relative(dir, absolutePath))
const resultPath = normalizedPath.startsWith('.') ? normalizedPath : `./${normalizedPath}`

if (!isAliasGlobal(matchedAlias)) return resultPath
if (importResolves(absolutePath)) return resultPath
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,41 @@ export function parseTsAliases(basePath: string, paths: ts.MapLike<string[]>) {
return result
}

const rootAsteriskImportRE = /^(?!\.{1,2}\/)([^*]+)$/
export function isAliasGlobal(alias: Alias) {
return alias.find.toString() === rootAsteriskImportRE.toString()
}

export function importResolves(path: string) {
const files = [
// js
'.js',
'.jsx',
'.mjs',
'.cjs',
// ts
'.ts',
'.tsx',
'.mts',
'.cts',
'.d.ts',
// json
'.json',
// vue
'.vue',
'.vue.d.ts',
// svelte
'.svelte'
]

for (const ext of files) {
if (existsSync(path + ext)) {
return true
}
}
return false
}

export function tryGetPackageInfo(name: string) {
if (process.versions.pnp) {
const targetRequire = createRequire(import.meta.url)
Expand Down
1 change: 1 addition & 0 deletions tests/__fixtures__/resolvePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
1 change: 1 addition & 0 deletions tests/__fixtures__/test.resolvePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
1 change: 1 addition & 0 deletions tests/__fixtures__/utils/test.data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
8 changes: 4 additions & 4 deletions tests/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('transform tests', () => {
{ find: /^~\//, replacement: resolve(__dirname, '../src/') },
// '$src/*' -> '/src/*'
{ find: '$src', replacement: resolve(__dirname, '../src') },
// '*' -> '/*'
// '*' -> '/tests/__fixtures__/*' (needs real path here to test if local file exists)
{
find: /^(?!\.{1,2}\/)([^*]+)$/,
replacement: resolve(__dirname, '../tests/__fixtures__/$1')
Expand Down Expand Up @@ -125,8 +125,8 @@ describe('transform tests', () => {
}> = [
{
description: 'type import alias at root level',
content: 'import type { TestBase } from "@/test";',
output: "import { TestBase } from '../test';\n"
content: 'import type { TestBase } from "@/src/test";',
output: "import { TestBase } from './test';\n"
},
{
description: 'dynamic import inside subfolder with alias at root level',
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('transform tests', () => {
},
{
description: 'wildcard alias at root, relative import',
filePath: './tests/__fixtures__/resolvePath.ts',
filePath: './tests/__fixtures__/index.ts',
content: 'import { TestBase } from "resolvePath";',
output: "import { TestBase } from './resolvePath';\n"
},
Expand Down

0 comments on commit a5978c0

Please sign in to comment.