Skip to content

Commit 3cc97ac

Browse files
authored
make swc transform behave the same as oxc (#5)
1 parent 2e8df6e commit 3cc97ac

File tree

7 files changed

+46
-19
lines changed

7 files changed

+46
-19
lines changed

Diff for: Cargo.lock

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

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ swc = "0.276.0"
2323
swc_common = "0.33.26"
2424
swc_ecma_ast = "0.113.7"
2525
swc_ecma_parser = { version = "0.144.2", features = ["typescript"] }
26+
swc_ecma_transforms = "0.230.1"
2627
swc_ecma_transforms_react = "0.184.1"
2728
swc_ecma_transforms_typescript = "0.189.1"
2829
swc_ecma_visit = "0.99.1"

Diff for: README.md

+11
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ pnpm run table
5454

5555
## Maximum Resident Set Size
5656

57+
```
58+
./memory.sh
59+
60+
./files/cal.com.tsx
61+
oxc 41.7 mb
62+
swc 31.1 mb
63+
64+
./files/typescript.js
65+
oxc 224.4 mb
66+
swc 160.1 mb
67+
```
5768

5869
## Setup
5970

Diff for: benches/transformer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ trait TheBencher {
3737
struct OxcBencher;
3838

3939
impl TheBencher for OxcBencher {
40-
type RunOutput = oxc::allocator::Allocator;
40+
type RunOutput = (oxc::allocator::Allocator, String);
4141

4242
const ID: &'static str = "oxc";
4343

@@ -49,7 +49,7 @@ impl TheBencher for OxcBencher {
4949
struct SwcBencher;
5050

5151
impl TheBencher for SwcBencher {
52-
type RunOutput = swc_ecma_ast::Program;
52+
type RunOutput = (swc_ecma_ast::Program, String);
5353

5454
const ID: &'static str = "swc";
5555

Diff for: src/lib.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ pub mod oxc {
99
transformer::{ReactOptions, TransformOptions, Transformer, TypeScriptOptions},
1010
};
1111

12-
pub fn transform(path: &Path, source_text: &str) -> Allocator {
12+
pub fn transform(path: &Path, source_text: &str) -> (Allocator, String) {
1313
let allocator = Allocator::default();
1414
let source_type = SourceType::from_path(path).unwrap();
15-
{
15+
let printed = {
1616
let ret = Parser::new(&allocator, source_text, source_type).parse();
1717
let trivias = ret.trivias;
1818
let mut program = ret.program;
@@ -31,27 +31,29 @@ pub mod oxc {
3131
)
3232
.build(&mut program)
3333
.unwrap();
34-
let _transformed_text =
35-
Codegen::<false>::new("", source_text, CodegenOptions::default(), None)
36-
.build(&program);
37-
}
38-
allocator
34+
Codegen::<false>::new("", source_text, CodegenOptions::default(), None)
35+
.build(&program)
36+
.source_text
37+
};
38+
39+
(allocator, printed)
3940
}
4041
}
4142

4243
pub mod swc {
43-
use std::path::Path;
44+
use std::{path::Path, sync::Arc};
4445

45-
use std::sync::Arc;
4646
use swc::{Compiler, PrintArgs, SwcComments};
4747
use swc_common::{chain, source_map::SourceMap, sync::Lrc, Mark, GLOBALS};
4848
use swc_ecma_ast::Program;
4949
use swc_ecma_parser::{EsConfig, Parser, StringInput, Syntax, TsConfig};
50-
use swc_ecma_transforms_react::{react, Options};
50+
use swc_ecma_transforms::resolver;
51+
use swc_ecma_transforms_react::{react, Options, Runtime};
5152
use swc_ecma_transforms_typescript::strip;
5253
use swc_ecma_visit::FoldWith;
54+
use swc_ecma_visit::VisitMutWith;
5355

54-
pub fn transform(path: &Path, source_text: &str) -> Program {
56+
pub fn transform(path: &Path, source_text: &str) -> (Program, String) {
5557
let cm = Lrc::new(SourceMap::new(swc_common::FilePathMapping::empty()));
5658
let compiler = Compiler::new(Arc::clone(&cm));
5759
let comments = SwcComments::default();
@@ -66,29 +68,39 @@ pub mod swc {
6668

6769
GLOBALS.set(&Default::default(), || {
6870
let input = StringInput::new(source_text, Default::default(), Default::default());
69-
let program = Parser::new(syntax, input, Some(&comments))
71+
let mut program = Parser::new(syntax, input, Some(&comments))
7072
.parse_program()
7173
.unwrap();
7274

7375
let top_level_mark = Mark::new();
7476
let unresolved_mark = Mark::new();
77+
78+
program.visit_mut_with(&mut resolver(
79+
unresolved_mark,
80+
top_level_mark,
81+
syntax.typescript(),
82+
));
83+
7584
let mut ast_pass = chain!(
7685
strip(top_level_mark),
7786
react(
7887
Arc::clone(&cm),
7988
Some(comments),
80-
Options::default(),
89+
Options {
90+
runtime: Some(Runtime::Automatic),
91+
..Options::default()
92+
},
8193
top_level_mark,
8294
unresolved_mark
8395
),
8496
);
8597
let program = program.fold_with(&mut ast_pass);
8698

87-
let _ret = compiler
99+
let printed = compiler
88100
.print(&program, PrintArgs::default())
89101
.expect("print failed");
90102

91-
program
103+
(program, printed.code)
92104
})
93105
}
94106
}

Diff for: src/oxc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ pub fn main() {
99
let path = env::args().nth(1).unwrap();
1010
let path = Path::new(&path);
1111
let source_text = fs::read_to_string(path).unwrap();
12-
let _ = oxc::transform(path, &source_text);
12+
let _output = oxc::transform(path, &source_text);
13+
// println!("{}", output.1);
1314
}

Diff for: src/swc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ pub fn main() {
99
let path = env::args().nth(1).unwrap();
1010
let path = Path::new(&path);
1111
let source_text = fs::read_to_string(path).unwrap();
12-
let _ = swc::transform(path, &source_text);
12+
let _output = swc::transform(path, &source_text);
13+
// println!("{}", output.1);
1314
}

0 commit comments

Comments
 (0)