Skip to content

Commit 6f5b72a

Browse files
authored
refactor(es/preset-env): Apply swc_ecma_compiler in preset env (#10921)
1 parent 3ed4a12 commit 6f5b72a

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed

.changeset/gentle-spies-complain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_ecma_preset_env: major
3+
---
4+
5+
refactor(es/preset-env): Apply swc_ecma_compiler in preset env

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/swc_ecma_preset_env/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ string_enum = { version = "1.0.2", path = "../string_enum" }
3838
swc_atoms = { version = "7.0.0", path = "../swc_atoms" }
3939
swc_common = { version = "14.0.1", path = "../swc_common" }
4040
swc_ecma_ast = { version = "14.0.0", path = "../swc_ecma_ast" }
41+
swc_ecma_compiler = { version = "0.1.2", path = "../swc_ecma_compiler" }
4142
swc_ecma_transforms = { version = "26.0.0", path = "../swc_ecma_transforms", features = [
4243
"compat",
4344
"proposal",

crates/swc_ecma_preset_env/src/lib.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use swc_ecma_transforms::{
1616
bugfixes,
1717
class_fields_use_set::class_fields_use_set,
1818
es2015::{self, generator::generator},
19-
es2016, es2017, es2018, es2019, es2020, es2021, es2022, es3,
19+
es2016, es2017, es2018, es2019, es2020, es2022, es3,
2020
regexp::{self, regexp},
2121
},
2222
Assumptions,
@@ -68,6 +68,44 @@ where
6868
}};
6969
}
7070

71+
macro_rules! add_compiler {
72+
($($feature:ident)|*) => {{
73+
let mut feature = swc_ecma_compiler::Features::empty();
74+
$(
75+
{
76+
let f = transform_data::Feature::$feature;
77+
let enable = !caniuse(f);
78+
79+
if debug {
80+
println!("{}: {:?}", f.as_str(), enable);
81+
}
82+
83+
if enable {
84+
feature |= swc_ecma_compiler::Features::from(f);
85+
}
86+
}
87+
)*
88+
feature
89+
}};
90+
(| $($feature:ident)|*) => {{
91+
add_compiler!($($feature)|*)
92+
}};
93+
($prev:expr, $($feature:ident)|*) => {{
94+
let feature = add_compiler!($($feature)|*);
95+
(
96+
$prev,
97+
swc_ecma_compiler::Compiler::new(swc_ecma_compiler::Config {
98+
includes: feature,
99+
assumptions,
100+
..Default::default()
101+
}),
102+
)
103+
}};
104+
($prev:expr, | $($feature:ident)|*) => {{
105+
add_compiler!($prev, $($feature)|*)
106+
}};
107+
}
108+
71109
let pass = (
72110
pass,
73111
Optional::new(
@@ -116,7 +154,7 @@ where
116154
// static block needs to be placed before class property
117155
// because it transforms into private static property
118156

119-
let pass = add!(pass, ClassStaticBlock, es2022::static_blocks());
157+
let pass = add_compiler!(pass, ClassStaticBlock);
120158
let pass = add!(
121159
pass,
122160
ClassProperties,
@@ -131,18 +169,16 @@ where
131169
unresolved_mark
132170
)
133171
);
134-
let pass = add!(pass, PrivatePropertyInObject, es2022::private_in_object());
135172

136-
// ES2021
137-
let pass = add!(
173+
#[rustfmt::skip]
174+
let pass = add_compiler!(
138175
pass,
139-
LogicalAssignmentOperators,
140-
es2021::logical_assignments()
176+
/* ES2022 */ | PrivatePropertyInObject
177+
/* ES2021 */ | LogicalAssignmentOperators
178+
/* ES2020 */ | ExportNamespaceFrom
141179
);
142180

143181
// ES2020
144-
145-
let pass = add!(pass, ExportNamespaceFrom, es2020::export_namespace_from());
146182
let pass = add!(
147183
pass,
148184
NullishCoalescing,

crates/swc_ecma_preset_env/src/transform_data.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ pub enum Feature {
204204
BugfixTransformSafariClassFieldInitializerScope, // TODO
205205
}
206206

207+
// [TODO]: Unify Feature flag
208+
impl From<Feature> for swc_ecma_compiler::Features {
209+
fn from(value: Feature) -> Self {
210+
match value {
211+
Feature::ClassStaticBlock => Self::STATIC_BLOCKS,
212+
Feature::OptionalChaining => Self::OPTIONAL_CHAINING,
213+
Feature::PrivatePropertyInObject => Self::PRIVATE_IN_OBJECT,
214+
Feature::LogicalAssignmentOperators => Self::LOGICAL_ASSIGNMENTS,
215+
Feature::ExportNamespaceFrom => Self::EXPORT_NAMESPACE_FROM,
216+
_ => Self::empty(),
217+
}
218+
}
219+
}
220+
207221
pub(crate) static FEATURES: Lazy<FxHashMap<Feature, BrowserData<Option<Version>>>> =
208222
Lazy::new(|| {
209223
let map: FxHashMap<Feature, BrowserData<Option<String>>> =

0 commit comments

Comments
 (0)