farmfe_toolkit 2.1.0

Toolkit for farm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
use farmfe_core::{
  module::{
    meta_data::script::{
      statement::{ExportSpecifierInfo, ImportSpecifierInfo, Statement, SwcId},
      ModuleExportIdentType, AMBIGUOUS_EXPORT_ALL, EXPORT_NAMESPACE, FARM_RUNTIME_MODULE_HELPER_ID,
    },
    module_graph::ModuleGraph,
    ModuleId,
  },
  plugin::ResolveKind,
  swc_common::DUMMY_SP,
  swc_ecma_ast::{
    BinaryOp, Ident, ImportDecl, ImportDefaultSpecifier, ImportNamedSpecifier, ImportSpecifier,
    ImportStarAsSpecifier, ModuleDecl, ModuleExportName, ModuleItem,
  },
  HashSet,
};
use swc_ecma_visit::VisitMutWith;

use crate::script::{
  analyze_statement::{analyze_statement_info, AnalyzedStatementInfo},
  concatenate_modules::{
    strip_module_decl::{is_module_external, StripModuleDeclStatementParams},
    utils::{
      create_bin_expr, create_define_export_star_item, create_export_all_item,
      create_import_farm_define_export_helper_stmt, create_member_expr, create_var_decl_item,
      get_filename,
    },
  },
  create_ambiguous_export_all_ident,
};

use super::{
  strip_module_decl::{PreservedImportDeclItem, PreservedImportDeclType, StripModuleDeclResult},
  unique_idents::{RenameVisitor, TopLevelIdentsRenameHandler},
  utils::{create_import_external_namespace_stmt, replace_module_decl},
  StripModuleContext,
};

pub struct HandleExternalModuleOptions<'a> {
  pub module_id: &'a ModuleId,
  pub source_module_id: &'a ModuleId,
  pub is_entry_module: bool,
  pub statement: &'a Statement,
  pub result: &'a mut StripModuleDeclResult,
  pub strip_context: &'a mut StripModuleContext,
  pub module_graph: &'a ModuleGraph,
  pub module_ids: &'a HashSet<ModuleId>,
}

impl<'a> HandleExternalModuleOptions<'a> {
  pub fn from(
    strip_param: &'a mut StripModuleDeclStatementParams,
    source_module_id: &'a ModuleId,
    statement: &'a Statement,
  ) -> Self {
    Self {
      module_id: strip_param.module_id,
      source_module_id,
      is_entry_module: strip_param.is_entry_module,
      statement,
      result: strip_param.result,
      strip_context: strip_param.strip_context,
      module_graph: strip_param.module_graph,
      module_ids: strip_param.module_ids,
    }
  }
}

pub struct HandleExternalModuleResult {
  pub ambiguous_export_all_idents: Vec<SwcId>,
}

pub fn handle_external_module(options: HandleExternalModuleOptions) -> HandleExternalModuleResult {
  let mut result = HandleExternalModuleResult {
    ambiguous_export_all_idents: vec![],
  };

  if options.statement.import_info.is_some() {
    handle_external_import(options);
  } else if options.statement.export_info.is_some() {
    result
      .ambiguous_export_all_idents
      .extend(handle_external_export(options));
  }

  result
}

