Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ast/ast_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct Ast<'a> {
// them under the same lifetime-erased contract as `StoreRef`.
pub hashbang: StoreStr,
pub directive: Option<StoreStr>,
/// URL text and location of the file's last `sourceMappingURL` comment.
pub source_mapping_url: Option<crate::Span>,
/// `export default X` where `X` is an import binding in this file. When
/// `X` resolves to a module namespace the linker binds importers of
/// `default` through `X` (a namespace never changes identity, so the
Expand Down Expand Up @@ -125,6 +127,7 @@ impl<'a> Ast<'a> {
import_records: ImportRecordList::new_in(arena),
hashbang: StoreStr::EMPTY,
directive: None,
source_mapping_url: None,
export_default_alias_of_import: Ref::NONE,
parts: PartList::new_in(arena),
symbols: SymbolList::new_in(arena),
Expand Down
4 changes: 4 additions & 0 deletions src/bundler/Graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub struct InputFile {
pub unique_key_for_additional_file: Box<[u8], AstAlloc>,
pub content_hash_for_additional_file: u64,
pub flags: InputFileFlags,
/// From the file's `sourceMappingURL` comment. Global heap: drained in `deinit_without_freeing_arena`.
pub input_source_map: Option<Box<bun_sourcemap::InputSourceMap>>,
}

impl Default for InputFile {
Expand All @@ -125,6 +127,7 @@ impl Default for InputFile {
unique_key_for_additional_file: AstAlloc::vec().into_boxed_slice(),
content_hash_for_additional_file: 0,
flags: InputFileFlags::default(),
input_source_map: None,
}
}
}
Expand All @@ -142,6 +145,7 @@ bun_collections::multi_array_columns! {
unique_key_for_additional_file: Box<[u8], AstAlloc>,
content_hash_for_additional_file: u64,
flags: InputFileFlags,
input_source_map: Option<Box<bun_sourcemap::InputSourceMap>>,
}
}

