Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/update swc 0 7 5 #7087

Closed
wants to merge 4 commits into from
Closed
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
412 changes: 218 additions & 194 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ tikv-jemallocator = { version = "=0.5.4", features = ["disable_initial_exec_tls"

# Must be pinned with the same swc versions
#rkyv = { version = "=0.7.44" } # synced with swc wasm plugin
styled_components = { version = "=0.96.16" }
swc_config = { version = "=0.1.14" }
swc_core = { version = "0.93.0", default-features = false }
swc_ecma_minifier = { version = "0.195.0", default-features = false }
swc_emotion = { version = "=0.72.14" }
swc_error_reporters = { version = "=0.17.20" }
swc_html = { version = "=0.140.0" }
swc_html_minifier = { version = "=0.137.0" }
swc_node_comments = { version = "=0.20.20" }
styled_components = { version = "0.96.18" }
swc_config = { version = "0.1.13" }
swc_core = { version = "0.96.9", default-features = false }
swc_ecma_minifier = { version = "0.197.3", default-features = false }
swc_emotion = { version = "0.72.16" }
swc_error_reporters = { version = "=0.18.1" }
swc_html = { version = "=0.142.0" }
swc_html_minifier = { version = "=0.139.1" }
swc_node_comments = { version = "=0.21.0" }

[profile.dev]
codegen-units = 16
Expand Down
2 changes: 1 addition & 1 deletion crates/node_binding/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rspack/binding",
"version": "0.7.4",
"version": "0.7.5",
"license": "MIT",
"description": "Node binding for rspack",
"main": "binding.js",
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_javascript/src/ast/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn print(
let mut buf = vec![];

source_map
.build_source_map_with_config(&src_map_buf, input_source_map, source_map_config)
.build_source_map_with_config(&src_map_buf, input_source_map.cloned(), source_map_config)
.to_writer(&mut buf)
.unwrap_or_else(|e| panic!("{}", e.to_string()));
// SAFETY: This buffer is already sanitized
Expand Down
18 changes: 7 additions & 11 deletions crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rspack_core::{ConstDependency, RuntimeGlobals, RuntimeRequirementsDependency, SpanExt};
use swc_core::common::Spanned;
use swc_core::ecma::ast::{CallExpr, Callee, Expr, Ident};
use swc_core::ecma::ast::{CallExpr, Callee, Expr, Ident, UnaryExpr};

use crate::dependency::ModuleArgumentDependency;
use crate::parser_plugin::JavascriptParserPlugin;
Expand Down Expand Up @@ -63,17 +63,13 @@ fn get_typeof_evaluate_of_api(sym: &str) -> Option<&str> {
impl JavascriptParserPlugin for APIPlugin {
fn evaluate_typeof(
&self,
parser: &mut JavascriptParser,
expression: &Ident,
start: u32,
end: u32,
_parser: &mut JavascriptParser,
expr: &UnaryExpr,
for_name: &str,
) -> Option<BasicEvaluatedExpression> {
if parser.is_unresolved_ident(expression.sym.as_str()) {
get_typeof_evaluate_of_api(expression.sym.as_str())
.map(|res| eval::evaluate_to_string(res.to_string(), start, end))
} else {
None
}
get_typeof_evaluate_of_api(for_name).map(|res| {
eval::evaluate_to_string(res.to_string(), expr.span.real_lo(), expr.span.real_hi())
})
}

fn identifier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use rspack_core::{
};
use swc_core::atoms::Atom;
use swc_core::common::Spanned;
use swc_core::ecma::ast::{AssignExpr, AssignTarget, CallExpr, PropOrSpread, SimpleAssignTarget};
use swc_core::ecma::ast::{
AssignExpr, AssignTarget, CallExpr, PropOrSpread, SimpleAssignTarget, UnaryExpr,
};
use swc_core::ecma::ast::{Callee, ExprOrSpread, Ident, MemberExpr, ObjectLit};
use swc_core::ecma::ast::{Expr, Lit, Prop, PropName, ThisExpr, UnaryOp};

Expand Down Expand Up @@ -553,15 +555,16 @@ impl JavascriptParserPlugin for CommonJsExportsParserPlugin {

fn evaluate_typeof(
&self,
parser: &mut JavascriptParser,
expression: &Ident,
start: u32,
end: u32,
_parser: &mut JavascriptParser,
expr: &UnaryExpr,
for_name: &str,
) -> Option<BasicEvaluatedExpression> {
if parser.is_exports_ident(expression) || parser.is_module_ident(expression) {
Some(eval::evaluate_to_string("object".to_string(), start, end))
} else {
None
}
(for_name == "module" || for_name == "exports").then(|| {
eval::evaluate_to_string(
"object".to_string(),
expr.span.real_lo(),
expr.span.real_hi(),
)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rspack_core::{
use rspack_core::{ContextNameSpaceObject, ContextOptions};
use rspack_error::Severity;
use swc_core::common::{Span, Spanned};
use swc_core::ecma::ast::{CallExpr, Expr, Ident, Lit, MemberExpr};
use swc_core::ecma::ast::{CallExpr, Expr, Ident, Lit, MemberExpr, UnaryExpr};

use super::JavascriptParserPlugin;
use crate::dependency::RequireHeaderDependency;
Expand Down Expand Up @@ -272,18 +272,20 @@ impl JavascriptParserPlugin for CommonJsImportsParserPlugin {

fn evaluate_typeof(
&self,
parser: &mut JavascriptParser,
expression: &Ident,
start: u32,
end: u32,
_parser: &mut JavascriptParser,
expr: &UnaryExpr,
for_name: &str,
) -> Option<BasicEvaluatedExpression> {
if expression.sym.as_str() == expr_name::REQUIRE
&& parser.is_unresolved_ident(expr_name::REQUIRE)
{
Some(eval::evaluate_to_string("function".to_string(), start, end))
} else {
None
}
(for_name == expr_name::REQUIRE
|| for_name == expr_name::REQUIRE_RESOLVE
|| for_name == expr_name::REQUIRE_RESOLVE_WEAK)
.then(|| {
eval::evaluate_to_string(
"function".to_string(),
expr.span.real_lo(),
expr.span.real_hi(),
)
})
}

fn evaluate_identifier(
Expand Down
9 changes: 4 additions & 5 deletions crates/rspack_plugin_javascript/src/parser_plugin/drive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use swc_core::atoms::Atom;
use swc_core::common::Span;
use swc_core::ecma::ast::{
BinExpr, CallExpr, Callee, CondExpr, ExportDecl, ExportDefaultDecl, Expr, OptChainExpr,
BinExpr, CallExpr, Callee, CondExpr, ExportDecl, ExportDefaultDecl, Expr, OptChainExpr, UnaryExpr,
};
use swc_core::ecma::ast::{IfStmt, MemberExpr, Stmt, UnaryOp, VarDecl, VarDeclarator};

Expand Down Expand Up @@ -332,12 +332,11 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
fn evaluate_typeof(
&self,
parser: &mut JavascriptParser,
ident: &swc_core::ecma::ast::Ident,
start: u32,
end: u32,
expr: &UnaryExpr,
for_name: &str,
) -> Option<BasicEvaluatedExpression> {
for plugin in &self.plugins {
let res = plugin.evaluate_typeof(parser, ident, start, end);
let res = plugin.evaluate_typeof(parser, expr, for_name);
// `SyncBailHook`
if res.is_some() {
return res;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::ops::Add;

use rspack_core::{BuildMetaExportsType, ExportsArgument, ModuleArgument, ModuleType};
use rspack_core::{BuildMetaExportsType, ExportsArgument, ModuleArgument, ModuleType, SpanExt};
use swc_core::common::source_map::Pos;
use swc_core::common::{BytePos, Span, Spanned};
use swc_core::ecma::ast::{ModuleItem, Program};
use swc_core::ecma::ast::{Ident, ModuleItem, Program, UnaryExpr};

use super::JavascriptParserPlugin;
use crate::dependency::HarmonyCompatibilityDependency;
use crate::utils::eval::BasicEvaluatedExpression;
use crate::visitors::{create_traceable_error, JavascriptParser};

impl<'parser> JavascriptParser<'parser> {
Expand Down Expand Up @@ -90,6 +91,25 @@ impl JavascriptParserPlugin for HarmonyDetectionParserPlugin {
let span = Span::new(lo, hi, stmt.span.ctxt);
parser.handle_top_level_await(self.top_level_await, span);
}

fn evaluate_typeof(
&self,
parser: &mut JavascriptParser,
expr: &UnaryExpr,
for_name: &str,
) -> Option<BasicEvaluatedExpression> {
(parser.is_esm && for_name == "exports")
.then(|| BasicEvaluatedExpression::with_range(expr.span().real_lo(), expr.span_hi().0))
}

fn identifier(
&self,
parser: &mut JavascriptParser,
_ident: &Ident,
for_name: &str,
) -> Option<bool> {
(parser.is_esm && for_name == "exports").then_some(true)
}
}

/// "await".len();
Expand Down
5 changes: 2 additions & 3 deletions crates/rspack_plugin_javascript/src/parser_plugin/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ pub trait JavascriptParserPlugin {
fn evaluate_typeof(
&self,
_parser: &mut JavascriptParser,
_ident: &Ident,
_start: u32,
_end: u32,
_expr: &UnaryExpr,
_for_name: &str,
) -> Option<BasicEvaluatedExpression> {
None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use swc_core::ecma::ast::{UnaryExpr, UnaryOp};

use super::BasicEvaluatedExpression;
use crate::parser_plugin::JavascriptParserPlugin;
use crate::visitors::JavascriptParser;
use crate::visitors::{CallHooksName, JavascriptParser};

#[inline]
fn eval_typeof(
Expand All @@ -13,15 +13,14 @@ fn eval_typeof(
) -> Option<BasicEvaluatedExpression> {
assert!(expr.op == UnaryOp::TypeOf);
if let Some(ident) = expr.arg.as_ident()
&& /* FIXME: should use call hooks for name */ let res = parser.plugin_drive.clone().evaluate_typeof(
parser,
ident,
expr.span.real_lo(),
expr.span.hi().0,
)
&& res.is_some()
&& let Some(res) = ident.sym.call_hooks_name(parser, |parser, for_name| {
parser
.plugin_drive
.clone()
.evaluate_typeof(parser, expr, for_name)
})
{
return res;
return Some(res);
}

// TODO: if let `MetaProperty`, `MemberExpression` ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,19 @@ impl<'parser> JavascriptParser<'parser> {
)));
plugins.push(Box::new(parser_plugin::CompatibilityPlugin));

if module_type.is_js_auto() || module_type.is_js_esm() {
plugins.push(Box::new(parser_plugin::HarmonyTopLevelThisParserPlugin));
plugins.push(Box::new(parser_plugin::HarmonyDetectionParserPlugin::new(
compiler_options.experiments.top_level_await,
)));
plugins.push(Box::new(
parser_plugin::ImportMetaContextDependencyParserPlugin,
));
plugins.push(Box::new(parser_plugin::ImportMetaPlugin));
plugins.push(Box::new(parser_plugin::HarmonyImportDependencyParserPlugin));
plugins.push(Box::new(parser_plugin::HarmonyExportDependencyParserPlugin));
}

if module_type.is_js_auto() || module_type.is_js_dynamic() {
plugins.push(Box::new(parser_plugin::CommonJsImportsParserPlugin));
plugins.push(Box::new(parser_plugin::CommonJsPlugin));
Expand Down Expand Up @@ -345,26 +358,13 @@ impl<'parser> JavascriptParser<'parser> {
compiler_options.output.module,
)));
plugins.push(Box::new(parser_plugin::ImportParserPlugin));
}

if module_type.is_js_auto() || module_type.is_js_esm() {
let parse_url = javascript_options.url;
if !matches!(parse_url, JavascriptParserUrl::Disable) {
plugins.push(Box::new(parser_plugin::URLPlugin {
relative: matches!(parse_url, JavascriptParserUrl::Relative),
}));
}
plugins.push(Box::new(parser_plugin::HarmonyTopLevelThisParserPlugin));
plugins.push(Box::new(parser_plugin::HarmonyDetectionParserPlugin::new(
compiler_options.experiments.top_level_await,
)));
plugins.push(Box::new(parser_plugin::WorkerPlugin));
plugins.push(Box::new(
parser_plugin::ImportMetaContextDependencyParserPlugin,
));
plugins.push(Box::new(parser_plugin::ImportMetaPlugin));
plugins.push(Box::new(parser_plugin::HarmonyImportDependencyParserPlugin));
plugins.push(Box::new(parser_plugin::HarmonyExportDependencyParserPlugin));
}

let plugin_drive = Rc::new(JavaScriptParserPluginDrive::new(plugins));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,10 @@ impl<'parser> JavascriptParser<'parser> {

// TODO: `hooks.body_value`;
if let Some(body) = &ctor.body {
this.detect_mode(&body.stmts);
let prev = this.prev_statement.clone();
this.pre_walk_block_statement(body);
this.prev_statement = prev;
this.walk_block_statement(body);
}
});
Expand All @@ -1397,15 +1401,7 @@ impl<'parser> JavascriptParser<'parser> {
this.in_function_scope(
true,
method.function.params.iter().map(|p| Cow::Borrowed(&p.pat)),
|this| {
for param in &method.function.params {
this.walk_pattern(&param.pat);
}
// TODO: `hooks.body_value`;
if let Some(body) = &method.function.body {
this.walk_block_statement(body);
}
},
|this| this.walk_function(&method.function),
);
this.top_level_scope = was_top_level;
}
Expand All @@ -1416,15 +1412,7 @@ impl<'parser> JavascriptParser<'parser> {
this.in_function_scope(
true,
method.function.params.iter().map(|p| Cow::Borrowed(&p.pat)),
|this| {
for param in &method.function.params {
this.walk_pattern(&param.pat);
}
// TODO: `hooks.body_value`;
if let Some(body) = &method.function.body {
this.walk_block_statement(body);
}
},
|this| this.walk_function(&method.function),
);
this.top_level_scope = was_top_level;
}
Expand All @@ -1441,7 +1429,7 @@ impl<'parser> JavascriptParser<'parser> {
}
}
ClassMember::PrivateProp(prop) => {
this.walk_identifier(&prop.key.id);
// prop.key is always not computed in private prop, so we don't need to walk it
if let Some(value) = &prop.value {
let was_top_level = this.top_level_scope;
this.top_level_scope = TopLevelScope::False;
Expand Down
2 changes: 1 addition & 1 deletion npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rspack/binding-darwin-arm64",
"version": "0.7.4",
"version": "0.7.5",
"license": "MIT",
"description": "Node binding for rspack",
"main": "rspack.darwin-arm64.node",
Expand Down
2 changes: 1 addition & 1 deletion npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rspack/binding-darwin-x64",
"version": "0.7.4",
"version": "0.7.5",
"license": "MIT",
"description": "Node binding for rspack",
"main": "rspack.darwin-x64.node",
Expand Down
2 changes: 1 addition & 1 deletion npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rspack/binding-linux-x64-gnu",
"version": "0.7.4",
"version": "0.7.5",
"license": "MIT",
"description": "Node binding for rspack",
"main": "rspack.linux-x64-gnu.node",
Expand Down
2 changes: 1 addition & 1 deletion npm/win32-x64-msvc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rspack/binding-win32-x64-msvc",
"version": "0.7.4",
"version": "0.7.5",
"license": "MIT",
"description": "Node binding for rspack",
"main": "rspack.win32-x64-msvc.node",
Expand Down
Loading
Loading