Skip to content

Commit

Permalink
test: 💍 chore
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
IWANABETHATGUY committed Sep 23, 2023
1 parent 9d19864 commit 5bb0bc5
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 20 deletions.
99 changes: 99 additions & 0 deletions '
Original file line number Diff line number Diff line change
@@ -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<ErrorSpan>,
used_by_exports: Option<UsedByExports>,
}

impl URLDependency {
pub fn new(start: u32, end: u32, request: JsWord, span: Option<ErrorSpan>) -> 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<DependencyCondition> {
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,
);
}
}
12 changes: 5 additions & 7 deletions crates/rspack_core/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,21 +1109,19 @@ pub enum RuntimeUsageStateType {
Used,
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub enum UsedByExports {
Set(HashSet<JsWord>),
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<DependencyCondition> {
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| {
Expand All @@ -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,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct HarmonyImportSpecifierDependency {
call: bool,
direct_import: bool,
specifier: Specifier,
used_by_exports: UsedByExports,
used_by_exports: Option<UsedByExports>,
namespace_object_as_context: bool,
referenced_properties_in_destructuring: Option<HashSet<JsWord>>,
resource_identifier: String,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -229,7 +229,7 @@ impl ModuleDependency for HarmonyImportSpecifierDependency {
}

fn get_condition(&self) -> Option<DependencyCondition> {
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(
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_plugin_javascript/src/dependency/url/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct URLDependency {
id: DependencyId,
request: JsWord,
span: Option<ErrorSpan>,
used_by_exports: UsedByExports,
used_by_exports: Option<UsedByExports>,
}

impl URLDependency {
Expand All @@ -23,7 +23,7 @@ impl URLDependency {
id: DependencyId::new(),
request,
span,
used_by_exports: UsedByExports::default(),
used_by_exports: None,
}
}
}
Expand Down Expand Up @@ -60,7 +60,7 @@ impl ModuleDependency for URLDependency {
}

fn get_condition(&self) -> Option<DependencyCondition> {
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 {
Expand Down
11 changes: 4 additions & 7 deletions crates/rspack_plugin_javascript/src/plugin/inner_graph_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub enum InnerGraphMapUsage {
True,
}

pub type UsageCallback = Box<dyn Fn(UsedByExports)>;
pub type UsageCallback = Box<dyn Fn(Option<UsedByExports>)>;

#[derive(Default)]
pub struct InnerGraphState {
Expand Down Expand Up @@ -246,21 +246,18 @@ 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);
}
}

pub fn on_usage_by_span(&mut self, symbol: Option<JsWord>, start: u32, end: u32) {
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
Expand Down

0 comments on commit 5bb0bc5

Please sign in to comment.