Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions crates/vite_migration/src/import_rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ static RE_REF_TSDOWN: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"^(\s*///\s*<reference\s+types\s*=\s*["'])tsdown(["']\s*/>)"#).unwrap()
});

/// `tsdown/client` → `vite-plus/pack/client`
static RE_REF_TSDOWN_CLIENT: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"^(\s*///\s*<reference\s+types\s*=\s*["'])tsdown/client(["']\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.
Expand Down Expand Up @@ -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}") {
Comment thread
naokihaba marked this conversation as resolved.
changed = true;
continue;
}
if apply_regex_replace(line, &RE_REF_TSDOWN, "${1}vite-plus/pack${2}") {
changed = true;
}
}
}

Expand Down Expand Up @@ -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#"/// <reference types="tsdown/client" />"#;
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#"/// <reference types="vite-plus/pack/client" />"#);
}

#[test]
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ async function syncCorePackageExports() {
`/// <reference types="${CORE_PACKAGE_NAME}/client" />\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'),
`/// <reference types="${CORE_PACKAGE_NAME}/pack/client" />\n`,
);

// Create ./module-runner shim
console.log(' Creating ./module-runner');
await writeFile(
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
/// <reference types="vite-plus/test/globals" />
/// <reference types="vite-plus" />
/// <reference types="vite-plus/test/browser/context" />
/// <reference types="vite-plus/pack/client" />
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/// <reference types="vitest/globals" />
/// <reference types="vitest/config" />
/// <reference types="@vitest/browser/context" />
/// <reference types="tsdown/client" />
9 changes: 9 additions & 0 deletions packages/core/__tests__/build-artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Comment on lines +25 to +26
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some tests to handle file copying and validation based on tsdown/client.d.ts to make sure everything is working as expected.

https://github.com/rolldown/tsdown/blob/main/client.d.ts#L35-L58

});
});
4 changes: 4 additions & 0 deletions packages/core/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"default": "./dist/tsdown/index.js",
"types": "./dist/tsdown/index-types.d.ts"
},
"./pack/client": {
Comment thread
fengmk2 marked this conversation as resolved.
"types": "./dist/tsdown/client.d.ts"
},
"./package.json": "./package.json",
"./rolldown": {
"default": "./dist/rolldown/index.mjs",
Expand Down
Loading