From fcbef07223fbd68733e9e8f3c534105fc02e1c0a Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 15:55:46 +0800 Subject: [PATCH 01/14] feat(dts): support `dts.experiments.tsgo` --- packages/core/src/config.ts | 1 + packages/core/src/types/config.ts | 13 ++ packages/plugin-dts/package.json | 5 + packages/plugin-dts/src/dts.ts | 21 ++- packages/plugin-dts/src/index.ts | 7 +- packages/plugin-dts/src/tsc.ts | 73 ++++------ packages/plugin-dts/src/tsgo.ts | 232 ++++++++++++++++++++++++++++++ packages/plugin-dts/src/utils.ts | 52 ++++++- pnpm-lock.yaml | 81 +++++++++++ scripts/dictionary.txt | 1 + 10 files changed, 438 insertions(+), 48 deletions(-) create mode 100644 packages/plugin-dts/src/tsgo.ts diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index b79a86973..f7649a19d 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -1500,6 +1500,7 @@ const composeDtsConfig = async ( banner: banner?.dts, footer: footer?.dts, redirect: redirect?.dts, + experiments: dts?.experiments, }), ], }; diff --git a/packages/core/src/types/config.ts b/packages/core/src/types/config.ts index 6312f2a1f..ff1e00421 100644 --- a/packages/core/src/types/config.ts +++ b/packages/core/src/types/config.ts @@ -94,6 +94,19 @@ export type Dts = * @see {@link https://rslib.rs/config/lib/dts#dtsalias} */ alias?: Record; + /** + * [Experimental] Whether to enable experimental features. + * @defaultValue `{}` + * @see {@link https://rslib.rs/config/lib/dts#dtsexperiments} + */ + experiments?: { + /** + * [Experimental] Whether to generate declaration files with the `tsgo` compiler. + * @defaultValue `false` + * @see {@link https://rslib.rs/config/lib/dts#dtsexperimentstsgo} + */ + tsgo?: boolean; + }; } | boolean; diff --git a/packages/plugin-dts/package.json b/packages/plugin-dts/package.json index 246da0c4c..4cca304ba 100644 --- a/packages/plugin-dts/package.json +++ b/packages/plugin-dts/package.json @@ -40,6 +40,7 @@ "@microsoft/api-extractor": "^7.52.11", "@rsbuild/core": "~1.5.3", "@rslib/tsconfig": "workspace:*", + "@typescript/native-preview": "7.0.0-dev.20250903.1", "rsbuild-plugin-publint": "^0.3.3", "rslib": "npm:@rslib/core@0.12.4", "typescript": "^5.9.2" @@ -47,12 +48,16 @@ "peerDependencies": { "@microsoft/api-extractor": "^7", "@rsbuild/core": "1.x", + "@typescript/native-preview": "7.x", "typescript": "^5" }, "peerDependenciesMeta": { "@microsoft/api-extractor": { "optional": true }, + "@typescript/native-preview": { + "optional": true + }, "typescript": { "optional": true } diff --git a/packages/plugin-dts/src/dts.ts b/packages/plugin-dts/src/dts.ts index 62eb4013c..8313a2127 100644 --- a/packages/plugin-dts/src/dts.ts +++ b/packages/plugin-dts/src/dts.ts @@ -11,7 +11,6 @@ import { import { logger } from '@rsbuild/core'; import color from 'picocolors'; import type { DtsEntry, DtsGenOptions } from './index'; -import { emitDts } from './tsc'; import { calcLongestCommonPath, ensureTempDeclarationDir, @@ -136,7 +135,11 @@ export async function generateDts(data: DtsGenOptions): Promise { path: true, extension: false, }, + experiments = { + tsgo: false, + }, } = data; + const { tsgo } = experiments; if (!isWatch) { logger.start(`generating declaration files... ${color.dim(`(${name})`)}`); } @@ -248,7 +251,11 @@ export async function generateDts(data: DtsGenOptions): Promise { } }; - await emitDts( + const emitDts = tsgo + ? await import('./tsgo').then((mod) => mod.emitDtsTsgo) + : await import('./tsc').then((mod) => mod.emitDtsTsc); + + const hasError = await emitDts( { name, cwd, @@ -268,8 +275,14 @@ export async function generateDts(data: DtsGenOptions): Promise { build, ); - if (!isWatch) { - await bundleDtsIfNeeded(); + if (tsgo) { + if (!hasError) { + await bundleDtsIfNeeded(); + } + } else { + if (!isWatch) { + await bundleDtsIfNeeded(); + } } } diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index 443b4b7f2..437dc8092 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -44,6 +44,9 @@ export type PluginDtsOptions = { banner?: string; footer?: string; redirect?: DtsRedirect; + experiments?: { + tsgo?: boolean; + }; }; export type DtsEntry = { @@ -93,6 +96,8 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ options.redirect.path = options.redirect.path ?? true; options.redirect.extension = options.redirect.extension ?? false; options.alias = options.alias ?? {}; + options.experiments = options.experiments ?? {}; + options.experiments.tsgo = options.experiments.tsgo ?? false; const dtsPromises: Promise[] = []; let promisesResult: TaskResult[] = []; @@ -100,7 +105,7 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ api.onBeforeEnvironmentCompile( async ({ isWatch, isFirstCompile, environment }) => { - if (!isFirstCompile) { + if (!isFirstCompile && !options.experiments?.tsgo) { return; } diff --git a/packages/plugin-dts/src/tsc.ts b/packages/plugin-dts/src/tsc.ts index 40cc13d26..9bf9e1545 100644 --- a/packages/plugin-dts/src/tsc.ts +++ b/packages/plugin-dts/src/tsc.ts @@ -2,7 +2,12 @@ import { logger } from '@rsbuild/core'; import color from 'picocolors'; import ts from 'typescript'; import type { DtsRedirect } from './index'; -import { getTimeCost, processDtsFiles } from './utils'; +import { + getTimeCost, + processDtsFiles, + renameDtsFile, + updateDeclarationMapContent, +} from './utils'; const logPrefixTsc = color.dim('[tsc]'); @@ -75,7 +80,7 @@ async function handleDiagnosticsAndProcessFiles( } } -export async function emitDts( +export async function emitDtsTsc( options: EmitDtsOptions, onComplete: (isSuccess: boolean) => void, bundle = false, @@ -174,45 +179,17 @@ export async function emitDts( } }; - const renameDtsFile = (fileName: string): string => { - if (bundle) { - return fileName; - } - - if (fileName.endsWith('.d.ts.map')) { - return fileName.replace(/\.d\.ts\.map$/, `${dtsExtension}.map`); - } - - return fileName.replace(/\.d\.ts$/, dtsExtension); - }; - - const updateDeclarationMapContent = ( - fileName: string, - content: string, - ): string => { - if (bundle || !compilerOptions.declarationMap) { - return content; - } - - if (fileName.endsWith('.d.ts')) { - return content.replace( - /(\/\/# sourceMappingURL=.+)\.d\.ts\.map/g, - `$1${dtsExtension}.map`, - ); - } - - if (fileName.endsWith('.d.ts.map')) { - return content.replace(/("file":"[^"]*)\.d\.ts"/g, `$1${dtsExtension}"`); - } - - return content; - }; - const system: ts.System = { ...ts.sys, writeFile: (fileName, contents, writeByteOrderMark) => { - const newFileName = renameDtsFile(fileName); - const newContents = updateDeclarationMapContent(fileName, contents); + const newFileName = renameDtsFile(fileName, dtsExtension, bundle); + const newContents = updateDeclarationMapContent( + fileName, + contents, + dtsExtension, + bundle, + compilerOptions.declarationMap, + ); ts.sys.writeFile(newFileName, newContents, writeByteOrderMark); }, }; @@ -232,8 +209,14 @@ export async function emitDts( onError, sourceFiles, ) => { - const newFileName = renameDtsFile(fileName); - const newContents = updateDeclarationMapContent(fileName, contents); + const newFileName = renameDtsFile(fileName, dtsExtension, bundle); + const newContents = updateDeclarationMapContent( + fileName, + contents, + dtsExtension, + bundle, + compilerOptions.declarationMap, + ); originHost.writeFile( newFileName, newContents, @@ -285,8 +268,14 @@ export async function emitDts( onError, sourceFiles, ) => { - const newFileName = renameDtsFile(fileName); - const newContents = updateDeclarationMapContent(fileName, contents); + const newFileName = renameDtsFile(fileName, dtsExtension, bundle); + const newContents = updateDeclarationMapContent( + fileName, + contents, + dtsExtension, + bundle, + compilerOptions.declarationMap, + ); originHost.writeFile( newFileName, newContents, diff --git a/packages/plugin-dts/src/tsgo.ts b/packages/plugin-dts/src/tsgo.ts new file mode 100644 index 000000000..b8b48f43e --- /dev/null +++ b/packages/plugin-dts/src/tsgo.ts @@ -0,0 +1,232 @@ +import { spawn } from 'node:child_process'; +import fs from 'node:fs'; +import { createRequire } from 'node:module'; +import path from 'node:path'; +import { logger } from '@rsbuild/core'; +import color from 'picocolors'; +import ts from 'typescript'; +import type { DtsRedirect } from './index'; +import type { EmitDtsOptions } from './tsc'; +import { + getTimeCost, + globDtsFiles, + processDtsFiles, + renameDtsFile, + updateDeclarationMapContent, +} from './utils'; + +const require = createRequire(import.meta.url); + +const logPrefixTsgo = color.dim('[tsgo]'); + +const getTsgoBinPath = async (): Promise => { + const tsgoPkgPath = require.resolve( + '@typescript/native-preview/package.json', + ); + + const libPath = path.resolve( + path.dirname(tsgoPkgPath), + './lib/getExePath.js', + ); + + return import(libPath).then((mod) => { + const getExePath = mod.default; + return getExePath(); + }); +}; + +const generateTsgoArgs = ( + configPath: string, + declarationDir: string, + build: boolean, + isWatch: boolean, +): string[] => { + const args: string[] = []; + + if (build) { + args.push('--build', configPath); + } else { + args.push('--project', configPath); + args.push('--declarationDir', declarationDir); + } + + args.push('--noEmit', 'false'); + args.push('--declaration'); + args.push('--emitDeclarationOnly'); + + if (isWatch) { + // rebuild when watch since watch mode is proof-of-concept only currently in tsgo + // args.push('--watch'); + } + + return args; +}; + +async function handleDiagnosticsAndProcessFiles( + isWatch: boolean, + hasErrors: boolean, + tsConfigResult: ts.ParsedCommandLine, + configPath: string, + bundle: boolean, + declarationDir: string, + dtsExtension: string, + redirect: DtsRedirect, + rootDir: string, + paths: Record, + banner?: string, + footer?: string, + name?: string, +): Promise { + await processDtsFiles( + bundle, + declarationDir, + dtsExtension, + redirect, + configPath, + rootDir, + paths, + banner, + footer, + ); + + if (!bundle) { + const dtsFiles = await globDtsFiles(declarationDir, [ + '/**/*.d.ts', + '/**/*.d.ts.map', + ]); + await Promise.all( + dtsFiles.map(async (file) => { + const contents = ts.sys.readFile(file) ?? ''; + const newFileName = renameDtsFile(file, dtsExtension, bundle); + const newContents = updateDeclarationMapContent( + file, + contents, + dtsExtension, + bundle, + tsConfigResult.options.declarationMap, + ); + if (file !== newFileName || contents !== newContents) { + ts.sys.writeFile(newFileName, newContents); + if (ts.sys.deleteFile) { + ts.sys.deleteFile(file); + } else { + fs.unlinkSync(file); + } + } + }), + ); + } + + if (hasErrors && !isWatch) { + const error = new Error( + `Failed to generate declaration files. ${color.dim(`(${name})`)}`, + ); + // do not log the stack trace, diagnostic messages are enough + error.stack = ''; + throw error; + } +} + +export async function emitDtsTsgo( + options: EmitDtsOptions, + _onComplete: (isSuccess: boolean) => void, + bundle = false, + isWatch = false, + build = false, +): Promise { + const start = Date.now(); + const { + configPath, + tsConfigResult, + declarationDir, + name, + dtsExtension, + rootDir, + banner, + footer, + paths, + redirect, + cwd, + } = options; + + const tsgoBinFile = await getTsgoBinPath(); + const args = generateTsgoArgs(configPath, declarationDir, build, isWatch); + + logger.debug(logPrefixTsgo, `Running: ${tsgoBinFile} ${args.join(' ')}`); + + return new Promise((resolve, reject) => { + const childProcess = spawn( + tsgoBinFile, + [ + ...args, + /* Required parameter, use it stdout have color */ + '--pretty', + ], + { + cwd, + stdio: ['inherit', 'pipe', 'pipe'], + }, + ); + + let hasErrors = false; + + childProcess.stdout?.on('data', (data) => { + const output = data.toString(); + const lines = output.split('\n'); + for (const line of lines) { + if (line.trim()) { + // Reset color for each line to avoid color bleed + console.log(color.reset(`${logPrefixTsgo} ${line}`)); + } + } + }); + + childProcess.stderr?.on('data', (data) => { + const output = data.toString(); + const lines = output.split('\n').filter((line: string) => line.trim()); + for (const line of lines) { + logger.error(logPrefixTsgo, line); + } + }); + + childProcess.on('close', async (code) => { + try { + if (code !== 0) { + hasErrors = true; + } + + await handleDiagnosticsAndProcessFiles( + isWatch, + hasErrors, + tsConfigResult, + configPath, + bundle, + declarationDir, + dtsExtension, + redirect, + rootDir, + paths, + banner, + footer, + name, + ); + + if (!hasErrors) { + if (bundle) { + logger.info( + `declaration files prepared with tsgo in ${getTimeCost(start)} ${color.dim(`(${name})`)}`, + ); + } else { + logger.ready( + `declaration files generated with tsgo in ${getTimeCost(start)} ${color.dim(`(${name})`)}`, + ); + } + } + + resolve(hasErrors); + } catch (error) { + reject(error); + } + }); + }); +} diff --git a/packages/plugin-dts/src/utils.ts b/packages/plugin-dts/src/utils.ts index 1b022dd96..23126694b 100644 --- a/packages/plugin-dts/src/utils.ts +++ b/packages/plugin-dts/src/utils.ts @@ -584,7 +584,10 @@ export const globDtsFiles = async ( ): Promise => { const dtsFiles = await Promise.all( patterns.map(async (pattern) => - glob(convertPath(join(dir, pattern)), { absolute: true }), + glob(convertPath(join(dir, pattern)), { + absolute: true, + dot: true, + }), ), ); @@ -662,3 +665,50 @@ export function warnIfOutside( } } } + +/** + * Rename .d.ts and .d.ts.map files with corresponding extension + */ +export function renameDtsFile( + fileName: string, + dtsExtension: string, + bundle: boolean, +): string { + if (bundle) { + return fileName; + } + + if (fileName.endsWith('.d.ts.map')) { + return fileName.replace(/\.d\.ts\.map$/, `${dtsExtension}.map`); + } + + return fileName.replace(/\.d\.ts$/, dtsExtension); +} + +/** + * Update source map content for declaration files + */ +export function updateDeclarationMapContent( + fileName: string, + content: string, + dtsExtension: string, + bundle: boolean, + hasDeclarationMap = false, +): string { + if (bundle || !hasDeclarationMap) { + return content; + } + + if (fileName.endsWith('.d.ts')) { + return content.replace( + /(\/\/# sourceMappingURL=.+)\.d\.ts\.map/g, + `$1${dtsExtension}.map`, + ); + } + + if (fileName.endsWith('.d.ts.map')) { + return content.replace(/("file":"[^"]*)\.d\.ts"/g, `$1${dtsExtension}"`); + } + + return content; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e4c8b48e2..62c52a9a6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -450,6 +450,9 @@ importers: '@rslib/tsconfig': specifier: workspace:* version: link:../../scripts/tsconfig + '@typescript/native-preview': + specifier: 7.0.0-dev.20250903.1 + version: 7.0.0-dev.20250903.1 rsbuild-plugin-publint: specifier: ^0.3.3 version: 0.3.3(@rsbuild/core@1.5.3) @@ -3285,6 +3288,53 @@ packages: '@types/unist@3.0.3': resolution: {integrity: sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==} + '@typescript/native-preview-darwin-arm64@7.0.0-dev.20250903.1': + resolution: {integrity: sha512-acoMJ+HRAvuDsHqWIW2lIGG8cqBdkN8SdjB2z4QKMmIGkGkN0iJFFNXhsxZ7UbV2j1XEghTIxlVA4XrsUoS7EA==} + engines: {node: '>=20.6.0'} + cpu: [arm64] + os: [darwin] + + '@typescript/native-preview-darwin-x64@7.0.0-dev.20250903.1': + resolution: {integrity: sha512-/+SFZCUARYmamR9VgCEdNaq6g09yd/5nhuD07W7WG5OtGYJYLn79k0/bRnJb8u2yPuuebzNjOc86QqaKsaGFcQ==} + engines: {node: '>=20.6.0'} + cpu: [x64] + os: [darwin] + + '@typescript/native-preview-linux-arm64@7.0.0-dev.20250903.1': + resolution: {integrity: sha512-DKtsfyk43YLtAS1/RdTOMOfQj/loQhFVHXuysIlVc56PDD7b7hwgQ3phkhNgJZpKPgTfpn/eM4CD5rgHLdIduQ==} + engines: {node: '>=20.6.0'} + cpu: [arm64] + os: [linux] + + '@typescript/native-preview-linux-arm@7.0.0-dev.20250903.1': + resolution: {integrity: sha512-zqK6cJk2D+iE1prHwH16Ko5GsMsloKVJ+M0kiwyTrkjT2w6QcgpthEU8IMdOCBi9d9pXujURz1UufkXHAh02VA==} + engines: {node: '>=20.6.0'} + cpu: [arm] + os: [linux] + + '@typescript/native-preview-linux-x64@7.0.0-dev.20250903.1': + resolution: {integrity: sha512-USEhjORkiLKsgL1ogk09yn5XpK+FoKQqrqdNUQlc/zHhJPf/oHsaU0EmFTxLhPoBYcaxoZ0UBfQR5i/viGB1iQ==} + engines: {node: '>=20.6.0'} + cpu: [x64] + os: [linux] + + '@typescript/native-preview-win32-arm64@7.0.0-dev.20250903.1': + resolution: {integrity: sha512-iiHE1yZeDBPrJrlRYs6geBdV/MDu0BZGK1Yh93A9ofBymQpr3DXYNysqb5kEBy6ei2KTPJUu4YNAVzu9UoMVfQ==} + engines: {node: '>=20.6.0'} + cpu: [arm64] + os: [win32] + + '@typescript/native-preview-win32-x64@7.0.0-dev.20250903.1': + resolution: {integrity: sha512-2yf56TIhsPbjYZz0XT4+hOFz9jetqlADkFuIid0dpe/0HLAliIMOHRAMZj22PerYU5Qcg66llJ6xU+78evBZCw==} + engines: {node: '>=20.6.0'} + cpu: [x64] + os: [win32] + + '@typescript/native-preview@7.0.0-dev.20250903.1': + resolution: {integrity: sha512-8EnxWQp7wUfk6mV3Bw7aGLQwKqDwi6+YLqm4Sev+KRpiUyepHZKWGej/YTLko3WMd0uGoGAlODuZjoPBQNJdag==} + engines: {node: '>=20.6.0'} + hasBin: true + '@typescript/vfs@1.6.1': resolution: {integrity: sha512-JwoxboBh7Oz1v38tPbkrZ62ZXNHAk9bJ7c9x0eI5zBfBnBYGhURdbnh7Z4smN/MV48Y5OCcZb58n972UtbazsA==} peerDependencies: @@ -10080,6 +10130,37 @@ snapshots: '@types/unist@3.0.3': {} + '@typescript/native-preview-darwin-arm64@7.0.0-dev.20250903.1': + optional: true + + '@typescript/native-preview-darwin-x64@7.0.0-dev.20250903.1': + optional: true + + '@typescript/native-preview-linux-arm64@7.0.0-dev.20250903.1': + optional: true + + '@typescript/native-preview-linux-arm@7.0.0-dev.20250903.1': + optional: true + + '@typescript/native-preview-linux-x64@7.0.0-dev.20250903.1': + optional: true + + '@typescript/native-preview-win32-arm64@7.0.0-dev.20250903.1': + optional: true + + '@typescript/native-preview-win32-x64@7.0.0-dev.20250903.1': + optional: true + + '@typescript/native-preview@7.0.0-dev.20250903.1': + optionalDependencies: + '@typescript/native-preview-darwin-arm64': 7.0.0-dev.20250903.1 + '@typescript/native-preview-darwin-x64': 7.0.0-dev.20250903.1 + '@typescript/native-preview-linux-arm': 7.0.0-dev.20250903.1 + '@typescript/native-preview-linux-arm64': 7.0.0-dev.20250903.1 + '@typescript/native-preview-linux-x64': 7.0.0-dev.20250903.1 + '@typescript/native-preview-win32-arm64': 7.0.0-dev.20250903.1 + '@typescript/native-preview-win32-x64': 7.0.0-dev.20250903.1 + '@typescript/vfs@1.6.1(typescript@5.9.2)': dependencies: debug: 4.4.1 diff --git a/scripts/dictionary.txt b/scripts/dictionary.txt index 481c9b9ae..59e1114d0 100644 --- a/scripts/dictionary.txt +++ b/scripts/dictionary.txt @@ -138,6 +138,7 @@ treeshaking tsbuildinfo tsconfck tsdoc +tsgo tsup Twoslash unencapsulated From 6cac264aa2d602e51677bbd9fbc4c7f66b97f4dd Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 16:40:18 +0800 Subject: [PATCH 02/14] test: redirect dts with tsgo --- packages/plugin-dts/src/tsgo.ts | 24 +- pnpm-lock.yaml | 15 + .../dts-tsgo/compile/prebundle-pkg/index.d.ts | 4 + .../dts-tsgo/compile/prebundle-pkg/index.js | 3 + .../compile/prebundle-pkg/package.json | 5 + .../redirect/dts-tsgo/package.json | 14 + .../redirect/dts-tsgo/rslib.config.ts | 113 ++++++ .../dts-tsgo/src/.hidden-folder/index.ts | 1 + .../redirect/dts-tsgo/src/.hidden.ts | 1 + .../redirect/dts-tsgo/src/a.b/index.ts | 1 + .../redirect/dts-tsgo/src/bar.baz.ts | 1 + .../redirect/dts-tsgo/src/foo/foo.ts | 5 + .../redirect/dts-tsgo/src/foo/index.ts | 1 + .../redirect/dts-tsgo/src/index.ts | 29 ++ .../redirect/dts-tsgo/src/logger.ts | 25 ++ .../redirect/dts-tsgo/src/types.ts | 21 + .../redirect/dts-tsgo/tsconfig.json | 14 + tests/integration/redirect/dtsTsgo.test.ts | 373 ++++++++++++++++++ 18 files changed, 638 insertions(+), 12 deletions(-) create mode 100644 tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/index.d.ts create mode 100644 tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/index.js create mode 100644 tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/package.json create mode 100644 tests/integration/redirect/dts-tsgo/package.json create mode 100644 tests/integration/redirect/dts-tsgo/rslib.config.ts create mode 100644 tests/integration/redirect/dts-tsgo/src/.hidden-folder/index.ts create mode 100644 tests/integration/redirect/dts-tsgo/src/.hidden.ts create mode 100644 tests/integration/redirect/dts-tsgo/src/a.b/index.ts create mode 100644 tests/integration/redirect/dts-tsgo/src/bar.baz.ts create mode 100644 tests/integration/redirect/dts-tsgo/src/foo/foo.ts create mode 100644 tests/integration/redirect/dts-tsgo/src/foo/index.ts create mode 100644 tests/integration/redirect/dts-tsgo/src/index.ts create mode 100644 tests/integration/redirect/dts-tsgo/src/logger.ts create mode 100644 tests/integration/redirect/dts-tsgo/src/types.ts create mode 100644 tests/integration/redirect/dts-tsgo/tsconfig.json create mode 100644 tests/integration/redirect/dtsTsgo.test.ts diff --git a/packages/plugin-dts/src/tsgo.ts b/packages/plugin-dts/src/tsgo.ts index b8b48f43e..a23405868 100644 --- a/packages/plugin-dts/src/tsgo.ts +++ b/packages/plugin-dts/src/tsgo.ts @@ -77,18 +77,6 @@ async function handleDiagnosticsAndProcessFiles( footer?: string, name?: string, ): Promise { - await processDtsFiles( - bundle, - declarationDir, - dtsExtension, - redirect, - configPath, - rootDir, - paths, - banner, - footer, - ); - if (!bundle) { const dtsFiles = await globDtsFiles(declarationDir, [ '/**/*.d.ts', @@ -117,6 +105,18 @@ async function handleDiagnosticsAndProcessFiles( ); } + await processDtsFiles( + bundle, + declarationDir, + dtsExtension, + redirect, + configPath, + rootDir, + paths, + banner, + footer, + ); + if (hasErrors && !isWatch) { const error = new Error( `Failed to generate declaration files. ${color.dim(`(${name})`)}`, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 62c52a9a6..6c9535ade 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -938,6 +938,21 @@ importers: specifier: ^5.9.2 version: 5.9.2 + tests/integration/redirect/dts-tsgo: + devDependencies: + '@rslib/core': + specifier: workspace:* + version: link:../../../../packages/core + '@types/express': + specifier: ^5.0.3 + version: 5.0.3 + express: + specifier: ^5.1.0 + version: 5.1.0 + typescript: + specifier: ^5.9.2 + version: 5.9.2 + tests/integration/redirect/js: devDependencies: '@types/lodash': diff --git a/tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/index.d.ts b/tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/index.d.ts new file mode 100644 index 000000000..4c82305cf --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/index.d.ts @@ -0,0 +1,4 @@ +export declare function logger(): { + (...data: any[]): void; + (message?: any, ...optionalParams: any[]): void; +}; diff --git a/tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/index.js b/tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/index.js new file mode 100644 index 000000000..55cdb9deb --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/index.js @@ -0,0 +1,3 @@ +export function logger() { + return console.log; +} diff --git a/tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/package.json b/tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/package.json new file mode 100644 index 000000000..5126af028 --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/compile/prebundle-pkg/package.json @@ -0,0 +1,5 @@ +{ + "name": "prebundle-pkg", + "version": "0.0.0", + "private": true +} diff --git a/tests/integration/redirect/dts-tsgo/package.json b/tests/integration/redirect/dts-tsgo/package.json new file mode 100644 index 000000000..bcc4606cc --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/package.json @@ -0,0 +1,14 @@ +{ + "name": "redirect-dts-tsgo-test", + "version": "1.0.0", + "private": true, + "devDependencies": { + "@rslib/core": "workspace:*", + "@types/express": "^5.0.3", + "express": "^5.1.0", + "typescript": "^5.9.2" + }, + "peerDependencies": { + "express": "^4" + } +} diff --git a/tests/integration/redirect/dts-tsgo/rslib.config.ts b/tests/integration/redirect/dts-tsgo/rslib.config.ts new file mode 100644 index 000000000..35cbddc18 --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/rslib.config.ts @@ -0,0 +1,113 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + // 0 - default - path: true extension: false + generateBundleEsmConfig({ + dts: { + experiments: { + tsgo: true, + }, + }, + output: { + distPath: { + root: './dist/default/esm', + }, + }, + }), + // 1 - path: false extension: false + generateBundleEsmConfig({ + dts: { + experiments: { + tsgo: true, + }, + }, + output: { + distPath: { + root: './dist/path-false/esm', + }, + }, + redirect: { + dts: { + path: false, + }, + }, + }), + // 2 - path: true extension: true + generateBundleEsmConfig({ + dts: { + experiments: { + tsgo: true, + }, + }, + output: { + distPath: { + root: './dist/extension-true/esm', + }, + }, + redirect: { + dts: { + extension: true, + }, + }, + }), + // 3 - path: false extension: true + generateBundleEsmConfig({ + dts: { + experiments: { + tsgo: true, + }, + }, + output: { + distPath: { + root: './dist/path-false-extension-true/esm', + }, + }, + redirect: { + dts: { + path: false, + extension: true, + }, + }, + }), + // 4 - extension: true with dts.autoExtension true + generateBundleEsmConfig({ + dts: { + experiments: { + tsgo: true, + }, + autoExtension: true, + }, + output: { + distPath: { + root: './dist/auto-extension-true/esm', + }, + }, + redirect: { + dts: { + extension: true, + }, + }, + }), + // 5 - extension: true with dts.autoExtension true + generateBundleCjsConfig({ + dts: { + experiments: { + tsgo: true, + }, + autoExtension: true, + }, + output: { + distPath: { + root: './dist/auto-extension-true/cjs', + }, + }, + redirect: { + dts: { + extension: true, + }, + }, + }), + ], +}); diff --git a/tests/integration/redirect/dts-tsgo/src/.hidden-folder/index.ts b/tests/integration/redirect/dts-tsgo/src/.hidden-folder/index.ts new file mode 100644 index 000000000..afa9b24f7 --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/src/.hidden-folder/index.ts @@ -0,0 +1 @@ +export const hiddenFolder = 'This is a hidden folder'; diff --git a/tests/integration/redirect/dts-tsgo/src/.hidden.ts b/tests/integration/redirect/dts-tsgo/src/.hidden.ts new file mode 100644 index 000000000..4aa488db5 --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/src/.hidden.ts @@ -0,0 +1 @@ +export const hidden = 'This is a hidden file'; diff --git a/tests/integration/redirect/dts-tsgo/src/a.b/index.ts b/tests/integration/redirect/dts-tsgo/src/a.b/index.ts new file mode 100644 index 000000000..7cd8bd0a9 --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/src/a.b/index.ts @@ -0,0 +1 @@ +export const ab = 'a.b'; diff --git a/tests/integration/redirect/dts-tsgo/src/bar.baz.ts b/tests/integration/redirect/dts-tsgo/src/bar.baz.ts new file mode 100644 index 000000000..ba28c5f41 --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/src/bar.baz.ts @@ -0,0 +1 @@ +export const bar = 'bar-baz'; diff --git a/tests/integration/redirect/dts-tsgo/src/foo/foo.ts b/tests/integration/redirect/dts-tsgo/src/foo/foo.ts new file mode 100644 index 000000000..7891b894a --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/src/foo/foo.ts @@ -0,0 +1,5 @@ +import { logRequest } from '@src/logger'; +import { logger } from 'prebundle-pkg'; +import { logRequest as logRequest2 } from '../logger'; + +export { logRequest, logRequest2, logger }; diff --git a/tests/integration/redirect/dts-tsgo/src/foo/index.ts b/tests/integration/redirect/dts-tsgo/src/foo/index.ts new file mode 100644 index 000000000..77a95f681 --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/src/foo/index.ts @@ -0,0 +1 @@ +export type Barrel = string; diff --git a/tests/integration/redirect/dts-tsgo/src/index.ts b/tests/integration/redirect/dts-tsgo/src/index.ts new file mode 100644 index 000000000..a6a576679 --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/src/index.ts @@ -0,0 +1,29 @@ +import { logRequest } from '@src/logger'; +import { logger } from 'prebundle-pkg'; +import type { Baz } from 'self-entry'; +import type { LoggerOptions } from './types'; +import { defaultOptions } from './types.js'; + +import sources = require('@src/logger'); + +export { + sources, + type Baz as self, + logRequest, + logger, + type LoggerOptions, + defaultOptions, +}; + +export * from '@src/foo'; +export * from '@src/logger'; +export type { Foo } from '@src/types'; +// export { Router } from 'express'; +export * from 'prebundle-pkg'; +export type { Bar } from 'types'; +export * from './.hidden'; +export * from './.hidden-folder'; +export * from './a.b'; +export * from './bar.baz'; +export * from './foo'; +export * from './types'; diff --git a/tests/integration/redirect/dts-tsgo/src/logger.ts b/tests/integration/redirect/dts-tsgo/src/logger.ts new file mode 100644 index 000000000..09265c607 --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/src/logger.ts @@ -0,0 +1,25 @@ +// import type { Request } from 'express'; +import type { LoggerOptions } from './types'; + +export function logRequest(req: Request, options: LoggerOptions): void { + const { method, url } = req; + const logMessage = `${method} ${url}`; + + switch (options.logLevel) { + case 'debug': + console.debug(logMessage); + break; + case 'warn': + console.warn(logMessage); + break; + case 'error': + console.error(logMessage); + break; + default: + console.log(logMessage); + } + + if (options.logBody && req.body) { + console.log('Request body:', req.body); + } +} diff --git a/tests/integration/redirect/dts-tsgo/src/types.ts b/tests/integration/redirect/dts-tsgo/src/types.ts new file mode 100644 index 000000000..ef6a9bf2b --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/src/types.ts @@ -0,0 +1,21 @@ +export interface LoggerOptions { + logLevel: 'info' | 'debug' | 'warn' | 'error'; + logBody: boolean; +} + +export const defaultOptions: LoggerOptions = { + logLevel: 'info', + logBody: false, +}; + +export interface Foo { + foo: string; +} + +export interface Bar { + bar: string; +} + +export interface Baz { + baz: string; +} diff --git a/tests/integration/redirect/dts-tsgo/tsconfig.json b/tests/integration/redirect/dts-tsgo/tsconfig.json new file mode 100644 index 000000000..6db3ff6ac --- /dev/null +++ b/tests/integration/redirect/dts-tsgo/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "strict": true, + "skipLibCheck": true, + "paths": { + "@src/*": ["./src/*"], + "prebundle-pkg": ["./compile/prebundle-pkg"], + "self-entry": ["./src"], + "express": ["./node_modules/express"], + "*": ["./src/*"] + } + }, + "include": ["src/**/*"] +} diff --git a/tests/integration/redirect/dtsTsgo.test.ts b/tests/integration/redirect/dtsTsgo.test.ts new file mode 100644 index 000000000..ef56b6edd --- /dev/null +++ b/tests/integration/redirect/dtsTsgo.test.ts @@ -0,0 +1,373 @@ +import path from 'node:path'; +import { beforeAll, expect, test } from '@rstest/core'; +import { buildAndGetResults } from 'test-helper'; + +let contents: Awaited>['contents']; + +beforeAll(async () => { + const fixturePath = path.resolve(__dirname, './dts-tsgo'); + contents = (await buildAndGetResults({ fixturePath, type: 'dts' })).contents; +}, 20000); + +test('redirect.dts.path: true with redirect.dts.extension: false - default', async () => { + expect(contents.esm0).toMatchInlineSnapshot(` + { + "/tests/integration/redirect/dts-tsgo/dist/default/esm/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/.hidden.d.ts": "export declare const hidden = "This is a hidden file"; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/a.b/index.d.ts": "export declare const ab = "a.b"; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/bar.baz.d.ts": "export declare const bar = "bar-baz"; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/foo/foo.d.ts": "import { logRequest } from '../logger'; + import { logger } from '../../../../compile/prebundle-pkg'; + import { logRequest as logRequest2 } from '../logger'; + export { logRequest, logRequest2, logger }; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/foo/index.d.ts": "export type Barrel = string; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/index.d.ts": "import { logRequest } from './logger'; + import { logger } from '../../../compile/prebundle-pkg'; + import type { Baz } from './'; + import type { LoggerOptions } from './types'; + import { defaultOptions } from './types.js'; + import sources = require('./logger'); + export { sources, type Baz as self, logRequest, logger, type LoggerOptions, defaultOptions, }; + export * from './foo'; + export * from './logger'; + export type { Foo } from './types'; + // export { Router } from 'express'; + export * from '../../../compile/prebundle-pkg'; + export type { Bar } from './types'; + export * from './.hidden'; + export * from './.hidden-folder'; + export * from './a.b'; + export * from './bar.baz'; + export * from './foo'; + export * from './types'; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/logger.d.ts": "// import type { Request } from 'express'; + import type { LoggerOptions } from './types'; + export declare function logRequest(req: Request, options: LoggerOptions): void; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/types.d.ts": "export interface LoggerOptions { + logLevel: 'info' | 'debug' | 'warn' | 'error'; + logBody: boolean; + } + export declare const defaultOptions: LoggerOptions; + export interface Foo { + foo: string; + } + export interface Bar { + bar: string; + } + export interface Baz { + baz: string; + } + ", + } + `); +}); + +test('redirect.dts.path: false with redirect.dts.extension: false', async () => { + expect(contents.esm1).toMatchInlineSnapshot(` + { + "/tests/integration/redirect/dts-tsgo/dist/path-false/esm/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false/esm/.hidden.d.ts": "export declare const hidden = "This is a hidden file"; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false/esm/a.b/index.d.ts": "export declare const ab = "a.b"; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false/esm/bar.baz.d.ts": "export declare const bar = "bar-baz"; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false/esm/foo/foo.d.ts": "import { logRequest } from '@src/logger'; + import { logger } from 'prebundle-pkg'; + import { logRequest as logRequest2 } from '../logger'; + export { logRequest, logRequest2, logger }; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false/esm/foo/index.d.ts": "export type Barrel = string; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false/esm/index.d.ts": "import { logRequest } from '@src/logger'; + import { logger } from 'prebundle-pkg'; + import type { Baz } from 'self-entry'; + import type { LoggerOptions } from './types'; + import { defaultOptions } from './types.js'; + import sources = require('@src/logger'); + export { sources, type Baz as self, logRequest, logger, type LoggerOptions, defaultOptions, }; + export * from '@src/foo'; + export * from '@src/logger'; + export type { Foo } from '@src/types'; + // export { Router } from 'express'; + export * from 'prebundle-pkg'; + export type { Bar } from 'types'; + export * from './.hidden'; + export * from './.hidden-folder'; + export * from './a.b'; + export * from './bar.baz'; + export * from './foo'; + export * from './types'; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false/esm/logger.d.ts": "// import type { Request } from 'express'; + import type { LoggerOptions } from './types'; + export declare function logRequest(req: Request, options: LoggerOptions): void; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false/esm/types.d.ts": "export interface LoggerOptions { + logLevel: 'info' | 'debug' | 'warn' | 'error'; + logBody: boolean; + } + export declare const defaultOptions: LoggerOptions; + export interface Foo { + foo: string; + } + export interface Bar { + bar: string; + } + export interface Baz { + baz: string; + } + ", + } + `); +}); + +test('redirect.dts.path: true with redirect.dts.extension: true', async () => { + expect(contents.esm2).toMatchInlineSnapshot(` + { + "/tests/integration/redirect/dts-tsgo/dist/extension-true/esm/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; + ", + "/tests/integration/redirect/dts-tsgo/dist/extension-true/esm/.hidden.d.ts": "export declare const hidden = "This is a hidden file"; + ", + "/tests/integration/redirect/dts-tsgo/dist/extension-true/esm/a.b/index.d.ts": "export declare const ab = "a.b"; + ", + "/tests/integration/redirect/dts-tsgo/dist/extension-true/esm/bar.baz.d.ts": "export declare const bar = "bar-baz"; + ", + "/tests/integration/redirect/dts-tsgo/dist/extension-true/esm/foo/foo.d.ts": "import { logRequest } from '../logger.js'; + import { logger } from '../../../../compile/prebundle-pkg'; + import { logRequest as logRequest2 } from '../logger.js'; + export { logRequest, logRequest2, logger }; + ", + "/tests/integration/redirect/dts-tsgo/dist/extension-true/esm/foo/index.d.ts": "export type Barrel = string; + ", + "/tests/integration/redirect/dts-tsgo/dist/extension-true/esm/index.d.ts": "import { logRequest } from './logger.js'; + import { logger } from '../../../compile/prebundle-pkg'; + import type { Baz } from './index.js'; + import type { LoggerOptions } from './types.js'; + import { defaultOptions } from './types.js'; + import sources = require('./logger.js'); + export { sources, type Baz as self, logRequest, logger, type LoggerOptions, defaultOptions, }; + export * from './foo/index.js'; + export * from './logger.js'; + export type { Foo } from './types.js'; + // export { Router } from 'express'; + export * from '../../../compile/prebundle-pkg'; + export type { Bar } from './types.js'; + export * from './.hidden.js'; + export * from './.hidden-folder/index.js'; + export * from './a.b/index.js'; + export * from './bar.baz.js'; + export * from './foo/index.js'; + export * from './types.js'; + ", + "/tests/integration/redirect/dts-tsgo/dist/extension-true/esm/logger.d.ts": "// import type { Request } from 'express'; + import type { LoggerOptions } from './types.js'; + export declare function logRequest(req: Request, options: LoggerOptions): void; + ", + "/tests/integration/redirect/dts-tsgo/dist/extension-true/esm/types.d.ts": "export interface LoggerOptions { + logLevel: 'info' | 'debug' | 'warn' | 'error'; + logBody: boolean; + } + export declare const defaultOptions: LoggerOptions; + export interface Foo { + foo: string; + } + export interface Bar { + bar: string; + } + export interface Baz { + baz: string; + } + ", + } + `); +}); + +test('redirect.dts.path: false with dts.redirect.extension: true', async () => { + expect(contents.esm3).toMatchInlineSnapshot(` + { + "/tests/integration/redirect/dts-tsgo/dist/path-false-extension-true/esm/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false-extension-true/esm/.hidden.d.ts": "export declare const hidden = "This is a hidden file"; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false-extension-true/esm/a.b/index.d.ts": "export declare const ab = "a.b"; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false-extension-true/esm/bar.baz.d.ts": "export declare const bar = "bar-baz"; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false-extension-true/esm/foo/foo.d.ts": "import { logRequest } from '@src/logger'; + import { logger } from 'prebundle-pkg'; + import { logRequest as logRequest2 } from '../logger.js'; + export { logRequest, logRequest2, logger }; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false-extension-true/esm/foo/index.d.ts": "export type Barrel = string; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false-extension-true/esm/index.d.ts": "import { logRequest } from '@src/logger'; + import { logger } from 'prebundle-pkg'; + import type { Baz } from 'self-entry'; + import type { LoggerOptions } from './types.js'; + import { defaultOptions } from './types.js'; + import sources = require('@src/logger'); + export { sources, type Baz as self, logRequest, logger, type LoggerOptions, defaultOptions, }; + export * from '@src/foo'; + export * from '@src/logger'; + export type { Foo } from '@src/types'; + // export { Router } from 'express'; + export * from 'prebundle-pkg'; + export type { Bar } from 'types'; + export * from './.hidden.js'; + export * from './.hidden-folder/index.js'; + export * from './a.b/index.js'; + export * from './bar.baz.js'; + export * from './foo/index.js'; + export * from './types.js'; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false-extension-true/esm/logger.d.ts": "// import type { Request } from 'express'; + import type { LoggerOptions } from './types.js'; + export declare function logRequest(req: Request, options: LoggerOptions): void; + ", + "/tests/integration/redirect/dts-tsgo/dist/path-false-extension-true/esm/types.d.ts": "export interface LoggerOptions { + logLevel: 'info' | 'debug' | 'warn' | 'error'; + logBody: boolean; + } + export declare const defaultOptions: LoggerOptions; + export interface Foo { + foo: string; + } + export interface Bar { + bar: string; + } + export interface Baz { + baz: string; + } + ", + } + `); +}); + +test('redirect.dts.extension: true with dts.autoExtension: true', async () => { + expect(contents.esm4).toMatchInlineSnapshot(` + { + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/esm/.hidden-folder/index.d.mts": "export declare const hiddenFolder = "This is a hidden folder"; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/esm/.hidden.d.mts": "export declare const hidden = "This is a hidden file"; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/esm/a.b/index.d.mts": "export declare const ab = "a.b"; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/esm/bar.baz.d.mts": "export declare const bar = "bar-baz"; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/esm/foo/foo.d.mts": "import { logRequest } from '../logger.mjs'; + import { logger } from '../../../../compile/prebundle-pkg'; + import { logRequest as logRequest2 } from '../logger.mjs'; + export { logRequest, logRequest2, logger }; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/esm/foo/index.d.mts": "export type Barrel = string; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/esm/index.d.mts": "import { logRequest } from './logger.mjs'; + import { logger } from '../../../compile/prebundle-pkg'; + import type { Baz } from './index.mjs'; + import type { LoggerOptions } from './types.mjs'; + import { defaultOptions } from './types.mjs'; + import sources = require('./logger.mjs'); + export { sources, type Baz as self, logRequest, logger, type LoggerOptions, defaultOptions, }; + export * from './foo/index.mjs'; + export * from './logger.mjs'; + export type { Foo } from './types.mjs'; + // export { Router } from 'express'; + export * from '../../../compile/prebundle-pkg'; + export type { Bar } from './types.mjs'; + export * from './.hidden.mjs'; + export * from './.hidden-folder/index.mjs'; + export * from './a.b/index.mjs'; + export * from './bar.baz.mjs'; + export * from './foo/index.mjs'; + export * from './types.mjs'; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/esm/logger.d.mts": "// import type { Request } from 'express'; + import type { LoggerOptions } from './types.mjs'; + export declare function logRequest(req: Request, options: LoggerOptions): void; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/esm/types.d.mts": "export interface LoggerOptions { + logLevel: 'info' | 'debug' | 'warn' | 'error'; + logBody: boolean; + } + export declare const defaultOptions: LoggerOptions; + export interface Foo { + foo: string; + } + export interface Bar { + bar: string; + } + export interface Baz { + baz: string; + } + ", + } + `); + expect(contents.cjs).toMatchInlineSnapshot(` + { + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/cjs/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/cjs/.hidden.d.ts": "export declare const hidden = "This is a hidden file"; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/cjs/a.b/index.d.ts": "export declare const ab = "a.b"; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/cjs/bar.baz.d.ts": "export declare const bar = "bar-baz"; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/cjs/foo/foo.d.ts": "import { logRequest } from '../logger.js'; + import { logger } from '../../../../compile/prebundle-pkg'; + import { logRequest as logRequest2 } from '../logger.js'; + export { logRequest, logRequest2, logger }; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/cjs/foo/index.d.ts": "export type Barrel = string; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/cjs/index.d.ts": "import { logRequest } from './logger.js'; + import { logger } from '../../../compile/prebundle-pkg'; + import type { Baz } from './index.js'; + import type { LoggerOptions } from './types.js'; + import { defaultOptions } from './types.js'; + import sources = require('./logger.js'); + export { sources, type Baz as self, logRequest, logger, type LoggerOptions, defaultOptions, }; + export * from './foo/index.js'; + export * from './logger.js'; + export type { Foo } from './types.js'; + // export { Router } from 'express'; + export * from '../../../compile/prebundle-pkg'; + export type { Bar } from './types.js'; + export * from './.hidden.js'; + export * from './.hidden-folder/index.js'; + export * from './a.b/index.js'; + export * from './bar.baz.js'; + export * from './foo/index.js'; + export * from './types.js'; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/cjs/logger.d.ts": "// import type { Request } from 'express'; + import type { LoggerOptions } from './types.js'; + export declare function logRequest(req: Request, options: LoggerOptions): void; + ", + "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/cjs/types.d.ts": "export interface LoggerOptions { + logLevel: 'info' | 'debug' | 'warn' | 'error'; + logBody: boolean; + } + export declare const defaultOptions: LoggerOptions; + export interface Foo { + foo: string; + } + export interface Bar { + bar: string; + } + export interface Baz { + baz: string; + } + ", + } + `); +}); From 770f2cf698d7ca20467049ddea40c525f88994db Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 16:52:26 +0800 Subject: [PATCH 03/14] test: add banner.dts test --- tests/integration/banner-footer/index.test.ts | 2 +- .../integration/banner-footer/rslib.config.ts | 88 +++++++++++++++++++ tests/integration/banner-footer/tsconfig.json | 4 +- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/tests/integration/banner-footer/index.test.ts b/tests/integration/banner-footer/index.test.ts index 1f00a9ee7..9a713b5bd 100644 --- a/tests/integration/banner-footer/index.test.ts +++ b/tests/integration/banner-footer/index.test.ts @@ -21,7 +21,7 @@ test('banner and footer should work in js, css and dts', async () => { const cssContents = Object.values(css.contents); const dtsContents = Object.values(dts.contents); - // There are 5 cases included: + // There are 5 cases included in both tsc and tsgo // 1. bundle esm // 2. bundle cjs // 3. bundleless esm diff --git a/tests/integration/banner-footer/rslib.config.ts b/tests/integration/banner-footer/rslib.config.ts index 42899acde..0b6b0fc47 100644 --- a/tests/integration/banner-footer/rslib.config.ts +++ b/tests/integration/banner-footer/rslib.config.ts @@ -89,6 +89,94 @@ export default defineConfig({ }, ...bannerFooterConfig, }), + // bundle esm + generateBundleEsmConfig({ + output: { + distPath: { + root: './dist/esm-tsgo/bundle', + }, + }, + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + ...bannerFooterConfig, + }), + // bundle cjs + generateBundleCjsConfig({ + output: { + distPath: { + root: './dist/cjs-tsgo/bundle', + }, + }, + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + ...bannerFooterConfig, + }), + // bundleless esm + generateBundleEsmConfig({ + output: { + distPath: { + root: './dist/esm-tsgo/bundleless', + }, + }, + bundle: false, + dts: { + bundle: false, + experiments: { + tsgo: true, + }, + }, + source: { + entry: { + index: ['./src/**'], + }, + }, + ...bannerFooterConfig, + }), + // bundleless cjs + generateBundleCjsConfig({ + output: { + distPath: { + root: './dist/cjs-tsgo/bundleless', + }, + }, + bundle: false, + dts: { + bundle: false, + experiments: { + tsgo: true, + }, + }, + source: { + entry: { + index: ['./src/**'], + }, + }, + ...bannerFooterConfig, + }), + // bundle esm with minify enabled + generateBundleEsmConfig({ + output: { + distPath: { + root: './dist/esm-tsgo/bundle-minify', + }, + minify: true, + }, + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + ...bannerFooterConfig, + }), ], source: { entry: { diff --git a/tests/integration/banner-footer/tsconfig.json b/tests/integration/banner-footer/tsconfig.json index 888d3e460..5ee8bfa51 100644 --- a/tests/integration/banner-footer/tsconfig.json +++ b/tests/integration/banner-footer/tsconfig.json @@ -1,7 +1,5 @@ { "extends": "@rslib/tsconfig/base", - "compilerOptions": { - "baseUrl": "./" - }, + "compilerOptions": {}, "include": ["src"] } From d29c262b78cd083527a384c3bd68a4ebacc100dd Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 17:22:02 +0800 Subject: [PATCH 04/14] test: add bundleless dts test --- .gitignore | 1 + pnpm-lock.yaml | 18 ++ .../__snapshots__/bundleFalse.test.ts.snap | 21 ++ .../bundle-false/__fixtures__/src/index.ts | 3 + .../bundle-false/__fixtures__/src/sum.ts | 5 + .../__fixtures__/src/utils/numbers.ts | 3 + .../__fixtures__/src/utils/strings.ts | 3 + .../bundle-false/__fixtures__/tsconfig.json | 5 + .../bundle-false/abort-on-error/package.json | 6 + .../abort-on-error/rslib.config.ts | 25 ++ .../bundle-false/abort-on-error/src/const.ts | 3 + .../bundle-false/abort-on-error/src/index.ts | 6 + .../bundle-false/abort-on-error/tsconfig.json | 5 + .../alias/compile/prebundle-pkg/index.d.ts | 4 + .../alias/compile/prebundle-pkg/index.js | 3 + .../alias/compile/prebundle-pkg/package.json | 5 + .../dts-tsgo/bundle-false/alias/package.json | 6 + .../bundle-false/alias/rslib.config.ts | 29 +++ .../dts-tsgo/bundle-false/alias/src/index.ts | 1 + .../dts-tsgo/bundle-false/alias/tsconfig.json | 7 + .../bundle-false/auto-extension/package.json | 6 + .../auto-extension/rslib.config.ts | 35 +++ .../dts-tsgo/bundle-false/basic/package.json | 6 + .../bundle-false/basic/rslib.config.ts | 31 +++ .../dts-tsgo/bundle-false/clean/package.json | 6 + .../bundle-false/clean/rslib.config.ts | 33 +++ .../bundle-false/declaration-dir/package.json | 6 + .../declaration-dir/rslib.config.ts | 21 ++ .../declaration-dir/tsconfig.json | 8 + .../bundle-false/declaration-map/package.json | 6 + .../declaration-map/rslib.config.ts | 25 ++ .../bundle-false/declaration-map/src/index.ts | 1 + .../declaration-map/tsconfig.json | 8 + .../bundle-false/dist-path/package.json | 6 + .../bundle-false/dist-path/rslib.config.ts | 26 ++ .../bundle-false/tsconfig-path/package.json | 6 + .../tsconfig-path/rslib.config.ts | 22 ++ .../integration/dts-tsgo/bundleFalse.test.ts | 224 ++++++++++++++++++ 38 files changed, 635 insertions(+) create mode 100644 tests/integration/dts-tsgo/__snapshots__/bundleFalse.test.ts.snap create mode 100644 tests/integration/dts-tsgo/bundle-false/__fixtures__/src/index.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/__fixtures__/src/sum.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/__fixtures__/src/utils/numbers.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/__fixtures__/src/utils/strings.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/__fixtures__/tsconfig.json create mode 100644 tests/integration/dts-tsgo/bundle-false/abort-on-error/package.json create mode 100644 tests/integration/dts-tsgo/bundle-false/abort-on-error/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/abort-on-error/src/const.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/abort-on-error/src/index.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/abort-on-error/tsconfig.json create mode 100644 tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/index.d.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/index.js create mode 100644 tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/package.json create mode 100644 tests/integration/dts-tsgo/bundle-false/alias/package.json create mode 100644 tests/integration/dts-tsgo/bundle-false/alias/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/alias/src/index.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/alias/tsconfig.json create mode 100644 tests/integration/dts-tsgo/bundle-false/auto-extension/package.json create mode 100644 tests/integration/dts-tsgo/bundle-false/auto-extension/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/basic/package.json create mode 100644 tests/integration/dts-tsgo/bundle-false/basic/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/clean/package.json create mode 100644 tests/integration/dts-tsgo/bundle-false/clean/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/declaration-dir/package.json create mode 100644 tests/integration/dts-tsgo/bundle-false/declaration-dir/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/declaration-dir/tsconfig.json create mode 100644 tests/integration/dts-tsgo/bundle-false/declaration-map/package.json create mode 100644 tests/integration/dts-tsgo/bundle-false/declaration-map/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/declaration-map/src/index.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/declaration-map/tsconfig.json create mode 100644 tests/integration/dts-tsgo/bundle-false/dist-path/package.json create mode 100644 tests/integration/dts-tsgo/bundle-false/dist-path/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle-false/tsconfig-path/package.json create mode 100644 tests/integration/dts-tsgo/bundle-false/tsconfig-path/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundleFalse.test.ts diff --git a/.gitignore b/.gitignore index ac9cb468f..7e189dc0a 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ test-results .env.local .env.*.local .rslib/**/* +!dist-path/ \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6c9535ade..c5e073f37 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -714,6 +714,24 @@ importers: tests/integration/directive/shebang: {} + tests/integration/dts-tsgo/bundle-false/abort-on-error: {} + + tests/integration/dts-tsgo/bundle-false/alias: {} + + tests/integration/dts-tsgo/bundle-false/auto-extension: {} + + tests/integration/dts-tsgo/bundle-false/basic: {} + + tests/integration/dts-tsgo/bundle-false/clean: {} + + tests/integration/dts-tsgo/bundle-false/declaration-dir: {} + + tests/integration/dts-tsgo/bundle-false/declaration-map: {} + + tests/integration/dts-tsgo/bundle-false/dist-path: {} + + tests/integration/dts-tsgo/bundle-false/tsconfig-path: {} + tests/integration/dts/build/__references__: {} tests/integration/dts/build/abort-on-error: {} diff --git a/tests/integration/dts-tsgo/__snapshots__/bundleFalse.test.ts.snap b/tests/integration/dts-tsgo/__snapshots__/bundleFalse.test.ts.snap new file mode 100644 index 000000000..51668fadc --- /dev/null +++ b/tests/integration/dts-tsgo/__snapshots__/bundleFalse.test.ts.snap @@ -0,0 +1,21 @@ +// Rstest Snapshot v1 + +exports[`dts with tsgo when bundle: false > basic 3`] = ` +{ + "/tests/integration/dts-tsgo/bundle-false/basic/dist/esm/index.d.ts": "export * from './sum'; +export * from './utils/numbers'; +export * from './utils/strings'; +", + "/tests/integration/dts-tsgo/bundle-false/basic/dist/esm/sum.d.ts": "export declare const numSum: number; +export declare const strSum: string; +", + "/tests/integration/dts-tsgo/bundle-false/basic/dist/esm/utils/numbers.d.ts": "export declare const num1 = 1; +export declare const num2 = 2; +export declare const num3 = 3; +", + "/tests/integration/dts-tsgo/bundle-false/basic/dist/esm/utils/strings.d.ts": "export declare const str1 = "str1"; +export declare const str2 = "str2"; +export declare const str3 = "str3"; +", +} +`; diff --git a/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/index.ts b/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/index.ts new file mode 100644 index 000000000..fb828f24f --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/index.ts @@ -0,0 +1,3 @@ +export * from './sum'; +export * from './utils/numbers'; +export * from './utils/strings'; diff --git a/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/sum.ts b/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/sum.ts new file mode 100644 index 000000000..2759c8823 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/sum.ts @@ -0,0 +1,5 @@ +import { num1, num2, num3 } from './utils/numbers'; +import { str1, str2, str3 } from './utils/strings'; + +export const numSum = num1 + num2 + num3; +export const strSum = str1 + str2 + str3; diff --git a/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/utils/numbers.ts b/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/utils/numbers.ts new file mode 100644 index 000000000..c1d2a8cd2 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/utils/numbers.ts @@ -0,0 +1,3 @@ +export const num1 = 1; +export const num2 = 2; +export const num3 = 3; diff --git a/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/utils/strings.ts b/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/utils/strings.ts new file mode 100644 index 000000000..d35b5a606 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/__fixtures__/src/utils/strings.ts @@ -0,0 +1,3 @@ +export const str1 = 'str1'; +export const str2 = 'str2'; +export const str3 = 'str3'; diff --git a/tests/integration/dts-tsgo/bundle-false/__fixtures__/tsconfig.json b/tests/integration/dts-tsgo/bundle-false/__fixtures__/tsconfig.json new file mode 100644 index 000000000..5ee8bfa51 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/__fixtures__/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": {}, + "include": ["src"] +} diff --git a/tests/integration/dts-tsgo/bundle-false/abort-on-error/package.json b/tests/integration/dts-tsgo/bundle-false/abort-on-error/package.json new file mode 100644 index 000000000..11afcef18 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/abort-on-error/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-false-abort-on-error-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle-false/abort-on-error/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/abort-on-error/rslib.config.ts new file mode 100644 index 000000000..3b06f12c3 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/abort-on-error/rslib.config.ts @@ -0,0 +1,25 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + bundle: false, + abortOnError: false, + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + bundle: false, + }), + ], + source: { + entry: { + index: ['./src/**'], + }, + }, +}); diff --git a/tests/integration/dts-tsgo/bundle-false/abort-on-error/src/const.ts b/tests/integration/dts-tsgo/bundle-false/abort-on-error/src/const.ts new file mode 100644 index 000000000..88a6d19f4 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/abort-on-error/src/const.ts @@ -0,0 +1,3 @@ +export interface A { + a: number; +} diff --git a/tests/integration/dts-tsgo/bundle-false/abort-on-error/src/index.ts b/tests/integration/dts-tsgo/bundle-false/abort-on-error/src/index.ts new file mode 100644 index 000000000..2595bdbbf --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/abort-on-error/src/index.ts @@ -0,0 +1,6 @@ +import type { A } from './const'; + +export const getA = (item: A) => { + item.a = '0'; + return item; +}; diff --git a/tests/integration/dts-tsgo/bundle-false/abort-on-error/tsconfig.json b/tests/integration/dts-tsgo/bundle-false/abort-on-error/tsconfig.json new file mode 100644 index 000000000..5ee8bfa51 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/abort-on-error/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": {}, + "include": ["src"] +} diff --git a/tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/index.d.ts b/tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/index.d.ts new file mode 100644 index 000000000..4c82305cf --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/index.d.ts @@ -0,0 +1,4 @@ +export declare function logger(): { + (...data: any[]): void; + (message?: any, ...optionalParams: any[]): void; +}; diff --git a/tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/index.js b/tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/index.js new file mode 100644 index 000000000..55cdb9deb --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/index.js @@ -0,0 +1,3 @@ +export function logger() { + return console.log; +} diff --git a/tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/package.json b/tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/package.json new file mode 100644 index 000000000..5126af028 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/alias/compile/prebundle-pkg/package.json @@ -0,0 +1,5 @@ +{ + "name": "prebundle-pkg", + "version": "0.0.0", + "private": true +} diff --git a/tests/integration/dts-tsgo/bundle-false/alias/package.json b/tests/integration/dts-tsgo/bundle-false/alias/package.json new file mode 100644 index 000000000..81aa805bf --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/alias/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-false-alias-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle-false/alias/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/alias/rslib.config.ts new file mode 100644 index 000000000..09477ef7e --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/alias/rslib.config.ts @@ -0,0 +1,29 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: { + bundle: false, + alias: { + 'prebundle-pkg': './compile/prebundle-pkg', + }, + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + dts: { + bundle: false, + alias: { + 'prebundle-pkg': './compile/prebundle-pkg', + }, + experiments: { + tsgo: true, + }, + }, + }), + ], +}); diff --git a/tests/integration/dts-tsgo/bundle-false/alias/src/index.ts b/tests/integration/dts-tsgo/bundle-false/alias/src/index.ts new file mode 100644 index 000000000..8fc482265 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/alias/src/index.ts @@ -0,0 +1 @@ +export {} from 'prebundle-pkg'; diff --git a/tests/integration/dts-tsgo/bundle-false/alias/tsconfig.json b/tests/integration/dts-tsgo/bundle-false/alias/tsconfig.json new file mode 100644 index 000000000..1d2e6432e --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/alias/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "strict": true, + "skipLibCheck": true + }, + "include": ["src/**/*"] +} diff --git a/tests/integration/dts-tsgo/bundle-false/auto-extension/package.json b/tests/integration/dts-tsgo/bundle-false/auto-extension/package.json new file mode 100644 index 000000000..cca4a8b5e --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/auto-extension/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-false-auto-extension-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle-false/auto-extension/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/auto-extension/rslib.config.ts new file mode 100644 index 000000000..e6d051586 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/auto-extension/rslib.config.ts @@ -0,0 +1,35 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + autoExtension: true, + distPath: './dist/types/esm', + bundle: false, + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + bundle: false, + dts: { + autoExtension: true, + distPath: './dist/types/cjs', + bundle: false, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: ['../__fixtures__/src/**'], + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle-false/basic/package.json b/tests/integration/dts-tsgo/bundle-false/basic/package.json new file mode 100644 index 000000000..9bdb94a03 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/basic/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-false-basic-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle-false/basic/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/basic/rslib.config.ts new file mode 100644 index 000000000..1abcb2e0a --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/basic/rslib.config.ts @@ -0,0 +1,31 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + bundle: false, + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + bundle: false, + dts: { + bundle: false, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: ['../__fixtures__/src/**'], + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle-false/clean/package.json b/tests/integration/dts-tsgo/bundle-false/clean/package.json new file mode 100644 index 000000000..93fb42720 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/clean/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-false-clean-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle-false/clean/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/clean/rslib.config.ts new file mode 100644 index 000000000..110e1b482 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/clean/rslib.config.ts @@ -0,0 +1,33 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + bundle: false, + distPath: './dist-types/esm', + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + bundle: false, + dts: { + bundle: false, + distPath: './dist-types/cjs', + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: ['../__fixtures__/src/**'], + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle-false/declaration-dir/package.json b/tests/integration/dts-tsgo/bundle-false/declaration-dir/package.json new file mode 100644 index 000000000..f64fc409c --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/declaration-dir/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-false-declaration-dir-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle-false/declaration-dir/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/declaration-dir/rslib.config.ts new file mode 100644 index 000000000..db70754cb --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/declaration-dir/rslib.config.ts @@ -0,0 +1,21 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + bundle: false, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: ['../__fixtures__/src/**'], + }, + }, +}); diff --git a/tests/integration/dts-tsgo/bundle-false/declaration-dir/tsconfig.json b/tests/integration/dts-tsgo/bundle-false/declaration-dir/tsconfig.json new file mode 100644 index 000000000..04e915d38 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/declaration-dir/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "declaration": true, + "declarationDir": "./dist-types" + }, + "include": ["../__fixtures__/src"] +} diff --git a/tests/integration/dts-tsgo/bundle-false/declaration-map/package.json b/tests/integration/dts-tsgo/bundle-false/declaration-map/package.json new file mode 100644 index 000000000..072279bf9 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/declaration-map/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-false-declaration-map-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle-false/declaration-map/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/declaration-map/rslib.config.ts new file mode 100644 index 000000000..6192cd6f8 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/declaration-map/rslib.config.ts @@ -0,0 +1,25 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + autoExtension: true, + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + bundle: false, + dts: { + autoExtension: true, + experiments: { + tsgo: true, + }, + }, + }), + ], +}); diff --git a/tests/integration/dts-tsgo/bundle-false/declaration-map/src/index.ts b/tests/integration/dts-tsgo/bundle-false/declaration-map/src/index.ts new file mode 100644 index 000000000..cc798ff50 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/declaration-map/src/index.ts @@ -0,0 +1 @@ +export const a = 1; diff --git a/tests/integration/dts-tsgo/bundle-false/declaration-map/tsconfig.json b/tests/integration/dts-tsgo/bundle-false/declaration-map/tsconfig.json new file mode 100644 index 000000000..fbdefb504 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/declaration-map/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "declaration": true, + "declarationMap": true + }, + "include": ["src"] +} diff --git a/tests/integration/dts-tsgo/bundle-false/dist-path/package.json b/tests/integration/dts-tsgo/bundle-false/dist-path/package.json new file mode 100644 index 000000000..7cd343a16 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/dist-path/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-false-dist-path-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle-false/dist-path/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/dist-path/rslib.config.ts new file mode 100644 index 000000000..d04ec5c48 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/dist-path/rslib.config.ts @@ -0,0 +1,26 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + bundle: false, + distPath: './dist/custom', + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + bundle: false, + }), + ], + source: { + entry: { + index: ['../__fixtures__/src/**'], + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle-false/tsconfig-path/package.json b/tests/integration/dts-tsgo/bundle-false/tsconfig-path/package.json new file mode 100644 index 000000000..ec8ceda39 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/tsconfig-path/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-true-bundle-false-tsconfig-path-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle-false/tsconfig-path/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/tsconfig-path/rslib.config.ts new file mode 100644 index 000000000..70af17616 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle-false/tsconfig-path/rslib.config.ts @@ -0,0 +1,22 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + bundle: false, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: '../__fixtures__/src/index.ts', + }, + tsconfigPath: './path_not_exist/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundleFalse.test.ts b/tests/integration/dts-tsgo/bundleFalse.test.ts new file mode 100644 index 000000000..62e31a3cd --- /dev/null +++ b/tests/integration/dts-tsgo/bundleFalse.test.ts @@ -0,0 +1,224 @@ +import { spawnSync } from 'node:child_process'; +import { existsSync } from 'node:fs'; +import { join } from 'node:path'; +import { describe, expect, test } from '@rstest/core'; +import stripAnsi from 'strip-ansi'; +import { + buildAndGetResults, + createTempFiles, + globContentJSON, + queryContent, +} from 'test-helper'; + +describe('dts with tsgo when bundle: false', () => { + test('basic', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'basic'); + const { files, contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle-false/basic/dist/esm/index.d.ts", + "/tests/integration/dts-tsgo/bundle-false/basic/dist/esm/sum.d.ts", + "/tests/integration/dts-tsgo/bundle-false/basic/dist/esm/utils/numbers.d.ts", + "/tests/integration/dts-tsgo/bundle-false/basic/dist/esm/utils/strings.d.ts", + ] + `); + + expect(files.cjs).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle-false/basic/dist/cjs/index.d.ts", + "/tests/integration/dts-tsgo/bundle-false/basic/dist/cjs/sum.d.ts", + "/tests/integration/dts-tsgo/bundle-false/basic/dist/cjs/utils/numbers.d.ts", + "/tests/integration/dts-tsgo/bundle-false/basic/dist/cjs/utils/strings.d.ts", + ] + `); + + expect(contents.esm).toMatchSnapshot(); + }); + + test('distPath', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'dist-path'); + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle-false/dist-path/dist/custom/index.d.ts", + "/tests/integration/dts-tsgo/bundle-false/dist-path/dist/custom/sum.d.ts", + "/tests/integration/dts-tsgo/bundle-false/dist-path/dist/custom/utils/numbers.d.ts", + "/tests/integration/dts-tsgo/bundle-false/dist-path/dist/custom/utils/strings.d.ts", + ] + `); + }); + + test('abortOnError: false', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'abort-on-error'); + + const result = spawnSync('npx', ['rslib', 'build'], { + cwd: fixturePath, + // do not show output in test console + stdio: 'ignore', + shell: true, + }); + + expect(result.status).toBe(0); + }); + + test('autoExtension: true', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'auto-extension'); + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/esm/index.d.ts", + "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/esm/sum.d.ts", + "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/esm/utils/numbers.d.ts", + "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/esm/utils/strings.d.ts", + ] + `); + + expect(files.cjs).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/cjs/index.d.cts", + "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/cjs/sum.d.cts", + "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/cjs/utils/numbers.d.cts", + "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/cjs/utils/strings.d.cts", + ] + `); + }); + + test('should use declarationDir when not set dts.distPath', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'declaration-dir'); + const distTypesPath = join(fixturePath, 'dist-types'); + + await buildAndGetResults({ fixturePath, type: 'dts' }); + + const distTypeFiles = await globContentJSON(distTypesPath, { + absolute: true, + }); + const distTypeFilePaths = Object.keys(distTypeFiles).sort(); + + expect(distTypeFilePaths).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle-false/declaration-dir/dist-types/index.d.ts", + "/tests/integration/dts-tsgo/bundle-false/declaration-dir/dist-types/sum.d.ts", + "/tests/integration/dts-tsgo/bundle-false/declaration-dir/dist-types/utils/numbers.d.ts", + "/tests/integration/dts-tsgo/bundle-false/declaration-dir/dist-types/utils/strings.d.ts", + ] + `); + }); + + test('should clean dts dist files', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'clean'); + + const checkFiles = await createTempFiles(fixturePath, false); + + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + + for (const file of checkFiles) { + expect(existsSync(file)).toBe(false); + } + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/esm/index.d.ts", + "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/esm/sum.d.ts", + "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/esm/utils/numbers.d.ts", + "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/esm/utils/strings.d.ts", + ] + `); + + expect(files.cjs).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/cjs/index.d.ts", + "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/cjs/sum.d.ts", + "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/cjs/utils/numbers.d.ts", + "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/cjs/utils/strings.d.ts", + ] + `); + }); + + test('should emit error when tsconfig not found', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'tsconfig-path'); + await createTempFiles(fixturePath, false); + + try { + await buildAndGetResults({ fixturePath, type: 'dts' }); + } catch (err: any) { + expect(stripAnsi(err.message)).toMatchInlineSnapshot( + `"Failed to resolve tsconfig file "/tests/integration/dts-tsgo/bundle-false/tsconfig-path/path_not_exist/tsconfig.json" from /tests/integration/dts-tsgo/bundle-false/tsconfig-path. Please ensure that the file exists."`, + ); + } + }); + + test('alias', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'alias'); + const { contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(contents.esm).toMatchInlineSnapshot(` + { + "/tests/integration/dts-tsgo/bundle-false/alias/dist/esm/index.d.ts": "export {} from '../../compile/prebundle-pkg'; + ", + } + `); + + expect(contents.cjs).toMatchInlineSnapshot(` + { + "/tests/integration/dts-tsgo/bundle-false/alias/dist/cjs/index.d.ts": "export {} from '../../compile/prebundle-pkg'; + ", + } + `); + }); + + test('declarationMap', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'declaration-map'); + const { files, contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle-false/declaration-map/dist/esm/index.d.ts", + "/tests/integration/dts-tsgo/bundle-false/declaration-map/dist/esm/index.d.ts.map", + ] + `); + + expect(files.cjs).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle-false/declaration-map/dist/cjs/index.d.cts", + "/tests/integration/dts-tsgo/bundle-false/declaration-map/dist/cjs/index.d.cts.map", + ] + `); + + const { content: indexDtsEsm } = queryContent(contents.esm, 'index.d.ts', { + basename: true, + }); + const { content: indexDtsCjs } = queryContent(contents.cjs, 'index.d.cts', { + basename: true, + }); + const { content: indexMapEsm } = queryContent( + contents.esm, + 'index.d.ts.map', + { + basename: true, + }, + ); + const { content: indexMapCjs } = queryContent( + contents.cjs, + 'index.d.cts.map', + { + basename: true, + }, + ); + expect(indexDtsEsm).toContain('//# sourceMappingURL=index.d.ts.map'); + expect(indexDtsCjs).toContain('//# sourceMappingURL=index.d.cts.map'); + expect(indexMapEsm).toContain('"file":"index.d.ts"'); + expect(indexMapCjs).toContain('"file":"index.d.cts"'); + }); +}); From 58399a0505ad226e3f550afa06007ad59577d68e Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 17:44:51 +0800 Subject: [PATCH 05/14] test: add bundle dts test --- pnpm-lock.yaml | 28 +++ .../__snapshots__/bundle.test.ts.snap | 136 +++++++++++ tests/integration/dts-tsgo/bundle.test.ts | 223 ++++++++++++++++++ .../dts-tsgo/bundle/__fixtures__/src/index.ts | 3 + .../dts-tsgo/bundle/__fixtures__/src/sum.ts | 5 + .../bundle/__fixtures__/src/utils/numbers.ts | 3 + .../bundle/__fixtures__/src/utils/strings.ts | 3 + .../bundle/__fixtures__/tsconfig.json | 5 + .../bundle/abort-on-error/package.json | 6 + .../bundle/abort-on-error/rslib.config.ts | 22 ++ .../bundle/abort-on-error/src/const.ts | 3 + .../bundle/abort-on-error/src/index.ts | 6 + .../bundle/abort-on-error/tsconfig.json | 5 + .../bundle/absolute-entry/package.json | 6 + .../bundle/absolute-entry/rslib.config.ts | 22 ++ .../bundle/auto-extension/package.json | 6 + .../bundle/auto-extension/rslib.config.ts | 22 ++ .../dts-tsgo/bundle/basic/package.json | 6 + .../dts-tsgo/bundle/basic/rslib.config.ts | 29 +++ .../dts-tsgo/bundle/bundle-name/package.json | 6 + .../bundle/bundle-name/rslib.config.ts | 21 ++ .../bundle/bundled-packages/package.json | 9 + .../bundle/bundled-packages/rslib.config.ts | 58 +++++ .../bundle/bundled-packages/src/index.ts | 1 + .../bundle/bundled-packages/tsconfig.json | 5 + .../dts-tsgo/bundle/clean/package.json | 6 + .../dts-tsgo/bundle/clean/rslib.config.ts | 31 +++ .../dts-tsgo/bundle/dist-path/package.json | 6 + .../dts-tsgo/bundle/dist-path/rslib.config.ts | 22 ++ .../bundle/multiple-entries/package.json | 6 + .../bundle/multiple-entries/rslib.config.ts | 30 +++ .../dts-tsgo/bundle/rootdir/package.json | 9 + .../dts-tsgo/bundle/rootdir/rslib.config.ts | 28 +++ .../dts-tsgo/bundle/rootdir/tsconfig.json | 4 + 34 files changed, 781 insertions(+) create mode 100644 tests/integration/dts-tsgo/__snapshots__/bundle.test.ts.snap create mode 100644 tests/integration/dts-tsgo/bundle.test.ts create mode 100644 tests/integration/dts-tsgo/bundle/__fixtures__/src/index.ts create mode 100644 tests/integration/dts-tsgo/bundle/__fixtures__/src/sum.ts create mode 100644 tests/integration/dts-tsgo/bundle/__fixtures__/src/utils/numbers.ts create mode 100644 tests/integration/dts-tsgo/bundle/__fixtures__/src/utils/strings.ts create mode 100644 tests/integration/dts-tsgo/bundle/__fixtures__/tsconfig.json create mode 100644 tests/integration/dts-tsgo/bundle/abort-on-error/package.json create mode 100644 tests/integration/dts-tsgo/bundle/abort-on-error/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle/abort-on-error/src/const.ts create mode 100644 tests/integration/dts-tsgo/bundle/abort-on-error/src/index.ts create mode 100644 tests/integration/dts-tsgo/bundle/abort-on-error/tsconfig.json create mode 100644 tests/integration/dts-tsgo/bundle/absolute-entry/package.json create mode 100644 tests/integration/dts-tsgo/bundle/absolute-entry/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle/auto-extension/package.json create mode 100644 tests/integration/dts-tsgo/bundle/auto-extension/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle/basic/package.json create mode 100644 tests/integration/dts-tsgo/bundle/basic/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle/bundle-name/package.json create mode 100644 tests/integration/dts-tsgo/bundle/bundle-name/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle/bundled-packages/package.json create mode 100644 tests/integration/dts-tsgo/bundle/bundled-packages/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle/bundled-packages/src/index.ts create mode 100644 tests/integration/dts-tsgo/bundle/bundled-packages/tsconfig.json create mode 100644 tests/integration/dts-tsgo/bundle/clean/package.json create mode 100644 tests/integration/dts-tsgo/bundle/clean/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle/dist-path/package.json create mode 100644 tests/integration/dts-tsgo/bundle/dist-path/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle/multiple-entries/package.json create mode 100644 tests/integration/dts-tsgo/bundle/multiple-entries/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle/rootdir/package.json create mode 100644 tests/integration/dts-tsgo/bundle/rootdir/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/bundle/rootdir/tsconfig.json diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c5e073f37..f72e3ec5d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -732,6 +732,34 @@ importers: tests/integration/dts-tsgo/bundle-false/tsconfig-path: {} + tests/integration/dts-tsgo/bundle/abort-on-error: {} + + tests/integration/dts-tsgo/bundle/absolute-entry: {} + + tests/integration/dts-tsgo/bundle/auto-extension: {} + + tests/integration/dts-tsgo/bundle/basic: {} + + tests/integration/dts-tsgo/bundle/bundle-name: {} + + tests/integration/dts-tsgo/bundle/bundled-packages: + devDependencies: + '@reduxjs/toolkit': + specifier: ^2.8.2 + version: 2.8.2(react@19.1.1) + + tests/integration/dts-tsgo/bundle/clean: {} + + tests/integration/dts-tsgo/bundle/dist-path: {} + + tests/integration/dts-tsgo/bundle/multiple-entries: {} + + tests/integration/dts-tsgo/bundle/rootdir: + devDependencies: + '@types/chromecast-caf-sender': + specifier: ^1.0.11 + version: 1.0.11 + tests/integration/dts/build/__references__: {} tests/integration/dts/build/abort-on-error: {} diff --git a/tests/integration/dts-tsgo/__snapshots__/bundle.test.ts.snap b/tests/integration/dts-tsgo/__snapshots__/bundle.test.ts.snap new file mode 100644 index 000000000..0b1e7062c --- /dev/null +++ b/tests/integration/dts-tsgo/__snapshots__/bundle.test.ts.snap @@ -0,0 +1,136 @@ +// Rstest Snapshot v1 + +exports[`dts with tsgo when bundle: true > basic 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 { } +", +} +`; + +exports[`dts with tsgo when bundle: true > multiple entries 3`] = ` +[ + "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 { } +", + "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 { } +", + "export declare const numSum: number; + +export declare const strSum: string; + +export { } +", + "export declare const numSum: number; + +export declare const strSum: string; + +export { } +", +] +`; + +exports[`dts with tsgo 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 { } +", +} +`; diff --git a/tests/integration/dts-tsgo/bundle.test.ts b/tests/integration/dts-tsgo/bundle.test.ts new file mode 100644 index 000000000..23bcf3922 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle.test.ts @@ -0,0 +1,223 @@ +import { spawnSync } from 'node:child_process'; +import { existsSync } from 'node:fs'; +import { join, normalize } from 'node:path'; +import { describe, expect, test } from '@rstest/core'; +import stripAnsi from 'strip-ansi'; +import { + buildAndGetResults, + createTempFiles, + globContentJSON, + proxyConsole, + queryContent, +} from 'test-helper'; + +describe('dts with tsgo when bundle: true', () => { + test('basic', async () => { + const fixturePath = join(__dirname, 'bundle', 'basic'); + const { files, entries } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts-tsgo/bundle/basic/dist/esm/index.d.ts", + ] + `, + ); + + expect(files.cjs).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts-tsgo/bundle/basic/dist/cjs/index.d.ts", + ] + `, + ); + + expect(entries).toMatchSnapshot(); + }); + + test('distPath', async () => { + const fixturePath = join(__dirname, 'bundle', 'dist-path'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts-tsgo/bundle/dist-path/dist/custom/index.d.ts", + ] + `, + ); + }); + + test('abortOnError: false', async () => { + const fixturePath = join(__dirname, 'bundle', 'abort-on-error'); + + const result = spawnSync('npx', ['rslib', 'build'], { + cwd: fixturePath, + // do not show output in test console + stdio: 'ignore', + shell: true, + }); + + expect(result.status).toBe(0); + }); + + test('autoExtension: true', async () => { + const fixturePath = join(__dirname, 'bundle', 'auto-extension'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.cjs).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts-tsgo/bundle/auto-extension/dist/cjs/index.d.cts", + ] + `, + ); + }); + + test('bundleName -- set source.entry', async () => { + const fixturePath = join(__dirname, 'bundle', 'bundle-name'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts-tsgo/bundle/bundle-name/dist/esm/bundleName.d.ts", + ] + `, + ); + }); + + test('entry is an absolute path', async () => { + const fixturePath = join(__dirname, 'bundle', 'absolute-entry'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot( + ` + [ + "/tests/integration/dts-tsgo/bundle/absolute-entry/dist/esm/index.d.ts", + ] + `, + ); + }); + + 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(` + [ + "/tests/integration/dts-tsgo/bundle/rootdir/dist/esm/index.d.ts", + ] + `); + + expect(files.cjs).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle/rootdir/dist/cjs/index.d.ts", + ] + `); + + expect(entries).toMatchSnapshot(); + }); + + test('should clean dts dist files and .rslib folder', async () => { + const fixturePath = join(__dirname, 'bundle', 'clean'); + + const checkFiles = await createTempFiles(fixturePath, true); + + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + + for (const file of checkFiles) { + expect(existsSync(file)).toBe(false); + } + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle/clean/dist-types/esm/index.d.ts", + ] + `); + + expect(files.cjs).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle/clean/dist-types/cjs/index.d.ts", + ] + `); + }); + + test('multiple entries', async () => { + const fixturePath = join(__dirname, 'bundle', 'multiple-entries'); + const { files, contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle/multiple-entries/dist/esm/index.d.ts", + "/tests/integration/dts-tsgo/bundle/multiple-entries/dist/esm/sum.d.ts", + ] + `); + + expect(files.cjs).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/bundle/multiple-entries/dist/cjs/index.d.ts", + "/tests/integration/dts-tsgo/bundle/multiple-entries/dist/cjs/sum.d.ts", + ] + `); + + const { content: indexEsm } = queryContent(contents.esm, 'index.d.ts', { + basename: true, + }); + const { content: indexCjs } = queryContent(contents.cjs, 'index.d.ts', { + basename: true, + }); + const { content: sumEsm } = queryContent(contents.esm, 'sum.d.ts', { + basename: true, + }); + const { content: sumCjs } = queryContent(contents.cjs, 'sum.d.ts', { + basename: true, + }); + + expect([indexEsm, indexCjs, sumEsm, sumCjs]).toMatchSnapshot(); + }); + + test('override with bundledPackages', async () => { + const fixturePath = join(__dirname, 'bundle', 'bundled-packages'); + const { entries } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + // default + expect(entries.esm0).toContain(`import { Action } from 'redux';`); + + // override empty array + expect(entries.esm1).toMatchInlineSnapshot(` + " + export * from "@reduxjs/toolkit"; + + export { } + " + `); + + // override with bundledPackages + expect(entries.esm2).not.toContain(`import { Action } from 'redux';`); + }); +}); diff --git a/tests/integration/dts-tsgo/bundle/__fixtures__/src/index.ts b/tests/integration/dts-tsgo/bundle/__fixtures__/src/index.ts new file mode 100644 index 000000000..fb828f24f --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/__fixtures__/src/index.ts @@ -0,0 +1,3 @@ +export * from './sum'; +export * from './utils/numbers'; +export * from './utils/strings'; diff --git a/tests/integration/dts-tsgo/bundle/__fixtures__/src/sum.ts b/tests/integration/dts-tsgo/bundle/__fixtures__/src/sum.ts new file mode 100644 index 000000000..2759c8823 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/__fixtures__/src/sum.ts @@ -0,0 +1,5 @@ +import { num1, num2, num3 } from './utils/numbers'; +import { str1, str2, str3 } from './utils/strings'; + +export const numSum = num1 + num2 + num3; +export const strSum = str1 + str2 + str3; diff --git a/tests/integration/dts-tsgo/bundle/__fixtures__/src/utils/numbers.ts b/tests/integration/dts-tsgo/bundle/__fixtures__/src/utils/numbers.ts new file mode 100644 index 000000000..c1d2a8cd2 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/__fixtures__/src/utils/numbers.ts @@ -0,0 +1,3 @@ +export const num1 = 1; +export const num2 = 2; +export const num3 = 3; diff --git a/tests/integration/dts-tsgo/bundle/__fixtures__/src/utils/strings.ts b/tests/integration/dts-tsgo/bundle/__fixtures__/src/utils/strings.ts new file mode 100644 index 000000000..d35b5a606 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/__fixtures__/src/utils/strings.ts @@ -0,0 +1,3 @@ +export const str1 = 'str1'; +export const str2 = 'str2'; +export const str3 = 'str3'; diff --git a/tests/integration/dts-tsgo/bundle/__fixtures__/tsconfig.json b/tests/integration/dts-tsgo/bundle/__fixtures__/tsconfig.json new file mode 100644 index 000000000..5ee8bfa51 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/__fixtures__/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": {}, + "include": ["src"] +} diff --git a/tests/integration/dts-tsgo/bundle/abort-on-error/package.json b/tests/integration/dts-tsgo/bundle/abort-on-error/package.json new file mode 100644 index 000000000..cc6c4a5f6 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/abort-on-error/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-abort-on-error-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle/abort-on-error/rslib.config.ts b/tests/integration/dts-tsgo/bundle/abort-on-error/rslib.config.ts new file mode 100644 index 000000000..140517c6f --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/abort-on-error/rslib.config.ts @@ -0,0 +1,22 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: { + bundle: true, + abortOnError: false, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: './src/index.ts', + }, + tsconfigPath: 'tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle/abort-on-error/src/const.ts b/tests/integration/dts-tsgo/bundle/abort-on-error/src/const.ts new file mode 100644 index 000000000..88a6d19f4 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/abort-on-error/src/const.ts @@ -0,0 +1,3 @@ +export interface A { + a: number; +} diff --git a/tests/integration/dts-tsgo/bundle/abort-on-error/src/index.ts b/tests/integration/dts-tsgo/bundle/abort-on-error/src/index.ts new file mode 100644 index 000000000..2595bdbbf --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/abort-on-error/src/index.ts @@ -0,0 +1,6 @@ +import type { A } from './const'; + +export const getA = (item: A) => { + item.a = '0'; + return item; +}; diff --git a/tests/integration/dts-tsgo/bundle/abort-on-error/tsconfig.json b/tests/integration/dts-tsgo/bundle/abort-on-error/tsconfig.json new file mode 100644 index 000000000..5ee8bfa51 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/abort-on-error/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": {}, + "include": ["src"] +} diff --git a/tests/integration/dts-tsgo/bundle/absolute-entry/package.json b/tests/integration/dts-tsgo/bundle/absolute-entry/package.json new file mode 100644 index 000000000..360f7cd7a --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/absolute-entry/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-absolute-entry-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle/absolute-entry/rslib.config.ts b/tests/integration/dts-tsgo/bundle/absolute-entry/rslib.config.ts new file mode 100644 index 000000000..2f5ae4cfb --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/absolute-entry/rslib.config.ts @@ -0,0 +1,22 @@ +import { join } from 'node:path'; +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: join(__dirname, '../__fixtures__/src/index.ts'), + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle/auto-extension/package.json b/tests/integration/dts-tsgo/bundle/auto-extension/package.json new file mode 100644 index 000000000..5b072b527 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/auto-extension/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-auto-extension-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle/auto-extension/rslib.config.ts b/tests/integration/dts-tsgo/bundle/auto-extension/rslib.config.ts new file mode 100644 index 000000000..1c2ee6da6 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/auto-extension/rslib.config.ts @@ -0,0 +1,22 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleCjsConfig({ + dts: { + bundle: true, + autoExtension: true, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: '../__fixtures__/src/index.ts', + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle/basic/package.json b/tests/integration/dts-tsgo/bundle/basic/package.json new file mode 100644 index 000000000..82766bb33 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/basic/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-basic-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle/basic/rslib.config.ts b/tests/integration/dts-tsgo/bundle/basic/rslib.config.ts new file mode 100644 index 000000000..0b02f639e --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/basic/rslib.config.ts @@ -0,0 +1,29 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: '../__fixtures__/src/index.ts', + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle/bundle-name/package.json b/tests/integration/dts-tsgo/bundle/bundle-name/package.json new file mode 100644 index 000000000..c5437c022 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/bundle-name/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-bundle-name-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle/bundle-name/rslib.config.ts b/tests/integration/dts-tsgo/bundle/bundle-name/rslib.config.ts new file mode 100644 index 000000000..39d77531b --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/bundle-name/rslib.config.ts @@ -0,0 +1,21 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + bundleName: '../__fixtures__/src/index.ts', + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle/bundled-packages/package.json b/tests/integration/dts-tsgo/bundle/bundled-packages/package.json new file mode 100644 index 000000000..bdc3d7e28 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/bundled-packages/package.json @@ -0,0 +1,9 @@ +{ + "name": "dts-tsgo-bundle-bundled-packages-test", + "version": "1.0.0", + "private": true, + "type": "module", + "devDependencies": { + "@reduxjs/toolkit": "^2.8.2" + } +} diff --git a/tests/integration/dts-tsgo/bundle/bundled-packages/rslib.config.ts b/tests/integration/dts-tsgo/bundle/bundled-packages/rslib.config.ts new file mode 100644 index 000000000..b74f3cc4c --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/bundled-packages/rslib.config.ts @@ -0,0 +1,58 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + output: { + distPath: { + root: './dist/esm/default', + }, + }, + }), + generateBundleEsmConfig({ + dts: { + bundle: { + bundledPackages: [], + }, + experiments: { + tsgo: true, + }, + }, + output: { + distPath: { + root: './dist/esm/override-empty-array', + }, + }, + }), + generateBundleEsmConfig({ + dts: { + bundle: { + bundledPackages: [ + '@reduxjs/toolkit', + '@standard-schema/spec', + '@standard-schema/utils', + 'immer', + 'redux', + 'redux-thunk', + 'reselect', + ], + }, + experiments: { + tsgo: true, + }, + }, + output: { + distPath: { + root: './dist/esm/override-array-string', + }, + }, + }), + ], +}); diff --git a/tests/integration/dts-tsgo/bundle/bundled-packages/src/index.ts b/tests/integration/dts-tsgo/bundle/bundled-packages/src/index.ts new file mode 100644 index 000000000..a9c1fbbda --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/bundled-packages/src/index.ts @@ -0,0 +1 @@ +export * from '@reduxjs/toolkit'; diff --git a/tests/integration/dts-tsgo/bundle/bundled-packages/tsconfig.json b/tests/integration/dts-tsgo/bundle/bundled-packages/tsconfig.json new file mode 100644 index 000000000..5ee8bfa51 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/bundled-packages/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": {}, + "include": ["src"] +} diff --git a/tests/integration/dts-tsgo/bundle/clean/package.json b/tests/integration/dts-tsgo/bundle/clean/package.json new file mode 100644 index 000000000..a47725e0d --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/clean/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-clean-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle/clean/rslib.config.ts b/tests/integration/dts-tsgo/bundle/clean/rslib.config.ts new file mode 100644 index 000000000..93809d342 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/clean/rslib.config.ts @@ -0,0 +1,31 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: { + bundle: true, + distPath: './dist-types/esm', + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + dts: { + bundle: true, + distPath: './dist-types/cjs', + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: '../__fixtures__/src/index.ts', + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle/dist-path/package.json b/tests/integration/dts-tsgo/bundle/dist-path/package.json new file mode 100644 index 000000000..99b80f57a --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/dist-path/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-dist-path-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle/dist-path/rslib.config.ts b/tests/integration/dts-tsgo/bundle/dist-path/rslib.config.ts new file mode 100644 index 000000000..66b94b940 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/dist-path/rslib.config.ts @@ -0,0 +1,22 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: { + bundle: true, + distPath: './dist/custom', + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: '../__fixtures__/src/index.ts', + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle/multiple-entries/package.json b/tests/integration/dts-tsgo/bundle/multiple-entries/package.json new file mode 100644 index 000000000..f91e66445 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/multiple-entries/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-bundle-multiple-entries-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/bundle/multiple-entries/rslib.config.ts b/tests/integration/dts-tsgo/bundle/multiple-entries/rslib.config.ts new file mode 100644 index 000000000..75866e157 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/multiple-entries/rslib.config.ts @@ -0,0 +1,30 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: '../__fixtures__/src/index.ts', + sum: '../__fixtures__/src/sum.ts', + }, + tsconfigPath: '../__fixtures__/tsconfig.json', + }, +}); diff --git a/tests/integration/dts-tsgo/bundle/rootdir/package.json b/tests/integration/dts-tsgo/bundle/rootdir/package.json new file mode 100644 index 000000000..219103863 --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/rootdir/package.json @@ -0,0 +1,9 @@ +{ + "name": "dts-tsgo-bundle-rootdir-test", + "version": "1.0.0", + "private": true, + "type": "module", + "devDependencies": { + "@types/chromecast-caf-sender": "^1.0.11" + } +} diff --git a/tests/integration/dts-tsgo/bundle/rootdir/rslib.config.ts b/tests/integration/dts-tsgo/bundle/rootdir/rslib.config.ts new file mode 100644 index 000000000..29bcae09b --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/rootdir/rslib.config.ts @@ -0,0 +1,28 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + }), + generateBundleCjsConfig({ + dts: { + bundle: true, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: '../__fixtures__/src/index.ts', + }, + }, +}); diff --git a/tests/integration/dts-tsgo/bundle/rootdir/tsconfig.json b/tests/integration/dts-tsgo/bundle/rootdir/tsconfig.json new file mode 100644 index 000000000..46e1f344f --- /dev/null +++ b/tests/integration/dts-tsgo/bundle/rootdir/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "@rslib/tsconfig/base", + "include": ["../__fixtures__/src", "node_modules/@types"] +} From 760e59d372a662a7c05a8b46da8d652a1e7dda0d Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 19:11:58 +0800 Subject: [PATCH 06/14] test: add -b dts test --- pnpm-lock.yaml | 16 ++ tests/integration/dts-tsgo/build.test.ts | 203 ++++++++++++++++++ .../build/__references__/package.json | 6 + .../build/__references__/src/index.ts | 1 + .../build/__references__/tsconfig.json | 9 + .../build/abort-on-error/package.json | 6 + .../build/abort-on-error/rslib.config.ts | 23 ++ .../build/abort-on-error/src/const.ts | 3 + .../build/abort-on-error/src/index.ts | 6 + .../build/abort-on-error/tsconfig.json | 14 ++ .../build/auto-extension/package.json | 6 + .../build/auto-extension/rslib.config.ts | 24 +++ .../build/auto-extension/src/index.ts | 1 + .../dts-tsgo/build/auto-extension/src/sum.ts | 10 + .../build/auto-extension/tsconfig.json | 14 ++ .../dts-tsgo/build/basic/package.json | 6 + .../dts-tsgo/build/basic/rslib.config.ts | 22 ++ .../dts-tsgo/build/basic/src/index.ts | 1 + .../dts-tsgo/build/basic/src/sum.ts | 10 + .../dts-tsgo/build/basic/tsconfig.json | 14 ++ .../dts-tsgo/build/clean/package.json | 6 + .../dts-tsgo/build/clean/rslib.config.ts | 40 ++++ .../dts-tsgo/build/clean/src/index.ts | 1 + .../dts-tsgo/build/clean/src/sum.ts | 10 + .../dts-tsgo/build/clean/tsconfig.cjs.json | 14 ++ .../dts-tsgo/build/clean/tsconfig.esm.json | 14 ++ .../build/declaration-map/package.json | 6 + .../build/declaration-map/rslib.config.ts | 33 +++ .../build/declaration-map/src/index.ts | 1 + .../build/declaration-map/tsconfig.cjs.json | 15 ++ .../build/declaration-map/tsconfig.esm.json | 15 ++ .../dts-tsgo/build/dist-path/package.json | 6 + .../dts-tsgo/build/dist-path/rslib.config.ts | 23 ++ .../dts-tsgo/build/dist-path/src/index.ts | 1 + .../dts-tsgo/build/dist-path/tsconfig.json | 14 ++ .../dts-tsgo/build/process-files/package.json | 5 + .../build/process-files/rslib.config.ts | 29 +++ .../dts-tsgo/build/process-files/src/index.ts | 1 + .../build/process-files/tsconfig.json | 14 ++ tests/integration/dts-tsgo/bundle.test.ts | 11 +- 40 files changed, 645 insertions(+), 9 deletions(-) create mode 100644 tests/integration/dts-tsgo/build.test.ts create mode 100644 tests/integration/dts-tsgo/build/__references__/package.json create mode 100644 tests/integration/dts-tsgo/build/__references__/src/index.ts create mode 100644 tests/integration/dts-tsgo/build/__references__/tsconfig.json create mode 100644 tests/integration/dts-tsgo/build/abort-on-error/package.json create mode 100644 tests/integration/dts-tsgo/build/abort-on-error/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/build/abort-on-error/src/const.ts create mode 100644 tests/integration/dts-tsgo/build/abort-on-error/src/index.ts create mode 100644 tests/integration/dts-tsgo/build/abort-on-error/tsconfig.json create mode 100644 tests/integration/dts-tsgo/build/auto-extension/package.json create mode 100644 tests/integration/dts-tsgo/build/auto-extension/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/build/auto-extension/src/index.ts create mode 100644 tests/integration/dts-tsgo/build/auto-extension/src/sum.ts create mode 100644 tests/integration/dts-tsgo/build/auto-extension/tsconfig.json create mode 100644 tests/integration/dts-tsgo/build/basic/package.json create mode 100644 tests/integration/dts-tsgo/build/basic/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/build/basic/src/index.ts create mode 100644 tests/integration/dts-tsgo/build/basic/src/sum.ts create mode 100644 tests/integration/dts-tsgo/build/basic/tsconfig.json create mode 100644 tests/integration/dts-tsgo/build/clean/package.json create mode 100644 tests/integration/dts-tsgo/build/clean/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/build/clean/src/index.ts create mode 100644 tests/integration/dts-tsgo/build/clean/src/sum.ts create mode 100644 tests/integration/dts-tsgo/build/clean/tsconfig.cjs.json create mode 100644 tests/integration/dts-tsgo/build/clean/tsconfig.esm.json create mode 100644 tests/integration/dts-tsgo/build/declaration-map/package.json create mode 100644 tests/integration/dts-tsgo/build/declaration-map/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/build/declaration-map/src/index.ts create mode 100644 tests/integration/dts-tsgo/build/declaration-map/tsconfig.cjs.json create mode 100644 tests/integration/dts-tsgo/build/declaration-map/tsconfig.esm.json create mode 100644 tests/integration/dts-tsgo/build/dist-path/package.json create mode 100644 tests/integration/dts-tsgo/build/dist-path/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/build/dist-path/src/index.ts create mode 100644 tests/integration/dts-tsgo/build/dist-path/tsconfig.json create mode 100644 tests/integration/dts-tsgo/build/process-files/package.json create mode 100644 tests/integration/dts-tsgo/build/process-files/rslib.config.ts create mode 100644 tests/integration/dts-tsgo/build/process-files/src/index.ts create mode 100644 tests/integration/dts-tsgo/build/process-files/tsconfig.json diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f72e3ec5d..a4370d7fa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -714,6 +714,22 @@ importers: tests/integration/directive/shebang: {} + tests/integration/dts-tsgo/build/__references__: {} + + tests/integration/dts-tsgo/build/abort-on-error: {} + + tests/integration/dts-tsgo/build/auto-extension: {} + + tests/integration/dts-tsgo/build/basic: {} + + tests/integration/dts-tsgo/build/clean: {} + + tests/integration/dts-tsgo/build/declaration-map: {} + + tests/integration/dts-tsgo/build/dist-path: {} + + tests/integration/dts-tsgo/build/process-files: {} + tests/integration/dts-tsgo/bundle-false/abort-on-error: {} tests/integration/dts-tsgo/bundle-false/alias: {} diff --git a/tests/integration/dts-tsgo/build.test.ts b/tests/integration/dts-tsgo/build.test.ts new file mode 100644 index 000000000..c9b3be2d6 --- /dev/null +++ b/tests/integration/dts-tsgo/build.test.ts @@ -0,0 +1,203 @@ +import { spawnSync } from 'node:child_process'; +import { existsSync } from 'node:fs'; +import { join } from 'node:path'; +import { describe, expect, test } from '@rstest/core'; +import { buildAndGetResults, createTempFiles, queryContent } from 'test-helper'; + +describe('dts with tsgo when build: true', () => { + test('basic', async () => { + const fixturePath = join(__dirname, 'build', 'basic'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/build/basic/dist/esm/index.d.ts", + "/tests/integration/dts-tsgo/build/basic/dist/esm/sum.d.ts", + ] + `); + + const referenceDistPath = join( + fixturePath, + '../__references__/dist/index.d.ts', + ); + expect(existsSync(referenceDistPath)).toBeTruthy(); + + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); + expect(existsSync(buildInfoPath)).toBeTruthy(); + }); + + test('distPath', async () => { + const fixturePath = join(__dirname, 'build', 'dist-path'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/build/dist-path/dist/custom/index.d.ts", + ] + `); + + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); + 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-tsgo/build/auto-extension/dist/types/index.d.cts", + "/tests/integration/dts-tsgo/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({ + fixturePath, + type: 'dts', + }); + + expect(contents.esm).toMatchInlineSnapshot(` + { + "/tests/integration/dts-tsgo/build/process-files/dist/esm/index.d.mts": "/*! hello banner dts build*/ + export declare const num1 = 1; + + /*! hello banner dts build*/ + ", + } + `); + + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); + expect(existsSync(buildInfoPath)).toBeTruthy(); + }); + + test('abortOnError: false', async () => { + const fixturePath = join(__dirname, 'build', 'abort-on-error'); + + const result = spawnSync('npx', ['rslib', 'build'], { + cwd: fixturePath, + // do not show output in test console + stdio: 'ignore', + shell: true, + }); + + expect(result.status).toBe(0); + + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); + expect(existsSync(buildInfoPath)).toBeTruthy(); + }); + + test('should clean dts dist files', async () => { + const fixturePath = join(__dirname, 'build', 'clean'); + + const checkFiles = await createTempFiles(fixturePath, false); + + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + + for (const file of checkFiles) { + expect(existsSync(file)).toBe(false); + } + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/build/clean/dist-types/esm/index.d.ts", + "/tests/integration/dts-tsgo/build/clean/dist-types/esm/sum.d.ts", + ] + `); + + expect(files.cjs).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/build/clean/dist-types/cjs/index.d.ts", + "/tests/integration/dts-tsgo/build/clean/dist-types/cjs/sum.d.ts", + ] + `); + + const referenceDistPath = join( + fixturePath, + '../__references__/dist/index.d.ts', + ); + expect(existsSync(referenceDistPath)).toBeTruthy(); + + const cjsBuildInfoPath = join(fixturePath, 'tsconfig.cjs.tsbuildinfo'); + expect(existsSync(cjsBuildInfoPath)).toBeTruthy(); + + const esmBuildInfoPath = join(fixturePath, 'tsconfig.esm.tsbuildinfo'); + expect(existsSync(esmBuildInfoPath)).toBeTruthy(); + }); + + test('declarationMap', async () => { + const fixturePath = join(__dirname, 'build', 'declaration-map'); + const { files, contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/build/declaration-map/dist/esm/index.d.ts", + "/tests/integration/dts-tsgo/build/declaration-map/dist/esm/index.d.ts.map", + ] + `); + + expect(files.cjs).toMatchInlineSnapshot(` + [ + "/tests/integration/dts-tsgo/build/declaration-map/dist/cjs/index.d.cts", + "/tests/integration/dts-tsgo/build/declaration-map/dist/cjs/index.d.cts.map", + ] + `); + + const { content: indexDtsEsm } = queryContent(contents.esm, 'index.d.ts', { + basename: true, + }); + const { content: indexDtsCjs } = queryContent(contents.cjs, 'index.d.cts', { + basename: true, + }); + const { content: indexMapEsm } = queryContent( + contents.esm, + 'index.d.ts.map', + { + basename: true, + }, + ); + const { content: indexMapCjs } = queryContent( + contents.cjs, + 'index.d.cts.map', + { + basename: true, + }, + ); + expect(indexDtsEsm).toContain('//# sourceMappingURL=index.d.ts.map'); + expect(indexDtsCjs).toContain('//# sourceMappingURL=index.d.cts.map'); + expect(indexMapEsm).toContain('"file":"index.d.ts"'); + expect(indexMapCjs).toContain('"file":"index.d.cts"'); + + const referenceEsmDistPath = join( + fixturePath, + '../__references__/dist/index.d.ts', + ); + expect(existsSync(referenceEsmDistPath)).toBeTruthy(); + + // TODO: can not rename dts files in reference yet + // const referenceCjsDistPath = join( + // fixturePath, + // '../__references__/dist/index.d.cts', + // ); + // expect(existsSync(referenceCjsDistPath)).toBeTruthy(); + + const esmBuildInfoPath = join(fixturePath, 'tsconfig.esm.tsbuildinfo'); + const cjsBuildInfoPath = join(fixturePath, 'tsconfig.cjs.tsbuildinfo'); + expect(existsSync(esmBuildInfoPath)).toBeTruthy(); + expect(existsSync(cjsBuildInfoPath)).toBeTruthy(); + }); +}); diff --git a/tests/integration/dts-tsgo/build/__references__/package.json b/tests/integration/dts-tsgo/build/__references__/package.json new file mode 100644 index 000000000..06d356ba9 --- /dev/null +++ b/tests/integration/dts-tsgo/build/__references__/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-build-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/build/__references__/src/index.ts b/tests/integration/dts-tsgo/build/__references__/src/index.ts new file mode 100644 index 000000000..37537151f --- /dev/null +++ b/tests/integration/dts-tsgo/build/__references__/src/index.ts @@ -0,0 +1 @@ +export const b = 'hello world'; diff --git a/tests/integration/dts-tsgo/build/__references__/tsconfig.json b/tests/integration/dts-tsgo/build/__references__/tsconfig.json new file mode 100644 index 000000000..56bec502e --- /dev/null +++ b/tests/integration/dts-tsgo/build/__references__/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "src", + "outDir": "dist", + "composite": true + }, + "include": ["src"] +} diff --git a/tests/integration/dts-tsgo/build/abort-on-error/package.json b/tests/integration/dts-tsgo/build/abort-on-error/package.json new file mode 100644 index 000000000..105c00aaf --- /dev/null +++ b/tests/integration/dts-tsgo/build/abort-on-error/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-build-abort-on-error-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/build/abort-on-error/rslib.config.ts b/tests/integration/dts-tsgo/build/abort-on-error/rslib.config.ts new file mode 100644 index 000000000..1199897e0 --- /dev/null +++ b/tests/integration/dts-tsgo/build/abort-on-error/rslib.config.ts @@ -0,0 +1,23 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + bundle: false, + build: true, + abortOnError: false, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: ['./src/**'], + }, + }, +}); diff --git a/tests/integration/dts-tsgo/build/abort-on-error/src/const.ts b/tests/integration/dts-tsgo/build/abort-on-error/src/const.ts new file mode 100644 index 000000000..88a6d19f4 --- /dev/null +++ b/tests/integration/dts-tsgo/build/abort-on-error/src/const.ts @@ -0,0 +1,3 @@ +export interface A { + a: number; +} diff --git a/tests/integration/dts-tsgo/build/abort-on-error/src/index.ts b/tests/integration/dts-tsgo/build/abort-on-error/src/index.ts new file mode 100644 index 000000000..2595bdbbf --- /dev/null +++ b/tests/integration/dts-tsgo/build/abort-on-error/src/index.ts @@ -0,0 +1,6 @@ +import type { A } from './const'; + +export const getA = (item: A) => { + item.a = '0'; + return item; +}; diff --git a/tests/integration/dts-tsgo/build/abort-on-error/tsconfig.json b/tests/integration/dts-tsgo/build/abort-on-error/tsconfig.json new file mode 100644 index 000000000..2f987306b --- /dev/null +++ b/tests/integration/dts-tsgo/build/abort-on-error/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "src", + "declaration": true, + "declarationDir": "./dist/esm" + }, + "include": ["src"], + "references": [ + { + "path": "../__references__" + } + ] +} diff --git a/tests/integration/dts-tsgo/build/auto-extension/package.json b/tests/integration/dts-tsgo/build/auto-extension/package.json new file mode 100644 index 000000000..997fc45b9 --- /dev/null +++ b/tests/integration/dts-tsgo/build/auto-extension/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-build-auto-extension-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/build/auto-extension/rslib.config.ts b/tests/integration/dts-tsgo/build/auto-extension/rslib.config.ts new file mode 100644 index 000000000..6957f5a6c --- /dev/null +++ b/tests/integration/dts-tsgo/build/auto-extension/rslib.config.ts @@ -0,0 +1,24 @@ +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, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: ['./src/**'], + }, + }, +}); diff --git a/tests/integration/dts-tsgo/build/auto-extension/src/index.ts b/tests/integration/dts-tsgo/build/auto-extension/src/index.ts new file mode 100644 index 000000000..cf1f61676 --- /dev/null +++ b/tests/integration/dts-tsgo/build/auto-extension/src/index.ts @@ -0,0 +1 @@ +export * from './sum'; diff --git a/tests/integration/dts-tsgo/build/auto-extension/src/sum.ts b/tests/integration/dts-tsgo/build/auto-extension/src/sum.ts new file mode 100644 index 000000000..bc0dd4eba --- /dev/null +++ b/tests/integration/dts-tsgo/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-tsgo/build/auto-extension/tsconfig.json b/tests/integration/dts-tsgo/build/auto-extension/tsconfig.json new file mode 100644 index 000000000..fbd119bad --- /dev/null +++ b/tests/integration/dts-tsgo/build/auto-extension/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "src", + "declaration": true, + "declarationDir": "./dist/types" + }, + "include": ["src"], + "references": [ + { + "path": "../__references__" + } + ] +} diff --git a/tests/integration/dts-tsgo/build/basic/package.json b/tests/integration/dts-tsgo/build/basic/package.json new file mode 100644 index 000000000..0377ba55c --- /dev/null +++ b/tests/integration/dts-tsgo/build/basic/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-build-basic-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/build/basic/rslib.config.ts b/tests/integration/dts-tsgo/build/basic/rslib.config.ts new file mode 100644 index 000000000..c474c2166 --- /dev/null +++ b/tests/integration/dts-tsgo/build/basic/rslib.config.ts @@ -0,0 +1,22 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + bundle: false, + build: true, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: ['./src/**'], + }, + }, +}); diff --git a/tests/integration/dts-tsgo/build/basic/src/index.ts b/tests/integration/dts-tsgo/build/basic/src/index.ts new file mode 100644 index 000000000..cf1f61676 --- /dev/null +++ b/tests/integration/dts-tsgo/build/basic/src/index.ts @@ -0,0 +1 @@ +export * from './sum'; diff --git a/tests/integration/dts-tsgo/build/basic/src/sum.ts b/tests/integration/dts-tsgo/build/basic/src/sum.ts new file mode 100644 index 000000000..bc0dd4eba --- /dev/null +++ b/tests/integration/dts-tsgo/build/basic/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-tsgo/build/basic/tsconfig.json b/tests/integration/dts-tsgo/build/basic/tsconfig.json new file mode 100644 index 000000000..2f987306b --- /dev/null +++ b/tests/integration/dts-tsgo/build/basic/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "src", + "declaration": true, + "declarationDir": "./dist/esm" + }, + "include": ["src"], + "references": [ + { + "path": "../__references__" + } + ] +} diff --git a/tests/integration/dts-tsgo/build/clean/package.json b/tests/integration/dts-tsgo/build/clean/package.json new file mode 100644 index 000000000..9a6b2d0bd --- /dev/null +++ b/tests/integration/dts-tsgo/build/clean/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-build-clean-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/build/clean/rslib.config.ts b/tests/integration/dts-tsgo/build/clean/rslib.config.ts new file mode 100644 index 000000000..2baa1b1cf --- /dev/null +++ b/tests/integration/dts-tsgo/build/clean/rslib.config.ts @@ -0,0 +1,40 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + distPath: './dist-types/esm', + bundle: false, + build: true, + experiments: { + tsgo: true, + }, + }, + source: { + tsconfigPath: './tsconfig.esm.json', + }, + }), + generateBundleCjsConfig({ + bundle: false, + dts: { + distPath: './dist-types/cjs', + bundle: false, + build: true, + experiments: { + tsgo: true, + }, + }, + source: { + tsconfigPath: './tsconfig.cjs.json', + }, + }), + ], + source: { + entry: { + index: ['./src/**'], + }, + }, +}); diff --git a/tests/integration/dts-tsgo/build/clean/src/index.ts b/tests/integration/dts-tsgo/build/clean/src/index.ts new file mode 100644 index 000000000..cf1f61676 --- /dev/null +++ b/tests/integration/dts-tsgo/build/clean/src/index.ts @@ -0,0 +1 @@ +export * from './sum'; diff --git a/tests/integration/dts-tsgo/build/clean/src/sum.ts b/tests/integration/dts-tsgo/build/clean/src/sum.ts new file mode 100644 index 000000000..bc0dd4eba --- /dev/null +++ b/tests/integration/dts-tsgo/build/clean/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-tsgo/build/clean/tsconfig.cjs.json b/tests/integration/dts-tsgo/build/clean/tsconfig.cjs.json new file mode 100644 index 000000000..adc0b9181 --- /dev/null +++ b/tests/integration/dts-tsgo/build/clean/tsconfig.cjs.json @@ -0,0 +1,14 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "src", + "declaration": true, + "declarationDir": "./dist-types/cjs" + }, + "include": ["src"], + "references": [ + { + "path": "../__references__" + } + ] +} diff --git a/tests/integration/dts-tsgo/build/clean/tsconfig.esm.json b/tests/integration/dts-tsgo/build/clean/tsconfig.esm.json new file mode 100644 index 000000000..f36149f4f --- /dev/null +++ b/tests/integration/dts-tsgo/build/clean/tsconfig.esm.json @@ -0,0 +1,14 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "src", + "declaration": true, + "declarationDir": "./dist-types/esm" + }, + "include": ["src"], + "references": [ + { + "path": "../__references__" + } + ] +} diff --git a/tests/integration/dts-tsgo/build/declaration-map/package.json b/tests/integration/dts-tsgo/build/declaration-map/package.json new file mode 100644 index 000000000..f4ae81c60 --- /dev/null +++ b/tests/integration/dts-tsgo/build/declaration-map/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-build-declaration-map-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/build/declaration-map/rslib.config.ts b/tests/integration/dts-tsgo/build/declaration-map/rslib.config.ts new file mode 100644 index 000000000..8f1d28294 --- /dev/null +++ b/tests/integration/dts-tsgo/build/declaration-map/rslib.config.ts @@ -0,0 +1,33 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + autoExtension: true, + build: true, + experiments: { + tsgo: true, + }, + }, + source: { + tsconfigPath: './tsconfig.esm.json', + }, + }), + generateBundleCjsConfig({ + bundle: false, + dts: { + autoExtension: true, + build: true, + experiments: { + tsgo: true, + }, + }, + source: { + tsconfigPath: './tsconfig.cjs.json', + }, + }), + ], +}); diff --git a/tests/integration/dts-tsgo/build/declaration-map/src/index.ts b/tests/integration/dts-tsgo/build/declaration-map/src/index.ts new file mode 100644 index 000000000..cc798ff50 --- /dev/null +++ b/tests/integration/dts-tsgo/build/declaration-map/src/index.ts @@ -0,0 +1 @@ +export const a = 1; diff --git a/tests/integration/dts-tsgo/build/declaration-map/tsconfig.cjs.json b/tests/integration/dts-tsgo/build/declaration-map/tsconfig.cjs.json new file mode 100644 index 000000000..acf2f0390 --- /dev/null +++ b/tests/integration/dts-tsgo/build/declaration-map/tsconfig.cjs.json @@ -0,0 +1,15 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "src", + "declaration": true, + "declarationMap": true, + "declarationDir": "./dist/cjs" + }, + "include": ["src"], + "references": [ + { + "path": "../__references__" + } + ] +} diff --git a/tests/integration/dts-tsgo/build/declaration-map/tsconfig.esm.json b/tests/integration/dts-tsgo/build/declaration-map/tsconfig.esm.json new file mode 100644 index 000000000..69c88953d --- /dev/null +++ b/tests/integration/dts-tsgo/build/declaration-map/tsconfig.esm.json @@ -0,0 +1,15 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "src", + "declaration": true, + "declarationMap": true, + "declarationDir": "./dist/esm" + }, + "include": ["src"], + "references": [ + { + "path": "../__references__" + } + ] +} diff --git a/tests/integration/dts-tsgo/build/dist-path/package.json b/tests/integration/dts-tsgo/build/dist-path/package.json new file mode 100644 index 000000000..c44fded9d --- /dev/null +++ b/tests/integration/dts-tsgo/build/dist-path/package.json @@ -0,0 +1,6 @@ +{ + "name": "dts-tsgo-build-dist-path-test", + "version": "1.0.0", + "private": true, + "type": "module" +} diff --git a/tests/integration/dts-tsgo/build/dist-path/rslib.config.ts b/tests/integration/dts-tsgo/build/dist-path/rslib.config.ts new file mode 100644 index 000000000..2c272410d --- /dev/null +++ b/tests/integration/dts-tsgo/build/dist-path/rslib.config.ts @@ -0,0 +1,23 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + dts: { + bundle: false, + build: true, + distPath: './dist/custom', + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: ['./src/**'], + }, + }, +}); diff --git a/tests/integration/dts-tsgo/build/dist-path/src/index.ts b/tests/integration/dts-tsgo/build/dist-path/src/index.ts new file mode 100644 index 000000000..dfa3258cb --- /dev/null +++ b/tests/integration/dts-tsgo/build/dist-path/src/index.ts @@ -0,0 +1 @@ +export const num1 = 1; diff --git a/tests/integration/dts-tsgo/build/dist-path/tsconfig.json b/tests/integration/dts-tsgo/build/dist-path/tsconfig.json new file mode 100644 index 000000000..1ec5d0035 --- /dev/null +++ b/tests/integration/dts-tsgo/build/dist-path/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "src", + "declaration": true, + "declarationDir": "./dist/custom" + }, + "include": ["src"], + "references": [ + { + "path": "../__references__" + } + ] +} diff --git a/tests/integration/dts-tsgo/build/process-files/package.json b/tests/integration/dts-tsgo/build/process-files/package.json new file mode 100644 index 000000000..e94debbcd --- /dev/null +++ b/tests/integration/dts-tsgo/build/process-files/package.json @@ -0,0 +1,5 @@ +{ + "name": "dts-tsgo-build-process-files-test", + "version": "1.0.0", + "private": true +} diff --git a/tests/integration/dts-tsgo/build/process-files/rslib.config.ts b/tests/integration/dts-tsgo/build/process-files/rslib.config.ts new file mode 100644 index 000000000..38b668fad --- /dev/null +++ b/tests/integration/dts-tsgo/build/process-files/rslib.config.ts @@ -0,0 +1,29 @@ +import { defineConfig } from '@rslib/core'; +import { generateBundleEsmConfig } from 'test-helper'; + +export default defineConfig({ + lib: [ + generateBundleEsmConfig({ + bundle: false, + banner: { + dts: '/*! hello banner dts build*/', + }, + footer: { + dts: '/*! hello banner dts build*/', + }, + dts: { + bundle: false, + build: true, + autoExtension: true, + experiments: { + tsgo: true, + }, + }, + }), + ], + source: { + entry: { + index: ['./src/**'], + }, + }, +}); diff --git a/tests/integration/dts-tsgo/build/process-files/src/index.ts b/tests/integration/dts-tsgo/build/process-files/src/index.ts new file mode 100644 index 000000000..dfa3258cb --- /dev/null +++ b/tests/integration/dts-tsgo/build/process-files/src/index.ts @@ -0,0 +1 @@ +export const num1 = 1; diff --git a/tests/integration/dts-tsgo/build/process-files/tsconfig.json b/tests/integration/dts-tsgo/build/process-files/tsconfig.json new file mode 100644 index 000000000..2f987306b --- /dev/null +++ b/tests/integration/dts-tsgo/build/process-files/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "@rslib/tsconfig/base", + "compilerOptions": { + "rootDir": "src", + "declaration": true, + "declarationDir": "./dist/esm" + }, + "include": ["src"], + "references": [ + { + "path": "../__references__" + } + ] +} diff --git a/tests/integration/dts-tsgo/bundle.test.ts b/tests/integration/dts-tsgo/bundle.test.ts index 23bcf3922..684acf399 100644 --- a/tests/integration/dts-tsgo/bundle.test.ts +++ b/tests/integration/dts-tsgo/bundle.test.ts @@ -1,15 +1,8 @@ import { spawnSync } from 'node:child_process'; import { existsSync } from 'node:fs'; -import { join, normalize } from 'node:path'; +import { join } from 'node:path'; import { describe, expect, test } from '@rstest/core'; -import stripAnsi from 'strip-ansi'; -import { - buildAndGetResults, - createTempFiles, - globContentJSON, - proxyConsole, - queryContent, -} from 'test-helper'; +import { buildAndGetResults, createTempFiles, queryContent } from 'test-helper'; describe('dts with tsgo when bundle: true', () => { test('basic', async () => { From 824e912b42f7f479dadac0f1aa6cbd8b6bdd9694 Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 19:18:39 +0800 Subject: [PATCH 07/14] chore: update --- packages/plugin-dts/src/tsgo.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/plugin-dts/src/tsgo.ts b/packages/plugin-dts/src/tsgo.ts index a23405868..6ed7b0ec4 100644 --- a/packages/plugin-dts/src/tsgo.ts +++ b/packages/plugin-dts/src/tsgo.ts @@ -1,10 +1,10 @@ import { spawn } from 'node:child_process'; -import fs from 'node:fs'; +import fsP from 'node:fs/promises'; import { createRequire } from 'node:module'; import path from 'node:path'; import { logger } from '@rsbuild/core'; import color from 'picocolors'; -import ts from 'typescript'; +import type ts from 'typescript'; import type { DtsRedirect } from './index'; import type { EmitDtsOptions } from './tsc'; import { @@ -84,7 +84,7 @@ async function handleDiagnosticsAndProcessFiles( ]); await Promise.all( dtsFiles.map(async (file) => { - const contents = ts.sys.readFile(file) ?? ''; + const contents = await fsP.readFile(file, 'utf8'); const newFileName = renameDtsFile(file, dtsExtension, bundle); const newContents = updateDeclarationMapContent( file, @@ -94,12 +94,8 @@ async function handleDiagnosticsAndProcessFiles( tsConfigResult.options.declarationMap, ); if (file !== newFileName || contents !== newContents) { - ts.sys.writeFile(newFileName, newContents); - if (ts.sys.deleteFile) { - ts.sys.deleteFile(file); - } else { - fs.unlinkSync(file); - } + await fsP.writeFile(newFileName, newContents); + await fsP.unlink(file); } }), ); From e079ba0d16fbd759e53d8baf6e09f93ce865f847 Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 19:20:31 +0800 Subject: [PATCH 08/14] chore: update --- packages/plugin-dts/package.json | 2 +- pnpm-lock.yaml | 66 ++++++++++++++++---------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/plugin-dts/package.json b/packages/plugin-dts/package.json index 4cca304ba..ea50ea2d2 100644 --- a/packages/plugin-dts/package.json +++ b/packages/plugin-dts/package.json @@ -40,7 +40,7 @@ "@microsoft/api-extractor": "^7.52.11", "@rsbuild/core": "~1.5.3", "@rslib/tsconfig": "workspace:*", - "@typescript/native-preview": "7.0.0-dev.20250903.1", + "@typescript/native-preview": "7.0.0-dev.20250904.1", "rsbuild-plugin-publint": "^0.3.3", "rslib": "npm:@rslib/core@0.12.4", "typescript": "^5.9.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a4370d7fa..52ca8ea6d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -451,8 +451,8 @@ importers: specifier: workspace:* version: link:../../scripts/tsconfig '@typescript/native-preview': - specifier: 7.0.0-dev.20250903.1 - version: 7.0.0-dev.20250903.1 + specifier: 7.0.0-dev.20250904.1 + version: 7.0.0-dev.20250904.1 rsbuild-plugin-publint: specifier: ^0.3.3 version: 0.3.3(@rsbuild/core@1.5.3) @@ -3365,50 +3365,50 @@ packages: '@types/unist@3.0.3': resolution: {integrity: sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==} - '@typescript/native-preview-darwin-arm64@7.0.0-dev.20250903.1': - resolution: {integrity: sha512-acoMJ+HRAvuDsHqWIW2lIGG8cqBdkN8SdjB2z4QKMmIGkGkN0iJFFNXhsxZ7UbV2j1XEghTIxlVA4XrsUoS7EA==} + '@typescript/native-preview-darwin-arm64@7.0.0-dev.20250904.1': + resolution: {integrity: sha512-1rt4DhERW1VM4OwWYVIrCp1k1S4kpZAxzbCnprNinVJInhHexY2K0FFD9IGXKWSRANHg/OmJRQYTEoDKM6pqNw==} engines: {node: '>=20.6.0'} cpu: [arm64] os: [darwin] - '@typescript/native-preview-darwin-x64@7.0.0-dev.20250903.1': - resolution: {integrity: sha512-/+SFZCUARYmamR9VgCEdNaq6g09yd/5nhuD07W7WG5OtGYJYLn79k0/bRnJb8u2yPuuebzNjOc86QqaKsaGFcQ==} + '@typescript/native-preview-darwin-x64@7.0.0-dev.20250904.1': + resolution: {integrity: sha512-d2DMQnsXAkZDDk9bU/FhY/D74tbMAkboIGb+hq7kIIgOVcxOswhwLFZ/ajW/9NTesktz8Z14t40Ber+/Pny25A==} engines: {node: '>=20.6.0'} cpu: [x64] os: [darwin] - '@typescript/native-preview-linux-arm64@7.0.0-dev.20250903.1': - resolution: {integrity: sha512-DKtsfyk43YLtAS1/RdTOMOfQj/loQhFVHXuysIlVc56PDD7b7hwgQ3phkhNgJZpKPgTfpn/eM4CD5rgHLdIduQ==} + '@typescript/native-preview-linux-arm64@7.0.0-dev.20250904.1': + resolution: {integrity: sha512-+fv13RDSk+7wFYY846q5ig7X6G07JT7wbajk6p4rELXTIfS1c6gRHGhODETCfFVaPziP4IlvqyinNP8F8wc9uQ==} engines: {node: '>=20.6.0'} cpu: [arm64] os: [linux] - '@typescript/native-preview-linux-arm@7.0.0-dev.20250903.1': - resolution: {integrity: sha512-zqK6cJk2D+iE1prHwH16Ko5GsMsloKVJ+M0kiwyTrkjT2w6QcgpthEU8IMdOCBi9d9pXujURz1UufkXHAh02VA==} + '@typescript/native-preview-linux-arm@7.0.0-dev.20250904.1': + resolution: {integrity: sha512-YyfTK1SGmfeDJv6G3vSmVxjM914Xio7O57NzRKOyEQnmBT5tdXTzeWgkjrUh1jE8wCUu0f0ZZ+xDTwgys+E2ug==} engines: {node: '>=20.6.0'} cpu: [arm] os: [linux] - '@typescript/native-preview-linux-x64@7.0.0-dev.20250903.1': - resolution: {integrity: sha512-USEhjORkiLKsgL1ogk09yn5XpK+FoKQqrqdNUQlc/zHhJPf/oHsaU0EmFTxLhPoBYcaxoZ0UBfQR5i/viGB1iQ==} + '@typescript/native-preview-linux-x64@7.0.0-dev.20250904.1': + resolution: {integrity: sha512-BjWJI42cUUilIyQHZpQQeSjC/Ifj/UaIf4oj6lRHDcg5qgLHWe5bAUxuNjE6i7wi+TTN9YxUvBDkMAcm/hI8wg==} engines: {node: '>=20.6.0'} cpu: [x64] os: [linux] - '@typescript/native-preview-win32-arm64@7.0.0-dev.20250903.1': - resolution: {integrity: sha512-iiHE1yZeDBPrJrlRYs6geBdV/MDu0BZGK1Yh93A9ofBymQpr3DXYNysqb5kEBy6ei2KTPJUu4YNAVzu9UoMVfQ==} + '@typescript/native-preview-win32-arm64@7.0.0-dev.20250904.1': + resolution: {integrity: sha512-rPv/mVaneZTuFESk/zDg3dFiZjpdipVMcLaF10Ns1fIyWdZ0ja79Ufm1eCFbk8KFNEX2dEx+vFEvD9n4bhEneg==} engines: {node: '>=20.6.0'} cpu: [arm64] os: [win32] - '@typescript/native-preview-win32-x64@7.0.0-dev.20250903.1': - resolution: {integrity: sha512-2yf56TIhsPbjYZz0XT4+hOFz9jetqlADkFuIid0dpe/0HLAliIMOHRAMZj22PerYU5Qcg66llJ6xU+78evBZCw==} + '@typescript/native-preview-win32-x64@7.0.0-dev.20250904.1': + resolution: {integrity: sha512-+twwqKYEv5UdZX5FRaBo0bDQgw/uPQjU3hqaqaO0Dhp1Ou8Ce4oi5hgwauB1j29JwBbvOi9/yoEcjsjT2Wsaxw==} engines: {node: '>=20.6.0'} cpu: [x64] os: [win32] - '@typescript/native-preview@7.0.0-dev.20250903.1': - resolution: {integrity: sha512-8EnxWQp7wUfk6mV3Bw7aGLQwKqDwi6+YLqm4Sev+KRpiUyepHZKWGej/YTLko3WMd0uGoGAlODuZjoPBQNJdag==} + '@typescript/native-preview@7.0.0-dev.20250904.1': + resolution: {integrity: sha512-IzPzhumNsWsIg4Kmt0y+0b2BBtsvD17rDmKj78yNeU3AsuA6xignQ5eDkFtRmLdGPVZwa8Yg5zPcJRFln98Ocw==} engines: {node: '>=20.6.0'} hasBin: true @@ -10207,36 +10207,36 @@ snapshots: '@types/unist@3.0.3': {} - '@typescript/native-preview-darwin-arm64@7.0.0-dev.20250903.1': + '@typescript/native-preview-darwin-arm64@7.0.0-dev.20250904.1': optional: true - '@typescript/native-preview-darwin-x64@7.0.0-dev.20250903.1': + '@typescript/native-preview-darwin-x64@7.0.0-dev.20250904.1': optional: true - '@typescript/native-preview-linux-arm64@7.0.0-dev.20250903.1': + '@typescript/native-preview-linux-arm64@7.0.0-dev.20250904.1': optional: true - '@typescript/native-preview-linux-arm@7.0.0-dev.20250903.1': + '@typescript/native-preview-linux-arm@7.0.0-dev.20250904.1': optional: true - '@typescript/native-preview-linux-x64@7.0.0-dev.20250903.1': + '@typescript/native-preview-linux-x64@7.0.0-dev.20250904.1': optional: true - '@typescript/native-preview-win32-arm64@7.0.0-dev.20250903.1': + '@typescript/native-preview-win32-arm64@7.0.0-dev.20250904.1': optional: true - '@typescript/native-preview-win32-x64@7.0.0-dev.20250903.1': + '@typescript/native-preview-win32-x64@7.0.0-dev.20250904.1': optional: true - '@typescript/native-preview@7.0.0-dev.20250903.1': + '@typescript/native-preview@7.0.0-dev.20250904.1': optionalDependencies: - '@typescript/native-preview-darwin-arm64': 7.0.0-dev.20250903.1 - '@typescript/native-preview-darwin-x64': 7.0.0-dev.20250903.1 - '@typescript/native-preview-linux-arm': 7.0.0-dev.20250903.1 - '@typescript/native-preview-linux-arm64': 7.0.0-dev.20250903.1 - '@typescript/native-preview-linux-x64': 7.0.0-dev.20250903.1 - '@typescript/native-preview-win32-arm64': 7.0.0-dev.20250903.1 - '@typescript/native-preview-win32-x64': 7.0.0-dev.20250903.1 + '@typescript/native-preview-darwin-arm64': 7.0.0-dev.20250904.1 + '@typescript/native-preview-darwin-x64': 7.0.0-dev.20250904.1 + '@typescript/native-preview-linux-arm': 7.0.0-dev.20250904.1 + '@typescript/native-preview-linux-arm64': 7.0.0-dev.20250904.1 + '@typescript/native-preview-linux-x64': 7.0.0-dev.20250904.1 + '@typescript/native-preview-win32-arm64': 7.0.0-dev.20250904.1 + '@typescript/native-preview-win32-x64': 7.0.0-dev.20250904.1 '@typescript/vfs@1.6.1(typescript@5.9.2)': dependencies: From 1f9ad44a4072a3836980988897df06f7339eafa2 Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 21:22:13 +0800 Subject: [PATCH 09/14] docs: add --- packages/core/src/types/config.ts | 2 +- packages/plugin-dts/README.md | 36 +++++++++ website/docs/en/config/lib/dts.mdx | 46 ++++++++++- website/docs/en/guide/advanced/dts.mdx | 77 ++++++++++++++++-- website/docs/zh/config/lib/dts.mdx | 46 ++++++++++- website/docs/zh/guide/advanced/dts.mdx | 106 ++++++++++++++++++++----- 6 files changed, 281 insertions(+), 32 deletions(-) diff --git a/packages/core/src/types/config.ts b/packages/core/src/types/config.ts index ff1e00421..439113afc 100644 --- a/packages/core/src/types/config.ts +++ b/packages/core/src/types/config.ts @@ -101,7 +101,7 @@ export type Dts = */ experiments?: { /** - * [Experimental] Whether to generate declaration files with the `tsgo` compiler. + * [Experimental] Whether to generate declaration files with `tsgo`. * @defaultValue `false` * @see {@link https://rslib.rs/config/lib/dts#dtsexperimentstsgo} */ diff --git a/packages/plugin-dts/README.md b/packages/plugin-dts/README.md index 15d80cfcc..02359d410 100644 --- a/packages/plugin-dts/README.md +++ b/packages/plugin-dts/README.md @@ -136,6 +136,8 @@ pluginDts({ }); ``` +> When [experiments.tsgo](#experimentstsgo) is enabled, if the project also enables [build](#build) or emits declaration files with different extensions to the same directory, `dtsExtension` may not work correctly. + ### alias - **Type:** `Record` @@ -280,6 +282,40 @@ import { foo } from './foo.mjs'; // expected output of './dist/bar.d.mts' - When set to `false`, the file extension will remain unchanged from the original import path in the rewritten import path of the output file (regardless of whether it is specified or specified as any value). +### experiments + +- **Type:** `{ tsgo?: boolean }` +- **Default:** `{}` + +Whether to enable experimental features. + +#### experiments.tsgo + +- **Type:** `boolean` +- **Default:** `false` + +Whether to generate declaration files with [tsgo](https://github.com/microsoft/typescript-go). + +To enable this option, you need to: + +1. Install [@typescript/native-preview](https://www.npmjs.com/package/@typescript/native-preview) as a development dependency. + +```bash +npm add @typescript/native-preview -D +``` + +2. Set `experiments.tsgo` to `true`. + +```js +pluginDts({ + experiments: { + tsgo: true, + }, +}); +``` + +> `tsgo` can provide faster generation of declaration files, especially for large projects. However, since `tsgo` is still experimental, there may be unresolved issues or limitations. Therefore, please make sure to thoroughly test it in your project before enabling this option. + ## Contributing Please read the [Contributing Guide](https://github.com/web-infra-dev/rslib/blob/main/CONTRIBUTING.md). diff --git a/website/docs/en/config/lib/dts.mdx b/website/docs/en/config/lib/dts.mdx index 6228da934..da49af696 100644 --- a/website/docs/en/config/lib/dts.mdx +++ b/website/docs/en/config/lib/dts.mdx @@ -15,6 +15,9 @@ type Dts = abortOnError?: boolean; autoExtension?: boolean; alias?: Record; + experiments?: { + tsgo?: boolean; + }; } | boolean; ``` @@ -216,7 +219,9 @@ When `dts.autoExtension` is set to `true`, the declaration file extension will b ::: note -It follows the same logic as [lib.autoExtension](/config/lib/auto-extension), but the default value is different since the declaration file extension may cause some issues with different module resolution strategies. +1. It follows the same logic as [lib.autoExtension](/config/lib/auto-extension), but the default value is different since the declaration file extension may cause some issues with different module resolution strategies. + +2. When [dts.experiments.tsgo](/config/lib/dts#dtsexperimentstsgo) is enabled, if the project also enables [dts.build](/config/lib/dts#dtsbuild) or emits declaration files with different extensions to the same directory, `dts.autoExtension` may not work correctly. ::: @@ -245,3 +250,42 @@ export default { ], }; ``` + +### dts.experiments + +- **Type:** `{ tsgo?: boolean }` +- **Default:** `{}` + +Whether to enable experimental features. + +#### dts.experiments.tsgo + +- **Type:** `boolean` +- **Default:** `false` + +Whether to generate declaration files with [tsgo](https://github.com/microsoft/typescript-go). + +To enable this option, you need to install [@typescript/native-preview](https://www.npmjs.com/package/@typescript/native-preview) as a development dependency. + + + +```ts title="rslib.config.ts" +export default { + lib: [ + { + dts: { + // [!code highlight:3] + experiments: { + tsgo: true, + }, + }, + }, + ], +}; +``` + +::: note + +`tsgo` can provide faster generation of declaration files, especially for large projects. However, since `tsgo` is still experimental, there may be unresolved issues or limitations. Therefore, please make sure to thoroughly test it in your project before enabling this option. + +::: diff --git a/website/docs/en/guide/advanced/dts.mdx b/website/docs/en/guide/advanced/dts.mdx index 158f2e3ba..61dca1807 100644 --- a/website/docs/en/guide/advanced/dts.mdx +++ b/website/docs/en/guide/advanced/dts.mdx @@ -40,13 +40,35 @@ Bundleless declaration files involves generating a separate declaration file for ## How to generate declaration files in Rslib -Rslib defaults to generating bundleless declaration files, which using [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) and bundle declaration files is also supported by [API Extractor](https://api-extractor.com/). - -If you want to generate bundleless declaration files, you can: - -- Set `dts: true` or `dts: { bundle: false }` in the Rslib configuration file. +Rslib supports generating declaration files using both the [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) and [tsgo](https://github.com/microsoft/typescript-go), and also supports bundling declaration files with [API Extractor](https://api-extractor.com/). + +| Type | Supported Method | Description | +| ---------- | --------------------------------------- | -------------- | +| bundleless | TypeScript Compiler API | Default method | +| bundleless | tsgo | | +| bundle | TypeScript Compiler API + API Extractor | Default method | +| bundle | tsgo + API Extractor | | + +### Generate bundleless declaration files + +Configure in the Rslib config file: + +```ts title="rslib.config.ts" +export default { + lib: [ + { + dts: true; // [!code highlight] + // or + // [!code highlight:3] + dts: { + bundle: false; + } + }, + ], +}; +``` -If you want to generate bundle declaration files, you can: +### Generate bundle declaration files 1. Install `@microsoft/api-extractor` as `devDependencies`, which is the underlying tool used for bundling declaration files. @@ -54,9 +76,47 @@ import { PackageManagerTabs } from '@theme'; -2. Set `dts: { bundle: true }` in the Rslib configuration file. +2. Configure in the Rslib config file: + +```ts title="rslib.config.ts" +export default { + lib: [ + { + // [!code highlight:3] + dts: { + bundle: true; + } + }, + ], +}; +``` + +### Generate declaration files with tsgo + +1. Install `@typescript/native-preview` as `devDependencies`: + + + +2. Configure in the Rslib config file: + +```ts title="rslib.config.ts" +export default { + lib: [ + { + dts: { + // [!code highlight:3] + experiments: { + tsgo: true, + }, + }, + }, + ], +}; +``` + +### Notes -It should be noted that during the generation of declaration files, Rslib will automatically enforce some configuration options in `tsconfig.json` to ensure that the [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) generates only declaration files. +During the generation of declaration files, Rslib will automatically enforce some configuration options in `tsconfig.json` to ensure that the [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) or [tsgo](https://github.com/microsoft/typescript-go) generates only declaration files. ```json { @@ -84,6 +144,7 @@ The priority from highest to lowest of final output directory of declaration fil | [dts.abortOnError](/config/lib/dts#dtsabortonerror) | Whether to abort the build process when an error occurs during declaration files generation. | | [dts.autoExtension](/config/lib/dts#dtsautoextension) | Whether to automatically set the declaration file extension based on the [format](/config/lib/format) option. | | [dts.alias](/config/lib/dts#dtsalias) | The path alias of the declaration files. | +| [dts.experiments.tsgo](/config/lib/dts#dtsexperimentstsgo) | Whether to generate declaration files with [tsgo](https://github.com/microsoft/TypeScript/pull/48729). | | [banner.dts](/config/lib/banner#bannerdts) | Inject content into the top of each declaration output file. | | [footer.dts](/config/lib/footer#footerdts) | Inject content into the bottom of each declaration file. | | [redirect.dts.path](/config/lib/redirect#redirectdtspath) | Whether to automatically redirect the import paths of TypeScript declaration output files. | diff --git a/website/docs/zh/config/lib/dts.mdx b/website/docs/zh/config/lib/dts.mdx index 22ec5a1a5..62db20f74 100644 --- a/website/docs/zh/config/lib/dts.mdx +++ b/website/docs/zh/config/lib/dts.mdx @@ -15,6 +15,9 @@ type Dts = abortOnError?: boolean; autoExtension?: boolean; alias?: Record; + experiments?: { + tsgo?: boolean; + }; } | boolean; ``` @@ -216,7 +219,9 @@ export default { ::: note -这遵循与 [lib.autoExtension](/config/lib/auto-extension) 相同的逻辑,但默认值不同,因为类型声明文件扩展名可能会在不同的模块解析策略中造成一些问题。 +1. 这遵循与 [lib.autoExtension](/config/lib/auto-extension) 相同的逻辑,但默认值不同,因为类型声明文件扩展名可能会在不同的模块解析策略中造成一些问题。 + +2. 当开启 [dts.experiments.tsgo](/config/lib/dts#dtsexperimentstsgo) 时,如果项目同时开启了 [dts.build](/config/lib/dts#dtsbuild) 或者将不同后缀的类型声明文件输出到同一目录,`dts.autoExtension` 无法正常生效。 ::: @@ -245,3 +250,42 @@ export default { ], }; ``` + +### dts.experiments + +- **类型:** `{ tsgo?: boolean }` +- **默认值:** `{}` + +用于启用实验性功能。 + +#### dts.experiments.tsgo + +- **类型:** `boolean` +- **默认值:** `false` + +是否使用 [tsgo](https://github.com/microsoft/typescript-go) 生成类型声明文件。 + +如果需要启用该选项,需要安装 [@typescript/native-preview](https://www.npmjs.com/package/@typescript/native-preview) 作为开发依赖。 + + + +```ts title="rslib.config.ts" +export default { + lib: [ + { + dts: { + // [!code highlight:3] + experiments: { + tsgo: true, + }, + }, + }, + ], +}; +``` + +::: note + +`tsgo` 可以提供更快的类型声明文件生成速度,尤其是对于大型项目。然而,由于 `tsgo` 仍处于实验阶段,可能会存在一些未解决的问题或限制。因此,在启用该选项前,请确保在你的项目中进行充分测试。 + +::: diff --git a/website/docs/zh/guide/advanced/dts.mdx b/website/docs/zh/guide/advanced/dts.mdx index fb20581e5..da5e7e80b 100644 --- a/website/docs/zh/guide/advanced/dts.mdx +++ b/website/docs/zh/guide/advanced/dts.mdx @@ -40,23 +40,86 @@ Bundleless 类型为库中的每个模块生成单独的声明文件,就像 `t ## 如何在 Rslib 中生成类型声明文件 -Rslib 默认使用 [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) 生成 bundleless 类型,用 [API Extractor](https://api-extractor.com/) 生成 bundle 类型。 - -如果你想生成 bundleless 类型,可以: - -- 设置 `dts: true` 或者 `dts: { bundle: false }` 在 Rslib 配置文件。 +Rslib 支持使用 [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) 和 [tsgo](https://github.com/microsoft/typescript-go) 两种方式来生成类型声明文件,并且支持使用 [API Extractor](https://api-extractor.com/) 将生成的类型声明文件打包。 + +| 类型 | 支持方式 | 说明 | +| ---------- | --------------------------------------- | -------- | +| bundleless | TypeScript Compiler API | 默认方式 | +| bundleless | tsgo | | +| bundle | TypeScript Compiler API + API Extractor | 默认方式 | +| bundle | tsgo + API Extractor | | + +### 生成 bundleless 类型 + +在 Rslib 配置文件中设置: + +```ts title="rslib.config.ts" +export default { + lib: [ + { + dts: true; // [!code highlight] + // 或者 + // [!code highlight:3] + dts: { + bundle: false; + } + }, + ], +}; +``` -如果你想生成 bundle 类型,可以: +### 生成 bundle 类型 -1. 安装 `@microsoft/api-extractor` 作为 `devDependencies`, 这是用于打包类型声明文件的底层工具。 +1. 安装 `@microsoft/api-extractor` 作为 `devDependencies`,这是用于打包类型声明文件的底层工具。 import { PackageManagerTabs } from '@theme'; -2. 在 Rslib 配置文件中设置 `dts: { bundle: true }`。 +2. 在 Rslib 配置文件中设置: + +```ts title="rslib.config.ts" +export default { + lib: [ + { + // [!code highlight:3] + dts: { + bundle: true; + } + }, + ], +}; +``` + +### 使用 tsgo 生成类型声明文件 + +1. 安装 `@typescript/native-preview` 作为 `devDependencies`: + + + +2. 在 Rslib 配置文件中设置: + +```ts title="rslib.config.ts" +export default { + lib: [ + { + dts: { + // [!code highlight:3] + experiments: { + tsgo: true, + }, + }, + }, + ], +}; +``` + +### 注意事项 -需要注意的是,Rslib 在生成类型声明文件的过程中,默认会强制设置 `tsconfig.json` 中的一些配置项以保证 [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) 能够仅生成类型声明文件。 +Rslib 在生成类型声明文件的过程中,默认会强制设置 `tsconfig.json` +中的一些配置项以保证 [TypeScript Compiler +API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) 或 [tsgo](https://github.com/microsoft/typescript-go) +能够仅生成类型声明文件。 ```json { @@ -76,15 +139,16 @@ import { PackageManagerTabs } from '@theme'; ## 类型声明文件的相关配置 -| 配置项 | 描述说明 | -| ------------------------------------------------------------------- | ---------------------------------------------------------------------- | -| [dts.bundle](/config/lib/dts#dtsbundle) | 是否打包类型声明文件。 | -| [dts.distPath](/config/lib/dts#dtsdistpath) | 类型声明文件的输出目录。 | -| [dts.build](/config/lib/dts#dtsbuild) | 是否在生成类型声明文件时构建项目的 project references。 | -| [dts.abortOnError](/config/lib/dts#dtsabortonerror) | 当类型声明文件生成过程中出现错误时,是否中止构建过程。 | -| [dts.autoExtension](/config/lib/dts#dtsautoextension) | 是否根据 [format](/config/lib/format) 选项自动设置类型声明文件扩展名。 | -| [dts.alias](/config/lib/dts#dtsalias) | 类型声明文件的路径别名。 | -| [banner.dts](/config/lib/banner#bannerdts) | 在每个类型声明文件顶部注入内容。 | -| [footer.dts](/config/lib/footer#footerdts) | 在每个类型声明文件底部注入内容。 | -| [redirect.dts.path](/config/lib/redirect#redirectdtspath) | 是否自动重定向类型声明文件中的导入路径。 | -| [redirect.dts.extension](/config/lib/redirect#redirectdtsextension) | 是否根据类型声明文件自动重定向文件扩展名到导入路径。 | +| 配置项 | 描述说明 | +| ------------------------------------------------------------------- | ------------------------------------------------------------------------------ | +| [dts.bundle](/config/lib/dts#dtsbundle) | 是否打包类型声明文件。 | +| [dts.distPath](/config/lib/dts#dtsdistpath) | 类型声明文件的输出目录。 | +| [dts.build](/config/lib/dts#dtsbuild) | 是否在生成类型声明文件时构建项目的 project references。 | +| [dts.abortOnError](/config/lib/dts#dtsabortonerror) | 当类型声明文件生成过程中出现错误时,是否中止构建过程。 | +| [dts.autoExtension](/config/lib/dts#dtsautoextension) | 是否根据 [format](/config/lib/format) 选项自动设置类型声明文件扩展名。 | +| [dts.alias](/config/lib/dts#dtsalias) | 类型声明文件的路径别名。 | +| [dts.experiments.tsgo](/config/lib/dts#dtsexperimentstsgo) | 是否使用 [tsgo](https://github.com/microsoft/typescript-go) 生成类型声明文件。 | +| [banner.dts](/config/lib/banner#bannerdts) | 在每个类型声明文件顶部注入内容。 | +| [footer.dts](/config/lib/footer#footerdts) | 在每个类型声明文件底部注入内容。 | +| [redirect.dts.path](/config/lib/redirect#redirectdtspath) | 是否自动重定向类型声明文件中的导入路径。 | +| [redirect.dts.extension](/config/lib/redirect#redirectdtsextension) | 是否根据类型声明文件自动重定向文件扩展名到导入路径。 | From 5937e58153902c7f699cf1490ec32c03b3019c45 Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 22:10:37 +0800 Subject: [PATCH 10/14] chore: update --- packages/plugin-dts/README.md | 2 + packages/plugin-dts/src/tsgo.ts | 7 +- tests/integration/dts-tsgo/build.test.ts | 265 +++++++++--------- tests/integration/dts-tsgo/bundle.test.ts | 247 ++++++++-------- .../integration/dts-tsgo/bundleFalse.test.ts | 223 ++++++++------- website/docs/en/config/lib/dts.mdx | 6 + website/docs/en/guide/advanced/dts.mdx | 6 + website/docs/zh/config/lib/dts.mdx | 6 + website/docs/zh/guide/advanced/dts.mdx | 6 + 9 files changed, 410 insertions(+), 358 deletions(-) diff --git a/packages/plugin-dts/README.md b/packages/plugin-dts/README.md index 02359d410..92a152c4d 100644 --- a/packages/plugin-dts/README.md +++ b/packages/plugin-dts/README.md @@ -304,6 +304,8 @@ To enable this option, you need to: npm add @typescript/native-preview -D ``` +> `@typescript/native-preview` requires Node.js 20.6.0 or higher. + 2. Set `experiments.tsgo` to `true`. ```js diff --git a/packages/plugin-dts/src/tsgo.ts b/packages/plugin-dts/src/tsgo.ts index 6ed7b0ec4..0523c1f16 100644 --- a/packages/plugin-dts/src/tsgo.ts +++ b/packages/plugin-dts/src/tsgo.ts @@ -55,7 +55,8 @@ const generateTsgoArgs = ( args.push('--emitDeclarationOnly'); if (isWatch) { - // rebuild when watch since watch mode is proof-of-concept only currently in tsgo + // TODO: Enable watch mode when tsgo's watch support is ready. + // Currently, watch mode is proof-of-concept only. // args.push('--watch'); } @@ -155,7 +156,7 @@ export async function emitDtsTsgo( tsgoBinFile, [ ...args, - /* Required parameter, use it stdout have color */ + // Required parameter to enable colored stdout '--pretty', ], { @@ -221,7 +222,7 @@ export async function emitDtsTsgo( resolve(hasErrors); } catch (error) { - reject(error); + reject(error instanceof Error ? error : new Error(String(error))); } }); }); diff --git a/tests/integration/dts-tsgo/build.test.ts b/tests/integration/dts-tsgo/build.test.ts index c9b3be2d6..3bb0fef9e 100644 --- a/tests/integration/dts-tsgo/build.test.ts +++ b/tests/integration/dts-tsgo/build.test.ts @@ -4,71 +4,73 @@ import { join } from 'node:path'; import { describe, expect, test } from '@rstest/core'; import { buildAndGetResults, createTempFiles, queryContent } from 'test-helper'; -describe('dts with tsgo when build: true', () => { - test('basic', async () => { - const fixturePath = join(__dirname, 'build', 'basic'); - const { files } = await buildAndGetResults({ - fixturePath, - type: 'dts', - }); - - expect(files.esm).toMatchInlineSnapshot(` +describe.skipIf(process.version.startsWith('v18'))( + 'dts with tsgo when build: true', + () => { + test('basic', async () => { + const fixturePath = join(__dirname, 'build', 'basic'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/build/basic/dist/esm/index.d.ts", "/tests/integration/dts-tsgo/build/basic/dist/esm/sum.d.ts", ] `); - const referenceDistPath = join( - fixturePath, - '../__references__/dist/index.d.ts', - ); - expect(existsSync(referenceDistPath)).toBeTruthy(); - - const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); - expect(existsSync(buildInfoPath)).toBeTruthy(); - }); - - test('distPath', async () => { - const fixturePath = join(__dirname, 'build', 'dist-path'); - const { files } = await buildAndGetResults({ - fixturePath, - type: 'dts', + const referenceDistPath = join( + fixturePath, + '../__references__/dist/index.d.ts', + ); + expect(existsSync(referenceDistPath)).toBeTruthy(); + + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); + expect(existsSync(buildInfoPath)).toBeTruthy(); }); - expect(files.esm).toMatchInlineSnapshot(` + test('distPath', async () => { + const fixturePath = join(__dirname, 'build', 'dist-path'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/build/dist-path/dist/custom/index.d.ts", ] `); - const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); - expect(existsSync(buildInfoPath)).toBeTruthy(); - }); + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); + expect(existsSync(buildInfoPath)).toBeTruthy(); + }); - test('autoExtension: true', async () => { - const fixturePath = join(__dirname, 'build', 'auto-extension'); - const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + test('autoExtension: true', async () => { + const fixturePath = join(__dirname, 'build', 'auto-extension'); + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); - expect(files.cjs).toMatchInlineSnapshot(` + expect(files.cjs).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/build/auto-extension/dist/types/index.d.cts", "/tests/integration/dts-tsgo/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({ - fixturePath, - type: 'dts', + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); + expect(existsSync(buildInfoPath)).toBeTruthy(); }); - expect(contents.esm).toMatchInlineSnapshot(` + test('process files - auto extension and banner / footer', async () => { + const fixturePath = join(__dirname, 'build', 'process-files'); + const { contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(contents.esm).toMatchInlineSnapshot(` { "/tests/integration/dts-tsgo/build/process-files/dist/esm/index.d.mts": "/*! hello banner dts build*/ export declare const num1 = 1; @@ -78,126 +80,135 @@ describe('dts with tsgo when build: true', () => { } `); - const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); - expect(existsSync(buildInfoPath)).toBeTruthy(); - }); + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); + expect(existsSync(buildInfoPath)).toBeTruthy(); + }); - test('abortOnError: false', async () => { - const fixturePath = join(__dirname, 'build', 'abort-on-error'); + test('abortOnError: false', async () => { + const fixturePath = join(__dirname, 'build', 'abort-on-error'); - const result = spawnSync('npx', ['rslib', 'build'], { - cwd: fixturePath, - // do not show output in test console - stdio: 'ignore', - shell: true, - }); + const result = spawnSync('npx', ['rslib', 'build'], { + cwd: fixturePath, + // do not show output in test console + stdio: 'ignore', + shell: true, + }); - expect(result.status).toBe(0); + expect(result.status).toBe(0); - const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); - expect(existsSync(buildInfoPath)).toBeTruthy(); - }); + const buildInfoPath = join(fixturePath, 'tsconfig.tsbuildinfo'); + expect(existsSync(buildInfoPath)).toBeTruthy(); + }); - test('should clean dts dist files', async () => { - const fixturePath = join(__dirname, 'build', 'clean'); + test('should clean dts dist files', async () => { + const fixturePath = join(__dirname, 'build', 'clean'); - const checkFiles = await createTempFiles(fixturePath, false); + const checkFiles = await createTempFiles(fixturePath, false); - const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); - for (const file of checkFiles) { - expect(existsSync(file)).toBe(false); - } + for (const file of checkFiles) { + expect(existsSync(file)).toBe(false); + } - expect(files.esm).toMatchInlineSnapshot(` + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/build/clean/dist-types/esm/index.d.ts", "/tests/integration/dts-tsgo/build/clean/dist-types/esm/sum.d.ts", ] `); - expect(files.cjs).toMatchInlineSnapshot(` + expect(files.cjs).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/build/clean/dist-types/cjs/index.d.ts", "/tests/integration/dts-tsgo/build/clean/dist-types/cjs/sum.d.ts", ] `); - const referenceDistPath = join( - fixturePath, - '../__references__/dist/index.d.ts', - ); - expect(existsSync(referenceDistPath)).toBeTruthy(); - - const cjsBuildInfoPath = join(fixturePath, 'tsconfig.cjs.tsbuildinfo'); - expect(existsSync(cjsBuildInfoPath)).toBeTruthy(); + const referenceDistPath = join( + fixturePath, + '../__references__/dist/index.d.ts', + ); + expect(existsSync(referenceDistPath)).toBeTruthy(); - const esmBuildInfoPath = join(fixturePath, 'tsconfig.esm.tsbuildinfo'); - expect(existsSync(esmBuildInfoPath)).toBeTruthy(); - }); + const cjsBuildInfoPath = join(fixturePath, 'tsconfig.cjs.tsbuildinfo'); + expect(existsSync(cjsBuildInfoPath)).toBeTruthy(); - test('declarationMap', async () => { - const fixturePath = join(__dirname, 'build', 'declaration-map'); - const { files, contents } = await buildAndGetResults({ - fixturePath, - type: 'dts', + const esmBuildInfoPath = join(fixturePath, 'tsconfig.esm.tsbuildinfo'); + expect(existsSync(esmBuildInfoPath)).toBeTruthy(); }); - expect(files.esm).toMatchInlineSnapshot(` + test('declarationMap', async () => { + const fixturePath = join(__dirname, 'build', 'declaration-map'); + const { files, contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/build/declaration-map/dist/esm/index.d.ts", "/tests/integration/dts-tsgo/build/declaration-map/dist/esm/index.d.ts.map", ] `); - expect(files.cjs).toMatchInlineSnapshot(` + expect(files.cjs).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/build/declaration-map/dist/cjs/index.d.cts", "/tests/integration/dts-tsgo/build/declaration-map/dist/cjs/index.d.cts.map", ] `); - const { content: indexDtsEsm } = queryContent(contents.esm, 'index.d.ts', { - basename: true, - }); - const { content: indexDtsCjs } = queryContent(contents.cjs, 'index.d.cts', { - basename: true, + const { content: indexDtsEsm } = queryContent( + contents.esm, + 'index.d.ts', + { + basename: true, + }, + ); + const { content: indexDtsCjs } = queryContent( + contents.cjs, + 'index.d.cts', + { + basename: true, + }, + ); + const { content: indexMapEsm } = queryContent( + contents.esm, + 'index.d.ts.map', + { + basename: true, + }, + ); + const { content: indexMapCjs } = queryContent( + contents.cjs, + 'index.d.cts.map', + { + basename: true, + }, + ); + expect(indexDtsEsm).toContain('//# sourceMappingURL=index.d.ts.map'); + expect(indexDtsCjs).toContain('//# sourceMappingURL=index.d.cts.map'); + expect(indexMapEsm).toContain('"file":"index.d.ts"'); + expect(indexMapCjs).toContain('"file":"index.d.cts"'); + + const referenceEsmDistPath = join( + fixturePath, + '../__references__/dist/index.d.ts', + ); + expect(existsSync(referenceEsmDistPath)).toBeTruthy(); + + // TODO: can not rename dts files in reference yet + // const referenceCjsDistPath = join( + // fixturePath, + // '../__references__/dist/index.d.cts', + // ); + // expect(existsSync(referenceCjsDistPath)).toBeTruthy(); + + const esmBuildInfoPath = join(fixturePath, 'tsconfig.esm.tsbuildinfo'); + const cjsBuildInfoPath = join(fixturePath, 'tsconfig.cjs.tsbuildinfo'); + expect(existsSync(esmBuildInfoPath)).toBeTruthy(); + expect(existsSync(cjsBuildInfoPath)).toBeTruthy(); }); - const { content: indexMapEsm } = queryContent( - contents.esm, - 'index.d.ts.map', - { - basename: true, - }, - ); - const { content: indexMapCjs } = queryContent( - contents.cjs, - 'index.d.cts.map', - { - basename: true, - }, - ); - expect(indexDtsEsm).toContain('//# sourceMappingURL=index.d.ts.map'); - expect(indexDtsCjs).toContain('//# sourceMappingURL=index.d.cts.map'); - expect(indexMapEsm).toContain('"file":"index.d.ts"'); - expect(indexMapCjs).toContain('"file":"index.d.cts"'); - - const referenceEsmDistPath = join( - fixturePath, - '../__references__/dist/index.d.ts', - ); - expect(existsSync(referenceEsmDistPath)).toBeTruthy(); - - // TODO: can not rename dts files in reference yet - // const referenceCjsDistPath = join( - // fixturePath, - // '../__references__/dist/index.d.cts', - // ); - // expect(existsSync(referenceCjsDistPath)).toBeTruthy(); - - const esmBuildInfoPath = join(fixturePath, 'tsconfig.esm.tsbuildinfo'); - const cjsBuildInfoPath = join(fixturePath, 'tsconfig.cjs.tsbuildinfo'); - expect(existsSync(esmBuildInfoPath)).toBeTruthy(); - expect(existsSync(cjsBuildInfoPath)).toBeTruthy(); - }); -}); + }, +); diff --git a/tests/integration/dts-tsgo/bundle.test.ts b/tests/integration/dts-tsgo/bundle.test.ts index 684acf399..7c4e03225 100644 --- a/tests/integration/dts-tsgo/bundle.test.ts +++ b/tests/integration/dts-tsgo/bundle.test.ts @@ -4,205 +4,207 @@ import { join } from 'node:path'; import { describe, expect, test } from '@rstest/core'; import { buildAndGetResults, createTempFiles, queryContent } from 'test-helper'; -describe('dts with tsgo when bundle: true', () => { - test('basic', async () => { - const fixturePath = join(__dirname, 'bundle', 'basic'); - const { files, entries } = await buildAndGetResults({ - fixturePath, - type: 'dts', - }); - - expect(files.esm).toMatchInlineSnapshot( - ` +describe.skipIf(process.version.startsWith('v18'))( + 'dts with tsgo when bundle: true', + () => { + test('basic', async () => { + const fixturePath = join(__dirname, 'bundle', 'basic'); + const { files, entries } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot( + ` [ "/tests/integration/dts-tsgo/bundle/basic/dist/esm/index.d.ts", ] `, - ); + ); - expect(files.cjs).toMatchInlineSnapshot( - ` + expect(files.cjs).toMatchInlineSnapshot( + ` [ "/tests/integration/dts-tsgo/bundle/basic/dist/cjs/index.d.ts", ] `, - ); - - expect(entries).toMatchSnapshot(); - }); + ); - test('distPath', async () => { - const fixturePath = join(__dirname, 'bundle', 'dist-path'); - const { files } = await buildAndGetResults({ - fixturePath, - type: 'dts', + expect(entries).toMatchSnapshot(); }); - expect(files.esm).toMatchInlineSnapshot( - ` + test('distPath', async () => { + const fixturePath = join(__dirname, 'bundle', 'dist-path'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot( + ` [ "/tests/integration/dts-tsgo/bundle/dist-path/dist/custom/index.d.ts", ] `, - ); - }); - - test('abortOnError: false', async () => { - const fixturePath = join(__dirname, 'bundle', 'abort-on-error'); - - const result = spawnSync('npx', ['rslib', 'build'], { - cwd: fixturePath, - // do not show output in test console - stdio: 'ignore', - shell: true, + ); }); - expect(result.status).toBe(0); - }); + test('abortOnError: false', async () => { + const fixturePath = join(__dirname, 'bundle', 'abort-on-error'); + + const result = spawnSync('npx', ['rslib', 'build'], { + cwd: fixturePath, + // do not show output in test console + stdio: 'ignore', + shell: true, + }); - test('autoExtension: true', async () => { - const fixturePath = join(__dirname, 'bundle', 'auto-extension'); - const { files } = await buildAndGetResults({ - fixturePath, - type: 'dts', + expect(result.status).toBe(0); }); - expect(files.cjs).toMatchInlineSnapshot( - ` + test('autoExtension: true', async () => { + const fixturePath = join(__dirname, 'bundle', 'auto-extension'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.cjs).toMatchInlineSnapshot( + ` [ "/tests/integration/dts-tsgo/bundle/auto-extension/dist/cjs/index.d.cts", ] `, - ); - }); - - test('bundleName -- set source.entry', async () => { - const fixturePath = join(__dirname, 'bundle', 'bundle-name'); - const { files } = await buildAndGetResults({ - fixturePath, - type: 'dts', + ); }); - expect(files.esm).toMatchInlineSnapshot( - ` + test('bundleName -- set source.entry', async () => { + const fixturePath = join(__dirname, 'bundle', 'bundle-name'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot( + ` [ "/tests/integration/dts-tsgo/bundle/bundle-name/dist/esm/bundleName.d.ts", ] `, - ); - }); - - test('entry is an absolute path', async () => { - const fixturePath = join(__dirname, 'bundle', 'absolute-entry'); - const { files } = await buildAndGetResults({ - fixturePath, - type: 'dts', + ); }); - expect(files.esm).toMatchInlineSnapshot( - ` + test('entry is an absolute path', async () => { + const fixturePath = join(__dirname, 'bundle', 'absolute-entry'); + const { files } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot( + ` [ "/tests/integration/dts-tsgo/bundle/absolute-entry/dist/esm/index.d.ts", ] `, - ); - }); - - 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(` + 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(` [ "/tests/integration/dts-tsgo/bundle/rootdir/dist/esm/index.d.ts", ] `); - expect(files.cjs).toMatchInlineSnapshot(` + expect(files.cjs).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle/rootdir/dist/cjs/index.d.ts", ] `); - expect(entries).toMatchSnapshot(); - }); + expect(entries).toMatchSnapshot(); + }); - test('should clean dts dist files and .rslib folder', async () => { - const fixturePath = join(__dirname, 'bundle', 'clean'); + test('should clean dts dist files and .rslib folder', async () => { + const fixturePath = join(__dirname, 'bundle', 'clean'); - const checkFiles = await createTempFiles(fixturePath, true); + const checkFiles = await createTempFiles(fixturePath, true); - const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); - for (const file of checkFiles) { - expect(existsSync(file)).toBe(false); - } + for (const file of checkFiles) { + expect(existsSync(file)).toBe(false); + } - expect(files.esm).toMatchInlineSnapshot(` + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle/clean/dist-types/esm/index.d.ts", ] `); - expect(files.cjs).toMatchInlineSnapshot(` + expect(files.cjs).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle/clean/dist-types/cjs/index.d.ts", ] `); - }); - - test('multiple entries', async () => { - const fixturePath = join(__dirname, 'bundle', 'multiple-entries'); - const { files, contents } = await buildAndGetResults({ - fixturePath, - type: 'dts', }); - expect(files.esm).toMatchInlineSnapshot(` + test('multiple entries', async () => { + const fixturePath = join(__dirname, 'bundle', 'multiple-entries'); + const { files, contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle/multiple-entries/dist/esm/index.d.ts", "/tests/integration/dts-tsgo/bundle/multiple-entries/dist/esm/sum.d.ts", ] `); - expect(files.cjs).toMatchInlineSnapshot(` + expect(files.cjs).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle/multiple-entries/dist/cjs/index.d.ts", "/tests/integration/dts-tsgo/bundle/multiple-entries/dist/cjs/sum.d.ts", ] `); - const { content: indexEsm } = queryContent(contents.esm, 'index.d.ts', { - basename: true, - }); - const { content: indexCjs } = queryContent(contents.cjs, 'index.d.ts', { - basename: true, - }); - const { content: sumEsm } = queryContent(contents.esm, 'sum.d.ts', { - basename: true, - }); - const { content: sumCjs } = queryContent(contents.cjs, 'sum.d.ts', { - basename: true, + const { content: indexEsm } = queryContent(contents.esm, 'index.d.ts', { + basename: true, + }); + const { content: indexCjs } = queryContent(contents.cjs, 'index.d.ts', { + basename: true, + }); + const { content: sumEsm } = queryContent(contents.esm, 'sum.d.ts', { + basename: true, + }); + const { content: sumCjs } = queryContent(contents.cjs, 'sum.d.ts', { + basename: true, + }); + + expect([indexEsm, indexCjs, sumEsm, sumCjs]).toMatchSnapshot(); }); - expect([indexEsm, indexCjs, sumEsm, sumCjs]).toMatchSnapshot(); - }); + test('override with bundledPackages', async () => { + const fixturePath = join(__dirname, 'bundle', 'bundled-packages'); + const { entries } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); - test('override with bundledPackages', async () => { - const fixturePath = join(__dirname, 'bundle', 'bundled-packages'); - const { entries } = await buildAndGetResults({ - fixturePath, - type: 'dts', - }); + // default + expect(entries.esm0).toContain(`import { Action } from 'redux';`); - // default - expect(entries.esm0).toContain(`import { Action } from 'redux';`); - - // override empty array - expect(entries.esm1).toMatchInlineSnapshot(` + // override empty array + expect(entries.esm1).toMatchInlineSnapshot(` " export * from "@reduxjs/toolkit"; @@ -210,7 +212,8 @@ describe('dts with tsgo when bundle: true', () => { " `); - // override with bundledPackages - expect(entries.esm2).not.toContain(`import { Action } from 'redux';`); - }); -}); + // override with bundledPackages + expect(entries.esm2).not.toContain(`import { Action } from 'redux';`); + }); + }, +); diff --git a/tests/integration/dts-tsgo/bundleFalse.test.ts b/tests/integration/dts-tsgo/bundleFalse.test.ts index 62e31a3cd..16738b0ab 100644 --- a/tests/integration/dts-tsgo/bundleFalse.test.ts +++ b/tests/integration/dts-tsgo/bundleFalse.test.ts @@ -10,15 +10,17 @@ import { queryContent, } from 'test-helper'; -describe('dts with tsgo when bundle: false', () => { - test('basic', async () => { - const fixturePath = join(__dirname, 'bundle-false', 'basic'); - const { files, contents } = await buildAndGetResults({ - fixturePath, - type: 'dts', - }); - - expect(files.esm).toMatchInlineSnapshot(` +describe.skipIf(process.version.startsWith('v18'))( + 'dts with tsgo when bundle: false', + () => { + test('basic', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'basic'); + const { files, contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle-false/basic/dist/esm/index.d.ts", "/tests/integration/dts-tsgo/bundle-false/basic/dist/esm/sum.d.ts", @@ -27,7 +29,7 @@ describe('dts with tsgo when bundle: false', () => { ] `); - expect(files.cjs).toMatchInlineSnapshot(` + expect(files.cjs).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle-false/basic/dist/cjs/index.d.ts", "/tests/integration/dts-tsgo/bundle-false/basic/dist/cjs/sum.d.ts", @@ -36,14 +38,14 @@ describe('dts with tsgo when bundle: false', () => { ] `); - expect(contents.esm).toMatchSnapshot(); - }); + expect(contents.esm).toMatchSnapshot(); + }); - test('distPath', async () => { - const fixturePath = join(__dirname, 'bundle-false', 'dist-path'); - const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + test('distPath', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'dist-path'); + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); - expect(files.esm).toMatchInlineSnapshot(` + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle-false/dist-path/dist/custom/index.d.ts", "/tests/integration/dts-tsgo/bundle-false/dist-path/dist/custom/sum.d.ts", @@ -51,26 +53,26 @@ describe('dts with tsgo when bundle: false', () => { "/tests/integration/dts-tsgo/bundle-false/dist-path/dist/custom/utils/strings.d.ts", ] `); - }); + }); - test('abortOnError: false', async () => { - const fixturePath = join(__dirname, 'bundle-false', 'abort-on-error'); + test('abortOnError: false', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'abort-on-error'); - const result = spawnSync('npx', ['rslib', 'build'], { - cwd: fixturePath, - // do not show output in test console - stdio: 'ignore', - shell: true, - }); + const result = spawnSync('npx', ['rslib', 'build'], { + cwd: fixturePath, + // do not show output in test console + stdio: 'ignore', + shell: true, + }); - expect(result.status).toBe(0); - }); + expect(result.status).toBe(0); + }); - test('autoExtension: true', async () => { - const fixturePath = join(__dirname, 'bundle-false', 'auto-extension'); - const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + test('autoExtension: true', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'auto-extension'); + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); - expect(files.esm).toMatchInlineSnapshot(` + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/esm/index.d.ts", "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/esm/sum.d.ts", @@ -79,7 +81,7 @@ describe('dts with tsgo when bundle: false', () => { ] `); - expect(files.cjs).toMatchInlineSnapshot(` + expect(files.cjs).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/cjs/index.d.cts", "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/cjs/sum.d.cts", @@ -87,20 +89,20 @@ describe('dts with tsgo when bundle: false', () => { "/tests/integration/dts-tsgo/bundle-false/auto-extension/dist/types/cjs/utils/strings.d.cts", ] `); - }); + }); - test('should use declarationDir when not set dts.distPath', async () => { - const fixturePath = join(__dirname, 'bundle-false', 'declaration-dir'); - const distTypesPath = join(fixturePath, 'dist-types'); + test('should use declarationDir when not set dts.distPath', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'declaration-dir'); + const distTypesPath = join(fixturePath, 'dist-types'); - await buildAndGetResults({ fixturePath, type: 'dts' }); + await buildAndGetResults({ fixturePath, type: 'dts' }); - const distTypeFiles = await globContentJSON(distTypesPath, { - absolute: true, - }); - const distTypeFilePaths = Object.keys(distTypeFiles).sort(); + const distTypeFiles = await globContentJSON(distTypesPath, { + absolute: true, + }); + const distTypeFilePaths = Object.keys(distTypeFiles).sort(); - expect(distTypeFilePaths).toMatchInlineSnapshot(` + expect(distTypeFilePaths).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle-false/declaration-dir/dist-types/index.d.ts", "/tests/integration/dts-tsgo/bundle-false/declaration-dir/dist-types/sum.d.ts", @@ -108,20 +110,20 @@ describe('dts with tsgo when bundle: false', () => { "/tests/integration/dts-tsgo/bundle-false/declaration-dir/dist-types/utils/strings.d.ts", ] `); - }); + }); - test('should clean dts dist files', async () => { - const fixturePath = join(__dirname, 'bundle-false', 'clean'); + test('should clean dts dist files', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'clean'); - const checkFiles = await createTempFiles(fixturePath, false); + const checkFiles = await createTempFiles(fixturePath, false); - const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); + const { files } = await buildAndGetResults({ fixturePath, type: 'dts' }); - for (const file of checkFiles) { - expect(existsSync(file)).toBe(false); - } + for (const file of checkFiles) { + expect(existsSync(file)).toBe(false); + } - expect(files.esm).toMatchInlineSnapshot(` + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/esm/index.d.ts", "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/esm/sum.d.ts", @@ -130,7 +132,7 @@ describe('dts with tsgo when bundle: false', () => { ] `); - expect(files.cjs).toMatchInlineSnapshot(` + expect(files.cjs).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/cjs/index.d.ts", "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/cjs/sum.d.ts", @@ -138,87 +140,96 @@ describe('dts with tsgo when bundle: false', () => { "/tests/integration/dts-tsgo/bundle-false/clean/dist-types/cjs/utils/strings.d.ts", ] `); - }); + }); - test('should emit error when tsconfig not found', async () => { - const fixturePath = join(__dirname, 'bundle-false', 'tsconfig-path'); - await createTempFiles(fixturePath, false); + test('should emit error when tsconfig not found', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'tsconfig-path'); + await createTempFiles(fixturePath, false); - try { - await buildAndGetResults({ fixturePath, type: 'dts' }); - } catch (err: any) { - expect(stripAnsi(err.message)).toMatchInlineSnapshot( - `"Failed to resolve tsconfig file "/tests/integration/dts-tsgo/bundle-false/tsconfig-path/path_not_exist/tsconfig.json" from /tests/integration/dts-tsgo/bundle-false/tsconfig-path. Please ensure that the file exists."`, - ); - } - }); - - test('alias', async () => { - const fixturePath = join(__dirname, 'bundle-false', 'alias'); - const { contents } = await buildAndGetResults({ - fixturePath, - type: 'dts', + try { + await buildAndGetResults({ fixturePath, type: 'dts' }); + } catch (err: any) { + expect(stripAnsi(err.message)).toMatchInlineSnapshot( + `"Failed to resolve tsconfig file "/tests/integration/dts-tsgo/bundle-false/tsconfig-path/path_not_exist/tsconfig.json" from /tests/integration/dts-tsgo/bundle-false/tsconfig-path. Please ensure that the file exists."`, + ); + } }); - expect(contents.esm).toMatchInlineSnapshot(` + test('alias', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'alias'); + const { contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(contents.esm).toMatchInlineSnapshot(` { "/tests/integration/dts-tsgo/bundle-false/alias/dist/esm/index.d.ts": "export {} from '../../compile/prebundle-pkg'; ", } `); - expect(contents.cjs).toMatchInlineSnapshot(` + expect(contents.cjs).toMatchInlineSnapshot(` { "/tests/integration/dts-tsgo/bundle-false/alias/dist/cjs/index.d.ts": "export {} from '../../compile/prebundle-pkg'; ", } `); - }); - - test('declarationMap', async () => { - const fixturePath = join(__dirname, 'bundle-false', 'declaration-map'); - const { files, contents } = await buildAndGetResults({ - fixturePath, - type: 'dts', }); - expect(files.esm).toMatchInlineSnapshot(` + test('declarationMap', async () => { + const fixturePath = join(__dirname, 'bundle-false', 'declaration-map'); + const { files, contents } = await buildAndGetResults({ + fixturePath, + type: 'dts', + }); + + expect(files.esm).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle-false/declaration-map/dist/esm/index.d.ts", "/tests/integration/dts-tsgo/bundle-false/declaration-map/dist/esm/index.d.ts.map", ] `); - expect(files.cjs).toMatchInlineSnapshot(` + expect(files.cjs).toMatchInlineSnapshot(` [ "/tests/integration/dts-tsgo/bundle-false/declaration-map/dist/cjs/index.d.cts", "/tests/integration/dts-tsgo/bundle-false/declaration-map/dist/cjs/index.d.cts.map", ] `); - const { content: indexDtsEsm } = queryContent(contents.esm, 'index.d.ts', { - basename: true, - }); - const { content: indexDtsCjs } = queryContent(contents.cjs, 'index.d.cts', { - basename: true, + const { content: indexDtsEsm } = queryContent( + contents.esm, + 'index.d.ts', + { + basename: true, + }, + ); + const { content: indexDtsCjs } = queryContent( + contents.cjs, + 'index.d.cts', + { + basename: true, + }, + ); + const { content: indexMapEsm } = queryContent( + contents.esm, + 'index.d.ts.map', + { + basename: true, + }, + ); + const { content: indexMapCjs } = queryContent( + contents.cjs, + 'index.d.cts.map', + { + basename: true, + }, + ); + expect(indexDtsEsm).toContain('//# sourceMappingURL=index.d.ts.map'); + expect(indexDtsCjs).toContain('//# sourceMappingURL=index.d.cts.map'); + expect(indexMapEsm).toContain('"file":"index.d.ts"'); + expect(indexMapCjs).toContain('"file":"index.d.cts"'); }); - const { content: indexMapEsm } = queryContent( - contents.esm, - 'index.d.ts.map', - { - basename: true, - }, - ); - const { content: indexMapCjs } = queryContent( - contents.cjs, - 'index.d.cts.map', - { - basename: true, - }, - ); - expect(indexDtsEsm).toContain('//# sourceMappingURL=index.d.ts.map'); - expect(indexDtsCjs).toContain('//# sourceMappingURL=index.d.cts.map'); - expect(indexMapEsm).toContain('"file":"index.d.ts"'); - expect(indexMapCjs).toContain('"file":"index.d.cts"'); - }); -}); + }, +); diff --git a/website/docs/en/config/lib/dts.mdx b/website/docs/en/config/lib/dts.mdx index da49af696..3c34b36d9 100644 --- a/website/docs/en/config/lib/dts.mdx +++ b/website/docs/en/config/lib/dts.mdx @@ -269,6 +269,12 @@ To enable this option, you need to install [@typescript/native-preview](https:// +::: tip Version requirements + +`@typescript/native-preview` requires Node.js 20.6.0 or higher. + +::: + ```ts title="rslib.config.ts" export default { lib: [ diff --git a/website/docs/en/guide/advanced/dts.mdx b/website/docs/en/guide/advanced/dts.mdx index 61dca1807..8fc20b18b 100644 --- a/website/docs/en/guide/advanced/dts.mdx +++ b/website/docs/en/guide/advanced/dts.mdx @@ -97,6 +97,12 @@ export default { +::: tip Version requirements + +`@typescript/native-preview` requires Node.js 20.6.0 or higher. + +::: + 2. Configure in the Rslib config file: ```ts title="rslib.config.ts" diff --git a/website/docs/zh/config/lib/dts.mdx b/website/docs/zh/config/lib/dts.mdx index 62db20f74..11daa0b82 100644 --- a/website/docs/zh/config/lib/dts.mdx +++ b/website/docs/zh/config/lib/dts.mdx @@ -269,6 +269,12 @@ export default { +::: tip 版本要求 + +`@typescript/native-preview` 要求 Node.js 20.6.0 或更高版本。 + +::: + ```ts title="rslib.config.ts" export default { lib: [ diff --git a/website/docs/zh/guide/advanced/dts.mdx b/website/docs/zh/guide/advanced/dts.mdx index da5e7e80b..f9002b3cd 100644 --- a/website/docs/zh/guide/advanced/dts.mdx +++ b/website/docs/zh/guide/advanced/dts.mdx @@ -97,6 +97,12 @@ export default { +::: tip 版本要求 + +`@typescript/native-preview` 要求 Node.js 20.6.0 或更高版本。 + +::: + 2. 在 Rslib 配置文件中设置: ```ts title="rslib.config.ts" From 63fe406edef9faabb4ba3ea46e4b1c6110e552ce Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 22:27:42 +0800 Subject: [PATCH 11/14] chore: update --- tests/integration/banner-footer/index.test.ts | 104 ++++++----- tests/integration/redirect/dtsTsgo.test.ts | 164 +++++++++--------- 2 files changed, 145 insertions(+), 123 deletions(-) diff --git a/tests/integration/banner-footer/index.test.ts b/tests/integration/banner-footer/index.test.ts index 9a713b5bd..b5a1f4b56 100644 --- a/tests/integration/banner-footer/index.test.ts +++ b/tests/integration/banner-footer/index.test.ts @@ -1,4 +1,4 @@ -import { expect, test } from '@rstest/core'; +import { beforeAll, describe, expect, test } from '@rstest/core'; import { buildAndGetResults } from 'test-helper'; enum BannerFooter { @@ -10,51 +10,67 @@ enum BannerFooter { DTS_FOOTER = '/*! hello footer dts */', } -test('banner and footer should work in js, css and dts', async () => { - const fixturePath = __dirname; - const { js, css, dts } = await buildAndGetResults({ - fixturePath, - type: 'all', - }); - - const jsContents = Object.values(js.contents); - const cssContents = Object.values(css.contents); - const dtsContents = Object.values(dts.contents); +// There are 5 cases included in both tsc and tsgo +// 1. bundle esm +// 2. bundle cjs +// 3. bundleless esm +// 4. bundleless cjs +// 5. bundle esm with minify enabled +const checkBannerAndFooter = ( + contents: Record[], + type: 'js' | 'css' | 'dts', +) => { + for (const content of Object.values(contents)) { + if (content) { + const expectedBanner = + type === 'js' + ? BannerFooter.JS_BANNER + : type === 'css' + ? BannerFooter.CSS_BANNER + : BannerFooter.DTS_BANNER; + const expectedFooter = + type === 'js' + ? BannerFooter.JS_FOOTER + : type === 'css' + ? BannerFooter.CSS_FOOTER + : BannerFooter.DTS_FOOTER; - // There are 5 cases included in both tsc and tsgo - // 1. bundle esm - // 2. bundle cjs - // 3. bundleless esm - // 4. bundleless cjs - // 5. bundle esm with minify enabled - const checkBannerAndFooter = ( - contents: Record[], - type: 'js' | 'css' | 'dts', - ) => { - for (const content of Object.values(contents)) { - if (content) { - const expectedBanner = - type === 'js' - ? BannerFooter.JS_BANNER - : type === 'css' - ? BannerFooter.CSS_BANNER - : BannerFooter.DTS_BANNER; - const expectedFooter = - type === 'js' - ? BannerFooter.JS_FOOTER - : type === 'css' - ? BannerFooter.CSS_FOOTER - : BannerFooter.DTS_FOOTER; - - for (const value of Object.values(content)) { - expect(value).toContain(expectedBanner); - expect(value).toContain(expectedFooter); - } + for (const value of Object.values(content)) { + expect(value).toContain(expectedBanner); + expect(value).toContain(expectedFooter); } } - }; + } +}; + +describe('banner and footer should work in js, css and dts', () => { + let jsContents: Record[]; + let cssContents: Record[]; + let dtsContents: Record[]; + + beforeAll(async () => { + const fixturePath = __dirname; + const { js, css, dts } = await buildAndGetResults({ + fixturePath, + type: 'all', + }); + jsContents = Object.values(js.contents); + cssContents = Object.values(css.contents); + dtsContents = Object.values(dts.contents); + }); + + test('tsc to generate declaration files', () => { + checkBannerAndFooter(jsContents.slice(0, 5), 'js'); + checkBannerAndFooter(cssContents.slice(0, 5), 'css'); + checkBannerAndFooter(dtsContents.slice(0, 5), 'dts'); + }); - checkBannerAndFooter(jsContents, 'js'); - checkBannerAndFooter(cssContents, 'css'); - checkBannerAndFooter(dtsContents, 'dts'); + test.skipIf(process.version.startsWith('v18'))( + 'tsgo to generate declaration files', + () => { + checkBannerAndFooter(jsContents.slice(-5), 'js'); + checkBannerAndFooter(cssContents.slice(-5), 'css'); + checkBannerAndFooter(dtsContents.slice(-5), 'dts'); + }, + ); }); diff --git a/tests/integration/redirect/dtsTsgo.test.ts b/tests/integration/redirect/dtsTsgo.test.ts index ef56b6edd..6c77b9f92 100644 --- a/tests/integration/redirect/dtsTsgo.test.ts +++ b/tests/integration/redirect/dtsTsgo.test.ts @@ -1,77 +1,81 @@ import path from 'node:path'; -import { beforeAll, expect, test } from '@rstest/core'; +import { beforeAll, describe, expect, test } from '@rstest/core'; import { buildAndGetResults } from 'test-helper'; -let contents: Awaited>['contents']; +describe.skipIf(process.version.startsWith('v18'))( + 'dts redirect with tsgo', + () => { + let contents: Awaited>['contents']; -beforeAll(async () => { - const fixturePath = path.resolve(__dirname, './dts-tsgo'); - contents = (await buildAndGetResults({ fixturePath, type: 'dts' })).contents; -}, 20000); + beforeAll(async () => { + const fixturePath = path.resolve(__dirname, './dts-tsgo'); + contents = (await buildAndGetResults({ fixturePath, type: 'dts' })) + .contents; + }, 20000); -test('redirect.dts.path: true with redirect.dts.extension: false - default', async () => { - expect(contents.esm0).toMatchInlineSnapshot(` - { - "/tests/integration/redirect/dts-tsgo/dist/default/esm/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; - ", - "/tests/integration/redirect/dts-tsgo/dist/default/esm/.hidden.d.ts": "export declare const hidden = "This is a hidden file"; - ", - "/tests/integration/redirect/dts-tsgo/dist/default/esm/a.b/index.d.ts": "export declare const ab = "a.b"; - ", - "/tests/integration/redirect/dts-tsgo/dist/default/esm/bar.baz.d.ts": "export declare const bar = "bar-baz"; - ", - "/tests/integration/redirect/dts-tsgo/dist/default/esm/foo/foo.d.ts": "import { logRequest } from '../logger'; - import { logger } from '../../../../compile/prebundle-pkg'; - import { logRequest as logRequest2 } from '../logger'; - export { logRequest, logRequest2, logger }; - ", - "/tests/integration/redirect/dts-tsgo/dist/default/esm/foo/index.d.ts": "export type Barrel = string; - ", - "/tests/integration/redirect/dts-tsgo/dist/default/esm/index.d.ts": "import { logRequest } from './logger'; - import { logger } from '../../../compile/prebundle-pkg'; - import type { Baz } from './'; - import type { LoggerOptions } from './types'; - import { defaultOptions } from './types.js'; - import sources = require('./logger'); - export { sources, type Baz as self, logRequest, logger, type LoggerOptions, defaultOptions, }; - export * from './foo'; - export * from './logger'; - export type { Foo } from './types'; - // export { Router } from 'express'; - export * from '../../../compile/prebundle-pkg'; - export type { Bar } from './types'; - export * from './.hidden'; - export * from './.hidden-folder'; - export * from './a.b'; - export * from './bar.baz'; - export * from './foo'; - export * from './types'; - ", - "/tests/integration/redirect/dts-tsgo/dist/default/esm/logger.d.ts": "// import type { Request } from 'express'; - import type { LoggerOptions } from './types'; - export declare function logRequest(req: Request, options: LoggerOptions): void; - ", - "/tests/integration/redirect/dts-tsgo/dist/default/esm/types.d.ts": "export interface LoggerOptions { - logLevel: 'info' | 'debug' | 'warn' | 'error'; - logBody: boolean; - } - export declare const defaultOptions: LoggerOptions; - export interface Foo { - foo: string; - } - export interface Bar { - bar: string; - } - export interface Baz { - baz: string; - } - ", - } - `); -}); + test('redirect.dts.path: true with redirect.dts.extension: false - default', async () => { + expect(contents.esm0).toMatchInlineSnapshot(` + { + "/tests/integration/redirect/dts-tsgo/dist/default/esm/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/.hidden.d.ts": "export declare const hidden = "This is a hidden file"; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/a.b/index.d.ts": "export declare const ab = "a.b"; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/bar.baz.d.ts": "export declare const bar = "bar-baz"; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/foo/foo.d.ts": "import { logRequest } from '../logger'; + import { logger } from '../../../../compile/prebundle-pkg'; + import { logRequest as logRequest2 } from '../logger'; + export { logRequest, logRequest2, logger }; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/foo/index.d.ts": "export type Barrel = string; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/index.d.ts": "import { logRequest } from './logger'; + import { logger } from '../../../compile/prebundle-pkg'; + import type { Baz } from './'; + import type { LoggerOptions } from './types'; + import { defaultOptions } from './types.js'; + import sources = require('./logger'); + export { sources, type Baz as self, logRequest, logger, type LoggerOptions, defaultOptions, }; + export * from './foo'; + export * from './logger'; + export type { Foo } from './types'; + // export { Router } from 'express'; + export * from '../../../compile/prebundle-pkg'; + export type { Bar } from './types'; + export * from './.hidden'; + export * from './.hidden-folder'; + export * from './a.b'; + export * from './bar.baz'; + export * from './foo'; + export * from './types'; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/logger.d.ts": "// import type { Request } from 'express'; + import type { LoggerOptions } from './types'; + export declare function logRequest(req: Request, options: LoggerOptions): void; + ", + "/tests/integration/redirect/dts-tsgo/dist/default/esm/types.d.ts": "export interface LoggerOptions { + logLevel: 'info' | 'debug' | 'warn' | 'error'; + logBody: boolean; + } + export declare const defaultOptions: LoggerOptions; + export interface Foo { + foo: string; + } + export interface Bar { + bar: string; + } + export interface Baz { + baz: string; + } + ", + } + `); + }); -test('redirect.dts.path: false with redirect.dts.extension: false', async () => { - expect(contents.esm1).toMatchInlineSnapshot(` + test('redirect.dts.path: false with redirect.dts.extension: false', async () => { + expect(contents.esm1).toMatchInlineSnapshot(` { "/tests/integration/redirect/dts-tsgo/dist/path-false/esm/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; ", @@ -129,10 +133,10 @@ test('redirect.dts.path: false with redirect.dts.extension: false', async () => ", } `); -}); + }); -test('redirect.dts.path: true with redirect.dts.extension: true', async () => { - expect(contents.esm2).toMatchInlineSnapshot(` + test('redirect.dts.path: true with redirect.dts.extension: true', async () => { + expect(contents.esm2).toMatchInlineSnapshot(` { "/tests/integration/redirect/dts-tsgo/dist/extension-true/esm/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; ", @@ -190,10 +194,10 @@ test('redirect.dts.path: true with redirect.dts.extension: true', async () => { ", } `); -}); + }); -test('redirect.dts.path: false with dts.redirect.extension: true', async () => { - expect(contents.esm3).toMatchInlineSnapshot(` + test('redirect.dts.path: false with dts.redirect.extension: true', async () => { + expect(contents.esm3).toMatchInlineSnapshot(` { "/tests/integration/redirect/dts-tsgo/dist/path-false-extension-true/esm/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; ", @@ -251,10 +255,10 @@ test('redirect.dts.path: false with dts.redirect.extension: true', async () => { ", } `); -}); + }); -test('redirect.dts.extension: true with dts.autoExtension: true', async () => { - expect(contents.esm4).toMatchInlineSnapshot(` + test('redirect.dts.extension: true with dts.autoExtension: true', async () => { + expect(contents.esm4).toMatchInlineSnapshot(` { "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/esm/.hidden-folder/index.d.mts": "export declare const hiddenFolder = "This is a hidden folder"; ", @@ -312,7 +316,7 @@ test('redirect.dts.extension: true with dts.autoExtension: true', async () => { ", } `); - expect(contents.cjs).toMatchInlineSnapshot(` + expect(contents.cjs).toMatchInlineSnapshot(` { "/tests/integration/redirect/dts-tsgo/dist/auto-extension-true/cjs/.hidden-folder/index.d.ts": "export declare const hiddenFolder = "This is a hidden folder"; ", @@ -370,4 +374,6 @@ test('redirect.dts.extension: true with dts.autoExtension: true', async () => { ", } `); -}); + }); + }, +); From 75b9133993e80461d71b2d1d01b0499ec49422e7 Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 4 Sep 2025 22:35:03 +0800 Subject: [PATCH 12/14] chore: update --- tests/integration/banner-footer/index.test.ts | 103 ++++++++---------- 1 file changed, 45 insertions(+), 58 deletions(-) diff --git a/tests/integration/banner-footer/index.test.ts b/tests/integration/banner-footer/index.test.ts index b5a1f4b56..7eb1bcda2 100644 --- a/tests/integration/banner-footer/index.test.ts +++ b/tests/integration/banner-footer/index.test.ts @@ -1,4 +1,4 @@ -import { beforeAll, describe, expect, test } from '@rstest/core'; +import { expect, test } from '@rstest/core'; import { buildAndGetResults } from 'test-helper'; enum BannerFooter { @@ -10,67 +10,54 @@ enum BannerFooter { DTS_FOOTER = '/*! hello footer dts */', } -// There are 5 cases included in both tsc and tsgo -// 1. bundle esm -// 2. bundle cjs -// 3. bundleless esm -// 4. bundleless cjs -// 5. bundle esm with minify enabled -const checkBannerAndFooter = ( - contents: Record[], - type: 'js' | 'css' | 'dts', -) => { - for (const content of Object.values(contents)) { - if (content) { - const expectedBanner = - type === 'js' - ? BannerFooter.JS_BANNER - : type === 'css' - ? BannerFooter.CSS_BANNER - : BannerFooter.DTS_BANNER; - const expectedFooter = - type === 'js' - ? BannerFooter.JS_FOOTER - : type === 'css' - ? BannerFooter.CSS_FOOTER - : BannerFooter.DTS_FOOTER; - - for (const value of Object.values(content)) { - expect(value).toContain(expectedBanner); - expect(value).toContain(expectedFooter); - } - } - } -}; - -describe('banner and footer should work in js, css and dts', () => { - let jsContents: Record[]; - let cssContents: Record[]; - let dtsContents: Record[]; - - beforeAll(async () => { +test.skipIf(process.version.startsWith('v18'))( + 'banner and footer should work in js, css and dts', + async () => { const fixturePath = __dirname; const { js, css, dts } = await buildAndGetResults({ fixturePath, type: 'all', }); - jsContents = Object.values(js.contents); - cssContents = Object.values(css.contents); - dtsContents = Object.values(dts.contents); - }); - test('tsc to generate declaration files', () => { - checkBannerAndFooter(jsContents.slice(0, 5), 'js'); - checkBannerAndFooter(cssContents.slice(0, 5), 'css'); - checkBannerAndFooter(dtsContents.slice(0, 5), 'dts'); - }); + const jsContents = Object.values(js.contents); + const cssContents = Object.values(css.contents); + const dtsContents = Object.values(dts.contents); + + // There are 5 cases included in both tsc and tsgo + // 1. bundle esm + // 2. bundle cjs + // 3. bundleless esm + // 4. bundleless cjs + // 5. bundle esm with minify enabled + const checkBannerAndFooter = ( + contents: Record[], + type: 'js' | 'css' | 'dts', + ) => { + for (const content of Object.values(contents)) { + if (content) { + const expectedBanner = + type === 'js' + ? BannerFooter.JS_BANNER + : type === 'css' + ? BannerFooter.CSS_BANNER + : BannerFooter.DTS_BANNER; + const expectedFooter = + type === 'js' + ? BannerFooter.JS_FOOTER + : type === 'css' + ? BannerFooter.CSS_FOOTER + : BannerFooter.DTS_FOOTER; + + for (const value of Object.values(content)) { + expect(value).toContain(expectedBanner); + expect(value).toContain(expectedFooter); + } + } + } + }; - test.skipIf(process.version.startsWith('v18'))( - 'tsgo to generate declaration files', - () => { - checkBannerAndFooter(jsContents.slice(-5), 'js'); - checkBannerAndFooter(cssContents.slice(-5), 'css'); - checkBannerAndFooter(dtsContents.slice(-5), 'dts'); - }, - ); -}); + checkBannerAndFooter(jsContents, 'js'); + checkBannerAndFooter(cssContents, 'css'); + checkBannerAndFooter(dtsContents, 'dts'); + }, +); From 11273968cccb71e265895fc739d75f4803f76daf Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Fri, 5 Sep 2025 16:48:23 +0800 Subject: [PATCH 13/14] chore: update --- packages/core/src/config.ts | 2 +- packages/core/src/types/config.ts | 15 ++----- packages/plugin-dts/README.md | 29 +++++++------ packages/plugin-dts/src/dts.ts | 5 +-- packages/plugin-dts/src/index.ts | 9 ++-- .../integration/banner-footer/rslib.config.ts | 20 +++------ .../build/abort-on-error/rslib.config.ts | 4 +- .../build/auto-extension/rslib.config.ts | 4 +- .../dts-tsgo/build/basic/rslib.config.ts | 4 +- .../dts-tsgo/build/clean/rslib.config.ts | 8 +--- .../build/declaration-map/rslib.config.ts | 8 +--- .../dts-tsgo/build/dist-path/rslib.config.ts | 4 +- .../build/process-files/rslib.config.ts | 4 +- .../abort-on-error/rslib.config.ts | 4 +- .../bundle-false/alias/rslib.config.ts | 8 +--- .../auto-extension/rslib.config.ts | 8 +--- .../bundle-false/basic/rslib.config.ts | 8 +--- .../bundle-false/clean/rslib.config.ts | 8 +--- .../declaration-dir/rslib.config.ts | 4 +- .../declaration-map/rslib.config.ts | 8 +--- .../bundle-false/dist-path/rslib.config.ts | 4 +- .../tsconfig-path/rslib.config.ts | 4 +- .../bundle/abort-on-error/rslib.config.ts | 4 +- .../bundle/absolute-entry/rslib.config.ts | 4 +- .../bundle/auto-extension/rslib.config.ts | 4 +- .../dts-tsgo/bundle/basic/rslib.config.ts | 8 +--- .../bundle/bundle-name/rslib.config.ts | 4 +- .../bundle/bundled-packages/rslib.config.ts | 12 ++---- .../dts-tsgo/bundle/clean/rslib.config.ts | 8 +--- .../dts-tsgo/bundle/dist-path/rslib.config.ts | 4 +- .../bundle/multiple-entries/rslib.config.ts | 8 +--- .../dts-tsgo/bundle/rootdir/rslib.config.ts | 8 +--- .../redirect/dts-tsgo/rslib.config.ts | 24 +++-------- website/docs/en/config/lib/dts.mdx | 42 +++++++++---------- website/docs/en/guide/advanced/dts.mdx | 25 +++++++---- website/docs/zh/config/lib/dts.mdx | 42 +++++++++---------- website/docs/zh/guide/advanced/dts.mdx | 25 +++++++---- 37 files changed, 151 insertions(+), 243 deletions(-) diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index f7649a19d..71dffc357 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -1500,7 +1500,7 @@ const composeDtsConfig = async ( banner: banner?.dts, footer: footer?.dts, redirect: redirect?.dts, - experiments: dts?.experiments, + tsgo: dts?.tsgo, }), ], }; diff --git a/packages/core/src/types/config.ts b/packages/core/src/types/config.ts index 439113afc..4e86bffd1 100644 --- a/packages/core/src/types/config.ts +++ b/packages/core/src/types/config.ts @@ -95,18 +95,11 @@ export type Dts = */ alias?: Record; /** - * [Experimental] Whether to enable experimental features. - * @defaultValue `{}` - * @see {@link https://rslib.rs/config/lib/dts#dtsexperiments} + * [Experimental] Whether to generate declaration files with `tsgo`. + * @defaultValue `false` + * @see {@link https://rslib.rs/config/lib/dts#dtstsgo} */ - experiments?: { - /** - * [Experimental] Whether to generate declaration files with `tsgo`. - * @defaultValue `false` - * @see {@link https://rslib.rs/config/lib/dts#dtsexperimentstsgo} - */ - tsgo?: boolean; - }; + tsgo?: boolean; } | boolean; diff --git a/packages/plugin-dts/README.md b/packages/plugin-dts/README.md index 92a152c4d..3f3b51583 100644 --- a/packages/plugin-dts/README.md +++ b/packages/plugin-dts/README.md @@ -136,7 +136,7 @@ pluginDts({ }); ``` -> When [experiments.tsgo](#experimentstsgo) is enabled, if the project also enables [build](#build) or emits declaration files with different extensions to the same directory, `dtsExtension` may not work correctly. +> When [tsgo](#tsgo) is enabled, if the project also enables [build](#build) or emits declaration files with different extensions to the same directory, `dtsExtension` may not work correctly. ### alias @@ -282,19 +282,14 @@ import { foo } from './foo.mjs'; // expected output of './dist/bar.d.mts' - When set to `false`, the file extension will remain unchanged from the original import path in the rewritten import path of the output file (regardless of whether it is specified or specified as any value). -### experiments - -- **Type:** `{ tsgo?: boolean }` -- **Default:** `{}` - -Whether to enable experimental features. - -#### experiments.tsgo +### tsgo - **Type:** `boolean` - **Default:** `false` -Whether to generate declaration files with [tsgo](https://github.com/microsoft/typescript-go). +Whether to generate declaration files with [tsgo](https://github.com/microsoft/typescript-go), which can provide faster generation of declaration files, especially for large projects. + +> This feature is currently an **experimental feature**. Since tsgo is still in the **experimental stage**, there may be some bugs and unresolved issues or limitations. So, make sure to fully test it in your project before enabling this option. To enable this option, you need to: @@ -306,17 +301,21 @@ npm add @typescript/native-preview -D > `@typescript/native-preview` requires Node.js 20.6.0 or higher. -2. Set `experiments.tsgo` to `true`. +2. Set `tsgo` to `true`. ```js pluginDts({ - experiments: { - tsgo: true, - }, + tsgo: true, }); ``` -> `tsgo` can provide faster generation of declaration files, especially for large projects. However, since `tsgo` is still experimental, there may be unresolved issues or limitations. Therefore, please make sure to thoroughly test it in your project before enabling this option. +3. In order to ensure the consistency of local development, you need to install the corresponding [VS Code Preview Extension](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.native-preview) and add the following configuration in the VS Code settings: + +```json +{ + "typescript.experimental.useTsgo": true +} +``` ## Contributing diff --git a/packages/plugin-dts/src/dts.ts b/packages/plugin-dts/src/dts.ts index 8313a2127..028757672 100644 --- a/packages/plugin-dts/src/dts.ts +++ b/packages/plugin-dts/src/dts.ts @@ -135,11 +135,8 @@ export async function generateDts(data: DtsGenOptions): Promise { path: true, extension: false, }, - experiments = { - tsgo: false, - }, + tsgo, } = data; - const { tsgo } = experiments; if (!isWatch) { logger.start(`generating declaration files... ${color.dim(`(${name})`)}`); } diff --git a/packages/plugin-dts/src/index.ts b/packages/plugin-dts/src/index.ts index 437dc8092..3a1cf7260 100644 --- a/packages/plugin-dts/src/index.ts +++ b/packages/plugin-dts/src/index.ts @@ -44,9 +44,7 @@ export type PluginDtsOptions = { banner?: string; footer?: string; redirect?: DtsRedirect; - experiments?: { - tsgo?: boolean; - }; + tsgo?: boolean; }; export type DtsEntry = { @@ -96,8 +94,7 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ options.redirect.path = options.redirect.path ?? true; options.redirect.extension = options.redirect.extension ?? false; options.alias = options.alias ?? {}; - options.experiments = options.experiments ?? {}; - options.experiments.tsgo = options.experiments.tsgo ?? false; + options.tsgo = options.tsgo ?? false; const dtsPromises: Promise[] = []; let promisesResult: TaskResult[] = []; @@ -105,7 +102,7 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({ api.onBeforeEnvironmentCompile( async ({ isWatch, isFirstCompile, environment }) => { - if (!isFirstCompile && !options.experiments?.tsgo) { + if (!isFirstCompile && !options.tsgo) { return; } diff --git a/tests/integration/banner-footer/rslib.config.ts b/tests/integration/banner-footer/rslib.config.ts index 0b6b0fc47..a18eada8d 100644 --- a/tests/integration/banner-footer/rslib.config.ts +++ b/tests/integration/banner-footer/rslib.config.ts @@ -98,9 +98,7 @@ export default defineConfig({ }, dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, ...bannerFooterConfig, }), @@ -113,9 +111,7 @@ export default defineConfig({ }, dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, ...bannerFooterConfig, }), @@ -129,9 +125,7 @@ export default defineConfig({ bundle: false, dts: { bundle: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, source: { entry: { @@ -150,9 +144,7 @@ export default defineConfig({ bundle: false, dts: { bundle: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, source: { entry: { @@ -171,9 +163,7 @@ export default defineConfig({ }, dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, ...bannerFooterConfig, }), diff --git a/tests/integration/dts-tsgo/build/abort-on-error/rslib.config.ts b/tests/integration/dts-tsgo/build/abort-on-error/rslib.config.ts index 1199897e0..7891e7fa0 100644 --- a/tests/integration/dts-tsgo/build/abort-on-error/rslib.config.ts +++ b/tests/integration/dts-tsgo/build/abort-on-error/rslib.config.ts @@ -9,9 +9,7 @@ export default defineConfig({ bundle: false, build: true, abortOnError: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/build/auto-extension/rslib.config.ts b/tests/integration/dts-tsgo/build/auto-extension/rslib.config.ts index 6957f5a6c..2c31c42af 100644 --- a/tests/integration/dts-tsgo/build/auto-extension/rslib.config.ts +++ b/tests/integration/dts-tsgo/build/auto-extension/rslib.config.ts @@ -10,9 +10,7 @@ export default defineConfig({ distPath: './dist/types', bundle: false, build: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/build/basic/rslib.config.ts b/tests/integration/dts-tsgo/build/basic/rslib.config.ts index c474c2166..811bb118b 100644 --- a/tests/integration/dts-tsgo/build/basic/rslib.config.ts +++ b/tests/integration/dts-tsgo/build/basic/rslib.config.ts @@ -8,9 +8,7 @@ export default defineConfig({ dts: { bundle: false, build: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/build/clean/rslib.config.ts b/tests/integration/dts-tsgo/build/clean/rslib.config.ts index 2baa1b1cf..97ca4aec7 100644 --- a/tests/integration/dts-tsgo/build/clean/rslib.config.ts +++ b/tests/integration/dts-tsgo/build/clean/rslib.config.ts @@ -9,9 +9,7 @@ export default defineConfig({ distPath: './dist-types/esm', bundle: false, build: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, source: { tsconfigPath: './tsconfig.esm.json', @@ -23,9 +21,7 @@ export default defineConfig({ distPath: './dist-types/cjs', bundle: false, build: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, source: { tsconfigPath: './tsconfig.cjs.json', diff --git a/tests/integration/dts-tsgo/build/declaration-map/rslib.config.ts b/tests/integration/dts-tsgo/build/declaration-map/rslib.config.ts index 8f1d28294..6419acbfc 100644 --- a/tests/integration/dts-tsgo/build/declaration-map/rslib.config.ts +++ b/tests/integration/dts-tsgo/build/declaration-map/rslib.config.ts @@ -8,9 +8,7 @@ export default defineConfig({ dts: { autoExtension: true, build: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, source: { tsconfigPath: './tsconfig.esm.json', @@ -21,9 +19,7 @@ export default defineConfig({ dts: { autoExtension: true, build: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, source: { tsconfigPath: './tsconfig.cjs.json', diff --git a/tests/integration/dts-tsgo/build/dist-path/rslib.config.ts b/tests/integration/dts-tsgo/build/dist-path/rslib.config.ts index 2c272410d..97dc1f979 100644 --- a/tests/integration/dts-tsgo/build/dist-path/rslib.config.ts +++ b/tests/integration/dts-tsgo/build/dist-path/rslib.config.ts @@ -9,9 +9,7 @@ export default defineConfig({ bundle: false, build: true, distPath: './dist/custom', - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/build/process-files/rslib.config.ts b/tests/integration/dts-tsgo/build/process-files/rslib.config.ts index 38b668fad..35ffb1d85 100644 --- a/tests/integration/dts-tsgo/build/process-files/rslib.config.ts +++ b/tests/integration/dts-tsgo/build/process-files/rslib.config.ts @@ -15,9 +15,7 @@ export default defineConfig({ bundle: false, build: true, autoExtension: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle-false/abort-on-error/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/abort-on-error/rslib.config.ts index 3b06f12c3..a96dbc210 100644 --- a/tests/integration/dts-tsgo/bundle-false/abort-on-error/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle-false/abort-on-error/rslib.config.ts @@ -8,9 +8,7 @@ export default defineConfig({ dts: { bundle: false, abortOnError: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ diff --git a/tests/integration/dts-tsgo/bundle-false/alias/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/alias/rslib.config.ts index 09477ef7e..84a84882f 100644 --- a/tests/integration/dts-tsgo/bundle-false/alias/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle-false/alias/rslib.config.ts @@ -9,9 +9,7 @@ export default defineConfig({ alias: { 'prebundle-pkg': './compile/prebundle-pkg', }, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ @@ -20,9 +18,7 @@ export default defineConfig({ alias: { 'prebundle-pkg': './compile/prebundle-pkg', }, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle-false/auto-extension/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/auto-extension/rslib.config.ts index e6d051586..2a1bafcb5 100644 --- a/tests/integration/dts-tsgo/bundle-false/auto-extension/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle-false/auto-extension/rslib.config.ts @@ -9,9 +9,7 @@ export default defineConfig({ autoExtension: true, distPath: './dist/types/esm', bundle: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ @@ -20,9 +18,7 @@ export default defineConfig({ autoExtension: true, distPath: './dist/types/cjs', bundle: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle-false/basic/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/basic/rslib.config.ts index 1abcb2e0a..b95611b23 100644 --- a/tests/integration/dts-tsgo/bundle-false/basic/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle-false/basic/rslib.config.ts @@ -7,18 +7,14 @@ export default defineConfig({ bundle: false, dts: { bundle: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ bundle: false, dts: { bundle: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle-false/clean/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/clean/rslib.config.ts index 110e1b482..0e75393b7 100644 --- a/tests/integration/dts-tsgo/bundle-false/clean/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle-false/clean/rslib.config.ts @@ -8,9 +8,7 @@ export default defineConfig({ dts: { bundle: false, distPath: './dist-types/esm', - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ @@ -18,9 +16,7 @@ export default defineConfig({ dts: { bundle: false, distPath: './dist-types/cjs', - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle-false/declaration-dir/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/declaration-dir/rslib.config.ts index db70754cb..3730a8bf6 100644 --- a/tests/integration/dts-tsgo/bundle-false/declaration-dir/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle-false/declaration-dir/rslib.config.ts @@ -7,9 +7,7 @@ export default defineConfig({ bundle: false, dts: { bundle: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle-false/declaration-map/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/declaration-map/rslib.config.ts index 6192cd6f8..00f279cee 100644 --- a/tests/integration/dts-tsgo/bundle-false/declaration-map/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle-false/declaration-map/rslib.config.ts @@ -7,18 +7,14 @@ export default defineConfig({ bundle: false, dts: { autoExtension: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ bundle: false, dts: { autoExtension: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle-false/dist-path/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/dist-path/rslib.config.ts index d04ec5c48..4bde7b478 100644 --- a/tests/integration/dts-tsgo/bundle-false/dist-path/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle-false/dist-path/rslib.config.ts @@ -8,9 +8,7 @@ export default defineConfig({ dts: { bundle: false, distPath: './dist/custom', - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ diff --git a/tests/integration/dts-tsgo/bundle-false/tsconfig-path/rslib.config.ts b/tests/integration/dts-tsgo/bundle-false/tsconfig-path/rslib.config.ts index 70af17616..04a1c203b 100644 --- a/tests/integration/dts-tsgo/bundle-false/tsconfig-path/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle-false/tsconfig-path/rslib.config.ts @@ -7,9 +7,7 @@ export default defineConfig({ bundle: false, dts: { bundle: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle/abort-on-error/rslib.config.ts b/tests/integration/dts-tsgo/bundle/abort-on-error/rslib.config.ts index 140517c6f..3924b5584 100644 --- a/tests/integration/dts-tsgo/bundle/abort-on-error/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle/abort-on-error/rslib.config.ts @@ -7,9 +7,7 @@ export default defineConfig({ dts: { bundle: true, abortOnError: false, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle/absolute-entry/rslib.config.ts b/tests/integration/dts-tsgo/bundle/absolute-entry/rslib.config.ts index 2f5ae4cfb..0dfc52e20 100644 --- a/tests/integration/dts-tsgo/bundle/absolute-entry/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle/absolute-entry/rslib.config.ts @@ -7,9 +7,7 @@ export default defineConfig({ generateBundleEsmConfig({ dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle/auto-extension/rslib.config.ts b/tests/integration/dts-tsgo/bundle/auto-extension/rslib.config.ts index 1c2ee6da6..24ff80728 100644 --- a/tests/integration/dts-tsgo/bundle/auto-extension/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle/auto-extension/rslib.config.ts @@ -7,9 +7,7 @@ export default defineConfig({ dts: { bundle: true, autoExtension: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle/basic/rslib.config.ts b/tests/integration/dts-tsgo/bundle/basic/rslib.config.ts index 0b02f639e..7e95c2f27 100644 --- a/tests/integration/dts-tsgo/bundle/basic/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle/basic/rslib.config.ts @@ -6,17 +6,13 @@ export default defineConfig({ generateBundleEsmConfig({ dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle/bundle-name/rslib.config.ts b/tests/integration/dts-tsgo/bundle/bundle-name/rslib.config.ts index 39d77531b..6ffdf3d60 100644 --- a/tests/integration/dts-tsgo/bundle/bundle-name/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle/bundle-name/rslib.config.ts @@ -6,9 +6,7 @@ export default defineConfig({ generateBundleEsmConfig({ dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle/bundled-packages/rslib.config.ts b/tests/integration/dts-tsgo/bundle/bundled-packages/rslib.config.ts index b74f3cc4c..1d0e80b00 100644 --- a/tests/integration/dts-tsgo/bundle/bundled-packages/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle/bundled-packages/rslib.config.ts @@ -6,9 +6,7 @@ export default defineConfig({ generateBundleEsmConfig({ dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, output: { distPath: { @@ -21,9 +19,7 @@ export default defineConfig({ bundle: { bundledPackages: [], }, - experiments: { - tsgo: true, - }, + tsgo: true, }, output: { distPath: { @@ -44,9 +40,7 @@ export default defineConfig({ 'reselect', ], }, - experiments: { - tsgo: true, - }, + tsgo: true, }, output: { distPath: { diff --git a/tests/integration/dts-tsgo/bundle/clean/rslib.config.ts b/tests/integration/dts-tsgo/bundle/clean/rslib.config.ts index 93809d342..75515e15a 100644 --- a/tests/integration/dts-tsgo/bundle/clean/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle/clean/rslib.config.ts @@ -7,18 +7,14 @@ export default defineConfig({ dts: { bundle: true, distPath: './dist-types/esm', - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ dts: { bundle: true, distPath: './dist-types/cjs', - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle/dist-path/rslib.config.ts b/tests/integration/dts-tsgo/bundle/dist-path/rslib.config.ts index 66b94b940..4336bd842 100644 --- a/tests/integration/dts-tsgo/bundle/dist-path/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle/dist-path/rslib.config.ts @@ -7,9 +7,7 @@ export default defineConfig({ dts: { bundle: true, distPath: './dist/custom', - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle/multiple-entries/rslib.config.ts b/tests/integration/dts-tsgo/bundle/multiple-entries/rslib.config.ts index 75866e157..e76e671f5 100644 --- a/tests/integration/dts-tsgo/bundle/multiple-entries/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle/multiple-entries/rslib.config.ts @@ -6,17 +6,13 @@ export default defineConfig({ generateBundleEsmConfig({ dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/dts-tsgo/bundle/rootdir/rslib.config.ts b/tests/integration/dts-tsgo/bundle/rootdir/rslib.config.ts index 29bcae09b..f8e4f74bc 100644 --- a/tests/integration/dts-tsgo/bundle/rootdir/rslib.config.ts +++ b/tests/integration/dts-tsgo/bundle/rootdir/rslib.config.ts @@ -6,17 +6,13 @@ export default defineConfig({ generateBundleEsmConfig({ dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), generateBundleCjsConfig({ dts: { bundle: true, - experiments: { - tsgo: true, - }, + tsgo: true, }, }), ], diff --git a/tests/integration/redirect/dts-tsgo/rslib.config.ts b/tests/integration/redirect/dts-tsgo/rslib.config.ts index 35cbddc18..1f266fc55 100644 --- a/tests/integration/redirect/dts-tsgo/rslib.config.ts +++ b/tests/integration/redirect/dts-tsgo/rslib.config.ts @@ -6,9 +6,7 @@ export default defineConfig({ // 0 - default - path: true extension: false generateBundleEsmConfig({ dts: { - experiments: { - tsgo: true, - }, + tsgo: true, }, output: { distPath: { @@ -19,9 +17,7 @@ export default defineConfig({ // 1 - path: false extension: false generateBundleEsmConfig({ dts: { - experiments: { - tsgo: true, - }, + tsgo: true, }, output: { distPath: { @@ -37,9 +33,7 @@ export default defineConfig({ // 2 - path: true extension: true generateBundleEsmConfig({ dts: { - experiments: { - tsgo: true, - }, + tsgo: true, }, output: { distPath: { @@ -55,9 +49,7 @@ export default defineConfig({ // 3 - path: false extension: true generateBundleEsmConfig({ dts: { - experiments: { - tsgo: true, - }, + tsgo: true, }, output: { distPath: { @@ -74,10 +66,8 @@ export default defineConfig({ // 4 - extension: true with dts.autoExtension true generateBundleEsmConfig({ dts: { - experiments: { - tsgo: true, - }, autoExtension: true, + tsgo: true, }, output: { distPath: { @@ -93,10 +83,8 @@ export default defineConfig({ // 5 - extension: true with dts.autoExtension true generateBundleCjsConfig({ dts: { - experiments: { - tsgo: true, - }, autoExtension: true, + tsgo: true, }, output: { distPath: { diff --git a/website/docs/en/config/lib/dts.mdx b/website/docs/en/config/lib/dts.mdx index 3c34b36d9..8a889f2e9 100644 --- a/website/docs/en/config/lib/dts.mdx +++ b/website/docs/en/config/lib/dts.mdx @@ -15,9 +15,7 @@ type Dts = abortOnError?: boolean; autoExtension?: boolean; alias?: Record; - experiments?: { - tsgo?: boolean; - }; + tsgo?: boolean; } | boolean; ``` @@ -221,7 +219,7 @@ When `dts.autoExtension` is set to `true`, the declaration file extension will b 1. It follows the same logic as [lib.autoExtension](/config/lib/auto-extension), but the default value is different since the declaration file extension may cause some issues with different module resolution strategies. -2. When [dts.experiments.tsgo](/config/lib/dts#dtsexperimentstsgo) is enabled, if the project also enables [dts.build](/config/lib/dts#dtsbuild) or emits declaration files with different extensions to the same directory, `dts.autoExtension` may not work correctly. +2. When [dts.tsgo](/config/lib/dts#dtstsgo) is enabled, if the project also enables [dts.build](/config/lib/dts#dtsbuild) or emits declaration files with different extensions to the same directory, `dts.autoExtension` may not work correctly. ::: @@ -251,21 +249,22 @@ export default { }; ``` -### dts.experiments +### dts.tsgo -- **Type:** `{ tsgo?: boolean }` -- **Default:** `{}` +- **Type:** `boolean` +- **Default:** `false` -Whether to enable experimental features. +Whether to generate declaration files with [tsgo](https://github.com/microsoft/typescript-go), which can provide faster generation of declaration files, especially for large projects. -#### dts.experiments.tsgo +::: tip -- **Type:** `boolean` -- **Default:** `false` +This feature is currently an **experimental feature**. Since tsgo is still in the **experimental stage**, there may be some bugs and unresolved issues or limitations. So, make sure to fully test it in your project before enabling this option. + +::: -Whether to generate declaration files with [tsgo](https://github.com/microsoft/typescript-go). +To enable this option, you need to: -To enable this option, you need to install [@typescript/native-preview](https://www.npmjs.com/package/@typescript/native-preview) as a development dependency. +1. Install [@typescript/native-preview](https://www.npmjs.com/package/@typescript/native-preview) as a development dependency. @@ -275,23 +274,24 @@ To enable this option, you need to install [@typescript/native-preview](https:// ::: +2. Configure in the Rslib config file: + ```ts title="rslib.config.ts" export default { lib: [ { dts: { - // [!code highlight:3] - experiments: { - tsgo: true, - }, + tsgo: true, // [!code highlight] }, }, ], }; ``` -::: note +3. In order to ensure the consistency of local development, you need to install the corresponding [VS Code Preview Extension](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.native-preview) and add the following configuration in the VS Code settings: -`tsgo` can provide faster generation of declaration files, especially for large projects. However, since `tsgo` is still experimental, there may be unresolved issues or limitations. Therefore, please make sure to thoroughly test it in your project before enabling this option. - -::: +```json +{ + "typescript.experimental.useTsgo": true +} +``` diff --git a/website/docs/en/guide/advanced/dts.mdx b/website/docs/en/guide/advanced/dts.mdx index 8fc20b18b..34ee984a9 100644 --- a/website/docs/en/guide/advanced/dts.mdx +++ b/website/docs/en/guide/advanced/dts.mdx @@ -70,7 +70,7 @@ export default { ### Generate bundle declaration files -1. Install `@microsoft/api-extractor` as `devDependencies`, which is the underlying tool used for bundling declaration files. +1. Install `@microsoft/api-extractor` as a development dependency, which is the underlying tool used for bundling declaration files. import { PackageManagerTabs } from '@theme'; @@ -93,7 +93,13 @@ export default { ### Generate declaration files with tsgo -1. Install `@typescript/native-preview` as `devDependencies`: +::: tip + +This feature is currently an **experimental feature**. Since tsgo is still in the **experimental stage**, there may be some bugs and unresolved issues or limitations. So, make sure to fully test it in your project before enabling this option. + +::: + +1. Install `@typescript/native-preview` as a development dependency: @@ -110,16 +116,21 @@ export default { lib: [ { dts: { - // [!code highlight:3] - experiments: { - tsgo: true, - }, + tsgo: true, // [!code highlight] }, }, ], }; ``` +3. In order to ensure the consistency of local development, you need to install the corresponding [VS Code Preview Extension](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.native-preview) and add the following configuration in the VS Code settings: + +```json +{ + "typescript.experimental.useTsgo": true +} +``` + ### Notes During the generation of declaration files, Rslib will automatically enforce some configuration options in `tsconfig.json` to ensure that the [TypeScript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) or [tsgo](https://github.com/microsoft/typescript-go) generates only declaration files. @@ -150,7 +161,7 @@ The priority from highest to lowest of final output directory of declaration fil | [dts.abortOnError](/config/lib/dts#dtsabortonerror) | Whether to abort the build process when an error occurs during declaration files generation. | | [dts.autoExtension](/config/lib/dts#dtsautoextension) | Whether to automatically set the declaration file extension based on the [format](/config/lib/format) option. | | [dts.alias](/config/lib/dts#dtsalias) | The path alias of the declaration files. | -| [dts.experiments.tsgo](/config/lib/dts#dtsexperimentstsgo) | Whether to generate declaration files with [tsgo](https://github.com/microsoft/TypeScript/pull/48729). | +| [dts.tsgo](/config/lib/dts#dtstsgo) | Whether to generate declaration files with [tsgo](https://github.com/microsoft/TypeScript/pull/48729). | | [banner.dts](/config/lib/banner#bannerdts) | Inject content into the top of each declaration output file. | | [footer.dts](/config/lib/footer#footerdts) | Inject content into the bottom of each declaration file. | | [redirect.dts.path](/config/lib/redirect#redirectdtspath) | Whether to automatically redirect the import paths of TypeScript declaration output files. | diff --git a/website/docs/zh/config/lib/dts.mdx b/website/docs/zh/config/lib/dts.mdx index 11daa0b82..cba0d4186 100644 --- a/website/docs/zh/config/lib/dts.mdx +++ b/website/docs/zh/config/lib/dts.mdx @@ -15,9 +15,7 @@ type Dts = abortOnError?: boolean; autoExtension?: boolean; alias?: Record; - experiments?: { - tsgo?: boolean; - }; + tsgo?: boolean; } | boolean; ``` @@ -221,7 +219,7 @@ export default { 1. 这遵循与 [lib.autoExtension](/config/lib/auto-extension) 相同的逻辑,但默认值不同,因为类型声明文件扩展名可能会在不同的模块解析策略中造成一些问题。 -2. 当开启 [dts.experiments.tsgo](/config/lib/dts#dtsexperimentstsgo) 时,如果项目同时开启了 [dts.build](/config/lib/dts#dtsbuild) 或者将不同后缀的类型声明文件输出到同一目录,`dts.autoExtension` 无法正常生效。 +2. 当开启 [dts.tsgo](/config/lib/dts#dtstsgo) 时,如果项目同时开启了 [dts.build](/config/lib/dts#dtsbuild) 或者将不同后缀的类型声明文件输出到同一目录,`dts.autoExtension` 无法正常生效。 ::: @@ -251,21 +249,22 @@ export default { }; ``` -### dts.experiments +### dts.tsgo -- **类型:** `{ tsgo?: boolean }` -- **默认值:** `{}` +- **类型:** `boolean` +- **默认值:** `false` -用于启用实验性功能。 +是否使用 [tsgo](https://github.com/microsoft/typescript-go) 生成类型声明文件。tsgo 可以提供更快的类型声明文件生成速度,尤其是对于大型项目。 -#### dts.experiments.tsgo +::: tip -- **类型:** `boolean` -- **默认值:** `false` +此功能目前为**实验性功能**。由于 tsgo 仍处于**实验阶段**,可能会存在一些 bug 以及未解决的问题或限制。因此,在启用该选项前,请确保在你的项目中进行充分测试。 + +::: -是否使用 [tsgo](https://github.com/microsoft/typescript-go) 生成类型声明文件。 +启用该选项,你需要: -如果需要启用该选项,需要安装 [@typescript/native-preview](https://www.npmjs.com/package/@typescript/native-preview) 作为开发依赖。 +1. 安装 [@typescript/native-preview](https://www.npmjs.com/package/@typescript/native-preview) 作为开发依赖。 @@ -275,23 +274,24 @@ export default { ::: +2. 在 Rslib 配置文件中设置: + ```ts title="rslib.config.ts" export default { lib: [ { dts: { - // [!code highlight:3] - experiments: { - tsgo: true, - }, + tsgo: true, // [!code highlight] }, }, ], }; ``` -::: note +3. 为了保证本地开发的一致性,你需要安装对应的 [VS Code 预览版扩展](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.native-preview),并在 VS Code 设置中添加如下配置: -`tsgo` 可以提供更快的类型声明文件生成速度,尤其是对于大型项目。然而,由于 `tsgo` 仍处于实验阶段,可能会存在一些未解决的问题或限制。因此,在启用该选项前,请确保在你的项目中进行充分测试。 - -::: +```json +{ + "typescript.experimental.useTsgo": true +} +``` diff --git a/website/docs/zh/guide/advanced/dts.mdx b/website/docs/zh/guide/advanced/dts.mdx index f9002b3cd..ca2bd38d4 100644 --- a/website/docs/zh/guide/advanced/dts.mdx +++ b/website/docs/zh/guide/advanced/dts.mdx @@ -70,7 +70,7 @@ export default { ### 生成 bundle 类型 -1. 安装 `@microsoft/api-extractor` 作为 `devDependencies`,这是用于打包类型声明文件的底层工具。 +1. 安装 `@microsoft/api-extractor` 作为开发依赖,这是用于打包类型声明文件的底层工具。 import { PackageManagerTabs } from '@theme'; @@ -93,7 +93,13 @@ export default { ### 使用 tsgo 生成类型声明文件 -1. 安装 `@typescript/native-preview` 作为 `devDependencies`: +::: tip + +此功能目前为**实验性功能**。由于 tsgo 仍处于**实验阶段**,可能会存在一些 bug 以及未解决的问题或限制。因此,在启用该选项前,请确保在你的项目中进行充分测试。 + +::: + +1. 安装 `@typescript/native-preview` 作为开发依赖: @@ -110,16 +116,21 @@ export default { lib: [ { dts: { - // [!code highlight:3] - experiments: { - tsgo: true, - }, + tsgo: true, // [!code highlight] }, }, ], }; ``` +3. 为了保证本地开发的一致性,你需要安装对应的 [VS Code 预览版扩展](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.native-preview),并在 VS Code 设置中添加如下配置: + +```json +{ + "typescript.experimental.useTsgo": true +} +``` + ### 注意事项 Rslib 在生成类型声明文件的过程中,默认会强制设置 `tsconfig.json` @@ -153,7 +164,7 @@ API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) 或 [t | [dts.abortOnError](/config/lib/dts#dtsabortonerror) | 当类型声明文件生成过程中出现错误时,是否中止构建过程。 | | [dts.autoExtension](/config/lib/dts#dtsautoextension) | 是否根据 [format](/config/lib/format) 选项自动设置类型声明文件扩展名。 | | [dts.alias](/config/lib/dts#dtsalias) | 类型声明文件的路径别名。 | -| [dts.experiments.tsgo](/config/lib/dts#dtsexperimentstsgo) | 是否使用 [tsgo](https://github.com/microsoft/typescript-go) 生成类型声明文件。 | +| [dts.tsgo](/config/lib/dts#dtstsgo) | 是否使用 [tsgo](https://github.com/microsoft/typescript-go) 生成类型声明文件。 | | [banner.dts](/config/lib/banner#bannerdts) | 在每个类型声明文件顶部注入内容。 | | [footer.dts](/config/lib/footer#footerdts) | 在每个类型声明文件底部注入内容。 | | [redirect.dts.path](/config/lib/redirect#redirectdtspath) | 是否自动重定向类型声明文件中的导入路径。 | From 1db1b954d8dbed482cf4296320ebfaf3686bc3fa Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Fri, 5 Sep 2025 18:10:12 +0800 Subject: [PATCH 14/14] chore: update --- packages/core/src/types/config.ts | 3 ++- website/docs/en/config/lib/dts.mdx | 2 +- website/docs/en/guide/advanced/dts.mdx | 2 +- website/docs/zh/config/lib/dts.mdx | 2 +- website/docs/zh/guide/advanced/dts.mdx | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/core/src/types/config.ts b/packages/core/src/types/config.ts index 4e86bffd1..e00f2597f 100644 --- a/packages/core/src/types/config.ts +++ b/packages/core/src/types/config.ts @@ -95,7 +95,8 @@ export type Dts = */ alias?: Record; /** - * [Experimental] Whether to generate declaration files with `tsgo`. + * Whether to generate declaration files with `tsgo`. + * @experimental * @defaultValue `false` * @see {@link https://rslib.rs/config/lib/dts#dtstsgo} */ diff --git a/website/docs/en/config/lib/dts.mdx b/website/docs/en/config/lib/dts.mdx index 8a889f2e9..88516411c 100644 --- a/website/docs/en/config/lib/dts.mdx +++ b/website/docs/en/config/lib/dts.mdx @@ -290,7 +290,7 @@ export default { 3. In order to ensure the consistency of local development, you need to install the corresponding [VS Code Preview Extension](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.native-preview) and add the following configuration in the VS Code settings: -```json +```json title=".vscode/settings.json" { "typescript.experimental.useTsgo": true } diff --git a/website/docs/en/guide/advanced/dts.mdx b/website/docs/en/guide/advanced/dts.mdx index 34ee984a9..11755389b 100644 --- a/website/docs/en/guide/advanced/dts.mdx +++ b/website/docs/en/guide/advanced/dts.mdx @@ -125,7 +125,7 @@ export default { 3. In order to ensure the consistency of local development, you need to install the corresponding [VS Code Preview Extension](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.native-preview) and add the following configuration in the VS Code settings: -```json +```json title=".vscode/settings.json" { "typescript.experimental.useTsgo": true } diff --git a/website/docs/zh/config/lib/dts.mdx b/website/docs/zh/config/lib/dts.mdx index cba0d4186..2f9757f9c 100644 --- a/website/docs/zh/config/lib/dts.mdx +++ b/website/docs/zh/config/lib/dts.mdx @@ -290,7 +290,7 @@ export default { 3. 为了保证本地开发的一致性,你需要安装对应的 [VS Code 预览版扩展](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.native-preview),并在 VS Code 设置中添加如下配置: -```json +```json title=".vscode/settings.json" { "typescript.experimental.useTsgo": true } diff --git a/website/docs/zh/guide/advanced/dts.mdx b/website/docs/zh/guide/advanced/dts.mdx index ca2bd38d4..cd7fd9ca6 100644 --- a/website/docs/zh/guide/advanced/dts.mdx +++ b/website/docs/zh/guide/advanced/dts.mdx @@ -125,7 +125,7 @@ export default { 3. 为了保证本地开发的一致性,你需要安装对应的 [VS Code 预览版扩展](https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.native-preview),并在 VS Code 设置中添加如下配置: -```json +```json title=".vscode/settings.json" { "typescript.experimental.useTsgo": true }