From 060f0e70914782cf68e9961f3bde636308fba844 Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Fri, 18 Apr 2025 15:05:11 +0800 Subject: [PATCH 1/3] chore: update --- packages/plugin-dts/src/tsc.ts | 55 ++++++++++++++++++- packages/plugin-dts/src/utils.ts | 5 +- .../auto-extension/rslib.config.ts | 6 ++ tests/integration/dts/index.test.ts | 12 ++-- 4 files changed, 67 insertions(+), 11 deletions(-) diff --git a/packages/plugin-dts/src/tsc.ts b/packages/plugin-dts/src/tsc.ts index 6baa04334..c4fa46dac 100644 --- a/packages/plugin-dts/src/tsc.ts +++ b/packages/plugin-dts/src/tsc.ts @@ -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, @@ -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, diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index c4e6ab626..a36cb4845 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -378,7 +378,7 @@ export async function processDtsFiles( ); } - const dtsFiles = await glob(convertPath(join(dir, '/**/*.d.ts')), { + const dtsFiles = await glob(convertPath(join(dir, '/**/*.d.{ts,cts,mts}')), { absolute: true, }); @@ -399,9 +399,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}`); } diff --git a/tests/integration/dts/bundle-false/auto-extension/rslib.config.ts b/tests/integration/dts/bundle-false/auto-extension/rslib.config.ts index f868f9b59..a0218a29e 100644 --- a/tests/integration/dts/bundle-false/auto-extension/rslib.config.ts +++ b/tests/integration/dts/bundle-false/auto-extension/rslib.config.ts @@ -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, }, }), diff --git a/tests/integration/dts/index.test.ts b/tests/integration/dts/index.test.ts index 0b5632639..345c007d3 100644 --- a/tests/integration/dts/index.test.ts +++ b/tests/integration/dts/index.test.ts @@ -90,10 +90,14 @@ describe('dts when bundle: false', () => { expect(files.cjs).toMatchInlineSnapshot(` [ - "/tests/integration/dts/bundle-false/auto-extension/dist/cjs/index.d.cts", - "/tests/integration/dts/bundle-false/auto-extension/dist/cjs/sum.d.cts", - "/tests/integration/dts/bundle-false/auto-extension/dist/cjs/utils/numbers.d.cts", - "/tests/integration/dts/bundle-false/auto-extension/dist/cjs/utils/strings.d.cts", + "/tests/integration/dts/bundle-false/auto-extension/dist/types/index.d.cts", + "/tests/integration/dts/bundle-false/auto-extension/dist/types/index.d.ts", + "/tests/integration/dts/bundle-false/auto-extension/dist/types/sum.d.cts", + "/tests/integration/dts/bundle-false/auto-extension/dist/types/sum.d.ts", + "/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/numbers.d.cts", + "/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/numbers.d.ts", + "/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/strings.d.cts", + "/tests/integration/dts/bundle-false/auto-extension/dist/types/utils/strings.d.ts", ] `); }); From a5c50b89c9d14c41e84287226c531b7677ecc7fd Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Fri, 18 Apr 2025 16:05:12 +0800 Subject: [PATCH 2/3] chore: update --- packages/plugin-dts/src/utils.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index a36cb4845..75143c446 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -378,9 +378,7 @@ export async function processDtsFiles( ); } - const dtsFiles = await glob(convertPath(join(dir, '/**/*.d.{ts,cts,mts}')), { - absolute: true, - }); + const dtsFiles = await globDtsFiles(dir); await Promise.all( dtsFiles.map(async (file) => { @@ -468,15 +466,19 @@ export async function calcLongestCommonPath( return lca; } -export async function cleanDtsFiles(dir: string): Promise { +export const globDtsFiles = async (dir: string): Promise => { 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 { + const allFiles = await globDtsFiles(dir); await Promise.all(allFiles.map((file) => fsP.rm(file, { force: true }))); } From fdffdcf8dee64fa745f428ab0aeee673e4b829ad Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Fri, 18 Apr 2025 16:54:02 +0800 Subject: [PATCH 3/3] chore: update --- pnpm-lock.yaml | 4 +++ .../dts/build/auto-extension/package.json | 6 ++++ .../dts/build/auto-extension/rslib.config.ts | 21 ++++++++++++++ .../dts/build/auto-extension/src/index.ts | 1 + .../dts/build/auto-extension/src/sum.ts | 10 +++++++ .../dts/build/auto-extension/tsconfig.json | 15 ++++++++++ .../dts/composite/auto-extension/package.json | 6 ++++ .../composite/auto-extension/rslib.config.ts | 20 +++++++++++++ .../composite/auto-extension/tsconfig.json | 9 ++++++ tests/integration/dts/index.test.ts | 29 +++++++++++++++++++ 10 files changed, 121 insertions(+) create mode 100644 tests/integration/dts/build/auto-extension/package.json create mode 100644 tests/integration/dts/build/auto-extension/rslib.config.ts create mode 100644 tests/integration/dts/build/auto-extension/src/index.ts create mode 100644 tests/integration/dts/build/auto-extension/src/sum.ts create mode 100644 tests/integration/dts/build/auto-extension/tsconfig.json create mode 100644 tests/integration/dts/composite/auto-extension/package.json create mode 100644 tests/integration/dts/composite/auto-extension/rslib.config.ts create mode 100644 tests/integration/dts/composite/auto-extension/tsconfig.json diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index de6e15e89..238686eab 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -640,6 +640,8 @@ importers: tests/integration/dts/build/abort-on-error: {} + tests/integration/dts/build/auto-extension: {} + tests/integration/dts/build/basic: {} tests/integration/dts/build/clean: {} @@ -698,6 +700,8 @@ importers: tests/integration/dts/composite/abort-on-error: {} + tests/integration/dts/composite/auto-extension: {} + tests/integration/dts/composite/basic: {} tests/integration/dts/composite/clean: {} diff --git a/tests/integration/dts/build/auto-extension/package.json b/tests/integration/dts/build/auto-extension/package.json new file mode 100644 index 000000000..0e9b8804e --- /dev/null +++ b/tests/integration/dts/build/auto-extension/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-build-auto-extension-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts/build/auto-extension/rslib.config.ts b/tests/integration/dts/build/auto-extension/rslib.config.ts new file mode 100644 index 000000000..a916d90ed --- /dev/null +++ b/tests/integration/dts/build/auto-extension/rslib.config.ts @@ -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/**'], + }, + }, +}); diff --git a/tests/integration/dts/build/auto-extension/src/index.ts b/tests/integration/dts/build/auto-extension/src/index.ts new file mode 100644 index 000000000..cf1f61676 --- /dev/null +++ b/tests/integration/dts/build/auto-extension/src/index.ts @@ -0,0 +1 @@ +export * from './sum'; diff --git a/tests/integration/dts/build/auto-extension/src/sum.ts b/tests/integration/dts/build/auto-extension/src/sum.ts new file mode 100644 index 000000000..bc0dd4eba --- /dev/null +++ b/tests/integration/dts/build/auto-extension/src/sum.ts @@ -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; diff --git a/tests/integration/dts/build/auto-extension/tsconfig.json b/tests/integration/dts/build/auto-extension/tsconfig.json new file mode 100644 index 000000000..cbbf57fe7 --- /dev/null +++ b/tests/integration/dts/build/auto-extension/tsconfig.json @@ -0,0 +1,15 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "baseUrl": "./", + "rootDir": "src", + "declaration": true, + "declarationDir": "./dist/types" + }, + "include": ["src"], + "references": [ + { + "path": "../__references__" + } + ] +} diff --git a/tests/integration/dts/composite/auto-extension/package.json b/tests/integration/dts/composite/auto-extension/package.json new file mode 100644 index 000000000..042a4ec6b --- /dev/null +++ b/tests/integration/dts/composite/auto-extension/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-composite-auto-extension-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts/composite/auto-extension/rslib.config.ts b/tests/integration/dts/composite/auto-extension/rslib.config.ts new file mode 100644 index 000000000..0985f019f --- /dev/null +++ b/tests/integration/dts/composite/auto-extension/rslib.config.ts @@ -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/**'], + }, + }, +}); diff --git a/tests/integration/dts/composite/auto-extension/tsconfig.json b/tests/integration/dts/composite/auto-extension/tsconfig.json new file mode 100644 index 000000000..4bbcc6074 --- /dev/null +++ b/tests/integration/dts/composite/auto-extension/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "../__fixtures__/src", + "baseUrl": "./", + "composite": true + }, + "include": ["../__fixtures__/src"] +} diff --git a/tests/integration/dts/index.test.ts b/tests/integration/dts/index.test.ts index 345c007d3..7a3b0b7ab 100644 --- a/tests/integration/dts/index.test.ts +++ b/tests/integration/dts/index.test.ts @@ -425,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(` + [ + "/tests/integration/dts/build/auto-extension/dist/types/index.d.cts", + "/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({ @@ -567,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(` + [ + "/tests/integration/dts/composite/auto-extension/dist/types/index.d.cts", + "/tests/integration/dts/composite/auto-extension/dist/types/sum.d.cts", + "/tests/integration/dts/composite/auto-extension/dist/types/utils/numbers.d.cts", + "/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({