Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ swc_html_minifier = { version = "35.0.0", default-features = false }
swc_node_comments = { version = "16.0.0", default-features = false }
swc_plugin_runner = { version = "21.0.0", default-features = false }

swc_experimental_ecma_ast = { version = "0.1.1", default-features = false }
swc_experimental_ecma_parser = { version = "0.1.1", default-features = false }
swc_experimental_ecma_semantic = { version = "0.1.1", default-features = false }
swc_experimental_ecma_ast = { version = "0.3.0", default-features = false }
swc_experimental_ecma_parser = { version = "0.3.0", default-features = false }
swc_experimental_ecma_semantic = { version = "0.3.0", default-features = false }

rspack_dojang = { version = "0.1.11", default-features = false }
tracy-client = { version = "=0.18.2", default-features = false, features = [
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ swc_core = { workspace = true, features = [
"swc_ecma_codegen",
"swc_ecma_visit",
] }
swc_node_comments = { workspace = true }
tokio = { workspace = true, features = ["rt", "macros"] }
tracing = { workspace = true }
urlencoding = { workspace = true }
Expand Down
103 changes: 20 additions & 83 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ use rspack_util::{
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};
use swc_core::{
atoms::Atom,
common::{FileName, Spanned, SyntaxContext},
ecma::visit::swc_ecma_ast,
common::{Spanned, SyntaxContext},
};
use swc_experimental_ecma_ast::{Ast, ClassExpr, EsVersion, FromNodeId, Ident, NodeKind};
use swc_experimental_ecma_ast::EsVersion;
use swc_experimental_ecma_parser::{EsSyntax, Parser, StringSource, Syntax};
use swc_experimental_ecma_semantic::resolver::{Semantic, UNRESOLVED_SCOPE_ID, resolver};
use swc_node_comments::SwcComments;
use swc_experimental_ecma_semantic::resolver::resolver;

use crate::{
AsyncDependenciesBlockIdentifier, BoxDependency, BoxDependencyTemplate, BoxModuleDependency,
Expand All @@ -48,8 +46,8 @@ use crate::{
ModuleGraphCacheArtifact, ModuleGraphConnection, ModuleIdentifier, ModuleLayer,
ModuleStaticCacheArtifact, ModuleType, NAMESPACE_OBJECT_EXPORT, ParserOptions,
PrefetchExportsInfoMode, Resolve, RuntimeCondition, RuntimeGlobals, RuntimeSpec, SourceType,
URLStaticMode, UsageState, UsedName, UsedNameItem, escape_identifier, filter_runtime,
get_runtime_key, impl_source_map_config, merge_runtime_condition,
URLStaticMode, UsageState, UsedName, UsedNameItem, collect_ident, escape_identifier,
filter_runtime, get_runtime_key, impl_source_map_config, merge_runtime_condition,
merge_runtime_condition_non_false, module_update_hash, property_access, property_name,
render_make_deferred_namespace_mode_from_exports_type, reserved_names::RESERVED_NAMES,
subtract_runtime_condition, to_identifier_with_escaped, to_normal_comment,
Expand Down Expand Up @@ -2295,21 +2293,13 @@ impl ConcatenatedModule {
}

let concatenation_scope = concatenation_scope.expect("should have concatenation_scope");
let mut module_info = concatenation_scope.current_module;

let source = inner
.remove(&SourceType::JavaScript)
.expect("should have javascript source");
let source_code = source.source();

let cm: Arc<swc_core::common::SourceMap> = Default::default();
let fm = cm.new_source_file(
Arc::new(FileName::Custom(format!(
"{}",
self.readable_identifier(&compilation.options.context),
))),
source_code.into_string_lossy().into_owned(),
);
let comments = SwcComments::default();
let mut module_info = concatenation_scope.current_module;
let source_string = source_code.into_string_lossy().into_owned();

let jsx = module
.as_ref()
Expand All @@ -2328,8 +2318,8 @@ impl ConcatenatedModule {
..Default::default()
}),
EsVersion::EsNext,
StringSource::new(fm.src.as_str()),
Some(&comments),
StringSource::new(&source_string),
None,
);
let p = Parser::new_from(lexer);
let ret = p.parse_module();
Expand All @@ -2339,7 +2329,7 @@ impl ConcatenatedModule {
Err(err) => {
// return empty error as we already push error to compilation.diagnostics
return Err(Error::from_string(
Some(fm.src.clone().into_string()),
Some(source_string),
err.span().real_lo() as usize,
err.span().real_hi() as usize,
"JavaScript parse error:\n".to_string(),
Expand All @@ -2353,26 +2343,24 @@ impl ConcatenatedModule {
let semantic = resolver(ret.root, ast);
let ids = collect_ident(ast, &semantic);

module_info.module_ctxt = SyntaxContext::from_u32(semantic.top_level_scope_id().raw());
module_info.global_ctxt = SyntaxContext::from_u32(UNRESOLVED_SCOPE_ID.raw());
module_info.module_ctxt = semantic.top_level_scope_id().to_ctxt();
module_info.global_ctxt = semantic.unresolved_scope_id().to_ctxt();
for ident in ids {
if semantic.node_scope(ident.id) == UNRESOLVED_SCOPE_ID {
module_info
.global_scope_ident
.push(ident.to_legacy(ast, &semantic));
all_used_names.insert(Atom::new(ast.get_utf8(ident.id.sym(ast))));
if ident.id.ctxt == module_info.global_ctxt {
module_info.global_scope_ident.push(ident.clone());
all_used_names.insert(ident.id.sym.clone());
}
if ident.is_class_expr_with_ident {
all_used_names.insert(Atom::new(ast.get_utf8(ident.id.sym(ast))));
all_used_names.insert(ident.id.sym.clone());
continue;
}
// deconflict naming from inner scope, the module level deconflict will be finished
// you could see tests/webpack-test/cases/scope-hoisting/renaming-4967 as a example
// during module eval phase.
if semantic.node_scope(ident.id) != semantic.top_level_scope_id() {
all_used_names.insert(Atom::new(ast.get_utf8(ident.id.sym(ast))));
if ident.id.ctxt != module_info.module_ctxt {
all_used_names.insert(ident.id.sym.clone());
}
module_info.idents.push(ident.to_legacy(ast, &semantic));
module_info.idents.push(ident);
}
module_info.all_used_names = all_used_names;

Expand Down Expand Up @@ -3217,54 +3205,3 @@ pub fn escape_name(name: &str) -> String {

escape_identifier(name).into_owned()
}

#[derive(Clone, Debug)]
pub struct NewConcatenatedModuleIdent {
pub id: Ident,
pub shorthand: bool,
pub is_class_expr_with_ident: bool,
}

impl NewConcatenatedModuleIdent {
pub fn to_legacy(&self, ast: &Ast, semantic: &Semantic) -> ConcatenatedModuleIdent {
let span = self.id.span(ast);
let sym = Atom::new(ast.get_utf8(self.id.sym(ast)));
let ctxt = SyntaxContext::from_u32(semantic.node_scope(self.id).raw());
ConcatenatedModuleIdent {
id: swc_ecma_ast::Ident::new(sym, span, ctxt),
is_class_expr_with_ident: self.is_class_expr_with_ident,
shorthand: self.shorthand,
}
}
}

fn collect_ident(ast: &Ast, semantic: &Semantic) -> Vec<NewConcatenatedModuleIdent> {
let mut ids = Vec::new();
for (node_id, node) in ast.nodes() {
if node.kind == NodeKind::Ident {
let ident = Ident::from_node_id(node_id, ast);
let parent_id = semantic.parent_node(node_id);
let (shorthand, is_class_expr_with_ident) = match ast.get_node(parent_id).kind {
NodeKind::BindingIdent => {
let parent_id = semantic.parent_node(parent_id);
(
ast.get_node(parent_id).kind == NodeKind::AssignPatProp,
false,
)
}
NodeKind::ClassExpr => {
let class_expr = ClassExpr::from_node_id(parent_id, ast);
(false, class_expr.class(ast).super_class(ast).is_some())
}
NodeKind::ObjectLit => (true, false),
_ => (false, false),
};
ids.push(NewConcatenatedModuleIdent {
id: ident,
shorthand,
is_class_expr_with_ident,
});
}
}
ids
}
Loading
Loading