Skip to content

Commit

Permalink
feat: lazy context dep critical
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Sep 11, 2024
1 parent 29c9ebe commit 212d4e8
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 74 deletions.
2 changes: 2 additions & 0 deletions crates/node_binding/src/plugins/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,8 @@ impl ContextModuleFactoryBeforeResolve for ContextModuleFactoryBeforeResolveTap
request: d.request,
reg_exp,
recursive: d.recursive,
// TODO
dependencies: vec![],
};
Ok(BeforeResolveResult::Data(Box::new(data)))
}
Expand Down
10 changes: 6 additions & 4 deletions crates/rspack_core/src/context_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use rspack_regex::RspackRegex;
use tracing::instrument;

use crate::{
resolve, ContextModule, ContextModuleOptions, DependencyCategory, ModuleExt, ModuleFactory,
ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, RawModule, ResolveArgs,
ResolveOptionsWithDependencyType, ResolveResult, Resolver, ResolverFactory, SharedPluginDriver,
resolve, ContextModule, ContextModuleOptions, DependencyCategory, DependencyId, ModuleExt,
ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, RawModule,
ResolveArgs, ResolveOptionsWithDependencyType, ResolveResult, Resolver, ResolverFactory,
SharedPluginDriver,
};

#[derive(Clone)]
Expand All @@ -25,7 +26,7 @@ pub struct BeforeResolveData {
pub context: String,
pub request: Option<String>,
// assertions
// dependencies
pub dependencies: Vec<DependencyId>,
// dependency_type
// file_dependencies
// missing_dependencies
Expand Down Expand Up @@ -132,6 +133,7 @@ impl ContextModuleFactory {
request: data.request().map(|r| r.to_string()),
recursive: dependency_options.recursive,
reg_exp: dependency_options.reg_exp.clone(),
dependencies: vec![*dependency.id()],
};

match self
Expand Down
14 changes: 13 additions & 1 deletion crates/rspack_core/src/dependency/context_dependency.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{ContextOptions, ContextTypePrefix, Dependency};
use rspack_error::Diagnostic;

use crate::{ContextOptions, ContextTypePrefix, Dependency, ModuleGraph};

pub trait ContextDependency: Dependency {
fn request(&self) -> &str;
Expand All @@ -12,6 +14,16 @@ pub trait ContextDependency: Dependency {
}

fn type_prefix(&self) -> ContextTypePrefix;

fn critical(&self) -> &Option<Diagnostic>;
fn critical_mut(&mut self) -> &mut Option<Diagnostic>;

fn get_diagnostics(&self, _module_graph: &ModuleGraph) -> Option<Vec<Diagnostic>> {
if let Some(critical) = self.critical() {
return Some(vec![critical.clone()]);
}
None
}
}

pub trait AsContextDependency {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rspack_core::{
AsModuleDependency, Compilation, ContextDependency, RealDependencyLocation, RuntimeSpec,
AsModuleDependency, Compilation, ContextDependency, ContextOptions, Dependency,
DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ErrorSpan,
RealDependencyLocation, RuntimeSpec, TemplateContext, TemplateReplaceSource,
};
use rspack_core::{ContextOptions, Dependency, TemplateReplaceSource};
use rspack_core::{DependencyCategory, DependencyId, DependencyTemplate};
use rspack_core::{DependencyType, ErrorSpan, TemplateContext};
use rspack_error::Diagnostic;

use super::{
context_dependency_template_as_require_call, create_resource_identifier_for_context_dependency,
Expand All @@ -17,6 +17,7 @@ pub struct CommonJsRequireContextDependency {
resource_identifier: String,
options: ContextOptions,
optional: bool,
critical: Option<Diagnostic>,
}

impl CommonJsRequireContextDependency {
Expand All @@ -34,6 +35,7 @@ impl CommonJsRequireContextDependency {
resource_identifier,
optional,
id: DependencyId::new(),
critical: None,
}
}
}
Expand Down Expand Up @@ -88,6 +90,14 @@ impl ContextDependency for CommonJsRequireContextDependency {
fn type_prefix(&self) -> rspack_core::ContextTypePrefix {
rspack_core::ContextTypePrefix::Normal
}

fn critical(&self) -> &Option<Diagnostic> {
&self.critical
}

fn critical_mut(&mut self) -> &mut Option<Diagnostic> {
&mut self.critical
}
}

impl DependencyTemplate for CommonJsRequireContextDependency {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rspack_core::{
AsModuleDependency, Compilation, ContextDependency, RealDependencyLocation, RuntimeSpec,
AsModuleDependency, Compilation, ContextDependency, ContextOptions, Dependency,
DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ErrorSpan,
RealDependencyLocation, RuntimeSpec, TemplateContext, TemplateReplaceSource,
};
use rspack_core::{ContextOptions, Dependency, TemplateReplaceSource};
use rspack_core::{DependencyCategory, DependencyId, DependencyTemplate};
use rspack_core::{DependencyType, ErrorSpan, TemplateContext};
use rspack_error::Diagnostic;

use super::{
context_dependency_template_as_require_call, create_resource_identifier_for_context_dependency,
Expand All @@ -17,6 +17,7 @@ pub struct ImportContextDependency {
range_callee: (u32, u32),
resource_identifier: String,
optional: bool,
critical: Option<Diagnostic>,
}

impl ImportContextDependency {
Expand All @@ -34,6 +35,7 @@ impl ImportContextDependency {
id: DependencyId::new(),
resource_identifier,
optional,
critical: None,
}
}
}
Expand Down Expand Up @@ -88,6 +90,14 @@ impl ContextDependency for ImportContextDependency {
fn type_prefix(&self) -> rspack_core::ContextTypePrefix {
rspack_core::ContextTypePrefix::Import
}

fn critical(&self) -> &Option<Diagnostic> {
&self.critical
}

fn critical_mut(&mut self) -> &mut Option<Diagnostic> {
&mut self.critical
}
}

impl DependencyTemplate for ImportContextDependency {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use rspack_core::{
module_raw, AsModuleDependency, Compilation, ContextDependency, RealDependencyLocation,
RuntimeSpec,
module_raw, AsModuleDependency, Compilation, ContextDependency, ContextOptions, Dependency,
DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ErrorSpan,
RealDependencyLocation, RuntimeSpec, TemplateContext, TemplateReplaceSource,
};
use rspack_core::{ContextOptions, Dependency, DependencyCategory, DependencyId};
use rspack_core::{DependencyTemplate, DependencyType, ErrorSpan};
use rspack_core::{TemplateContext, TemplateReplaceSource};
use rspack_error::TraceableError;

use super::create_resource_identifier_for_context_dependency;

Expand All @@ -15,6 +14,7 @@ pub struct ImportMetaContextDependency {
range: RealDependencyLocation,
resource_identifier: String,
optional: bool,
critical: Option<TraceableError>,
}

impl ImportMetaContextDependency {
Expand All @@ -26,6 +26,7 @@ impl ImportMetaContextDependency {
resource_identifier,
optional,
id: DependencyId::new(),
critical: None,
}
}
}
Expand Down Expand Up @@ -80,6 +81,14 @@ impl ContextDependency for ImportMetaContextDependency {
fn type_prefix(&self) -> rspack_core::ContextTypePrefix {
rspack_core::ContextTypePrefix::Normal
}

fn critical(&self) -> &Option<TraceableError> {
&self.critical
}

fn critical_mut(&mut self) -> &mut Option<TraceableError> {
&mut self.critical
}
}

impl DependencyTemplate for ImportMetaContextDependency {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use rspack_core::{
module_raw, AsModuleDependency, Compilation, ContextDependency, RealDependencyLocation,
RuntimeSpec,
module_raw, AsModuleDependency, Compilation, ContextDependency, ContextOptions, Dependency,
DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ErrorSpan,
RealDependencyLocation, RuntimeSpec, TemplateContext, TemplateReplaceSource,
};
use rspack_core::{ContextOptions, Dependency, DependencyCategory, DependencyId};
use rspack_core::{DependencyTemplate, DependencyType, ErrorSpan};
use rspack_core::{TemplateContext, TemplateReplaceSource};
use rspack_error::Diagnostic;

use super::create_resource_identifier_for_context_dependency;

Expand All @@ -15,6 +14,7 @@ pub struct RequireContextDependency {
range: RealDependencyLocation,
resource_identifier: String,
optional: bool,
critical: Option<Diagnostic>,
}

impl RequireContextDependency {
Expand All @@ -26,6 +26,7 @@ impl RequireContextDependency {
id: DependencyId::new(),
resource_identifier,
optional,
critical: None,
}
}
}
Expand Down Expand Up @@ -80,6 +81,14 @@ impl ContextDependency for RequireContextDependency {
fn type_prefix(&self) -> rspack_core::ContextTypePrefix {
rspack_core::ContextTypePrefix::Normal
}

fn critical(&self) -> &Option<Diagnostic> {
&self.critical
}

fn critical_mut(&mut self) -> &mut Option<Diagnostic> {
&mut self.critical
}
}

impl DependencyTemplate for RequireContextDependency {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use rspack_core::{
ConstDependency, ContextMode, DependencyCategory, RealDependencyLocation, SpanExt,
ConstDependency, ContextDependency, ContextMode, DependencyCategory, RealDependencyLocation,
SpanExt,
};
use rspack_core::{ContextNameSpaceObject, ContextOptions};
use rspack_error::Severity;
use rspack_error::{DiagnosticExt, Severity};
use swc_core::common::{Span, Spanned};
use swc_core::ecma::ast::{CallExpr, Expr, Ident, Lit, MemberExpr, UnaryExpr};

Expand Down Expand Up @@ -44,7 +45,10 @@ fn create_commonjs_require_context_dependency(
referenced_exports: None,
attributes: None,
};
CommonJsRequireContextDependency::new(options, span.into(), (start, end), parser.in_try)
let mut dep =
CommonJsRequireContextDependency::new(options, span.into(), (start, end), parser.in_try);
*dep.critical_mut() = result.critical;
dep
}

pub struct CommonJsImportsParserPlugin;
Expand Down Expand Up @@ -200,7 +204,7 @@ impl CommonJsImportsParserPlugin {
) -> Option<bool> {
let start = ident.span().real_lo();
let end = ident.span().real_hi();
let dep = CommonJsRequireContextDependency::new(
let mut dep = CommonJsRequireContextDependency::new(
ContextOptions {
mode: ContextMode::Sync,
recursive: true,
Expand All @@ -222,16 +226,18 @@ impl CommonJsImportsParserPlugin {
(start, end),
parser.in_try,
);
parser.warning_diagnostics.push(Box::new(
*dep.critical_mut() = Some(
create_traceable_error(
"Critical dependency".into(),
"require function is used in a way in which dependencies cannot be statically extracted"
.to_string(),
parser.source_file,
ident.span().into(),
)
.with_severity(Severity::Warn),
));
.with_severity(Severity::Warn)
.boxed()
.into(),
);
parser.dependencies.push(Box::new(dep));
Some(true)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use itertools::Itertools;
use rspack_core::{
AsyncDependenciesBlock, DependencyLocation, DynamicImportMode, GroupOptions, ImportAttributes,
RealDependencyLocation,
AsyncDependenciesBlock, ContextDependency, DependencyLocation, DynamicImportMode, GroupOptions,
ImportAttributes, RealDependencyLocation,
};
use rspack_core::{ChunkGroupOptions, DynamicImportFetchPriority};
use rspack_core::{ContextNameSpaceObject, ContextOptions, DependencyCategory, SpanExt};
Expand Down Expand Up @@ -147,41 +147,42 @@ impl JavascriptParserPlugin for ImportParserPlugin {
query,
fragment,
replaces,
critical,
} = create_context_dependency(&param, &dyn_imported.expr, parser);
let reg_exp = context_reg_exp(&reg, "", Some(dyn_imported.span().into()), parser);
parser
.dependencies
.push(Box::new(ImportContextDependency::new(
ContextOptions {
mode: mode.into(),
recursive: true,
reg_exp,
include,
exclude,
category: DependencyCategory::Esm,
request: format!("{}{}{}", context.clone(), query, fragment),
context,
namespace_object: if parser.build_meta.strict_harmony_module {
ContextNameSpaceObject::Strict
} else {
ContextNameSpaceObject::Bool(true)
},
group_options: Some(GroupOptions::ChunkGroup(ChunkGroupOptions::new(
chunk_name,
chunk_preload,
chunk_prefetch,
fetch_priority,
))),
replaces,
start: node.span().real_lo(),
end: node.span().real_hi(),
referenced_exports: exports,
attributes,
let mut dep = ImportContextDependency::new(
ContextOptions {
mode: mode.into(),
recursive: true,
reg_exp,
include,
exclude,
category: DependencyCategory::Esm,
request: format!("{}{}{}", context.clone(), query, fragment),
context,
namespace_object: if parser.build_meta.strict_harmony_module {
ContextNameSpaceObject::Strict
} else {
ContextNameSpaceObject::Bool(true)
},
node.span().into(),
(import_call.span.real_lo(), import_call.span.real_hi()),
parser.in_try,
)));
group_options: Some(GroupOptions::ChunkGroup(ChunkGroupOptions::new(
chunk_name,
chunk_preload,
chunk_prefetch,
fetch_priority,
))),
replaces,
start: node.span().real_lo(),
end: node.span().real_hi(),
referenced_exports: exports,
attributes,
},
node.span().into(),
(import_call.span.real_lo(), import_call.span.real_hi()),
parser.in_try,
);
*dep.critical_mut() = critical;
parser.dependencies.push(Box::new(dep));
Some(true)
}
}
Expand Down
Loading

0 comments on commit 212d4e8

Please sign in to comment.