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

refactor(rspack): improve dependency location #7930

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ proc-macro2 = { version = "1.0.79" }
quote = { version = "1.0.35" }
rayon = { version = "1.10.0" }
regex = { version = "1.10.4" }
rspack_resolver = { version = "0.3.3", features = ["package_json_raw_json_api"] }
ropey = "1.6.1"
rspack_resolver = { version = "0.3.3", features = ["package_json_raw_json_api"] }
rspack_sources = { version = "=0.3.2" }
rustc-hash = { version = "1.1.0" }
serde = { version = "1.0.197" }
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_core/src/build_chunk_graph/code_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
AsyncDependenciesBlockIdentifier, ChunkGroup, ChunkGroupKind, ChunkGroupOptions, ChunkGroupUkey,
ChunkLoading, ChunkUkey, Compilation, ConnectionId, ConnectionState, DependenciesBlock,
DependencyLocation, EntryDependency, EntryRuntime, GroupOptions, Logger, ModuleDependency,
ModuleGraph, ModuleIdentifier, RuntimeSpec, SyntheticDependencyLocation,
ModuleGraph, ModuleIdentifier, RuntimeSpec, SyntheticDependencyName,
};

type IndexMap<K, V, H = FxHasher> = RawIndexMap<K, V, BuildHasherDefault<H>>;
Expand Down Expand Up @@ -424,9 +424,9 @@ impl<'me> CodeSplitter<'me> {
));

for request in requests {
let loc = Some(DependencyLocation::Synthetic(
SyntheticDependencyLocation::new(&name),
));
let loc = Some(DependencyLocation::Synthetic(SyntheticDependencyName::new(
&name,
)));
entrypoint.add_origin(None, loc, request);
}

Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use crate::{
BuildMetaDefaultObject, BuildMetaExportsType, BuildResult, ChunkGraph, ChunkGroupOptions,
CodeGenerationResult, Compilation, ConcatenationScope, ContextElementDependency,
DependenciesBlock, Dependency, DependencyCategory, DependencyId, DependencyLocation,
DynamicImportMode, ExportsType, FactoryMeta, FakeNamespaceObjectMode, GroupOptions,
ImportAttributes, LibIdentOptions, Module, ModuleLayer, ModuleType, RealDependencyLocation,
Resolve, RuntimeGlobals, RuntimeSpec, SourceType,
DependencyRange, DynamicImportMode, ExportsType, FactoryMeta, FakeNamespaceObjectMode,
GroupOptions, ImportAttributes, LibIdentOptions, Module, ModuleLayer, ModuleType, Resolve,
RuntimeGlobals, RuntimeSpec, SourceType,
};

