Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/node_binding/napi-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,7 @@ export interface RawEnvironment {

export interface RawEsmLibraryPlugin {
preserveModules?: string
splitChunks?: RawSplitChunksOptions
}

export interface RawEvalDevToolModulePluginOptions {
Expand Down
9 changes: 7 additions & 2 deletions crates/rspack_binding_api/src/raw_options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,13 @@ impl<'a> BuiltinPlugin<'a> {
BuiltinPluginName::EsmLibraryPlugin => {
let options = downcast_into::<RawEsmLibraryPlugin>(self.options)
.map_err(|report| napi::Error::from_reason(report.to_string()))?;
plugins
.push(EsmLibraryPlugin::new(options.preserve_modules.as_deref().map(Into::into)).boxed());
plugins.push(
EsmLibraryPlugin::new(
options.preserve_modules.as_deref().map(Into::into),
options.split_chunks.map(Into::into),
)
.boxed(),
);
}
BuiltinPluginName::ArrayPushCallbackChunkFormatPlugin => {
plugins.push(ArrayPushCallbackChunkFormatPlugin::default().boxed());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
use std::sync::Arc;

use napi::bindgen_prelude::Either3;
use rayon::iter::Either;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_esm_library::EsmLibraryPlugin;
use rspack_plugin_split_chunks::CacheGroup;
use rspack_regex::RspackRegex;

use crate::{
module::ModuleObject,
raw_options::{self, RawSplitChunksOptions},
};

#[napi(object, object_to_js = false)]
pub struct RawEsmLibraryPlugin {
pub struct RawEsmLibraryPlugin<'a> {
pub preserve_modules: Option<String>,
pub split_chunks: Option<RawSplitChunksOptions<'a>>,
}

impl<'a> From<RawSplitChunksOptions<'a>> for Vec<CacheGroup> {
fn from(value: RawSplitChunksOptions<'a>) -> Self {
let mut groups = rspack_plugin_split_chunks::PluginOptions::from(value).cache_groups;

groups.sort_by(|a, b| a.priority.total_cmp(&b.priority));

groups
}
}
8 changes: 8 additions & 0 deletions crates/rspack_core/src/chunk_graph/chunk_graph_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ impl ChunkGraphModule {
}

impl ChunkGraph {
pub fn modules(&self) -> IdentifierSet {
self
.chunk_graph_module_by_module_identifier
.keys()
.copied()
.collect()
}

pub fn add_module(&mut self, module_identifier: ModuleIdentifier) {
self
.chunk_graph_module_by_module_identifier
Expand Down
23 changes: 13 additions & 10 deletions crates/rspack_plugin_esm_library/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ repository.workspace = true
version.workspace = true

[dependencies]
async-trait = { workspace = true }
atomic_refcell = { workspace = true }
rayon = { workspace = true }
regex = { workspace = true }
rspack_cacheable = { workspace = true }
rspack_collections = { workspace = true }
rspack_core = { workspace = true }
Expand All @@ -25,13 +21,20 @@ rspack_hook = { workspace = true }
rspack_javascript_compiler = { workspace = true }
rspack_plugin_javascript = { workspace = true }
rspack_plugin_runtime = { workspace = true }
rspack_plugin_split_chunks = { workspace = true }
rspack_util = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
sugar_path = { workspace = true }
swc_core = { workspace = true, features = ["swc_ecma_transforms_base"] }
tokio = { workspace = true }
tracing = { workspace = true }

async-trait = { workspace = true }
atomic_refcell = { workspace = true }
futures = { workspace = true }
rayon = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
sugar_path = { workspace = true }
swc_core = { workspace = true, features = ["swc_ecma_transforms_base"] }
tokio = { workspace = true }
tracing = { workspace = true }

[lints]
workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_plugin_esm_library/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ mod plugin;
mod preserve_modules;
mod render;
mod runtime;

mod split_chunks;
pub use plugin::EsmLibraryPlugin;
pub use split_chunks::{GetNameGetter, ModuleFilter, ModuleTypeFilter};
12 changes: 9 additions & 3 deletions crates/rspack_plugin_esm_library/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use rspack_plugin_javascript::{
JavascriptModulesRenderChunkContent, JsPlugin, RenderSource,
dependency::ImportDependencyTemplate, parser_and_generator::JavaScriptParserAndGenerator,
};
use rspack_plugin_split_chunks::CacheGroup;
use rspack_util::fx_hash::FxHashMap;
use sugar_path::SugarPath;
use tokio::sync::RwLock;
Expand All @@ -39,6 +40,8 @@ pub static RSPACK_ESM_RUNTIME_CHUNK: &str = "RSPACK_ESM_RUNTIME";
#[derive(Debug, Default)]
pub struct EsmLibraryPlugin {
pub(crate) preserve_modules: Option<PathBuf>,
pub(crate) split_chunks: Option<Vec<CacheGroup>>,

// module instance will hold this map till compile done, we can't mutate it,
// normal concatenateModule just read the info from it
// the Arc here is to for module_codegen API, which needs to render module in parallel
Expand All @@ -51,9 +54,10 @@ pub struct EsmLibraryPlugin {
}

impl EsmLibraryPlugin {
pub fn new(preserve_modules: Option<PathBuf>) -> Self {
pub fn new(preserve_modules: Option<PathBuf>, split_chunks: Option<Vec<CacheGroup>>) -> Self {
Self::new_inner(
preserve_modules,
split_chunks,
Default::default(),
Default::default(),
Default::default(),
Expand Down Expand Up @@ -485,10 +489,12 @@ async fn optimize_chunks(&self, compilation: &mut Compilation) -> Result<Option<
if !errors.is_empty() {
compilation.extend_diagnostics(errors);
}
} else {
ensure_entry_exports(compilation);
} else if let Some(cache_groups) = &self.split_chunks {
crate::split_chunks::split(cache_groups, compilation).await?;
}

ensure_entry_exports(compilation);

Ok(None)
}

Expand Down
Loading
Loading