Skip to content

Commit

Permalink
revert: ⏪ update swc from 83 to 86 (#907)
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly authored Feb 1, 2024
1 parent 1023b1b commit 9aee458
Show file tree
Hide file tree
Showing 13 changed files with 1,225 additions and 1,739 deletions.
2,735 changes: 1,134 additions & 1,601 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ members = ["crates/*"]
serde = { version = "1.0.171", features = ["derive"] }
serde_json = "1.0.100"
cached = "0.46.1"
swc_core = { version = "0.86.104", default-features = false }
swc_core = { version = "=0.83.19", default-features = false }
6 changes: 3 additions & 3 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ swc_core = { workspace = true, features = [
"base_node",
] }

swc_error_reporters = "0.17.12"
swc_node_comments = "0.20.12"
swc_emotion = "0.67.0"
swc_error_reporters = "0.16.1"
swc_node_comments = "0.19.1"
swc_emotion = "0.51.0"

base64 = "0.21.2"
petgraph = "0.6.3"
Expand Down
4 changes: 2 additions & 2 deletions crates/mako/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn build_css_ast(
legacy_ie: true,
..Default::default()
};
let lexer = swc_css_parser::lexer::Lexer::new(StringInput::from(&*fm), None, config);
let lexer = swc_css_parser::lexer::Lexer::new(StringInput::from(&*fm), config);
let mut parser = swc_css_parser::parser::Parser::new(lexer, config);
let parse_result = parser.parse_all();

Expand Down Expand Up @@ -328,7 +328,7 @@ mod tests {
r#"
export const foo = "我是中文";
export const bar = {
"中文": "xxx"
中文: "xxx"
}
"#,
);
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/plugins/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ mod tests {
code.trim(),
r#"
import "/test/path?modules";
export default {"a": `a-hlnPCer-`,"b": `b-KOXpblx_ a`,"c": `c-WTxpkVWA c`}
export default {"b": `b-KOXpblx_ a`,"c": `c-WTxpkVWA c`,"a": `a-hlnPCer-`}
"#
.trim()
);
Expand Down
4 changes: 2 additions & 2 deletions crates/mako/src/plugins/invalid_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ fn is_member_prop(
..
} = expr
{
let is_obj_match = ident.sym == obj;
let is_obj_match = ident.sym.to_string() == obj;
let has_binding = ident.span.ctxt.outer() != unresolved_mark;
let is_prop_match = prop_ident.sym == prop;
let is_prop_match = prop_ident.sym.to_string() == prop;
is_obj_match && (check_obj_binding && !has_binding) && is_prop_match
} else {
false
Expand Down
4 changes: 2 additions & 2 deletions crates/mako/src/plugins/node_stuff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ impl VisitMut for NodeStuff<'_> {
if let Expr::Ident(ident) = expr
&& ident.span.ctxt.outer() == self.unresolved_mark
{
let is_filename = ident.sym == "__filename";
let is_dirname = ident.sym == "__dirname";
let is_filename = ident.sym.to_string() == "__filename";
let is_dirname = ident.sym.to_string() == "__dirname";
if is_filename || is_dirname {
let path = diff_paths(self.current_path, self.root).unwrap_or("".into());
let value = if is_filename {
Expand Down
20 changes: 5 additions & 15 deletions crates/mako/src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
use std::sync::Arc;

use mako_core::anyhow::Result;
use mako_core::swc_common::comments::NoopComments;
use mako_core::swc_common::errors::HANDLER;
use mako_core::swc_common::pass::Optional;
use mako_core::swc_common::sync::Lrc;
use mako_core::swc_common::util::take::Take;
use mako_core::swc_common::{chain, Mark, GLOBALS};
use mako_core::swc_css_ast::Stylesheet;
use mako_core::swc_css_visit::VisitMutWith as CssVisitMutWith;
use mako_core::swc_ecma_ast::{Module, Program};
use mako_core::swc_ecma_ast::Module;
use mako_core::swc_ecma_preset_env::{self as swc_preset_env};
use mako_core::swc_ecma_transforms::feature::FeatureFlag;
use mako_core::swc_ecma_transforms::helpers::{inject_helpers, Helpers, HELPERS};
use mako_core::swc_ecma_transforms::{resolver, Assumptions};
use mako_core::swc_ecma_transforms_optimization::simplifier;
use mako_core::swc_ecma_transforms_optimization::simplify::{dce, Config as SimpilifyConfig};
use mako_core::swc_ecma_transforms_proposals::decorators;
use mako_core::swc_ecma_transforms_react::{default_pragma, default_pragma_frag};
use mako_core::swc_ecma_transforms_typescript as typescript;
use mako_core::swc_ecma_transforms_typescript::strip_with_jsx;
use mako_core::swc_ecma_visit::{Fold, VisitMutWith};
use mako_core::swc_error_reporters::handler::try_with_handler;
use swc_core::ecma::ast::ModuleItem;
Expand Down Expand Up @@ -85,20 +83,12 @@ fn transform_js(
// since when use this in js, it will remove all unused imports
// which is not expected as what webpack does
if is_ts {
let mut program = Program::Module(ast.take());
program.visit_mut_with(&mut typescript::tsx(
ast.visit_mut_with(&mut strip_with_jsx(
cm.clone(),
typescript::Config::default(),
typescript::TsxConfig {
pragma: Some(default_pragma()),
pragma_frag: Some(default_pragma_frag()),
},
NoopComments,
Default::default(),
origin_comments.get_swc_comments(),
top_level_mark,
));
if let Program::Module(ast_copy) = program {
*ast = ast_copy;
}
}

ast.visit_mut_with(&mut mako_react(
Expand Down
24 changes: 3 additions & 21 deletions crates/mako/src/transformers/transform_dep_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,35 +386,17 @@ mod tests {

#[test]
fn test_import_replace() {
let context: Arc<Context> = Arc::new(Default::default());

GLOBALS.set(&context.meta.script.globals, || {
let code = transform_code("import x from 'x'");

assert_eq!(code, r#"import x from "/x/index.js";"#);
});
assert_display_snapshot!(transform_code("import x from 'x'"));
}

#[test]
fn test_export_from_replace() {
let context: Arc<Context> = Arc::new(Default::default());

GLOBALS.set(&context.meta.script.globals, || {
let code = transform_code("export {x} from 'x'");

assert_eq!(code, r#"export { x } from "/x/index.js";"#);
})
assert_display_snapshot!(transform_code("export {x} from 'x'"));
}

#[test]
fn test_dynamic_import_from_replace() {
let context: Arc<Context> = Arc::new(Default::default());

GLOBALS.set(&context.meta.script.globals, || {
let code = transform_code("const x = import('x')");

assert_eq!(code, r#"const x = import("/x/index.js");"#);
})
assert_display_snapshot!(transform_code("const x = import('x')"));
}

fn transform_code(code: &str) -> String {
Expand Down
114 changes: 57 additions & 57 deletions crates/mako/src/transformers/transform_env_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,53 +79,38 @@ impl VisitMut for EnvReplacer {
}
}

if let Expr::Member(MemberExpr {
obj: box obj, prop, ..
}) = expr
{
if match obj {
Expr::Member(MemberExpr {
prop: MemberProp::Ident(Ident { sym, .. }),
..
}) => sym == "env",
_ => false,
} {
if let Expr::Member(MemberExpr { obj, prop, .. }) = expr {
if let Expr::Member(MemberExpr {
obj: first_obj,
prop:
MemberProp::Ident(Ident {
sym: js_word!("env"),
..
}),
..
}) = &**obj
{
// handle `env.XX`
let mut envs = EnvsType::Node(self.envs.clone());

if let Expr::Member(MemberExpr {
obj: box first_obj, ..
}) = obj
{
if match first_obj {
Expr::Ident(Ident { sym, .. }) => sym == "process",
Expr::MetaProp(MetaPropExpr {
kind: MetaPropKind::ImportMeta,
..
}) => {
envs = EnvsType::Browser(self.meta_envs.clone());
true
}
_ => false,
} {
// handle `process.env.XX` and `import.meta.env.XX`
match prop {
MemberProp::Computed(ComputedPropName { expr: c, .. }) => {
if let Expr::Lit(Lit::Str(Str { value: sym, .. })) = &**c {
if let Some(env) = EnvReplacer::get_env(&envs, sym) {
// replace with real value if env found
*expr = env;
} else {
// replace with `undefined` if env not found
*expr = *Box::new(Expr::Ident(Ident::new(
js_word!("undefined"),
DUMMY_SP,
)));
}
}
}

MemberProp::Ident(Ident { sym, .. }) => {
if match &**first_obj {
Expr::Ident(Ident {
sym: js_word!("process"),
..
}) => true,
Expr::MetaProp(MetaPropExpr {
kind: MetaPropKind::ImportMeta,
..
}) => {
envs = EnvsType::Browser(self.meta_envs.clone());
true
}
_ => false,
} {
// handle `process.env.XX` and `import.meta.env.XX`
match prop {
MemberProp::Computed(ComputedPropName { expr: c, .. }) => {
if let Expr::Lit(Lit::Str(Str { value: sym, .. })) = &**c {
if let Some(env) = EnvReplacer::get_env(&envs, sym) {
// replace with real value if env found
*expr = env;
Expand All @@ -137,22 +122,37 @@ impl VisitMut for EnvReplacer {
)));
}
}
_ => {}
}

MemberProp::Ident(Ident { sym, .. }) => {
if let Some(env) = EnvReplacer::get_env(&envs, sym) {
// replace with real value if env found
*expr = env;
} else {
// replace with `undefined` if env not found
*expr = *Box::new(Expr::Ident(Ident::new(
js_word!("undefined"),
DUMMY_SP,
)));
}
}
_ => {}
}
}
} else if match expr {
Expr::Member(MemberExpr {
obj:
box Expr::MetaProp(MetaPropExpr {
kind: MetaPropKind::ImportMeta,
..
}),
prop: MemberProp::Ident(Ident { sym, .. }),
..
}) => sym == "env",
_ => false,
} {
} else if let Expr::Member(MemberExpr {
obj:
box Expr::MetaProp(MetaPropExpr {
kind: MetaPropKind::ImportMeta,
..
}),
prop:
MemberProp::Ident(Ident {
sym: js_word!("env"),
..
}),
..
}) = *expr
{
// replace independent `import.meta.env` to json object
let mut props = Vec::new();

Expand Down
12 changes: 6 additions & 6 deletions crates/mako/src/transformers/transform_optimize_define_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl VisitMut for OptimizeDefineUtils {
let mut no_directive_index = 0;
for (index, item) in items.iter().enumerate() {
if let Some(stmt) = item.as_stmt()
&& stmt.directive_continue()
&& stmt.is_directive()
{
no_directive_index = index + 1
} else {
Expand Down Expand Up @@ -67,7 +67,7 @@ impl VisitMut for OptimizeDefineUtils {
&& call_expr.args.len() == 2
&& is_export_arg(call_expr.args.first())
&& is_obj_lit_arg(call_expr.args.get(1))
&& callee_ident.sym == "_export"
&& callee_ident.sym.to_string() == "_export"
// is private ident
&& !callee_ident
.span
Expand Down Expand Up @@ -96,14 +96,14 @@ fn is_object_define(expr: &Expr) -> bool {
let is_object = member
.obj
.as_ident()
.map(|ident| ident.sym == "Object")
.map(|ident| ident.sym.to_string() == "Object")
.unwrap_or(false);

is_object
&& member
.prop
.as_ident()
.map(|ident| ident.sym == "defineProperty")
.map(|ident| ident.sym.to_string() == "defineProperty")
.unwrap_or(false)
})
.unwrap_or(false)
Expand All @@ -115,7 +115,7 @@ fn is_export_arg(arg: Option<&ExprOrSpread>) -> bool {
&& arg
.expr
.as_ident()
.map(|ident| ident.sym == "exports")
.map(|ident| ident.sym.to_string() == "exports")
.unwrap_or(false)
})
.unwrap_or(false)
Expand All @@ -141,7 +141,7 @@ fn is_string_lit_arg_with_value(arg: Option<&ExprOrSpread>, value: &str) -> bool
.as_lit()
.map(|lit| {
if let Lit::Str(str_lit) = lit {
str_lit.value == value
str_lit.value.to_string() == value
} else {
false
}
Expand Down
4 changes: 2 additions & 2 deletions crates/mako/src/transformers/transform_px2rem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl VisitMut for Px2Rem<'_> {
self.current_decl = None;
}
fn visit_mut_length(&mut self, n: &mut Length) {
if n.unit.value == "px" && self.should_transform() {
if n.unit.value.to_string() == "px" && self.should_transform() {
n.value.value /= self.context.config.px2rem.as_ref().unwrap().root;
n.value.raw = None;
n.unit.value = "rem".into();
Expand All @@ -71,7 +71,7 @@ impl VisitMut for Px2Rem<'_> {
}
fn visit_mut_token(&mut self, t: &mut Token) {
if let Token::Dimension(dimension) = t {
if dimension.unit == "px" && self.should_transform() {
if dimension.unit.to_string() == "px" && self.should_transform() {
let rem_val = dimension.value / self.context.config.px2rem.as_ref().unwrap().root;
dimension.raw_value = rem_val.to_string().into();
dimension.value = rem_val;
Expand Down
Loading

0 comments on commit 9aee458

Please sign in to comment.