fn handle_external_import(options: HandleExternalModuleOptions) {
  let HandleExternalModuleOptions {
    module_id,
    source_module_id,
    statement,
    result,
    strip_context,
    ..
  } = options;
  let rename_handler = strip_context.rename_handler.clone();
  let mut rename_handler = rename_handler.borrow_mut();

  let item = replace_module_decl(statement, result);
  let is_namespace_import = is_namespace_import_stmt(statement);

  // if the external module has been imported, we should reuse the import statement to avoid duplicate imports
  if !is_namespace_import
    && let Some(preserved_item) = find_preserved_import_named_stmt(strip_context, source_module_id)
  {
    for sp in &statement.import_info.as_ref().unwrap().specifiers {
      if let Some((existing_ident, sp_ident)) =
        get_imported_external_ident(&preserved_item.import_item, sp)
      {
        // rename the imported ident to the unique name
        let renamed_ident = rename_handler
          .get_renamed_ident(source_module_id, &existing_ident)
          .unwrap_or(existing_ident.clone());

        rename_handler.rename_ident(module_id.clone(), sp_ident, renamed_ident.clone());
      } else {
        // push the ident to existing import decl and rename it
        let import_decl = preserved_item
          .import_item
          .as_mut_module_decl()
          .unwrap()
          .as_mut_import()
          .unwrap();
        let new_sp = match sp {
          ImportSpecifierInfo::Namespace(_) => {
            unreachable!("Namespace should be in a separate import")
          }
          ImportSpecifierInfo::Named { local, imported } => {
            rename_handler.rename_ident_if_conflict(module_id, local);
            let renamed_ident = rename_handler.get_renamed_ident(module_id, local);

            if let Some(renamed_ident) = renamed_ident {
              ImportSpecifier::Named(ImportNamedSpecifier {
                span: DUMMY_SP,
                local: Ident::new(renamed_ident.sym.clone(), DUMMY_SP, renamed_ident.ctxt()),
                imported: imported
                  .as_ref()
                  .map(|i| ModuleExportName::Ident(Ident::new(i.sym.clone(), DUMMY_SP, i.ctxt())))
                  .or(Some(ModuleExportName::Ident(Ident::new(
                    local.sym.clone(),
                    DUMMY_SP,
                    local.ctxt(),
                  )))),
                is_type_only: false,
              })
            } else {
              ImportSpecifier::Named(ImportNamedSpecifier {
                span: DUMMY_SP,
                local: Ident::new(local.sym.clone(), DUMMY_SP, local.ctxt()),
                imported: imported
                  .as_ref()
                  .map(|i| ModuleExportName::Ident(Ident::new(i.sym.clone(), DUMMY_SP, i.ctxt()))),
                is_type_only: false,
              })
            }
          }
          ImportSpecifierInfo::Default(swc_id) => {
            rename_handler.rename_ident_if_conflict(module_id, swc_id);
            let renamed_ident = rename_handler
              .get_renamed_ident(module_id, swc_id)
              .unwrap_or(swc_id.clone());
            let local = Ident::new(renamed_ident.sym.clone(), DUMMY_SP, renamed_ident.ctxt());
            ImportSpecifier::Default(ImportDefaultSpecifier {
              span: DUMMY_SP,
              local,
            })
          }
        };
        import_decl.specifiers.push(new_sp);
      }
    }
  } else if let Some(preserved_item) = strip_context
    .preserved_import_decls
    .iter_mut()
    .find(|item| item.is_namespace_import && item.source_module_id == *source_module_id)
  {
    for sp in &statement.import_info.as_ref().unwrap().specifiers {
      if let ImportSpecifierInfo::Namespace(swc_id) = sp {
        rename_handler.rename_ident(
          module_id.clone(),
          swc_id.clone(),
          preserved_item
            .namespace_ident
            .as_ref()
            .unwrap()
            .to_id()
            .into(),
        );
      } else {
        // the import decl is not a namespace import, append the new import decl
        push_new_preserved_import(
          module_id,
          source_module_id,
          statement,
          item,
          strip_context,
          &mut rename_handler,
        );
        break;
      }
    }
  } else {
    push_new_preserved_import(
      module_id,
      source_module_id,
      statement,
      item,
      strip_context,
      &mut rename_handler,
    );
  }
}

