Skip to content

fix: rootDir calculation should ignore declaration files #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2024
Merged
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
10 changes: 9 additions & 1 deletion packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,17 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
throw new Error();
}
const { options: rawCompilerOptions, fileNames } = loadTsconfig(configPath);

// The longest common path of all non-declaration input files.
// If composite is set, the default is instead the directory containing the tsconfig.json file.
// see https://www.typescriptlang.org/tsconfig/#rootDir
const rootDir =
rawCompilerOptions.rootDir ??
(await calcLongestCommonPath(fileNames)) ??
(rawCompilerOptions.composite
? dirname(configPath)
: await calcLongestCommonPath(
fileNames.filter((fileName) => !/\.d\.(ts|mts|cts)$/.test(fileName)),
)) ??
dirname(configPath);

const outDir = distPath
Expand Down
38 changes: 38 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripts/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pxtorem
quxx
rebranded
rolldown
rootdir
rsbuild
rsdoctor
rsfamily
Expand Down
41 changes: 41 additions & 0 deletions tests/integration/dts/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,44 @@ export { }
",
}
`;

exports[`dts when bundle: true > rootdir calculation should ignore declaration files 3`] = `
{
"cjs": "export declare const num1 = 1;

export declare const num2 = 2;

export declare const num3 = 3;

export declare const numSum: number;

export declare const str1 = "str1";

export declare const str2 = "str2";

export declare const str3 = "str3";

export declare const strSum: string;

export { }
",
"esm": "export declare const num1 = 1;

export declare const num2 = 2;

export declare const num3 = 3;

export declare const numSum: number;

export declare const str1 = "str1";

export declare const str2 = "str2";

export declare const str3 = "str3";

export declare const strSum: string;

export { }
",
}
`;
9 changes: 9 additions & 0 deletions tests/integration/dts/bundle/rootdir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "dts-bundle-rootdir-test",
"version": "1.0.0",
"private": true,
"type": "module",
"devDependencies": {
"@types/chromecast-caf-sender": "^1.0.10"
}
}
22 changes: 22 additions & 0 deletions tests/integration/dts/bundle/rootdir/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
dts: {
bundle: true,
},
}),
generateBundleCjsConfig({
dts: {
bundle: true,
},
}),
],
source: {
entry: {
index: '../__fixtures__/src/index.ts',
},
},
});
4 changes: 4 additions & 0 deletions tests/integration/dts/bundle/rootdir/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@rslib/tsconfig/base",
"include": ["../__fixtures__/src", "node_modules/@types"]
}
22 changes: 22 additions & 0 deletions tests/integration/dts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,28 @@ describe('dts when bundle: true', () => {
`,
);
});

test('rootdir calculation should ignore declaration files', async () => {
const fixturePath = join(__dirname, 'bundle', 'rootdir');
const { files, entries } = await buildAndGetResults({
fixturePath,
type: 'dts',
});

expect(files.esm).toMatchInlineSnapshot(`
[
"<ROOT>/tests/integration/dts/bundle/rootdir/dist/esm/index.d.ts",
]
`);

expect(files.cjs).toMatchInlineSnapshot(`
[
"<ROOT>/tests/integration/dts/bundle/rootdir/dist/cjs/index.d.ts",
]
`);

expect(entries).toMatchSnapshot();
});
});

describe('dts when build: true', () => {
Expand Down
Loading