diff --git a/crates/vite_migration/src/import_rewriter.rs b/crates/vite_migration/src/import_rewriter.rs index a191031ea4..9a017bf2d1 100644 --- a/crates/vite_migration/src/import_rewriter.rs +++ b/crates/vite_migration/src/import_rewriter.rs @@ -336,6 +336,11 @@ static RE_REF_TSDOWN: LazyLock = LazyLock::new(|| { Regex::new(r#"^(\s*///\s*)"#).unwrap() }); +/// `tsdown/client` → `vite-plus/pack/client` +static RE_REF_TSDOWN_CLIENT: LazyLock = LazyLock::new(|| { + Regex::new(r#"^(\s*///\s*)"#).unwrap() +}); + /// Apply a single regex replacement, updating `content` in place if matched. /// Uses `Cow::Owned` variant check to avoid O(n) string comparison on no-match. /// Uses `replace` (not `replace_all`) since each line contains at most one reference directive. @@ -495,10 +500,14 @@ fn rewrite_reference_types(content: &mut String, skip_packages: &SkipPackages) - continue; } } - if !skip_packages.skip_tsdown - && apply_regex_replace(line, &RE_REF_TSDOWN, "${1}vite-plus/pack${2}") - { - changed = true; + if !skip_packages.skip_tsdown { + if apply_regex_replace(line, &RE_REF_TSDOWN_CLIENT, "${1}vite-plus/pack/client${2}") { + changed = true; + continue; + } + if apply_regex_replace(line, &RE_REF_TSDOWN, "${1}vite-plus/pack${2}") { + changed = true; + } } } @@ -2463,12 +2472,12 @@ export default defineConfig({});"# } #[test] - fn test_rewrite_reference_types_tsdown_subpath_not_rewritten() { - // tsdown subpaths should NOT be rewritten because vite-plus only exports ./pack (no subpaths) + fn test_rewrite_reference_types_tsdown_client_rewritten() { + // tsdown/client should be rewritten to vite-plus/pack/client let content = r#"/// "#; let result = rewrite_import_content(content, &SkipPackages::default()).unwrap(); - assert!(!result.updated); - assert_eq!(result.content, content); + assert!(result.updated); + assert_eq!(result.content, r#"/// "#); } #[test] diff --git a/packages/cli/build.ts b/packages/cli/build.ts index c4be9c3e40..c8ca718e61 100644 --- a/packages/cli/build.ts +++ b/packages/cli/build.ts @@ -168,6 +168,13 @@ async function syncCorePackageExports() { `/// \n`, ); + // Create ./pack/client shim (types only) - ambient type declarations for tsdown bundler features + console.log(' Creating ./pack/client'); + await writeFile( + join(distDir, 'pack-client.d.ts'), + `/// \n`, + ); + // Create ./module-runner shim console.log(' Creating ./module-runner'); await writeFile( diff --git a/packages/cli/package.json b/packages/cli/package.json index 2416708991..3f1a35377d 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -79,6 +79,9 @@ "types": "./dist/pack.d.ts", "import": "./dist/pack.js" }, + "./pack/client": { + "types": "./dist/pack-client.d.ts" + }, "./versions": { "types": "./dist/versions.d.ts", "default": "./dist/versions.js" diff --git a/packages/cli/snap-tests-global/migration-rewrite-reference-types/snap.txt b/packages/cli/snap-tests-global/migration-rewrite-reference-types/snap.txt index adf957897a..bb8e5f381f 100644 --- a/packages/cli/snap-tests-global/migration-rewrite-reference-types/snap.txt +++ b/packages/cli/snap-tests-global/migration-rewrite-reference-types/snap.txt @@ -10,3 +10,4 @@ /// /// /// +/// diff --git a/packages/cli/snap-tests-global/migration-rewrite-reference-types/src/env.d.ts b/packages/cli/snap-tests-global/migration-rewrite-reference-types/src/env.d.ts index 9ea86e9ef9..25551609c9 100644 --- a/packages/cli/snap-tests-global/migration-rewrite-reference-types/src/env.d.ts +++ b/packages/cli/snap-tests-global/migration-rewrite-reference-types/src/env.d.ts @@ -4,3 +4,4 @@ /// /// /// +/// diff --git a/packages/core/__tests__/build-artifacts.spec.ts b/packages/core/__tests__/build-artifacts.spec.ts index 995ad5edb8..c917168f92 100644 --- a/packages/core/__tests__/build-artifacts.spec.ts +++ b/packages/core/__tests__/build-artifacts.spec.ts @@ -16,4 +16,13 @@ describe('build artifacts', () => { expect(content).toContain('__dirname'); expect(content).toContain('__filename'); }); + + it('should include tsdown client.d.ts in dist/tsdown for pack/client support', () => { + const clientPath = path.join(distDir, 'tsdown/client.d.ts'); + expect(fs.existsSync(clientPath), `${clientPath} should exist`).toBe(true); + + const content = fs.readFileSync(clientPath, 'utf8'); + expect(content).toContain('ImportMeta'); + expect(content).toContain('glob'); + }); }); diff --git a/packages/core/build.ts b/packages/core/build.ts index 15354cd6d9..3741f7029d 100644 --- a/packages/core/build.ts +++ b/packages/core/build.ts @@ -436,6 +436,10 @@ async function bundleTsdown() { // tsdown resolves this file via path.resolve(import.meta.dirname, '..', 'esm-shims.js'), // which means it expects the file at dist/esm-shims.js (one level up from dist/tsdown/). await copyFile(join(tsdownSourceDir, 'esm-shims.js'), join(projectDir, 'dist/esm-shims.js')); + + // Copy client.d.ts to dist/tsdown/ to expose it as the vite-plus/pack/client entry point, + // equivalent to tsdown/client for registering bundler type features with TypeScript. + await copyFile(join(tsdownSourceDir, 'client.d.ts'), join(projectDir, 'dist/tsdown/client.d.ts')); } async function brandTsdown() { diff --git a/packages/core/package.json b/packages/core/package.json index d6df374112..a211a9b8f6 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -46,6 +46,9 @@ "default": "./dist/tsdown/index.js", "types": "./dist/tsdown/index-types.d.ts" }, + "./pack/client": { + "types": "./dist/tsdown/client.d.ts" + }, "./package.json": "./package.json", "./rolldown": { "default": "./dist/rolldown/index.mjs",