fn push_new_preserved_import(
  module_id: &ModuleId,
  source_module_id: &ModuleId,
  statement: &Statement,
  mut item: ModuleItem,
  strip_context: &mut StripModuleContext,
  rename_handler: &mut TopLevelIdentsRenameHandler,
) {
  let mut renamed_ident = None;
  // rename the imported ident if there are conflicts
  for defined_ident in &statement.defined_idents {
    rename_handler.rename_ident_if_conflict(module_id, defined_ident);
    renamed_ident = rename_handler
      .get_renamed_ident(module_id, defined_ident)
      .or(Some(defined_ident.clone()));
  }

  let mut rename_visitor = RenameVisitor::new(module_id, Some(source_module_id), &rename_handler);
  item.visit_mut_with(&mut rename_visitor);

  let is_namespace_import = is_namespace_import_stmt(statement);

  // preserve the import statement, e.g. `import { createRequire } from 'module';`
  strip_context
    .preserved_import_decls
    .push(PreservedImportDeclItem {
      import_item: item,
      source_module_id: source_module_id.clone(),
      preserved_type: PreservedImportDeclType::ExternalOriginal,
      namespace_ident: if is_namespace_import {
        renamed_ident.map(|i| {
          let ctxt = i.ctxt();
          Ident::new(i.sym, DUMMY_SP, ctxt)
        })
      } else {
        None
      },
      is_namespace_import,
    });
}

fn is_namespace_import_stmt(statement: &Statement) -> bool {
  if let Some(import_info) = statement.import_info.as_ref() {
    import_info
      .specifiers
      .iter()
      .any(|sp| matches!(sp, ImportSpecifierInfo::Namespace(_)))
  } else {
    false
  }
}

pub fn get_imported_external_ident(
  existing_import_decl: &ModuleItem,
  current_specifier: &ImportSpecifierInfo,
) -> Option<(SwcId, SwcId)> {
  let module_decl = existing_import_decl.as_module_decl().unwrap();
  let import_decl = module_decl.as_import();

  import_decl.and_then(|import_decl| {
    import_decl
      .specifiers
      .iter()
      .find_map(|specifier| match specifier {
        ImportSpecifier::Named(import_named_specifier) => {
          let local_ident: SwcId = import_named_specifier.local.to_id().into();
          let local_imported_atom = import_named_specifier
            .imported
            .as_ref()
            .map(|i| i.atom())
            .unwrap_or(&local_ident.sym);

          if let ImportSpecifierInfo::Named { local, imported } = current_specifier {
            let imported_ident = imported.as_ref().unwrap_or(local);

            if *local_imported_atom == imported_ident.sym {
              return Some((local_ident, local.clone()));
            }
          }

          None
        }
        ImportSpecifier::Default(import_default_specifier) => {
          let local_ident: SwcId = import_default_specifier.local.to_id().into();

          if let ImportSpecifierInfo::Default(default_ident) = current_specifier {
            Some((local_ident, default_ident.clone()))
          } else {
            None
          }
        }
        ImportSpecifier::Namespace(import_star_as_specifier) => {
          let local_ident: SwcId = import_star_as_specifier.local.to_id().into();

          if let ImportSpecifierInfo::Namespace(ns_ident) = current_specifier {
            Some((local_ident, ns_ident.clone()))
          } else {
            None
          }
        }
      })
  })
}

/// insert `import * as external_all_farm_internal_ from 'module';`
fn find_or_create_preserved_import_item<'a>(
  strip_context: &'a mut StripModuleContext,
  module_id: &ModuleId,
  source_module_id: &ModuleId,
) -> &'a mut PreservedImportDeclItem {
  // check if the external module has been handled
  if let Some(index) = find_external_export_preserved_import(strip_context, source_module_id) {
    let result = strip_context.preserved_import_decls.get(index).unwrap();

    if result.is_namespace_import {
      return strip_context.preserved_import_decls.get_mut(index).unwrap();
    }
  }

  // create a unique ident for the external module
  let ident = {
    let mut rename_handler = strip_context.rename_handler.borrow_mut();
    create_unique_ambiguous_export_all_ident(module_id, source_module_id, &mut rename_handler)
  };
  // insert `import * as external_all_farm_internal_ from 'module';`
  let import_item = create_import_external_namespace_stmt(ident.clone(), source_module_id);

  // insert the generated import statement to the preserved import decls
  strip_context
    .preserved_import_decls
    .push(PreservedImportDeclItem {
      import_item,
      source_module_id: source_module_id.clone(),
      preserved_type: PreservedImportDeclType::ExternalGenerated,
      namespace_ident: Some(ident),
      is_namespace_import: true,
    });
  strip_context.preserved_import_decls.last_mut().unwrap()
}

