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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ oxc_ast = { version = "0.105.0", path = "crates/oxc_ast" } # AST definitions
oxc_ast_macros = { version = "0.105.0", path = "crates/oxc_ast_macros" } # AST proc macros
oxc_ast_visit = { version = "0.105.0", path = "crates/oxc_ast_visit" } # AST visitor pattern
oxc_cfg = { version = "0.105.0", path = "crates/oxc_cfg" } # Control flow graph
oxc_codegen = { version = "0.105.0", path = "crates/oxc_codegen" } # Code generation
oxc_codegen = { version = "0.105.0", path = "crates/oxc_codegen", default-features = false } # Code generation
oxc_compat = { version = "0.105.0", path = "crates/oxc_compat" } # Browser compatibility
oxc_data_structures = { version = "0.105.0", path = "crates/oxc_data_structures" } # Shared data structures
oxc_diagnostics = { version = "0.105.0", path = "crates/oxc_diagnostics" } # Error reporting
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ full = [
semantic = ["oxc_semantic"]
transformer = ["oxc_transformer", "oxc_transformer_plugins"]
minifier = ["oxc_mangler", "oxc_minifier"]
codegen = ["oxc_codegen"]
codegen = ["oxc_codegen", "oxc_codegen/sourcemap"]
mangler = ["oxc_mangler"]
cfg = ["oxc_cfg", "oxc_semantic/cfg"]
isolated_declarations = ["oxc_isolated_declarations"]
Expand Down
6 changes: 5 additions & 1 deletion crates/oxc_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ oxc_ast = { workspace = true }
oxc_data_structures = { workspace = true, features = ["code_buffer", "slice_iter", "stack"] }
oxc_index = { workspace = true }
oxc_semantic = { workspace = true }
oxc_sourcemap = { workspace = true }
oxc_sourcemap = { workspace = true, optional = true }
oxc_span = { workspace = true }
oxc_syntax = { workspace = true }

Expand All @@ -39,3 +39,7 @@ rustc-hash = { workspace = true }
insta = { workspace = true }
oxc_parser = { workspace = true }
pico-args = { workspace = true }

[features]
default = ["sourcemap"]
sourcemap = ["dep:oxc_sourcemap"]
29 changes: 28 additions & 1 deletion crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ mod context;
mod r#gen;
mod operator;
mod options;
#[cfg(feature = "sourcemap")]
mod sourcemap_builder;
mod str;

use binary_expr_visitor::BinaryExpressionVisitor;
use comment::CommentsMap;
use operator::Operator;
#[cfg(feature = "sourcemap")]
use sourcemap_builder::SourcemapBuilder;
use str::{Quote, cold_branch, is_script_close_tag};

Expand All @@ -53,6 +55,7 @@ pub struct CodegenReturn {
/// The source map from the input source code to the generated source code.
///
/// You must set [`CodegenOptions::source_map_path`] for this to be [`Some`].
#[cfg(feature = "sourcemap")]
pub map: Option<oxc_sourcemap::SourceMap>,

/// All the legal comments returned from [LegalComment::Linked] or [LegalComment::External].
Expand Down Expand Up @@ -121,6 +124,7 @@ pub struct Codegen<'a> {
// Builders
comments: CommentsMap,

#[cfg(feature = "sourcemap")]
sourcemap_builder: Option<SourcemapBuilder<'a>>,
}

Expand Down Expand Up @@ -172,6 +176,7 @@ impl<'a> Codegen<'a> {
indent: 0,
quote: Quote::Double,
comments: CommentsMap::default(),
#[cfg(feature = "sourcemap")]
sourcemap_builder: None,
}
}
Expand Down Expand Up @@ -224,14 +229,21 @@ impl<'a> Codegen<'a> {
self.indent = self.options.initial_indent;
self.code.reserve(program.source_text.len());
self.build_comments(&program.comments);
#[cfg(feature = "sourcemap")]
if let Some(path) = &self.options.source_map_path {
self.sourcemap_builder = Some(SourcemapBuilder::new(path, program.source_text));
}
program.print(&mut self, Context::default());
let legal_comments = self.handle_eof_linked_or_external_comments(program);
let code = self.code.into_string();
#[cfg(feature = "sourcemap")]
let map = self.sourcemap_builder.map(SourcemapBuilder::into_sourcemap);
CodegenReturn { code, map, legal_comments }
CodegenReturn {
code,
#[cfg(feature = "sourcemap")]
map,
legal_comments,
}
}

/// Turn what's been built so far into a string. Like [`build`],
Expand Down Expand Up @@ -877,6 +889,7 @@ impl<'a> Codegen<'a> {
}
}

