@@ -37,8 +37,6 @@ import type { Targets, InputTargets } from "@babel/helper-compilation-targets";
37
37
import availablePlugins from "./available-plugins.ts" ;
38
38
import { declarePreset } from "@babel/helper-plugin-utils" ;
39
39
40
- type ModuleTransformationsType =
41
- typeof import ( "./module-transformations" ) . default ;
42
40
import type { BuiltInsOption , ModuleOption , Options } from "./types.ts" ;
43
41
44
42
// TODO: Remove in Babel 8
@@ -117,55 +115,37 @@ export const transformIncludesAndExcludes = (opts: Array<string>): any => {
117
115
) ;
118
116
} ;
119
117
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
+ ) {
133
122
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 ] ) ;
149
125
}
150
126
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
+ }
153
136
}
154
137
155
138
if ( ! process . env . BABEL_8_BREAKING ) {
156
139
// Enable module-related syntax plugins for older Babel versions
157
140
if ( ! shouldTransformDynamicImport ) {
158
141
modulesPluginNames . push ( "syntax-dynamic-import" ) ;
159
142
}
160
- if ( ! shouldTransformExportNamespaceFrom ) {
161
- modulesPluginNames . push ( "syntax-export-namespace-from" ) ;
162
- }
163
143
modulesPluginNames . push ( "syntax-top-level-await" ) ;
164
144
modulesPluginNames . push ( "syntax-import-meta" ) ;
165
145
}
166
146
167
147
return modulesPluginNames ;
168
- } ;
148
+ }
169
149
170
150
const getCoreJSOptions = ( {
171
151
useBuiltIns,
@@ -312,16 +292,19 @@ function getLocalTargets(
312
292
}
313
293
314
294
function supportsStaticESM ( caller : CallerMetadata | undefined ) {
295
+ // TODO(Babel 8): Fallback to true
315
296
// @ts -expect-error supportsStaticESM is not defined in CallerMetadata
316
297
return ! ! caller ?. supportsStaticESM ;
317
298
}
318
299
319
300
function supportsDynamicImport ( caller : CallerMetadata | undefined ) {
301
+ // TODO(Babel 8): Fallback to true
320
302
// @ts -expect-error supportsDynamicImport is not defined in CallerMetadata
321
303
return ! ! caller ?. supportsDynamicImport ;
322
304
}
323
305
324
306
function supportsExportNamespaceFrom ( caller : CallerMetadata | undefined ) {
307
+ // TODO(Babel 8): Fallback to null
325
308
// @ts -expect-error supportsExportNamespaceFrom is not defined in CallerMetadata
326
309
return ! ! caller ?. supportsExportNamespaceFrom ;
327
310
}
@@ -344,7 +327,7 @@ export default declarePreset((api, opts: Options) => {
344
327
ignoreBrowserslistConfig,
345
328
include : optionsInclude ,
346
329
loose,
347
- modules,
330
+ modules : optionsModules ,
348
331
shippedProposals,
349
332
spec,
350
333
targets : optionsTargets ,
@@ -402,31 +385,34 @@ option \`forceAllTransforms: true\` instead.
402
385
const exclude = transformIncludesAndExcludes ( optionsExclude ) ;
403
386
404
387
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
+ }
423
409
424
410
const pluginNames = filterItems (
425
411
compatData ,
426
412
include . plugins ,
427
413
exclude . plugins ,
428
414
transformTargets ,
429
- modulesPluginNames ,
415
+ getSpecialModulesPluginNames ( modules , shouldTransformDynamicImport ) ,
430
416
getOptionSpecificExcludesFor ( { loose } ) ,
431
417
pluginSyntaxMap ,
432
418
) ;
@@ -500,7 +486,7 @@ option \`forceAllTransforms: true\` instead.
500
486
console . log ( "@babel/preset-env: `DEBUG` option" ) ;
501
487
console . log ( "\nUsing targets:" ) ;
502
488
console . log ( JSON . stringify ( prettifyTargets ( targets ) , null , 2 ) ) ;
503
- console . log ( `\nUsing modules transform: ${ modules . toString ( ) } ` ) ;
489
+ console . log ( `\nUsing modules transform: ${ optionsModules . toString ( ) } ` ) ;
504
490
console . log ( "\nUsing plugins:" ) ;
505
491
pluginNames . forEach ( pluginName => {
506
492
logPlugin ( pluginName , targets , compatData ) ;
@@ -515,3 +501,55 @@ option \`forceAllTransforms: true\` instead.
515
501
516
502
return { plugins } ;
517
503
} ) ;
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