Skip to content
Merged
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
231 changes: 106 additions & 125 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,15 @@ rkyv = { version = "=0.8.8", default-features = false, features = ["std", "

# Must be pinned with the same swc versions
pnp = { version = "0.12.1", default-features = false }
swc = { version = "43.0.0", default-features = false }
swc = { version = "44.0.0", default-features = false }
swc_config = { version = "3.1.2", default-features = false }
swc_core = { version = "45.0.1", default-features = false, features = ["parallel_rayon"] }
swc_ecma_lexer = { version = "=24.0.0", default-features = false }
swc_ecma_minifier = { version = "34.0.1", default-features = false }
swc_error_reporters = { version = "17.0.0", default-features = false }
swc_html = { version = "27.0.0", default-features = false }
swc_html_minifier = { version = "34.0.0", default-features = false }
swc_node_comments = { version = "15.0.0", default-features = false }
swc_plugin_runner = { version = "20.0.0", default-features = false }
swc_core = { version = "46.0.2", default-features = false, features = ["parallel_rayon"] }
swc_ecma_minifier = { version = "35.0.0", default-features = false }
swc_error_reporters = { version = "18.0.0", default-features = false }
swc_html = { version = "28.0.0", default-features = false }
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 }

wasi-common = { version = "35.0.0", default-features = false }
wasmtime = { version = "35.0.0", default-features = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub struct DependencyVisitor {
impl Visit for DependencyVisitor {
/// handle `import .. from "..."`
fn visit_import_decl(&mut self, node: &ImportDecl) {
self.requests.push(node.src.value.to_string());
self
.requests
.push(node.src.value.to_string_lossy().to_string());
}

/// handle `import("...")` and `require("...")`
Expand All @@ -28,19 +30,21 @@ impl Visit for DependencyVisitor {
&& let Some(args) = node.args.first()
&& let Expr::Lit(Lit::Str(s)) = args.expr.as_ref()
{
self.requests.push(s.value.to_string());
self.requests.push(s.value.to_string_lossy().to_string());
}
}

/// handle `export * from "..."`
fn visit_export_all(&mut self, node: &ExportAll) {
self.requests.push(node.src.value.to_string());
self
.requests
.push(node.src.value.to_string_lossy().to_string());
}

/// handle `export .. from "..."`
fn visit_named_export(&mut self, node: &swc_core::ecma::ast::NamedExport) {
if let Some(src) = &node.src {
self.requests.push(src.value.to_string());
self.requests.push(src.value.to_string_lossy().to_string());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl LazyDependencies {
ForwardedIdSet::All => self.all_lazy_dependencies().collect(),
ForwardedIdSet::IdSet(set) => set
.iter()
.filter(|forward_id| !self.terminal_forward_ids.contains(forward_id))
.filter(|forward_id| !self.terminal_forward_ids.contains(*forward_id))
.flat_map(|forward_id| {
self
.forward_id_to_request
Expand Down
17 changes: 9 additions & 8 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
borrow::Cow,
collections::{BTreeMap, hash_map::Entry},
collections::BTreeMap,
fmt::Debug,
hash::{BuildHasherDefault, Hasher},
sync::{Arc, LazyLock},
Expand All @@ -22,7 +22,8 @@ use rspack_sources::{
BoxSource, CachedSource, ConcatSource, RawStringSource, ReplaceSource, Source, SourceExt,
};
use rspack_util::{
SpanExt, ext::DynHash, itoa, json_stringify, source_map::SourceMapKind, swc::join_atom,
SpanExt, ext::DynHash, fx_hash::FxIndexMap, itoa, json_stringify, source_map::SourceMapKind,
swc::join_atom,
};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};
use swc_core::{
Expand Down Expand Up @@ -204,7 +205,7 @@ pub struct ConcatenatedModuleInfo {
pub global_scope_ident: Vec<ConcatenatedModuleIdent>,
pub idents: Vec<ConcatenatedModuleIdent>,
pub all_used_names: HashSet<Atom>,
pub binding_to_ref: HashMap<(Atom, SyntaxContext), Vec<ConcatenatedModuleIdent>>,
pub binding_to_ref: FxIndexMap<(Atom, SyntaxContext), Vec<ConcatenatedModuleIdent>>,

pub public_path_auto_replacement: Option<bool>,
pub static_url_replacement: bool,
Expand All @@ -217,7 +218,7 @@ impl ConcatenatedModuleInfo {
}

if atom.as_str() == "default" {
return self.internal_names.get(&DEFAULT_EXPORT_ATOM);
return self.internal_names.get(&*DEFAULT_EXPORT_ATOM);
}

if let Some(name) = &self.namespace_export_symbol
Expand Down Expand Up @@ -2319,15 +2320,15 @@ impl ConcatenatedModule {
}
module_info.all_used_names = all_used_names;

let mut binding_to_ref: HashMap<(Atom, SyntaxContext), Vec<ConcatenatedModuleIdent>> =
HashMap::default();
let mut binding_to_ref: FxIndexMap<(Atom, SyntaxContext), Vec<ConcatenatedModuleIdent>> =
Default::default();

for ident in module_info.idents.iter() {
match binding_to_ref.entry((ident.id.sym.clone(), ident.id.ctxt)) {
Entry::Occupied(mut occ) => {
indexmap::map::Entry::Occupied(mut occ) => {
occ.get_mut().push(ident.clone());
}
Entry::Vacant(vac) => {
indexmap::map::Entry::Vacant(vac) => {
vac.insert(vec![ident.clone()]);
}
};
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_javascript_compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ swc_core = { workspace = true, features = [
"swc_ecma_codegen",
"swc_ecma_visit",
] }
swc_ecma_lexer = { workspace = true }
swc_ecma_minifier = { workspace = true, features = ["concurrent"] }
swc_error_reporters = { workspace = true }
swc_node_comments = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_javascript_compiler/src/compiler/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ fn parse_with_lexer(
is_module: IsModule,
with_tokens: bool,
) -> Result<(SwcProgram, Option<Vec<TokenAndSpan>>), Vec<parser::error::Error>> {
use swc_ecma_lexer::common::parser::{Parser as _, buffer::Buffer};
let inner = || {
// don't call capturing when with_tokens is false to avoid performance cost
let (tokens, program_result, mut errors) = if with_tokens {
Expand Down
11 changes: 7 additions & 4 deletions crates/rspack_loader_swc/src/collect_ts_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rspack_swc_plugin_ts_collector::{
EnumMemberValue, ExportedEnumCollector, TypeExportsCollector,
};
use rustc_hash::FxHashMap;
use swc::atoms::Atom;
use swc::atoms::{Atom, Wtf8Atom};
use swc_core::{
common::SyntaxContext,
ecma::{ast::Program, visit::VisitWith},
Expand All @@ -20,7 +20,8 @@ pub fn collect_typescript_info(
if options.type_exports.unwrap_or_default() {
program.visit_with(&mut TypeExportsCollector::new(&mut type_exports));
}
let mut exported_enums: FxHashMap<Atom, FxHashMap<Atom, EnumMemberValue>> = Default::default();
let mut exported_enums: FxHashMap<Atom, FxHashMap<Wtf8Atom, EnumMemberValue>> =
Default::default();
if let Some(kind) = &options.exported_enum {
program.visit_with(&mut ExportedEnumCollector::new(
matches!(kind, CollectingEnumKind::ConstOnly),
Expand All @@ -39,10 +40,12 @@ pub fn collect_typescript_info(
.map(|(id, v)| {
let value = match v {
EnumMemberValue::Number(n) => Some(EvaluatedInlinableValue::new_number(n)),
EnumMemberValue::String(s) => Some(EvaluatedInlinableValue::new_string(s)),
EnumMemberValue::String(s) => Some(EvaluatedInlinableValue::new_string(
s.to_atom_lossy().into_owned(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use Wtf8Atom here? Enum values may contain unpaired surrogates.

)),
EnumMemberValue::Unknown => None,
};
(id, value)
(id.to_atom_lossy().into_owned(), value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum name would contain unpaired surrogates as well

})
.collect(),
);
Expand Down
15 changes: 6 additions & 9 deletions crates/rspack_plugin_esm_library/src/chunk_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ impl ExternalInterop {
if let Some(namespace_object) = &self.namespace_object {
namespace_object.clone()
} else {
let mut new_name = format!(
let mut new_name = Atom::new(format!(
"{}_namespace",
self.required_symbol.as_ref().expect("already set")
)
.into();
));

if used_names.contains(&new_name) {
new_name = find_new_name(new_name.as_str(), used_names, &vec![]);
Expand All @@ -112,11 +111,10 @@ impl ExternalInterop {
if let Some(namespace_object) = &self.namespace_object2 {
namespace_object.clone()
} else {
let mut new_name = format!(
let mut new_name = Atom::new(format!(
"{}_namespace2",
self.required_symbol.as_ref().expect("already set")
)
.into();
));

if used_names.contains(&new_name) {
new_name = find_new_name(new_name.as_str(), used_names, &vec![]);
Expand All @@ -137,11 +135,10 @@ impl ExternalInterop {
if let Some(default_access) = &self.default_access {
default_access.clone()
} else {
let mut new_name = format!(
let mut new_name = Atom::new(format!(
"{}_default",
self.required_symbol.as_ref().expect("already set")
)
.into();
));

if used_names.contains(&new_name) {
new_name = find_new_name(new_name.as_str(), used_names, &vec![]);
Expand Down
23 changes: 15 additions & 8 deletions crates/rspack_plugin_esm_library/src/link.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{self, hash_map::Entry},
collections::{self},
hash::BuildHasher,
sync::Arc,
};
Expand Down Expand Up @@ -27,7 +27,7 @@ use rspack_plugin_javascript::{
};
use rspack_util::{
atom::Atom,
fx_hash::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet},
fx_hash::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, indexmap},
swc::join_atom,
};
use swc_core::{
Expand Down Expand Up @@ -129,7 +129,7 @@ impl EsmLibraryPlugin {

// we find another name to export this symbol
let mut idx = 0;
let mut new_export = format!("{exported}_{idx}").into();
let mut new_export = Atom::new(format!("{exported}_{idx}"));
while ctx.exported_symbols.contains_key(&new_export) {
idx += 1;
new_export = format!("{exported}_{idx}").into();
Expand Down Expand Up @@ -821,17 +821,17 @@ impl EsmLibraryPlugin {
idents.push(ident);
}

let mut binding_to_ref: FxHashMap<
let mut binding_to_ref: FxIndexMap<
(Atom, SyntaxContext),
Vec<ConcatenatedModuleIdent>,
> = FxHashMap::default();
> = Default::default();

for ident in &idents {
match binding_to_ref.entry((ident.id.sym.clone(), ident.id.ctxt)) {
Entry::Occupied(mut occ) => {
indexmap::map::Entry::Occupied(mut occ) => {
occ.get_mut().push(ident.clone());
}
Entry::Vacant(vac) => {
indexmap::map::Entry::Vacant(vac) => {
vac.insert(vec![ident.clone()]);
}
};
Expand Down Expand Up @@ -1517,7 +1517,14 @@ impl EsmLibraryPlugin {

// link entry direct exports
if let Some(preserve_modules) = &self.preserve_modules {
for module_id in module_graph.modules().keys() {
let modules = module_graph.modules();
let mut modules = modules.keys().collect::<Vec<_>>();
modules.sort_by(|a, b| {
let ad = module_graph.get_depth(a);
let bd = module_graph.get_depth(b);
ad.cmp(&bd)
});
for module_id in modules {
if compilation.entry_modules().contains(module_id) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const RESERVED_NAMES: [&str; 3] = [REQUIRE, EXPORTS, MODULE];

fn get_lit_str(expr: &Expr) -> Option<Atom> {
expr.as_lit().and_then(|lit| match lit {
Lit::Str(s) => Some(s.value.clone()),
Lit::Str(s) => Some(s.value.to_atom_lossy().into_owned()),
_ => None,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn create_import_meta_context_dependency(
.as_lit()
.and_then(|lit| {
if let Lit::Str(str) = lit {
return Some(str.value.to_string());
return Some(str.value.to_string_lossy().to_string());
}
None
})
Expand All @@ -57,7 +57,7 @@ fn create_import_meta_context_dependency(
let exclude = get_regex_by_obj_prop(obj, "exclude")
.map(|regexp| RspackRegex::try_from(regexp).expect("reg failed"));
let mode = get_literal_str_by_obj_prop(obj, "mode")
.map(|s| s.value.to_string().as_str().into())
.map(|s| s.value.to_string_lossy().as_ref().into())
.unwrap_or(ContextMode::Sync);
let recursive = get_bool_by_obj_prop(obj, "recursive")
.map(|bool| bool.value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rspack_core::ConstDependency;
use swc_core::atoms::wtf8::Wtf8;

use super::JavascriptParserPlugin;
use crate::visitors::JavascriptParser;
Expand All @@ -17,13 +18,10 @@ impl JavascriptParserPlugin for UseStrictPlugin {
}
.and_then(|i| i.as_expr());
if let Some(first) = first
&& matches!(
first.expr.as_lit().and_then(|i| match i {
swc_core::ecma::ast::Lit::Str(s) => Some(s.value.as_str()),
_ => None,
}),
Some("use strict")
)
&& first.expr.as_lit().and_then(|i| match i {
swc_core::ecma::ast::Lit::Str(s) => Some(s.value.as_wtf8()),
_ => None,
}) == Some(Wtf8::from_str("use strict"))
{
// Remove "use strict" expression. It will be added later by the renderer again.
// This is necessary in order to not break the strict mode when webpack prepends code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn parse_new_worker_options(arg: &ExprOrSpread) -> ParsedNewWorkerOptions {
let obj = arg.expr.as_object();
let name = obj
.and_then(|obj| get_literal_str_by_obj_prop(obj, "name"))
.map(|str| str.value.to_string());
.map(|str| str.value.to_string_lossy().into());
let span = arg.span();
ParsedNewWorkerOptions {
range: Some((span.real_lo(), span.real_hi())),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::BasicEvaluatedExpression;
#[inline]
pub fn eval_str(str: &Str) -> BasicEvaluatedExpression<'_> {
let mut res = BasicEvaluatedExpression::with_range(str.span().real_lo(), str.span().real_hi());
res.set_string(str.value.to_string());
res.set_string(str.value.to_string_lossy().to_string());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use Wtf8Atom or Wtf8Buf here for the string type?

res
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn get_simplified_template_result<'a>(
TemplateStringKind::Cooked => quasi_expr
.cooked
.as_ref()
.and_then(|q| q.as_atom())
.expect("quasic should be not empty"),
TemplateStringKind::Raw => &quasi_expr.raw,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ pub fn get_attributes(obj: &ObjectLit) -> ImportAttributes {
kv.key
.as_ident()
.map(|k| k.sym.as_str())
.or_else(|| kv.key.as_str().map(|k| k.value.as_str()))
.or_else(|| kv.key.as_str().and_then(|k| k.value.as_str()))
.map(|s| s.to_string())
.zip(kv.value.as_lit().and_then(|lit| match lit {
Lit::Str(s) => Some(s.value.to_string()),
Lit::Str(s) => Some(s.value.to_string_lossy().to_string()),
_ => None,
}))
})
Expand Down
Loading
Loading