fn create_unique_ambiguous_export_all_ident(
  module_id: &ModuleId,
  source_module_id: &ModuleId,
  rename_handler: &mut TopLevelIdentsRenameHandler,
) -> Ident {
  let ident = create_ambiguous_export_all_ident(source_module_id);
  // rename the imported ident to the unique name
  rename_handler.rename_ident_if_conflict(source_module_id, &ident.to_id().into());
  let renamed_ident = rename_handler.get_renamed_ident(source_module_id, &ident.to_id().into());

  if let Some(renamed_ident) = &renamed_ident {
    rename_handler.rename_ident(
      module_id.clone(),
      ident.to_id().into(),
      renamed_ident.clone(),
    );
  }

  let ident = renamed_ident
    .map(|i| Ident::new(i.sym.clone(), DUMMY_SP, i.ctxt()))
    .unwrap_or(ident);

  ident
}

/// Handle external export statements. Following is the example:
/// in `foo.js`:
/// ```js
/// export * from 'module'; // `module` is a external module
/// export const foo = 'foo';
/// ```
/// in `index.js`:
/// ```js
/// import { foo, baz, zoo } from './foo';
/// console.log(foo, baz, zoo);
/// ```
/// =>
///
/// in `module`:
/// ```js
/// import * as external_all_farm_internal_ from 'module';
/// var zoo = external_all_farm_internal_.zoo;
/// var bar = external_all_farm_internal_.bar;
/// export { zoo, bar as baz }
/// ```
/// full bundle:
/// ```js
/// // module
/// import * as external_all_farm_internal_ from 'module';
/// var zoo = external_all_farm_internal_.zoo;
/// var bar = external_all_farm_internal_.bar;
///
/// // foo.js
/// const foo = 'foo';
///
/// // index.js
/// console.log(foo, baz, zoo);
/// ```
fn handle_external_export(options: HandleExternalModuleOptions) -> Vec<SwcId> {
  let HandleExternalModuleOptions {
    module_id,
    source_module_id,
    is_entry_module,
    statement,
    result,
    strip_context,
    module_graph,
    module_ids,
  } = options;

  let module = module_graph.module(module_id).unwrap();

  if module.external || !module.module_type.is_script() {
    return vec![];
  }

  let mut ambiguous_export_all_idents = vec![];

  let mut import_specifiers = vec![];
  let mut idents_to_rename = vec![];

  for sp in &statement.export_info.as_ref().unwrap().specifiers {
    match sp {
      ExportSpecifierInfo::All => {
        // for `export * from 'module';` where module is external module.
        if let Some(source) = statement
          .export_info
          .as_ref()
          .and_then(|i| i.source.as_ref())
        {
          ambiguous_export_all_idents.extend(handle_ambiguous_export_all(
            HandleAmbiguousExportAllOptions {
              module_id,
              is_entry_module,
              result,
              strip_context,
              module_graph,
              module_ids,
              source,
            },
          ));
        }
      }
      ExportSpecifierInfo::Default => unreachable!(),
      ExportSpecifierInfo::Named { local, exported } => {
        let local = local.clone();
        let exported = exported.clone();

        idents_to_rename.push((local.clone(), exported.clone().unwrap_or(local.clone())));

        // `export { foo, bar as baz } from 'module';`
        // =>
        // import { foo, bar as baz } from 'module';`
        let (import_local, imported) = if let Some(exported) = exported {
          (exported.into(), Some(local.into()))
        } else {
          (local.into(), None)
        };

        import_specifiers.push(ImportSpecifier::Named(ImportNamedSpecifier {
          local: import_local,
          imported,
          span: DUMMY_SP,
          is_type_only: false,
        }));
      }
      ExportSpecifierInfo::Namespace(ns) => {
        import_specifiers.push(ImportSpecifier::Namespace(ImportStarAsSpecifier {
          span: DUMMY_SP,
          local: Ident::new(ns.sym.clone(), DUMMY_SP, ns.ctxt()),
        }));

        break;
      }
    }
  }

  if !import_specifiers.is_empty() {
    let (info, import_item) = transform_export_stmt_to_import_stmt(statement, import_specifiers);
    result.ast.body[statement.id] = import_item;
    // reuse the preserved import decl
    handle_external_import(HandleExternalModuleOptions {
      module_id,
      source_module_id,
      is_entry_module,
      statement: &info.into(),
      result,
      strip_context,
      module_graph,
      module_ids,
    });

    let rename_handler = strip_context.rename_handler.clone();
    let mut rename_handler = rename_handler.borrow_mut();

    for (export_to_rename, import_to_get) in idents_to_rename {
      let renamed_import_ident = rename_handler
        .get_renamed_ident(module_id, &import_to_get)
        .unwrap_or(import_to_get.clone());

      if let Some(export_ident) = module
        .meta
        .as_script()
        .export_ident_map
        .get(import_to_get.sym.as_str())
      {
        let export_ident = export_ident.as_internal();
        rename_handler.rename_ident(
          export_ident.module_id.clone(),
          export_ident.ident.clone(),
          renamed_import_ident,
        );
      } else {
        rename_handler.rename_ident(
          source_module_id.clone(),
          export_to_rename,
          renamed_import_ident,
        );
      }
    }
  }

  ambiguous_export_all_idents
}

