Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
55 changes: 52 additions & 3 deletions packages/plugin-dts/src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,44 @@ export async function emitDts(
}
};

const system = { ...ts.sys };
const renameDtsFile = (fileName: string): string => {
if (bundle) {
return fileName;
}
return fileName.replace(/\.d\.ts$/, dtsExtension);
};

const system: ts.System = {
...ts.sys,
writeFile: (fileName, contents, writeByteOrderMark) => {
ts.sys.writeFile(renameDtsFile(fileName), contents, writeByteOrderMark);
},
};

// build mode
if (!isWatch) {
// normal build - npx tsc
if (!build && !compilerOptions.composite) {
const host: ts.CompilerHost = ts.createCompilerHost(compilerOptions);
const originHost: ts.CompilerHost =
ts.createCompilerHost(compilerOptions);
const host: ts.CompilerHost = {
...originHost,
writeFile: (
fileName,
contents,
writeByteOrderMark,
onError,
sourceFiles,
) => {
originHost.writeFile(
renameDtsFile(fileName),
contents,
writeByteOrderMark,
onError,
sourceFiles,
);
},
};

const program: ts.Program = ts.createProgram({
rootNames: fileNames,
Expand Down Expand Up @@ -202,8 +233,26 @@ export async function emitDts(
);
} else if (!build && compilerOptions.composite) {
// incremental build with composite true - npx tsc
const host: ts.CompilerHost =
const originHost: ts.CompilerHost =
ts.createIncrementalCompilerHost(compilerOptions);
const host: ts.CompilerHost = {
...originHost,
writeFile: (
fileName,
contents,
writeByteOrderMark,
onError,
sourceFiles,
) => {
originHost.writeFile(
renameDtsFile(fileName),
contents,
writeByteOrderMark,
onError,
sourceFiles,
);
},
};

const program = ts.createIncrementalProgram({
rootNames: fileNames,
Expand Down
17 changes: 8 additions & 9 deletions packages/plugin-dts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,7 @@ export async function processDtsFiles(
);
}

const dtsFiles = await glob(convertPath(join(dir, '/**/*.d.ts')), {
absolute: true,
});
const dtsFiles = await globDtsFiles(dir);

await Promise.all(
dtsFiles.map(async (file) => {
Expand All @@ -399,9 +397,6 @@ export async function processDtsFiles(
rootDir,
);
}

const newFile = file.replace('.d.ts', dtsExtension);
await fsP.rename(file, newFile);
} catch (error) {
logger.error(`Failed to rename declaration file ${file}: ${error}`);
}
Expand Down Expand Up @@ -471,15 +466,19 @@ export async function calcLongestCommonPath(
return lca;
}

export async function cleanDtsFiles(dir: string): Promise<void> {
export const globDtsFiles = async (dir: string): Promise<string[]> => {
const patterns = ['/**/*.d.ts', '/**/*.d.cts', '/**/*.d.mts'];
const files = await Promise.all(
const dtsFiles = await Promise.all(
patterns.map((pattern) =>
glob(convertPath(join(dir, pattern)), { absolute: true }),
),
);

const allFiles = files.flat();
return dtsFiles.flat();
};

export async function cleanDtsFiles(dir: string): Promise<void> {
const allFiles = await globDtsFiles(dir);

await Promise.all(allFiles.map((file) => fsP.rm(file, { force: true })));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ export default defineConfig({
lib: [
generateBundleEsmConfig({
bundle: false,
dts: {
autoExtension: true,
distPath: './dist/types',
bundle: false,
},
}),
generateBundleCjsConfig({
bundle: false,
dts: {
autoExtension: true,
distPath: './dist/types',
bundle: false,
},
}),
Expand Down
12 changes: 8 additions & 4 deletions tests/integration/dts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ describe('dts when bundle: false', () => {

expect(files.cjs).toMatchInlineSnapshot(`
[
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/cjs/index.d.cts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/cjs/sum.d.cts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/cjs/utils/numbers.d.cts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/cjs/utils/strings.d.cts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/index.d.cts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/index.d.ts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/sum.d.cts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/sum.d.ts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/numbers.d.cts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/numbers.d.ts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/strings.d.cts",
"<ROOT>/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/strings.d.ts",
]
`);
});
Expand Down
Loading