From cec9cb78d9fda437f7befed92435b7b4786d603c Mon Sep 17 00:00:00 2001 From: Lachlan Heywood Date: Sat, 16 Nov 2024 01:29:31 -0500 Subject: [PATCH] fix: add resolution step if alias is wildcard at root. Closes #330 --- src/transform.ts | 10 +++++++--- src/utils.ts | 27 ++++++++++++++++++++++++++ tests/__fixtures__/resolvePath.ts | 1 + tests/__fixtures__/test.resolvePath.ts | 1 + tests/__fixtures__/utils/test.data.ts | 1 + tests/transform.spec.ts | 8 ++++---- 6 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 tests/__fixtures__/resolvePath.ts create mode 100644 tests/__fixtures__/test.resolvePath.ts create mode 100644 tests/__fixtures__/utils/test.data.ts diff --git a/src/transform.ts b/src/transform.ts index a5bdf8a..afdc7d8 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -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' @@ -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 } } diff --git a/src/utils.ts b/src/utils.ts index 0fb1432..dddf152 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -438,6 +438,33 @@ export function parseTsAliases(basePath: string, paths: ts.MapLike) { 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', + '.jsx', + '.ts', + '.tsx', + '.json', + '.mjs', + '.cjs', + '.d.ts', + '.vue', + '.vue.d.ts' + ] + + 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) diff --git a/tests/__fixtures__/resolvePath.ts b/tests/__fixtures__/resolvePath.ts new file mode 100644 index 0000000..336ce12 --- /dev/null +++ b/tests/__fixtures__/resolvePath.ts @@ -0,0 +1 @@ +export {} diff --git a/tests/__fixtures__/test.resolvePath.ts b/tests/__fixtures__/test.resolvePath.ts new file mode 100644 index 0000000..336ce12 --- /dev/null +++ b/tests/__fixtures__/test.resolvePath.ts @@ -0,0 +1 @@ +export {} diff --git a/tests/__fixtures__/utils/test.data.ts b/tests/__fixtures__/utils/test.data.ts new file mode 100644 index 0000000..336ce12 --- /dev/null +++ b/tests/__fixtures__/utils/test.data.ts @@ -0,0 +1 @@ +export {} diff --git a/tests/transform.spec.ts b/tests/transform.spec.ts index c8993cd..17bd686 100644 --- a/tests/transform.spec.ts +++ b/tests/transform.spec.ts @@ -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') @@ -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', @@ -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" },