fn transform_export_stmt_to_import_stmt(
  statement: &Statement,
  specifiers: Vec<ImportSpecifier>,
) -> (AnalyzedStatementInfo, ModuleItem) {
  // transform `export * as ns from 'module';` to `import * as ns from 'module';`
  let import_item = ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
    span: DUMMY_SP,
    specifiers,
    src: Box::new(
      statement
        .export_info
        .as_ref()
        .unwrap()
        .source
        .as_ref()
        .unwrap()
        .as_str()
        .into(),
    ),
    type_only: false,
    with: None,
    phase: Default::default(),
  }));

  (
    analyze_statement_info(&statement.id, &import_item),
    import_item,
  )
}

fn find_external_export_preserved_import(
  strip_context: &StripModuleContext,
  source_module_id: &ModuleId,
) -> Option<usize> {
  strip_context
    .preserved_import_decls
    .iter()
    .position(|item| {
      item.source_module_id == *source_module_id
        && matches!(
          item.preserved_type,
          PreservedImportDeclType::ExternalGenerated
        )
    })
}

pub struct HandleAmbiguousExportAllOptions<'a> {
  pub module_id: &'a ModuleId,
  pub is_entry_module: bool,
  pub result: &'a mut StripModuleDeclResult,
  pub strip_context: &'a mut StripModuleContext,
  pub module_graph: &'a ModuleGraph,
  pub module_ids: &'a HashSet<ModuleId>,
  pub source: &'a str,
}

