Skip to content
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
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
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 6 additions & 0 deletions tests/integration/dts/build/auto-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dts-build-auto-extension-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
21 changes: 21 additions & 0 deletions tests/integration/dts/build/auto-extension/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleCjsConfig({
bundle: false,
dts: {
autoExtension: true,
distPath: './dist/types',
bundle: false,
build: true,
},
}),
],
source: {
entry: {
index: ['./src/**'],
},
},
});
1 change: 1 addition & 0 deletions tests/integration/dts/build/auto-extension/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './sum';
10 changes: 10 additions & 0 deletions tests/integration/dts/build/auto-extension/src/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const num1 = 1;
export const num2 = 2;
export const num3 = 3;

export const str1 = 'str1';
export const str2 = 'str2';
export const str3 = 'str3';

export const numSum = num1 + num2 + num3;
export const strSum = str1 + str2 + str3;
15 changes: 15 additions & 0 deletions tests/integration/dts/build/auto-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src",
"declaration": true,
"declarationDir": "./dist/types"
},
"include": ["src"],
"references": [
{
"path": "../__references__"
}
]
}
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
6 changes: 6 additions & 0 deletions tests/integration/dts/composite/auto-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dts-composite-auto-extension-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
20 changes: 20 additions & 0 deletions tests/integration/dts/composite/auto-extension/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleCjsConfig({
bundle: false,
dts: {
autoExtension: true,
distPath: './dist/types',
bundle: false,
},
}),
],
source: {
entry: {
index: ['../__fixtures__/src/**'],
},
},
});
9 changes: 9 additions & 0 deletions tests/integration/dts/composite/auto-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"rootDir": "../__fixtures__/src",
"baseUrl": "./",
"composite": true
},
"include": ["../__fixtures__/src"]
}
41 changes: 37 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 Expand Up @@ -421,6 +425,21 @@ describe('dts when build: true', () => {
expect(existsSync(buildInfoPath)).toBeTruthy();
});

test('autoExtension: true', async () => {
const fixturePath = join(__dirname, 'build', 'auto-extension');
const { files } = await buildAndGetResults({ fixturePath, type: 'dts' });

expect(files.cjs).toMatchInlineSnapshot(`
[
"<ROOT>/tests/integration/dts/build/auto-extension/dist/types/index.d.cts",
"<ROOT>/tests/integration/dts/build/auto-extension/dist/types/sum.d.cts",
]
`);

const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo');
expect(existsSync(buildInfoPath)).toBeTruthy();
});

test('process files - auto extension and banner / footer', async () => {
const fixturePath = join(__dirname, 'build', 'process-files');
const { contents } = await buildAndGetResults({
Expand Down Expand Up @@ -563,6 +582,20 @@ describe('dts when composite: true', () => {
expect(existsSync(buildInfoPath)).toBeTruthy();
});

test('autoExtension: true', async () => {
const fixturePath = join(__dirname, 'composite', 'auto-extension');
const { files } = await buildAndGetResults({ fixturePath, type: 'dts' });

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

test('process files - auto extension and banner / footer', async () => {
const fixturePath = join(__dirname, 'composite', 'process-files');
const { contents } = await buildAndGetResults({
Expand Down
Loading