Skip to content
Closed
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
1 change: 1 addition & 0 deletions packages/cli/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/artifacts
/LICENSE
/skills/vite-plus/docs
/test
40 changes: 38 additions & 2 deletions packages/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import { execSync } from 'node:child_process';
import { existsSync, globSync, readFileSync, readdirSync, statSync } from 'node:fs';
import { copyFile, mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { basename, dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';

Expand Down Expand Up @@ -391,6 +391,11 @@ async function syncTestPackageExports() {
const testPkgPath = join(projectDir, '../test/package.json');
const cliPkgPath = join(projectDir, 'package.json');
const testDistDir = join(projectDir, 'dist/test');
// Proxy files for TypeScript compilerOptions.types resolution.
// TypeScript resolves "vite-plus/test/globals" via the filesystem directly
// (without consulting package.json exports), so we need actual files at
// test/globals.d.ts that TypeScript can find for type-only exports.
const testTypesDir = join(projectDir, 'test');

// Read test package.json
const testPkg = JSON.parse(await readFile(testPkgPath, 'utf-8'));
Expand All @@ -399,6 +404,8 @@ async function syncTestPackageExports() {
// Clean up previous build
await rm(testDistDir, { recursive: true, force: true });
await mkdir(testDistDir, { recursive: true });
await rm(testTypesDir, { recursive: true, force: true });
await mkdir(testTypesDir, { recursive: true });

const generatedExports: Record<string, unknown> = {};

Expand All @@ -416,6 +423,24 @@ async function syncTestPackageExports() {
if (shimExport) {
generatedExports[cliExportPath] = shimExport;
console.log(` Created ${cliExportPath}`);

// For type-only exports, also create a proxy file at test/{name}.d.ts so
// TypeScript can resolve "vite-plus/test/globals" via compilerOptions.types.
// compilerOptions.types uses filesystem resolution (not exports field), so
// it looks for node_modules/vite-plus/test/globals.d.ts directly.
if (isTypeOnlyExport(shimExport)) {
const testImportSpecifier =
exportPath === '.' ? TEST_PACKAGE_NAME : `${TEST_PACKAGE_NAME}${exportPath.slice(1)}`;
const shimBaseName = exportPath === '.' ? 'index' : exportPath.slice(2);
const proxyRelDir = dirname(shimBaseName);
const proxyDir = proxyRelDir === '.' ? testTypesDir : join(testTypesDir, proxyRelDir);
await mkdir(proxyDir, { recursive: true });
const baseFileName = basename(shimBaseName);
await writeFile(
join(proxyDir, `${baseFileName}.d.ts`),
`/// <reference types="${testImportSpecifier}" />\n`,
);
}
}
}

Expand All @@ -425,6 +450,14 @@ async function syncTestPackageExports() {
console.log(`\nSynced ${Object.keys(generatedExports).length} exports from test package`);
}

function isTypeOnlyExport(exportValue: ExportValue): boolean {
if (typeof exportValue === 'string') {
return false;
}
const keys = Object.keys(exportValue);
return keys.length === 1 && keys[0] === 'types';
}

/**
* Copy markdown doc files from the monorepo docs/ directory into skills/vite-plus/docs/,
* preserving the relative directory structure. This keeps stable file paths for
Expand Down Expand Up @@ -713,10 +746,13 @@ async function updateCliPackageJson(pkgPath: string, generatedExports: Record<st
...generatedExports,
};

// Ensure dist/test is included in files
// Ensure dist/test and test are included in files
if (!pkg.files.includes('dist/test')) {
pkg.files.push('dist/test');
}
if (!pkg.files.includes('test')) {
pkg.files.push('test');
}

const { code, errors } = await format(pkgPath, JSON.stringify(pkg, null, 2) + '\n', {
sortPackageJson: true,
Expand Down
Loading