Skip to content

Commit

Permalink
fix: rootDir calculation should ignore declaration files
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Nov 8, 2024
1 parent 3f0c8cf commit e329398
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 1 deletion.
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

0 comments on commit e329398

Please sign in to comment.