pub fn handle_ambiguous_export_all(options: HandleAmbiguousExportAllOptions) -> Vec<SwcId> {
  let HandleAmbiguousExportAllOptions {
    module_id,
    is_entry_module,
    result,
    strip_context,
    module_graph,
    module_ids,
    source,
  } = options;

  let mut ns_idents = vec![];

  let source_module_id =
    module_graph.get_dep_by_source(module_id, source, Some(ResolveKind::ExportFrom));

  let contains_ambiguous_export_ident = module_graph
    .module(module_id)
    .map(|module| {
      module
        .meta
        .as_script()
        .ambiguous_export_ident_map
        .iter()
        .filter(|(export_str, export_idents)| {
          *export_str != AMBIGUOUS_EXPORT_ALL
            && export_idents.iter().any(|export_ident| {
              // the ident is external or it's an ambiguous ident
              !module_ids.contains(&export_ident.as_internal().module_id)
                || matches!(
                  export_ident.as_internal().export_type,
                  ModuleExportIdentType::ExternalExportAll
                    | ModuleExportIdentType::UnresolvedExportAll
                )
            })
        })
        .count()
        > 0
    })
    .unwrap_or(false);
  let should_add_namespace_ident = strip_context.should_add_namespace_ident.contains(module_id);

  let source_module = module_graph.module(&source_module_id).unwrap();

  // if module and source_module are both in module ids
  if should_add_namespace_ident
    && module_ids.contains(module_id)
    && module_ids.contains(&source_module_id)
    && !source_module.external
    && source_module.module_type.is_script()
    && let Some(ns_ident) = source_module
      .meta
      .as_script()
      .export_ident_map
      .get(EXPORT_NAMESPACE)
  {
    let renamed_ident = strip_context
      .rename_handler
      .borrow()
      .get_renamed_ident(&source_module_id, &ns_ident.as_internal().ident)
      .unwrap_or(ns_ident.as_internal().ident.clone());

    // append defineExportStar
    result.items_to_append.push(create_define_export_star_item(
      &module_id,
      renamed_ident.clone().into(),
    ));

    ns_idents.push(renamed_ident)
  }
  // 1. add export star only namespace ident is enabled
  // 2. add import if there are ambiguous export all idents
  else if contains_ambiguous_export_ident || should_add_namespace_ident {
    if source_module.external || !source_module.module_type.is_script() {
      ns_idents.push(add_external_export_all_helper(
        strip_context,
        module_id,
        &source_module_id,
        result,
      ));
    } else {
      let source_module_meta = source_module.meta.as_script();

      if let Some(export_all_idents) = source_module_meta
        .ambiguous_export_ident_map
        .get(AMBIGUOUS_EXPORT_ALL)
      {
        for export_all_ident in export_all_idents {
          ns_idents.push(add_external_export_all_helper(
            strip_context,
            module_id,
            &export_all_ident.as_internal().module_id,
            result,
          ));
        }
      }
    };
  }

  // for entry module, we should add all export * from for all external module
  if is_entry_module {
    let source_module = module_graph.module(&source_module_id).unwrap();

    if !source_module.external && source_module.module_type.is_script() {
      let source_module_meta = source_module.meta.as_script();

      if let Some(export_all_idents) = source_module_meta
        .ambiguous_export_ident_map
        .get(AMBIGUOUS_EXPORT_ALL)
      {
        for export_all_ident in export_all_idents {
          let export_all_module = module_graph
            .module(&export_all_ident.as_internal().module_id)
            .unwrap();

          if is_module_external(export_all_module, module_ids) {
            // add `export * from 'xxx`
            strip_context
              .preserved_import_decls
              .push(PreservedImportDeclItem {
                import_item: create_export_all_item(&export_all_module.id),
                source_module_id: export_all_module.id.clone(),
                preserved_type: PreservedImportDeclType::ExternalGenerated,
                namespace_ident: None,
                is_namespace_import: false,
              });
          }
        }
      }
    }
  }

  ns_idents
}

