Skip to content

Commit

Permalink
feat: splitChunks support usedExports
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Aug 8, 2024
1 parent 2bd905d commit 98fa976
Show file tree
Hide file tree
Showing 16 changed files with 527 additions and 193 deletions.
2 changes: 2 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ export interface RawCacheGroupOptions {
name?: string | false | Function
reuseExistingChunk?: boolean
enforce?: boolean
usedExports?: boolean
}

export interface RawCacheGroupTestCtx {
Expand Down Expand Up @@ -1666,6 +1667,7 @@ export interface RawSplitChunksOptions {
cacheGroups?: Array<RawCacheGroupOptions>
/** What kind of chunks should be selected. */
chunks?: RegExp | 'async' | 'initial' | 'all' | Function
usedExports?: boolean
automaticNameDelimiter?: string
maxAsyncRequests?: number
maxInitialRequests?: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct RawSplitChunksOptions {
#[napi(ts_type = "RegExp | 'async' | 'initial' | 'all' | Function")]
#[derivative(Debug = "ignore")]
pub chunks: Option<Chunks>,
pub used_exports: Option<bool>,
pub automatic_name_delimiter: Option<String>,
pub max_async_requests: Option<u32>,
pub max_initial_requests: Option<u32>,
Expand Down Expand Up @@ -95,6 +96,7 @@ pub struct RawCacheGroupOptions {
// used_exports: bool,
pub reuse_existing_chunk: Option<bool>,
pub enforce: Option<bool>,
pub used_exports: Option<bool>,
}

impl From<RawSplitChunksOptions> for rspack_plugin_split_chunks::PluginOptions {
Expand Down Expand Up @@ -220,6 +222,9 @@ impl From<RawSplitChunksOptions> for rspack_plugin_split_chunks::PluginOptions {
max_initial_size,
r#type,
layer,
used_exports: v
.used_exports
.unwrap_or_else(|| raw_opts.used_exports.unwrap_or_default()),
}
}),
);
Expand Down
37 changes: 37 additions & 0 deletions crates/rspack_core/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::atomic::Ordering::Relaxed;
use std::sync::Arc;
use std::sync::LazyLock;

use either::Either;
use itertools::Itertools;
use rspack_collections::impl_item_ukey;
use rspack_collections::Ukey;
Expand Down Expand Up @@ -672,6 +673,33 @@ impl ExportsInfo {
}
}

pub fn get_usage_key(&self, mg: &ModuleGraph, runtime: Option<&RuntimeSpec>) -> UsageKey {
let exports_info = self.as_exports_info(mg);

// only expand capacity when this has redirect_to
let mut key = UsageKey(Vec::with_capacity(exports_info.exports.len() + 2));

if let Some(redirect_to) = &exports_info.redirect_to {
key.add(Either::Left(Box::new(
redirect_to.get_usage_key(mg, runtime),
)));
} else {
key.add(Either::Right(
self.other_exports_info(mg).get_used(mg, runtime),
));
};

key.add(Either::Right(
exports_info.side_effects_only_info.get_used(mg, runtime),
));

for export_info in self.ordered_exports(mg) {
key.add(Either::Right(export_info.get_used(mg, runtime)));
}

key
}

pub fn is_used(&self, mg: &ModuleGraph, runtime: Option<&RuntimeSpec>) -> bool {
let info = self.as_exports_info(mg);
if let Some(redirect_to) = info.redirect_to {
Expand Down Expand Up @@ -1607,6 +1635,15 @@ pub struct FindTargetRetValue {
pub export: Option<Vec<Atom>>,
}

#[derive(Debug, Hash, PartialEq, Eq, Default)]
pub struct UsageKey(pub(crate) Vec<Either<Box<UsageKey>, UsageState>>);

impl UsageKey {
fn add(&mut self, value: Either<Box<UsageKey>, UsageState>) {
self.0.push(value);
}
}

#[derive(Debug, Clone)]
struct UnResolvedExportInfoTarget {
connection: Option<DependencyId>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ pub struct CacheGroup {
pub max_initial_size: SplitChunkSizes,
pub filename: Option<Filename>,
pub automatic_name_delimiter: String,
pub used_exports: bool,
}
Loading

0 comments on commit 98fa976

Please sign in to comment.