diff --git a/Cargo.toml b/Cargo.toml index a955da074e8f5..e22206f3c27c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/crates/oxc/Cargo.toml b/crates/oxc/Cargo.toml index bb3931165848b..747e93399a798 100644 --- a/crates/oxc/Cargo.toml +++ b/crates/oxc/Cargo.toml @@ -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"] diff --git a/crates/oxc_codegen/Cargo.toml b/crates/oxc_codegen/Cargo.toml index 3d724cddc5ddf..dafebfe3c25c5 100644 --- a/crates/oxc_codegen/Cargo.toml +++ b/crates/oxc_codegen/Cargo.toml @@ -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 } @@ -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"] diff --git a/crates/oxc_codegen/src/lib.rs b/crates/oxc_codegen/src/lib.rs index 997b7d62cef5d..56c6c56290b6a 100644 --- a/crates/oxc_codegen/src/lib.rs +++ b/crates/oxc_codegen/src/lib.rs @@ -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}; @@ -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, /// All the legal comments returned from [LegalComment::Linked] or [LegalComment::External]. @@ -121,6 +124,7 @@ pub struct Codegen<'a> { // Builders comments: CommentsMap, + #[cfg(feature = "sourcemap")] sourcemap_builder: Option>, } @@ -172,6 +176,7 @@ impl<'a> Codegen<'a> { indent: 0, quote: Quote::Double, comments: CommentsMap::default(), + #[cfg(feature = "sourcemap")] sourcemap_builder: None, } } @@ -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`], @@ -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() @@ -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() @@ -903,6 +921,11 @@ 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() @@ -910,4 +933,8 @@ impl<'a> Codegen<'a> { 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) {} } diff --git a/crates/oxc_codegen/tests/integration/main.rs b/crates/oxc_codegen/tests/integration/main.rs index f90d155972ca7..c3522983b9a0f 100644 --- a/crates/oxc_codegen/tests/integration/main.rs +++ b/crates/oxc_codegen/tests/integration/main.rs @@ -2,6 +2,7 @@ pub mod comments; pub mod esbuild; pub mod js; +#[cfg(feature = "sourcemap")] pub mod sourcemap; pub mod ts; diff --git a/crates/oxc_isolated_declarations/Cargo.toml b/crates/oxc_isolated_declarations/Cargo.toml index 8c851abd50646..3d65fb660798a 100644 --- a/crates/oxc_isolated_declarations/Cargo.toml +++ b/crates/oxc_isolated_declarations/Cargo.toml @@ -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 } diff --git a/crates/oxc_linter/Cargo.toml b/crates/oxc_linter/Cargo.toml index f47433ecd9b15..19d01d9ac36de 100644 --- a/crates/oxc_linter/Cargo.toml +++ b/crates/oxc_linter/Cargo.toml @@ -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 } diff --git a/crates/oxc_minifier/Cargo.toml b/crates/oxc_minifier/Cargo.toml index ff8a512b6c7bc..e1f7095e0ea7a 100644 --- a/crates/oxc_minifier/Cargo.toml +++ b/crates/oxc_minifier/Cargo.toml @@ -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 } diff --git a/crates/oxc_transformer/Cargo.toml b/crates/oxc_transformer/Cargo.toml index 9d73dc506966d..8c17ac29f98b2 100644 --- a/crates/oxc_transformer/Cargo.toml +++ b/crates/oxc_transformer/Cargo.toml @@ -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 } diff --git a/crates/oxc_transformer_plugins/Cargo.toml b/crates/oxc_transformer_plugins/Cargo.toml index 5247d9f3f5dd7..f4a7ea723b69b 100644 --- a/crates/oxc_transformer_plugins/Cargo.toml +++ b/crates/oxc_transformer_plugins/Cargo.toml @@ -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 } diff --git a/napi/minify/Cargo.toml b/napi/minify/Cargo.toml index f3398775b116e..c113c946699a3 100644 --- a/napi/minify/Cargo.toml +++ b/napi/minify/Cargo.toml @@ -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 } diff --git a/tasks/ast_tools/Cargo.toml b/tasks/ast_tools/Cargo.toml index e1f5c2e296551..dd08e509842f7 100644 --- a/tasks/ast_tools/Cargo.toml +++ b/tasks/ast_tools/Cargo.toml @@ -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 } diff --git a/tasks/benchmark/Cargo.toml b/tasks/benchmark/Cargo.toml index f24a789daff83..b7089df1d78cc 100644 --- a/tasks/benchmark/Cargo.toml +++ b/tasks/benchmark/Cargo.toml @@ -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 } diff --git a/tasks/minsize/Cargo.toml b/tasks/minsize/Cargo.toml index 2248d3e133037..20451846689b1 100644 --- a/tasks/minsize/Cargo.toml +++ b/tasks/minsize/Cargo.toml @@ -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 }