use std::{ fmt::{self, Write}, sync::Arc, }; use parcel_core::*; /// Accumulates the bundle output, tracking the current line/column so that each /// module's source map can be recorded as a section of an index map. The output is a /// `ChunkedText`, so modules' code is referenced or moved into it rather than copied. pub(super) struct Printer { output: ChunkedText, line: u32, column: u32, source_map_sections: Option>, /// Whether the last section belongs to code that `end_source_map` hasn't ended yet. section_open: bool, should_optimize: bool, } /// A source map generated by Parcel, as JSON. pub(crate) struct SectionMap { pub(crate) json: MapJson, /// The `sourceRoot` to add to a map generated without one (see `GeneratedCode`), or None to use /// the map as is. pub(crate) source_root: Option>, } pub(crate) enum MapJson { /// A map kept by an asset, e.g. one generated when it was transformed. Shared(Arc), Owned(String), Static(&'static str), } impl MapJson { fn as_str(&self) -> &str { match self { MapJson::Shared(json) => json, MapJson::Owned(json) => json, MapJson::Static(json) => json, } } } impl SectionMap { pub(crate) fn new(json: String) -> Self { SectionMap { json: MapJson::Owned(json), source_root: None, } } } impl Printer { pub(super) fn new(source_maps: bool, should_optimize: bool) -> Self { Printer { output: ChunkedText::new(), line: 0, column: 0, source_map_sections: source_maps.then(Vec::new), section_open: false, should_optimize, } } /// Starts a section of the bundle's map with `map`, at the current position. pub(super) fn add_source_map(&mut self, map: Option) { if let Some(source_map_sections) = &mut self.source_map_sections && let Some(map) = map { source_map_sections.push(SourceMapSection { line: self.line, column: self.column, map, }); self.section_open = true; } } /// Ends the section started by the last `add_source_map`, so that the code written after it /// (e.g. the runtime) isn't mapped to the last position in the module. A section extends to /// the start of the next one, so this starts an empty one. pub(super) fn end_source_map(&mut self) { if let Some(source_map_sections) = &mut self.source_map_sections && std::mem::take(&mut self.section_open) { source_map_sections.push(SourceMapSection { line: self.line, column: self.column, map: SectionMap { json: MapJson::Static(EMPTY_SOURCE_MAP), source_root: None, }, }); } } /// Writes `text[range]`, referencing `text` rather than copying it. pub(super) fn write_shared(&mut self, text: &Arc, range: std::ops::Range) { if self.source_map_sections.is_some() { update_position(&text[range.clone()], &mut self.line, &mut self.column); } self.output.push_shared(text, range); } /// Writes `text`, moving it rather than copying it. pub(super) fn write_owned(&mut self, text: String) { if self.source_map_sections.is_some() { update_position(&text, &mut self.line, &mut self.column); } self.output.push_owned(text); } #[inline] pub(super) fn write_module_header(&mut self, id: String) -> std::fmt::Result { if self.should_optimize { write!(self, "'{}':", id) } else { writeln!(self, "'{}':[function(module,exports,require) {{", id) } } #[inline] pub(super) fn write_module_trailer(&mut self, deps: String) -> std::fmt::Result { write!(self, "\n}}, {}]", deps) } /// Writes a module's code as an expression, without its final semicolon. pub(super) fn write_expression_code(&mut self, mut code: Vec) -> Result<(), DiagnosticList> { let mut end = code.len(); while end > 0 && code[end - 1].is_ascii_whitespace() { end -= 1; } if end > 0 && code[end - 1] == b';' { code.remove(end - 1); } self.write_owned(String::from_utf8(code).map_err(|e| Diagnostic::from_message(e.to_string()))?); Ok(()) } #[inline] pub(super) fn newline(&mut self) -> std::fmt::Result { if !self.should_optimize { writeln!(self) } else { Ok(()) } } #[inline] pub(super) fn write_var(&mut self, name: &str, value: &str, semi: bool) -> std::fmt::Result { if self.should_optimize { write!(self, "var {name}={value}")?; } else { write!(self, "var {name} = {value}")?; } if semi { self.write_char(';')?; } self.newline() } pub(super) fn into_content(self) -> Result, DiagnosticList> { let map = self .source_map_sections .filter(|sections| !sections.is_empty()) .map(source_map_index); Ok(Arc::new(ChunkedTextContent::new(self.output, map))) } } impl std::fmt::Write for Printer { fn write_str(&mut self, s: &str) -> fmt::Result { if self.source_map_sections.is_some() { update_position(s, &mut self.line, &mut self.column); } self.output.push_str(s); Ok(()) } } fn update_position(s: &str, line: &mut u32, column: &mut u32) { let newlines = bytecount::count(s.as_bytes(), b'\n'); if newlines > 0 { *line += newlines as u32; if let Some((_, last)) = s.rsplit_once('\n') { *column = simd_utf16_len::utf16_len(last) as u32; } } else { *column += simd_utf16_len::utf16_len(s) as u32; } } pub(crate) const EMPTY_SOURCE_MAP: &str = r#"{"version":3,"sources":[],"names":[],"mappings":""}"#; /// A section of an index map: a map that applies from a position in the bundle. pub(crate) struct SourceMapSection { pub(crate) line: u32, pub(crate) column: u32, pub(crate) map: SectionMap, } /// Serializes an index map of `sections`, referencing their maps rather than copying them. The /// maps are JSON that Parcel generated, so they're written as is rather than validated. /// /// The top-level `sources` isn't part of an index map, but Node ignores index maps without it (it /// resolves the top-level `sources` when loading a map), and then applies the sections. Node /// doesn't resolve the sections' `sources` though, so its stack traces show them relative to the /// project root. See https://github.com/nodejs/node/issues/66657 pub(crate) fn source_map_index(sections: Vec) -> ChunkedText { let mut out = ChunkedText::new(); out.push_str(r#"{"version":3,"sources":[],"sections":["#); let mut header = String::new(); for (index, section) in sections.into_iter().enumerate() { header.clear(); if index > 0 { header.push(','); } write!( header, r#"{{"offset":{{"line":{},"column":{}}},"map":"#, section.line, section.column ) .unwrap(); out.push_str(&header); write_section_map(&mut out, section.map); out.push_str("}"); } out.push_str("]}"); out } /// Writes `map`, adding its `sourceRoot` if it has one to add. fn write_section_map(out: &mut ChunkedText, map: SectionMap) { let json = map.json.as_str(); let Some(source_root) = map.source_root.as_deref().filter(|_| json.starts_with('{')) else { match map.json { MapJson::Shared(json) => out.push_shared(&json, 0..json.len()), MapJson::Owned(json) => out.push_owned(json), MapJson::Static(json) => out.push_str(json), } return; }; out.push_str(r#"{"sourceRoot":"#); out.push_str(&serde_json::to_string(source_root).unwrap()); if !json[1..].trim_start().starts_with('}') { out.push_str(","); } match &map.json { MapJson::Shared(json) => out.push_shared(json, 1..json.len()), json => out.push_str(&json.as_str()[1..]), } } #[cfg(test)] mod tests { use super::*; fn col(s: &str) -> u32 { let mut line = 0; let mut column = 0; update_position(s, &mut line, &mut column); column } #[test] fn ascii_column_is_length() { assert_eq!(col("abc"), 3); } #[test] fn bmp_nonascii_counts_one_utf16_unit() { // 'é' is 2 UTF-8 bytes but 1 UTF-16 code unit. assert_eq!(col("é"), 1); } #[test] fn astral_counts_two_utf16_units() { // '𝐀' (U+1D400) is 4 UTF-8 bytes but 2 UTF-16 code units. assert_eq!(col("𝐀"), 2); } #[test] fn newline_resets_column() { // After a newline, column restarts; trailing 'é' adds 1. assert_eq!(col("abc\né"), 1); } #[test] fn index_map_adds_source_roots() { let sections = [ SourceMapSection { line: 0, column: 0, map: SectionMap { json: MapJson::Shared( r#"{"version":3,"sources":["a.js"],"names":[],"mappings":"AAAA"}"#.into(), ), source_root: Some("../".into()), }, }, SourceMapSection { line: 2, column: 1, map: SectionMap { json: MapJson::Static(EMPTY_SOURCE_MAP), source_root: None, }, }, ]; let index = source_map_index(sections.into()).to_string(); let value: serde_json::Value = serde_json::from_str(&index).unwrap(); assert_eq!(value["sections"][0]["map"]["sourceRoot"], "../"); assert_eq!(value["sections"][0]["map"]["sources"][0], "a.js"); assert_eq!(value["sections"][1]["offset"]["line"], 2); assert_eq!(value["sections"][1]["offset"]["column"], 1); assert!(value["sections"][1]["map"].get("sourceRoot").is_none()); } }