Skip to content

Commit e079ed8

Browse files
authored
feat: allow external modules placed in async chunks (#11421)
1 parent 644f227 commit e079ed8

File tree

19 files changed

+87
-28
lines changed

19 files changed

+87
-28
lines changed

crates/node_binding/napi-binding.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,7 @@ export interface RawExternalItemFnResult {
21442144
export interface RawExternalsPluginOptions {
21452145
type: string
21462146
externals: (string | RegExp | Record<string, string | boolean | string[] | Record<string, string[]>> | ((...args: any[]) => any))[]
2147+
placeInInitial: boolean
21472148
}
21482149

21492150
export interface RawExternalsPresets {

crates/rspack/src/builder/builder_context.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rspack_core::{
1212
#[repr(u8)]
1313
pub(super) enum BuiltinPluginOptions {
1414
// External handling plugins
15-
ExternalsPlugin((ExternalType, Vec<ExternalItem>)),
15+
ExternalsPlugin((ExternalType, Vec<ExternalItem>, bool)),
1616
NodeTargetPlugin,
1717
ElectronTargetPlugin(rspack_plugin_externals::ElectronTargetContext),
1818
HttpExternalsRspackPlugin((bool /* css */, bool /* web_async */)),
@@ -111,9 +111,11 @@ impl BuilderContext {
111111
let mut plugins = Vec::new();
112112
self.plugins.drain(..).for_each(|plugin| match plugin {
113113
// External handling plugins
114-
BuiltinPluginOptions::ExternalsPlugin((external_type, externals)) => {
115-
plugins
116-
.push(rspack_plugin_externals::ExternalsPlugin::new(external_type, externals).boxed());
114+
BuiltinPluginOptions::ExternalsPlugin((external_type, externals, place_in_initial)) => {
115+
plugins.push(
116+
rspack_plugin_externals::ExternalsPlugin::new(external_type, externals, place_in_initial)
117+
.boxed(),
118+
);
117119
}
118120
BuiltinPluginOptions::NodeTargetPlugin => {
119121
plugins.push(rspack_plugin_externals::node_target_plugin())

crates/rspack/src/builder/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ impl CompilerOptionsBuilder {
10841084
.push(BuiltinPluginOptions::ExternalsPlugin((
10851085
expect!(self.externals_type.clone()),
10861086
externals,
1087+
false,
10871088
)));
10881089
}
10891090

@@ -1135,6 +1136,7 @@ impl CompilerOptionsBuilder {
11351136
.push(BuiltinPluginOptions::ExternalsPlugin((
11361137
"node-commonjs".to_string(),
11371138
vec!["nw.gui".to_string().into()],
1139+
false,
11381140
)));
11391141
}
11401142

crates/rspack_binding_api/src/raw_options/raw_builtins/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,12 @@ impl<'a> BuiltinPlugin<'a> {
364364
.map(|e| RawExternalItemWrapper(e).try_into())
365365
.collect::<Result<Vec<_>>>()
366366
.map_err(|report| napi::Error::from_reason(report.to_string()))?;
367-
let plugin = ExternalsPlugin::new(plugin_options.r#type, externals).boxed();
367+
let plugin = ExternalsPlugin::new(
368+
plugin_options.r#type,
369+
externals,
370+
plugin_options.place_in_initial,
371+
)
372+
.boxed();
368373
plugins.push(plugin);
369374
}
370375
BuiltinPluginName::NodeTargetPlugin => plugins.push(node_target_plugin()),

crates/rspack_binding_api/src/raw_options/raw_external.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct RawExternalsPluginOptions {
3636
ts_type = "(string | RegExp | Record<string, string | boolean | string[] | Record<string, string[]>> | ((...args: any[]) => any))[]"
3737
)]
3838
pub externals: Vec<RawExternalItem>,
39+
pub place_in_initial: bool,
3940
}
4041

4142
type RawExternalItem = Either4<

crates/rspack_core/src/external_module.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ pub struct ExternalModule {
209209
build_info: BuildInfo,
210210
build_meta: BuildMeta,
211211
dependency_meta: DependencyMeta,
212+
place_in_initial: bool,
212213
}
213214

214215
#[cacheable]
@@ -234,6 +235,7 @@ impl ExternalModule {
234235
external_type: ExternalType,
235236
user_request: String,
236237
dependency_meta: DependencyMeta,
238+
place_in_initial: bool,
237239
) -> Self {
238240
Self {
239241
dependencies: Vec::new(),
@@ -255,6 +257,7 @@ impl ExternalModule {
255257
build_meta: Default::default(),
256258
source_map_kind: SourceMapKind::empty(),
257259
dependency_meta,
260+
place_in_initial,
258261
}
259262
}
260263

@@ -765,15 +768,15 @@ impl Module for ExternalModule {
765768
}
766769

767770
fn chunk_condition(&self, chunk_key: &ChunkUkey, compilation: &Compilation) -> Option<bool> {
768-
if self.external_type == "css-import" {
769-
return Some(true);
771+
match self.external_type.as_str() {
772+
"css-import" | "module" | "import" | "module-import" if !self.place_in_initial => Some(true),
773+
_ => Some(
774+
compilation
775+
.chunk_graph
776+
.get_number_of_entry_modules(chunk_key)
777+
> 0,
778+
),
770779
}
771-
Some(
772-
compilation
773-
.chunk_graph
774-
.get_number_of_entry_modules(chunk_key)
775-
> 0,
776-
)
777780
}
778781

779782
fn source(&self) -> Option<&BoxSource> {

crates/rspack_plugin_dll/src/dll_reference/dll_reference_agency_plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl Plugin for DllReferenceAgencyPlugin {
6767

6868
let external = ExternalItem::Object(external_item_object);
6969

70-
ExternalsPlugin::new(source_type.unwrap_or("var".into()), vec![external]).apply(ctx)?;
70+
ExternalsPlugin::new(source_type.unwrap_or("var".into()), vec![external], false).apply(ctx)?;
7171

7272
DelegatedPlugin::new(DelegatedPluginOptions {
7373
source: source.clone(),

crates/rspack_plugin_externals/src/electron_target_plugin.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub fn electron_target_plugin(context: ElectronTargetContext, plugins: &mut Vec<
4141
.into_iter()
4242
.map(|i| ExternalItem::String(i.to_string()))
4343
.collect(),
44+
false,
4445
)
4546
.boxed(),
4647
);
@@ -68,6 +69,7 @@ pub fn electron_target_plugin(context: ElectronTargetContext, plugins: &mut Vec<
6869
.into_iter()
6970
.map(|i| ExternalItem::String(i.to_string()))
7071
.collect(),
72+
false,
7173
)
7274
.boxed(),
7375
),
@@ -78,6 +80,7 @@ pub fn electron_target_plugin(context: ElectronTargetContext, plugins: &mut Vec<
7880
.into_iter()
7981
.map(|i| ExternalItem::String(i.to_string()))
8082
.collect(),
83+
false,
8184
)
8285
.boxed(),
8386
),

crates/rspack_plugin_externals/src/http_externals_plugin.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ use crate::ExternalsPlugin;
66

77
pub fn http_externals_rspack_plugin(css: bool, web_async: bool) -> BoxPlugin {
88
if web_async {
9-
ExternalsPlugin::new("import".to_owned(), vec![http_external_item_web_async(css)]).boxed()
9+
ExternalsPlugin::new(
10+
"import".to_owned(),
11+
vec![http_external_item_web_async(css)],
12+
false,
13+
)
14+
.boxed()
1015
} else {
11-
ExternalsPlugin::new("module".to_owned(), vec![http_external_item_web(css)]).boxed()
16+
ExternalsPlugin::new(
17+
"module".to_owned(),
18+
vec![http_external_item_web(css)],
19+
false,
20+
)
21+
.boxed()
1222
}
1323
}
1424

crates/rspack_plugin_externals/src/node_target_plugin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub fn node_target_plugin() -> BoxPlugin {
6565
// Yarn PnP adds pnpapi as "builtin"
6666
ExternalItem::from("pnpapi".to_string()),
6767
],
68+
false,
6869
)
6970
.boxed()
7071
}

0 commit comments

Comments
 (0)