@@ -37,8 +37,6 @@ import type { Targets, InputTargets } from "@babel/helper-compilation-targets";
3737import availablePlugins from "./available-plugins.ts" ;
3838import { declarePreset } from "@babel/helper-plugin-utils" ;
3939
40- type ModuleTransformationsType =
41- typeof import ( "./module-transformations" ) . default ;
4240import type { BuiltInsOption , ModuleOption , Options } from "./types.ts" ;
4341
4442// TODO: Remove in Babel 8
@@ -117,55 +115,37 @@ export const transformIncludesAndExcludes = (opts: Array<string>): any => {
117115 ) ;
118116} ;
119117
120- export const getModulesPluginNames = ( {
121- modules,
122- transformations,
123- shouldTransformESM,
124- shouldTransformDynamicImport,
125- shouldTransformExportNamespaceFrom,
126- } : {
127- modules : ModuleOption ;
128- transformations : ModuleTransformationsType ;
129- shouldTransformESM : boolean ;
130- shouldTransformDynamicImport : boolean ;
131- shouldTransformExportNamespaceFrom : boolean ;
132- } ) => {
118+ function getSpecialModulesPluginNames (
119+ modules : Exclude < ModuleOption , "auto" > ,
120+ shouldTransformDynamicImport : boolean ,
121+ ) {
133122 const modulesPluginNames = [ ] ;
134- if ( modules !== false && transformations [ modules ] ) {
135- if ( shouldTransformESM ) {
136- modulesPluginNames . push ( transformations [ modules ] ) ;
137- }
138-
139- if ( shouldTransformDynamicImport ) {
140- if ( shouldTransformESM && modules !== "umd" ) {
141- modulesPluginNames . push ( "transform-dynamic-import" ) ;
142- } else {
143- console . warn (
144- "Dynamic import can only be transformed when transforming ES" +
145- " modules to AMD, CommonJS or SystemJS." ,
146- ) ;
147- }
148- }
123+ if ( modules ) {
124+ modulesPluginNames . push ( moduleTransformations [ modules ] ) ;
149125 }
150126
151- if ( shouldTransformExportNamespaceFrom ) {
152- modulesPluginNames . push ( "transform-export-namespace-from" ) ;
127+ if ( shouldTransformDynamicImport ) {
128+ if ( modules && modules !== "umd" ) {
129+ modulesPluginNames . push ( "transform-dynamic-import" ) ;
130+ } else {
131+ console . warn (
132+ "Dynamic import can only be transformed when transforming ES" +
133+ " modules to AMD, CommonJS or SystemJS." ,
134+ ) ;
135+ }
153136 }
154137
155138 if ( ! process . env . BABEL_8_BREAKING ) {
156139 // Enable module-related syntax plugins for older Babel versions
157140 if ( ! shouldTransformDynamicImport ) {
158141 modulesPluginNames . push ( "syntax-dynamic-import" ) ;
159142 }
160- if ( ! shouldTransformExportNamespaceFrom ) {
161- modulesPluginNames . push ( "syntax-export-namespace-from" ) ;
162- }
163143 modulesPluginNames . push ( "syntax-top-level-await" ) ;
164144 modulesPluginNames . push ( "syntax-import-meta" ) ;
165145 }
166146
167147 return modulesPluginNames ;
168- } ;
148+ }
169149
170150const getCoreJSOptions = ( {
171151 useBuiltIns,
@@ -312,16 +292,19 @@ function getLocalTargets(
312292}
313293
314294function supportsStaticESM ( caller : CallerMetadata | undefined ) {
295+ // TODO(Babel 8): Fallback to true
315296 // @ts -expect-error supportsStaticESM is not defined in CallerMetadata
316297 return ! ! caller ?. supportsStaticESM ;
317298}
318299
319300function supportsDynamicImport ( caller : CallerMetadata | undefined ) {
301+ // TODO(Babel 8): Fallback to true
320302 // @ts -expect-error supportsDynamicImport is not defined in CallerMetadata
321303 return ! ! caller ?. supportsDynamicImport ;
322304}
323305
324306function supportsExportNamespaceFrom ( caller : CallerMetadata | undefined ) {
307+ // TODO(Babel 8): Fallback to null
325308 // @ts -expect-error supportsExportNamespaceFrom is not defined in CallerMetadata
326309 return ! ! caller ?. supportsExportNamespaceFrom ;
327310}
@@ -344,7 +327,7 @@ export default declarePreset((api, opts: Options) => {
344327 ignoreBrowserslistConfig,
345328 include : optionsInclude ,
346329 loose,
347- modules,
330+ modules : optionsModules ,
348331 shippedProposals,
349332 spec,
350333 targets : optionsTargets ,
@@ -402,31 +385,34 @@ option \`forceAllTransforms: true\` instead.
402385 const exclude = transformIncludesAndExcludes ( optionsExclude ) ;
403386
404387 const compatData = getPluginList ( shippedProposals , bugfixes ) ;
405- const shouldSkipExportNamespaceFrom =
406- ( modules === "auto" && api . caller ?.( supportsExportNamespaceFrom ) ) ||
407- ( modules === false &&
408- ! isRequired ( "transform-export-namespace-from" , transformTargets , {
409- compatData,
410- includes : include . plugins ,
411- excludes : exclude . plugins ,
412- } ) ) ;
413- const modulesPluginNames = getModulesPluginNames ( {
414- modules,
415- transformations : moduleTransformations ,
416- // TODO: Remove the 'api.caller' check eventually. Just here to prevent
417- // unnecessary breakage in the short term for users on older betas/RCs.
418- shouldTransformESM : modules !== "auto" || ! api . caller ?.( supportsStaticESM ) ,
419- shouldTransformDynamicImport :
420- modules !== "auto" || ! api . caller ?.( supportsDynamicImport ) ,
421- shouldTransformExportNamespaceFrom : ! shouldSkipExportNamespaceFrom ,
422- } ) ;
388+ const modules =
389+ optionsModules === "auto"
390+ ? api . caller ( supportsStaticESM )
391+ ? false
392+ : "commonjs"
393+ : optionsModules ;
394+ const shouldTransformDynamicImport =
395+ optionsModules === "auto" ? ! api . caller ( supportsDynamicImport ) : ! ! modules ;
396+
397+ // If the caller does not support export-namespace-from, we forcefully add
398+ // the plugin to `includes`.
399+ // TODO(Babel 8): stop doing this, similarly to how we don't do this for any
400+ // other plugin. We can consider adding bundlers as targets in the future,
401+ // but we should not have a one-off special case for this plugin.
402+ if (
403+ optionsModules === "auto" &&
404+ ! api . caller ( supportsExportNamespaceFrom ) &&
405+ ! exclude . plugins . has ( "transform-export-namespace-from" )
406+ ) {
407+ include . plugins . add ( "transform-export-namespace-from" ) ;
408+ }
423409
424410 const pluginNames = filterItems (
425411 compatData ,
426412 include . plugins ,
427413 exclude . plugins ,
428414 transformTargets ,
429- modulesPluginNames ,
415+ getSpecialModulesPluginNames ( modules , shouldTransformDynamicImport ) ,
430416 getOptionSpecificExcludesFor ( { loose } ) ,
431417 pluginSyntaxMap ,
432418 ) ;
@@ -500,7 +486,7 @@ option \`forceAllTransforms: true\` instead.
500486 console . log ( "@babel/preset-env: `DEBUG` option" ) ;
501487 console . log ( "\nUsing targets:" ) ;
502488 console . log ( JSON . stringify ( prettifyTargets ( targets ) , null , 2 ) ) ;
503- console . log ( `\nUsing modules transform: ${ modules . toString ( ) } ` ) ;
489+ console . log ( `\nUsing modules transform: ${ optionsModules . toString ( ) } ` ) ;
504490 console . log ( "\nUsing plugins:" ) ;
505491 pluginNames . forEach ( pluginName => {
506492 logPlugin ( pluginName , targets , compatData ) ;
@@ -515,3 +501,55 @@ option \`forceAllTransforms: true\` instead.
515501
516502 return { plugins } ;
517503} ) ;
504+
505+ // TODO(Babel 8): This is only here for backward compatibility. Remove it.
506+ export { getModulesPluginNamesBackwardCompat as getModulesPluginNames } ;
507+ const getModulesPluginNamesBackwardCompat = ( {
508+ modules,
509+ transformations,
510+ shouldTransformESM,
511+ shouldTransformDynamicImport,
512+ shouldTransformExportNamespaceFrom,
513+ } : {
514+ modules : ModuleOption ;
515+ transformations : typeof import ( "./module-transformations" ) . default ;
516+ shouldTransformESM : boolean ;
517+ shouldTransformDynamicImport : boolean ;
518+ shouldTransformExportNamespaceFrom : boolean ;
519+ } ) => {
520+ const modulesPluginNames = [ ] ;
521+ if ( modules !== false && transformations [ modules ] ) {
522+ if ( shouldTransformESM ) {
523+ modulesPluginNames . push ( transformations [ modules ] ) ;
524+ }
525+
526+ if ( shouldTransformDynamicImport ) {
527+ if ( shouldTransformESM && modules !== "umd" ) {
528+ modulesPluginNames . push ( "transform-dynamic-import" ) ;
529+ } else {
530+ console . warn (
531+ "Dynamic import can only be transformed when transforming ES" +
532+ " modules to AMD, CommonJS or SystemJS." ,
533+ ) ;
534+ }
535+ }
536+ }
537+
538+ if ( shouldTransformExportNamespaceFrom ) {
539+ modulesPluginNames . push ( "transform-export-namespace-from" ) ;
540+ }
541+
542+ if ( ! process . env . BABEL_8_BREAKING ) {
543+ // Enable module-related syntax plugins for older Babel versions
544+ if ( ! shouldTransformDynamicImport ) {
545+ modulesPluginNames . push ( "syntax-dynamic-import" ) ;
546+ }
547+ if ( ! shouldTransformExportNamespaceFrom ) {
548+ modulesPluginNames . push ( "syntax-export-namespace-from" ) ;
549+ }
550+ modulesPluginNames . push ( "syntax-top-level-await" ) ;
551+ modulesPluginNames . push ( "syntax-import-meta" ) ;
552+ }
553+
554+ return modulesPluginNames ;
555+ } ;
0 commit comments