-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support generating source map (#607)
* feat: generate sourcemap * chore: fix spell * chore: fix test * chore: fix test
- Loading branch information
Showing
22 changed files
with
252 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,61 @@ | ||
use oxc::codegen::CodegenReturn; | ||
use rolldown_common::NormalModule; | ||
use rolldown_oxc_utils::{OxcCompiler, OxcProgram}; | ||
use rolldown_sourcemap::SourceMap; | ||
use rolldown_sourcemap::{SourceMap, SourceMapBuilder}; | ||
use string_wizard::MagicString; | ||
|
||
use crate::types::module_render_context::ModuleRenderContext; | ||
|
||
pub struct RenderedNormalModuleOutput { | ||
pub code: MagicString<'static>, | ||
pub map: Option<SourceMap>, | ||
pub sourcemap: Option<SourceMap>, | ||
} | ||
|
||
#[allow(clippy::unnecessary_wraps)] | ||
#[allow( | ||
clippy::unnecessary_wraps, | ||
clippy::cast_possible_truncation, | ||
clippy::needless_pass_by_value | ||
)] | ||
pub fn render_normal_module( | ||
module: &NormalModule, | ||
_ctx: &ModuleRenderContext<'_>, | ||
ast: &OxcProgram, | ||
enable_sourcemap: Option<String>, | ||
) -> Option<RenderedNormalModuleOutput> { | ||
if ast.program().body.is_empty() { | ||
None | ||
} else { | ||
let generated_code = OxcCompiler::print(ast); | ||
let mut source = MagicString::new(generated_code); | ||
let CodegenReturn { source_map, source_text } = | ||
OxcCompiler::print(ast, enable_sourcemap.clone()); | ||
|
||
let mut source = MagicString::new(source_text); | ||
source.prepend(format!("// {}\n", module.pretty_path)); | ||
// Here `MagicString` sourcemap is not valid, because it need to include valid ast token. | ||
|
||
Some(RenderedNormalModuleOutput { code: source, map: None }) | ||
Some(RenderedNormalModuleOutput { | ||
code: source, | ||
sourcemap: source_map.map(|source_map| { | ||
let mut sourcemap_builder = SourceMapBuilder::new(None); | ||
|
||
for (id, source) in source_map.sources().enumerate() { | ||
let source_id = sourcemap_builder.add_source(source); | ||
sourcemap_builder | ||
.set_source_contents(source_id, source_map.get_source_contents(id as u32)); | ||
} | ||
|
||
for token in source_map.tokens() { | ||
sourcemap_builder.add( | ||
token.get_dst_line() + 1, // line offset by prepend comment | ||
token.get_dst_col(), | ||
token.get_src_line(), | ||
token.get_src_col(), | ||
token.get_source(), | ||
token.get_name(), | ||
); | ||
} | ||
|
||
sourcemap_builder.into_sourcemap() | ||
}), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,4 +32,4 @@ | |
"devDependencies": { | ||
"wasm-pack": "^0.12.1" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.