Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-bars-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": patch
---

Fix bundling with `babel-swc-loader` when `disableImportExportTransform` is set to `true` in RN babel preset
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { partitionTransforms } from '../babelSwcLoader.js';
import { buildFinalSwcConfig, partitionTransforms } from '../babelSwcLoader.js';

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

Expand Down Expand Up @@ -66,3 +66,27 @@ describe('partitionTransforms', () => {
expect(result).toMatchSnapshot('empty arrays');
});
});

describe('buildFinalSwcConfig', () => {
it('uses module.type from swcConfig when transform-modules-commonjs is present', () => {
const result = buildFinalSwcConfig({
swcConfig: { module: { type: 'commonjs' } },
includedSwcTransforms: [],
lazyImports: false,
sourceType: 'module',
});

expect(result.module?.type).toBe('commonjs');
});

it('falls back to es6 module.type when transform-modules-commonjs is not present', () => {
const result = buildFinalSwcConfig({
swcConfig: {},
includedSwcTransforms: [],
lazyImports: false,
sourceType: 'module',
});

expect(result.module?.type).toBe('es6');
});
});
58 changes: 41 additions & 17 deletions packages/repack/src/loaders/babelSwcLoader/babelSwcLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,41 @@ export function partitionTransforms(
return { includedSwcTransforms, supportedSwcTransforms, swcConfig };
}

export interface BuildFinalSwcConfigOptions {
swcConfig: SwcLoaderOptions;
includedSwcTransforms: string[];
lazyImports: boolean | string[];
sourceType: 'module' | 'script' | undefined;
}

export function buildFinalSwcConfig(
options: BuildFinalSwcConfigOptions
): SwcLoaderOptions {
const { swcConfig, includedSwcTransforms, lazyImports, sourceType } = options;
return {
...swcConfig,
// set env based on babel transforms
env: {
// node supports everything and does not include
// any transforms by default, so it can as a template
targets: { node: 24 },
include: includedSwcTransforms,
},
// set lazy imports based on loader options
module: {
...swcConfig.module,
lazy: lazyImports,
// if type is not set, this means that we are not using
// transform-modules-commonjs babel plugin, which can be disabled
// in babel config through `disableImportExportTransform` option in the RN preset
// we use 'es6' as a fallback here to achieve the desired behaviour of
// keeping ES modules as they are found in the source code
type: swcConfig.module?.type ?? 'es6',
},
isModule: sourceType === 'module',
};
}

export default async function babelSwcLoader(
this: LoaderContext<BabelSwcLoaderOptions>,
source: string,
Expand Down Expand Up @@ -118,23 +153,12 @@ export default async function babelSwcLoader(
hermesParserOverrides: options.hermesParserOverrides,
});

const finalSwcConfig: SwcLoaderOptions = {
...swcConfig,
// set env based on babel transforms
env: {
// node supports everything and does not include
// any transforms by default, so it can as a template
targets: { node: 24 },
include: includedSwcTransforms,
},
// set lazy imports based on loader options
module: {
...swcConfig.module,
lazy: lazyImports,
type: swcConfig.module!.type,
},
isModule: babelResult.sourceType === 'module',
};
const finalSwcConfig = buildFinalSwcConfig({
swcConfig,
includedSwcTransforms,
lazyImports,
sourceType: babelResult.sourceType,
});

const swcResult = swc.transformSync(babelResult?.code!, {
...finalSwcConfig,
Expand Down