@@ -22,7 +22,6 @@ const propertyAccess = require("./util/propertyAccess");
22
22
const { register } = require ( "./util/serialization" ) ;
23
23
24
24
/** @typedef {import("webpack-sources").Source } Source */
25
- /** @typedef {import("../declarations/WebpackOptions").ExternalsPresets } ExternalsPresets */
26
25
/** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized } WebpackOptions */
27
26
/** @typedef {import("./Chunk") } Chunk */
28
27
/** @typedef {import("./ChunkGraph") } ChunkGraph */
@@ -54,7 +53,7 @@ const { register } = require("./util/serialization");
54
53
/** @typedef {import("./util/fs").InputFileSystem } InputFileSystem */
55
54
/** @typedef {import("./util/runtime").RuntimeSpec } RuntimeSpec */
56
55
57
- /** @typedef {{ attributes?: ImportAttributes, externalType: "import" | "module" | undefined, externalsPresets: ExternalsPresets | undefined } } ImportDependencyMeta */
56
+ /** @typedef {{ attributes?: ImportAttributes, externalType: "import" | "module" | undefined } } ImportDependencyMeta */
58
57
/** @typedef {{ layer?: string, supports?: string, media?: string } } CssImportDependencyMeta */
59
58
60
59
/** @typedef {ImportDependencyMeta | CssImportDependencyMeta } DependencyMeta */
@@ -167,7 +166,7 @@ const getSourceForImportExternal = (
167
166
const importName = runtimeTemplate . outputOptions . importFunctionName ;
168
167
if (
169
168
! runtimeTemplate . supportsDynamicImport ( ) &&
170
- ( importName === "import" || importName = == "module-import" )
169
+ ( importName === "import" || importName ! == "module-import" )
171
170
) {
172
171
throw new Error (
173
172
"The target environment doesn't support 'import()' so it's not possible to use external type 'import'"
@@ -579,25 +578,6 @@ class ExternalModule extends Module {
579
578
canMangle = true ;
580
579
}
581
580
break ;
582
- case "module" :
583
- if ( this . buildInfo . module ) {
584
- if ( ! Array . isArray ( request ) || request . length === 1 ) {
585
- this . buildMeta . exportsType = "namespace" ;
586
- canMangle = true ;
587
- }
588
- } else {
589
- this . buildMeta . async = true ;
590
- EnvironmentNotSupportAsyncWarning . check (
591
- this ,
592
- compilation . runtimeTemplate ,
593
- "external module"
594
- ) ;
595
- if ( ! Array . isArray ( request ) || request . length === 1 ) {
596
- this . buildMeta . exportsType = "namespace" ;
597
- canMangle = false ;
598
- }
599
- }
600
- break ;
601
581
case "script" :
602
582
this . buildMeta . async = true ;
603
583
EnvironmentNotSupportAsyncWarning . check (
@@ -614,18 +594,52 @@ class ExternalModule extends Module {
614
594
"external promise"
615
595
) ;
616
596
break ;
597
+ case "module" :
617
598
case "import" :
618
- this . buildMeta . async = true ;
619
- EnvironmentNotSupportAsyncWarning . check (
620
- this ,
621
- compilation . runtimeTemplate ,
622
- "external import"
623
- ) ;
624
- if ( ! Array . isArray ( request ) || request . length === 1 ) {
625
- this . buildMeta . exportsType = "namespace" ;
626
- canMangle = false ;
599
+ case "module-import" : {
600
+ const type =
601
+ externalType === "module-import" &&
602
+ this . dependencyMeta &&
603
+ /** @type {ImportDependencyMeta } */ ( this . dependencyMeta ) . externalType
604
+ ? /** @type {ImportDependencyMeta } */ ( this . dependencyMeta )
605
+ . externalType
606
+ : externalType ;
607
+
608
+ if ( type === "module" ) {
609
+ if ( this . buildInfo . module ) {
610
+ if ( ! Array . isArray ( request ) || request . length === 1 ) {
611
+ this . buildMeta . exportsType = "namespace" ;
612
+ canMangle = true ;
613
+ }
614
+ } else {
615
+ this . buildMeta . async = true ;
616
+ EnvironmentNotSupportAsyncWarning . check (
617
+ this ,
618
+ compilation . runtimeTemplate ,
619
+ "external module"
620
+ ) ;
621
+ if ( ! Array . isArray ( request ) || request . length === 1 ) {
622
+ this . buildMeta . exportsType = "namespace" ;
623
+ canMangle = false ;
624
+ }
625
+ }
626
+ }
627
+
628
+ if ( type === "import" ) {
629
+ this . buildMeta . async = true ;
630
+ EnvironmentNotSupportAsyncWarning . check (
631
+ this ,
632
+ compilation . runtimeTemplate ,
633
+ "external import"
634
+ ) ;
635
+ if ( ! Array . isArray ( request ) || request . length === 1 ) {
636
+ this . buildMeta . exportsType = "namespace" ;
637
+ canMangle = false ;
638
+ }
627
639
}
640
+
628
641
break ;
642
+ }
629
643
}
630
644
this . addDependency ( new StaticExportsDependency ( true , canMangle ) ) ;
631
645
callback ( ) ;
@@ -659,36 +673,6 @@ class ExternalModule extends Module {
659
673
660
674
_getRequestAndExternalType ( ) {
661
675
let { request, externalType } = this ;
662
-
663
- if ( externalType === "module-import" ) {
664
- const dependencyMeta = /** @type {ImportDependencyMeta } */ (
665
- this . dependencyMeta
666
- ) ;
667
-
668
- if ( dependencyMeta && dependencyMeta . externalType ) {
669
- externalType = dependencyMeta . externalType ;
670
- } else if ( dependencyMeta && dependencyMeta . externalsPresets ) {
671
- const presets = dependencyMeta . externalsPresets ;
672
- // TODO: what if user set multiple presets?
673
- if ( presets . web ) {
674
- externalType = "module" ;
675
- } else if ( presets . webAsync ) {
676
- externalType = "import" ;
677
- } else if (
678
- presets . electron ||
679
- presets . electronMain ||
680
- presets . electronPreload ||
681
- presets . electronRenderer ||
682
- presets . node ||
683
- presets . nwjs
684
- ) {
685
- externalType = "node-commonjs" ;
686
- }
687
- } else {
688
- externalType = "commonjs" ;
689
- }
690
- }
691
-
692
676
if ( typeof request === "object" && ! Array . isArray ( request ) )
693
677
request = request [ externalType ] ;
694
678
return { request, externalType } ;
@@ -753,43 +737,58 @@ class ExternalModule extends Module {
753
737
runtimeTemplate
754
738
) ;
755
739
}
756
- case "import" :
757
- return getSourceForImportExternal (
758
- request ,
759
- runtimeTemplate ,
760
- /** @type {ImportDependencyMeta } */ ( dependencyMeta )
761
- ) ;
762
740
case "script" :
763
741
return getSourceForScriptExternal ( request , runtimeTemplate ) ;
764
- case "module" : {
765
- if ( ! ( /** @type { BuildInfo } */ <
17A6
/span> ( this . buildInfo ) . module ) ) {
766
- if ( ! runtimeTemplate . supportsDynamicImport ( ) ) {
767
- throw new Error (
768
- `The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script ${
769
- runtimeTemplate . supportsEcmaScriptModuleSyntax ( )
770
- ? "\nDid you mean to build a EcmaScript Module ('output.module: true')?"
771
- : ""
772
- } `
773
- ) ;
774
- }
742
+ case "module" :
743
+ case "import" :
744
+ case "module-import" : {
745
+ const type =
746
+ externalType === "module- import" &&
747
+ dependencyMeta &&
748
+ /** @type { ImportDependencyMeta } */ ( dependencyMeta ) . externalType
749
+ ? /** @type { ImportDependencyMeta } */ ( dependencyMeta ) . externalType
750
+ : externalType ;
751
+
752
+ if ( type === "import" ) {
775
753
return getSourceForImportExternal (
776
754
request ,
777
755
runtimeTemplate ,
778
756
/** @type {ImportDependencyMeta } */ ( dependencyMeta )
779
757
) ;
780
758
}
781
- if ( ! runtimeTemplate . supportsEcmaScriptModuleSyntax ( ) ) {
782
- throw new Error (
783
- "The target environment doesn't support EcmaScriptModule syntax so it's not possible to use external type 'module'"
759
+
760
+ if ( type === "module" ) {
761
+ if ( ! ( /** @type {BuildInfo } */ ( this . buildInfo ) . module ) ) {
762
+ if ( ! runtimeTemplate . supportsDynamicImport ( ) ) {
763
+ throw new Error (
764
+ `The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script${
765
+ runtimeTemplate . supportsEcmaScriptModuleSyntax ( )
766
+ ? "\nDid you mean to build a EcmaScript Module ('output.module: true')?"
767
+ : ""
768
+ } `
769
+ ) ;
770
+ }
771
+ return getSourceForImportExternal (
772
+ request ,
773
+ runtimeTemplate ,
774
+ /** @type {ImportDependencyMeta } */ ( dependencyMeta )
775
+ ) ;
776
+ }
777
+ if ( ! runtimeTemplate . supportsEcmaScriptModuleSyntax ( ) ) {
778
+ throw new Error (
779
+ "The target environment doesn't support EcmaScriptModule syntax so it's not possible to use external type 'module'"
780
+ ) ;
781
+ }
782
+ return getSourceForModuleExternal (
783
+ request ,
784
+ moduleGraph . getExportsInfo ( this ) ,
785
+ runtime ,
786
+ runtimeTemplate ,
787
+ /** @type {ImportDependencyMeta } */ ( dependencyMeta )
784
788
) ;
785
789
}
786
- return getSourceForModuleExternal (
787
- request ,
788
- moduleGraph . getExportsInfo ( this ) ,
789
- runtime ,
790
- runtimeTemplate ,
791
- /** @type {ImportDependencyMeta } */ ( dependencyMeta )
792
- ) ;
790
+
791
+ break ;
793
792
}
794
793
case "var" :
795
794
case "promise" :
0 commit comments