static WEBPACK_CHUNK_NAME_PLACEHOLDER: LazyLock<Regex> =
Expand Down Expand Up @@ -883,7 +883,7 @@ impl Module for ContextModule {
if matches!(self.options.context_options.mode, ContextMode::LazyOnce)
&& !context_element_dependencies.is_empty()
{
let loc = RealDependencyLocation::new(
let loc = DependencyRange::new(
self.options.context_options.start,
self.options.context_options.end,
);
Expand Down
24 changes: 12 additions & 12 deletions crates/rspack_core/src/dependency/dependency_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use derivative::Derivative;

#[derive(Derivative)]
#[derivative(Debug, Clone, Hash)]
pub struct RealDependencyLocation {
pub struct DependencyRange {
pub end: u32,
pub start: u32,
#[derivative(Debug = "ignore", Hash = "ignore")]
source: Option<Arc<dyn SourceLocation>>,
}

impl RealDependencyLocation {
impl DependencyRange {
pub fn new(start: u32, end: u32) -> Self {
RealDependencyLocation {
DependencyRange {
end,
start,
source: None,
Expand All @@ -26,7 +26,7 @@ impl RealDependencyLocation {
}
}

impl From<(u32, u32)> for RealDependencyLocation {
impl From<(u32, u32)> for DependencyRange {
fn from(range: (u32, u32)) -> Self {
Self {
start: range.0,
Expand All @@ -36,7 +36,7 @@ impl From<(u32, u32)> for RealDependencyLocation {
}
}

impl From<swc_core::common::Span> for RealDependencyLocation {
impl From<swc_core::common::Span> for DependencyRange {
fn from(span: swc_core::common::Span) -> Self {
Self {
start: span.lo.0.saturating_sub(1),
Expand All @@ -46,7 +46,7 @@ impl From<swc_core::common::Span> for RealDependencyLocation {
}
}

impl fmt::Display for RealDependencyLocation {
impl fmt::Display for DependencyRange {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(source) = &self.source {
let (start, end) = source.look_up_range_pos(self.start, self.end);
Expand All @@ -71,28 +71,28 @@ impl fmt::Display for RealDependencyLocation {
}

#[derive(Debug, Clone)]
pub struct SyntheticDependencyLocation {
pub struct SyntheticDependencyName {
pub name: String,
}

impl SyntheticDependencyLocation {
impl SyntheticDependencyName {
pub fn new(name: &str) -> Self {
SyntheticDependencyLocation {
SyntheticDependencyName {
name: name.to_string(),
}
}
}

impl fmt::Display for SyntheticDependencyLocation {
impl fmt::Display for SyntheticDependencyName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}

#[derive(Debug, Clone)]
pub enum DependencyLocation {
Real(RealDependencyLocation),
Synthetic(SyntheticDependencyLocation),
Real(DependencyRange),
Synthetic(SyntheticDependencyName),
}

impl fmt::Display for DependencyLocation {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/dependency/dependency_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use swc_core::ecma::atoms::Atom;

use super::dependency_template::AsDependencyTemplate;
use super::module_dependency::*;
use super::DependencyRange;
use super::ExportsSpec;
use super::RealDependencyLocation;
use super::{DependencyCategory, DependencyId, DependencyType};
use crate::create_exports_object_referenced;
use crate::AsContextDependency;
Expand Down Expand Up @@ -78,7 +78,7 @@ pub trait Dependency:
None
}

fn range(&self) -> Option<&RealDependencyLocation> {
fn range(&self) -> Option<&DependencyRange> {
None
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rspack_error::{
};
use rspack_util::ext::AsAny;

use crate::{BoxLoader, RealDependencyLocation};
use crate::{BoxLoader, DependencyRange};

///////////////////// Module Factory /////////////////////

Expand All @@ -18,7 +18,7 @@ use crate::{BoxLoader, RealDependencyLocation};
pub struct EmptyDependency(Box<dyn Diagnostic + Send + Sync>);

impl EmptyDependency {
pub fn new(span: RealDependencyLocation) -> Self {
pub fn new(span: DependencyRange) -> Self {
Self(
TraceableError::from_lazy_file(
span.start as usize,
Expand Down
8 changes: 3 additions & 5 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use swc_core::common::Span;
use crate::{
diagnostics::EmptyDependency, module_rules_matcher, parse_resource, resolve,
stringify_loaders_and_resource, BoxLoader, BoxModule, CompilerOptions, Context, Dependency,
DependencyCategory, FuncUseCtx, GeneratorOptions, ModuleExt, ModuleFactory,
DependencyCategory, DependencyRange, FuncUseCtx, GeneratorOptions, ModuleExt, ModuleFactory,
ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer, ModuleRuleEffect,
ModuleRuleEnforce, ModuleRuleUse, ModuleRuleUseLoader, ModuleType, NormalModule,
ParserAndGenerator, ParserOptions, RawModule, RealDependencyLocation, Resolve, ResolveArgs,
ParserAndGenerator, ParserOptions, RawModule, Resolve, ResolveArgs,
ResolveOptionsWithDependencyType, ResolveResult, Resolver, ResolverFactory, ResourceData,
ResourceParsedData, RunnerContext, SharedPluginDriver,
};
Expand Down Expand Up @@ -231,9 +231,7 @@ impl NormalModuleFactory {

if first_char.is_none() {
let span = dependency.source_span().unwrap_or_default();
return Err(
EmptyDependency::new(RealDependencyLocation::new(span.start, span.end)).into(),
);
return Err(EmptyDependency::new(DependencyRange::new(span.start, span.end)).into());
}

// See: https://webpack.js.org/concepts/loaders/#inline
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_plugin_css/src/dependency/compose.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use rspack_core::{
AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId,
DependencyType, ModuleDependency, RealDependencyLocation,
DependencyRange, DependencyType, ModuleDependency,
};

#[derive(Debug, Clone)]
pub struct CssComposeDependency {
id: DependencyId,
request: String,
range: RealDependencyLocation,
range: DependencyRange,
}

impl CssComposeDependency {
pub fn new(request: String, range: RealDependencyLocation) -> Self {
pub fn new(request: String, range: DependencyRange) -> Self {
Self {
id: DependencyId::new(),
request,
Expand All @@ -33,7 +33,7 @@ impl Dependency for CssComposeDependency {
&DependencyType::CssCompose
}

fn range(&self) -> Option<&RealDependencyLocation> {
fn range(&self) -> Option<&DependencyRange> {
Some(&self.range)
}

Expand Down
12 changes: 6 additions & 6 deletions crates/rspack_plugin_css/src/dependency/import.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use rspack_core::{
AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId,
DependencyTemplate, DependencyType, ModuleDependency, RealDependencyLocation, RuntimeSpec,
TemplateContext, TemplateReplaceSource,
AsContextDependency, Compilation, Dependency, DependencyCategory, DependencyId, DependencyRange,
DependencyTemplate, DependencyType, ModuleDependency, RuntimeSpec, TemplateContext,
TemplateReplaceSource,
};

#[derive(Debug, Clone)]
pub struct CssImportDependency {
id: DependencyId,
request: String,
range: RealDependencyLocation,
range: DependencyRange,
}

impl CssImportDependency {
pub fn new(request: String, range: RealDependencyLocation) -> Self {
pub fn new(request: String, range: DependencyRange) -> Self {
Self {
id: DependencyId::new(),
request,
Expand All @@ -34,7 +34,7 @@ impl Dependency for CssImportDependency {
&DependencyType::CssImport
}

fn range(&self) -> Option<&RealDependencyLocation> {
fn range(&self) -> Option<&DependencyRange> {
Some(&self.range)
}

Expand Down
10 changes: 5 additions & 5 deletions crates/rspack_plugin_css/src/dependency/url.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rspack_core::{
AsContextDependency, CodeGenerationDataFilename, CodeGenerationDataUrl, Compilation, Dependency,
DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ModuleDependency,
ModuleIdentifier, PublicPath, RealDependencyLocation, RuntimeSpec, TemplateContext,
DependencyCategory, DependencyId, DependencyRange, DependencyTemplate, DependencyType,
ModuleDependency, ModuleIdentifier, PublicPath, RuntimeSpec, TemplateContext,
TemplateReplaceSource,
};

Expand All @@ -11,12 +11,12 @@ use crate::utils::{css_escape_string, AUTO_PUBLIC_PATH_PLACEHOLDER};
pub struct CssUrlDependency {
id: DependencyId,
request: String,
range: RealDependencyLocation,
range: DependencyRange,
replace_function: bool,
}

impl CssUrlDependency {
pub fn new(request: String, range: RealDependencyLocation, replace_function: bool) -> Self {
pub fn new(request: String, range: DependencyRange, replace_function: bool) -> Self {
Self {
request,
range,
Expand Down Expand Up @@ -64,7 +64,7 @@ impl Dependency for CssUrlDependency {
&DependencyType::CssUrl
}

fn range(&self) -> Option<&RealDependencyLocation> {
fn range(&self) -> Option<&DependencyRange> {
Some(&self.range)
}

Expand Down
14 changes: 7 additions & 7 deletions crates/rspack_plugin_css/src/parser_and_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use rspack_core::{
diagnostics::map_box_diagnostics_to_module_parse_diagnostics,
rspack_sources::{BoxSource, ConcatSource, RawSource, ReplaceSource, Source, SourceExt},
BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, Compilation, ConstDependency,
CssExportsConvention, Dependency, DependencyId, DependencyTemplate, GenerateContext,
LocalIdentName, Module, ModuleDependency, ModuleGraph, ModuleIdentifier, ModuleType,
NormalModule, ParseContext, ParseResult, ParserAndGenerator, RealDependencyLocation, RuntimeSpec,
SourceType, TemplateContext, UsageState,
CssExportsConvention, Dependency, DependencyId, DependencyRange, DependencyTemplate,
GenerateContext, LocalIdentName, Module, ModuleDependency, ModuleGraph, ModuleIdentifier,
ModuleType, NormalModule, ParseContext, ParseResult, ParserAndGenerator, RuntimeSpec, SourceType,
TemplateContext, UsageState,
};
use rspack_core::{ModuleInitFragments, RuntimeGlobals};
use rspack_error::{
Expand Down Expand Up @@ -161,7 +161,7 @@ impl ParserAndGenerator for CssParserAndGenerator {
let request = normalize_url(request);
let dep = Box::new(CssUrlDependency::new(
request,
RealDependencyLocation::new(range.start, range.end),
DependencyRange::new(range.start, range.end),
matches!(kind, css_module_lexer::UrlRangeKind::Function),
));
dependencies.push(dep.clone());
Expand All @@ -186,7 +186,7 @@ impl ParserAndGenerator for CssParserAndGenerator {
);
dependencies.push(Box::new(CssImportDependency::new(
request.to_string(),
RealDependencyLocation::new(range.start, range.end),
DependencyRange::new(range.start, range.end),
)));
}
css_module_lexer::Dependency::Replace { content, range } => presentational_dependencies
Expand Down Expand Up @@ -280,7 +280,7 @@ impl ParserAndGenerator for CssParserAndGenerator {
let from = from.trim_matches(|c| c == '\'' || c == '"');
let dep = CssComposeDependency::new(
from.to_string(),
RealDependencyLocation::new(range.start, range.end),
DependencyRange::new(range.start, range.end),
);
dep_id = Some(*dep.id());
dependencies.push(Box::new(dep));
Expand Down
8 changes: 4 additions & 4 deletions crates/rspack_plugin_extract_css/src/css_dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use rspack_collections::IdentifierSet;
use rspack_core::{
AffectType, AsContextDependency, AsDependencyTemplate, ConnectionState, Dependency,
DependencyCategory, DependencyId, ModuleDependency, ModuleGraph, RealDependencyLocation,
DependencyCategory, DependencyId, DependencyRange, ModuleDependency, ModuleGraph,
};
use rustc_hash::FxHashSet;

Expand All @@ -26,7 +26,7 @@ pub struct CssDependency {
// determine module's postOrderIndex
// @TODO(shulaoda) Does this have any additional side effects?
// pub(crate) order_index: u32,
range: RealDependencyLocation,
range: DependencyRange,
resource_identifier: String,
pub(crate) cacheable: bool,
pub(crate) file_dependencies: FxHashSet<PathBuf>,
Expand All @@ -46,7 +46,7 @@ impl CssDependency {
supports: Option<String>,
source_map: Option<String>,
identifier_index: u32,
range: RealDependencyLocation,
range: DependencyRange,
cacheable: bool,
file_dependencies: FxHashSet<PathBuf>,
context_dependencies: FxHashSet<PathBuf>,
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Dependency for CssDependency {
// it can keep the right order, but Rspack uses HashSet,
// when determining the postOrderIndex, Rspack uses
// dependency span to set correct order
fn range(&self) -> Option<&RealDependencyLocation> {
fn range(&self) -> Option<&DependencyRange> {
Some(&self.range)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_extract_css/src/parser_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rspack_core::{BoxDependency, RealDependencyLocation};
use rspack_core::{BoxDependency, DependencyRange};
use rspack_plugin_javascript::{visitors::JavascriptParser, JavascriptParserPlugin};
use rspack_util::fx_hash::FxDashMap;
use serde::Deserialize;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl JavascriptParserPlugin for PluginCssExtractParserPlugin {
supports.clone(),
source_map.clone(),
*identifier_index,
RealDependencyLocation::new(index as u32, (index + 1) as u32),
DependencyRange::new(index as u32, (index + 1) as u32),
parser.build_info.cacheable,
parser.build_info.file_dependencies.clone(),
parser.build_info.context_dependencies.clone(),
Expand Down
Loading
Loading