Skip to content

Commit

Permalink
feat(wasm): support codegen sourcemap (#9315)
Browse files Browse the repository at this point in the history
- Related oxc-project/playground#52
- Required by oxc-project/playground#63

This PR adds `CodegenOption.enable_sourcemap` option and
`codegen_sourcemap_text` state for
oxc-project/playground#52.
  • Loading branch information
hi-ogawa authored Feb 24, 2025
1 parent 6764b8d commit d216bb8
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 92 deletions.
30 changes: 25 additions & 5 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ pub struct Oxc {
#[wasm_bindgen(readonly, skip_typescript, js_name = "codegenText")]
pub codegen_text: String,

#[wasm_bindgen(readonly, skip_typescript, js_name = "codegenSourcemapText")]
pub codegen_sourcemap_text: Option<String>,

#[wasm_bindgen(readonly, skip_typescript, js_name = "formattedText")]
pub formatted_text: String,

Expand Down Expand Up @@ -178,7 +181,7 @@ impl Oxc {
let parser_options = parser_options.unwrap_or_default();
let _linter_options = linter_options.unwrap_or_default();
let minifier_options = minifier_options.unwrap_or_default();
let _codegen_options = codegen_options.unwrap_or_default();
let codegen_options = codegen_options.unwrap_or_default();
let transform_options = transform_options.unwrap_or_default();
let control_flow_options = control_flow_options.unwrap_or_default();

Expand Down Expand Up @@ -252,10 +255,22 @@ impl Oxc {
IsolatedDeclarations::new(&allocator, IsolatedDeclarationsOptions::default())
.build(&program);
if ret.errors.is_empty() {
self.codegen_text = CodeGenerator::new().build(&ret.program).code;
let codegen_result = CodeGenerator::new()
.with_options(CodegenOptions {
source_map_path: codegen_options
.enable_sourcemap
.unwrap_or_default()
.then(|| path.clone()),
..CodegenOptions::default()
})
.build(&ret.program);
self.codegen_text = codegen_result.code;
self.codegen_sourcemap_text =
codegen_result.map.map(|map| map.to_json_string());
} else {
self.save_diagnostics(ret.errors.into_iter().collect::<Vec<_>>());
self.codegen_text = String::new();
self.codegen_sourcemap_text = None;
}
return Ok(());
}
Expand Down Expand Up @@ -301,14 +316,19 @@ impl Oxc {
None
};

self.codegen_text = CodeGenerator::new()
let codegen_result = CodeGenerator::new()
.with_symbol_table(symbol_table)
.with_options(CodegenOptions {
minify: minifier_options.whitespace.unwrap_or_default(),
source_map_path: codegen_options
.enable_sourcemap
.unwrap_or_default()
.then(|| path.clone()),
..CodegenOptions::default()
})
.build(&program)
.code;
.build(&program);
self.codegen_text = codegen_result.code;
self.codegen_sourcemap_text = codegen_result.map.map(|map| map.to_json_string());
self.ir = format!("{:#?}", program.body);
self.convert_ast(&mut program);

Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_wasm/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub struct OxcCodegenOptions {
pub indentation: Option<u8>,
#[tsify(optional)]
pub enable_typescript: Option<bool>,
#[tsify(optional)]
pub enable_sourcemap: Option<bool>,
}

#[derive(Debug, Default, Clone, Deserialize, Tsify)]
Expand Down
83 changes: 43 additions & 40 deletions npm/oxc-wasm/oxc_wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
/* tslint:disable */
/* eslint-disable */
export function browserslist(query: string, opts: any): any;

import type { Program, Span } from "@oxc-project/types";
export * from "@oxc-project/types";


export interface Oxc {
ast: Program;
astJson: string;
ir: string;
controlFlowGraph: string;
symbols: any;
scopeText: string;
codegenText: string;
codegenSourcemapText: string | null;
formattedText: string;
prettierFormattedText: string;
prettierIrText: string;
}

export interface Comment {
type: CommentType;
value: string;
start: number;
end: number;
}

export type CommentType = "Line" | "Block";

export interface OxcOptions {
run?: OxcRunOptions;
parser?: OxcParserOptions;
Expand Down Expand Up @@ -40,6 +68,7 @@ export interface OxcTransformerOptions {
export interface OxcCodegenOptions {
indentation?: number;
enableTypescript?: boolean;
enableSourcemap?: boolean;
}

export interface OxcControlFlowOptions {
Expand All @@ -64,43 +93,6 @@ export interface OxcCompressOptions {
}


import type { Program, Span } from "@oxc-project/types";
export * from "@oxc-project/types";


export interface Oxc {
ast: Program;
astJson: string;
ir: string;
controlFlowGraph: string;
symbols: any;
scopeText: string;
codegenText: string;
formattedText: string;
prettierFormattedText: string;
prettierIrText: string;
}

export interface Comment {
type: CommentType;
value: string;
start: number;
end: number;
}

export type CommentType = "Line" | "Block";


export type NodeId = number;
export type NodeFlags = {
JSDoc: 1,
Class: 2,
HasYield: 4
Parameter: 8
};



export type ReferenceId = number;
export type ReferenceFlags = {
None: 0,
Expand All @@ -112,13 +104,23 @@ export type ReferenceFlags = {



export type SymbolId = number;
export type SymbolFlags = unknown;
export type RedeclarationId = unknown;



export type ScopeId = number;



export type SymbolId = number;
export type SymbolFlags = unknown;
export type RedeclarationId = unknown;
export type NodeId = number;
export type NodeFlags = {
JSDoc: 1,
Class: 2,
HasYield: 4
Parameter: 8
};


export class Oxc {
Expand Down Expand Up @@ -153,6 +155,7 @@ export interface InitOutput {
readonly __wbg_get_oxc_symbols: (a: number) => any;
readonly __wbg_get_oxc_scopeText: (a: number) => [number, number];
readonly __wbg_get_oxc_codegenText: (a: number) => [number, number];
readonly __wbg_get_oxc_codegenSourcemapText: (a: number) => [number, number];
readonly __wbg_get_oxc_formattedText: (a: number) => [number, number];
readonly __wbg_get_oxc_prettierFormattedText: (a: number) => [number, number];
readonly __wbg_get_oxc_prettierIrText: (a: number) => [number, number];
Expand Down
Loading

0 comments on commit d216bb8

Please sign in to comment.