From bab35d5a0828c89ca1b84481475d5948b5a8dc5a Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Sun, 24 Sep 2023 12:16:20 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20add=20rewrite=20usage?= =?UTF-8?q?=20span=20to=20record=20usage=20to=20rewrite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rspack_core/src/dependency/mod.rs | 4 +++- .../esm/harmony_import_specifier_dependency.rs | 4 +++- .../src/parser_and_generator/mod.rs | 7 ++++++- .../harmony_import_dependency_scanner.rs | 16 +++++++++++++++- .../src/visitors/dependency/mod.rs | 18 ++++++++++++++++-- 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/crates/rspack_core/src/dependency/mod.rs b/crates/rspack_core/src/dependency/mod.rs index eea740777c5..44ee4dc27e1 100644 --- a/crates/rspack_core/src/dependency/mod.rs +++ b/crates/rspack_core/src/dependency/mod.rs @@ -32,7 +32,7 @@ use dyn_clone::{clone_trait_object, DynClone}; use crate::{ ChunkGroupOptionsKindRef, ConnectionState, Context, ContextMode, ContextOptions, ErrorSpan, ExtendedReferencedExport, ModuleGraph, ModuleGraphConnection, ModuleIdentifier, ReferencedExport, - RuntimeSpec, + RuntimeSpec, UsedByExports, }; // Used to describe dependencies' types, see webpack's `type` getter in `Dependency` @@ -191,6 +191,8 @@ pub trait Dependency: None } + fn set_used_by_exports(&mut self, used_by_exports: Option) {} + fn get_module_evaluation_side_effects_state( &self, _module_graph: &ModuleGraph, diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_specifier_dependency.rs index a5d7a887ecf..d2611d0140c 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_specifier_dependency.rs @@ -189,7 +189,9 @@ impl Dependency for HarmonyImportSpecifierDependency { fn id(&self) -> &DependencyId { &self.id } - + fn set_used_by_exports(&mut self, used_by_exports: Option) { + self.used_by_exports = used_by_exports; + } fn category(&self) -> &DependencyCategory { &DependencyCategory::Esm } diff --git a/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs b/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs index c0aa5cd0522..43938fa9bdb 100644 --- a/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs +++ b/crates/rspack_plugin_javascript/src/parser_and_generator/mod.rs @@ -13,6 +13,7 @@ use rspack_error::{internal_error, IntoTWithDiagnosticArray, Result, TWithDiagno use swc_core::common::SyntaxContext; use crate::utils::syntax_by_module_type; +use crate::visitors::ScanDependenciesResult; use crate::visitors::{run_before_pass, scan_dependencies, swc_visitor::resolver}; use crate::{SideEffectsFlagPluginVisitor, SyntaxContextInfo}; #[derive(Debug)] @@ -127,7 +128,11 @@ impl ParserAndGenerator for JavaScriptParserAndGenerator { )); }); - let (dependencies, presentational_dependencies) = ast.visit(|program, context| { + let ScanDependenciesResult { + dependencies, + presentational_dependencies, + rewrite_usage_span, + } = ast.visit(|program, context| { scan_dependencies( program, context.unresolved_mark, diff --git a/crates/rspack_plugin_javascript/src/visitors/dependency/harmony_import_dependency_scanner.rs b/crates/rspack_plugin_javascript/src/visitors/dependency/harmony_import_dependency_scanner.rs index d9a4efaa78b..a811b7d2bfb 100644 --- a/crates/rspack_plugin_javascript/src/visitors/dependency/harmony_import_dependency_scanner.rs +++ b/crates/rspack_plugin_javascript/src/visitors/dependency/harmony_import_dependency_scanner.rs @@ -66,6 +66,7 @@ pub struct HarmonyImportDependencyScanner<'a> { pub import_map: &'a mut ImportMap, pub imports: Imports, pub build_info: &'a mut BuildInfo, + pub rewrite_usage_span: &'a mut HashSet, } impl<'a> HarmonyImportDependencyScanner<'a> { @@ -74,6 +75,7 @@ impl<'a> HarmonyImportDependencyScanner<'a> { presentational_dependencies: &'a mut Vec, import_map: &'a mut ImportMap, build_info: &'a mut BuildInfo, + rewrite_usage_span: &'a mut HashSet, ) -> Self { Self { dependencies, @@ -81,6 +83,7 @@ impl<'a> HarmonyImportDependencyScanner<'a> { import_map, imports: Default::default(), build_info, + rewrite_usage_span, } } } @@ -158,6 +161,7 @@ impl Visit for HarmonyImportDependencyScanner<'_> { program.visit_children_with(&mut HarmonyImportRefDependencyScanner::new( self.import_map, self.dependencies, + self.rewrite_usage_span, )); } @@ -300,15 +304,22 @@ pub struct HarmonyImportRefDependencyScanner<'a> { pub import_map: &'a ImportMap, pub dependencies: &'a mut Vec, pub properties_in_destructuring: HashMap>, + + pub rewrite_usage_span: &'a mut HashSet, } impl<'a> HarmonyImportRefDependencyScanner<'a> { - pub fn new(import_map: &'a ImportMap, dependencies: &'a mut Vec) -> Self { + pub fn new( + import_map: &'a ImportMap, + dependencies: &'a mut Vec, + rewrite_usage_span: &'a mut HashSet, + ) -> Self { Self { import_map, dependencies, enter_callee: false, properties_in_destructuring: HashMap::default(), + rewrite_usage_span, } } } @@ -331,6 +342,7 @@ impl Visit for HarmonyImportRefDependencyScanner<'_> { fn visit_prop(&mut self, n: &Prop) { match n { Prop::Shorthand(shorthand) => { + self.rewrite_usage_span.insert(shorthand.span); if let Some(reference) = self.import_map.get(&shorthand.to_id()) { self .dependencies @@ -353,6 +365,7 @@ impl Visit for HarmonyImportRefDependencyScanner<'_> { fn visit_ident(&mut self, ident: &Ident) { if let Some(reference) = self.import_map.get(&ident.to_id()) { + self.rewrite_usage_span.insert(ident.span); self .dependencies .push(Box::new(HarmonyImportSpecifierDependency::new( @@ -386,6 +399,7 @@ impl Visit for HarmonyImportRefDependencyScanner<'_> { if let Some(prop) = prop { let mut ids = reference.names.clone().map(|f| vec![f]).unwrap_or_default(); ids.push(prop); + self.rewrite_usage_span.insert(member_expr.span); self .dependencies .push(Box::new(HarmonyImportSpecifierDependency::new( diff --git a/crates/rspack_plugin_javascript/src/visitors/dependency/mod.rs b/crates/rspack_plugin_javascript/src/visitors/dependency/mod.rs index 6cb7c6fde72..4e7baf0e3f9 100644 --- a/crates/rspack_plugin_javascript/src/visitors/dependency/mod.rs +++ b/crates/rspack_plugin_javascript/src/visitors/dependency/mod.rs @@ -20,6 +20,8 @@ use rspack_core::{ ast::javascript::Program, BoxDependency, BoxDependencyTemplate, BuildInfo, BuildMeta, CompilerOptions, ModuleIdentifier, ModuleType, ResourceData, }; +use rustc_hash::FxHashSet as HashSet; +use swc_core::common::Span; use swc_core::common::{comments::Comments, Mark, SyntaxContext}; pub use util::*; @@ -36,7 +38,12 @@ use self::{ node_stuff_scanner::NodeStuffScanner, require_context_scanner::RequireContextScanner, url_scanner::UrlScanner, worker_scanner::WorkerScanner, }; -pub type ScanDependenciesResult = (Vec, Vec); + +pub struct ScanDependenciesResult { + pub dependencies: Vec, + pub presentational_dependencies: Vec, + pub rewrite_usage_span: HashSet, +} #[allow(clippy::too_many_arguments)] pub fn scan_dependencies( @@ -54,6 +61,8 @@ pub fn scan_dependencies( let unresolved_ctxt = SyntaxContext::empty().apply_mark(unresolved_mark); let comments = program.comments.clone(); let mut parser_exports_state = None; + + let mut rewrite_usage_span = HashSet::default(); program.visit_with(&mut ApiScanner::new( &unresolved_ctxt, resource_data, @@ -114,6 +123,7 @@ pub fn scan_dependencies( &mut presentational_dependencies, &mut import_map, build_info, + &mut rewrite_usage_span, )); program.visit_with(&mut HarmonyExportDependencyScanner::new( &mut dependencies, @@ -156,5 +166,9 @@ pub fn scan_dependencies( )); } - (dependencies, presentational_dependencies) + ScanDependenciesResult { + dependencies, + presentational_dependencies, + rewrite_usage_span, + } }