Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: experiments layers #7330

Merged
merged 5 commits into from
Jul 29, 2024
Merged
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
7 changes: 7 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export class JsStatsModule {
get type(): string
get moduleType(): string
get identifier(): string
get layer(): string | undefined
get name(): string
get id(): string | undefined
get chunks(): Array<string | undefined | null>
Expand Down Expand Up @@ -509,6 +510,7 @@ export interface JsModule {
rawRequest?: string
factoryMeta?: JsFactoryMeta
type: string
layer?: string
}

export interface JsNormalModuleFactoryCreateModuleArgs {
Expand Down Expand Up @@ -814,6 +816,7 @@ export interface RawCacheGroupOptions {
/** What kind of chunks should be selected. */
chunks?: RegExp | 'async' | 'initial' | 'all'
type?: RegExp | string
layer?: RegExp | string
automaticNameDelimiter?: string
minChunks?: number
minSize?: number | RawSplitChunkSizes
Expand Down Expand Up @@ -973,6 +976,7 @@ export interface RawEntryOptions {
filename?: JsFilename
library?: RawLibraryOptions
dependOn?: Array<string>
layer?: string
}

export interface RawEntryPluginOptions {
Expand All @@ -993,6 +997,7 @@ export interface RawEvalDevToolModulePluginOptions {
}

export interface RawExperiments {
layers: boolean
topLevelAwait: boolean
rspackFuture: RawRspackFuture
}
Expand Down Expand Up @@ -1227,10 +1232,12 @@ export interface RawModuleRule {
sideEffects?: boolean
use?: RawModuleRuleUse[] | ((arg: RawFuncUseCtx) => RawModuleRuleUse[])
type?: string
layer?: string
parser?: RawParserOptions
generator?: RawGeneratorOptions
resolve?: RawResolveOptions
issuer?: RawRuleSetCondition
issuerLayer?: RawRuleSetCondition
dependency?: RawRuleSetCondition
scheme?: RawRuleSetCondition
mimetype?: RawRuleSetCondition
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_binding_options/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl TryFrom<RawOptions> for CompilerOptions {
},
emit_asset: true,
},
layers: value.experiments.layers,
top_level_await: value.experiments.top_level_await,
rspack_future: value.experiments.rspack_future.into(),
};
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_options/src/options/raw_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct RawEntryOptions {
pub filename: Option<JsFilename>,
pub library: Option<RawLibraryOptions>,
pub depend_on: Option<Vec<String>>,
pub layer: Option<String>,
}

impl From<RawEntryOptions> for EntryOptions {
Expand All @@ -56,6 +57,7 @@ impl From<RawEntryOptions> for EntryOptions {
filename: value.filename.map(Into::into),
library: value.library.map(Into::into),
depend_on: value.depend_on.map(Into::into),
layer: value.layer,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct RawRspackFuture {}
#[derive(Debug, Default)]
#[napi(object)]
pub struct RawExperiments {
pub layers: bool,
pub top_level_await: bool,
pub rspack_future: RawRspackFuture,
}
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ pub struct RawModuleRule {
#[napi(ts_type = "RawModuleRuleUse[] | ((arg: RawFuncUseCtx) => RawModuleRuleUse[])")]
pub r#use: Option<Either<Vec<RawModuleRuleUse>, ThreadsafeUse>>,
pub r#type: Option<String>,
pub layer: Option<String>,
pub parser: Option<RawParserOptions>,
pub generator: Option<RawGeneratorOptions>,
pub resolve: Option<RawResolveOptions>,
pub issuer: Option<RawRuleSetCondition>,
pub issuer_layer: Option<RawRuleSetCondition>,
pub dependency: Option<RawRuleSetCondition>,
pub scheme: Option<RawRuleSetCondition>,
pub mimetype: Option<RawRuleSetCondition>,
Expand Down Expand Up @@ -755,11 +757,13 @@ impl TryFrom<RawModuleRule> for ModuleRule {
description_data,
r#use: uses.transpose()?.unwrap_or_default(),
r#type: module_type,
layer: value.layer,
parser: value.parser.map(|raw| raw.into()),
generator: value.generator.map(|raw| raw.into()),
resolve: value.resolve.map(|raw| raw.try_into()).transpose()?,
side_effects: value.side_effects,
issuer: value.issuer.map(|raw| raw.try_into()).transpose()?,
issuer_layer: value.issuer_layer.map(|raw| raw.try_into()).transpose()?,
dependency: value.dependency.map(|raw| raw.try_into()).transpose()?,
scheme: value.scheme.map(|raw| raw.try_into()).transpose()?,
mimetype: value.mimetype.map(|raw| raw.try_into()).transpose()?,
Expand Down
35 changes: 35 additions & 0 deletions crates/rspack_binding_options/src/options/raw_split_chunks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub struct RawCacheGroupOptions {
#[napi(ts_type = "RegExp | string")]
#[derivative(Debug = "ignore")]
pub r#type: Option<Either<JsRegExp, JsString>>,
#[napi(ts_type = "RegExp | string")]
#[derivative(Debug = "ignore")]
pub layer: Option<Either<JsRegExp, JsString>>,
pub automatic_name_delimiter: Option<String>,
// pub max_async_requests: usize,
// pub max_initial_requests: usize,
Expand Down Expand Up @@ -178,6 +181,11 @@ impl From<RawSplitChunksOptions> for rspack_plugin_split_chunks::PluginOptions {
.map(create_module_type_filter)
.unwrap_or_else(rspack_plugin_split_chunks::create_default_module_type_filter);

let layer = v
.layer
.map(create_module_layer_filter)
.unwrap_or_else(rspack_plugin_split_chunks::create_default_module_layer_filter);

let mut name = v.name.map_or(default_chunk_option_name(), |name| {
normalize_raw_chunk_name(name)
});
Expand Down Expand Up @@ -211,6 +219,7 @@ impl From<RawSplitChunksOptions> for rspack_plugin_split_chunks::PluginOptions {
max_async_size,
max_initial_size,
r#type,
layer,
}
}),
);
Expand Down Expand Up @@ -282,3 +291,29 @@ fn create_module_type_filter(
}
}
}

fn create_module_layer_filter(
raw: Either<JsRegExp, JsString>,
) -> rspack_plugin_split_chunks::ModuleLayerFilter {
match raw {
Either::A(js_reg) => {
let regex = js_reg.to_rspack_regex();
Arc::new(move |m| {
m.get_layer()
.map(|layer| regex.test(layer))
.unwrap_or_default()
})
}
Either::B(js_str) => {
let test = js_str.into_string();
Arc::new(move |m| {
let layer = m.get_layer();
if let Some(layer) = layer {
layer.starts_with(&test)
} else {
test.is_empty()
}
})
}
}
}
11 changes: 10 additions & 1 deletion crates/rspack_binding_values/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct JsModule {
pub raw_request: Option<String>,
pub factory_meta: Option<JsFactoryMeta>,
pub r#type: String,
pub layer: Option<String>,
}

pub trait ToJsModule {
Expand All @@ -41,14 +42,16 @@ impl ToJsModule for dyn Module {
let module_identifier = || self.identifier().to_string();
let context = || self.get_context().map(|c| c.to_string());
let module_type = || self.module_type().to_string();
let module_layer = || self.get_layer().cloned();

self
.try_as_normal_module()
.map(|normal_module| JsModule {
context: context(),
original_source: original_source(),
resource: Some(normal_module.resource_resolved_data().resource.to_string()),
r#type: normal_module.module_type().to_string(),
r#type: module_type(),
layer: module_layer(),
module_identifier: module_identifier(),
name_for_condition: name_for_condition(),
request: Some(normal_module.request().to_string()),
Expand All @@ -64,6 +67,7 @@ impl ToJsModule for dyn Module {
self.try_as_raw_module().map(|_| JsModule {
context: context(),
r#type: module_type(),
layer: module_layer(),
original_source: original_source(),
resource: None,
module_identifier: module_identifier(),
Expand All @@ -79,6 +83,7 @@ impl ToJsModule for dyn Module {
context: context(),
original_source: original_source(),
r#type: module_type(),
layer: module_layer(),
resource: None,
module_identifier: module_identifier(),
name_for_condition: name_for_condition(),
Expand All @@ -93,6 +98,7 @@ impl ToJsModule for dyn Module {
context: context(),
original_source: original_source(),
r#type: module_type(),
layer: module_layer(),
resource: None,
module_identifier: module_identifier(),
name_for_condition: name_for_condition(),
Expand All @@ -107,6 +113,8 @@ impl ToJsModule for dyn Module {
context: context(),
module_identifier: module_identifier(),
name_for_condition: name_for_condition(),
layer: module_layer(),
r#type: module_type(),
..Default::default()
})
})
Expand All @@ -120,6 +128,7 @@ impl ToJsModule for CompilerModuleContext {
module_identifier: self.module_identifier.to_string(),
name_for_condition: self.name_for_condition.clone(),
r#type: self.r#type.to_string(),
layer: self.layer.clone(),
resource: self.resource_data.as_ref().map(|r| r.resource.to_string()),
original_source: None,
request: self.request.clone(),
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_values/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ pub struct JsStatsModule {
r#type: &'static str,
module_type: &'static str,
identifier: &'static str,
layer: Option<String>,
name: String,
id: Option<String>,
chunks: Vec<Option<String>>,
Expand Down Expand Up @@ -431,6 +432,7 @@ impl TryFrom<StatsModule<'_>> for JsStatsModule {
depth: stats_module.depth.map(|d| d as u32),
chunks: stats_module.chunks,
module_type: stats_module.module_type.as_str(),
layer: stats_module.layer.map(|i| i.into_owned()),
identifier: stats_module.identifier.as_str(),
id: stats_module.id.map(|i| i.to_owned()),
dependent: stats_module.dependent,
Expand Down
7 changes: 5 additions & 2 deletions crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rustc_hash::FxHashMap as HashMap;
use crate::{
compare_chunk_group, get_chunk_from_ukey, get_chunk_group_from_ukey, Chunk, ChunkByUkey,
ChunkGroupByUkey, ChunkGroupUkey, DependencyLocation, DynamicImportFetchPriority, Filename,
ModuleLayer,
};
use crate::{ChunkLoading, ChunkUkey, Compilation};
use crate::{LibraryOptions, ModuleIdentifier, PublicPath};
Expand Down Expand Up @@ -439,7 +440,7 @@ impl EntryRuntime {

// pub type EntryRuntime = String;

#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq)]
pub struct EntryOptions {
pub name: Option<String>,
pub runtime: Option<EntryRuntime>,
Expand All @@ -450,6 +451,7 @@ pub struct EntryOptions {
pub filename: Option<Filename>,
pub library: Option<LibraryOptions>,
pub depend_on: Option<Vec<String>>,
pub layer: Option<ModuleLayer>,
}

impl EntryOptions {
Expand All @@ -474,6 +476,7 @@ impl EntryOptions {
merge_field!(filename);
merge_field!(library);
merge_field!(depend_on);
merge_field!(layer);
Ok(())
}

Expand Down Expand Up @@ -535,7 +538,7 @@ impl ChunkGroupOptions {
}
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum GroupOptions {
Entrypoint(Box<EntryOptions>),
ChunkGroup(ChunkGroupOptions),
Expand Down
12 changes: 9 additions & 3 deletions crates/rspack_core/src/compiler/make/repair/factorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{
module_graph::ModuleGraphModule,
utils::task_loop::{Task, TaskResult, TaskType},
BoxDependency, CompilerOptions, Context, DependencyId, ExportInfo, ExportsInfo, ModuleFactory,
ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleProfile, Resolve,
UsageState,
ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer, ModuleProfile,
Resolve, UsageState,
};

#[derive(Debug)]
Expand All @@ -21,6 +21,7 @@ pub struct FactorizeTask {
pub original_module_source: Option<BoxSource>,
pub original_module_context: Option<Box<Context>>,
pub issuer: Option<Box<str>>,
pub issuer_layer: Option<ModuleLayer>,
pub dependency: BoxDependency,
pub dependencies: Vec<DependencyId>,
pub resolve_options: Option<Box<Resolve>>,
Expand All @@ -38,7 +39,6 @@ impl Task<MakeTaskContext> for FactorizeTask {
current_profile.mark_factory_start();
}
let dependency = self.dependency;
// let dep_id = *dependency.id();

let context = if let Some(context) = dependency.get_context()
&& !context.is_empty()
Expand All @@ -53,6 +53,11 @@ impl Task<MakeTaskContext> for FactorizeTask {
}
.clone();

let issuer_layer = dependency
.get_layer()
.or(self.issuer_layer.as_ref())
.cloned();

let other_exports_info = ExportInfo::new(None, UsageState::Unknown, None);
let side_effects_only_info = ExportInfo::new(
Some("*side effects only*".into()),
Expand Down Expand Up @@ -86,6 +91,7 @@ impl Task<MakeTaskContext> for FactorizeTask {
dependency,
issuer: self.issuer,
issuer_identifier: self.original_module_identifier,
issuer_layer,

file_dependencies: Default::default(),
missing_dependencies: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/compiler/make/repair/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub fn repair(
issuer: parent_module
.and_then(|m| m.as_normal_module())
.and_then(|module| module.name_for_condition()),
issuer_layer: parent_module.and_then(|m| m.get_layer()).cloned(),
original_module_context: parent_module.and_then(|m| m.get_context()),
dependency: dependency.clone(),
dependencies: vec![id],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl Task<MakeTaskContext> for ProcessDependenciesTask {
issuer: module
.as_normal_module()
.and_then(|module| module.name_for_condition()),
issuer_layer: module.get_layer().cloned(),
dependency,
dependencies,
resolve_options: module.get_resolve_options(),
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/compiler/module_executor/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Task<MakeTaskContext> for EntryTask {
original_module_identifier: None,
original_module_source: None,
issuer: None,
issuer_layer: None,
original_module_context: None,
dependency: dep,
dependencies: vec![dep_id],
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/compiler/module_executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl Task<MakeTaskContext> for ExecuteTask {
filename: None,
library: None,
depend_on: None,
layer: None,
}),
});

Expand Down
Loading
Loading