diff --git a/crates/rspack_core/src/build_chunk_graph/code_splitter.rs b/crates/rspack_core/src/build_chunk_graph/code_splitter.rs index 3c3ebea31cb..18ba43df735 100644 --- a/crates/rspack_core/src/build_chunk_graph/code_splitter.rs +++ b/crates/rspack_core/src/build_chunk_graph/code_splitter.rs @@ -21,9 +21,9 @@ use crate::incremental::{IncrementalPasses, Mutation}; use crate::{ assign_depths, get_entry_runtime, merge_runtime, AsyncDependenciesBlockIdentifier, ChunkGroup, ChunkGroupKind, ChunkGroupOptions, ChunkGroupUkey, ChunkLoading, ChunkUkey, Compilation, - ConnectionState, DependenciesBlock, DependencyId, DependencyLocation, DependencyName, - EntryDependency, EntryRuntime, GroupOptions, Logger, ModuleDependency, ModuleGraph, - ModuleIdentifier, RuntimeSpec, + ConnectionState, DependenciesBlock, DependencyId, DependencyLocation, EntryDependency, + EntryRuntime, GroupOptions, Logger, ModuleDependency, ModuleGraph, ModuleIdentifier, RuntimeSpec, + SyntheticDependencyLocation, }; type IndexMap = RawIndexMap>; @@ -407,7 +407,9 @@ impl CodeSplitter { )); for request in requests { - let loc = Some(DependencyLocation::Synthetic(DependencyName::new(name))); + let loc = Some(DependencyLocation::Synthetic( + SyntheticDependencyLocation::new(name), + )); entrypoint.add_origin(None, loc, request); } diff --git a/crates/rspack_core/src/compiler/make/repair/factorize.rs b/crates/rspack_core/src/compiler/make/repair/factorize.rs index ee09b30f11c..69582af1da6 100644 --- a/crates/rspack_core/src/compiler/make/repair/factorize.rs +++ b/crates/rspack_core/src/compiler/make/repair/factorize.rs @@ -128,7 +128,10 @@ impl Task for FactorizeTask { return Err(e); } let mut diagnostics = Vec::with_capacity(create_data.diagnostics.len() + 1); - diagnostics.push(Into::::into(e).with_loc(create_data.dependencies[0].loc())); + diagnostics.push( + Into::::into(e) + .with_loc(create_data.dependencies[0].loc().map(|loc| loc.to_string())), + ); diagnostics.append(&mut create_data.diagnostics); // Continue bundling if `options.bail` set to `false`. Ok(vec![Box::new( diff --git a/crates/rspack_core/src/context_module.rs b/crates/rspack_core/src/context_module.rs index a9a8119671d..29e8412eb50 100644 --- a/crates/rspack_core/src/context_module.rs +++ b/crates/rspack_core/src/context_module.rs @@ -24,9 +24,9 @@ use crate::{ BuildMetaDefaultObject, BuildMetaExportsType, BuildResult, ChunkGraph, ChunkGroupOptions, CodeGenerationResult, Compilation, ConcatenationScope, ContextElementDependency, DependenciesBlock, Dependency, DependencyCategory, DependencyId, DependencyLocation, - DependencyRange, DynamicImportMode, ExportsType, FactoryMeta, FakeNamespaceObjectMode, - GroupOptions, ImportAttributes, LibIdentOptions, Module, ModuleId, ModuleLayer, ModuleType, - Resolve, RuntimeGlobals, RuntimeSpec, SourceType, + DynamicImportMode, ExportsType, FactoryMeta, FakeNamespaceObjectMode, GroupOptions, + ImportAttributes, LibIdentOptions, Module, ModuleId, ModuleLayer, ModuleType, + RealDependencyLocation, Resolve, RuntimeGlobals, RuntimeSpec, SourceType, }; static WEBPACK_CHUNK_NAME_INDEX_PLACEHOLDER: &str = "[index]"; @@ -879,13 +879,17 @@ impl Module for ContextModule { if matches!(self.options.context_options.mode, ContextMode::LazyOnce) && !context_element_dependencies.is_empty() { - let loc = DependencyRange::new( - self.options.context_options.start, - self.options.context_options.end, - ); + let loc = DependencyLocation::Real(RealDependencyLocation::new( + ( + self.options.context_options.start, + self.options.context_options.end, + ) + .into(), + None, + )); let mut block = AsyncDependenciesBlock::new( (*self.identifier).into(), - Some(DependencyLocation::Real(loc)), + Some(loc), None, context_element_dependencies .into_iter() diff --git a/crates/rspack_core/src/dependency/dependency_location.rs b/crates/rspack_core/src/dependency/dependency_location.rs index 39cb267f296..46364906394 100644 --- a/crates/rspack_core/src/dependency/dependency_location.rs +++ b/crates/rspack_core/src/dependency/dependency_location.rs @@ -1,29 +1,17 @@ -use std::{fmt, sync::Arc}; +use std::{ + fmt::{self, Debug}, + sync::Arc, +}; use derivative::Derivative; +/// Represents a range in a dependency, typically used for tracking the span of code in a source file. +/// It stores the start and end positions (as offsets) of the range, typically using base-0 indexing. #[derive(Derivative)] #[derivative(Debug, Clone, Hash)] pub struct DependencyRange { pub end: u32, pub start: u32, - #[derivative(Debug = "ignore", Hash = "ignore")] - source: Option>, -} - -impl DependencyRange { - pub fn new(start: u32, end: u32) -> Self { - DependencyRange { - end, - start, - source: None, - } - } - - pub fn with_source(mut self, source: Arc) -> Self { - self.source = Some(source); - self - } } impl From<(u32, u32)> for DependencyRange { @@ -31,7 +19,6 @@ impl From<(u32, u32)> for DependencyRange { Self { start: range.0, end: range.1, - source: None, } } } @@ -41,49 +28,90 @@ impl From for DependencyRange { Self { start: span.lo.0.saturating_sub(1), end: span.hi.0.saturating_sub(1), - source: None, } } } -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); +impl DependencyRange { + pub fn new(start: u32, end: u32) -> Self { + DependencyRange { end, start } + } - if start.line == end.line { - if start.column == end.column { - return write!(f, "{}:{}", start.line, start.column); + /// Converts the `DependencyRange` into a `DependencyLocation`. + /// The `source` parameter is an optional source map used to resolve the exact position in the source file. + pub fn to_loc(&self, source: Option<&Arc>) -> DependencyLocation { + DependencyLocation::Real(match source { + Some(source) => { + let (start, end) = source.look_up_range_pos(self.start, self.end); + + if start.line == end.line && start.column == end.column { + RealDependencyLocation::new(start, None) + } else { + RealDependencyLocation::new(start, Some(end)) } - - return write!(f, "{}:{}-{}", start.line, start.column, end.column); } + None => RealDependencyLocation::new( + SourcePosition { + line: self.start as usize, + column: self.end as usize, + }, + None, + ), + }) + } +} - write!( - f, - "{}:{}-{}:{}", - start.line, start.column, end.line, end.column - ) +/// Represents the real location of a dependency in a source file, including both start and optional end positions. +/// These positions are described in terms of lines and columns in the source code. +#[derive(Debug, Clone)] +pub struct RealDependencyLocation { + start: SourcePosition, + end: Option, +} + +impl RealDependencyLocation { + pub fn new(start: SourcePosition, end: Option) -> Self { + Self { start, end } + } +} + +impl fmt::Display for RealDependencyLocation { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if let Some(end) = self.end { + if self.start.line == end.line { + write!( + f, + "{}:{}-{}", + self.start.line, self.start.column, end.column + ) + } else { + write!( + f, + "{}:{}-{}:{}", + self.start.line, self.start.column, end.line, end.column + ) + } } else { - write!(f, "{}:{}", self.start, self.end) + write!(f, "{}:{}", self.start.line, self.start.column) } } } +/// Represents a synthetic dependency location, such as a generated dependency. #[derive(Debug, Clone)] -pub struct DependencyName { +pub struct SyntheticDependencyLocation { pub name: String, } -impl DependencyName { +impl SyntheticDependencyLocation { pub fn new(name: &str) -> Self { - DependencyName { + SyntheticDependencyLocation { name: name.to_string(), } } } -impl fmt::Display for DependencyName { +impl fmt::Display for SyntheticDependencyLocation { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) } @@ -91,8 +119,8 @@ impl fmt::Display for DependencyName { #[derive(Debug, Clone)] pub enum DependencyLocation { - Real(DependencyRange), - Synthetic(DependencyName), + Real(RealDependencyLocation), + Synthetic(SyntheticDependencyLocation), } impl fmt::Display for DependencyLocation { @@ -105,16 +133,33 @@ impl fmt::Display for DependencyLocation { } } +/// Represents a position in the source file, including the line number and column number. #[derive(Debug, Clone, Copy)] pub struct SourcePosition { - pub line: usize, - pub column: usize, + line: usize, + column: usize, +} + +impl From<(u32, u32)> for SourcePosition { + fn from(range: (u32, u32)) -> Self { + Self { + line: range.0 as usize, + column: range.1 as usize, + } + } } +/// Trait representing a source map that can resolve the positions of code ranges to source file positions. pub trait SourceLocation: Send + Sync { fn look_up_range_pos(&self, start: u32, end: u32) -> (SourcePosition, SourcePosition); } +impl Debug for dyn SourceLocation { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("SourceMap").finish() + } +} + impl SourceLocation for swc_core::common::SourceMap { fn look_up_range_pos(&self, start: u32, end: u32) -> (SourcePosition, SourcePosition) { let lo = self.lookup_char_pos(swc_core::common::BytePos(start + 1)); @@ -132,3 +177,6 @@ impl SourceLocation for swc_core::common::SourceMap { ) } } + +/// Type alias for a shared reference to a `SourceLocation` trait object, typically used for source maps. +pub type SharedSourceMap = Arc; diff --git a/crates/rspack_core/src/dependency/dependency_trait.rs b/crates/rspack_core/src/dependency/dependency_trait.rs index 09ecb786e9b..ed3d067ad67 100644 --- a/crates/rspack_core/src/dependency/dependency_trait.rs +++ b/crates/rspack_core/src/dependency/dependency_trait.rs @@ -8,6 +8,7 @@ use swc_core::ecma::atoms::Atom; use super::dependency_template::AsDependencyTemplate; use super::module_dependency::*; +use super::DependencyLocation; use super::DependencyRange; use super::ExportsSpec; use super::{DependencyCategory, DependencyId, DependencyType}; @@ -74,7 +75,7 @@ pub trait Dependency: ConnectionState::Bool(true) } - fn loc(&self) -> Option { + fn loc(&self) -> Option { None } diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_full_require_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_full_require_dependency.rs index 772ed0bd244..1b750ce8c7a 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_full_require_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_full_require_dependency.rs @@ -1,6 +1,7 @@ use rspack_core::{ - module_id, property_access, to_normal_comment, Compilation, DependencyRange, ExportsType, - ExtendedReferencedExport, ModuleGraph, RuntimeGlobals, RuntimeSpec, UsedName, + module_id, property_access, to_normal_comment, Compilation, DependencyLocation, DependencyRange, + ExportsType, ExtendedReferencedExport, ModuleGraph, RuntimeGlobals, RuntimeSpec, SharedSourceMap, + UsedName, }; use rspack_core::{AsContextDependency, Dependency, DependencyCategory}; use rspack_core::{DependencyId, DependencyTemplate}; @@ -17,6 +18,7 @@ pub struct CommonJsFullRequireDependency { is_call: bool, optional: bool, asi_safe: bool, + source_map: Option, } impl CommonJsFullRequireDependency { @@ -27,6 +29,7 @@ impl CommonJsFullRequireDependency { is_call: bool, optional: bool, asi_safe: bool, + source_map: Option, ) -> Self { Self { id: DependencyId::new(), @@ -36,6 +39,7 @@ impl CommonJsFullRequireDependency { is_call, optional, asi_safe, + source_map, } } } @@ -53,8 +57,8 @@ impl Dependency for CommonJsFullRequireDependency { &DependencyType::CjsRequire } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn range(&self) -> Option<&DependencyRange> { diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_require_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_require_dependency.rs index 77df7a4ec90..1ac83e1788c 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_require_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/common_js_require_dependency.rs @@ -1,4 +1,6 @@ -use rspack_core::{module_id, Compilation, DependencyRange, RuntimeSpec}; +use rspack_core::{ + module_id, Compilation, DependencyLocation, DependencyRange, RuntimeSpec, SharedSourceMap, +}; use rspack_core::{AsContextDependency, Dependency, DependencyCategory}; use rspack_core::{DependencyId, DependencyTemplate}; use rspack_core::{DependencyType, ModuleDependency}; @@ -11,6 +13,7 @@ pub struct CommonJsRequireDependency { optional: bool, range: DependencyRange, range_expr: Option, + source_map: Option, } impl CommonJsRequireDependency { @@ -19,6 +22,7 @@ impl CommonJsRequireDependency { range: DependencyRange, range_expr: Option, optional: bool, + source_map: Option, ) -> Self { Self { id: DependencyId::new(), @@ -26,6 +30,7 @@ impl CommonJsRequireDependency { optional, range, range_expr, + source_map, } } } @@ -35,8 +40,8 @@ impl Dependency for CommonJsRequireDependency { &self.id } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn category(&self) -> &DependencyCategory { diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_header_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_header_dependency.rs index 3f88dd88d47..35f8a9d105b 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_header_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_header_dependency.rs @@ -1,20 +1,23 @@ -use rspack_core::DependencyId; use rspack_core::{ - AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyRange, RuntimeSpec, + AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyLocation, + DependencyRange, RuntimeSpec, }; +use rspack_core::{DependencyId, SharedSourceMap}; use rspack_core::{DependencyTemplate, RuntimeGlobals, TemplateContext}; #[derive(Debug, Clone)] pub struct RequireHeaderDependency { id: DependencyId, range: DependencyRange, + source_map: Option, } impl RequireHeaderDependency { - pub fn new(range: DependencyRange) -> Self { + pub fn new(range: DependencyRange, source_map: Option) -> Self { Self { id: DependencyId::new(), range, + source_map, } } } @@ -24,8 +27,8 @@ impl Dependency for RequireHeaderDependency { &self.id } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn could_affect_referencing_module(&self) -> rspack_core::AffectType { diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_header_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_header_dependency.rs index 50ef4e5fe3e..227f995afc5 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_header_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/require_resolve_header_dependency.rs @@ -1,19 +1,22 @@ use rspack_core::{ AffectType, AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId, - DependencyRange, DependencyTemplate, RuntimeSpec, TemplateContext, TemplateReplaceSource, + DependencyLocation, DependencyRange, DependencyTemplate, RuntimeSpec, SharedSourceMap, + TemplateContext, TemplateReplaceSource, }; #[derive(Debug, Clone)] pub struct RequireResolveHeaderDependency { id: DependencyId, range: DependencyRange, + source_map: Option, } impl RequireResolveHeaderDependency { - pub fn new(range: DependencyRange) -> Self { + pub fn new(range: DependencyRange, source_map: Option) -> Self { Self { id: DependencyId::new(), range, + source_map, } } } @@ -23,8 +26,8 @@ impl Dependency for RequireResolveHeaderDependency { &self.id } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn could_affect_referencing_module(&self) -> AffectType { diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_expression_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_expression_dependency.rs index f0aca2662b0..c9c9fbb9745 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_expression_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_expression_dependency.rs @@ -3,9 +3,9 @@ use rspack_collections::{Identifier, IdentifierSet}; use rspack_core::rspack_sources::ReplacementEnforce; use rspack_core::{ property_access, AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId, - DependencyRange, DependencyTemplate, DependencyType, ESMExportInitFragment, ExportNameOrSpec, - ExportsOfExportsSpec, ExportsSpec, ModuleGraph, RuntimeGlobals, RuntimeSpec, TemplateContext, - TemplateReplaceSource, UsedName, DEFAULT_EXPORT, + DependencyLocation, DependencyRange, DependencyTemplate, DependencyType, ESMExportInitFragment, + ExportNameOrSpec, ExportsOfExportsSpec, ExportsSpec, ModuleGraph, RuntimeGlobals, RuntimeSpec, + SharedSourceMap, TemplateContext, TemplateReplaceSource, UsedName, DEFAULT_EXPORT, }; use swc_core::atoms::Atom; @@ -41,6 +41,7 @@ pub struct ESMExportExpressionDependency { range_stmt: DependencyRange, prefix: String, declaration: Option, + source_map: Option, } impl ESMExportExpressionDependency { @@ -49,13 +50,15 @@ impl ESMExportExpressionDependency { range_stmt: DependencyRange, prefix: String, declaration: Option, + source_map: Option, ) -> Self { Self { + id: DependencyId::default(), range, range_stmt, declaration, prefix, - id: DependencyId::default(), + source_map, } } } @@ -69,8 +72,8 @@ impl Dependency for ESMExportExpressionDependency { &self.id } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn get_exports(&self, _mg: &ModuleGraph) -> Option { diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_header_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_header_dependency.rs index d5a6f7808a4..316eb6650e7 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_header_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_header_dependency.rs @@ -1,6 +1,7 @@ use rspack_core::{ - AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId, DependencyRange, - DependencyTemplate, DependencyType, RuntimeSpec, TemplateContext, TemplateReplaceSource, + AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyId, + DependencyLocation, DependencyRange, DependencyTemplate, DependencyType, RuntimeSpec, + SharedSourceMap, TemplateContext, TemplateReplaceSource, }; // Remove `export` label. @@ -11,13 +12,19 @@ pub struct ESMExportHeaderDependency { id: DependencyId, range: DependencyRange, range_decl: Option, + source_map: Option, } impl ESMExportHeaderDependency { - pub fn new(range: DependencyRange, range_decl: Option) -> Self { + pub fn new( + range: DependencyRange, + range_decl: Option, + source_map: Option, + ) -> Self { Self { range, range_decl, + source_map, id: DependencyId::default(), } } @@ -28,8 +35,8 @@ impl Dependency for ESMExportHeaderDependency { &self.id } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn dependency_type(&self) -> &DependencyType { diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs index 1bb0b0eeb2c..ab6519b6a36 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs @@ -7,13 +7,13 @@ use rspack_core::{ create_exports_object_referenced, create_no_exports_referenced, filter_runtime, get_exports_type, process_export_info, property_access, property_name, string_of_used_name, AsContextDependency, Compilation, ConditionalInitFragment, ConnectionState, Dependency, DependencyCategory, - DependencyCondition, DependencyConditionFn, DependencyId, DependencyRange, DependencyTemplate, - DependencyType, ESMExportInitFragment, ExportInfo, ExportInfoProvided, ExportNameOrSpec, - ExportPresenceMode, ExportSpec, ExportsInfo, ExportsOfExportsSpec, ExportsSpec, ExportsType, - ExtendedReferencedExport, ImportAttributes, InitFragmentExt, InitFragmentKey, InitFragmentStage, - JavascriptParserOptions, ModuleDependency, ModuleGraph, ModuleIdentifier, NormalInitFragment, - RuntimeCondition, RuntimeGlobals, RuntimeSpec, Template, TemplateContext, TemplateReplaceSource, - UsageState, UsedName, + DependencyCondition, DependencyConditionFn, DependencyId, DependencyLocation, DependencyRange, + DependencyTemplate, DependencyType, ESMExportInitFragment, ExportInfo, ExportInfoProvided, + ExportNameOrSpec, ExportPresenceMode, ExportSpec, ExportsInfo, ExportsOfExportsSpec, ExportsSpec, + ExportsType, ExtendedReferencedExport, ImportAttributes, InitFragmentExt, InitFragmentKey, + InitFragmentStage, JavascriptParserOptions, ModuleDependency, ModuleGraph, ModuleIdentifier, + NormalInitFragment, RuntimeCondition, RuntimeGlobals, RuntimeSpec, SharedSourceMap, Template, + TemplateContext, TemplateReplaceSource, UsageState, UsedName, }; use rspack_error::{ miette::{MietteDiagnostic, Severity}, @@ -44,6 +44,7 @@ pub struct ESMExportImportedSpecifierDependency { attributes: Option, resource_identifier: String, export_presence_mode: ExportPresenceMode, + source_map: Option, } impl ESMExportImportedSpecifierDependency { @@ -58,6 +59,7 @@ impl ESMExportImportedSpecifierDependency { range: DependencyRange, export_presence_mode: ExportPresenceMode, attributes: Option, + source_map: Option, ) -> Self { let resource_identifier = create_resource_identifier_for_esm_dependency(&request, attributes.as_ref()); @@ -73,6 +75,7 @@ impl ESMExportImportedSpecifierDependency { range, export_presence_mode, attributes, + source_map, } } @@ -1038,8 +1041,8 @@ impl Dependency for ESMExportImportedSpecifierDependency { &self.id } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn range(&self) -> Option<&DependencyRange> { diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_specifier_dependency.rs index 9f4e2639a2c..b7f98238e2a 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_specifier_dependency.rs @@ -1,9 +1,9 @@ use rspack_collections::IdentifierSet; use rspack_core::{ AsContextDependency, AsModuleDependency, Compilation, Dependency, DependencyCategory, - DependencyId, DependencyRange, DependencyTemplate, DependencyType, ESMExportInitFragment, - ExportNameOrSpec, ExportsOfExportsSpec, ExportsSpec, ModuleGraph, RuntimeSpec, TemplateContext, - TemplateReplaceSource, UsedName, + DependencyId, DependencyLocation, DependencyRange, DependencyTemplate, DependencyType, + ESMExportInitFragment, ExportNameOrSpec, ExportsOfExportsSpec, ExportsSpec, ModuleGraph, + RuntimeSpec, SharedSourceMap, TemplateContext, TemplateReplaceSource, UsedName, }; use swc_core::ecma::atoms::Atom; @@ -12,16 +12,23 @@ use swc_core::ecma::atoms::Atom; pub struct ESMExportSpecifierDependency { id: DependencyId, range: DependencyRange, + source_map: Option, pub name: Atom, pub value: Atom, // id } impl ESMExportSpecifierDependency { - pub fn new(name: Atom, value: Atom, range: DependencyRange) -> Self { + pub fn new( + name: Atom, + value: Atom, + range: DependencyRange, + source_map: Option, + ) -> Self { Self { name, value, range, + source_map, id: DependencyId::new(), } } @@ -32,8 +39,8 @@ impl Dependency for ESMExportSpecifierDependency { &self.id } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn category(&self) -> &DependencyCategory { diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs index bddb62f71db..34480e35b3e 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs @@ -3,7 +3,9 @@ use std::sync::Arc; use rspack_collections::IdentifierSet; use rspack_core::Compilation; use rspack_core::DependencyConditionFn; +use rspack_core::DependencyLocation; use rspack_core::DependencyRange; +use rspack_core::SharedSourceMap; use rspack_core::{ filter_runtime, import_statement, merge_runtime, AsContextDependency, AwaitDependenciesInitFragment, BuildMetaDefaultObject, ConditionalInitFragment, ConnectionState, @@ -67,6 +69,7 @@ pub struct ESMImportSideEffectDependency { pub export_all: bool, attributes: Option, resource_identifier: String, + source_map: Option, } impl ESMImportSideEffectDependency { @@ -79,6 +82,7 @@ impl ESMImportSideEffectDependency { dependency_type: DependencyType, export_all: bool, attributes: Option, + source_map: Option, ) -> Self { let resource_identifier = create_resource_identifier_for_esm_dependency(&request, attributes.as_ref()); @@ -92,6 +96,7 @@ impl ESMImportSideEffectDependency { export_all, attributes, resource_identifier, + source_map, } } } @@ -390,8 +395,8 @@ impl Dependency for ESMImportSideEffectDependency { &self.id } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn range(&self) -> Option<&DependencyRange> { diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_specifier_dependency.rs index e881f538508..e1e04efbd1a 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_specifier_dependency.rs @@ -2,10 +2,10 @@ use rspack_collections::IdentifierSet; use rspack_core::{ create_exports_object_referenced, export_from_import, get_dependency_used_by_exports_condition, get_exports_type, AsContextDependency, Compilation, ConnectionState, Dependency, - DependencyCategory, DependencyCondition, DependencyId, DependencyRange, DependencyTemplate, - DependencyType, ExportPresenceMode, ExportsType, ExtendedReferencedExport, ImportAttributes, - JavascriptParserOptions, ModuleDependency, ModuleGraph, ReferencedExport, RuntimeSpec, - TemplateContext, TemplateReplaceSource, UsedByExports, + DependencyCategory, DependencyCondition, DependencyId, DependencyLocation, DependencyRange, + DependencyTemplate, DependencyType, ExportPresenceMode, ExportsType, ExtendedReferencedExport, + ImportAttributes, JavascriptParserOptions, ModuleDependency, ModuleGraph, ReferencedExport, + RuntimeSpec, SharedSourceMap, TemplateContext, TemplateReplaceSource, UsedByExports, }; use rspack_core::{property_access, ModuleReferenceOptions}; use rspack_error::Diagnostic; @@ -32,6 +32,7 @@ pub struct ESMImportSpecifierDependency { resource_identifier: String, export_presence_mode: ExportPresenceMode, attributes: Option, + source_map: Option, pub namespace_object_as_context: bool, } @@ -50,6 +51,7 @@ impl ESMImportSpecifierDependency { export_presence_mode: ExportPresenceMode, referenced_properties_in_destructuring: Option>, attributes: Option, + source_map: Option, ) -> Self { let resource_identifier = create_resource_identifier_for_esm_dependency(&request, attributes.as_ref()); @@ -70,6 +72,7 @@ impl ESMImportSpecifierDependency { referenced_properties_in_destructuring, attributes, resource_identifier, + source_map, } } @@ -224,8 +227,8 @@ impl Dependency for ESMImportSpecifierDependency { &self.id } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn range(&self) -> Option<&DependencyRange> { diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/provide_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/provide_dependency.rs index 0f517817fa1..8615b6def1e 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/provide_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/provide_dependency.rs @@ -1,7 +1,8 @@ use itertools::Itertools; use rspack_core::{ - create_exports_object_referenced, module_raw, Compilation, DependencyRange, DependencyType, - ExtendedReferencedExport, ModuleGraph, NormalInitFragment, RuntimeSpec, UsedName, + create_exports_object_referenced, module_raw, Compilation, DependencyLocation, DependencyRange, + DependencyType, ExtendedReferencedExport, ModuleGraph, NormalInitFragment, RuntimeSpec, + SharedSourceMap, UsedName, }; use rspack_core::{AsContextDependency, Dependency, InitFragmentKey, InitFragmentStage}; use rspack_core::{DependencyCategory, DependencyId, DependencyTemplate}; @@ -16,13 +17,21 @@ pub struct ProvideDependency { identifier: String, ids: Vec, range: DependencyRange, + source_map: Option, } impl ProvideDependency { - pub fn new(range: DependencyRange, request: Atom, identifier: String, ids: Vec) -> Self { + pub fn new( + range: DependencyRange, + request: Atom, + identifier: String, + ids: Vec, + source_map: Option, + ) -> Self { Self { range, request, + source_map, identifier, ids, id: DependencyId::new(), @@ -35,8 +44,8 @@ impl Dependency for ProvideDependency { &self.id } - fn loc(&self) -> Option { - Some(self.range.to_string()) + fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } fn category(&self) -> &DependencyCategory { diff --git a/crates/rspack_plugin_javascript/src/dependency/hmr/esm_accept_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/hmr/esm_accept_dependency.rs index da507080ffa..d34f4e86835 100644 --- a/crates/rspack_plugin_javascript/src/dependency/hmr/esm_accept_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/hmr/esm_accept_dependency.rs @@ -1,7 +1,7 @@ use rspack_core::{ import_statement, runtime_condition_expression, AsDependency, Compilation, DependencyId, - DependencyRange, DependencyTemplate, RuntimeCondition, RuntimeSpec, TemplateContext, - TemplateReplaceSource, + DependencyLocation, DependencyRange, DependencyTemplate, RuntimeCondition, RuntimeSpec, + SharedSourceMap, TemplateContext, TemplateReplaceSource, }; use crate::dependency::import_emitted_runtime; @@ -11,6 +11,7 @@ pub struct ESMAcceptDependency { range: DependencyRange, has_callback: bool, dependency_ids: Vec, + source_map: Option, } impl ESMAcceptDependency { @@ -18,16 +19,18 @@ impl ESMAcceptDependency { range: DependencyRange, has_callback: bool, dependency_ids: Vec, + source_map: Option, ) -> Self { Self { range, has_callback, dependency_ids, + source_map, } } - pub fn loc(&self) -> Option { - Some(self.range.to_string()) + pub fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } } diff --git a/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs index e2d63762877..6eebdb9d0bf 100644 --- a/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs @@ -1,6 +1,6 @@ use rspack_core::{ - AsDependency, Compilation, DependencyRange, DependencyTemplate, RuntimeGlobals, RuntimeSpec, - TemplateContext, TemplateReplaceSource, + AsDependency, Compilation, DependencyLocation, DependencyRange, DependencyTemplate, + RuntimeGlobals, RuntimeSpec, SharedSourceMap, TemplateContext, TemplateReplaceSource, }; use rspack_util::ext::DynHash; @@ -8,15 +8,24 @@ use rspack_util::ext::DynHash; pub struct ModuleArgumentDependency { id: Option<&'static str>, range: DependencyRange, + source_map: Option, } impl ModuleArgumentDependency { - pub fn new(id: Option<&'static str>, range: DependencyRange) -> Self { - Self { id, range } + pub fn new( + id: Option<&'static str>, + range: DependencyRange, + source_map: Option, + ) -> Self { + Self { + id, + range, + source_map, + } } - pub fn loc(&self) -> Option { - Some(self.range.to_string()) + pub fn loc(&self) -> Option { + Some(self.range.to_loc(self.source_map.as_ref())) } } diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/amd_require_dependencies_block_parser_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/amd_require_dependencies_block_parser_plugin.rs index 549f2b0ddef..854fbfd66c1 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/amd_require_dependencies_block_parser_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/amd_require_dependencies_block_parser_plugin.rs @@ -3,8 +3,8 @@ use std::{borrow::Cow, iter}; use either::Either; use itertools::Itertools; use rspack_core::{ - AsyncDependenciesBlock, BoxDependency, ConstDependency, DependencyLocation, DependencyRange, - RuntimeGlobals, SpanExt, + AsyncDependenciesBlock, BoxDependency, ConstDependency, DependencyRange, RuntimeGlobals, + SharedSourceMap, SpanExt, }; use rspack_error::miette::Severity; use rspack_util::atom::Atom; @@ -258,9 +258,9 @@ impl AMDRequireDependenciesBlockParserPlugin { error_callback_arg.map(|arg| (arg.expr.span().real_lo(), arg.expr.span().real_hi())), )); - let block_loc = Some(DependencyLocation::Real( - Into::::into(call_expr.span).with_source(parser.source_map.clone()), - )); + let source_map: SharedSourceMap = parser.source_map.clone(); + let block_loc = + Some(Into::::into(call_expr.span).to_loc(Some(source_map).as_ref())); if call_expr.args.len() == 1 { let mut block_deps: Vec = vec![dep]; diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs index f69ed69d1a0..bc2a4323f7f 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs @@ -1,6 +1,4 @@ -use rspack_core::{ - ConstDependency, DependencyRange, RuntimeGlobals, RuntimeRequirementsDependency, SpanExt, -}; +use rspack_core::{ConstDependency, RuntimeGlobals, RuntimeRequirementsDependency, SpanExt}; use swc_core::common::Spanned; use swc_core::ecma::ast::{CallExpr, Callee, Expr, Ident, UnaryExpr}; @@ -168,12 +166,12 @@ impl JavascriptParserPlugin for APIPlugin { Some(true) } WEBPACK_MODULE => { - let range: DependencyRange = ident.span.into(); parser .presentational_dependencies .push(Box::new(ModuleArgumentDependency::new( None, - range.with_source(parser.source_map.clone()), + ident.span.into(), + Some(parser.source_map.clone()), ))); Some(true) } @@ -400,12 +398,12 @@ impl JavascriptParserPlugin for APIPlugin { .push(Box::new(RuntimeRequirementsDependency::new( RuntimeGlobals::MODULE_ID, ))); - let range: DependencyRange = expr.span().into(); parser .presentational_dependencies .push(Box::new(ModuleArgumentDependency::new( Some("id"), - range.with_source(parser.source_map.clone()), + expr.span().into(), + Some(parser.source_map.clone()), ))); Some(true) } else { diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/common_js_imports_parse_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/common_js_imports_parse_plugin.rs index 4c2501c78ab..06520a1c0ac 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/common_js_imports_parse_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/common_js_imports_parse_plugin.rs @@ -102,6 +102,7 @@ impl CommonJsImportsParserPlugin { let param = parser.evaluate_expression(argument_expr); let require_resolve_header_dependency = Box::new(RequireResolveHeaderDependency::new( call_expr.callee.span().into(), + Some(parser.source_map.clone()), )); if param.is_conditional() { @@ -185,10 +186,11 @@ impl CommonJsImportsParserPlugin { CommonJsFullRequireDependency::new( param.string().to_owned(), members, - range.with_source(parser.source_map.clone()), + range, is_call, parser.in_try, !parser.is_asi_position(mem_expr.span_lo()), + Some(parser.source_map.clone()), ) }) } @@ -203,9 +205,10 @@ impl CommonJsImportsParserPlugin { let range_expr: DependencyRange = param.range().into(); let dep = CommonJsRequireDependency::new( param.string().to_string(), - range_expr.with_source(parser.source_map.clone()), + range_expr, Some(span.into()), parser.in_try, + Some(parser.source_map.clone()), ); parser.dependencies.push(Box::new(dep)); true @@ -265,7 +268,8 @@ impl CommonJsImportsParserPlugin { parser .presentational_dependencies .push(Box::new(RequireHeaderDependency::new( - range.with_source(parser.source_map.clone()), + range, + Some(parser.source_map.clone()), ))); return Some(true); } @@ -286,7 +290,8 @@ impl CommonJsImportsParserPlugin { parser .presentational_dependencies .push(Box::new(RequireHeaderDependency::new( - range.with_source(parser.source_map.clone()), + range, + Some(parser.source_map.clone()), ))); } Some(true) diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs index 42c31617d39..ae0860baf75 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs @@ -66,7 +66,7 @@ impl CompatibilityPlugin { Some(NestedRequireData { name: rename, update: false, - loc: DependencyRange::new(start, end).with_source(parser.source_map.clone()), + loc: DependencyRange::new(start, end), }), ); } diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/esm_export_dependency_parser_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/esm_export_dependency_parser_plugin.rs index 2e98ef6b2e4..cd8139f0ee2 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/esm_export_dependency_parser_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/esm_export_dependency_parser_plugin.rs @@ -24,10 +24,10 @@ pub struct ESMExportDependencyParserPlugin; impl JavascriptParserPlugin for ESMExportDependencyParserPlugin { fn export(&self, parser: &mut JavascriptParser, statement: ExportLocal) -> Option { - let range: DependencyRange = statement.span().into(); let dep = ESMExportHeaderDependency::new( - range.with_source(parser.source_map.clone()), + statement.span().into(), statement.declaration_span().map(|span| span.into()), + Some(parser.source_map.clone()), ); parser.presentational_dependencies.push(Box::new(dep)); Some(true) @@ -41,17 +41,17 @@ impl JavascriptParserPlugin for ESMExportDependencyParserPlugin { ) -> Option { parser.last_esm_import_order += 1; let span = statement.span(); - let range: DependencyRange = span.into(); let clean_dep = ConstDependency::new(span.real_lo(), span.real_hi(), "".into(), None); parser.presentational_dependencies.push(Box::new(clean_dep)); let side_effect_dep = ESMImportSideEffectDependency::new( source.clone(), parser.last_esm_import_order, - range.with_source(parser.source_map.clone()), + span.into(), statement.source_span().into(), DependencyType::EsmExport, matches!(statement, ExportImport::All(_)), statement.get_with_obj().map(get_attributes), + Some(parser.source_map.clone()), ); parser.dependencies.push(Box::new(side_effect_dep)); Some(true) @@ -75,7 +75,6 @@ impl JavascriptParserPlugin for ESMExportDependencyParserPlugin { .insert(export_name.clone()); let dep = if let Some(settings) = parser.get_tag_data(local_id, ESM_SPECIFIER_TAG) { let settings = ESMSpecifierData::downcast(settings); - let range: DependencyRange = statement.span().into(); Box::new(ESMExportImportedSpecifierDependency::new( settings.source, settings.source_order, @@ -83,18 +82,19 @@ impl JavascriptParserPlugin for ESMExportDependencyParserPlugin { Some(export_name.clone()), false, None, - range.with_source(parser.source_map.clone()), + statement.span().into(), ESMExportImportedSpecifierDependency::create_export_presence_mode( parser.javascript_options, ), settings.attributes, + Some(parser.source_map.clone()), )) as BoxDependency } else { - let range: DependencyRange = statement.span().into(); Box::new(ESMExportSpecifierDependency::new( export_name.clone(), local_id.clone(), - range.with_source(parser.source_map.clone()), + statement.span().into(), + Some(parser.source_map.clone()), )) }; let is_asi_safe = !parser.is_asi_position(statement.span_lo()); @@ -122,7 +122,6 @@ impl JavascriptParserPlugin for ESMExportDependencyParserPlugin { } else { Some(parser.build_info.all_star_exports.clone()) }; - let range: DependencyRange = statement.span().into(); let dep = ESMExportImportedSpecifierDependency::new( source.clone(), parser.last_esm_import_order, @@ -130,9 +129,10 @@ impl JavascriptParserPlugin for ESMExportDependencyParserPlugin { export_name.cloned(), local_id.is_some(), star_exports, - range.with_source(parser.source_map.clone()), + statement.span().into(), ESMExportImportedSpecifierDependency::create_export_presence_mode(parser.javascript_options), None, + Some(parser.source_map.clone()), ); if export_name.is_none() { parser.build_info.all_star_exports.push(dep.id); @@ -154,9 +154,8 @@ impl JavascriptParserPlugin for ESMExportDependencyParserPlugin { let expr_span = expr.span(); let statement_span = statement.span(); - let range: DependencyRange = expr_span.into(); let dep: ESMExportExpressionDependency = ESMExportExpressionDependency::new( - range.with_source(parser.source_map.clone()), + expr_span.into(), statement_span.into(), parser .comments @@ -203,6 +202,7 @@ impl JavascriptParserPlugin for ESMExportDependencyParserPlugin { .map(|ident| DeclarationId::Id(ident.sym.to_string())), ExportDefaultExpression::Expr(_) => None, }, + Some(parser.source_map.clone()), ); parser.dependencies.push(Box::new(dep)); InnerGraphPlugin::add_variable_usage( diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/esm_import_dependency_parser_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/esm_import_dependency_parser_plugin.rs index a7fa213b209..e9ad4fb0fe1 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/esm_import_dependency_parser_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/esm_import_dependency_parser_plugin.rs @@ -1,6 +1,4 @@ -use rspack_core::{ - ConstDependency, Dependency, DependencyRange, DependencyType, ImportAttributes, SpanExt, -}; +use rspack_core::{ConstDependency, Dependency, DependencyType, ImportAttributes, SpanExt}; use swc_core::atoms::Atom; use swc_core::common::{Span, Spanned}; use swc_core::ecma::ast::{ @@ -70,16 +68,16 @@ impl JavascriptParserPlugin for ESMImportDependencyParserPlugin { source: &str, ) -> Option { parser.last_esm_import_order += 1; - let range: DependencyRange = import_decl.span.into(); let attributes = import_decl.with.as_ref().map(|obj| get_attributes(obj)); let dependency = ESMImportSideEffectDependency::new( source.into(), parser.last_esm_import_order, - range.with_source(parser.source_map.clone()), + import_decl.span.into(), import_decl.src.span.into(), DependencyType::EsmImport, false, attributes, + Some(parser.source_map.clone()), ); parser.dependencies.push(Box::new(dependency)); @@ -134,20 +132,20 @@ impl JavascriptParserPlugin for ESMImportDependencyParserPlugin { .definitions_db .expect_get_tag_info(parser.current_tag_info?); let settings = ESMSpecifierData::downcast(tag_info.data.clone()?); - let range: DependencyRange = ident.span.into(); let dep = ESMImportSpecifierDependency::new( settings.source, settings.name, settings.source_order, parser.in_short_hand, !parser.is_asi_position(ident.span_lo()), - range.with_source(parser.source_map.clone()), + ident.span.into(), settings.ids, parser.in_tagged_template_tag, true, ESMImportSpecifierDependency::create_export_presence_mode(parser.javascript_options), parser.properties_in_destructuring.remove(&ident.sym), settings.attributes, + Some(parser.source_map.clone()), ); let dep_id = *dep.id(); parser.dependencies.push(Box::new(dep)); @@ -201,20 +199,20 @@ impl JavascriptParserPlugin for ESMImportDependencyParserPlugin { let mut ids = settings.ids; ids.extend(non_optional_members.iter().cloned()); let direct_import = members.is_empty(); - let range: DependencyRange = span.into(); let dep = ESMImportSpecifierDependency::new( settings.source, settings.name, settings.source_order, false, !parser.is_asi_position(call_expr.span_lo()), - range.with_source(parser.source_map.clone()), + span.into(), ids, true, direct_import, ESMImportSpecifierDependency::create_export_presence_mode(parser.javascript_options), None, settings.attributes, + Some(parser.source_map.clone()), ); let dep_id = *dep.id(); parser.dependencies.push(Box::new(dep)); @@ -265,20 +263,20 @@ impl JavascriptParserPlugin for ESMImportDependencyParserPlugin { }; let mut ids = settings.ids; ids.extend(non_optional_members.iter().cloned()); - let range: DependencyRange = span.into(); let dep = ESMImportSpecifierDependency::new( settings.source, settings.name, settings.source_order, false, !parser.is_asi_position(member_expr.span_lo()), - range.with_source(parser.source_map.clone()), + span.into(), ids, false, false, // x.xx() ESMImportSpecifierDependency::create_export_presence_mode(parser.javascript_options), None, settings.attributes, + Some(parser.source_map.clone()), ); let dep_id = *dep.id(); parser.dependencies.push(Box::new(dep)); diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/hot_module_replacement_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/hot_module_replacement_plugin.rs index 67ff6172c34..f52a4f64968 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/hot_module_replacement_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/hot_module_replacement_plugin.rs @@ -40,13 +40,13 @@ fn extract_deps(call_expr: &CallExpr, create_dependency: CreateDependency) -> Ve impl<'parser> JavascriptParser<'parser> { fn create_hmr_expression_handler(&mut self, span: Span) { - let range: DependencyRange = span.into(); self.build_info.module_concatenation_bailout = Some(String::from("Hot Module Replacement")); self .presentational_dependencies .push(Box::new(ModuleArgumentDependency::new( Some("hot"), - range.with_source(self.source_map.clone()), + span.into(), + Some(self.source_map.clone()), ))); } @@ -55,13 +55,13 @@ impl<'parser> JavascriptParser<'parser> { call_expr: &CallExpr, create_dependency: CreateDependency, ) -> Option { - let range: DependencyRange = call_expr.callee.span().into(); self.build_info.module_concatenation_bailout = Some(String::from("Hot Module Replacement")); self .presentational_dependencies .push(Box::new(ModuleArgumentDependency::new( Some("hot.accept"), - range.with_source(self.source_map.clone()), + call_expr.callee.span().into(), + Some(self.source_map.clone()), ))); let dependencies = extract_deps(call_expr, create_dependency); if self.build_meta.esm && !call_expr.args.is_empty() { @@ -75,9 +75,10 @@ impl<'parser> JavascriptParser<'parser> { self .presentational_dependencies .push(Box::new(ESMAcceptDependency::new( - range.with_source(self.source_map.clone()), + range, callback_arg.is_some(), dependency_ids, + Some(self.source_map.clone()), ))); } self.dependencies.extend(dependencies); @@ -90,13 +91,13 @@ impl<'parser> JavascriptParser<'parser> { call_expr: &CallExpr, create_dependency: CreateDependency, ) -> Option { - let range: DependencyRange = call_expr.callee.span().into(); self.build_info.module_concatenation_bailout = Some(String::from("Hot Module Replacement")); self .presentational_dependencies .push(Box::new(ModuleArgumentDependency::new( Some("hot.decline"), - range.with_source(self.source_map.clone()), + call_expr.callee.span().into(), + Some(self.source_map.clone()), ))); let dependencies = extract_deps(call_expr, create_dependency); self.dependencies.extend(dependencies); diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/import_parser_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/import_parser_plugin.rs index d0792227753..2b697bcb67d 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/import_parser_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/import_parser_plugin.rs @@ -1,7 +1,7 @@ use itertools::Itertools; use rspack_core::{ - AsyncDependenciesBlock, ContextDependency, DependencyLocation, DependencyRange, - DynamicImportMode, GroupOptions, ImportAttributes, + AsyncDependenciesBlock, ContextDependency, DependencyRange, DynamicImportMode, GroupOptions, + ImportAttributes, SharedSourceMap, }; use rspack_core::{ChunkGroupOptions, DynamicImportFetchPriority}; use rspack_core::{ContextNameSpaceObject, ContextOptions, DependencyCategory, SpanExt}; @@ -128,11 +128,10 @@ impl JavascriptParserPlugin for ImportParserPlugin { exports, attributes, )); + let source_map: SharedSourceMap = parser.source_map.clone(); let mut block = AsyncDependenciesBlock::new( *parser.module_identifier, - Some(DependencyLocation::Real( - Into::::into(node.span).with_source(parser.source_map.clone()), - )), + Some(Into::::into(node.span).to_loc(Some(source_map).as_ref())), None, vec![dep], Some(param.string().clone()), diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/provide_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/provide_plugin.rs index c8f3eaaf3de..55f1ccafc7d 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/provide_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/provide_plugin.rs @@ -3,7 +3,7 @@ use itertools::Itertools; use once_cell::sync::OnceCell; use rspack_core::{ ApplyContext, CompilerOptions, DependencyRange, ModuleType, NormalModuleFactoryParser, - ParserAndGenerator, ParserOptions, Plugin, PluginContext, + ParserAndGenerator, ParserOptions, Plugin, PluginContext, SharedSourceMap, }; use rspack_error::Result; use rspack_hook::{plugin, plugin_hook}; @@ -21,6 +21,7 @@ fn create_provide_dep( name: &str, value: &ProvideValue, range: DependencyRange, + source_map: SharedSourceMap, ) -> Option { if let Some(requests) = value.get(name) { let name_identifier = if name.contains(SOURCE_DOT) { @@ -39,6 +40,7 @@ fn create_provide_dep( .iter() .map(|s| Atom::from(s.as_str())) .collect_vec(), + Some(source_map), )); } None @@ -90,11 +92,11 @@ impl JavascriptParserPlugin for ProvidePlugin { expr: &swc_core::ecma::ast::CallExpr, for_name: &str, ) -> Option { - let range: DependencyRange = expr.callee.span().into(); create_provide_dep( for_name, &self.provide, - range.with_source(parser.source_map.clone()), + expr.callee.span().into(), + parser.source_map.clone(), ) .map(|dep| { parser.dependencies.push(Box::new(dep)); @@ -110,11 +112,11 @@ impl JavascriptParserPlugin for ProvidePlugin { expr: &swc_core::ecma::ast::MemberExpr, for_name: &str, ) -> Option { - let range: DependencyRange = expr.span().into(); create_provide_dep( for_name, &self.provide, - range.with_source(parser.source_map.clone()), + expr.span().into(), + parser.source_map.clone(), ) .map(|dep| { parser.dependencies.push(Box::new(dep)); @@ -128,11 +130,11 @@ impl JavascriptParserPlugin for ProvidePlugin { ident: &swc_core::ecma::ast::Ident, for_name: &str, ) -> Option { - let range: DependencyRange = ident.span.into(); create_provide_dep( for_name, &self.provide, - range.with_source(parser.source_map.clone()), + ident.span.into(), + parser.source_map.clone(), ) .map(|dep| { parser.dependencies.push(Box::new(dep)); diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/require_ensure_dependencies_block_parse_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/require_ensure_dependencies_block_parse_plugin.rs index 93bfc207d57..84d87c8f176 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/require_ensure_dependencies_block_parse_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/require_ensure_dependencies_block_parse_plugin.rs @@ -2,8 +2,8 @@ use std::borrow::Cow; use either::Either; use rspack_core::{ - AsyncDependenciesBlock, BoxDependency, ChunkGroupOptions, ConstDependency, DependencyLocation, - DependencyRange, GroupOptions, SpanExt, + AsyncDependenciesBlock, BoxDependency, ChunkGroupOptions, ConstDependency, DependencyRange, + GroupOptions, SharedSourceMap, SpanExt, }; use swc_core::{ common::Spanned, @@ -139,12 +139,10 @@ impl JavascriptParserPlugin for RequireEnsureDependenciesBlockParserPlugin { }, } } - + let source_map: SharedSourceMap = parser.source_map.clone(); let mut block = AsyncDependenciesBlock::new( *parser.module_identifier, - Some(DependencyLocation::Real( - Into::::into(expr.span).with_source(parser.source_map.clone()), - )), + Some(Into::::into(expr.span).to_loc(Some(source_map).as_ref())), None, deps, None, diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/worker_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/worker_plugin.rs index 81dfdd29492..c6428ea3bb4 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/worker_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/worker_plugin.rs @@ -4,8 +4,8 @@ use std::sync::LazyLock; use itertools::Itertools; use regex::Regex; use rspack_core::{ - AsyncDependenciesBlock, ConstDependency, DependencyLocation, DependencyRange, EntryOptions, - GroupOptions, SpanExt, + AsyncDependenciesBlock, ConstDependency, DependencyRange, EntryOptions, GroupOptions, + SharedSourceMap, SpanExt, }; use rspack_hash::RspackHash; use rustc_hash::{FxHashMap, FxHashSet}; @@ -96,11 +96,10 @@ fn add_dependencies( span.into(), parsed_path.range.into(), )); + let source_map: SharedSourceMap = parser.source_map.clone(); let mut block = AsyncDependenciesBlock::new( *parser.module_identifier, - Some(DependencyLocation::Real( - Into::::into(span).with_source(parser.source_map.clone()), - )), + Some(Into::::into(span).to_loc(Some(source_map).as_ref())), None, vec![dep], None, diff --git a/crates/rspack_plugin_lazy_compilation/src/module.rs b/crates/rspack_plugin_lazy_compilation/src/module.rs index 8d79c337c81..980787e9263 100644 --- a/crates/rspack_plugin_lazy_compilation/src/module.rs +++ b/crates/rspack_plugin_lazy_compilation/src/module.rs @@ -137,8 +137,13 @@ impl Module for LazyCompilationProxyModule { _build_context: BuildContext, _compilation: Option<&Compilation>, ) -> Result { - let client_dep = - CommonJsRequireDependency::new(self.client.clone(), DependencyRange::new(0, 0), None, false); + let client_dep = CommonJsRequireDependency::new( + self.client.clone(), + DependencyRange::new(0, 0), + None, + false, + None, + ); let mut dependencies = vec![]; let mut blocks = vec![];