From e698418d1a0b91cdf449be827cd3a90e47dcf112 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Mon, 9 Sep 2024 15:55:23 +0000 Subject: [PATCH] feat(napi/transform): align output `SourceMap` with Rollup's `ExistingRawSourceMap` (#5657) closes #5578 --- napi/transform/index.d.ts | 4 ++- napi/transform/src/lib.rs | 29 +++------------------ napi/transform/src/sourcemap.rs | 46 +++++++++++++++++++++++++++++++++ napi/transform/test.mjs | 32 ++++++++++++++++------- 4 files changed, 74 insertions(+), 37 deletions(-) create mode 100644 napi/transform/src/sourcemap.rs diff --git a/napi/transform/index.d.ts b/napi/transform/index.d.ts index c7081c9d3f350..2c63cef777134 100644 --- a/napi/transform/index.d.ts +++ b/napi/transform/index.d.ts @@ -136,10 +136,12 @@ export interface ReactRefreshBindingOptions { export interface SourceMap { file?: string mappings?: string + names?: Array sourceRoot?: string sources?: Array sourcesContent?: Array - names?: Array + version: number + x_google_ignoreList?: Array } /** diff --git a/napi/transform/src/lib.rs b/napi/transform/src/lib.rs index b6a6e87b8639d..d75efc8ea5b8b 100644 --- a/napi/transform/src/lib.rs +++ b/napi/transform/src/lib.rs @@ -5,36 +5,13 @@ mod context; mod options; -use napi_derive::napi; - -#[napi(object)] -pub struct SourceMap { - pub file: Option, - pub mappings: Option, - pub source_root: Option, - pub sources: Option>>, - pub sources_content: Option>>, - pub names: Option>, -} - pub use crate::options::*; +mod sourcemap; +pub use crate::sourcemap::*; + mod isolated_declaration; pub use isolated_declaration::*; mod transformer; pub use transformer::*; - -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, - source_root: json.source_root, - sources: json.sources, - sources_content: json.sources_content, - names: json.names, - } - } -} diff --git a/napi/transform/src/sourcemap.rs b/napi/transform/src/sourcemap.rs new file mode 100644 index 0000000000000..a9d3db830c1ad --- /dev/null +++ b/napi/transform/src/sourcemap.rs @@ -0,0 +1,46 @@ +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: Option, + pub names: Option>, + pub source_root: Option, + pub sources: Option>>, + 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, + version: 3, + x_google_ignorelist: None, + } + } +} diff --git a/napi/transform/test.mjs b/napi/transform/test.mjs index ab127de27f7a6..e8484f12fc494 100644 --- a/napi/transform/test.mjs +++ b/napi/transform/test.mjs @@ -3,16 +3,6 @@ import oxc from './index.js'; console.log(`Testing on ${process.platform}-${process.arch}`); -test(oxc.isolatedDeclaration('test.ts', 'class A {}', { sourcemap: true }), { - code: 'declare class A {}\n', - map: { - mappings: 'AAAA,cAAM,EAAE,CAAE', - names: [], - sources: ['test.ts'], - sourcesContent: ['class A {}'], - }, -}); - function test(ret, expected) { console.log(ret.code); console.log(ret.map); @@ -23,3 +13,25 @@ function test(ret, expected) { assert.deepEqual(ret.map, expected.map); assert(ret.errors.length == 0); } + +test(oxc.isolatedDeclaration('test.ts', 'class A {}', { sourcemap: true }), { + code: 'declare class A {}\n', + map: { + mappings: 'AAAA,cAAM,EAAE,CAAE', + names: [], + sources: ['test.ts'], + sourcesContent: ['class A {}'], + version: 3, + }, +}); + +test(oxc.transform('test.ts', 'class A {}', { sourcemap: true }), { + code: 'class A {}\n', + map: { + mappings: 'AAAA,MAAM,EAAK,CAAE', + names: [], + sources: ['test.ts'], + sourcesContent: ['class A {}'], + version: 3, + }, +});