diff --git a/crates/rspack_core/src/external_module.rs b/crates/rspack_core/src/external_module.rs index 5581182baca8..0b4732e1bbe7 100644 --- a/crates/rspack_core/src/external_module.rs +++ b/crates/rspack_core/src/external_module.rs @@ -533,16 +533,23 @@ impl Module for ExternalModule { async fn build( &mut self, - _build_context: BuildContext, + build_context: BuildContext, _: Option<&Compilation>, ) -> Result { + self.build_info.module = build_context.compiler_options.output.module; let resolved_external_type = self.resolve_external_type(); // TODO add exports_type for request match resolved_external_type { "this" => self.build_info.strict = false, "system" => self.build_meta.exports_type = BuildMetaExportsType::Namespace, - "module" => self.build_meta.exports_type = BuildMetaExportsType::Namespace, + "module" => { + self.build_meta.exports_type = BuildMetaExportsType::Namespace; + // align with https://github.com/webpack/webpack/blob/3919c844eca394d73ca930e4fc5506fb86e2b094/lib/ExternalModule.js#L597 + if !self.build_info.module { + self.build_meta.has_top_level_await = true; + } + } "script" | "promise" => self.build_meta.has_top_level_await = true, "import" => { self.build_meta.has_top_level_await = true; diff --git a/crates/rspack_core/src/module.rs b/crates/rspack_core/src/module.rs index 65dd19282d0b..36f81d9a02f5 100644 --- a/crates/rspack_core/src/module.rs +++ b/crates/rspack_core/src/module.rs @@ -68,6 +68,7 @@ pub struct BuildInfo { pub top_level_declarations: Option>, pub module_concatenation_bailout: Option, pub assets: HashMap, + pub module: bool, } impl Default for BuildInfo { @@ -87,6 +88,7 @@ impl Default for BuildInfo { top_level_declarations: None, module_concatenation_bailout: None, assets: Default::default(), + module: false, } } } @@ -168,6 +170,7 @@ impl Display for ExportsArgument { #[serde(rename_all = "camelCase")] pub struct BuildMeta { pub strict_esm_module: bool, + // same as is_async https://github.com/webpack/webpack/blob/3919c844eca394d73ca930e4fc5506fb86e2b094/lib/Module.js#L107 pub has_top_level_await: bool, pub esm: bool, pub exports_type: BuildMetaExportsType,