@@ -22,7 +22,6 @@ const propertyAccess = require("./util/propertyAccess");
2222const { register } = require ( "./util/serialization" ) ;
2323
2424/** @typedef {import("webpack-sources").Source } Source */
25- /** @typedef {import("../declarations/WebpackOptions").ExternalsPresets } ExternalsPresets */
2625/** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized } WebpackOptions */
2726/** @typedef {import("./Chunk") } Chunk */
2827/** @typedef {import("./ChunkGraph") } ChunkGraph */
@@ -54,7 +53,7 @@ const { register } = require("./util/serialization");
5453/** @typedef {import("./util/fs").InputFileSystem } InputFileSystem */
5554/** @typedef {import("./util/runtime").RuntimeSpec } RuntimeSpec */
5655
57- /** @typedef {{ attributes?: ImportAttributes, externalType: "import" | "module" | undefined, externalsPresets: ExternalsPresets | undefined } } ImportDependencyMeta */
56+ /** @typedef {{ attributes?: ImportAttributes, externalType: "import" | "module" | undefined } } ImportDependencyMeta */
5857/** @typedef {{ layer?: string, supports?: string, media?: string } } CssImportDependencyMeta */
5958
6059/** @typedef {ImportDependencyMeta | CssImportDependencyMeta } DependencyMeta */
@@ -547,6 +546,25 @@ class ExternalModule extends Module {
547546 return callback ( null , ! this . buildMeta ) ;
548547 }
549548
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+
550568 /**
551569 * @param {WebpackOptions } options webpack options
552570 * @param {Compilation } compilation the compilation
@@ -579,25 +597,6 @@ class ExternalModule extends Module {
579597 canMangle = true ;
580598 }
581599 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 ;
601600 case "script" :
602601 this . buildMeta . async = true ;
603602 EnvironmentNotSupportAsyncWarning . check (
@@ -614,18 +613,45 @@ class ExternalModule extends Module {
614613 "external promise"
615614 ) ;
616615 break ;
616+ case "module" :
617617 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 ;
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+ }
627638 }
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+ }
651+ }
652+
628653 break ;
654+ }
629655 }
630656 this . addDependency ( new StaticExportsDependency ( true , canMangle ) ) ;
631657 callback ( ) ;
@@ -659,36 +685,6 @@ class ExternalModule extends Module {
659685
660686 _getRequestAndExternalType ( ) {
661687 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-
692688 if ( typeof request === "object" && ! Array . isArray ( request ) )
693689 request = request [ externalType ] ;
694690 return { request, externalType } ;
@@ -753,43 +749,52 @@ class ExternalModule extends Module {
753749 runtimeTemplate
754750 ) ;
755751 }
756- case "import" :
757- return getSourceForImportExternal (
758- request ,
759- runtimeTemplate ,
760- /** @type {ImportDependencyMeta } */ ( dependencyMeta )
761- ) ;
762752 case "script" :
763753 return getSourceForScriptExternal ( request , runtimeTemplate ) ;
764- case "module" : {
765- if ( ! ( /** @type {BuildInfo } */ ( 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- }
754+ case "module" :
755+ case "import" :
756+ case "module-import" : {
757+ const type = this . getModuleImportType ( externalType ) ;
758+ if ( type === "import" ) {
775759 return getSourceForImportExternal (
776760 request ,
777761 runtimeTemplate ,
778762 /** @type {ImportDependencyMeta } */ ( dependencyMeta )
779763 ) ;
780764 }
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'"
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 ( ) ) {
784+ throw new Error (
785+ "The target environment doesn't support EcmaScriptModule syntax so it's not possible to use external type 'module'"
786+ ) ;
787+ }
788+ return getSourceForModuleExternal (
789+ request ,
790+ moduleGraph . getExportsInfo ( this ) ,
791+ runtime ,
792+ runtimeTemplate ,
793+ /** @type {ImportDependencyMeta } */ ( dependencyMeta )
784794 ) ;
785795 }
786- return getSourceForModuleExternal (
787- request ,
788- moduleGraph . getExportsInfo ( this ) ,
789- runtime ,
790- runtimeTemplate ,
791- /** @type {ImportDependencyMeta } */ ( dependencyMeta )
792- ) ;
796+
797+ break ;
793798 }
794799 case "var" :
795800 case "promise" :
0 commit comments