Skip to content

Commit

Permalink
chore: update swc v1 (farm-fe#1836)
Browse files Browse the repository at this point in the history
* chore: update to swc v1

* chore: swc updated
  • Loading branch information
wre232114 authored Oct 20, 2024
1 parent e49bf71 commit 571b37f
Show file tree
Hide file tree
Showing 72 changed files with 1,312 additions and 953 deletions.
1,232 changes: 771 additions & 461 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 2 additions & 8 deletions crates/compiler/src/generate/partial_bundling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,7 @@ mod tests {
fn test_generate_resource_pot_map() {
let mut module_graph = construct_test_module_graph_complex();
let module_group_graph = module_group_graph_from_entries(
&module_graph
.entries
.clone().into_keys()
.collect(),
&module_graph.entries.clone().into_keys().collect(),
&mut module_graph,
);

Expand Down Expand Up @@ -307,10 +304,7 @@ mod tests {
module_graph.module_mut(&"H".into()).unwrap().external = true;

let module_group_graph = module_group_graph_from_entries(
&module_graph
.entries
.clone().into_keys()
.collect(),
&module_graph.entries.clone().into_keys().collect(),
&mut module_graph,
);

Expand Down
20 changes: 4 additions & 16 deletions crates/compiler/src/update/patch_module_group_graph/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ fn test_patch_module_group_graph_3() {

let updated_modules = vec!["F".into(), "E".into(), "B".into()];
let mut module_group_graph = module_group_graph_from_entries(
&module_graph
.entries
.clone().into_keys()
.collect(),
&module_graph.entries.clone().into_keys().collect(),
&mut module_graph,
);
let diff_result = diff_module_graph(updated_modules.clone(), &module_graph, &update_module_graph);
Expand All @@ -210,10 +207,7 @@ fn test_patch_module_group_graph_3() {
);

let update_module_group_graph = module_group_graph_from_entries(
&module_graph
.entries
.clone().into_keys()
.collect(),
&module_graph.entries.clone().into_keys().collect(),
&mut module_graph,
);

Expand Down Expand Up @@ -291,10 +285,7 @@ fn test_patch_module_group_graph_css_modules() {

let start_points = vec!["D".into()];
let mut module_group_graph = module_group_graph_from_entries(
&module_graph
.entries
.clone().into_keys()
.collect(),
&module_graph.entries.clone().into_keys().collect(),
&mut module_graph,
);
let diff_result = diff_module_graph(start_points.clone(), &module_graph, &update_module_graph);
Expand All @@ -315,10 +306,7 @@ fn test_patch_module_group_graph_css_modules() {
assert_eq!(affected_groups, HashSet::from(["D".into(), "B".into()]));

let update_module_group_graph = module_group_graph_from_entries(
&module_graph
.entries
.clone().into_keys()
.collect(),
&module_graph.entries.clone().into_keys().collect(),
&mut module_graph,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,7 @@ fn diff_and_patch_resource_pot_map(
let resource_pot = resource_pot_map
.remove_resource_pot(resource_pot)
.unwrap_or_else(|| {
panic!(
"The resource pot {resource_pot:?} should be in the resource pot map"
)
panic!("The resource pot {resource_pot:?} should be in the resource pot map")
});

// also remove the related resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ fn test_generate_and_diff_resource_pots() {

let updated_modules = vec!["F".into(), "E".into(), "B".into()];
let mut module_group_graph = module_group_graph_from_entries(
&module_graph
.entries
.clone().into_keys()
.collect(),
&module_graph.entries.clone().into_keys().collect(),
&mut module_graph,
);
let diff_result = diff_module_graph(updated_modules.clone(), &module_graph, &update_module_graph);
Expand Down Expand Up @@ -244,10 +241,7 @@ fn test_generate_and_diff_resource_pots_one_module_changed() {
)
.unwrap();
let mut module_group_graph = module_group_graph_from_entries(
&module_graph
.entries
.clone().into_keys()
.collect(),
&module_graph.entries.clone().into_keys().collect(),
&mut module_graph,
);
let updated_modules = vec!["I".into()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ fn test_handle_enforce_resource_pots() {

let updated_modules = vec!["F".into(), "E".into(), "B".into()];
let mut module_group_graph = module_group_graph_from_entries(
&module_graph
.entries
.clone().into_keys()
.collect(),
&module_graph.entries.clone().into_keys().collect(),
&mut module_graph,
);
let diff_result = diff_module_graph(updated_modules.clone(), &module_graph, &update_module_graph);
Expand Down Expand Up @@ -169,10 +166,7 @@ fn test_handle_enforce_resource_pots_one_module_changed() {
)
.unwrap();
let mut module_group_graph = module_group_graph_from_entries(
&module_graph
.entries
.clone().into_keys()
.collect(),
&module_graph.entries.clone().into_keys().collect(),
&mut module_graph,
);
let updated_modules = vec!["I".into()];
Expand Down
1 change: 1 addition & 0 deletions crates/compiler/tests/tree_shake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ fn tree_shake_changed_ast() {
Default::default(),
Default::default(),
None as Option<NoopComments>,
unresolved_mark,
top_level_mark,
));
ast.visit_mut_with(&mut react::<NoopComments>(
Expand Down
19 changes: 9 additions & 10 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,30 @@ ptr_meta = "0.1.4"
thiserror = "1.0"
rayon = "1.5"
petgraph = "0.6"
bytecheck = "0.7.0"
downcast-rs = "1.2"
wax = { version = "0.6", default-features = false, features = [
"miette",
"walk",
] }
globset = { version = "0.4.14" }
swc_ecma_ast = { version = "0.115.1", features = ["rkyv-impl", "serde-impl"] }
swc_ecma_parser = { version = "0.146.12" }
swc_common = { version = "0.34.4", features = [
swc_ecma_ast = { version = "1.0.0", features = ["rkyv-impl", "serde-impl"] }
swc_ecma_parser = { version = "1.0.1" }
swc_common = { version = "1.0.0", features = [
"concurrent",
"sourcemap",
"rkyv-impl",
] }
swc_css_ast = { version = "0.141.0", features = ["rkyv-impl"] }
swc_css_prefixer = { version = "0.155.0" }
swc_html_ast = { version = "0.34.0", features = ["rkyv-impl"] }
heck = "0.4.1"
puffin = { version = "0.18.0", features = [
swc_css_ast = { version = "1.0.0", features = ["rkyv-impl"] }
swc_css_prefixer = { version = "1.0.0" }
swc_html_ast = { version = "1.0.0", features = ["rkyv-impl"] }
heck = "0.5.0"
puffin = { version = "0.19.1", features = [
"packing",
"lz4",
"serialization",
], optional = true }
regex = "1.7.3"
enhanced-magic-string = { version = "0.0.15" }
enhanced-magic-string = { version = "0.1.0" }

[features]
profile = ["dep:puffin"]
2 changes: 1 addition & 1 deletion crates/core/src/cache/cache_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use crate::config::Mode;

const FARM_CACHE_VERSION: &str = "0.4.9";
const FARM_CACHE_VERSION: &str = "0.5.0";
const FARM_CACHE_MANIFEST_FILE: &str = "farm-cache.json";

// TODO make CacheStore a trait and implement DiskCacheStore or RemoteCacheStore or more.
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/config/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub fn get_config_external_record(config: &Config) -> ExternalConfig {
return ExternalConfig::new();
}

let external: HashMap<String, String> = serde_json::from_str(val)
.unwrap_or_else(|_| panic!("failed parse record external {val:?}"));
let external: HashMap<String, String> =
serde_json::from_str(val).unwrap_or_else(|_| panic!("failed parse record external {val:?}"));

let mut external_config = ExternalConfig::new();

Expand Down
10 changes: 0 additions & 10 deletions crates/core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ pub mod tree_shaking;

pub use output::*;

type ResolverFunction = Box<dyn Fn(String, String) -> Option<String>>;

// #[derive(Debug, Clone, Serialize, Deserialize)]
// pub struct ResolverObject {
// resolve: ResolverFunction,
// }

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AliasItem {
Expand All @@ -50,9 +43,6 @@ pub enum AliasItem {
Complex {
find: StringOrRegex,
replacement: String,
// TODO custom resolver
// #[serde(skip_serializing_if = "Option::is_none")]
// custom_resolver: Option<ResolverObject>,
},
}

Expand Down
14 changes: 7 additions & 7 deletions crates/core/src/config/persistent_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ impl PersistentCacheConfig {
let mut keys: Vec<_> = cloned_obj.envs.keys().collect();
keys.sort();
let config_str = keys
.into_iter()
.map(|k| {
let v = cloned_obj.envs.get(k).unwrap();
format!("{k}={v}")
})
.collect::<Vec<_>>()
.join("&");
.into_iter()
.map(|k| {
let v = cloned_obj.envs.get(k).unwrap();
format!("{k}={v}")
})
.collect::<Vec<_>>()
.join("&");
let config_hash = sha256(config_str.as_bytes(), 32);

cloned_obj.build_dependencies.push(config_hash);
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub const VERSION: &str = "0.5.0";
// re-export common external crates
pub use dashmap;
pub use enhanced_magic_string;
pub use heck;
pub use parking_lot;
pub use petgraph;
#[cfg(feature = "profile")]
Expand All @@ -42,7 +43,6 @@ pub use swc_ecma_ast;
pub use swc_ecma_parser;
pub use swc_html_ast;
pub use wax;
pub use heck;

#[macro_export]
macro_rules! farm_profile_scope {
Expand Down
2 changes: 1 addition & 1 deletion crates/create-farm-rs/src/package_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl PackageManager {
Template::Preact,
Template::Nestjs,
Template::Tauri(None),
Template::Electron(None)
Template::Electron(None),
],
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/create-farm-rs/src/utils/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ pub fn is_color_enabled() -> bool {
true
}

pub fn create_formatter<'a>(open: &'a str, close: &'a str, replace: Option<&'a str>) -> impl Fn(&'a str) -> String + 'a {
pub fn create_formatter<'a>(
open: &'a str,
close: &'a str,
replace: Option<&'a str>,
) -> impl Fn(&'a str) -> String + 'a {
move |input| {
let string = input.to_string();
let index = string.find(close).unwrap_or(open.len());
Expand Down
6 changes: 1 addition & 5 deletions crates/create-farm-rs/src/utils/prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ pub(crate) fn select<'t, T: Displayable>(
Ok(items.get(selected))
}

pub(crate) fn input(
prompt: &str,
default: Option<&str>,
allow_empty: bool,
) -> Result<String> {
pub(crate) fn input(prompt: &str, default: Option<&str>, allow_empty: bool) -> Result<String> {
let theme = ColorfulTheme::default();
let mut builder = dialoguer::Input::with_theme(&theme)
.with_prompt(prompt)
Expand Down
8 changes: 2 additions & 6 deletions crates/node/src/plugin_adapters/js_plugin_adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,15 @@ impl Plugin for JsPluginAdapter {

pub fn get_named_property<T: FromNapiValue>(env: &Env, obj: &JsObject, field: &str) -> Result<T> {
if obj.has_named_property(field).map_err(|e| {
CompilationError::NAPIError(format!(
"Get field {field} of config object failed. {e:?}"
))
CompilationError::NAPIError(format!("Get field {field} of config object failed. {e:?}"))
})? {
unsafe {
T::from_napi_value(
env.raw(),
obj
.get_named_property::<JsUnknown>(field)
.map_err(|e| {
CompilationError::NAPIError(format!(
"Get field {field} of config object failed. {e:?}"
))
CompilationError::NAPIError(format!("Get field {field} of config object failed. {e:?}"))
})?
.raw(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Please upgrade or downgrade the plugin or @farmfe/core to make the versions matc
plugin core 0.4 -> @farmfe/core < 1.3.0
plugin core 0.5 -> @farmfe/core >= 1.3.0
If you are plugin author, please build your plugin with rust toolchain `nightly-2024-04-16`\n",
If you are plugin author, please build your plugin with rust toolchain `nightly-2024-10-07`\n",
VERSION, core_version, filename
);
}
Expand Down
7 changes: 4 additions & 3 deletions crates/plugin_bundle/src/resource_pot_to_bundle/bundle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use farmfe_core::{
module::{module_graph::ModuleGraph, ModuleId, ModuleSystem},
plugin::ResolveKind,
rayon::iter::{IntoParallelIterator, ParallelIterator},
swc_common::{util::take::Take, DUMMY_SP},
swc_common::{util::take::Take, SyntaxContext, DUMMY_SP},
swc_ecma_ast::{
self, BindingIdent, CallExpr, ClassDecl, Decl, EmptyStmt, Expr, ExprStmt, FnDecl, Ident,
Module, ModuleDecl, ModuleItem, Stmt, VarDecl, VarDeclarator,
Expand Down Expand Up @@ -606,6 +606,7 @@ impl<'a> ModuleAnalyzerManager<'a> {
init: Some(export_default_decl.expr),
definite: false,
}],
ctxt: SyntaxContext::empty()
}))));
},
_ => {}
Expand All @@ -623,7 +624,7 @@ impl<'a> ModuleAnalyzerManager<'a> {
if !commonjs_import_executed.contains(source) {
ast.body[*index] = ModuleItem::Stmt(Stmt::Expr(
ExprStmt { span: DUMMY_SP, expr: Box::new(Expr::Call(
CallExpr { span: DUMMY_SP, callee: swc_ecma_ast::Callee::Expr(Box::new(Expr::Ident(bundle_variable.name(self.module_global_uniq_name.commonjs_name(source).unwrap()).as_str().into()))), args: vec![], type_args: None }
CallExpr { span: DUMMY_SP, callee: swc_ecma_ast::Callee::Expr(Box::new(Expr::Ident(bundle_variable.name(self.module_global_uniq_name.commonjs_name(source).unwrap()).as_str().into()))), args: vec![], type_args: None, ctxt: SyntaxContext::empty() }
)) }
));
commonjs_import_executed.insert(source.clone());
Expand Down Expand Up @@ -721,7 +722,7 @@ impl<'a> ModuleAnalyzerManager<'a> {
bundle_variable,
&context.config,
polyfill,
external_config
external_config,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::collections::{HashMap, HashSet};
use farmfe_core::{
swc_common::DUMMY_SP,
swc_ecma_ast::{
AssignExpr, AssignOp, AssignTarget, BindingIdent, Expr, Id, KeyValuePatProp, KeyValueProp, MemberProp, ObjectPat, ObjectPatProp, Pat, Prop, PropName, PropOrSpread, SimpleAssignTarget
AssignExpr, AssignOp, AssignTarget, BindingIdent, Expr, Id, IdentName, KeyValuePatProp,
KeyValueProp, MemberProp, ObjectPat, ObjectPatProp, Pat, Prop, PropName, PropOrSpread,
SimpleAssignTarget,
},
};
use farmfe_toolkit::{
Expand Down Expand Up @@ -98,10 +100,9 @@ impl<'a> VisitMut for RenameIdent<'a> {
Prop::Shorthand(m) => {
if let Some(new_name) = self.rename(m) {
*n = Prop::KeyValue(farmfe_core::swc_ecma_ast::KeyValueProp {
key: farmfe_core::swc_ecma_ast::PropName::Ident(Ident {
key: farmfe_core::swc_ecma_ast::PropName::Ident(IdentName {
span: DUMMY_SP,
sym: m.sym.as_str().into(),
optional: false,
}),
value: Box::new(farmfe_core::swc_ecma_ast::Expr::Ident(
new_name.as_str().into(),
Expand All @@ -123,10 +124,9 @@ impl<'a> VisitMut for RenameIdent<'a> {
Prop::Shorthand(ident) => {
if let Some(new_name) = self.rename(ident) {
*p = Prop::KeyValue(KeyValueProp {
key: farmfe_core::swc_ecma_ast::PropName::Ident(Ident {
key: farmfe_core::swc_ecma_ast::PropName::Ident(IdentName {
span: DUMMY_SP,
sym: ident.sym.as_str().into(),
optional: false,
}),
value: Box::new(farmfe_core::swc_ecma_ast::Expr::Ident(
new_name.as_str().into(),
Expand Down
Loading

0 comments on commit 571b37f

Please sign in to comment.