Skip to content

make swc transform behave the same as oxc #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ swc = "0.276.0"
swc_common = "0.33.26"
swc_ecma_ast = "0.113.7"
swc_ecma_parser = { version = "0.144.2", features = ["typescript"] }
swc_ecma_transforms = "0.230.1"
swc_ecma_transforms_react = "0.184.1"
swc_ecma_transforms_typescript = "0.189.1"
swc_ecma_visit = "0.99.1"
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ pnpm run table

## Maximum Resident Set Size

```
./memory.sh

./files/cal.com.tsx
oxc 41.7 mb
swc 31.1 mb

./files/typescript.js
oxc 224.4 mb
swc 160.1 mb
```

## Setup

Expand Down
4 changes: 2 additions & 2 deletions benches/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait TheBencher {
struct OxcBencher;

impl TheBencher for OxcBencher {
type RunOutput = oxc::allocator::Allocator;
type RunOutput = (oxc::allocator::Allocator, String);

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

Expand All @@ -49,7 +49,7 @@ impl TheBencher for OxcBencher {
struct SwcBencher;

impl TheBencher for SwcBencher {
type RunOutput = swc_ecma_ast::Program;
type RunOutput = (swc_ecma_ast::Program, String);

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

Expand Down
42 changes: 27 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ pub mod oxc {
transformer::{ReactOptions, TransformOptions, Transformer, TypeScriptOptions},
};

pub fn transform(path: &Path, source_text: &str) -> Allocator {
pub fn transform(path: &Path, source_text: &str) -> (Allocator, String) {
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
{
let printed = {
let ret = Parser::new(&allocator, source_text, source_type).parse();
let trivias = ret.trivias;
let mut program = ret.program;
Expand All @@ -31,27 +31,29 @@ pub mod oxc {
)
.build(&mut program)
.unwrap();
let _transformed_text =
Codegen::<false>::new("", source_text, CodegenOptions::default(), None)
.build(&program);
}
allocator
Codegen::<false>::new("", source_text, CodegenOptions::default(), None)
.build(&program)
.source_text
};

(allocator, printed)
}
}

pub mod swc {
use std::path::Path;
use std::{path::Path, sync::Arc};

use std::sync::Arc;
use swc::{Compiler, PrintArgs, SwcComments};
use swc_common::{chain, source_map::SourceMap, sync::Lrc, Mark, GLOBALS};
use swc_ecma_ast::Program;
use swc_ecma_parser::{EsConfig, Parser, StringInput, Syntax, TsConfig};
use swc_ecma_transforms_react::{react, Options};
use swc_ecma_transforms::resolver;
use swc_ecma_transforms_react::{react, Options, Runtime};
use swc_ecma_transforms_typescript::strip;
use swc_ecma_visit::FoldWith;
use swc_ecma_visit::VisitMutWith;

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

GLOBALS.set(&Default::default(), || {
let input = StringInput::new(source_text, Default::default(), Default::default());
let program = Parser::new(syntax, input, Some(&comments))
let mut program = Parser::new(syntax, input, Some(&comments))
.parse_program()
.unwrap();

let top_level_mark = Mark::new();
let unresolved_mark = Mark::new();

program.visit_mut_with(&mut resolver(
unresolved_mark,
top_level_mark,
syntax.typescript(),
));

let mut ast_pass = chain!(
strip(top_level_mark),
react(
Arc::clone(&cm),
Some(comments),
Options::default(),
Options {
runtime: Some(Runtime::Automatic),
..Options::default()
},
top_level_mark,
unresolved_mark
),
);
let program = program.fold_with(&mut ast_pass);

let _ret = compiler
let printed = compiler
.print(&program, PrintArgs::default())
.expect("print failed");

program
(program, printed.code)
})
}
}
3 changes: 2 additions & 1 deletion src/oxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ pub fn main() {
let path = env::args().nth(1).unwrap();
let path = Path::new(&path);
let source_text = fs::read_to_string(path).unwrap();
let _ = oxc::transform(path, &source_text);
let _output = oxc::transform(path, &source_text);
// println!("{}", output.1);
}
3 changes: 2 additions & 1 deletion src/swc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ pub fn main() {
let path = env::args().nth(1).unwrap();
let path = Path::new(&path);
let source_text = fs::read_to_string(path).unwrap();
let _ = swc::transform(path, &source_text);
let _output = swc::transform(path, &source_text);
// println!("{}", output.1);
}