Skip to content

Commit a13f5cb

Browse files
authoredSep 29, 2023
refactor(es): Extract parser/codegen code for swc::Compiler (#8030)
1 parent 9203626 commit a13f5cb

File tree

9 files changed

+672
-513
lines changed

9 files changed

+672
-513
lines changed
 

‎Cargo.lock

+60-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bindings/Cargo.lock

+87-62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bindings/binding_minifier_node/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ swc_core = { version = "0.83.18", features = [
4242
"ecma_minifier",
4343
] }
4444
swc_node_base = "0.5.8"
45+
swc_compiler_base = { version = "0.1.0", features = ["node"] }

‎crates/swc/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ concurrent = [
2727
debug = ["swc_ecma_visit/debug"]
2828
default = ["es3"]
2929
es3 = []
30-
node = ["napi", "napi-derive"]
30+
node = ["napi", "napi-derive", "swc_compiler_base/node"]
3131
plugin = [
3232
"swc_plugin_runner/ecma",
3333
"swc_plugin_runner/rkyv-impl",
@@ -73,6 +73,7 @@ swc_common = { version = "0.32.1", path = "../swc_common", features = [
7373
"sourcemap",
7474
"parking_lot",
7575
] }
76+
swc_compiler_base = { version = "0.1.0", path = "../swc_compiler_base" }
7677
swc_config = { version = "0.1.7", path = "../swc_config" }
7778
swc_ecma_ast = { version = "0.109.1", path = "../swc_ecma_ast" }
7879
swc_ecma_codegen = { version = "0.145.5", path = "../swc_ecma_codegen" }

‎crates/swc/src/config/mod.rs

+13-112
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
#![cfg_attr(
2-
any(not(any(feature = "plugin")), target_arch = "wasm32"),
3-
allow(unused)
4-
)]
51
use std::{
62
collections::{HashMap, HashSet},
7-
env, fmt,
3+
env,
84
path::{Path, PathBuf},
95
sync::Arc,
106
usize,
@@ -16,20 +12,19 @@ use either::Either;
1612
use indexmap::IndexMap;
1713
use once_cell::sync::Lazy;
1814
use rustc_hash::FxHashMap;
19-
use serde::{
20-
de::{Unexpected, Visitor},
21-
Deserialize, Deserializer, Serialize, Serializer,
22-
};
15+
use serde::{Deserialize, Serialize};
2316
use swc_atoms::JsWord;
2417
use swc_cached::regex::CachedRegex;
18+
#[allow(unused)]
19+
use swc_common::plugin::metadata::TransformPluginMetadataContext;
2520
use swc_common::{
2621
chain,
2722
collections::{AHashMap, AHashSet, ARandomState},
2823
comments::{Comments, SingleThreadedComments},
2924
errors::Handler,
30-
plugin::metadata::TransformPluginMetadataContext,
3125
FileName, Mark, SourceMap, SyntaxContext,
3226
};
27+
pub use swc_compiler_base::{IsModule, SourceMapsConfig};
3328
use swc_config::{
3429
config_types::{BoolConfig, BoolOr, BoolOrDataConfig, MergingOption},
3530
merge::Merge,
@@ -40,15 +35,11 @@ use swc_ecma_lints::{
4035
config::LintConfig,
4136
rules::{lint_to_fold, LintParams},
4237
};
43-
use swc_ecma_loader::{
44-
resolvers::{lru::CachingResolver, node::NodeModulesResolver, tsc::TsConfigResolver},
45-
TargetEnv,
38+
use swc_ecma_loader::resolvers::{
39+
lru::CachingResolver, node::NodeModulesResolver, tsc::TsConfigResolver,
4640
};
4741
pub use swc_ecma_minifier::js::*;
48-
use swc_ecma_minifier::option::{
49-
terser::{TerserCompressorOptions, TerserEcmaVersion, TerserTopLevelOptions},
50-
MangleOptions,
51-
};
42+
use swc_ecma_minifier::option::terser::TerserTopLevelOptions;
5243
#[allow(deprecated)]
5344
pub use swc_ecma_parser::JscTarget;
5445
use swc_ecma_parser::{parse_file_as_expr, Syntax, TsConfig};
@@ -111,74 +102,6 @@ pub fn init_plugin_module_cache_once(
111102
});
112103
}
113104

114-
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
115-
pub enum IsModule {
116-
Bool(bool),
117-
Unknown,
118-
}
119-
120-
impl Default for IsModule {
121-
fn default() -> Self {
122-
IsModule::Bool(true)
123-
}
124-
}
125-
126-
impl Merge for IsModule {
127-
fn merge(&mut self, other: Self) {
128-
if *self == Default::default() {
129-
*self = other;
130-
}
131-
}
132-
}
133-
134-
impl Serialize for IsModule {
135-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
136-
where
137-
S: Serializer,
138-
{
139-
match *self {
140-
IsModule::Bool(ref b) => b.serialize(serializer),
141-
IsModule::Unknown => "unknown".serialize(serializer),
142-
}
143-
}
144-
}
145-
146-
impl<'de> Deserialize<'de> for IsModule {
147-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
148-
where
149-
D: Deserializer<'de>,
150-
{
151-
struct IsModuleVisitor;
152-
153-
impl<'de> Visitor<'de> for IsModuleVisitor {
154-
type Value = IsModule;
155-
156-
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
157-
formatter.write_str("a boolean or the string 'unknown'")
158-
}
159-
160-
fn visit_bool<E>(self, b: bool) -> Result<Self::Value, E>
161-
where
162-
E: serde::de::Error,
163-
{
164-
Ok(IsModule::Bool(b))
165-
}
166-
167-
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
168-
where
169-
E: serde::de::Error,
170-
{
171-
match s {
172-
"unknown" => Ok(IsModule::Unknown),
173-
_ => Err(serde::de::Error::invalid_value(Unexpected::Str(s), &self)),
174-
}
175-
}
176-
}
177-
178-
deserializer.deserialize_any(IsModuleVisitor)
179-
}
180-
}
181-
182105
#[derive(Default, Clone, Serialize, Deserialize)]
183106
#[serde(rename_all = "camelCase")]
184107
pub struct ParseOptions {
@@ -273,32 +196,6 @@ impl Options {
273196
}
274197
}
275198

276-
/// Configuration related to source map generated by swc.
277-
#[derive(Clone, Serialize, Deserialize, Debug)]
278-
#[serde(untagged)]
279-
pub enum SourceMapsConfig {
280-
Bool(bool),
281-
Str(String),
282-
}
283-
284-
impl SourceMapsConfig {
285-
pub fn enabled(&self) -> bool {
286-
match *self {
287-
SourceMapsConfig::Bool(b) => b,
288-
SourceMapsConfig::Str(ref s) => {
289-
assert_eq!(s, "inline", "Source map must be true, false or inline");
290-
true
291-
}
292-
}
293-
}
294-
}
295-
296-
impl Default for SourceMapsConfig {
297-
fn default() -> Self {
298-
SourceMapsConfig::Bool(true)
299-
}
300-
}
301-
302199
#[derive(Debug, Clone, Serialize, Deserialize)]
303200
#[serde(untagged)]
304201
pub enum InputSourceMap {
@@ -694,7 +591,11 @@ impl Options {
694591

695592
let plugin_resolver = CachingResolver::new(
696593
40,
697-
NodeModulesResolver::new(TargetEnv::Node, Default::default(), true),
594+
NodeModulesResolver::new(
595+
swc_ecma_loader::TargetEnv::Node,
596+
Default::default(),
597+
true,
598+
),
698599
);
699600

700601
if let Some(plugins) = &experimental.plugins {

‎crates/swc/src/config/tests.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use serde_json;
2-
3-
use crate::{parse_swcrc, Options};
1+
use crate::parse_swcrc;
42

53
#[test]
64
fn object() {

‎crates/swc/src/lib.rs

+39-298
Large diffs are not rendered by default.

‎crates/swc_compiler_base/Cargo.toml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[package]
2+
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
3+
description = "Base crate for the 'swc' crate. This is not a public API."
4+
documentation = "https://rustdoc.swc.rs/swc_compiler_base/"
5+
edition = "2021"
6+
include = ["Cargo.toml", "src/**/*.rs"]
7+
license = "Apache-2.0"
8+
name = "swc_compiler_base"
9+
repository = "https://github.com/swc-project/swc.git"
10+
version = "0.1.0"
11+
12+
[features]
13+
node = ["napi", "napi-derive"]
14+
15+
[dependencies]
16+
anyhow = "1.0.75"
17+
base64 = "0.13.0"
18+
pathdiff = "0.2.0"
19+
serde = { version = "1.0.188", features = ["derive"] }
20+
sourcemap = "6.2"
21+
swc_atoms = { version = "0.5.9", path = "../swc_atoms" }
22+
swc_common = { version = "0.32.1", path = "../swc_common" }
23+
swc_config = { version = "0.1.7", path = "../swc_config" }
24+
swc_ecma_ast = { version = "0.109.1", path = "../swc_ecma_ast" }
25+
swc_ecma_codegen = { version = "0.145.5", path = "../swc_ecma_codegen" }
26+
swc_ecma_minifier = { version = "0.187.22", path = "../swc_ecma_minifier" }
27+
swc_ecma_parser = { version = "0.140.0", path = "../swc_ecma_parser" }
28+
swc_ecma_visit = { version = "0.95.1", path = "../swc_ecma_visit" }
29+
swc_timer = { version = "0.20.1", path = "../swc_timer" }
30+
31+
[dependencies.napi-derive]
32+
default-features = false
33+
features = ["type-def"]
34+
optional = true
35+
version = "2.0.0"
36+
37+
[dependencies.napi]
38+
default-features = false
39+
features = ["napi3"]
40+
optional = true
41+
version = "2.0.0"

‎crates/swc_compiler_base/src/lib.rs

+428
Large diffs are not rendered by default.

1 commit comments

Comments
 (1)

github-actions[bot] commented on Sep 29, 2023

@github-actions[bot]

Benchmark

Benchmark suite Current: a13f5cb Previous: 725f7f5 Ratio
es/full/bugs-1 280201 ns/iter (± 5607) 275732 ns/iter (± 5271) 1.02
es/full/minify/libraries/antd 1310526047 ns/iter (± 11376564) 1317335861 ns/iter (± 18339464) 0.99
es/full/minify/libraries/d3 275164752 ns/iter (± 3153972) 278448673 ns/iter (± 4068117) 0.99
es/full/minify/libraries/echarts 1054495733 ns/iter (± 7356799) 1069715861 ns/iter (± 18519258) 0.99
es/full/minify/libraries/jquery 84242648 ns/iter (± 275071) 83943412 ns/iter (± 356041) 1.00
es/full/minify/libraries/lodash 97837238 ns/iter (± 365983) 97164732 ns/iter (± 1003130) 1.01
es/full/minify/libraries/moment 50195787 ns/iter (± 236687) 49692797 ns/iter (± 507924) 1.01
es/full/minify/libraries/react 18104776 ns/iter (± 52715) 17927125 ns/iter (± 121391) 1.01
es/full/minify/libraries/terser 218314555 ns/iter (± 1291933) 217851544 ns/iter (± 556096) 1.00
es/full/minify/libraries/three 387263870 ns/iter (± 5311824) 384824470 ns/iter (± 4641007) 1.01
es/full/minify/libraries/typescript 2656679454 ns/iter (± 8650809) 2619621222 ns/iter (± 14311976) 1.01
es/full/minify/libraries/victory 565451908 ns/iter (± 5860535) 565626120 ns/iter (± 13492290) 1.00
es/full/minify/libraries/vue 119774923 ns/iter (± 662793) 119160498 ns/iter (± 887111) 1.01
es/full/codegen/es3 34428 ns/iter (± 308) 32902 ns/iter (± 350) 1.05
es/full/codegen/es5 34455 ns/iter (± 144) 33216 ns/iter (± 105) 1.04
es/full/codegen/es2015 34489 ns/iter (± 108) 33148 ns/iter (± 326) 1.04
es/full/codegen/es2016 34539 ns/iter (± 95) 33032 ns/iter (± 342) 1.05
es/full/codegen/es2017 34455 ns/iter (± 96) 32954 ns/iter (± 237) 1.05
es/full/codegen/es2018 34432 ns/iter (± 144) 32607 ns/iter (± 355) 1.06
es/full/codegen/es2019 34432 ns/iter (± 74) 32854 ns/iter (± 245) 1.05
es/full/codegen/es2020 34382 ns/iter (± 113) 32569 ns/iter (± 558) 1.06
es/full/all/es3 163524166 ns/iter (± 1438852) 163851776 ns/iter (± 2172918) 1.00
es/full/all/es5 156285705 ns/iter (± 1300526) 156793280 ns/iter (± 1166950) 1.00
es/full/all/es2015 117285573 ns/iter (± 1237867) 116819362 ns/iter (± 1439385) 1.00
es/full/all/es2016 115618089 ns/iter (± 1139415) 114867685 ns/iter (± 1483834) 1.01
es/full/all/es2017 115560119 ns/iter (± 812321) 114815280 ns/iter (± 882306) 1.01
es/full/all/es2018 112997450 ns/iter (± 1708116) 112425090 ns/iter (± 1664909) 1.01
es/full/all/es2019 112210471 ns/iter (± 918013) 111800171 ns/iter (± 927539) 1.00
es/full/all/es2020 108142879 ns/iter (± 839416) 107668447 ns/iter (± 879134) 1.00
es/full/parser 490869 ns/iter (± 4803) 484279 ns/iter (± 7857) 1.01
es/full/base/fixer 18805 ns/iter (± 187) 18425 ns/iter (± 241) 1.02
es/full/base/resolver_and_hygiene 81280 ns/iter (± 315) 78884 ns/iter (± 922) 1.03
serialization of serde 281 ns/iter (± 5) 292 ns/iter (± 4) 0.96

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.