Skip to content
Merged
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/itchy-deer-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": patch
---

Babel can detect whether a file being transformed is ESM or CJS and return this information. However, SWC previously assumed it was transforming only ESM files, which could cause issues with default export interop in some cases. SWC will now inherit the source type from Babel.
14 changes: 12 additions & 2 deletions packages/repack/src/loaders/babelLoader/babelLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
type BabelFileResult,
type TransformOptions,
loadOptions,
parseSync,
Expand Down Expand Up @@ -63,11 +64,15 @@ function buildBabelConfig(
return babelConfig;
}

type BabelTransformResult = BabelFileResult & {
sourceType: 'script' | 'module';
};

export const transform = async (
src: string,
transformOptions: TransformOptions,
customOptions?: CustomTransformOptions
) => {
): Promise<BabelTransformResult> => {
const babelConfig = buildBabelConfig(transformOptions, {
includePlugins: customOptions?.includePlugins,
excludePlugins: customOptions?.excludePlugins,
Expand Down Expand Up @@ -100,7 +105,12 @@ export const transform = async (
throw new Error(`Failed to transform source file: ${babelConfig.filename}`);
}

return result;
const sourceType = sourceAst.program.sourceType;

return {
...result,
sourceType,
};
};

export default async function babelLoader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export default async function babelSwcLoader(
lazy: lazyImports,
type: swcConfig.module!.type,
},
isModule: babelResult.sourceType === 'module',
};

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