#[cfg(feature = "sourcemap")]
fn add_source_mapping(&mut self, span: Span) {
if let Some(sourcemap_builder) = self.sourcemap_builder.as_mut()
&& !span.is_empty()
Expand All @@ -885,6 +898,11 @@ impl<'a> Codegen<'a> {
}
}

#[cfg(not(feature = "sourcemap"))]
#[inline]
fn add_source_mapping(&mut self, _span: Span) {}

#[cfg(feature = "sourcemap")]
fn add_source_mapping_end(&mut self, span: Span) {
if let Some(sourcemap_builder) = self.sourcemap_builder.as_mut()
&& !span.is_empty()
Expand All @@ -903,11 +921,20 @@ impl<'a> Codegen<'a> {
}
}

#[cfg(not(feature = "sourcemap"))]
#[inline]
fn add_source_mapping_end(&mut self, _span: Span) {}

#[cfg(feature = "sourcemap")]
fn add_source_mapping_for_name(&mut self, span: Span, name: &str) {
if let Some(sourcemap_builder) = self.sourcemap_builder.as_mut()
&& !span.is_empty()
{
sourcemap_builder.add_source_mapping_for_name(self.code.as_bytes(), span, name);
}
}

#[cfg(not(feature = "sourcemap"))]
#[inline]
fn add_source_mapping_for_name(&mut self, _span: Span, _name: &str) {}
}
1 change: 1 addition & 0 deletions crates/oxc_codegen/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub mod comments;
pub mod esbuild;
pub mod js;
#[cfg(feature = "sourcemap")]
pub mod sourcemap;
pub mod ts;

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ rustc-hash = { workspace = true }

[dev-dependencies]
insta = { workspace = true, features = ["glob"] }
oxc_codegen = { workspace = true }
oxc_codegen = { workspace = true, features = ["sourcemap"] }
oxc_parser = { workspace = true }
2 changes: 1 addition & 1 deletion crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ oxc_ast = { workspace = true }
oxc_ast_macros = { workspace = true }
oxc_ast_visit = { workspace = true, features = ["serialize"] }
oxc_cfg = { workspace = true }
oxc_codegen = { workspace = true }
oxc_codegen = { workspace = true, default-features = false }
oxc_data_structures = { workspace = true, features = ["box_macros"] }
oxc_diagnostics = { workspace = true }
oxc_ecmascript = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ doctest = false
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_ast_visit = { workspace = true }
oxc_codegen = { workspace = true }
oxc_codegen = { workspace = true, features = ["sourcemap"] }
oxc_compat = { workspace = true }
oxc_data_structures = { workspace = true, features = ["stack"] }
oxc_ecmascript = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ sha1 = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
oxc_codegen = { workspace = true }
oxc_codegen = { workspace = true, features = ["sourcemap"] }
oxc_parser = { workspace = true }
pico-args = { workspace = true }
2 changes: 1 addition & 1 deletion crates/oxc_transformer_plugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ insta = { workspace = true }
pico-args = { workspace = true }
similar = { workspace = true }

oxc_codegen = { workspace = true }
oxc_codegen = { workspace = true, features = ["sourcemap"] }
oxc_minifier = { workspace = true }
oxc_parser = { workspace = true }
oxc_semantic = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion napi/minify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ doctest = false

[dependencies]
oxc_allocator = { workspace = true }
oxc_codegen = { workspace = true }
oxc_codegen = { workspace = true, features = ["sourcemap"] }
oxc_compat = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_minifier = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion tasks/ast_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ doctest = false
oxc_allocator = { workspace = true, optional = true }
oxc_ast = { workspace = true, optional = true }
oxc_ast_visit = { workspace = true, optional = true }
oxc_codegen = { workspace = true, optional = true }
oxc_codegen = { workspace = true, optional = true, features = ["sourcemap"] }
oxc_minifier = { workspace = true, optional = true }
oxc_parser = { workspace = true, optional = true }
oxc_span = { workspace = true, features = ["serialize"], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion tasks/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bench = false
oxc_allocator = { workspace = true, optional = true }
oxc_ast = { workspace = true, optional = true, features = ["serialize"] }
oxc_ast_visit = { workspace = true, optional = true, features = ["serialize"] }
oxc_codegen = { workspace = true, optional = true }
oxc_codegen = { workspace = true, optional = true, features = ["sourcemap"] }
oxc_formatter = { workspace = true, optional = true }
oxc_isolated_declarations = { workspace = true, optional = true }
oxc_linter = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion tasks/minsize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ doctest = false

[dependencies]
oxc_allocator = { workspace = true }
oxc_codegen = { workspace = true }
oxc_codegen = { workspace = true, features = ["sourcemap"] }
oxc_minifier = { workspace = true }
oxc_parser = { workspace = true }
oxc_semantic = { workspace = true }
Expand Down
Loading