diff --git a/Cargo.lock b/Cargo.lock index c3eda61c6181..74c7181cb8e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3853,7 +3853,6 @@ dependencies = [ "swc_experimental_ecma_ast", "swc_experimental_ecma_parser", "swc_experimental_ecma_semantic", - "swc_node_comments", "tokio", "tracing", "urlencoding", @@ -4369,7 +4368,6 @@ dependencies = [ "rspack_futures", "rspack_hash", "rspack_hook", - "rspack_javascript_compiler", "rspack_plugin_javascript", "rspack_plugin_runtime", "rspack_util", @@ -4377,6 +4375,9 @@ dependencies = [ "serde_json", "sugar_path", "swc_core", + "swc_experimental_ecma_ast", + "swc_experimental_ecma_parser", + "swc_experimental_ecma_semantic", "tokio", "tracing", ] @@ -4516,6 +4517,9 @@ dependencies = [ "serde_json", "sugar_path", "swc_core", + "swc_experimental_ecma_ast", + "swc_experimental_ecma_parser", + "swc_experimental_ecma_semantic", "swc_node_comments", "tokio", "tracing", @@ -6508,59 +6512,54 @@ dependencies = [ [[package]] name = "swc_experimental_ecma_ast" -version = "0.1.1" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f8d2d1395dee9633e93427f23df04c0dbd6648fe87d8fe8925a1093b5364343" +checksum = "78d9d999c1793ffba298e447566f4644c8e1effcb5a68a7cb96f9e36bf43941d" dependencies = [ "num-bigint", "oxc_index", - "swc_atoms", - "swc_common", + "swc_core", "swc_experimental_ast_macros", "unicode-id-start", ] [[package]] name = "swc_experimental_ecma_parser" -version = "0.1.1" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b28206a21db7bf7cd767cc10ca2726f20f8062b5aa70f1786c41398c86b17411" +checksum = "7da6a68bf51264e6a95cee44fc3a5cee4d75461d2c9b1d7c842874946e76cf89" dependencies = [ "bitflags 2.9.1", "either", "num-bigint", - "oxc_index", "rustc-hash", "seq-macro", "smartstring", - "stacker", - "swc_atoms", - "swc_common", + "swc_core", "swc_experimental_ecma_ast", "tracing", ] [[package]] name = "swc_experimental_ecma_semantic" -version = "0.1.1" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e26a9fe675fe7c79b7eaccd52b8dcb9ff99505ecaae020a50e3843d861e4ddc9" +checksum = "1713abe315d207e69a6a418da2ecabaeee887278135a5626c7dae0b019676aa4" dependencies = [ "bitflags 2.9.1", "oxc_index", "rustc-hash", - "swc_common", + "swc_core", "swc_experimental_ecma_ast", "swc_experimental_ecma_visit", ] [[package]] name = "swc_experimental_ecma_visit" -version = "0.1.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98f09d9eb1fe206b4813304f0145dab14ce39eda21b0a785a33d3106924a9570" +checksum = "ec14f38ebeec39e138c3fb7127dd0867f3a6d531a5fea73715ecbd65f4fb249d" dependencies = [ - "swc_common", "swc_experimental_ecma_ast", ] diff --git a/Cargo.toml b/Cargo.toml index 7c664784330e..f115d02ee78a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [ diff --git a/crates/rspack_core/Cargo.toml b/crates/rspack_core/Cargo.toml index b394f6d5552a..639e067051e8 100644 --- a/crates/rspack_core/Cargo.toml +++ b/crates/rspack_core/Cargo.toml @@ -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 } diff --git a/crates/rspack_core/src/concatenated_module.rs b/crates/rspack_core/src/concatenated_module.rs index c33383ed7a1f..2fd33bb36f66 100644 --- a/crates/rspack_core/src/concatenated_module.rs +++ b/crates/rspack_core/src/concatenated_module.rs @@ -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, @@ -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, @@ -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 = 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() @@ -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(); @@ -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(), @@ -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; @@ -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 { - 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 -} diff --git a/crates/rspack_core/src/utils/concatenated_module_visitor.rs b/crates/rspack_core/src/utils/concatenated_module_visitor.rs index f7c21df1c8a6..865e2a87545b 100644 --- a/crates/rspack_core/src/utils/concatenated_module_visitor.rs +++ b/crates/rspack_core/src/utils/concatenated_module_visitor.rs @@ -1,79 +1,46 @@ -use swc_core::ecma::{ - ast::{ClassExpr, Ident, ObjectPatProp, Prop}, - visit::{Visit, VisitWith, noop_visit_type}, -}; +use swc_core::{atoms::Atom, ecma::ast::Ident as LegacyIdent}; +use swc_experimental_ecma_ast::{Ast, ClassExpr, Ident, NodeIdTrait, NodeKind}; +use swc_experimental_ecma_semantic::resolver::Semantic; #[derive(Clone, Debug)] pub struct ConcatenatedModuleIdent { - pub id: Ident, + pub id: LegacyIdent, pub shorthand: bool, pub is_class_expr_with_ident: bool, } -#[derive(Default)] -pub struct IdentCollector { - pub ids: Vec, -} - -impl IdentCollector { - pub fn new(ids: Vec) -> Self { - Self { ids } - } -} - -impl Visit for IdentCollector { - noop_visit_type!(); - - fn visit_ident(&mut self, node: &Ident) { - self.ids.push(ConcatenatedModuleIdent { - id: node.clone(), - shorthand: false, - is_class_expr_with_ident: false, - }); - } - - fn visit_object_pat_prop(&mut self, n: &ObjectPatProp) { - match n { - ObjectPatProp::Assign(assign) => { - self.ids.push(ConcatenatedModuleIdent { - id: assign.key.clone().into(), - shorthand: true, - is_class_expr_with_ident: false, - }); - assign.value.visit_with(self); - } - ObjectPatProp::KeyValue(_) | ObjectPatProp::Rest(_) => { - n.visit_children_with(self); - } - } - } - - fn visit_prop(&mut self, node: &Prop) { - match node { - Prop::Shorthand(node) => { - self.ids.push(ConcatenatedModuleIdent { - id: node.clone(), - shorthand: true, - is_class_expr_with_ident: false, - }); - } - _ => { - node.visit_children_with(self); - } - } - } - - /// https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ConcatenatedModule.js#L1173-L1197 - fn visit_class_expr(&mut self, node: &ClassExpr) { - if let Some(ref ident) = node.ident - && node.class.super_class.is_some() - { - self.ids.push(ConcatenatedModuleIdent { - id: ident.clone(), - shorthand: false, - is_class_expr_with_ident: true, +pub fn collect_ident(ast: &Ast, semantic: &Semantic) -> Vec { + 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, + ) + } + // https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ConcatenatedModule.js#L1173-L1197 + 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), + }; + + let span = ident.span(ast); + let sym = Atom::new(ast.get_utf8(ident.sym(ast))); + let ctxt = semantic.node_scope(ident).to_ctxt(); + ids.push(ConcatenatedModuleIdent { + id: LegacyIdent::new(sym, span, ctxt), + shorthand, + is_class_expr_with_ident, }); } - node.class.visit_with(self); } + ids } diff --git a/crates/rspack_plugin_esm_library/Cargo.toml b/crates/rspack_plugin_esm_library/Cargo.toml index 64fe7eea3178..59b226e10435 100644 --- a/crates/rspack_plugin_esm_library/Cargo.toml +++ b/crates/rspack_plugin_esm_library/Cargo.toml @@ -11,27 +11,30 @@ repository.workspace = true version.workspace = true [dependencies] -async-trait = { workspace = true } -atomic_refcell = { workspace = true } -rayon = { workspace = true } -regex = { workspace = true } -rspack_cacheable = { workspace = true } -rspack_collections = { workspace = true } -rspack_core = { workspace = true } -rspack_error = { workspace = true } -rspack_futures = { workspace = true } -rspack_hash = { workspace = true } -rspack_hook = { workspace = true } -rspack_javascript_compiler = { workspace = true } -rspack_plugin_javascript = { workspace = true } -rspack_plugin_runtime = { workspace = true } -rspack_util = { workspace = true } -serde = { workspace = true } -serde_json = { workspace = true } -sugar_path = { workspace = true } -swc_core = { workspace = true, features = ["swc_ecma_transforms_base"] } -tokio = { workspace = true } -tracing = { workspace = true } +async-trait = { workspace = true } +atomic_refcell = { workspace = true } +rayon = { workspace = true } +regex = { workspace = true } +rspack_cacheable = { workspace = true } +rspack_collections = { workspace = true } +rspack_core = { workspace = true } +rspack_error = { workspace = true } +rspack_futures = { workspace = true } +rspack_hash = { workspace = true } +rspack_hook = { workspace = true } +rspack_plugin_javascript = { workspace = true } +rspack_plugin_runtime = { workspace = true } +rspack_util = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +sugar_path = { workspace = true } +swc_core = { workspace = true, features = ["swc_ecma_transforms_base"] } +tokio = { workspace = true } +tracing = { workspace = true } + +swc_experimental_ecma_ast = { workspace = true } +swc_experimental_ecma_parser = { workspace = true } +swc_experimental_ecma_semantic = { workspace = true } [lints] workspace = true diff --git a/crates/rspack_plugin_esm_library/src/link.rs b/crates/rspack_plugin_esm_library/src/link.rs index e943a689b8e3..d03c09b7e360 100644 --- a/crates/rspack_plugin_esm_library/src/link.rs +++ b/crates/rspack_plugin_esm_library/src/link.rs @@ -10,33 +10,28 @@ use rspack_core::{ BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, ChunkInitFragments, ChunkUkey, CodeGenerationPublicPathAutoReplace, Compilation, ConcatenatedModuleIdent, DependencyType, ExportMode, ExportModeNormalReexport, ExportProvided, ExportsInfoGetter, ExportsType, - ExternalModule, FindTargetResult, GetUsedNameParam, IdentCollector, InitFragmentKey, - InitFragmentStage, MaybeDynamicTargetExportInfoHashKey, ModuleGraph, ModuleGraphCacheArtifact, - ModuleIdentifier, ModuleInfo, NAMESPACE_OBJECT_EXPORT, NormalInitFragment, NormalReexportItem, - PathData, PrefetchExportsInfoMode, RuntimeGlobals, SourceType, URLStaticMode, UsageState, - UsedName, UsedNameItem, escape_name, find_new_name, get_cached_readable_identifier, + ExternalModule, FindTargetResult, GetUsedNameParam, InitFragmentKey, InitFragmentStage, + MaybeDynamicTargetExportInfoHashKey, ModuleGraph, ModuleGraphCacheArtifact, ModuleIdentifier, + ModuleInfo, NAMESPACE_OBJECT_EXPORT, NormalInitFragment, NormalReexportItem, PathData, + PrefetchExportsInfoMode, RuntimeGlobals, SourceType, URLStaticMode, UsageState, UsedName, + UsedNameItem, collect_ident, escape_name, find_new_name, get_cached_readable_identifier, get_js_chunk_filename_template, get_target, property_access, property_name, reserved_names::RESERVED_NAMES, rspack_sources::ReplaceSource, split_readable_identifier, to_normal_comment, }; use rspack_error::{Diagnostic, Result}; -use rspack_javascript_compiler::ast::Ast; use rspack_plugin_javascript::{ JS_DEFAULT_KEYWORD, JsPlugin, RenderSource, dependency::ESMExportImportedSpecifierDependency, - visitors::swc_visitor::resolver, }; use rspack_util::{ atom::Atom, fx_hash::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, indexmap}, swc::join_atom, }; -use swc_core::{ - common::{FileName, SyntaxContext}, - ecma::{ - ast::{EsVersion, Program}, - parser::{Syntax, parse_file_as_module}, - }, -}; +use swc_core::common::SyntaxContext; +use swc_experimental_ecma_ast::EsVersion; +use swc_experimental_ecma_parser::{Syntax, parse_file_as_module}; +use swc_experimental_ecma_semantic::resolver::resolver; use crate::{ EsmLibraryPlugin, @@ -765,47 +760,25 @@ var {} = {{}}; .current_module .clone(); - let m = module_graph - .module_by_identifier(&id) - .expect("should have module"); - let cm: Arc = Default::default(); - let readable_identifier = m.readable_identifier(&compilation.options.context); - let fm = cm.new_source_file( - Arc::new(FileName::Custom(readable_identifier.clone().into_owned())), - render_source - .source - .source() - .into_string_lossy() - .into_owned(), - ); - let mut errors = vec![]; - let module = parse_file_as_module( - &fm, - Syntax::default(), - EsVersion::EsNext, - None, - &mut errors, - ) - .expect("parse failed"); - let mut ast = Ast::new(Program::Module(module), cm, None); + let source_string = render_source + .source + .source() + .into_string_lossy() + .into_owned(); - let mut global_ctxt = SyntaxContext::empty(); - let mut module_ctxt = SyntaxContext::empty(); - let mut collector = IdentCollector::default(); - let mut all_used_names = FxHashSet::default(); - ast.transform(|program, context| { - global_ctxt = global_ctxt.apply_mark(context.unresolved_mark); - module_ctxt = module_ctxt.apply_mark(context.top_level_mark); - program.visit_mut_with(&mut resolver( - context.unresolved_mark, - context.top_level_mark, - false, - )); - program.visit_with(&mut collector); - }); + let ret = + parse_file_as_module(&source_string, Syntax::default(), EsVersion::EsNext, None) + .expect("parse failed"); + let ast = &ret.ast; + let semantic = resolver(ret.root, ast); + let global_ctxt = semantic.unresolved_scope_id().to_ctxt(); + let module_ctxt = semantic.top_level_scope_id().to_ctxt(); + let ids = collect_ident(ast, &semantic); + + let mut all_used_names = FxHashSet::default(); let mut idents = vec![]; - for ident in collector.ids { + for ident in ids { if ident.id.ctxt == global_ctxt { all_used_names.insert(ident.id.sym.clone()); } diff --git a/crates/rspack_plugin_javascript/Cargo.toml b/crates/rspack_plugin_javascript/Cargo.toml index 4482659d1fe0..f01eacf2ec2d 100644 --- a/crates/rspack_plugin_javascript/Cargo.toml +++ b/crates/rspack_plugin_javascript/Cargo.toml @@ -56,6 +56,11 @@ tracing = { workspace = true } url = { workspace = true } winnow = { workspace = true } +swc_experimental_ecma_ast = { workspace = true } +swc_experimental_ecma_parser = { workspace = true } +swc_experimental_ecma_semantic = { workspace = true } + + [package.metadata.cargo-shear] ignored = ["tracing"] diff --git a/crates/rspack_plugin_javascript/src/plugin/mod.rs b/crates/rspack_plugin_javascript/src/plugin/mod.rs index d9971d268240..0a1139636083 100644 --- a/crates/rspack_plugin_javascript/src/plugin/mod.rs +++ b/crates/rspack_plugin_javascript/src/plugin/mod.rs @@ -29,7 +29,7 @@ use rspack_collections::{Identifier, IdentifierDashMap, IdentifierLinkedMap, Ide use rspack_core::{ ChunkGraph, ChunkGroupUkey, ChunkInitFragments, ChunkRenderContext, ChunkUkey, CodeGenerationDataTopLevelDeclarations, Compilation, CompilationId, ConcatenatedModuleIdent, - ExportsArgument, IdentCollector, Module, RuntimeGlobals, RuntimeVariable, SourceType, + ExportsArgument, Module, RuntimeGlobals, RuntimeVariable, SourceType, collect_ident, concatenated_module::find_new_name, render_init_fragments, reserved_names::RESERVED_NAMES, @@ -39,7 +39,6 @@ use rspack_core::{ use rspack_error::{Result, ToStringResultToRspackResultExt}; use rspack_hash::{RspackHash, RspackHashDigest}; use rspack_hook::plugin; -use rspack_javascript_compiler::ast::Ast; use rspack_util::SpanExt; #[cfg(allocative)] use rspack_util::allocative; @@ -47,9 +46,9 @@ use rustc_hash::FxHashMap; pub use side_effects_flag_plugin::*; use swc_core::{ atoms::Atom, - common::{FileName, Spanned, SyntaxContext}, - ecma::transforms::base::resolver, + common::{Spanned, SyntaxContext}, }; +use swc_experimental_ecma_semantic::resolver::resolver; use tokio::sync::RwLock; use crate::runtime::{ @@ -1103,44 +1102,24 @@ var {} = {{}}; } if !use_cache { - let cm: Arc = Default::default(); - let fm = cm.new_source_file( - Arc::new(FileName::Custom(m.identifier().to_string())), - code.source().into_string_lossy().into_owned(), - ); - let comments = swc_node_comments::SwcComments::default(); - let mut errors = vec![]; - - if let Ok(program) = swc_core::ecma::parser::parse_file_as_program( - &fm, - swc_core::ecma::parser::Syntax::default(), - swc_core::ecma::ast::EsVersion::EsNext, - Some(&comments), - &mut errors, + let source_string = code.source().into_string_lossy().into_owned(); + + if let Ok(ret) = swc_experimental_ecma_parser::parse_file_as_program( + &source_string, + swc_experimental_ecma_parser::Syntax::default(), + swc_experimental_ecma_ast::EsVersion::EsNext, + None, ) { - let mut ast: Ast = Ast::new(program, cm, Some(comments)); - let mut global_ctxt = SyntaxContext::empty(); - let mut module_ctxt = SyntaxContext::empty(); - - ast.transform(|program, context| { - global_ctxt = global_ctxt.apply_mark(context.unresolved_mark); - module_ctxt = module_ctxt.apply_mark(context.top_level_mark); - program.visit_mut_with(&mut resolver( - context.unresolved_mark, - context.top_level_mark, - false, - )); - }); - - let mut collector = IdentCollector::default(); - ast.visit(|program, _ctxt| { - program.visit_with(&mut collector); - }); + let ast = &ret.ast; + let semantic = resolver(ret.root, ast); + let global_ctxt = semantic.unresolved_scope_id().to_ctxt(); + let module_ctxt = semantic.top_level_scope_id().to_ctxt(); + let ids = collect_ident(ast, &semantic); if is_inlined_module { let mut module_scope_idents = Vec::new(); - for ident in collector.ids { + for ident in ids { if ident.id.ctxt == global_ctxt || ident.id.ctxt != module_ctxt || ident.is_class_expr_with_ident @@ -1177,7 +1156,7 @@ var {} = {{}}; let module_ident = m.identifier(); let runtime = compilation.chunk_by_ukey.expect_get(chunk_ukey).runtime(); - for ident in collector.ids { + for ident in ids { if ident.id.ctxt == global_ctxt { acc.all_used_names.insert(ident.clone().id.sym.clone()); idents_vec.push(ident.clone());