Skip to content

Commit

Permalink
feat(rspack_plugin_javascript): improve provide dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed Sep 12, 2024
1 parent f6f1798 commit 12bbc53
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
24 changes: 24 additions & 0 deletions crates/rspack_core/src/dependency/dependency_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ use std::{fmt, sync::Arc};

use derivative::Derivative;

#[derive(Debug, Clone)]
pub struct DependencyRange {
pub start: u32,
pub end: u32,
}

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

impl From<swc_core::common::Span> for DependencyRange {
fn from(span: swc_core::common::Span) -> Self {
Self {
start: span.lo.0.saturating_sub(1),
end: span.hi.0.saturating_sub(1),
}
}
}

#[derive(Derivative)]
#[derivative(Debug, Clone, Hash)]
pub struct RealDependencyLocation {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use itertools::Itertools;
use rspack_core::DependencyType;
use rspack_core::{
create_exports_object_referenced, module_raw, Compilation, ExtendedReferencedExport, ModuleGraph,
NormalInitFragment, RuntimeSpec, UsedName,
create_exports_object_referenced, module_raw, Compilation, DependencyRange, DependencyType,
ExtendedReferencedExport, ModuleGraph, NormalInitFragment, RuntimeSpec, UsedName,
};
use rspack_core::{AsContextDependency, Dependency, InitFragmentKey, InitFragmentStage};
use rspack_core::{DependencyCategory, DependencyId, DependencyTemplate};
Expand All @@ -12,19 +11,17 @@ use swc_core::atoms::Atom;

#[derive(Debug, Clone)]
pub struct ProvideDependency {
start: u32,
end: u32,
id: DependencyId,
request: Atom,
identifier: String,
ids: Vec<Atom>,
range: DependencyRange,
}

impl ProvideDependency {
pub fn new(start: u32, end: u32, request: Atom, identifier: String, ids: Vec<Atom>) -> Self {
pub fn new(range: DependencyRange, request: Atom, identifier: String, ids: Vec<Atom>) -> Self {
Self {
start,
end,
range,
request,
identifier,
ids,
Expand All @@ -38,6 +35,10 @@ impl Dependency for ProvideDependency {
&self.id
}

fn loc(&self) -> Option<String> {
Some(self.request.to_string())
}

fn category(&self) -> &DependencyCategory {
&DependencyCategory::Esm
}
Expand Down Expand Up @@ -116,7 +117,7 @@ impl DependencyTemplate for ProvideDependency {
InitFragmentKey::ModuleExternal(format!("provided {}", self.identifier)),
None,
)));
source.replace(self.start, self.end, &self.identifier, None);
source.replace(self.range.start, self.range.end, &self.identifier, None);
}

fn dependency_id(&self) -> Option<DependencyId> {
Expand Down
38 changes: 11 additions & 27 deletions crates/rspack_plugin_javascript/src/parser_plugin/provide_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use itertools::Itertools;
use once_cell::sync::OnceCell;
use rspack_core::{
ApplyContext, CompilerOptions, ModuleType, NormalModuleFactoryParser, ParserAndGenerator,
ParserOptions, Plugin, PluginContext, SpanExt,
ApplyContext, CompilerOptions, DependencyRange, ModuleType, NormalModuleFactoryParser,
ParserAndGenerator, ParserOptions, Plugin, PluginContext,
};
use rspack_error::Result;
use rspack_hook::{plugin, plugin_hook};
Expand All @@ -13,20 +13,22 @@ use crate::{
dependency::ProvideDependency, parser_and_generator::JavaScriptParserAndGenerator,
visitors::JavascriptParser, BoxJavascriptParserPlugin,
};

const SOURCE_DOT: &str = r#"."#;
const MODULE_DOT: &str = r#"_dot_"#;

fn dep(value: &ProvideValue, name: &str, start: u32, end: u32) -> Option<ProvideDependency> {
fn create_provide_dep(
name: &str,
value: &ProvideValue,
range: DependencyRange,
) -> Option<ProvideDependency> {
if let Some(requests) = value.get(name) {
let name_identifier = if name.contains(SOURCE_DOT) {
format!("__webpack_provide_{}", name.replace(SOURCE_DOT, MODULE_DOT))
} else {
name.to_string()
};
return Some(ProvideDependency::new(
start,
end,
range,
Atom::from(requests[0].as_str()),
name_identifier,
requests[1..]
Expand Down Expand Up @@ -84,13 +86,7 @@ impl JavascriptParserPlugin for ProvidePlugin {
expr: &swc_core::ecma::ast::CallExpr,
for_name: &str,
) -> Option<bool> {
dep(
&self.provide,
for_name,
expr.callee.span().real_lo(),
expr.callee.span().real_hi(),
)
.map(|dep| {
create_provide_dep(for_name, &self.provide, expr.callee.span().into()).map(|dep| {
parser.dependencies.push(Box::new(dep));
// FIXME: webpack use `walk_expression` here
parser.walk_expr_or_spread(&expr.args);
Expand All @@ -104,13 +100,7 @@ impl JavascriptParserPlugin for ProvidePlugin {
expr: &swc_core::ecma::ast::MemberExpr,
for_name: &str,
) -> Option<bool> {
dep(
&self.provide,
for_name,
expr.span().real_lo(),
expr.span().real_hi(),
)
.map(|dep| {
create_provide_dep(for_name, &self.provide, expr.span().into()).map(|dep| {
parser.dependencies.push(Box::new(dep));
true
})
Expand All @@ -122,13 +112,7 @@ impl JavascriptParserPlugin for ProvidePlugin {
ident: &swc_core::ecma::ast::Ident,
for_name: &str,
) -> Option<bool> {
dep(
&self.provide,
for_name,
ident.span.real_lo(),
ident.span.real_hi(),
)
.map(|dep| {
create_provide_dep(for_name, &self.provide, ident.span.into()).map(|dep| {
parser.dependencies.push(Box::new(dep));
true
})
Expand Down

0 comments on commit 12bbc53

Please sign in to comment.