Skip to content

Commit c59871a

Browse files
Copilotstipsan
andcommitted
Add React Compiler support and tsdown config options
- Add reactCompiler config option (boolean | {target: '18' | '19'}) - Add tsdown.tsgo option for enabling native TypeScript preview - Implement React Compiler using @rollup/plugin-babel per tsdown docs - Update all playground configs to use new reactCompiler option - Remove dts: 'rolldown' from all configs (tsdown handles DTS automatically) - Convert tsgo option to tsdown.tsgo in playground configs - Add @babel/preset-react dependency for JSX transformation Co-authored-by: stipsan <[email protected]>
1 parent 3cf5522 commit c59871a

File tree

18 files changed

+77
-54
lines changed

18 files changed

+77
-54
lines changed

packages/@sanity/pkg-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"dependencies": {
6868
"@babel/core": "^7.28.5",
6969
"@babel/parser": "^7.28.5",
70+
"@babel/preset-react": "^7.28.5",
7071
"@babel/preset-typescript": "^7.28.5",
7172
"@babel/types": "^7.28.5",
7273
"@microsoft/api-extractor": "^7.55.1",

packages/@sanity/pkg-utils/src/node/core/config/types.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type {PluginItem as BabelPluginItem} from '@babel/core'
2-
import type {PluginOptions as ReactCompilerOptions} from 'babel-plugin-react-compiler'
32
import type {StrictOptions} from '../../strict.ts'
43

54
/** @public */
@@ -70,8 +69,6 @@ export interface PkgConfigOptions {
7069
babel?: {
7170
plugins?: BabelPluginItem[] | null | undefined
7271
/** @alpha */
73-
reactCompiler?: boolean
74-
/** @alpha */
7572
styledComponents?:
7673
| boolean
7774
| {
@@ -98,10 +95,19 @@ export interface PkgConfigOptions {
9895
}
9996
}
10097
/**
101-
* Configure the React Compiler.
102-
* To enable it set `babel.reactCompiler` to `true`
103-
* @beta */
104-
reactCompilerOptions?: Partial<ReactCompilerOptions>
98+
* Enable React Compiler for automatic React optimizations.
99+
* Set to `true` to enable with defaults, or specify a target React version.
100+
* @alpha
101+
*/
102+
reactCompiler?:
103+
| boolean
104+
| {
105+
/**
106+
* Target React version for the compiler
107+
* @defaultValue '19'
108+
*/
109+
target?: '18' | '19'
110+
}
105111
bundles?: PkgBundle[]
106112
/** @alpha */
107113
define?: Record<string, string | number | boolean | undefined | null>
@@ -185,4 +191,15 @@ export interface PkgConfigOptions {
185191
* Configure what checks are made when running `--strict` builds and checks
186192
*/
187193
strictOptions?: Partial<StrictOptions>
194+
/**
195+
* Configure tsdown-specific options
196+
* @alpha
197+
*/
198+
tsdown?: {
199+
/**
200+
* Enable experimental tsgo for faster type generation using `@typescript/native-preview`
201+
* @alpha
202+
*/
203+
tsgo?: boolean
204+
}
188205
}

packages/@sanity/pkg-utils/src/node/tasks/tsdown/resolveTsdownConfig.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import type {BuildContext} from '../../core/contexts/buildContext.ts'
44
import type {TsdownTask, TsdownWatchTask} from '../types.ts'
55

66
/** @internal */
7-
export function resolveTsdownConfig(
7+
export async function resolveTsdownConfig(
88
ctx: BuildContext,
99
buildTask: TsdownTask | TsdownWatchTask,
10-
): TsdownOptions {
10+
): Promise<TsdownOptions> {
1111
const {format, runtime, target} = buildTask
1212
const {config, cwd, exports: _exports, external, distPath, pkg, ts} = ctx
1313
const minify = config?.minify ?? false
@@ -44,16 +44,36 @@ export function resolveTsdownConfig(
4444
// Determine platform based on runtime
4545
const platform = runtime === 'browser' ? 'browser' : runtime === 'node' ? 'node' : 'neutral'
4646

47-
// Warn if React Compiler or Styled Components are enabled - not yet supported with tsdown
48-
if (config?.babel?.reactCompiler) {
49-
ctx.logger.warn(
50-
'React Compiler is enabled but not yet fully supported with tsdown. Consider using @rollup/plugin-babel separately.',
51-
)
52-
}
53-
54-
if (config?.babel?.styledComponents) {
55-
ctx.logger.warn(
56-
'Styled Components is enabled but not yet fully supported with tsdown. Consider using babel-plugin-styled-components separately.',
47+
// Configure Babel plugin for React Compiler if enabled
48+
const plugins: any[] = []
49+
50+
if (config?.reactCompiler) {
51+
const reactCompilerTarget =
52+
typeof config.reactCompiler === 'object' ? config.reactCompiler.target || '19' : '19'
53+
54+
// Use @rollup/plugin-babel for React Compiler as recommended by tsdown
55+
const {babel} = await import('@rollup/plugin-babel')
56+
plugins.push(
57+
babel({
58+
babelHelpers: 'bundled',
59+
presets: [
60+
['@babel/preset-react', {runtime: 'automatic'}],
61+
'@babel/preset-typescript',
62+
],
63+
parserOpts: {
64+
sourceType: 'module',
65+
plugins: ['jsx', 'typescript'],
66+
},
67+
plugins: [
68+
[
69+
'babel-plugin-react-compiler',
70+
{
71+
target: reactCompilerTarget,
72+
},
73+
],
74+
],
75+
extensions: ['.js', '.jsx', '.ts', '.tsx'],
76+
}),
5777
)
5878
}
5979

@@ -105,6 +125,8 @@ export function resolveTsdownConfig(
105125
silent: true, // We handle logging ourselves
106126
// Don't use fixed extensions - let tsdown use package type to determine extension
107127
fixedExtension: false,
128+
// Add babel plugin for React Compiler if configured
129+
plugins: plugins.length > 0 ? plugins : undefined,
108130
}
109131

110132
return tsdownOptions

packages/@sanity/pkg-utils/src/node/tasks/tsdown/tsdownTask.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async function execPromise(ctx: BuildContext, task: TsdownTask) {
9999
})
100100

101101
try {
102-
const options = resolveTsdownConfig(ctx, task)
102+
const options = await resolveTsdownConfig(ctx, task)
103103

104104
// Build with tsdown
105105
await build(options)

packages/@sanity/pkg-utils/src/node/tasks/tsdown/tsdownWatchTask.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async function execWatch(
109109
})
110110

111111
try {
112-
const options = resolveTsdownConfig(ctx, task)
112+
const options = await resolveTsdownConfig(ctx, task)
113113

114114
// Enable watch mode
115115
const watchOptions = {

playground/react-18/package.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ import {defineConfig} from '@sanity/pkg-utils'
22

33
export default defineConfig({
44
tsconfig: 'tsconfig.dist.json',
5-
babel: {reactCompiler: true},
6-
reactCompilerOptions: {target: '18'},
5+
reactCompiler: {target: '18'},
76
})

playground/react-19/package.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ import {defineConfig} from '@sanity/pkg-utils'
22

33
export default defineConfig({
44
tsconfig: 'tsconfig.dist.json',
5-
babel: {reactCompiler: true},
6-
reactCompilerOptions: {target: '19'},
5+
reactCompiler: {target: '19'},
76
})

playground/sanity-plugin-with-styled-components/package.config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {defineConfig} from '@sanity/pkg-utils'
22

33
export default defineConfig({
44
tsconfig: 'tsconfig.dist.json',
5-
dts: 'rolldown',
6-
babel: {reactCompiler: true, styledComponents: true},
7-
reactCompilerOptions: {target: '18'},
5+
reactCompiler: {target: '18'},
6+
babel: {styledComponents: true},
87
})

playground/sanity-plugin-with-vanilla-extract/package.config.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,5 @@ import {defineConfig} from '@sanity/pkg-utils'
22

33
export default defineConfig({
44
tsconfig: 'tsconfig.dist.json',
5-
dts: 'rolldown',
6-
babel: {reactCompiler: true},
7-
reactCompilerOptions: {target: '19'},
8-
rollup: {
9-
output: {
10-
intro: (chunkInfo) => {
11-
if (!chunkInfo.isEntry) {
12-
return ''
13-
}
14-
return `import './bundle.css'`
15-
},
16-
},
17-
vanillaExtract: true,
18-
},
5+
reactCompiler: {target: '19'},
196
})

playground/ts-rolldown-bundle-dev-dependency/package.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ import {defineConfig} from '@sanity/pkg-utils'
22

33
export default defineConfig({
44
tsconfig: 'tsconfig.dist.json',
5-
dts: 'rolldown',
65
external: ['@sanity/logos'],
76
})

0 commit comments

Comments
 (0)