Skip to content

Commit 47bd1d4

Browse files
committed
test: add coverage for react transforms being included
1 parent 1000d29 commit 47bd1d4

3 files changed

Lines changed: 94 additions & 4 deletions

File tree

packages/repack/src/loaders/babelSwcLoader/__tests__/__snapshots__/babelSwcLoader.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ exports[`partitionTransforms only custom transforms are excluded from included s
3737
},
3838
"transform": {
3939
"react": {
40-
"importSource": undefined,
41-
"runtime": undefined,
40+
"importSource": "react",
41+
"runtime": "automatic",
4242
"useBuiltins": true,
4343
},
4444
},

packages/repack/src/loaders/babelSwcLoader/__tests__/swc.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,90 @@ describe('swc transforms support detection', () => {
175175
);
176176
});
177177

178+
it('should apply default react transform when plugin has no react transform options', () => {
179+
const inputConfig: SwcLoaderOptions = {
180+
jsc: {
181+
transform: { react: {} },
182+
},
183+
};
184+
const { swcConfig } = getSupportedSwcCustomTransforms(
185+
[['transform-react-jsx', {}]],
186+
inputConfig
187+
);
188+
189+
expect(swcConfig.jsc?.transform?.react).toEqual({
190+
runtime: 'automatic',
191+
importSource: 'react',
192+
});
193+
});
194+
195+
it('should preserve existing react transform config when plugin has none', () => {
196+
const inputConfig: SwcLoaderOptions = {
197+
jsc: {
198+
transform: {
199+
react: { runtime: 'automatic', importSource: 'nativewind' },
200+
},
201+
},
202+
};
203+
const { swcConfig } = getSupportedSwcCustomTransforms(
204+
[['transform-react-jsx', {}]],
205+
inputConfig
206+
);
207+
208+
expect(swcConfig.jsc?.transform?.react).toEqual({
209+
runtime: 'automatic',
210+
importSource: 'nativewind',
211+
});
212+
});
213+
214+
it('should use plugin importSource option for react transform', () => {
215+
const inputConfig: SwcLoaderOptions = {
216+
jsc: {
217+
transform: {
218+
react: {},
219+
},
220+
},
221+
};
222+
const { swcConfig } = getSupportedSwcCustomTransforms(
223+
[
224+
[
225+
'transform-react-jsx',
226+
{ runtime: 'automatic', importSource: 'nativewind' },
227+
],
228+
],
229+
inputConfig
230+
);
231+
232+
expect(swcConfig.jsc?.transform?.react).toEqual({
233+
runtime: 'automatic',
234+
importSource: 'nativewind',
235+
});
236+
});
237+
238+
it('should use plugin importSource option for react transform and override existing importSource', () => {
239+
const inputConfig: SwcLoaderOptions = {
240+
jsc: {
241+
transform: {
242+
react: { importSource: 'preact' },
243+
},
244+
},
245+
};
246+
const { swcConfig } = getSupportedSwcCustomTransforms(
247+
[
248+
[
249+
'transform-react-jsx',
250+
{ runtime: 'automatic', importSource: 'nativewind' },
251+
],
252+
],
253+
inputConfig
254+
);
255+
256+
expect(swcConfig.jsc?.transform?.react).toEqual({
257+
runtime: 'automatic',
258+
importSource: 'nativewind',
259+
});
260+
});
261+
178262
it('configures modules commonjs options based on provided config (snapshot)', () => {
179263
const inputConfig: SwcLoaderOptions = {};
180264
const { swcConfig } = getSupportedSwcCustomTransforms(

packages/repack/src/loaders/babelSwcLoader/swc.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,14 @@ function getTransformReactRuntimeConfig(
8787
...swcConfig.jsc?.transform,
8888
react: {
8989
...swcConfig.jsc?.transform?.react,
90-
runtime: reactRuntimeConfig.runtime ?? 'automatic',
91-
importSource: reactRuntimeConfig.importSource ?? 'react',
90+
runtime:
91+
reactRuntimeConfig.runtime ??
92+
swcConfig.jsc?.transform?.react?.runtime ??
93+
'automatic',
94+
importSource:
95+
reactRuntimeConfig.importSource ??
96+
swcConfig.jsc?.transform?.react?.importSource ??
97+
'react',
9298
},
9399
},
94100
},

0 commit comments

Comments
 (0)