diff --git a/Cargo.lock b/Cargo.lock index 51118298c9c16..efac304fc7e68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1945,13 +1945,15 @@ dependencies = [ [[package]] name = "oxc_sourcemap" -version = "1.0.0" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05e75cb20b3bb96af20d320f71d67c9f20b5bedb253cae36881fedef51f7688f" +checksum = "f15cc100c3a8070aa20420a6e1eafa612ebfc3d86db01cb92d928abf082d9696" dependencies = [ "base64-simd", "cfg-if", "cow-utils", + "napi", + "napi-derive", "rustc-hash", "serde", "serde_json", @@ -2037,6 +2039,7 @@ dependencies = [ "napi-derive", "oxc", "oxc_napi", + "oxc_sourcemap", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c0deb11531b74..c3c248e1a4da9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -122,7 +122,7 @@ unicode-id-start = "1" oxc-browserslist = "1.1.0" oxc_index = "1.0.0" oxc_resolver = "2.0.0" -oxc_sourcemap = "1" +oxc_sourcemap = "1.0.2" aho-corasick = "1.1.3" allocator-api2 = "0.2.18" diff --git a/crates/oxc_napi/Cargo.toml b/crates/oxc_napi/Cargo.toml index 08d21000bcf6f..43ed04239cc04 100644 --- a/crates/oxc_napi/Cargo.toml +++ b/crates/oxc_napi/Cargo.toml @@ -22,7 +22,7 @@ doctest = false [dependencies] oxc_isolated_declarations = { workspace = true } -oxc_sourcemap = { workspace = true } +oxc_sourcemap = { workspace = true, features = ["napi"] } oxc_span = { workspace = true } oxc_syntax = { workspace = true } oxc_transformer = { workspace = true } diff --git a/crates/oxc_napi/src/isolated_declarations.rs b/crates/oxc_napi/src/isolated_declarations.rs index 7e57b1c1c928f..11a4a0dbb0e06 100644 --- a/crates/oxc_napi/src/isolated_declarations.rs +++ b/crates/oxc_napi/src/isolated_declarations.rs @@ -1,6 +1,6 @@ use napi_derive::napi; -use super::source_map::SourceMap; +use oxc_sourcemap::napi::SourceMap; #[napi(object)] pub struct IsolatedDeclarationsResult { diff --git a/crates/oxc_napi/src/lib.rs b/crates/oxc_napi/src/lib.rs index 0b5958cce77bc..c341153196592 100644 --- a/crates/oxc_napi/src/lib.rs +++ b/crates/oxc_napi/src/lib.rs @@ -1,7 +1,5 @@ pub mod parse; -pub mod source_map; - pub mod isolated_declarations; pub mod transform; diff --git a/crates/oxc_napi/src/source_map.rs b/crates/oxc_napi/src/source_map.rs deleted file mode 100644 index 0e7cb4d83c2db..0000000000000 --- a/crates/oxc_napi/src/source_map.rs +++ /dev/null @@ -1,48 +0,0 @@ -use napi_derive::napi; - -// Aligned with Rollup's sourcemap input. -// -// -// -// ``` -// export interface ExistingRawSourceMap { -// file?: string; -// mappings: string; -// names: string[]; -// sourceRoot?: string; -// sources: string[]; -// sourcesContent?: string[]; -// version: number; -// x_google_ignoreList?: number[]; -// } -// ``` -#[napi(object)] -pub struct SourceMap { - pub file: Option, - pub mappings: String, - pub names: Vec, - pub source_root: Option, - pub sources: Vec, - pub sources_content: Option>, - pub version: u8, - #[napi(js_name = "x_google_ignoreList")] - pub x_google_ignorelist: Option>, -} - -impl From for SourceMap { - fn from(source_map: oxc_sourcemap::SourceMap) -> Self { - let json = source_map.to_json(); - Self { - file: json.file, - mappings: json.mappings, - names: json.names, - source_root: json.source_root, - sources: json.sources, - sources_content: json.sources_content.map(|content| { - content.into_iter().map(Option::unwrap_or_default).collect::>() - }), - version: 3, - x_google_ignorelist: None, - } - } -} diff --git a/crates/oxc_napi/src/transform.rs b/crates/oxc_napi/src/transform.rs index e247eb9e42c55..6586a6cd51f6e 100644 --- a/crates/oxc_napi/src/transform.rs +++ b/crates/oxc_napi/src/transform.rs @@ -8,9 +8,10 @@ use napi::Either; use napi_derive::napi; use rustc_hash::FxHashMap; +use oxc_sourcemap::napi::SourceMap; use oxc_transformer::{EnvOptions, JsxRuntime, RewriteExtensionsMode}; -use super::{isolated_declarations::IsolatedDeclarationsOptions, source_map::SourceMap}; +use super::isolated_declarations::IsolatedDeclarationsOptions; #[derive(Default)] #[napi(object)] diff --git a/napi/transform/Cargo.toml b/napi/transform/Cargo.toml index d7e414d758acc..a525b17d61e4c 100644 --- a/napi/transform/Cargo.toml +++ b/napi/transform/Cargo.toml @@ -23,6 +23,7 @@ doctest = false [dependencies] oxc = { workspace = true, features = ["full", "napi"] } oxc_napi = { workspace = true } +oxc_sourcemap = { workspace = true, features = ["napi"] } napi = { workspace = true } napi-derive = { workspace = true } diff --git a/napi/transform/src/isolated_declaration.rs b/napi/transform/src/isolated_declaration.rs index 5983cf0cc84c7..babd87f31703e 100644 --- a/napi/transform/src/isolated_declaration.rs +++ b/napi/transform/src/isolated_declaration.rs @@ -9,10 +9,8 @@ use oxc::{ parser::Parser, span::SourceType, }; -use oxc_napi::{ - isolated_declarations::{IsolatedDeclarationsOptions, IsolatedDeclarationsResult}, - source_map::SourceMap, -}; +use oxc_napi::isolated_declarations::{IsolatedDeclarationsOptions, IsolatedDeclarationsResult}; +use oxc_sourcemap::napi::SourceMap; use crate::errors::wrap_diagnostics; diff --git a/napi/transform/src/transformer.rs b/napi/transform/src/transformer.rs index 54c52748340b1..ce5e5854f3566 100644 --- a/napi/transform/src/transformer.rs +++ b/napi/transform/src/transformer.rs @@ -11,10 +11,8 @@ use oxc::{ transformer::{InjectGlobalVariablesConfig, InjectImport, ReplaceGlobalDefinesConfig}, CompilerInterface, }; -use oxc_napi::{ - source_map::SourceMap, - transform::{TransformOptions, TransformResult}, -}; +use oxc_napi::transform::{TransformOptions, TransformResult}; +use oxc_sourcemap::napi::SourceMap; use crate::errors::wrap_diagnostics; diff --git a/npm/oxc-parser/README.md b/npm/oxc-parser/README.md index 02d7a9a172566..afa0bda2ee1ab 100644 --- a/npm/oxc-parser/README.md +++ b/npm/oxc-parser/README.md @@ -8,7 +8,7 @@ import oxc from 'oxc-parser'; const sourceText = "let foo: Foo = 'foo';"; // Filename extension is used to determine which dialect to parse source as. -const filename = "test.tsx"; +const filename = 'test.tsx'; test(oxc.parseSync(filename, sourceText, options)); test(await oxc.parseAsync(filename, sourceText, options));