Skip to content

Commit

Permalink
feat: add OutputChunk map and sourcemapFileName (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Mar 21, 2024
1 parent 70b6cfd commit 3135778
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 3 deletions.
7 changes: 5 additions & 2 deletions crates/rolldown/src/stages/bundle_stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl<'a> BundleStage<'a> {
let mut assets = vec![];

render_chunks(self.plugin_driver, chunks).await?.into_iter().try_for_each(
|(mut content, map, rendered_chunk)| -> Result<(), BuildError> {
if let Some(mut map) = map {
|(mut content, mut map, rendered_chunk)| -> Result<(), BuildError> {
if let Some(map) = map.as_mut() {
map.set_file(Some(rendered_chunk.file_name.clone()));
match self.output_options.sourcemap {
SourceMapType::File => {
Expand All @@ -117,6 +117,7 @@ impl<'a> BundleStage<'a> {
SourceMapType::Hidden => {}
}
}
let sourcemap_file_name = map.as_ref().map(|_| format!("{}.map", rendered_chunk.file_name));
assets.push(Output::Chunk(Box::new(OutputChunk {
file_name: rendered_chunk.file_name,
code: content,
Expand All @@ -126,6 +127,8 @@ impl<'a> BundleStage<'a> {
modules: rendered_chunk.modules,
exports: rendered_chunk.exports,
module_ids: rendered_chunk.module_ids,
map,
sourcemap_file_name,
})));
Ok(())
},
Expand Down
8 changes: 8 additions & 0 deletions crates/rolldown_binding/src/types/binding_output_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct BindingOutputChunk {
pub modules: HashMap<String, BindingRenderedModule>,
// OutputChunk
pub code: String,
pub map: Option<String>,
pub sourcemap_file_name: Option<String>,
}

impl From<Box<rolldown_common::OutputChunk>> for BindingOutputChunk {
Expand All @@ -35,6 +37,12 @@ impl From<Box<rolldown_common::OutputChunk>> for BindingOutputChunk {
modules: chunk.modules.into_iter().map(|(key, value)| (key, value.into())).collect(),
exports: chunk.exports,
module_ids: chunk.module_ids,
map: chunk.map.map(|map| {
let mut buf = vec![];
map.to_writer(&mut buf).unwrap();
unsafe { String::from_utf8_unchecked(buf) }
}),
sourcemap_file_name: chunk.sourcemap_file_name,
}
}
}
3 changes: 3 additions & 0 deletions crates/rolldown_common/src/types/output_chunk.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rolldown_sourcemap::SourceMap;
use rustc_hash::FxHashMap;

use super::rendered_module::RenderedModule;
Expand All @@ -16,4 +17,6 @@ pub struct OutputChunk {
pub modules: FxHashMap<String, RenderedModule>,
// OutputChunk
pub code: String,
pub map: Option<SourceMap>,
pub sourcemap_file_name: Option<String>,
}
2 changes: 2 additions & 0 deletions packages/rolldown/src/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export interface BindingOutputChunk {
fileName: string
modules: Record<string, BindingRenderedModule>
code: string
map?: string
sourcemapFileName?: string
}

export interface BindingOutputOptions {
Expand Down
15 changes: 14 additions & 1 deletion packages/rolldown/src/objects/rolldown-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ function _assertRolldownOutputAsset() {
type _ = TypeAssert<IsPropertiesEqual<RolldownOutputAsset, OutputAsset>>
}

export interface SourceMap {
file: string
mappings: string
names: string[]
sources: string[]
sourcesContent: (string | null)[]
version: number
// toString(): string
// toUrl(): string
}

export interface RolldownOutputChunk {
type: 'chunk'
code: string
Expand All @@ -29,11 +40,13 @@ export interface RolldownOutputChunk {
facadeModuleId: string | null
isDynamicEntry: boolean
moduleIds: string[]
map: SourceMap | null
sourcemapFileName: string | null
}

function _assertRolldownOutputChunk() {
type _ = TypeAssert<
IsPropertiesEqual<Omit<RolldownOutputChunk, 'modules'>, OutputChunk>
IsPropertiesEqual<Omit<RolldownOutputChunk, 'modules' | 'map'>, OutputChunk>
>
}

Expand Down
2 changes: 2 additions & 0 deletions packages/rolldown/src/utils/transform-to-rollup-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ function transformToRollupOutputChunk(
facadeModuleId: chunk.facadeModuleId || null,
isDynamicEntry: chunk.isDynamicEntry,
moduleIds: chunk.moduleIds,
map: chunk.map ? JSON.parse(chunk.map) : null,
sourcemapFileName: chunk.sourcemapFileName || null,
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/rolldown/test/cases/sourcemap/inner/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default {
`)
// include map comment
expect(output.output[0].code).contains('//# sourceMappingURL=main.js.map')
expect(output.output[0].sourcemapFileName).toBe('main.js.map')
expect(output.output[0].map).toBeDefined()
// @ts-expect-error
const map = JSON.parse(output.output[1].source)
expect(map.file).toMatchInlineSnapshot(`"main.js"`)
Expand Down

0 comments on commit 3135778

Please sign in to comment.