@@ -526,7 +526,7 @@ class ExternalModule extends Module {
526
526
* @returns {string } a unique identifier of the module
527
527
*/
528
528
identifier ( ) {
529
- return `external ${ this . externalType } ${ JSON . stringify ( this . request ) } ` ;
529
+ return `external ${ this . _resolveExternalType ( this . externalType ) } ${ JSON . stringify ( this . request ) } ` ;
530
530
}
531
531
532
532
/**
@@ -546,25 +546,6 @@ class ExternalModule extends Module {
546
546
return callback ( null , ! this . buildMeta ) ;
547
547
}
548
548
549
- /**
550
- * @param {string } externalType raw external type
551
- * @returns {string } resolved external type
552
- */
553
- getModuleImportType ( externalType ) {
554
- if ( externalType === "module-import" ) {
555
- if (
556
- this . dependencyMeta &&
557
- /** @type {ImportDependencyMeta } */ ( this . dependencyMeta ) . externalType
558
- ) {
559
- return /** @type {ImportDependencyMeta } */ ( this . dependencyMeta )
560
- . externalType ;
561
- }
562
- return "module" ;
563
- }
564
-
565
- return externalType ;
566
- }
567
-
568
549
/**
569
550
* @param {WebpackOptions } options webpack options
570
551
* @param {Compilation } compilation the compilation
@@ -597,6 +578,25 @@ class ExternalModule extends Module {
597
578
canMangle = true ;
598
579
}
599
580
break ;
581
+ case "module" :
582
+ if ( this . buildInfo . module ) {
583
+ if ( ! Array . isArray ( request ) || request . length === 1 ) {
584
+ this . buildMeta . exportsType = "namespace" ;
585
+ canMangle = true ;
586
+ }
587
+ } else {
588
+ this . buildMeta . async = true ;
589
+ EnvironmentNotSupportAsyncWarning . check (
590
+ this ,
591
+ compilation . runtimeTemplate ,
592
+ "external module"
593
+ ) ;
594
+ if ( ! Array . isArray ( request ) || request . length === 1 ) {
595
+ this . buildMeta . exportsType = "namespace" ;
596
+ canMangle = false ;
597
+ }
598
+ }
599
+ break ;
600
600
case "script" :
601
601
this . buildMeta . async = true ;
602
602
EnvironmentNotSupportAsyncWarning . check (
@@ -613,45 +613,18 @@ class ExternalModule extends Module {
613
613
"external promise"
614
614
) ;
615
615
break ;
616
- case "module" :
617
616
case "import" :
618
- case "module-import" : {
619
- const type = this . getModuleImportType ( externalType ) ;
620
- if ( type === "module" ) {
621
- if ( this . buildInfo . module ) {
622
- if ( ! Array . isArray ( request ) || request . length === 1 ) {
623
- this . buildMeta . exportsType = "namespace" ;
624
- canMangle = true ;
625
- }
626
- } else {
627
- this . buildMeta . async = true ;
628
- EnvironmentNotSupportAsyncWarning . check (
629
- this ,
630
- compilation . runtimeTemplate ,
631
- "external module"
632
- ) ;
633
- if ( ! Array . isArray ( request ) || request . length === 1 ) {
634
- this . buildMeta . exportsType = "namespace" ;
635
- canMangle = false ;
636
- }
637
- }
638
- }
639
-
640
- if ( type === "import" ) {
641
- this . buildMeta . async = true ;
642
- EnvironmentNotSupportAsyncWarning . check (
643
- this ,
644
- compilation . runtimeTemplate ,
645
- "external import"
646
- ) ;
647
- if ( ! Array . isArray ( request ) || request . length === 1 ) {
648
- this . buildMeta . exportsType = "namespace" ;
649
- canMangle = false ;
650
- }
617
+ this . buildMeta . async = true ;
618
+ EnvironmentNotSupportAsyncWarning . check (
619
+ this ,
620
+ compilation . runtimeTemplate ,
621
+ "external import"
622
+ ) ;
623
+ if ( ! Array . isArray ( request ) || request . length === 1 ) {
624
+ this . buildMeta . exportsType = "namespace" ;
625
+ canMangle = false ;
651
626
}
652
-
653
627
break ;
654
- }
655
628
}
656
629
this . addDependency ( new StaticExportsDependency ( true , canMangle ) ) ;
657
630
callback ( ) ;
@@ -687,9 +660,31 @@ class ExternalModule extends Module {
687
660
let { request, externalType } = this ;
688
661
if ( typeof request === "object" && ! Array . isArray ( request ) )
689
662
request = request [ externalType ] ;
663
+ externalType = this . _resolveExternalType ( externalType ) ;
690
664
return { request, externalType } ;
691
665
}
692
666
667
+ /**
668
+ * Resolve the detailed external type from the raw external type.
669
+ * e.g. resolve "module" or "import" from "module-import" type
670
+ * @param {string } externalType raw external type
671
+ * @returns {string } resolved external type
672
+ */
673
+ _resolveExternalType ( externalType ) {
674
+ if ( externalType === "module-import" ) {
675
+ if (
676
+ this . dependencyMeta &&
677
+ /** @type {ImportDependencyMeta } */ ( this . dependencyMeta ) . externalType
678
+ ) {
679
+ return /** @type {ImportDependencyMeta } */ ( this . dependencyMeta )
680
+ . externalType ;
681
+ }
682
+ return "module" ;
683
+ }
684
+
685
+ return externalType ;
686
+ }
687
+
693
688
/**
694
689
* @private
695
690
* @param {string | string[] } request request
@@ -749,52 +744,43 @@ class ExternalModule extends Module {
749
744
runtimeTemplate
750
745
) ;
751
746
}
747
+ case "import" :
748
+ return getSourceForImportExternal (
749
+ request ,
750
+ runtimeTemplate ,
751
+ /** @type {ImportDependencyMeta } */ ( dependencyMeta )
752
+ ) ;
752
753
case "script" :
753
754
return getSourceForScriptExternal ( request , runtimeTemplate ) ;
754
- case "module" :
755
- case "import" :
756
- case "module-import" : {
757
- const type = this . getModuleImportType ( externalType ) ;
758
- if ( type === "import" ) {
759
- return getSourceForImportExternal (
760
- request ,
761
- runtimeTemplate ,
762
- /** @type {ImportDependencyMeta } */ ( dependencyMeta )
763
- ) ;
764
- }
765
-
766
- if ( type === "module" ) {
767
- if ( ! ( /** @type {BuildInfo } */ ( this . buildInfo ) . module ) ) {
768
- if ( ! runtimeTemplate . supportsDynamicImport ( ) ) {
769
- throw new Error (
770
- `The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script${
771
- runtimeTemplate . supportsEcmaScriptModuleSyntax ( )
772
- ? "\nDid you mean to build a EcmaScript Module ('output.module: true')?"
773
- : ""
774
- } `
775
- ) ;
776
- }
777
- return getSourceForImportExternal (
778
- request ,
779
- runtimeTemplate ,
780
- /** @type {ImportDependencyMeta } */ ( dependencyMeta )
781
- ) ;
782
- }
783
- if ( ! runtimeTemplate . supportsEcmaScriptModuleSyntax ( ) ) {
755
+ case "module" : {
756
+ if ( ! ( /** @type {BuildInfo } */ ( this . buildInfo ) . module ) ) {
757
+ if ( ! runtimeTemplate . supportsDynamicImport ( ) ) {
784
758
throw new Error (
785
- "The target environment doesn't support EcmaScriptModule syntax so it's not possible to use external type 'module'"
759
+ `The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script${
760
+ runtimeTemplate . supportsEcmaScriptModuleSyntax ( )
761
+ ? "\nDid you mean to build a EcmaScript Module ('output.module: true')?"
762
+ : ""
763
+ } `
786
764
) ;
787
765
}
788
- return getSourceForModuleExternal (
766
+ return getSourceForImportExternal (
789
767
request ,
790
- moduleGraph . getExportsInfo ( this ) ,
791
- runtime ,
792
768
runtimeTemplate ,
793
769
/** @type {ImportDependencyMeta } */ ( dependencyMeta )
794
770
) ;
795
771
}
796
-
797
- break ;
772
+ if ( ! runtimeTemplate . supportsEcmaScriptModuleSyntax ( ) ) {
773
+ throw new Error (
774
+ "The target environment doesn't support EcmaScriptModule syntax so it's not possible to use external type 'module'"
775
+ ) ;
776
+ }
777
+ return getSourceForModuleExternal (
778
+ request ,
779
+ moduleGraph . getExportsInfo ( this ) ,
780
+ runtime ,
781
+ runtimeTemplate ,
782
+ /** @type {ImportDependencyMeta } */ ( dependencyMeta )
783
+ ) ;
798
784
}
799
785
case "var" :
800
786
case "promise" :
@@ -939,7 +925,7 @@ class ExternalModule extends Module {
939
925
updateHash ( hash , context ) {
940
926
const { chunkGraph } = context ;
941
927
hash . update (
942
- `${ this . externalType } ${ JSON . stringify ( this . request ) } ${ this . isOptional (
928
+ `${ this . _resolveExternalType ( this . externalType ) } ${ JSON . stringify ( this . request ) } ${ this . isOptional (
943
929
chunkGraph . moduleGraph
944
930
) } `
945
931
) ;
0 commit comments