Skip to content

Commit a224e84

Browse files
authored
fix: use es6 as fallback module type in babel-swc-loader (#1321)
1 parent 8ecfdd5 commit a224e84

3 files changed

Lines changed: 71 additions & 18 deletions

File tree

.changeset/sharp-bars-peel.md

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+
Fix bundling with `babel-swc-loader` when `disableImportExportTransform` is set to `true` in RN babel preset

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { partitionTransforms } from '../babelSwcLoader.js';
1+
import { buildFinalSwcConfig, partitionTransforms } from '../babelSwcLoader.js';
22

33
type TransformEntry = [string, Record<string, any> | undefined];
44

@@ -66,3 +66,27 @@ describe('partitionTransforms', () => {
6666
expect(result).toMatchSnapshot('empty arrays');
6767
});
6868
});
69+
70+
describe('buildFinalSwcConfig', () => {
71+
it('uses module.type from swcConfig when transform-modules-commonjs is present', () => {
72+
const result = buildFinalSwcConfig({
73+
swcConfig: { module: { type: 'commonjs' } },
74+
includedSwcTransforms: [],
75+
lazyImports: false,
76+
sourceType: 'module',
77+
});
78+
79+
expect(result.module?.type).toBe('commonjs');
80+
});
81+
82+
it('falls back to es6 module.type when transform-modules-commonjs is not present', () => {
83+
const result = buildFinalSwcConfig({
84+
swcConfig: {},
85+
includedSwcTransforms: [],
86+
lazyImports: false,
87+
sourceType: 'module',
88+
});
89+
90+
expect(result.module?.type).toBe('es6');
91+
});
92+
});

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

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,41 @@ export function partitionTransforms(
5555
return { includedSwcTransforms, supportedSwcTransforms, swcConfig };
5656
}
5757

58+
export interface BuildFinalSwcConfigOptions {
59+
swcConfig: SwcLoaderOptions;
60+
includedSwcTransforms: string[];
61+
lazyImports: boolean | string[];
62+
sourceType: 'module' | 'script' | undefined;
63+
}
64+
65+
export function buildFinalSwcConfig(
66+
options: BuildFinalSwcConfigOptions
67+
): SwcLoaderOptions {
68+
const { swcConfig, includedSwcTransforms, lazyImports, sourceType } = options;
69+
return {
70+
...swcConfig,
71+
// set env based on babel transforms
72+
env: {
73+
// node supports everything and does not include
74+
// any transforms by default, so it can as a template
75+
targets: { node: 24 },
76+
include: includedSwcTransforms,
77+
},
78+
// set lazy imports based on loader options
79+
module: {
80+
...swcConfig.module,
81+
lazy: lazyImports,
82+
// if type is not set, this means that we are not using
83+
// transform-modules-commonjs babel plugin, which can be disabled
84+
// in babel config through `disableImportExportTransform` option in the RN preset
85+
// we use 'es6' as a fallback here to achieve the desired behaviour of
86+
// keeping ES modules as they are found in the source code
87+
type: swcConfig.module?.type ?? 'es6',
88+
},
89+
isModule: sourceType === 'module',
90+
};
91+
}
92+
5893
export default async function babelSwcLoader(
5994
this: LoaderContext<BabelSwcLoaderOptions>,
6095
source: string,
@@ -118,23 +153,12 @@ export default async function babelSwcLoader(
118153
hermesParserOverrides: options.hermesParserOverrides,
119154
});
120155

121-
const finalSwcConfig: SwcLoaderOptions = {
122-
...swcConfig,
123-
// set env based on babel transforms
124-
env: {
125-
// node supports everything and does not include
126-
// any transforms by default, so it can as a template
127-
targets: { node: 24 },
128-
include: includedSwcTransforms,
129-
},
130-
// set lazy imports based on loader options
131-
module: {
132-
...swcConfig.module,
133-
lazy: lazyImports,
134-
type: swcConfig.module!.type,
135-
},
136-
isModule: babelResult.sourceType === 'module',
137-
};
156+
const finalSwcConfig = buildFinalSwcConfig({
157+
swcConfig,
158+
includedSwcTransforms,
159+
lazyImports,
160+
sourceType: babelResult.sourceType,
161+
});
138162

139163
const swcResult = swc.transformSync(babelResult?.code!, {
140164
...finalSwcConfig,

0 commit comments

Comments
 (0)