@@ -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+
5893export 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