From 5bb0bc54d899a7b0ac469007b83e989a9ea794e5 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Sat, 23 Sep 2023 23:59:11 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20chore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refactor --- ' | 99 +++++++++++++++++++ crates/rspack_core/src/exports_info.rs | 12 +-- .../harmony_import_specifier_dependency.rs | 6 +- .../src/dependency/url/mod.rs | 6 +- .../src/plugin/inner_graph_plugin.rs | 11 +-- 5 files changed, 114 insertions(+), 20 deletions(-) create mode 100644 ' diff --git a/' b/' new file mode 100644 index 000000000000..5702291e7b68 --- /dev/null +++ b/' @@ -0,0 +1,99 @@ +use rspack_core::{ + get_dependency_used_by_exports_condition, module_id, Dependency, DependencyCategory, + DependencyCondition, DependencyId, DependencyTemplate, DependencyType, ErrorSpan, + ModuleDependency, RuntimeGlobals, TemplateContext, TemplateReplaceSource, UsedByExports, +}; +use swc_core::ecma::atoms::JsWord; + +#[derive(Debug, Clone)] +pub struct URLDependency { + start: u32, + end: u32, + id: DependencyId, + request: JsWord, + span: Option, + used_by_exports: Option, +} + +impl URLDependency { + pub fn new(start: u32, end: u32, request: JsWord, span: Option) -> Self { + Self { + start, + end, + id: DependencyId::new(), + request, + span, + used_by_exports: None, + } + } +} + +impl Dependency for URLDependency { + fn id(&self) -> &DependencyId { + &self.id + } + + fn category(&self) -> &DependencyCategory { + &DependencyCategory::Url + } + + fn dependency_type(&self) -> &DependencyType { + &DependencyType::NewUrl + } +} + +impl ModuleDependency for URLDependency { + fn request(&self) -> &str { + &self.request + } + + fn user_request(&self) -> &str { + &self.request + } + + fn span(&self) -> Option<&ErrorSpan> { + self.span.as_ref() + } + + fn set_request(&mut self, request: String) { + self.request = request.into(); + } + + fn get_condition(&self) -> Option { + get_dependency_used_by_exports_condition(self.id, self.used_by_exports.as_ref()) + } + + fn dependency_debug_name(&self) -> &'static str { + "URLDependency" + } +} + +impl DependencyTemplate for URLDependency { + fn apply( + &self, + source: &mut TemplateReplaceSource, + code_generatable_context: &mut TemplateContext, + ) { + let TemplateContext { + compilation, + runtime_requirements, + .. + } = code_generatable_context; + + runtime_requirements.insert(RuntimeGlobals::BASE_URI); + runtime_requirements.insert(RuntimeGlobals::REQUIRE); + + source.replace( + self.start, + self.end, + format!( + "/* asset import */{}({}), {}", + RuntimeGlobals::REQUIRE, + module_id(compilation, &self.id, &self.request, false), + RuntimeGlobals::BASE_URI + ) + .as_str(), + None, + ); + } +} diff --git a/crates/rspack_core/src/exports_info.rs b/crates/rspack_core/src/exports_info.rs index 91666e452925..2bbb1e5c441b 100644 --- a/crates/rspack_core/src/exports_info.rs +++ b/crates/rspack_core/src/exports_info.rs @@ -1109,21 +1109,19 @@ pub enum RuntimeUsageStateType { Used, } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] pub enum UsedByExports { Set(HashSet), Bool(bool), - #[default] - Nil, } // https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/InnerGraph.js#L319-L338 pub fn get_dependency_used_by_exports_condition( dependency_id: DependencyId, - used_by_exports: &UsedByExports, + used_by_exports: Option<&UsedByExports>, ) -> Option { match used_by_exports { - UsedByExports::Set(used_by_exports) => { + Some(UsedByExports::Set(used_by_exports)) => { let used_by_exports = Arc::new(used_by_exports.clone()); Some(DependencyCondition::Fn(Box::new( move |_, runtime, module_graph: &ModuleGraph| { @@ -1142,14 +1140,14 @@ pub fn get_dependency_used_by_exports_condition( }, ))) } - UsedByExports::Bool(bool) => { + Some(UsedByExports::Bool(bool)) => { if *bool { None } else { Some(DependencyCondition::False) } } - UsedByExports::Nil => None, + None => None, } } 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 e627026ab5c1..a5d7a887ecf1 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 @@ -25,7 +25,7 @@ pub struct HarmonyImportSpecifierDependency { call: bool, direct_import: bool, specifier: Specifier, - used_by_exports: UsedByExports, + used_by_exports: Option, namespace_object_as_context: bool, referenced_properties_in_destructuring: Option>, resource_identifier: String, @@ -55,7 +55,7 @@ impl HarmonyImportSpecifierDependency { call, direct_import, specifier, - used_by_exports: UsedByExports::default(), + used_by_exports: None, namespace_object_as_context: false, referenced_properties_in_destructuring, resource_identifier, @@ -229,7 +229,7 @@ impl ModuleDependency for HarmonyImportSpecifierDependency { } fn get_condition(&self) -> Option { - get_dependency_used_by_exports_condition(self.id, &self.used_by_exports) + get_dependency_used_by_exports_condition(self.id, self.used_by_exports.as_ref()) } fn get_referenced_exports( diff --git a/crates/rspack_plugin_javascript/src/dependency/url/mod.rs b/crates/rspack_plugin_javascript/src/dependency/url/mod.rs index 0d03441f81bf..5702291e7b68 100644 --- a/crates/rspack_plugin_javascript/src/dependency/url/mod.rs +++ b/crates/rspack_plugin_javascript/src/dependency/url/mod.rs @@ -12,7 +12,7 @@ pub struct URLDependency { id: DependencyId, request: JsWord, span: Option, - used_by_exports: UsedByExports, + used_by_exports: Option, } impl URLDependency { @@ -23,7 +23,7 @@ impl URLDependency { id: DependencyId::new(), request, span, - used_by_exports: UsedByExports::default(), + used_by_exports: None, } } } @@ -60,7 +60,7 @@ impl ModuleDependency for URLDependency { } fn get_condition(&self) -> Option { - get_dependency_used_by_exports_condition(self.id, &self.used_by_exports) + get_dependency_used_by_exports_condition(self.id, self.used_by_exports.as_ref()) } fn dependency_debug_name(&self) -> &'static str { diff --git a/crates/rspack_plugin_javascript/src/plugin/inner_graph_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/inner_graph_plugin.rs index cca840de0d4c..a03a1e17400f 100644 --- a/crates/rspack_plugin_javascript/src/plugin/inner_graph_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/inner_graph_plugin.rs @@ -49,7 +49,7 @@ pub enum InnerGraphMapUsage { True, } -pub type UsageCallback = Box; +pub type UsageCallback = Box)>; #[derive(Default)] pub struct InnerGraphState { @@ -246,10 +246,10 @@ impl<'a> InnerGraphPlugin<'a> { .or_insert(vec![]) .push(on_usage_callback); } else { - on_usage_callback(UsedByExports::Bool(true)); + on_usage_callback(Some(UsedByExports::Bool(true))); } } else { - on_usage_callback(UsedByExports::Nil); + on_usage_callback(None); } } @@ -257,10 +257,7 @@ impl<'a> InnerGraphPlugin<'a> { self.on_usage( symbol, Box::new(|used_by_exports| { - if matches!( - used_by_exports, - UsedByExports::Nil | UsedByExports::Bool(true) - ) { + if matches!(used_by_exports, None | Some(UsedByExports::Bool(true))) { return; } else { // TODO usedByExports