Expand Down
104 changes: 51 additions & 53 deletions src/bundler/LinkerContext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,7 @@ impl<'a> LinkerContext<'a> {
let mut j = StringJoiner::default();

let sources = self.parse_graph().input_files.items_source();
let input_source_maps = self.parse_graph().input_files.items_input_source_map();
let quoted_source_map_contents = self.graph.files.items_quoted_source_contents();

// Entries in `results` do not 1:1 map to source files, the mapping
Expand All @@ -1187,80 +1188,68 @@ impl<'a> LinkerContext<'a> {
// -->
// Which source index in the generated sourcemap, referred to
// as the "mapping source index" within this function to be distinct.
// A file with an input source map takes one slot per source of that map.
let mut source_id_map: ArrayHashMap<u32, i32> = ArrayHashMap::new();

let source_indices = results.items_source_index();

j.push_static(b"{\n \"version\": 3,\n \"sources\": [");
if !source_indices.is_empty() {
{
let index = source_indices[0];
let path = &sources[index as usize].path;
source_id_map.put_no_clobber(index, 0)?;
let mut next_mapping_source_index: i32 = 0;
for &index in source_indices {
let gop = source_id_map.get_or_put(index)?;
if gop.found_existing {
continue;
}
*gop.value_ptr = next_mapping_source_index;

// Note: the relative path lives in a local owned buffer
// (drops at scope exit).
let mut push_source = |j: &mut StringJoiner, abs_path_or_name: &[u8], is_file: bool| {
let rel_path_storage;
let pretty: &[u8] = if path.is_file() {
rel_path_storage = Self::source_map_relative_path(chunk_abs_dir, path.text)?;
let name: &[u8] = if is_file {
rel_path_storage =
Self::source_map_relative_path(chunk_abs_dir, abs_path_or_name)?;
&rel_path_storage
} else {
path.pretty
abs_path_or_name
};

let mut quote_buf = MutableString::init(pretty.len() + 2)?;
js_printer::quote_for_json(pretty, &mut quote_buf, false)?;
let mut quote_buf = MutableString::init(name.len() + ", ".len() + 2)?;
if next_mapping_source_index > 0 {
quote_buf.append_assume_capacity(b", ");
}
js_printer::quote_for_json(name, &mut quote_buf, false)?;
// `to_default_owned` moves the buffer into the joiner
// (joiner owns it until `done`).
j.push_owned(quote_buf.to_default_owned());
}

let mut next_mapping_source_index: i32 = 1;
for &index in &source_indices[1..] {
let gop = source_id_map.get_or_put(index)?;
if gop.found_existing {
continue;
}

*gop.value_ptr = next_mapping_source_index;
next_mapping_source_index += 1;
Ok::<(), BunError>(())
};

let path = &sources[index as usize].path;

let rel_path_storage;
let pretty: &[u8] = if path.is_file() {
rel_path_storage = Self::source_map_relative_path(chunk_abs_dir, path.text)?;
&rel_path_storage
} else {
path.pretty
};

let mut quote_buf = MutableString::init(pretty.len() + ", ".len() + 2)?;
quote_buf.append_assume_capacity(b", ");
js_printer::quote_for_json(pretty, &mut quote_buf, false)?;
j.push_owned(quote_buf.to_default_owned());
match input_source_maps[index as usize].as_deref() {
None => {
let path = &sources[index as usize].path;
if path.is_file() {
push_source(&mut j, path.text, true)?;
} else {
push_source(&mut j, path.pretty, false)?;
}
}
Some(input_source_map) => {
for source in input_source_map.sources.iter() {
push_source(&mut j, &source.path, source.is_file)?;
}
}
}
}

j.push_static(b"],\n \"sourcesContent\": [");

let source_indices_for_contents = source_id_map.keys();
if !source_indices_for_contents.is_empty() {
j.push_static(b"\n ");
j.push_static(
quoted_source_map_contents[source_indices_for_contents[0] as usize]
for (i, &index) in source_id_map.keys().iter().enumerate() {
j.push_static(if i == 0 { b"\n " } else { b",\n " });
j.push_static(match input_source_maps[index as usize].as_deref() {
None => quoted_source_map_contents[index as usize]
.as_deref()
.unwrap_or(b""),
);

for &index in &source_indices_for_contents[1..] {
j.push_static(b",\n ");
j.push_static(
quoted_source_map_contents[index as usize]
.as_deref()
.unwrap_or(b""),
);
}
Some(input_source_map) => &input_source_map.quoted_sources_content,
});
}
j.push_static(b"\n ],\n \"mappings\": \"");

Expand Down Expand Up @@ -1299,7 +1288,8 @@ impl<'a> LinkerContext<'a> {
)?;

prev_end_state = chunk.end_state;
prev_end_state.source_index = mapping_source_index;
// Chunk-local source indices count from 0: rebase onto the file's first slot.
prev_end_state.source_index += mapping_source_index;
prev_column_offset = chunk.final_generated_column;

if prev_end_state.generated_line == 0 {
Expand Down Expand Up @@ -1679,6 +1669,10 @@ impl SourceMapData {
if !loader.can_have_source_map() {
return;
}
// The output map carries the input map's `sourcesContent` instead.
if parse_graph.input_files.items_input_source_map()[source_index as usize].is_some() {
return;
}

let source: &Source = &parse_graph.input_files.items_source()[source_index as usize];
// Allocate from the worker's AST allocation state (installed by
Expand Down Expand Up @@ -2360,6 +2354,9 @@ impl<'a> LinkerContext<'a> {
// SAFETY: `self.mangled_props` is not mutated during printing; detached borrow
// outlives only this call (see above).
unsafe { bun_ptr::detach_lifetime_ref(&self.mangled_props) };
let input_source_map: Option<&bun_sourcemap::InputSourceMap> =
parse_graph.input_files.items_input_source_map()[source_index.get() as usize]
.as_deref();

let print_options = js_printer::Options {
bundling: true,
Expand Down Expand Up @@ -2406,6 +2403,7 @@ impl<'a> LinkerContext<'a> {
require_or_import_meta_for_source_callback:
js_printer::RequireOrImportMetaCallback::init(self),
line_offset_tables: Some(line_offset_table),
input_source_map,
target: self.options.target,

hmr_ref: if self.options.output_format == Format::InternalBakeDev {
Expand Down
23 changes: 22 additions & 1 deletion src/bundler/ParseTask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ pub(crate) struct Success {

/// The package name from package.json, used for barrel optimization.
pub(crate) package_name: ast::StoreStr,

/// See `InputFile::input_source_map`, where this moves on completion.
pub(crate) input_source_map: Option<Box<bun_sourcemap::InputSourceMap>>,
}

pub(crate) struct ResultError {
Expand Down Expand Up @@ -803,6 +806,7 @@ pub mod parse_worker {
unique_key_prefix: u64,
unique_key_for_additional_file: &mut FileLoaderHash,
has_any_css_locals: &AtomicU32,
source_mapping_url: &mut Option<bun_ast::Span>,
) -> core::result::Result<JSAst<'static>, AnyError> {
use core::fmt::Write as _;

Expand All @@ -828,7 +832,10 @@ pub mod parse_worker {
// `Cached`/`AlreadyBundled` are runtime-loader
// states that never reach the bundler's `getAST`, so unwrap.
match res {
bun_js_parser::Result::Ast(ast) => Ok(JSAst::init(*ast)),
bun_js_parser::Result::Ast(ast) => {
*source_mapping_url = ast.source_mapping_url;
Ok(JSAst::init(*ast))
}
bun_js_parser::Result::Cached
| bun_js_parser::Result::AlreadyBundled(_) => {
unreachable!("bundler parse never yields Cached/AlreadyBundled")
Expand Down Expand Up @@ -2663,9 +2670,13 @@ pub mod parse_worker {
// SAFETY: task.ctx backref valid for the bundle pass (outlives `'r`).
let task_ctx = unsafe { task.ctx() };
let module_type = opts.module_type;
// The dev server's `SourceMapStore` assumes one `sources` entry per file: no input maps there.
let use_input_source_maps =
topts.source_map != options::SourceMapOption::None && !topts.has_dev_server();
// `topts` (a `&BundleOptions`) is dead past this point; the callees take
// raw `*mut Transpiler` and reborrow `(*transpiler).options` mutably.
let _ = topts;
let mut source_mapping_url: Option<bun_ast::Span> = None;
let ast_result: core::result::Result<JSAst, AnyError> =
if !is_empty || loader.handles_empty_file() {
get_ast(
Expand All @@ -2679,6 +2690,7 @@ pub mod parse_worker {
task_ctx.unique_key,
&mut unique_key_for_additional_file,
&task_ctx.linker.has_any_css_locals,
&mut source_mapping_url,
)
} else if loader.is_css() {
get_empty_css_ast(log, transpiler, opts, bump, source)
Expand Down Expand Up @@ -2724,6 +2736,13 @@ pub mod parse_worker {

*step = Step::Resolve;

let input_source_map = match source_mapping_url {
Some(comment) if use_input_source_maps => {
crate::input_source_map::load(log, source, comment)
}
_ => None,
};

Ok(Success {
ast,
source: source.clone(),
Expand All @@ -2736,6 +2755,8 @@ pub mod parse_worker {

// Hash the files in here so that we do it in parallel.
content_hash_for_additional_file: unique_key_for_additional_file.content_hash,

input_source_map,
})
}

Expand Down
1 change: 1 addition & 0 deletions src/bundler/ServerComponentParseTask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ fn task_callback(
unique_key_for_additional_file: bun_ast::StoreStr::EMPTY,
content_hash_for_additional_file: 0,
package_name: bun_ast::StoreStr::EMPTY,
input_source_map: None,
})
}

Expand Down
6 changes: 6 additions & 0 deletions src/bundler/bundle_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5190,6 +5190,10 @@ pub mod bv2_impl {
// macro takes only one side (`linker.graph.ast` is a bitwise SoA
// `memcpy` of `graph.ast`), and `CssChunk::asts` `forget()`s its
// aliases, so this is the unique drop.
// `InputFile.input_source_map` is likewise global-heap, not arena.
for input_source_map in self.graph.input_files.items_input_source_map_mut() {
drop(input_source_map.take());
}
{
macro_rules! take_ast_cols {
($ast:expr) => {{
Expand Down Expand Up @@ -7356,6 +7360,8 @@ pub mod bv2_impl {

// Record which loader we used for this file
this.graph.input_files.items_loader_mut()[result_source_index] = result.loader;
this.graph.input_files.items_input_source_map_mut()[result_source_index] =
result.input_source_map.take();

bun_core::scoped_log!(
Bundle,
Expand Down
Loading
Loading