diff --git a/src/utils/resolve-module-name.ts b/src/utils/resolve-module-name.ts index fb989dc..ac0758d 100755 --- a/src/utils/resolve-module-name.ts +++ b/src/utils/resolve-module-name.ts @@ -122,8 +122,10 @@ export function resolveModuleName(context: VisitorContext, moduleName: string): const resolvedSourceFile = getResolvedSourceFile(context, resolvedModule.resolvedFileName); - const { indexType, resolvedBaseNameNoExtension, resolvedFileName, implicitPackageIndex, extName, resolvedDir } = - getPathDetail(moduleName, resolvedModule); + const { indexType, resolvedBaseNameNoExtension, resolvedFileName, implicitPackageIndex, extName } = getPathDetail( + moduleName, + resolvedModule, + ); /* Determine output filename */ let outputBaseName = resolvedBaseNameNoExtension ?? ""; @@ -133,7 +135,12 @@ export function resolveModuleName(context: VisitorContext, moduleName: string): /* Determine output dir */ let srcFileOutputDir = getOutputDirForSourceFile(context, sourceFile); - let moduleFileOutputDir = implicitPackageIndex ? resolvedDir : getOutputDirForSourceFile(context, resolvedSourceFile); + let moduleFileOutputDir = getOutputDirForSourceFile(context, resolvedSourceFile); + + if (implicitPackageIndex) { + const implicitPackageDir = tsInstance.normalizePath(path.dirname(implicitPackageIndex)); + if (implicitPackageDir !== ".") moduleFileOutputDir = removeSuffix(moduleFileOutputDir, `/${implicitPackageDir}`); + } // Handle rootDirs remapping if (config.useRootDirs && rootDirs) { diff --git a/test/projects/project-ref/b/deep/package-root.ts b/test/projects/project-ref/b/deep/package-root.ts new file mode 100644 index 0000000..89ac49b --- /dev/null +++ b/test/projects/project-ref/b/deep/package-root.ts @@ -0,0 +1,2 @@ +export { PackageRootConst } from "#pkg/pkg-root"; +export type { PackageRootType } from "#pkg/pkg-root"; diff --git a/test/projects/project-ref/b/packages/pkg-root/index.ts b/test/projects/project-ref/b/packages/pkg-root/index.ts new file mode 100644 index 0000000..3c2592d --- /dev/null +++ b/test/projects/project-ref/b/packages/pkg-root/index.ts @@ -0,0 +1,5 @@ +export const PackageRootConst = "package-root"; + +export interface PackageRootType { + value: string; +} diff --git a/test/projects/project-ref/b/packages/pkg-root/package.json b/test/projects/project-ref/b/packages/pkg-root/package.json new file mode 100644 index 0000000..0f1aa21 --- /dev/null +++ b/test/projects/project-ref/b/packages/pkg-root/package.json @@ -0,0 +1,4 @@ +{ + "name": "#pkg/pkg-root", + "version": "1.0.0" +} diff --git a/test/projects/project-ref/b/tsconfig.json b/test/projects/project-ref/b/tsconfig.json index 9a61c6d..22cdfb3 100755 --- a/test/projects/project-ref/b/tsconfig.json +++ b/test/projects/project-ref/b/tsconfig.json @@ -1,5 +1,5 @@ { - "files": ["index.ts", "local/index.ts"], + "files": ["index.ts", "local/index.ts", "deep/package-root.ts", "packages/pkg-root/index.ts"], "extends": "../tsconfig.base.json", "compilerOptions": { "outDir": "../lib/b" diff --git a/test/projects/project-ref/tsconfig.base.json b/test/projects/project-ref/tsconfig.base.json index b8d0bfe..eb1dedb 100755 --- a/test/projects/project-ref/tsconfig.base.json +++ b/test/projects/project-ref/tsconfig.base.json @@ -10,7 +10,8 @@ "baseUrl": ".", "paths": { "#a/*": ["./a/*"], - "#b/*": ["./b/*"] + "#b/*": ["./b/*"], + "#pkg/*": ["./b/packages/*"] }, "plugins": [ { diff --git a/test/tests/project-ref.test.ts b/test/tests/project-ref.test.ts index 1f945db..ade1dfe 100755 --- a/test/tests/project-ref.test.ts +++ b/test/tests/project-ref.test.ts @@ -12,6 +12,7 @@ import { createTsSolutionBuilder, type EmittedFiles } from "../utils/index.ts"; /* File Paths */ const projectDir = ts.normalizePath(path.join(projectsPaths, "project-ref")); const indexFile = ts.normalizePath(path.join(projectDir, "lib/b/index.ts")); +const packageRootFile = ts.normalizePath(path.join(projectDir, "lib/b/deep/package-root.ts")); /* ****************************************************************************************************************** * * Tests @@ -35,4 +36,9 @@ describe(`Project References`, () => { t.assert.match(emittedFiles[indexFile].js, /export { LocalConst } from ".\/local\/index"/); t.assert.match(emittedFiles[indexFile].dts, /export { LocalConst } from ".\/local\/index"/); }); + + test(`Specifier for package root resolves to emitted output`, (t) => { + t.assert.match(emittedFiles[packageRootFile].js, /export { PackageRootConst } from "..\/packages\/pkg-root"/); + t.assert.match(emittedFiles[packageRootFile].dts, /export type { PackageRootType } from "..\/packages\/pkg-root"/); + }); });