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..6df5890 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', @@ -158,6 +158,12 @@ describe('transform tests', () => { content: 'import { Sample } from "utils/test.data";', output: "import { Sample } from '../../../tests/__fixtures__/utils/test.data';\n" }, + { + description: 'wildcard alias at root, relative import', + filePath: './tests/__fixtures__/resolvePath.ts', + content: 'import { TestBase } from "resolvePath";', + output: "import { TestBase } from './resolvePath';\n" + }, { description: 'import inside folder with named alias at subfolder', content: 'import type { TestBase } from "@/components/test";', @@ -205,12 +211,6 @@ describe('transform tests', () => { content: 'import { utilFunction } from "@/utils/test";', output: "import { utilFunction } from '../../../utils/test';\n" }, - { - description: 'wildcard alias at root, relative import', - filePath: './tests/__fixtures__/resolvePath.ts', - content: 'import { TestBase } from "resolvePath";', - output: "import { TestBase } from './resolvePath';\n" - }, { description: 'wildcard alias at root, import is likely installed dependency', content: 'import { nothingReal } from "someDependency";',