Skip to content

Commit

Permalink
feat: support context module critical (#6331)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Apr 24, 2024
1 parent d140f5f commit ec727ba
Show file tree
Hide file tree
Showing 29 changed files with 618 additions and 163 deletions.
4 changes: 3 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,8 @@ export interface RawJavascriptParserOptions {
dynamicImportPreload: string
dynamicImportPrefetch: string
url: string
exprContextCritical: boolean
wrappedContextCritical: boolean
}

export interface RawLibraryAuxiliaryComment {
Expand Down Expand Up @@ -1118,7 +1120,7 @@ export interface RawOutputOptions {
}

export interface RawParserOptions {
type: "asset" | "css" | "css/auto" | "css/module" | "javascript"
type: "asset" | "css" | "css/auto" | "css/module" | "javascript" | "javascript/auto" | "javascript/dynamic" | "javascript/esm"
asset?: RawAssetParserOptions
css?: RawCssParserOptions
cssAuto?: RawCssAutoParserOptions
Expand Down
22 changes: 15 additions & 7 deletions crates/rspack_binding_options/src/options/raw_module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ pub struct RawModuleRule {
#[serde(rename_all = "camelCase")]
#[napi(object)]
pub struct RawParserOptions {
#[napi(ts_type = r#""asset" | "css" | "css/auto" | "css/module" | "javascript""#)]
#[napi(
ts_type = r#""asset" | "css" | "css/auto" | "css/module" | "javascript" | "javascript/auto" | "javascript/dynamic" | "javascript/esm""#
)]
pub r#type: String,
pub asset: Option<RawAssetParserOptions>,
pub css: Option<RawCssParserOptions>,
Expand All @@ -292,12 +294,14 @@ impl From<RawParserOptions> for ParserOptions {
.expect("should have an \"asset\" when RawParserOptions.type is \"asset\"")
.into(),
),
"javascript" => Self::Javascript(
value
.javascript
.expect("should have an \"javascript\" when RawParserOptions.type is \"javascript\"")
.into(),
),
"javascript" | "javascript/auto" | "javascript/dynamic" | "javascript/esm" => {
Self::Javascript(
value
.javascript
.expect("should have an \"javascript\" when RawParserOptions.type is \"javascript\"")
.into(),
)
}
"css" => Self::Css(
value
.css
Expand Down Expand Up @@ -332,6 +336,8 @@ pub struct RawJavascriptParserOptions {
pub dynamic_import_preload: String,
pub dynamic_import_prefetch: String,
pub url: String,
pub expr_context_critical: bool,
pub wrapped_context_critical: bool,
}

impl From<RawJavascriptParserOptions> for JavascriptParserOptions {
Expand All @@ -341,6 +347,8 @@ impl From<RawJavascriptParserOptions> for JavascriptParserOptions {
dynamic_import_preload: JavascriptParserOrder::from(value.dynamic_import_preload.as_str()),
dynamic_import_prefetch: JavascriptParserOrder::from(value.dynamic_import_prefetch.as_str()),
url: JavascriptParserUrl::from(value.url.as_str()),
expr_context_critical: value.expr_context_critical,
wrapped_context_critical: value.wrapped_context_critical,
}
}
}
Expand Down
17 changes: 14 additions & 3 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use crate::{
BuildContext, BuildInfo, BuildMeta, BuildMetaDefaultObject, BuildMetaExportsType, BuildResult,
ChunkGraph, ChunkGroupOptions, CodeGenerationResult, Compilation, ConcatenationScope,
ContextElementDependency, DependenciesBlock, Dependency, DependencyCategory, DependencyId,
ExportsType, FactoryMeta, FakeNamespaceObjectMode, GroupOptions, LibIdentOptions, Module,
ModuleType, Resolve, ResolveInnerOptions, ResolveOptionsWithDependencyType, ResolverFactory,
RuntimeGlobals, RuntimeSpec, SourceType,
DynamicImportMode, ExportsType, FactoryMeta, FakeNamespaceObjectMode, GroupOptions,
LibIdentOptions, Module, ModuleType, Resolve, ResolveInnerOptions,
ResolveOptionsWithDependencyType, ResolverFactory, RuntimeGlobals, RuntimeSpec, SourceType,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -77,6 +77,17 @@ impl From<&str> for ContextMode {
}
}

impl From<DynamicImportMode> for ContextMode {
fn from(value: DynamicImportMode) -> Self {
match value {
DynamicImportMode::Lazy => Self::Lazy,
DynamicImportMode::Weak => Self::AsyncWeak,
DynamicImportMode::Eager => Self::Eager,
DynamicImportMode::LazyOnce => Self::LazyOnce,
}
}
}

pub fn try_convert_str_to_context_mode(s: &str) -> Option<ContextMode> {
match s {
"sync" => Some(ContextMode::Sync),
Expand Down
71 changes: 27 additions & 44 deletions crates/rspack_core/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ pub enum ParserOptions {
}

macro_rules! get_variant {
($fn_name:ident, $variant:ident, $module_variant:ident, $ret_ty:ident) => {
pub fn $fn_name(&self, module_type: &ModuleType) -> Option<&$ret_ty> {
($fn_name:ident, $variant:ident, $ret_ty:ident) => {
pub fn $fn_name(&self) -> Option<&$ret_ty> {
match self {
Self::$variant(value) if *module_type == ModuleType::$module_variant => Some(value),
Self::$variant(value) => Some(value),
_ => None,
}
}
};
}

impl ParserOptions {
get_variant!(get_asset, Asset, Asset, AssetParserOptions);
get_variant!(get_css, Css, Css, CssParserOptions);
get_variant!(get_css_auto, CssAuto, CssAuto, CssAutoParserOptions);
get_variant!(get_css_module, CssModule, CssModule, CssModuleParserOptions);
get_variant!(get_javascript, Javascript, Js, JavascriptParserOptions);
get_variant!(get_asset, Asset, AssetParserOptions);
get_variant!(get_css, Css, CssParserOptions);
get_variant!(get_css_auto, CssAuto, CssAutoParserOptions);
get_variant!(get_css_module, CssModule, CssModuleParserOptions);
get_variant!(get_javascript, Javascript, JavascriptParserOptions);
}

#[derive(Debug, Clone, Copy, Default, MergeFrom)]
Expand Down Expand Up @@ -139,6 +139,8 @@ pub struct JavascriptParserOptions {
pub dynamic_import_preload: JavascriptParserOrder,
pub dynamic_import_prefetch: JavascriptParserOrder,
pub url: JavascriptParserUrl,
pub expr_context_critical: bool,
pub wrapped_context_critical: bool,
}

#[derive(Debug, Clone, MergeFrom)]
Expand Down Expand Up @@ -199,66 +201,47 @@ pub enum GeneratorOptions {
}

impl GeneratorOptions {
get_variant!(get_asset, Asset, Asset, AssetGeneratorOptions);
get_variant!(
get_asset_inline,
AssetInline,
AssetInline,
AssetInlineGeneratorOptions
);
get_variant!(get_asset, Asset, AssetGeneratorOptions);
get_variant!(get_asset_inline, AssetInline, AssetInlineGeneratorOptions);
get_variant!(
get_asset_resource,
AssetResource,
AssetResource,
AssetResourceGeneratorOptions
);
get_variant!(get_css, Css, Css, CssGeneratorOptions);
get_variant!(get_css_auto, CssAuto, CssAuto, CssAutoGeneratorOptions);
get_variant!(
get_css_module,
CssModule,
CssModule,
CssModuleGeneratorOptions
);
get_variant!(get_css, Css, CssGeneratorOptions);
get_variant!(get_css_auto, CssAuto, CssAutoGeneratorOptions);
get_variant!(get_css_module, CssModule, CssModuleGeneratorOptions);

pub fn asset_filename(&self, module_type: &ModuleType) -> Option<&Filename> {
pub fn asset_filename(&self) -> Option<&Filename> {
self
.get_asset(module_type)
.get_asset()
.and_then(|x| x.filename.as_ref())
.or_else(|| {
self
.get_asset_resource(module_type)
.and_then(|x| x.filename.as_ref())
})
.or_else(|| self.get_asset_resource().and_then(|x| x.filename.as_ref()))
}

pub fn asset_public_path(&self, module_type: &ModuleType) -> Option<&PublicPath> {
pub fn asset_public_path(&self) -> Option<&PublicPath> {
self
.get_asset(module_type)
.get_asset()
.and_then(|x| x.public_path.as_ref())
.or_else(|| {
self
.get_asset_resource(module_type)
.get_asset_resource()
.and_then(|x| x.public_path.as_ref())
})
}

pub fn asset_data_url(&self, module_type: &ModuleType) -> Option<&AssetGeneratorDataUrl> {
pub fn asset_data_url(&self) -> Option<&AssetGeneratorDataUrl> {
self
.get_asset(module_type)
.get_asset()
.and_then(|x| x.data_url.as_ref())
.or_else(|| {
self
.get_asset_inline(module_type)
.and_then(|x| x.data_url.as_ref())
})
.or_else(|| self.get_asset_inline().and_then(|x| x.data_url.as_ref()))
}

pub fn asset_emit(&self, module_type: &ModuleType) -> Option<bool> {
pub fn asset_emit(&self) -> Option<bool> {
self
.get_asset(module_type)
.get_asset()
.and_then(|x| x.emit)
.or_else(|| self.get_asset_resource(module_type).and_then(|x| x.emit))
.or_else(|| self.get_asset_resource().and_then(|x| x.emit))
}
}

Expand Down
20 changes: 9 additions & 11 deletions crates/rspack_plugin_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rspack_core::{
AssetGeneratorDataUrl, AssetGeneratorDataUrlFnArgs, AssetParserDataUrl, BuildExtraDataType,
BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, ChunkUkey, CodeGenerationDataAssetInfo,
CodeGenerationDataFilename, CodeGenerationDataUrl, Compilation, CompilationRenderManifest,
CompilerOptions, GenerateContext, Module, ModuleGraph, ModuleType, NormalModule, ParseContext,
CompilerOptions, GenerateContext, Module, ModuleGraph, NormalModule, ParseContext,
ParserAndGenerator, PathData, Plugin, RenderManifestEntry, ResourceData, RuntimeGlobals,
SourceType, NAMESPACE_OBJECT_EXPORT,
};
Expand Down Expand Up @@ -274,7 +274,6 @@ impl ParserAndGenerator for AssetParserAndGenerator {
source,
build_meta,
build_info,
module_type,
compiler_options,
module_identifier,
..
Expand All @@ -291,7 +290,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
let limit_size = parse_context
.module_parser_options
.and_then(|x| {
x.get_asset(module_type)
x.get_asset()
.and_then(|x| x.data_url_condition.as_ref())
.and_then(|x| match x {
AssetParserDataUrl::Options(x) => x.max_size,
Expand Down Expand Up @@ -337,7 +336,6 @@ impl ParserAndGenerator for AssetParserAndGenerator {
generate_context: &mut GenerateContext,
) -> Result<BoxSource> {
let compilation = generate_context.compilation;
let module_type = module.module_type();
let parsed_asset_config = self
.parsed_asset_config
.as_ref()
Expand All @@ -352,7 +350,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
let resource_data: &ResourceData = normal_module.resource_resolved_data();
let data_url = generate_context
.module_generator_options
.and_then(|x| x.asset_data_url(module_type));
.and_then(|x| x.asset_data_url());

let encoded_source: String;

Expand Down Expand Up @@ -381,7 +379,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
// Use [Rule.generator.filename] if it is set, otherwise use [output.assetModuleFilename].
let asset_filename_template = generate_context
.module_generator_options
.and_then(|x| x.asset_filename(module_type))
.and_then(|x| x.asset_filename())
.unwrap_or(&compilation.options.output.asset_module_filename);

let contenthash = self.hash_for_source(source, &compilation.options);
Expand All @@ -400,7 +398,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {

let asset_path = if let Some(public_path) = generate_context
.module_generator_options
.and_then(|x| x.asset_public_path(module_type))
.and_then(|x| x.asset_public_path())
{
let public_path = public_path.render(compilation, &filename);
serde_json::to_string(&format!("{public_path}{filename}"))
Expand All @@ -419,7 +417,7 @@ impl ParserAndGenerator for AssetParserAndGenerator {
filename,
generate_context
.module_generator_options
.and_then(|x| x.asset_public_path(module_type))
.and_then(|x| x.asset_public_path())
.unwrap_or_else(|| &compilation.options.output.public_path)
.clone(),
));
Expand Down Expand Up @@ -596,11 +594,11 @@ impl Plugin for AssetPlugin {
rspack_core::ModuleType::Asset,
Box::new(move |parser_options, generator_options| {
let data_url_condition = parser_options
.and_then(|x| x.get_asset(&ModuleType::Asset))
.and_then(|x| x.get_asset())
.and_then(|x| x.data_url_condition.clone());

let emit: Option<bool> = generator_options
.and_then(|x| x.get_asset(&ModuleType::Asset))
.and_then(|x| x.get_asset())
.and_then(|x| x.emit);

Box::new(AssetParserAndGenerator::with_auto(
Expand All @@ -619,7 +617,7 @@ impl Plugin for AssetPlugin {
rspack_core::ModuleType::AssetResource,
Box::new(move |_, generator_options| {
let emit = generator_options
.and_then(|x| x.get_asset_resource(&ModuleType::AssetResource))
.and_then(|x| x.get_asset_resource())
.and_then(|x| x.emit);

Box::new(AssetParserAndGenerator::with_resource(emit.unwrap_or(true)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ impl Plugin for CssPlugin {
ModuleType::Css,
Box::new(|p, g| {
let p = p
.and_then(|p| p.get_css(&ModuleType::Css))
.and_then(|p| p.get_css())
.expect("should have CssParserOptions");
let g = g
.and_then(|g| g.get_css(&ModuleType::Css))
.and_then(|g| g.get_css())
.expect("should have CssGeneratorOptions");
Box::new(CssParserAndGenerator {
exports: None,
Expand All @@ -350,10 +350,10 @@ impl Plugin for CssPlugin {
ModuleType::CssModule,
Box::new(|p, g| {
let p = p
.and_then(|p| p.get_css_module(&ModuleType::CssModule))
.and_then(|p| p.get_css_module())
.expect("should have CssModuleParserOptions");
let g = g
.and_then(|g| g.get_css_module(&ModuleType::CssModule))
.and_then(|g| g.get_css_module())
.expect("should have CssModuleGeneratorOptions");
Box::new(CssParserAndGenerator {
exports: None,
Expand All @@ -374,10 +374,10 @@ impl Plugin for CssPlugin {
ModuleType::CssAuto,
Box::new(|p, g| {
let p = p
.and_then(|p| p.get_css_auto(&ModuleType::CssAuto))
.and_then(|p| p.get_css_auto())
.expect("should have CssAutoParserOptions");
let g = g
.and_then(|g| g.get_css_auto(&ModuleType::CssAuto))
.and_then(|g| g.get_css_auto())
.expect("should have CssAutoGeneratorOptions");
Box::new(CssParserAndGenerator {
exports: None,
Expand Down
Loading

2 comments on commit ec727ba

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs, self-hosted, Linux, ci ✅ success
_selftest, ubuntu-latest ✅ success
nx, ubuntu-latest ✅ success
rspress, ubuntu-latest ✅ success
rsbuild, ubuntu-latest ✅ success
compat, ubuntu-latest ✅ success
examples, ubuntu-latest ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-04-24 332b127) Current Change
10000_development-mode + exec 2.66 s ± 28 ms 2.69 s ± 40 ms +0.94 %
10000_development-mode_hmr + exec 682 ms ± 3.7 ms 686 ms ± 6.9 ms +0.59 %
10000_production-mode + exec 2.46 s ± 31 ms 2.56 s ± 44 ms +3.99 %
arco-pro_development-mode + exec 2.48 s ± 86 ms 2.51 s ± 54 ms +1.10 %
arco-pro_development-mode_hmr + exec 429 ms ± 1.2 ms 432 ms ± 3.1 ms +0.74 %
arco-pro_development-mode_hmr_intercept-plugin + exec 441 ms ± 5.4 ms 443 ms ± 6.2 ms +0.50 %
arco-pro_development-mode_intercept-plugin + exec 3.22 s ± 88 ms 3.26 s ± 63 ms +1.24 %
arco-pro_production-mode + exec 3.95 s ± 74 ms 3.98 s ± 77 ms +0.91 %
arco-pro_production-mode_intercept-plugin + exec 4.66 s ± 238 ms 4.83 s ± 83 ms +3.66 %
threejs_development-mode_10x + exec 2.05 s ± 16 ms 2.09 s ± 25 ms +1.65 %
threejs_development-mode_10x_hmr + exec 753 ms ± 13 ms 761 ms ± 18 ms +1.02 %
threejs_production-mode_10x + exec 5.14 s ± 40 ms 5.18 s ± 29 ms +0.80 %

Please sign in to comment.