fn add_external_export_all_helper(
  strip_context: &mut StripModuleContext,
  module_id: &ModuleId,
  source_module_id: &ModuleId,
  result: &mut StripModuleDeclResult,
) -> SwcId {
  let should_add_namespace_ident = strip_context.should_add_namespace_ident.contains(module_id);
  let preserved_helper_module_id: ModuleId = FARM_RUNTIME_MODULE_HELPER_ID.into();

  if should_add_namespace_ident
    && find_external_export_preserved_import(strip_context, &preserved_helper_module_id).is_none()
  {
    // add `import { defineExportStar } from '@farm-runtime/module-helper` to the top
    strip_context
      .preserved_import_decls
      .push(PreservedImportDeclItem {
        import_item: create_import_farm_define_export_helper_stmt(),
        source_module_id: preserved_helper_module_id,
        preserved_type: PreservedImportDeclType::ExternalGenerated,
        namespace_ident: None,
        is_namespace_import: false,
      });
  }

  // add import * xxx from the external module
  let preserved_item =
    find_or_create_preserved_import_item(strip_context, module_id, &source_module_id);

  // append defineExportStar
  if should_add_namespace_ident {
    result.items_to_append.push(create_define_export_star_item(
      &module_id,
      preserved_item.namespace_ident.clone().unwrap().into(),
    ));
  }

  preserved_item.namespace_ident.clone().unwrap().into()
}

pub fn add_ambiguous_ident_decl(
  module_graph: &ModuleGraph,
  module_id: &ModuleId,
  ns_idents: Vec<SwcId>,
  strip_context: &mut StripModuleContext,
) {
  if ns_idents.is_empty() {
    return;
  }

  // var export_str = xxxx.export_str
  let module = module_graph.module(module_id).unwrap();
  let module_meta = module.meta.as_script();
  let mut rename_handler = strip_context.rename_handler.borrow_mut();

  // sort export ident map to make sure the order is deterministic
  let mut exports = module_meta.export_ident_map.keys().collect::<Vec<_>>();
  exports.sort();

  for export_str in exports {
    let module_export_ident = module_meta.export_ident_map.get(export_str).unwrap();
    let module_export_ident = module_export_ident.as_internal();

    // The ident is not a normal esm export ident an it's not renamed yet. For example, other rename handler like external rename will rename it, we should not override it.
    if matches!(
      module_export_ident.export_type,
      ModuleExportIdentType::UnresolvedExportAll | ModuleExportIdentType::ExternalExportAll
    ) {
      let extra_var_decl = strip_context
        .extra_var_decls
        .iter_mut()
        .find(|(id, _, _)| *id == module_export_ident.ident);

      let filename = get_filename(&module_export_ident.module_id);
      let final_export_str = format!("{filename}_{export_str}");
      let export_ident = SwcId::from(final_export_str.as_str());

      let export_ident = extra_var_decl
        .as_ref()
        .and_then(|(_, export_ident, _)| Some(export_ident.clone()))
        .or_else(|| rename_handler.get_unique_ident(&export_ident))
        .unwrap_or(export_ident);

      let decl_item = create_var_decl_item(
        export_ident.clone().into(),
        // xxx1.export_str || xxx2.export_str
        Box::new(create_bin_expr(
          ns_idents
            .iter()
            .map(|ns_ident| create_member_expr(ns_ident, export_str.as_str()))
            .collect(),
          BinaryOp::LogicalOr,
        )),
      );

      // use the outer decl item if there are multiple export * in the chain
      if let Some(extra_var_decl) = extra_var_decl {
        extra_var_decl.2 = decl_item;
      } else {
        rename_handler.rename_ident(
          module_export_ident.module_id.clone(),
          module_export_ident.ident.clone(),
          export_ident.clone(),
        );
        strip_context.extra_var_decls.push((
          module_export_ident.ident.clone(),
          export_ident,
          decl_item,
        ));
      }
    }
  }
}

pub fn find_preserved_import_named_stmt<'a>(
  strip_context: &'a mut StripModuleContext,
  source_module_id: &ModuleId,
) -> Option<&'a mut PreservedImportDeclItem> {
  strip_context
    .preserved_import_decls
    .iter_mut()
    .find(|item| {
      !item.is_namespace_import
        && item.source_module_id == *source_module_id
        && item // make sure it's a import statement
          .import_item
          .as_module_decl()
          .and_then(|m| m.as_import())
          .is_some()
    })
}