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: resolve conflicts for dependency location #7641

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
20 changes: 10 additions & 10 deletions crates/rspack_core/src/build_chunk_graph/code_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::{
add_connection_states, assign_depth, assign_depths, get_entry_runtime, merge_runtime,
AsyncDependenciesBlockIdentifier, ChunkGroup, ChunkGroupKind, ChunkGroupOptions, ChunkGroupUkey,
ChunkLoading, ChunkUkey, Compilation, ConnectionId, ConnectionState, DependenciesBlock,
EntryDependency, EntryRuntime, GroupOptions, Logger, ModuleDependency, ModuleGraph,
ModuleIdentifier, OriginLocation, RuntimeSpec, SyntheticDependencyLocation,
DependencyLocation, EntryDependency, EntryRuntime, GroupOptions, Logger, ModuleDependency,
ModuleGraph, ModuleIdentifier, RuntimeSpec, SyntheticDependencyLocation,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -249,7 +249,7 @@ pub(super) struct CodeSplitter<'me> {
fn add_chunk_in_group(
group_options: Option<&GroupOptions>,
module_id: ModuleIdentifier,
loc: Option<OriginLocation>,
loc: Option<DependencyLocation>,
request: Option<String>,
) -> ChunkGroup {
let options = ChunkGroupOptions::new(
Expand Down Expand Up @@ -419,9 +419,9 @@ impl<'me> CodeSplitter<'me> {
));

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

Expand Down Expand Up @@ -1248,7 +1248,7 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
let chunk_name = block.get_group_options().and_then(|o| o.name());
let entry_options = block.get_group_options().and_then(|o| o.entry_options());
let request = block.request().clone();
let loc = block.loc().cloned();
let loc = block.loc();

let cgi = if let Some(entry_options) = entry_options {
let cgi =
Expand Down Expand Up @@ -1343,7 +1343,7 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
.block_by_id(&block_id)
.expect("should have block");
let request = block.request().clone();
let loc = block.loc().cloned();
let loc = block.loc();

if self
.compilation
Expand All @@ -1367,7 +1367,7 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
let mut chunk_group = add_chunk_in_group(
block.get_group_options(),
module_id,
block.loc().map(|l| OriginLocation::Real(l.clone())),
block.loc(),
block.request().clone(),
);
let chunk = self.compilation.chunk_by_ukey.expect_get_mut(&chunk_ukey);
Expand Down Expand Up @@ -1443,7 +1443,7 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
.compilation
.chunk_group_by_ukey
.expect_get_mut(&chunk_group_ukey);
chunk_group.add_origin(None, loc.map(OriginLocation::Real), request);
chunk_group.add_origin(None, loc, request);
}
}

Expand Down
15 changes: 2 additions & 13 deletions crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,10 @@ use crate::{
use crate::{ChunkLoading, ChunkUkey, Compilation};
use crate::{LibraryOptions, ModuleIdentifier, PublicPath};

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

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

#[derive(Debug, Clone)]
pub struct OriginRecord {
pub module_id: Option<ModuleIdentifier>,
pub loc: Option<OriginLocation>,
pub loc: Option<DependencyLocation>,
shulaoda marked this conversation as resolved.
Show resolved Hide resolved
pub request: Option<String>,
}

Expand Down Expand Up @@ -313,7 +302,7 @@ impl ChunkGroup {
pub fn add_origin(
&mut self,
module_id: Option<ModuleIdentifier>,
loc: Option<OriginLocation>,
loc: Option<DependencyLocation>,
request: Option<String>,
) {
self.origins.push(OriginRecord {
Expand Down
22 changes: 11 additions & 11 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ use crate::{
AsyncDependenciesBlockIdentifier, BoxDependency, BuildContext, BuildInfo, BuildMeta,
BuildMetaDefaultObject, BuildMetaExportsType, BuildResult, ChunkGraph, ChunkGroupOptions,
CodeGenerationResult, Compilation, ConcatenationScope, ContextElementDependency,
DependenciesBlock, Dependency, DependencyCategory, DependencyId, DependencyType,
DynamicImportMode, ExportsType, FactoryMeta, FakeNamespaceObjectMode, GroupOptions,
ImportAttributes, LibIdentOptions, Module, ModuleLayer, ModuleType, Resolve, ResolveInnerOptions,
ResolveOptionsWithDependencyType, ResolverFactory, RuntimeGlobals, RuntimeSpec, SourceType,
DependenciesBlock, Dependency, DependencyCategory, DependencyId, DependencyLocation,
DependencyType, DynamicImportMode, ExportsType, FactoryMeta, FakeNamespaceObjectMode,
GroupOptions, ImportAttributes, LibIdentOptions, Module, ModuleLayer, ModuleType,
RealDependencyLocation, Resolve, ResolveInnerOptions, ResolveOptionsWithDependencyType,
ResolverFactory, RuntimeGlobals, RuntimeSpec, SourceType,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -1110,15 +1111,13 @@ impl ContextModule {
if matches!(self.options.context_options.mode, ContextMode::LazyOnce)
&& !context_element_dependencies.is_empty()
{
let loc = RealDependencyLocation::new(
self.options.context_options.start,
self.options.context_options.end,
);
let mut block = AsyncDependenciesBlock::new(
self.identifier,
Some(
(
self.options.context_options.start,
self.options.context_options.end,
)
.into(),
),
Some(DependencyLocation::Real(loc)),
None,
context_element_dependencies
.into_iter()
Expand All @@ -1132,6 +1131,7 @@ impl ContextModule {
blocks.push(Box::new(block));
} else if matches!(self.options.context_options.mode, ContextMode::Lazy) {
let mut index = 0;
// TODO(shulaoda): add loc for ContextElementDependency and AsyncDependenciesBlock
for context_element_dependency in context_element_dependencies {
let group_options = self
.options
Expand Down
65 changes: 8 additions & 57 deletions crates/rspack_core/src/dependencies_block.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::{borrow::Cow, fmt::Display, hash::Hash, sync::Arc};
use std::{borrow::Cow, hash::Hash};

use derivative::Derivative;
use rspack_collections::Identifier;
use rspack_error::{
miette::{self, Diagnostic},
thiserror::{self, Error},
};
use rspack_util::ext::DynHash;
use swc_core::common::{BytePos, SourceMap};

use crate::{
BoxDependency, Compilation, DependencyId, GroupOptions, ModuleIdentifier, RuntimeSpec,
BoxDependency, Compilation, DependencyId, DependencyLocation, GroupOptions, ModuleIdentifier,
RuntimeSpec,
};

pub trait DependenciesBlock {
Expand Down Expand Up @@ -43,53 +42,6 @@ pub fn dependencies_block_update_hash(
}
}

#[derive(Derivative)]
#[derivative(Debug, Clone)]
pub struct DependencyLocation {
start: u32,
end: u32,
#[derivative(Debug = "ignore")]
source: Option<Arc<SourceMap>>,
}

impl DependencyLocation {
pub fn new(start: u32, end: u32, source: Option<Arc<SourceMap>>) -> Self {
Self { start, end, source }
}

#[inline]
pub fn start(&self) -> u32 {
self.start
}

#[inline]
pub fn end(&self) -> u32 {
self.end
}
}

impl From<(u32, u32)> for DependencyLocation {
fn from(value: (u32, u32)) -> Self {
Self {
start: value.0,
end: value.1,
source: None,
}
}
}

impl Display for DependencyLocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(source) = &self.source {
let pos = source.lookup_char_pos(BytePos(self.start + 1));
let pos = format!("{}:{}", pos.line, pos.col.0);
f.write_str(format!("{}-{}", pos, self.end - self.start).as_str())
} else {
Ok(())
}
}
}

#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct AsyncDependenciesBlockIdentifier(Identifier);

Expand Down Expand Up @@ -124,10 +76,9 @@ impl AsyncDependenciesBlock {
dependencies: Vec<BoxDependency>,
request: Option<String>,
) -> Self {
let loc_str: Cow<str> = loc.clone().map_or_else(
|| "".into(),
|loc| format!("|loc={}:{}", loc.start(), loc.end()).into(),
);
let loc_str: Cow<str> = loc
.clone()
.map_or_else(|| "".into(), |loc| format!("|loc={loc}").into());

let modifier_str: Cow<str> = modifier.map_or_else(
|| "".into(),
Expand Down Expand Up @@ -182,8 +133,8 @@ impl AsyncDependenciesBlock {
std::mem::take(&mut self.blocks)
}

pub fn loc(&self) -> Option<&DependencyLocation> {
self.loc.as_ref()
pub fn loc(&self) -> Option<DependencyLocation> {
self.loc.clone()
}

pub fn parent(&self) -> &ModuleIdentifier {
Expand Down
65 changes: 44 additions & 21 deletions crates/rspack_core/src/dependency/dependency_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ impl RealDependencyLocation {
}
}

impl From<(u32, u32)> for RealDependencyLocation {
fn from(range: (u32, u32)) -> Self {
Self {
start: range.0,
end: range.1,
source: None,
}
}
}

impl From<swc_core::common::Span> for RealDependencyLocation {
fn from(span: swc_core::common::Span) -> Self {
Self {
Expand All @@ -38,49 +48,62 @@ impl From<swc_core::common::Span> for RealDependencyLocation {

impl fmt::Display for RealDependencyLocation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let source = &self.source.clone().expect("missing sourcemap");
let (start, end) = source.look_up_range_pos(self.start, self.end);
if let Some(source) = &self.source {
let (start, end) = source.look_up_range_pos(self.start, self.end);

if start.line == end.line {
if start.column == end.column {
return write!(f, "{}:{}", start.line, start.column);
if start.line == end.line {
if start.column == end.column {
return write!(f, "{}:{}", start.line, start.column);
}

return write!(f, "{}:{}-{}", start.line, start.column, end.column);
}

return write!(f, "{}:{}-{}", start.line, start.column, end.column);
write!(
f,
"{}:{}-{}:{}",
start.line, start.column, end.line, end.column
)
} else {
write!(f, "{}:{}", self.start, self.end)
}

write!(
f,
"{}:{}-{}:{}",
start.line, start.column, end.line, end.column
)
}
}

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

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

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

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

impl fmt::Display for DependencyLocation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let loc = match self {
DependencyLocation::Real(real) => real.to_string(),
DependencyLocation::Synthetic(synthetic) => synthetic.to_string(),
};
write!(f, "{loc}")
}
}

#[derive(Debug, Clone, Copy)]
pub struct SourcePosition {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{fmt, sync::Arc};
mod dependencies_block;
pub mod diagnostics;
pub use dependencies_block::{
AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, DependenciesBlock, DependencyLocation,
AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, DependenciesBlock,
};
mod fake_namespace_object;
mod template;
Expand Down
9 changes: 3 additions & 6 deletions crates/rspack_core/src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub use r#struct::*;

use crate::{
BoxModule, BoxRuntimeModule, Chunk, ChunkGroupOrderKey, ChunkGroupUkey, ChunkUkey, Compilation,
ExecutedRuntimeModule, LogType, ModuleGraph, ModuleIdentifier, OriginLocation, ProvidedExports,
SourceType, UsedExports,
ExecutedRuntimeModule, LogType, ModuleGraph, ModuleIdentifier, ProvidedExports, SourceType,
UsedExports,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -350,10 +350,7 @@ impl Stats<'_> {
loc: origin
.loc
.as_ref()
.map(|loc| match loc {
OriginLocation::Real(l) => format!("{l}"),
OriginLocation::Synthetic(l) => l.name.to_string(),
})
.map(|loc| loc.to_string())
.unwrap_or_default(),
request: origin.request.clone().unwrap_or_default(),
}
Expand Down
Loading
Loading