Skip to content

Commit

Permalink
Merge branch 'main' into fix/5578-2
Browse files Browse the repository at this point in the history
  • Loading branch information
IWANABETHATGUY authored Feb 19, 2024
2 parents 5711880 + f2130a0 commit f304ef6
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 237 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions benchcases/react-refresh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"devDependencies": {
"@rspack/cli": "workspace:*",
"react": "18.0.0",
"react-dom": "18.0.0",
"react-refresh": "0.13.0"
}
}
}
2 changes: 1 addition & 1 deletion crates/rspack_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ itertools = { workspace = true }
json = { workspace = true }
mime_guess = { workspace = true }
once_cell = { workspace = true }
oxc_resolver = { version = "1.5.1" }
oxc_resolver = { version = "1.5.2" }
paste = { workspace = true }
petgraph = { version = "0.6.4", features = ["serde-1"] }
rayon = { workspace = true }
Expand Down
35 changes: 19 additions & 16 deletions crates/rspack_core/src/tree_shaking/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ impl<'a> ModuleRefAnalyze<'a> {
}),
Part::Url(src) => {
let dep_id = self
.resolve_module_identifier(src, &DependencyType::NewUrl)
.resolve_module_identifier(src, &[DependencyType::NewUrl])
.unwrap_or_else(|| panic!("Can't resolve {} in {}", src, self.module_identifier));

Some(SymbolRef::Url {
Expand All @@ -402,7 +402,7 @@ impl<'a> ModuleRefAnalyze<'a> {
}
Part::Worker(src) => {
let dep_id = self
.resolve_module_identifier(src, &DependencyType::NewWorker)
.resolve_module_identifier(src, &[DependencyType::NewWorker])
.unwrap_or_else(|| panic!("Can't resolve {} in {}", src, self.module_identifier));

Some(SymbolRef::Url {
Expand Down Expand Up @@ -523,7 +523,7 @@ impl<'a> Visit for ModuleRefAnalyze<'a> {
},
Part::Url(src) => {
let dep_id = self
.resolve_module_identifier(src, &DependencyType::NewUrl)
.resolve_module_identifier(src, &[DependencyType::NewUrl])
.unwrap_or_else(|| {
panic!("Can't resolve {} in {}", src, self.module_identifier)
});
Expand All @@ -535,7 +535,7 @@ impl<'a> Visit for ModuleRefAnalyze<'a> {
}
Part::Worker(src) => {
let dep_id = self
.resolve_module_identifier(src, &DependencyType::NewWorker)
.resolve_module_identifier(src, &[DependencyType::NewWorker])
.unwrap_or_else(|| {
panic!("Can't resolve {} in {}", src, self.module_identifier)
});
Expand Down Expand Up @@ -584,7 +584,7 @@ impl<'a> Visit for ModuleRefAnalyze<'a> {
},
Part::Url(src) => {
let dep_id = self
.resolve_module_identifier(src, &DependencyType::NewUrl)
.resolve_module_identifier(src, &[DependencyType::NewUrl])
.unwrap_or_else(|| panic!("Can't resolve {} in {}", src, self.module_identifier));

return HashSet::from_iter([SymbolRef::Url {
Expand All @@ -595,7 +595,7 @@ impl<'a> Visit for ModuleRefAnalyze<'a> {
}
Part::Worker(src) => {
let dep_id = self
.resolve_module_identifier(src, &DependencyType::NewWorker)
.resolve_module_identifier(src, &[DependencyType::NewWorker])
.unwrap_or_else(|| panic!("Can't resolve {} in {}", src, self.module_identifier));

return HashSet::from_iter([SymbolRef::Url {
Expand Down Expand Up @@ -640,7 +640,7 @@ impl<'a> Visit for ModuleRefAnalyze<'a> {
},
Part::Url(src) => {
let dep_id = self
.resolve_module_identifier(src, &DependencyType::NewUrl)
.resolve_module_identifier(src, &[DependencyType::NewUrl])
.unwrap_or_else(|| panic!("Can't resolve {} in {}", src, self.module_identifier));

let url = SymbolRef::Url {
Expand All @@ -652,7 +652,7 @@ impl<'a> Visit for ModuleRefAnalyze<'a> {
}
Part::Worker(src) => {
let dep_id = self
.resolve_module_identifier(src, &DependencyType::NewWorker)
.resolve_module_identifier(src, &[DependencyType::NewWorker])
.unwrap_or_else(|| panic!("Can't resolve {} in {}", src, self.module_identifier));

let url = SymbolRef::Url {
Expand Down Expand Up @@ -776,7 +776,7 @@ impl<'a> Visit for ModuleRefAnalyze<'a> {
let src = &import.src.value;
let dep_id = match self.resolve_module_identifier(
src,
&DependencyType::EsmImport(ErrorSpan::from(import.span)),
&[DependencyType::EsmImport(ErrorSpan::from(import.span))],
) {
Some(module_identifier) => module_identifier,
None => {
Expand Down Expand Up @@ -882,7 +882,7 @@ impl<'a> Visit for ModuleRefAnalyze<'a> {
ModuleDecl::ExportAll(export_all) => {
let dep_id = match self.resolve_module_identifier(
&export_all.src.value,
&DependencyType::EsmExport(export_all.span.into()),
&[DependencyType::EsmExport(export_all.span.into())],
) {
Some(module_identifier) => module_identifier,
None => {
Expand Down Expand Up @@ -1106,7 +1106,10 @@ impl<'a> Visit for ModuleRefAnalyze<'a> {
fn visit_call_expr(&mut self, node: &CallExpr) {
if let Some(require_lit) = get_require_literal(node, self.unresolved_ctxt) {
self.module_syntax.insert(ModuleSyntax::COMMONJS);
match self.resolve_module_identifier(&require_lit, &DependencyType::CjsRequire) {
match self.resolve_module_identifier(
&require_lit,
&[DependencyType::CjsRequire, DependencyType::CjsExportRequire],
) {
Some(dep_id) => match self
.bail_out_module_identifiers
.entry(ModuleIdOrDepId::DepId(dep_id))
Expand All @@ -1126,7 +1129,7 @@ impl<'a> Visit for ModuleRefAnalyze<'a> {
}
};
} else if let Some(import_str) = get_dynamic_import_string_literal(node) {
match self.resolve_module_identifier(&import_str, &DependencyType::DynamicImport) {
match self.resolve_module_identifier(&import_str, &[DependencyType::DynamicImport]) {
Some(dep_id) => match self
.bail_out_module_identifiers
.entry(ModuleIdOrDepId::DepId(dep_id))
Expand Down Expand Up @@ -1532,7 +1535,7 @@ impl<'a> ModuleRefAnalyze<'a> {
let src = named_export.src.as_ref().map(|src| &src.value);
if let Some(src) = src {
let dep_id = match self
.resolve_module_identifier(src, &DependencyType::EsmExport(named_export.span.into()))
.resolve_module_identifier(src, &[DependencyType::EsmExport(named_export.span.into())])
{
Some(module_identifier) => module_identifier,
None => {
Expand Down Expand Up @@ -1651,17 +1654,17 @@ impl<'a> ModuleRefAnalyze<'a> {
fn resolve_module_identifier(
&self,
src: &str,
dependency_type: &DependencyType,
dependency_types: &[DependencyType],
) -> Option<DependencyId> {
self.dependencies.iter().find_map(|dep| {
if let Some(dep) = dep.as_module_dependency()
&& dep.request() == src
&& dependency_type == dep.dependency_type()
&& dependency_types.contains(dep.dependency_type())
{
Some(*dep.id())
} else if let Some(dep) = dep.as_context_dependency()
&& dep.request() == src
&& dependency_type == dep.dependency_type()
&& dependency_types.contains(dep.dependency_type())
{
Some(*dep.id())
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use swc_core::atoms::Atom;

use super::{ExportedVariableInfo, JavascriptParser};
use crate::visitors::scope_info::{FreeName, VariableInfo};

/// callHooksForName/callHooksForInfo in webpack
/// webpack use HookMap and filter at callHooksForName/callHooksForInfo
/// we need to pass the name to hook to filter in the hook
pub trait CallHooksName {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String>;
}

impl CallHooksName for &str {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String> {
if let Some(info) = parser.get_variable_info(self.as_ref()) {
call_hooks_info(info)
} else {
Some(self.to_string())
}
}
}

impl CallHooksName for String {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String> {
self.as_str().call_hooks_name(parser)
}
}

impl CallHooksName for Atom {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String> {
self.as_str().call_hooks_name(parser)
}
}

impl CallHooksName for VariableInfo {
fn call_hooks_name(&self, _parser: &mut JavascriptParser) -> Option<String> {
call_hooks_info(self)
}
}

impl CallHooksName for ExportedVariableInfo {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String> {
match self {
ExportedVariableInfo::Name(n) => n.call_hooks_name(parser),
ExportedVariableInfo::VariableInfo(v) => {
let info = parser.definitions_db.expect_get_variable(v);
call_hooks_info(info)
}
}
}
}

fn call_hooks_info(info: &VariableInfo) -> Option<String> {
// TODO: tag_info with hooks
if let Some(FreeName::String(free_name)) = &info.free_name {
Some(free_name.to_string())
} else {
// should run `defined ? defined() : None`
None
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#![allow(unused)]

mod call_hooks_name;
mod walk;
mod walk_block_pre;
mod walk_pre;

use std::borrow::Cow;
use std::fmt::Display;
use std::rc::Rc;
use std::sync::Arc;

use bitflags::bitflags;
pub use call_hooks_name::CallHooksName;
use rspack_core::needs_refactor::WorkerSyntaxList;
use rspack_core::{
AsyncDependenciesBlock, BoxDependency, BuildInfo, BuildMeta, DependencyTemplate,
Expand All @@ -24,10 +23,9 @@ use swc_core::common::util::take::Take;
use swc_core::common::{SourceFile, Span, Spanned};
use swc_core::ecma::ast::{
ArrayPat, AssignPat, CallExpr, Callee, MetaPropExpr, MetaPropKind, ObjectPat, ObjectPatProp, Pat,
Program, Stmt, Super, ThisExpr,
Program, Stmt, ThisExpr,
};
use swc_core::ecma::ast::{BlockStmt, Expr, Ident, Lit, MemberExpr, RestPat};
use swc_core::ecma::utils::ExprFactory;
use swc_core::ecma::ast::{Expr, Ident, Lit, MemberExpr, RestPat};

use crate::parser_plugin::{self, JavaScriptParserPluginDrive, JavascriptParserPlugin};
use crate::utils::eval::{self, BasicEvaluatedExpression};
Expand Down Expand Up @@ -145,63 +143,6 @@ pub struct FreeInfo<'a> {
pub info: Option<&'a VariableInfo>,
}

// callHooksForName/callHooksForInfo in webpack
// webpack use HookMap and filter at callHooksForName/callHooksForInfo
// we need to pass the name to hook to filter in the hook
pub trait CallHooksName {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String>;
}

impl CallHooksName for &str {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String> {
let mut name = *self;
if let Some(info) = parser.get_variable_info(name) {
if let Some(FreeName::String(free_name)) = &info.free_name {
name = free_name;
} else {
return None;
}
}
Some(name.to_string())
}
}

impl CallHooksName for String {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String> {
self.as_str().call_hooks_name(parser)
}
}

impl CallHooksName for Atom {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String> {
self.as_str().call_hooks_name(parser)
}
}

impl CallHooksName for VariableInfo {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String> {
if let Some(FreeName::String(free_name)) = &self.free_name {
return Some(free_name.to_string());
}
None
}
}

impl CallHooksName for ExportedVariableInfo {
fn call_hooks_name(&self, parser: &mut JavascriptParser) -> Option<String> {
match self {
ExportedVariableInfo::Name(n) => n.call_hooks_name(parser),
ExportedVariableInfo::VariableInfo(v) => {
let info = parser.definitions_db.expect_get_variable(v);
if let Some(FreeName::String(free_name)) = &info.free_name {
return Some(free_name.to_string());
}
None
}
}
}
}

#[derive(Clone, Copy, Debug)]
pub enum TopLevelScope {
Top,
Expand Down Expand Up @@ -234,8 +175,6 @@ pub struct JavascriptParser<'parser> {
pub(crate) enter_assign: bool,
// TODO: remove `is_esm` after `HarmonyExports::isEnabled`
pub(crate) is_esm: bool,
// TODO: delete `has_module_ident`
pub(crate) has_module_ident: bool,
pub(crate) parser_exports_state: &'parser mut Option<bool>,
// TODO: delete `enter_call`
pub(crate) enter_call: u32,
Expand Down Expand Up @@ -382,7 +321,6 @@ impl<'parser> JavascriptParser<'parser> {
compiler_options,
module_type,
enter_assign: false,
has_module_ident: false,
parser_exports_state,
enter_call: 0,
stmt_level: 0,
Expand Down Expand Up @@ -489,7 +427,7 @@ impl<'parser> JavascriptParser<'parser> {
&mut self,
object: Expr,
members: Vec<Atom>,
members_range: Vec<Span>,
_members_range: Vec<Span>,
allowed_types: AllowedMemberTypes,
) -> Option<MemberExpressionInfo> {
match object {
Expand Down Expand Up @@ -578,7 +516,7 @@ impl<'parser> JavascriptParser<'parser> {
let value = match lit {
Lit::Str(s) => s.value.clone(),
Lit::Bool(b) => if b.value { "true" } else { "false" }.into(),
Lit::Null(n) => "null".into(),
Lit::Null(_) => "null".into(),
Lit::Num(n) => n.value.to_string().into(),
Lit::BigInt(i) => i.value.to_string().into(),
Lit::Regex(r) => r.exp.clone(),
Expand Down
Loading

0 comments on commit f304ef6

Please sign in to comment.