Skip to content

Commit

Permalink
refactor: use BasicEvaluatedExpression (#4854)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi authored Dec 4, 2023
1 parent 10d4cb5 commit 1dd81be
Show file tree
Hide file tree
Showing 16 changed files with 513 additions and 204 deletions.
2 changes: 2 additions & 0 deletions crates/rspack_core/src/dependencies_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ impl DependencyLocation {
Self { start, end }
}

#[inline]
pub fn start(&self) -> u32 {
self.start
}

#[inline]
pub fn end(&self) -> u32 {
self.end
}
Expand Down
9 changes: 4 additions & 5 deletions crates/rspack_core/src/dependency/runtime_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,11 @@ pub fn module_raw(
}
}

pub fn miss_module(request: &str) -> String {
fn miss_module(request: &str) -> String {
format!("Object({}())", throw_missing_module_error_function(request))
}

pub fn throw_missing_module_error_function(request: &str) -> String {
fn throw_missing_module_error_function(request: &str) -> String {
format!(
"function webpackMissingModule() {{ {} }}",
throw_missing_module_error_block(request)
Expand All @@ -353,9 +353,8 @@ pub fn throw_missing_module_error_block(request: &str) -> String {
)
}

pub fn weak_error(request: &str) -> String {
let msg = format!("Module is not available (weak dependency), request is {request}.");
format!("var e = new Error('{msg}'); e.code = 'MODULE_NOT_FOUND'; throw e;")
fn weak_error(request: &str) -> String {
format!("var e = new Error('Module is not available (weak dependency), request is {request}'); e.code = 'MODULE_NOT_FOUND'; throw e;")
}

pub fn returning_function(return_value: &str, args: &str) -> String {
Expand Down
9 changes: 5 additions & 4 deletions crates/rspack_plugin_javascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ version = "0.1.0"
rspack_testing = { path = "../rspack_testing" }

[dependencies]
anyhow = { workspace = true }
anyhow = { workspace = true }
async-recursion = { workspace = true }
async-trait = { workspace = true }
either = "1"
indexmap = { workspace = true }
async-trait = { workspace = true }
either = "1"
indexmap = { workspace = true }
linked_hash_set = { workspace = true }
# num-bigint = { version = "0.4.4" }
once_cell = { workspace = true }
preset_env_base = { workspace = true }
rayon = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
use rspack_core::{module_id, AsContextDependency, Dependency, DependencyCategory};
use rspack_core::module_id;
use rspack_core::{AsContextDependency, Dependency, DependencyCategory, DependencyLocation};
use rspack_core::{DependencyId, DependencyTemplate};
use rspack_core::{DependencyType, ErrorSpan, ModuleDependency, RuntimeGlobals};
use rspack_core::{DependencyType, ErrorSpan, ModuleDependency};
use rspack_core::{TemplateContext, TemplateReplaceSource};
use swc_core::ecma::atoms::JsWord;

// Webpack RequireHeaderDependency + CommonJsRequireDependency
#[derive(Debug, Clone)]
pub struct CommonJsRequireDependency {
id: DependencyId,
request: JsWord,
request: String,
optional: bool,
start: u32,
end: u32,
loc: DependencyLocation,
span: Option<ErrorSpan>,
}

impl CommonJsRequireDependency {
pub fn new(
request: JsWord,
request: String,
span: Option<ErrorSpan>,
start: u32,
end: u32,
optional: bool,
) -> Self {
let loc = DependencyLocation::new(start, end);
Self {
id: DependencyId::new(),
request,
optional,
start,
end,
loc,
span,
}
}
Expand Down Expand Up @@ -70,7 +68,7 @@ impl ModuleDependency for CommonJsRequireDependency {
}

fn set_request(&mut self, request: String) {
self.request = request.into();
self.request = request;
}
}

Expand All @@ -80,20 +78,14 @@ impl DependencyTemplate for CommonJsRequireDependency {
source: &mut TemplateReplaceSource,
code_generatable_context: &mut TemplateContext,
) {
let TemplateContext {
runtime_requirements,
compilation,
..
} = code_generatable_context;

runtime_requirements.insert(RuntimeGlobals::REQUIRE);
source.replace(
self.start,
self.end,
format!(
"{}({})",
RuntimeGlobals::REQUIRE,
module_id(compilation, &self.id, &self.request, false).as_str()
self.loc.start(),
self.loc.end() - 1,
module_id(
code_generatable_context.compilation,
&self.id,
&self.request,
false,
)
.as_str(),
None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
mod common_js_exports_dependency;
mod common_js_require_dependency;
// mod common_js
mod module_decorator_dependency;
mod require_header_dependency;
mod require_resolve_dependency;

pub use common_js_exports_dependency::CommonJsExportsDependency;
pub use common_js_exports_dependency::ExportsBase;
pub use common_js_require_dependency::CommonJsRequireDependency;
mod require_resolve_dependency;
pub use require_resolve_dependency::RequireResolveDependency;
mod module_decorator_dependency;
pub use module_decorator_dependency::ModuleDecoratorDependency;
pub use require_header_dependency::RequireHeaderDependency;
pub use require_resolve_dependency::RequireResolveDependency;
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use rspack_core::{AsContextDependency, AsModuleDependency, Dependency};
use rspack_core::{DependencyId, DependencyLocation};
use rspack_core::{DependencyTemplate, RuntimeGlobals, TemplateContext};

#[derive(Debug, Clone)]
pub struct RequireHeaderDependency {
id: DependencyId,
loc: DependencyLocation,
}

impl RequireHeaderDependency {
pub fn new(start: u32, end: u32) -> Self {
let loc = DependencyLocation::new(start, end);
Self {
id: DependencyId::new(),
loc,
}
}
}

impl Dependency for RequireHeaderDependency {
fn dependency_debug_name(&self) -> &'static str {
"RequireHeaderDependency"
}

fn id(&self) -> &DependencyId {
&self.id
}
}

impl AsModuleDependency for RequireHeaderDependency {}
impl AsContextDependency for RequireHeaderDependency {}

impl DependencyTemplate for RequireHeaderDependency {
fn apply(
&self,
source: &mut rspack_core::TemplateReplaceSource,
code_generatable_context: &mut rspack_core::TemplateContext,
) {
let TemplateContext {
runtime_requirements,
..
} = code_generatable_context;
runtime_requirements.insert(RuntimeGlobals::REQUIRE);
source.replace(
self.loc.start(),
self.loc.end() - 1,
RuntimeGlobals::REQUIRE.name(),
None,
);
}
}
8 changes: 4 additions & 4 deletions crates/rspack_plugin_javascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
#![feature(box_patterns)]
#![recursion_limit = "256"]

pub(crate) mod dependency;
mod plugin;
pub use plugin::*;
pub mod ast;
pub(crate) mod parser_and_generator;
pub mod dependency;
pub mod parser_and_generator;
mod plugin;
pub mod runtime;
pub mod utils;
pub mod visitors;

pub use crate::plugin::infer_async_modules_plugin::InferAsyncModulesPlugin;
pub use crate::plugin::*;

#[derive(Debug)]
pub struct TransformOutput {
Expand Down
Loading

0 comments on commit 1dd81be

Please sign in to comment.