Skip to content
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
13 changes: 6 additions & 7 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/oxc_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ nonmax = { workspace = true }
rustc-hash = { workspace = true }

[dev-dependencies]
base64 = { workspace = true }
insta = { workspace = true }
oxc_parser = { workspace = true }
pico-args = { workspace = true }
33 changes: 23 additions & 10 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

use std::path::Path;

use pico_args::Arguments;

use oxc_allocator::Allocator;
use oxc_ast::ast::Program;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_codegen::{Codegen, CodegenOptions, CodegenReturn};
use oxc_parser::{ParseOptions, Parser};
use oxc_sourcemap::SourcemapVisualizer;
use oxc_span::SourceType;
use pico_args::Arguments;

// Instruction:
// create a `test.js`,
Expand All @@ -36,17 +38,20 @@ fn main() -> std::io::Result<()> {

let twice = args.contains("--twice");
let minify = args.contains("--minify");
let name = args.free_from_str().unwrap_or_else(|_| "test.js".to_string());
let sourcemap = args.contains("--sourcemap");

let name = args.free_from_str().unwrap_or_else(|_| "test.js".to_string());
let path = Path::new(&name);
let sourcemap = sourcemap.then_some(path);

let source_text = std::fs::read_to_string(path)?;
let source_type = SourceType::from_path(path).unwrap();
let mut allocator = Allocator::default();

// First round: parse and generate
let printed = {
let program = parse(&allocator, &source_text, source_type);
codegen(&program, minify)
codegen(&program, minify, sourcemap)
};
println!("First time:");
println!("{printed}");
Expand All @@ -58,7 +63,7 @@ fn main() -> std::io::Result<()> {

let program = parse(&allocator, &printed, source_type);
println!("Second time:");
let printed2 = codegen(&program, minify);
let printed2 = codegen(&program, minify, None);
println!("{printed2}");
// Check syntax error
parse(&allocator, &printed2, source_type);
Expand Down Expand Up @@ -87,9 +92,17 @@ fn parse<'a>(
}

/// Generate JavaScript code from an AST
fn codegen(program: &Program<'_>, minify: bool) -> String {
Codegen::new()
.with_options(if minify { CodegenOptions::minify() } else { CodegenOptions::default() })
.build(program)
.code
fn codegen(program: &Program<'_>, minify: bool, source_map_path: Option<&Path>) -> String {
let mut options = if minify { CodegenOptions::minify() } else { CodegenOptions::default() };
options.source_map_path = source_map_path.map(Path::to_path_buf);

let CodegenReturn { code, map, .. } = Codegen::new().with_options(options).build(program);

if let Some(map) = map {
let visualizer = SourcemapVisualizer::new(&code, &map);
println!("{}", visualizer.get_url());
println!("{}", visualizer.get_text());
}

code
}
61 changes: 0 additions & 61 deletions crates/oxc_codegen/examples/sourcemap.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,6 @@ log(__MEMBER__);
let output = result.code;
let output_map = result.map.unwrap();
let visualizer = SourcemapVisualizer::new(&output, &output_map);
let snapshot = visualizer.into_visualizer_text();
let snapshot = visualizer.get_text();
insta::assert_snapshot!("test_sourcemap", snapshot);
}
Loading