From ddf3ec6139d9859075c62e058cddfb49845325ae Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 10 Jul 2025 21:23:55 +0900 Subject: [PATCH 1/4] build: use tsdown for plugin-react-swc --- packages/plugin-react-swc/package.json | 7 +- packages/plugin-react-swc/scripts/bundle.ts | 91 -------------------- packages/plugin-react-swc/tsconfig.json | 34 ++------ packages/plugin-react-swc/tsconfig.src.json | 27 ++++++ packages/plugin-react-swc/tsconfig.test.json | 25 ++++++ packages/plugin-react-swc/tsdown.config.ts | 57 ++++++++++++ pnpm-lock.yaml | 31 ++----- 7 files changed, 125 insertions(+), 147 deletions(-) delete mode 100644 packages/plugin-react-swc/scripts/bundle.ts create mode 100644 packages/plugin-react-swc/tsconfig.src.json create mode 100644 packages/plugin-react-swc/tsconfig.test.json create mode 100644 packages/plugin-react-swc/tsdown.config.ts diff --git a/packages/plugin-react-swc/package.json b/packages/plugin-react-swc/package.json index ae5c286e3..1dcdc32b3 100644 --- a/packages/plugin-react-swc/package.json +++ b/packages/plugin-react-swc/package.json @@ -15,8 +15,8 @@ "type": "module", "private": true, "scripts": { - "dev": "tsx scripts/bundle.ts --dev", - "build": "tsx scripts/bundle.ts", + "dev": "tsdown --watch", + "build": "tsdown", "test": "playwright test" }, "repository": { @@ -40,10 +40,9 @@ "@types/fs-extra": "^11.0.4", "@types/node": "^22.16.0", "@vitejs/react-common": "workspace:*", - "esbuild": "^0.25.5", "fs-extra": "^11.3.0", - "picocolors": "^1.1.1", "prettier": "^3.0.3", + "tsdown": "^0.12.9", "typescript": "^5.8.3" } } diff --git a/packages/plugin-react-swc/scripts/bundle.ts b/packages/plugin-react-swc/scripts/bundle.ts deleted file mode 100644 index dd3c63707..000000000 --- a/packages/plugin-react-swc/scripts/bundle.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { copyFileSync, rmSync, writeFileSync } from 'node:fs' -import { execSync } from 'node:child_process' -import { type BuildOptions, build, context } from 'esbuild' - -import packageJSON from '../package.json' - -const dev = process.argv.includes('--dev') - -rmSync('dist', { force: true, recursive: true }) - -const serverOptions: BuildOptions = { - bundle: true, - platform: 'node', - target: 'node14', - legalComments: 'inline', - external: Object.keys(packageJSON.peerDependencies).concat( - Object.keys(packageJSON.dependencies), - ), -} - -const buildOrWatch = async (options: BuildOptions) => { - if (!dev) return build(options) - const ctx = await context(options) - await ctx.watch() - await ctx.rebuild() -} - -Promise.all([ - buildOrWatch({ - entryPoints: ['@vitejs/react-common/refresh-runtime'], - outdir: 'dist', - platform: 'browser', - format: 'esm', - target: 'safari13', - legalComments: 'inline', - }), - buildOrWatch({ - ...serverOptions, - stdin: { - contents: `import react from "./src"; -module.exports = react; -// For backward compatibility with the first broken version -module.exports.default = react;`, - resolveDir: '.', - }, - outfile: 'dist/index.cjs', - logOverride: { 'empty-import-meta': 'silent' }, - }), - buildOrWatch({ - ...serverOptions, - entryPoints: ['src/index.ts'], - format: 'esm', - outfile: 'dist/index.mjs', - }), -]).then(() => { - copyFileSync('LICENSE', 'dist/LICENSE') - copyFileSync('README.md', 'dist/README.md') - - execSync( - 'tsc src/index.ts --declaration --isolatedDeclarations --noCheck --emitDeclarationOnly --outDir dist --target es2020 --module es2020 --moduleResolution bundler', - { stdio: 'inherit' }, - ) - - writeFileSync( - 'dist/package.json', - JSON.stringify( - { - ...Object.fromEntries( - Object.entries(packageJSON).filter( - ([key, _val]) => - key !== 'devDependencies' && - key !== 'scripts' && - key !== 'private', - ), - ), - main: 'index.cjs', - types: 'index.d.ts', - module: 'index.mjs', - exports: { - '.': { - types: './index.d.ts', - require: './index.cjs', - import: './index.mjs', - }, - }, - }, - null, - 2, - ), - ) -}) diff --git a/packages/plugin-react-swc/tsconfig.json b/packages/plugin-react-swc/tsconfig.json index fb429cf57..ac687cd46 100644 --- a/packages/plugin-react-swc/tsconfig.json +++ b/packages/plugin-react-swc/tsconfig.json @@ -1,31 +1,7 @@ { - "include": [ - "src", - "scripts", - "playwright.config.ts", - "playground/utils.ts", - "playground/*/__tests__" - ], - "compilerOptions": { - /* Target node 22 */ - "module": "ESNext", - "lib": ["ES2023", "DOM"], - "target": "ES2023", - "skipLibCheck": true, - - /* Bundler mode */ - "moduleResolution": "bundler", - "allowImportingTsExtensions": true, - "verbatimModuleSyntax": true, - "noEmit": true, - - /* Linting */ - "strict": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noFallthroughCasesInSwitch": true, - "useUnknownInCatchVariables": true, - "noUncheckedSideEffectImports": true, - "noPropertyAccessFromIndexSignature": true - } + "include": [], + "references": [ + { "path": "./tsconfig.src.json" }, + { "path": "./tsconfig.test.json" } + ] } diff --git a/packages/plugin-react-swc/tsconfig.src.json b/packages/plugin-react-swc/tsconfig.src.json new file mode 100644 index 000000000..4194367e6 --- /dev/null +++ b/packages/plugin-react-swc/tsconfig.src.json @@ -0,0 +1,27 @@ +{ + "include": ["src"], + "compilerOptions": { + /* Target node 22 */ + "module": "ESNext", + "lib": ["ES2023", "DOM"], + "target": "ES2023", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + "declaration": true, + "isolatedDeclarations": true, + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "useUnknownInCatchVariables": true, + "noUncheckedSideEffectImports": true, + "noPropertyAccessFromIndexSignature": true + } +} diff --git a/packages/plugin-react-swc/tsconfig.test.json b/packages/plugin-react-swc/tsconfig.test.json new file mode 100644 index 000000000..8bc9cb430 --- /dev/null +++ b/packages/plugin-react-swc/tsconfig.test.json @@ -0,0 +1,25 @@ +{ + "include": ["playwright.config.ts", "playground"], + "compilerOptions": { + /* Target node 22 */ + "module": "ESNext", + "lib": ["ES2023", "DOM"], + "target": "ES2023", + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true, + "useUnknownInCatchVariables": true, + "noUncheckedSideEffectImports": true, + "noPropertyAccessFromIndexSignature": true + } +} diff --git a/packages/plugin-react-swc/tsdown.config.ts b/packages/plugin-react-swc/tsdown.config.ts new file mode 100644 index 000000000..3301e534f --- /dev/null +++ b/packages/plugin-react-swc/tsdown.config.ts @@ -0,0 +1,57 @@ +import { writeFileSync } from 'node:fs' +import { defineConfig } from 'tsdown' +import packageJSON from './package.json' + +export default defineConfig({ + entry: 'src/index.ts', + format: ['esm', 'cjs'], + dts: true, + copy: [ + { + from: 'node_modules/@vitejs/react-common/refresh-runtime.js', + to: 'dist/refresh-runtime.js', + }, + { + from: 'LICENSE', + to: 'dist/LICENSE', + }, + { + from: 'README.md', + to: 'dist/README.md', + }, + ], + outputOptions(outputOpts, format) { + if (format === 'cjs') { + outputOpts.footer = 'module.exports.default = module.exports' + } + return outputOpts + }, + onSuccess() { + writeFileSync( + 'dist/package.json', + JSON.stringify( + { + ...Object.fromEntries( + Object.entries(packageJSON).filter( + ([key, _val]) => + key !== 'devDependencies' && + key !== 'scripts' && + key !== 'private', + ), + ), + main: 'index.cjs', + types: 'index.d.ts', + module: 'index.js', + exports: { + '.': { + require: './index.cjs', + import: './index.js', + }, + }, + }, + null, + 2, + ), + ) + }, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 19c01e53d..cfda9727f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -157,18 +157,15 @@ importers: '@vitejs/react-common': specifier: workspace:* version: link:../common - esbuild: - specifier: ^0.25.5 - version: 0.25.5 fs-extra: specifier: ^11.3.0 version: 11.3.0 - picocolors: - specifier: ^1.1.1 - version: 1.1.1 prettier: specifier: ^3.0.3 version: 3.1.0 + tsdown: + specifier: ^0.12.9 + version: 0.12.9(publint@0.3.12)(typescript@5.8.3) typescript: specifier: ^5.8.3 version: 5.8.3 @@ -3725,14 +3722,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 +7619,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 +8354,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 +9728,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 +10088,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: {} From 8fc5acca76abbe032536250bb3b5d93fb56e706e Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 10 Jul 2025 21:29:30 +0900 Subject: [PATCH 2/4] chore: add workaround --- packages/plugin-react-swc/tsdown.config.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/plugin-react-swc/tsdown.config.ts b/packages/plugin-react-swc/tsdown.config.ts index 3301e534f..8131c929e 100644 --- a/packages/plugin-react-swc/tsdown.config.ts +++ b/packages/plugin-react-swc/tsdown.config.ts @@ -6,6 +6,7 @@ export default defineConfig({ entry: 'src/index.ts', format: ['esm', 'cjs'], dts: true, + tsconfig: './tsconfig.src.json', // https://github.com/sxzz/rolldown-plugin-dts/issues/55 copy: [ { from: 'node_modules/@vitejs/react-common/refresh-runtime.js', From 22f02f2587c24f2f749c2a87c822329cb2a211d6 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 10 Jul 2025 21:33:47 +0900 Subject: [PATCH 3/4] chore: dont append compat code to dts files --- packages/plugin-react-swc/tsdown.config.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/plugin-react-swc/tsdown.config.ts b/packages/plugin-react-swc/tsdown.config.ts index 8131c929e..21e059885 100644 --- a/packages/plugin-react-swc/tsdown.config.ts +++ b/packages/plugin-react-swc/tsdown.config.ts @@ -23,7 +23,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 ca97a985fd570260096125e67687dca63bd27eae Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 10 Jul 2025 21:34:10 +0900 Subject: [PATCH 4/4] chore: add type attributes --- packages/plugin-react-swc/tsdown.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-react-swc/tsdown.config.ts b/packages/plugin-react-swc/tsdown.config.ts index 21e059885..8220e365c 100644 --- a/packages/plugin-react-swc/tsdown.config.ts +++ b/packages/plugin-react-swc/tsdown.config.ts @@ -1,6 +1,6 @@ import { writeFileSync } from 'node:fs' import { defineConfig } from 'tsdown' -import packageJSON from './package.json' +import packageJSON from './package.json' with { type: 'json' } export default defineConfig({ entry: 'src/index.ts',