Skip to content

Commit 32c32c0

Browse files
whydidoodannyhw
andauthored
fix(1422): swc jsx development (#1493)
Co-authored-by: Daniel Williams <dannyhyunsoowilliams@gmail.com>
1 parent 87db9a1 commit 32c32c0

6 files changed

Lines changed: 131 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack": patch
3+
---
4+
5+
Make SWC JSX development transforms follow the build mode. `getSwcLoaderOptions` (and `getJsTransformRules`) tied `jsc.transform.react.development` to the JSX runtime, so development builds with the default `automatic` runtime emitted `jsx` instead of `jsxDEV` and lost `__source` / `__self`, accurate component stacks and "open in editor", while `classic` production builds kept them. `getSwcLoaderOptions` no longer sets `jsc.transform.react.development` by default, so Rspack's `builtin:swc-loader` derives it from `mode`, and development builds with the `automatic` runtime now import `<importSource>/jsx-dev-runtime`. Both helpers accept a `development` option to override it, e.g. `getJsTransformRules({ swc: { development: false } })` when a custom `importSource` doesn't ship a `jsx-dev-runtime` entry.

‎packages/repack/src/utils/getJsTransformRules.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ interface GetJsTransformRulesOptions {
1616
importSource?: string;
1717
/** The JSX runtime to use ('automatic' for React 17+ new JSX transform or 'classic' for traditional JSX transform) */
1818
jsxRuntime?: 'automatic' | 'classic';
19+
/** Whether to apply JSX development transforms (defaults to the build `mode`, set by Rspack's `builtin:swc-loader`) */
20+
development?: boolean;
1921
/** Enable lazy loading for all imports or specific modules */
2022
lazyImports?: boolean | string[];
2123
};

‎packages/repack/src/utils/getSwcLoaderOptions.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ function getParserOptions(syntax: 'js' | 'ts', jsx: boolean) {
5252

5353
function getJSCTransformOptions(
5454
jsxRuntime: 'automatic' | 'classic',
55-
importSource: string
55+
importSource: string,
56+
development?: boolean
5657
) {
5758
return {
5859
react: {
5960
runtime: jsxRuntime,
60-
development: jsxRuntime === 'classic',
61+
// when omitted, Rspack's builtin:swc-loader sets it from `mode`
62+
...(development !== undefined && { development }),
6163
importSource,
6264
},
6365
};
@@ -109,6 +111,14 @@ interface MakeSwcLoaderConfigOptions {
109111
*/
110112
jsxRuntime?: 'automatic' | 'classic';
111113

114+
/**
115+
* Whether to apply JSX development transforms (`jsxDEV`, `__source`, `__self`).
116+
* Defaults to the build `mode` (set by Rspack's `builtin:swc-loader` when omitted).
117+
* See SWC `jsc.transform.react.development`:
118+
* https://swc.rs/docs/configuration/compilation#jsctransformreactdevelopment
119+
*/
120+
development?: boolean;
121+
112122
/**
113123
* Whether to disable transformation of import/export statements.
114124
*/
@@ -137,6 +147,7 @@ interface MakeSwcLoaderConfigOptions {
137147
* @param options.jsx Whether to enable JSX parsing and transformation
138148
* @param options.externalHelpers Whether to use external helpers for transformations (equivalent of `@babel/runtime`)
139149
* @param options.jsxRuntime The JSX runtime to use ('automatic' for React 17+ new JSX transform or 'classic' for traditional JSX transform)
150+
* @param options.development Whether to apply JSX development transforms (defaults to the build `mode`, set by Rspack's `builtin:swc-loader`)
140151
* @param options.disableImportExportTransform Whether to disable transformation of import/export statements
141152
* @param options.importSource The source module for JSX runtime imports (defaults to 'react')
142153
* @param options.lazyImports Enable lazy loading for all imports or specific modules
@@ -148,6 +159,7 @@ export function getSwcLoaderOptions({
148159
jsx,
149160
externalHelpers = true,
150161
jsxRuntime = 'automatic',
162+
development,
151163
disableImportExportTransform = false,
152164
importSource = 'react',
153165
lazyImports = false,
@@ -158,7 +170,7 @@ export function getSwcLoaderOptions({
158170
assumptions: getCompilerAssumptions(),
159171
externalHelpers: externalHelpers,
160172
parser: getParserOptions(syntax, jsx),
161-
transform: getJSCTransformOptions(jsxRuntime, importSource),
173+
transform: getJSCTransformOptions(jsxRuntime, importSource, development),
162174
},
163175
module: getModuleOptions(disableImportExportTransform, lazyImports),
164176
};
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import path from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { getJsTransformRules } from '@callstack/repack';
4+
import { describe, expect, inject, it } from 'vitest';
5+
import {
6+
compile,
7+
createCompiler,
8+
createVirtualModulePlugin,
9+
} from '../helpers.js';
10+
11+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
12+
13+
// JSX source info that development transforms add for the fixture
14+
// (`jsxDEV` source argument for automatic runtime, `__source` prop for classic)
15+
const SOURCE_INFO = /fileName: ".*App\.jsx"/;
16+
17+
async function compileJsx(
18+
mode: 'development' | 'production',
19+
swc: NonNullable<Parameters<typeof getJsTransformRules>[0]>['swc']
20+
) {
21+
const virtualPlugin = await createVirtualModulePlugin({
22+
'App.jsx': 'export const App = () => <view title="hello" />;',
23+
});
24+
25+
const compiler = await createCompiler({
26+
context: __dirname,
27+
mode,
28+
devtool: false,
29+
entry: './App.jsx',
30+
output: { path: '/out' },
31+
optimization: { minimize: false },
32+
externals: {
33+
react: 'commonjs react',
34+
'react/jsx-runtime': 'commonjs react/jsx-runtime',
35+
'react/jsx-dev-runtime': 'commonjs react/jsx-dev-runtime',
36+
},
37+
module: { rules: getJsTransformRules({ swc }) },
38+
plugins: [virtualPlugin],
39+
});
40+
41+
const { code } = await compile(compiler);
42+
return code;
43+
}
44+
45+
// builtin:swc-loader only exists in Rspack
46+
describe.runIf(inject('bundlerType') === 'rspack')(
47+
'SWC JSX development transforms',
48+
() => {
49+
it('emits jsxDEV with source info for automatic runtime in development', async () => {
50+
const code = await compileJsx('development', { jsxRuntime: 'automatic' });
51+
52+
expect(code).toContain('react/jsx-dev-runtime');
53+
expect(code).toContain('jsxDEV');
54+
expect(code).toMatch(SOURCE_INFO);
55+
});
56+
57+
it('emits jsx without source info for automatic runtime in production', async () => {
58+
const code = await compileJsx('production', { jsxRuntime: 'automatic' });
59+
60+
expect(code).toContain('react/jsx-runtime');
61+
expect(code).not.toContain('jsx-dev-runtime');
62+
expect(code).not.toContain('jsxDEV');
63+
expect(code).not.toMatch(SOURCE_INFO);
64+
});
65+
66+
it('adds __source for classic runtime in development', async () => {
67+
const code = await compileJsx('development', { jsxRuntime: 'classic' });
68+
69+
expect(code).toContain('createElement');
70+
expect(code).toContain('__source');
71+
expect(code).toMatch(SOURCE_INFO);
72+
});
73+
74+
it('omits __source for classic runtime in production', async () => {
75+
const code = await compileJsx('production', { jsxRuntime: 'classic' });
76+
77+
expect(code).toContain('createElement');
78+
expect(code).not.toContain('__source');
79+
expect(code).not.toMatch(SOURCE_INFO);
80+
});
81+
82+
it('respects an explicit development: false in development', async () => {
83+
const code = await compileJsx('development', { development: false });
84+
85+
expect(code).toContain('react/jsx-runtime');
86+
expect(code).not.toContain('jsxDEV');
87+
expect(code).not.toMatch(SOURCE_INFO);
88+
});
89+
90+
it('respects an explicit development: true in production', async () => {
91+
const code = await compileJsx('production', { development: true });
92+
93+
expect(code).toContain('react/jsx-dev-runtime');
94+
expect(code).toContain('jsxDEV');
95+
expect(code).toMatch(SOURCE_INFO);
96+
});
97+
}
98+
);

‎website/src/latest/api/utils/get-js-transform-rules.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ interface GetJsTransformRulesOptions {
2626
externalHelpers?: boolean;
2727
importSource?: string;
2828
jsxRuntime?: "automatic" | "classic";
29+
development?: boolean;
2930
lazyImports?: boolean | string[];
3031
};
3132
flow?: {

‎website/src/latest/api/utils/get-swc-loader-options.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface MakeSwcLoaderConfigOptions {
1414
jsx: boolean;
1515
externalHelpers?: boolean;
1616
jsxRuntime?: "automatic" | "classic";
17+
development?: boolean;
1718
disableImportExportTransform?: boolean;
1819
importSource?: string;
1920
lazyImports?: boolean | string[];
@@ -58,6 +59,15 @@ The JSX runtime to use - 'automatic' for React 17+ new JSX transform or 'classic
5859

5960
See [SWC `jsc.transform.react.runtime`](https://swc.rs/docs/configuration/compilation#jsctransformreactruntime).
6061

62+
### options.development
63+
64+
- Type: `boolean`
65+
- Default: the build `mode` when used with Rspack's `builtin:swc-loader`
66+
67+
Whether to apply JSX development transforms (`jsxDEV`, `__source`, `__self`), which give accurate component stacks and "open in editor" during development. When omitted, Rspack's `builtin:swc-loader` sets it from `mode`: `true` in development, `false` in production. With the `automatic` runtime, development builds then import `<importSource>/jsx-dev-runtime`; set it to `false` if your `importSource` doesn't provide one.
68+
69+
See [SWC `jsc.transform.react.development`](https://swc.rs/docs/configuration/compilation#jsctransformreactdevelopment).
70+
6171
### options.disableImportExportTransform
6272

6373
- Type: `boolean`

0 commit comments

Comments
 (0)