From 50218a5f74dd80ecfb9e62693818f0d7cb3cebdb Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Fri, 23 May 2025 11:29:55 +0900 Subject: [PATCH] chore: generate dts internally by rolldown-plugin-dts --- packages/vite/package.json | 5 ++- packages/vite/rollup.dts.config.ts | 53 +++++++++++++++++------------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/packages/vite/package.json b/packages/vite/package.json index 70992c8d4a0bb2..f15bdc27f83185 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -70,9 +70,8 @@ "dev": "premove dist && pnpm build-bundle -w", "build": "premove dist && pnpm build-bundle && pnpm build-types", "build-bundle": "rolldown --config rollup.config.ts", - "build-types": "pnpm build-types-temp && pnpm build-types-roll && pnpm build-types-check", - "build-types-temp": "tsc --emitDeclarationOnly --outDir temp -p src/node/tsconfig.build.json", - "build-types-roll": "rolldown --config rollup.dts.config.ts && premove temp", + "build-types": "pnpm build-types-roll && pnpm build-types-check", + "build-types-roll": "rolldown --config rollup.dts.config.ts", "build-types-check": "tsc --project tsconfig.check.json", "typecheck": "tsc --noEmit && tsc --noEmit -p src/node", "lint": "eslint --cache --ext .ts src/**", diff --git a/packages/vite/rollup.dts.config.ts b/packages/vite/rollup.dts.config.ts index b171048c324eca..a079e2418447ce 100644 --- a/packages/vite/rollup.dts.config.ts +++ b/packages/vite/rollup.dts.config.ts @@ -35,15 +35,21 @@ const external = [ export default defineConfig({ input: { - index: './temp/src/node/index.d.ts', - 'module-runner': './temp/src/module-runner/index.d.ts', + index: './src/node/index.ts', + 'module-runner': './src/module-runner/index.ts', }, output: { dir: './dist/node', format: 'esm', }, + treeshake: { + moduleSideEffects: 'no-external', + }, external, - plugins: [patchTypes(), dts({ dtsInput: true })], + plugins: [ + patchTypes(), + dts({ tsconfig: './src/node/tsconfig.build.json', emitDtsOnly: true }), + ], }) // Taken from https://stackoverflow.com/a/36328890 @@ -114,7 +120,7 @@ function patchTypes(): Plugin { resolveId: { order: 'pre', filter: { - id: /^(dep-)*types\//, + id: /^(dep-)?types\//, }, handler(id) { // Dep types should be bundled @@ -134,26 +140,29 @@ function patchTypes(): Plugin { } }, }, - generateBundle(_opts, bundle) { - for (const chunk of Object.values(bundle)) { - if (chunk.type !== 'chunk') continue + generateBundle: { + order: 'post', + handler(_opts, bundle) { + for (const chunk of Object.values(bundle)) { + if (chunk.type !== 'chunk') continue - const ast = parseAst(chunk.code, { lang: 'ts', sourceType: 'module' }) - const importBindings = getAllImportBindings(ast) - if ( - chunk.fileName.startsWith('module-runner') || - // index and moduleRunner have a common chunk "moduleRunnerTransport" - chunk.fileName.startsWith('moduleRunnerTransport') || - chunk.fileName.startsWith('types.d-') - ) { - validateRunnerChunk.call(this, chunk, importBindings) - } else { - validateChunkImports.call(this, chunk, importBindings) - replaceConfusingTypeNames.call(this, chunk, importBindings) - stripInternalTypes.call(this, chunk) - cleanUnnecessaryComments(chunk) + const ast = parseAst(chunk.code, { lang: 'ts', sourceType: 'module' }) + const importBindings = getAllImportBindings(ast) + if ( + chunk.fileName.startsWith('module-runner') || + // index and moduleRunner have a common chunk "moduleRunnerTransport" + chunk.fileName.startsWith('moduleRunnerTransport') || + chunk.fileName.startsWith('types.d-') + ) { + validateRunnerChunk.call(this, chunk, importBindings) + } else { + validateChunkImports.call(this, chunk, importBindings) + replaceConfusingTypeNames.call(this, chunk, importBindings) + stripInternalTypes.call(this, chunk) + cleanUnnecessaryComments(chunk) + } } - } + }, }, } }