diff --git a/src/plugin.ts b/src/plugin.ts index dbe613d..74d773b 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -288,7 +288,13 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { : [ensureAbsolute(content?.raw.compilerOptions?.outDir || 'dist', root)] } - const { baseUrl, paths } = compilerOptions + const { + // Here we are using the default value to set the `baseUrl` to the current directory if no value exists. This is + // the same behavior as the TS Compiler. See TS source: + // https://github.com/microsoft/TypeScript/blob/3386e943215613c40f68ba0b108cda1ddb7faee1/src/compiler/utilities.ts#L6493-L6501 + baseUrl = compilerOptions.paths ? process.cwd() : undefined, + paths + } = compilerOptions if (pathsToAliases && baseUrl && paths) { aliases.push( diff --git a/src/utils.ts b/src/utils.ts index 85988cc..0fb1432 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -421,7 +421,7 @@ export function parseTsAliases(basePath: string, paths: ts.MapLike) { for (const [pathWithAsterisk, replacements] of Object.entries(paths)) { const find = new RegExp( - `^${pathWithAsterisk.replace(regexpSymbolRE, '\\$1').replace(asteriskRE, '(.+)')}$` + `^${pathWithAsterisk.replace(regexpSymbolRE, '\\$1').replace(asteriskRE, '(?!\\.{1,2}\\/)([^*]+)')}$` ) let index = 1 diff --git a/tests/transform.spec.ts b/tests/transform.spec.ts index a33ee1f..c5d8691 100644 --- a/tests/transform.spec.ts +++ b/tests/transform.spec.ts @@ -83,10 +83,14 @@ describe('transform tests', () => { it('test: transformCode (process aliases)', () => { const aliases: Alias[] = [ - { find: /^@\/(.+)/, replacement: resolve(__dirname, '../$1') }, - { find: /^@components\/(.+)/, replacement: resolve(__dirname, '../src/components/$1') }, + { find: /^@\/(?!\.{1,2}\/)([^*]+)/, replacement: resolve(__dirname, '../$1') }, + { + find: /^@components\/(?!\.{1,2}\/)([^*]+)/, + replacement: resolve(__dirname, '../src/components/$1') + }, { find: /^~\//, replacement: resolve(__dirname, '../src/') }, - { find: '$src', replacement: resolve(__dirname, '../src') } + { find: '$src', replacement: resolve(__dirname, '../src') }, + { find: /^(?!\.{1,2}\/)([^*]+)/, replacement: resolve(__dirname, '../src/$1') } ] const filePath = resolve(__dirname, '../src/index.ts') @@ -121,6 +125,31 @@ describe('transform tests', () => { content: 'import("@/components/test").Test;', output: "import('../components/test').Test;" }, + { + // https://github.com/qmhc/vite-plugin-dts/issues/330 + description: 'wildcard alias at root level with relative import', + filePath: './src/components/Sample/index.ts', + content: 'import { Sample } from "./Sample";', + output: "import { Sample } from './Sample';\n" + }, + { + description: 'wildcard alias at root level with relative import and dot in name', + filePath: './src/components/Sample/index.ts', + content: 'import { Sample } from "./test.data";', + output: "import { Sample } from './test.data';\n" + }, + { + description: 'wildcard alias at root level with relative parent import and dot in name', + filePath: './src/components/Sample/index.ts', + content: 'import { Sample } from "../test.data";', + output: "import { Sample } from '../test.data';\n" + }, + { + description: 'wildcard alias at root level with relative import and dot in name', + filePath: './src/components/Sample/index.ts', + content: 'import { Sample } from "utils/test.data";', + output: "import { Sample } from '../../utils/test.data';\n" + }, { description: 'import inside folder with named alias at subfolder', content: 'import type { TestBase } from "@/components/test";', @@ -170,7 +199,6 @@ describe('transform tests', () => { }, { description: 'alias as everything, relative import', - aliases: [{ find: /^(.+)$/, replacement: resolve(__dirname, '../src/$1') }], content: 'import { TestBase } from "test";', output: "import { TestBase } from './test';\n" } @@ -212,8 +240,24 @@ describe('transform tests', () => { ) expect(transformCode(options('import { TestBase } from "./test";')).content).toEqual( + "import { TestBase } from './test';\n" + ) + + expect(transformCode(options('import { TestBase } from "test";')).content).toEqual( "import { TestBase } from './utils/test';\n" ) + + expect(transformCode(options('import { TestBase } from "test.path";')).content).toEqual( + "import { TestBase } from './utils/test.path';\n" + ) + + expect(transformCode(options('import { TestBase } from "./test.path";')).content).toEqual( + "import { TestBase } from './test.path';\n" + ) + + expect(transformCode(options('import { TestBase } from "../test.path";')).content).toEqual( + "import { TestBase } from '../test.path';\n" + ) }) it('test: transformCode (remove pure imports)', () => { diff --git a/tests/utils.spec.ts b/tests/utils.spec.ts index 40c849d..4bc9cdb 100644 --- a/tests/utils.spec.ts +++ b/tests/utils.spec.ts @@ -168,14 +168,14 @@ describe('utils tests', () => { }) ).toStrictEqual([ { - find: /^@\/(.+)$/, + find: /^@\/(?!\.{1,2}\/)([^*]+)$/, replacement: expect.stringMatching(maybeWindowsPath('/tmp/fake/project/root/at/$1')) } ]) expect(parseTsAliases('/tmp/fake/project/root', { '~/*': ['./tilde/*'] })).toStrictEqual([ { - find: /^~\/(.+)$/, + find: /^~\/(?!\.{1,2}\/)([^*]+)$/, replacement: expect.stringMatching(maybeWindowsPath('/tmp/fake/project/root/tilde/$1')) } ]) @@ -184,7 +184,7 @@ describe('utils tests', () => { parseTsAliases('/tmp/fake/project/root', { '@/no-dot-prefix/*': ['no-dot-prefix/*'] }) ).toStrictEqual([ { - find: /^@\/no-dot-prefix\/(.+)$/, + find: /^@\/no-dot-prefix\/(?!\.{1,2}\/)([^*]+)$/, replacement: expect.stringMatching( maybeWindowsPath('/tmp/fake/project/root/no-dot-prefix/$1') ) @@ -195,7 +195,7 @@ describe('utils tests', () => { parseTsAliases('/tmp/fake/project/root', { '@/components/*': ['./at/components/*'] }) ).toStrictEqual([ { - find: /^@\/components\/(.+)$/, + find: /^@\/components\/(?!\.{1,2}\/)([^*]+)$/, replacement: expect.stringMatching( maybeWindowsPath('/tmp/fake/project/root/at/components/$1') ) @@ -204,7 +204,7 @@ describe('utils tests', () => { expect(parseTsAliases('/tmp/fake/project/root', { 'top/*': ['./top/*'] })).toStrictEqual([ { - find: /^top\/(.+)$/, + find: /^top\/(?!\.{1,2}\/)([^*]+)$/, replacement: expect.stringMatching(maybeWindowsPath('/tmp/fake/project/root/top/$1')) } ]) @@ -219,7 +219,7 @@ describe('utils tests', () => { // https://github.com/qmhc/vite-plugin-dts/issues/330 expect(parseTsAliases('/tmp/fake/project/root', { '*': ['./src/*'] })).toStrictEqual([ { - find: /^(.+)$/, + find: /^(?!\.{1,2}\/)([^*]+)$/, replacement: expect.stringMatching(maybeWindowsPath('/tmp/fake/project/root/src/$1')) } ]) @@ -227,7 +227,7 @@ describe('utils tests', () => { // https://github.com/qmhc/vite-plugin-dts/issues/290#issuecomment-1872495764 expect(parseTsAliases('/tmp/fake/project/root', { '#*': ['./hashed/*'] })).toStrictEqual([ { - find: /^#(.+)$/, + find: /^#(?!\.{1,2}\/)([^*]+)$/, replacement: expect.stringMatching(maybeWindowsPath('/tmp/fake/project/root/hashed/$1')) } ]) @@ -248,11 +248,8 @@ describe('utils tests', () => { }) it('test: getTsLibFolder', () => { - const root = normalizePath(resolve(__dirname, '..')) - const entryRoot = resolve(root, 'src') - - expect(getTsLibFolder({ root, entryRoot })).toMatch(/node_modules\/typescript$/) + expect(getTsLibFolder()).toMatch(/node_modules\/typescript$/) - expect(existsSync(getTsLibFolder({ root, entryRoot }) || '')).toBe(true) + expect(existsSync(getTsLibFolder() || '')).toBe(true) }) })