Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/utils/resolve-module-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? "";
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions test/projects/project-ref/b/deep/package-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { PackageRootConst } from "#pkg/pkg-root";
export type { PackageRootType } from "#pkg/pkg-root";
5 changes: 5 additions & 0 deletions test/projects/project-ref/b/packages/pkg-root/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const PackageRootConst = "package-root";

export interface PackageRootType {
value: string;
}
4 changes: 4 additions & 0 deletions test/projects/project-ref/b/packages/pkg-root/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "#pkg/pkg-root",
"version": "1.0.0"
}
2 changes: 1 addition & 1 deletion test/projects/project-ref/b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 2 additions & 1 deletion test/projects/project-ref/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"baseUrl": ".",
"paths": {
"#a/*": ["./a/*"],
"#b/*": ["./b/*"]
"#b/*": ["./b/*"],
"#pkg/*": ["./b/packages/*"]
},
"plugins": [
{
Expand Down
6 changes: 6 additions & 0 deletions test/tests/project-ref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"/);
});
});