From c022ae782cb0364b9f938f03a863a79f2e45f6df Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 10 Jul 2025 21:09:53 +0900 Subject: [PATCH 1/3] build: use tsdown for plugin-react / plugin-react-oxc --- packages/plugin-react-oxc/build.config.ts | 14 ----- packages/plugin-react-oxc/package.json | 9 +-- .../scripts/copyRefreshRuntime.ts | 6 -- packages/plugin-react-oxc/tsconfig.json | 2 +- packages/plugin-react-oxc/tsdown.config.ts | 14 +++++ packages/plugin-react/build.config.ts | 15 ----- packages/plugin-react/package.json | 13 ++--- .../scripts/copyRefreshRuntime.ts | 6 -- packages/plugin-react/tsconfig.json | 2 +- packages/plugin-react/tsdown.config.ts | 21 +++++++ pnpm-lock.yaml | 31 ++++------ scripts/patchCJS.ts | 57 ------------------- 12 files changed, 59 insertions(+), 131 deletions(-) delete mode 100644 packages/plugin-react-oxc/build.config.ts delete mode 100644 packages/plugin-react-oxc/scripts/copyRefreshRuntime.ts create mode 100644 packages/plugin-react-oxc/tsdown.config.ts delete mode 100644 packages/plugin-react/build.config.ts delete mode 100644 packages/plugin-react/scripts/copyRefreshRuntime.ts create mode 100644 packages/plugin-react/tsdown.config.ts delete mode 100644 scripts/patchCJS.ts diff --git a/packages/plugin-react-oxc/build.config.ts b/packages/plugin-react-oxc/build.config.ts deleted file mode 100644 index 68131222c..000000000 --- a/packages/plugin-react-oxc/build.config.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { defineBuildConfig } from 'unbuild' - -export default defineBuildConfig({ - entries: ['src/index'], - externals: ['vite'], - clean: true, - declaration: true, - rollup: { - inlineDependencies: true, - }, - replace: { - 'globalThis.__IS_BUILD__': 'true', - }, -}) diff --git a/packages/plugin-react-oxc/package.json b/packages/plugin-react-oxc/package.json index de0af4a01..cc860a81a 100644 --- a/packages/plugin-react-oxc/package.json +++ b/packages/plugin-react-oxc/package.json @@ -20,11 +20,11 @@ "dist" ], "type": "module", - "types": "./dist/index.d.mts", - "exports": "./dist/index.mjs", + "types": "./dist/index.d.ts", + "exports": "./dist/index.js", "scripts": { - "dev": "unbuild --stub", - "build": "unbuild && tsx scripts/copyRefreshRuntime.ts", + "dev": "tsdown --watch", + "build": "tsdown", "prepublishOnly": "npm run build" }, "engines": { @@ -44,6 +44,7 @@ }, "devDependencies": { "@vitejs/react-common": "workspace:*", + "tsdown": "^0.12.9", "unbuild": "^3.5.0", "vite": "catalog:rolldown-vite" }, diff --git a/packages/plugin-react-oxc/scripts/copyRefreshRuntime.ts b/packages/plugin-react-oxc/scripts/copyRefreshRuntime.ts deleted file mode 100644 index 2666e968e..000000000 --- a/packages/plugin-react-oxc/scripts/copyRefreshRuntime.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { copyFileSync } from 'node:fs' - -copyFileSync( - 'node_modules/@vitejs/react-common/refresh-runtime.js', - 'dist/refresh-runtime.js', -) diff --git a/packages/plugin-react-oxc/tsconfig.json b/packages/plugin-react-oxc/tsconfig.json index e2b17f9c7..1d2f67860 100644 --- a/packages/plugin-react-oxc/tsconfig.json +++ b/packages/plugin-react-oxc/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["src", "scripts"], + "include": ["src"], "compilerOptions": { "outDir": "dist", "target": "ES2020", diff --git a/packages/plugin-react-oxc/tsdown.config.ts b/packages/plugin-react-oxc/tsdown.config.ts new file mode 100644 index 000000000..e12ac845d --- /dev/null +++ b/packages/plugin-react-oxc/tsdown.config.ts @@ -0,0 +1,14 @@ +import { defineConfig } from 'tsdown' + +export default defineConfig({ + entry: 'src/index.ts', + define: { + 'globalThis.__IS_BUILD__': 'true', + }, + copy: [ + { + from: 'node_modules/@vitejs/react-common/refresh-runtime.js', + to: 'dist/refresh-runtime.js', + }, + ], +}) diff --git a/packages/plugin-react/build.config.ts b/packages/plugin-react/build.config.ts deleted file mode 100644 index 98285c8d0..000000000 --- a/packages/plugin-react/build.config.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { defineBuildConfig } from 'unbuild' - -export default defineBuildConfig({ - entries: ['src/index'], - externals: ['vite'], - clean: true, - declaration: true, - rollup: { - emitCJS: true, - inlineDependencies: true, - }, - replace: { - 'globalThis.__IS_BUILD__': 'true', - }, -}) diff --git a/packages/plugin-react/package.json b/packages/plugin-react/package.json index 53cf9b1ba..8a222df8f 100644 --- a/packages/plugin-react/package.json +++ b/packages/plugin-react/package.json @@ -21,18 +21,17 @@ ], "type": "module", "main": "./dist/index.cjs", - "module": "./dist/index.mjs", - "types": "./dist/index.d.mts", + "module": "./dist/index.js", + "types": "./dist/index.d.ts", "exports": { ".": { - "import": "./dist/index.mjs", + "import": "./dist/index.js", "require": "./dist/index.cjs" } }, "scripts": { - "dev": "unbuild --stub", - "build": "unbuild && pnpm run patch-cjs && tsx scripts/copyRefreshRuntime.ts", - "patch-cjs": "tsx ../../scripts/patchCJS.ts", + "dev": "tsdown --watch", + "build": "tsdown", "prepublishOnly": "npm run build", "test-unit": "vitest run" }, @@ -65,7 +64,7 @@ "react": "^19.1.0", "react-dom": "^19.1.0", "rolldown": "1.0.0-beta.24", - "unbuild": "^3.5.0", + "tsdown": "^0.12.9", "vitest": "^3.2.4" } } diff --git a/packages/plugin-react/scripts/copyRefreshRuntime.ts b/packages/plugin-react/scripts/copyRefreshRuntime.ts deleted file mode 100644 index 2666e968e..000000000 --- a/packages/plugin-react/scripts/copyRefreshRuntime.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { copyFileSync } from 'node:fs' - -copyFileSync( - 'node_modules/@vitejs/react-common/refresh-runtime.js', - 'dist/refresh-runtime.js', -) diff --git a/packages/plugin-react/tsconfig.json b/packages/plugin-react/tsconfig.json index e2b17f9c7..1d2f67860 100644 --- a/packages/plugin-react/tsconfig.json +++ b/packages/plugin-react/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["src", "scripts"], + "include": ["src"], "compilerOptions": { "outDir": "dist", "target": "ES2020", diff --git a/packages/plugin-react/tsdown.config.ts b/packages/plugin-react/tsdown.config.ts new file mode 100644 index 000000000..94108a635 --- /dev/null +++ b/packages/plugin-react/tsdown.config.ts @@ -0,0 +1,21 @@ +import { defineConfig } from 'tsdown' + +export default defineConfig({ + entry: 'src/index.ts', + format: ['esm', 'cjs'], + define: { + 'globalThis.__IS_BUILD__': 'true', + }, + copy: [ + { + from: 'node_modules/@vitejs/react-common/refresh-runtime.js', + to: 'dist/refresh-runtime.js', + }, + ], + outputOptions(outputOpts, format) { + if (format === 'cjs') { + outputOpts.footer = 'module.exports.default = module.exports' + } + return outputOpts + }, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 19c01e53d..77355bab9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -113,9 +113,9 @@ importers: rolldown: specifier: 1.0.0-beta.24 version: 1.0.0-beta.24 - unbuild: - specifier: ^3.5.0 - version: 3.5.0(typescript@5.8.3) + tsdown: + specifier: ^0.12.9 + version: 0.12.9(publint@0.3.12)(typescript@5.8.3) vitest: specifier: ^3.2.4 version: 3.2.4(@types/debug@4.1.12)(@types/node@22.16.0)(jiti@2.4.2)(lightningcss@1.30.1)(tsx@4.20.3)(yaml@2.7.1) @@ -129,6 +129,9 @@ importers: '@vitejs/react-common': specifier: workspace:* version: link:../common + tsdown: + specifier: ^0.12.9 + version: 0.12.9(publint@0.3.12)(typescript@5.8.3) unbuild: specifier: ^3.5.0 version: 3.5.0(typescript@5.8.3) @@ -3725,14 +3728,6 @@ packages: fd-package-json@1.2.0: resolution: {integrity: sha512-45LSPmWf+gC5tdCQMNH4s9Sr00bIkiD9aN7dc5hqkrEw1geRYyDQS1v1oMHAW3ysfxfndqGsrDREHHjNNbKUfA==} - fdir@6.4.4: - resolution: {integrity: sha512-1NZP+GK4GfuAv3PqKvxQRDMjdSRZjnkq7KfhlNrCNNlZ0ygQFpebfrnfnq/W7fpUnAv9aGWmY1zKx7FYL3gwhg==} - peerDependencies: - picomatch: ^3 || ^4 - peerDependenciesMeta: - picomatch: - optional: true - fdir@6.4.6: resolution: {integrity: sha512-hiFoqpyZcfNm1yc4u8oWCf9A2c4D3QjCrks3zmoVKVxpQRzmPNar1hUJcBG2RQHvEVGDN+Jm81ZheVLAQMK6+w==} peerDependencies: @@ -7630,7 +7625,7 @@ snapshots: ast-kit@2.1.1: dependencies: - '@babel/parser': 7.27.7 + '@babel/parser': 7.28.0 pathe: 2.0.3 astring@1.8.6: {} @@ -8365,10 +8360,6 @@ snapshots: dependencies: walk-up-path: 3.0.1 - fdir@6.4.4(picomatch@4.0.2): - optionalDependencies: - picomatch: 4.0.2 - fdir@6.4.6(picomatch@4.0.2): optionalDependencies: picomatch: 4.0.2 @@ -9743,9 +9734,9 @@ snapshots: rolldown-plugin-dts@0.13.13(rolldown@1.0.0-beta.24)(typescript@5.8.3): dependencies: - '@babel/generator': 7.27.5 - '@babel/parser': 7.27.7 - '@babel/types': 7.27.7 + '@babel/generator': 7.28.0 + '@babel/parser': 7.28.0 + '@babel/types': 7.28.0 ast-kit: 2.1.1 birpc: 2.4.0 debug: 4.4.1 @@ -10103,7 +10094,7 @@ snapshots: tinyglobby@0.2.14: dependencies: - fdir: 6.4.4(picomatch@4.0.2) + fdir: 6.4.6(picomatch@4.0.2) picomatch: 4.0.2 tinypool@1.1.1: {} diff --git a/scripts/patchCJS.ts b/scripts/patchCJS.ts deleted file mode 100644 index 192264e6a..000000000 --- a/scripts/patchCJS.ts +++ /dev/null @@ -1,57 +0,0 @@ -/** - -It converts - -```ts -exports.default = vuePlugin; -exports.parseVueRequest = parseVueRequest; -``` - -to - -```ts -module.exports = vuePlugin; -module.exports.default = vuePlugin; -module.exports.parseVueRequest = parseVueRequest; -``` -*/ - -import { readFileSync, writeFileSync } from 'node:fs' -import colors from 'picocolors' - -const indexPath = 'dist/index.cjs' -let code = readFileSync(indexPath, 'utf-8') - -const matchMixed = code.match(/\nexports.default = (\w+);/) -if (matchMixed) { - const name = matchMixed[1] - - const lines = code.trimEnd().split('\n') - - // search from the end to prepend `modules.` to `export[xxx]` - for (let i = lines.length - 1; i > 0; i--) { - if (lines[i].startsWith('exports')) lines[i] = 'module.' + lines[i] - else { - // at the beginning of exports, export the default function - lines[i] += `\nmodule.exports = ${name};` - break - } - } - - writeFileSync(indexPath, lines.join('\n')) - - console.log(colors.bold(`${indexPath} CJS patched`)) - process.exit(0) -} - -const matchDefault = code.match(/\nmodule.exports = (\w+);/) - -if (matchDefault) { - code += `module.exports.default = ${matchDefault[1]};\n` - writeFileSync(indexPath, code) - console.log(colors.bold(`${indexPath} CJS patched`)) - process.exit(0) -} - -console.error(colors.red(`${indexPath} CJS patch failed`)) -process.exit(1) From bafa0ad21be152b1becce814ef9c5ced8cba46fe Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 10 Jul 2025 21:49:06 +0900 Subject: [PATCH 2/3] chore: dont append compat code to dts files --- packages/plugin-react/tsdown.config.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/plugin-react/tsdown.config.ts b/packages/plugin-react/tsdown.config.ts index 94108a635..6f2b6dbe6 100644 --- a/packages/plugin-react/tsdown.config.ts +++ b/packages/plugin-react/tsdown.config.ts @@ -14,7 +14,13 @@ export default defineConfig({ ], outputOptions(outputOpts, format) { if (format === 'cjs') { - outputOpts.footer = 'module.exports.default = module.exports' + outputOpts.footer = (chunk) => { + // don't append to dts files + if (chunk.fileName.endsWith('.cjs')) { + return 'module.exports.default = module.exports' + } + return '' + } } return outputOpts }, From dd6f3a8fcfd990cd3619eb4a79b370d6d55a092e Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Fri, 11 Jul 2025 14:36:56 +0900 Subject: [PATCH 3/3] chore: remove `__IS_BUILD__` workaround --- packages/plugin-react-oxc/src/build.d.ts | 7 ------- packages/plugin-react-oxc/src/index.ts | 6 +----- packages/plugin-react-oxc/tsdown.config.ts | 3 --- packages/plugin-react/src/build.d.ts | 7 ------- packages/plugin-react/src/index.ts | 7 ++----- packages/plugin-react/tsdown.config.ts | 3 --- 6 files changed, 3 insertions(+), 30 deletions(-) delete mode 100644 packages/plugin-react-oxc/src/build.d.ts delete mode 100644 packages/plugin-react/src/build.d.ts diff --git a/packages/plugin-react-oxc/src/build.d.ts b/packages/plugin-react-oxc/src/build.d.ts deleted file mode 100644 index 262bd804b..000000000 --- a/packages/plugin-react-oxc/src/build.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -declare global { - /** replaced by unbuild only in build */ - // eslint-disable-next-line no-var --- top level var has to be var - var __IS_BUILD__: boolean | void -} - -export {} diff --git a/packages/plugin-react-oxc/src/index.ts b/packages/plugin-react-oxc/src/index.ts index 0d7fa5717..d54da0ded 100644 --- a/packages/plugin-react-oxc/src/index.ts +++ b/packages/plugin-react-oxc/src/index.ts @@ -12,11 +12,7 @@ import { import { exactRegex } from '@rolldown/pluginutils' const _dirname = dirname(fileURLToPath(import.meta.url)) - -const refreshRuntimePath = globalThis.__IS_BUILD__ - ? join(_dirname, 'refresh-runtime.js') - : // eslint-disable-next-line n/no-unsupported-features/node-builtins -- only used in dev - fileURLToPath(import.meta.resolve('@vitejs/react-common/refresh-runtime')) +const refreshRuntimePath = join(_dirname, 'refresh-runtime.js') export interface Options { include?: string | RegExp | Array diff --git a/packages/plugin-react-oxc/tsdown.config.ts b/packages/plugin-react-oxc/tsdown.config.ts index e12ac845d..1f9c7eb25 100644 --- a/packages/plugin-react-oxc/tsdown.config.ts +++ b/packages/plugin-react-oxc/tsdown.config.ts @@ -2,9 +2,6 @@ import { defineConfig } from 'tsdown' export default defineConfig({ entry: 'src/index.ts', - define: { - 'globalThis.__IS_BUILD__': 'true', - }, copy: [ { from: 'node_modules/@vitejs/react-common/refresh-runtime.js', diff --git a/packages/plugin-react/src/build.d.ts b/packages/plugin-react/src/build.d.ts deleted file mode 100644 index 262bd804b..000000000 --- a/packages/plugin-react/src/build.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -declare global { - /** replaced by unbuild only in build */ - // eslint-disable-next-line no-var --- top level var has to be var - var __IS_BUILD__: boolean | void -} - -export {} diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index ed5756531..f8dd135a4 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -19,10 +19,7 @@ import { } from '@rolldown/pluginutils' const _dirname = dirname(fileURLToPath(import.meta.url)) - -const refreshRuntimePath = globalThis.__IS_BUILD__ - ? join(_dirname, 'refresh-runtime.js') - : join(_dirname, '../../common/refresh-runtime.js') +const refreshRuntimePath = join(_dirname, 'refresh-runtime.js') // lazy load babel since it's not used during build if plugins are not used let babel: typeof babelCore | undefined @@ -351,7 +348,7 @@ export default function viteReact(opts: Options = {}): Plugin[] { jsxImportRuntime, ] const staticBabelPlugins = - typeof opts.babel === 'object' ? opts.babel?.plugins ?? [] : [] + typeof opts.babel === 'object' ? (opts.babel?.plugins ?? []) : [] const reactCompilerPlugin = getReactCompilerPlugin(staticBabelPlugins) if (reactCompilerPlugin != null) { const reactCompilerRuntimeModule = diff --git a/packages/plugin-react/tsdown.config.ts b/packages/plugin-react/tsdown.config.ts index 6f2b6dbe6..f816d7ae2 100644 --- a/packages/plugin-react/tsdown.config.ts +++ b/packages/plugin-react/tsdown.config.ts @@ -3,9 +3,6 @@ import { defineConfig } from 'tsdown' export default defineConfig({ entry: 'src/index.ts', format: ['esm', 'cjs'], - define: { - 'globalThis.__IS_BUILD__': 'true', - }, copy: [ { from: 'node_modules/@vitejs/react-common/refresh-runtime.js',