Skip to content

Commit

Permalink
fix invalid source maps on HMR
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Nov 5, 2024
1 parent aabb04c commit 2496107
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
16 changes: 16 additions & 0 deletions turbopack/crates/turbo-tasks-fs/src/rope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,22 @@ pub mod ser_as_string {
}
}

pub mod ser_option_as_string {
use serde::{ser::Error, Serializer};

use super::Rope;

/// Serializes a Rope into a string.
pub fn serialize<S: Serializer>(rope: &Option<Rope>, serializer: S) -> Result<S::Ok, S::Error> {
if let Some(rope) = rope {
let s = rope.to_str().map_err(Error::custom)?;
serializer.serialize_some(&s)
} else {
serializer.serialize_none()
}
}
}

impl PartialEq for Rope {
// Ropes with similar contents are equals, regardless of their structure.
fn eq(&self, other: &Self) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ struct EcmascriptModuleEntry {
#[serde(with = "turbo_tasks_fs::rope::ser_as_string")]
code: Rope,
url: String,
map: Option<String>,
#[serde(with = "turbo_tasks_fs::rope::ser_option_as_string")]
map: Option<Rope>,
}

impl EcmascriptModuleEntry {
async fn from_code(id: &ModuleId, code: Vc<Code>, chunk_path: &str) -> Result<Self> {
let map = match &*code.generate_source_map().await? {
Some(map) => {
let map = map.await?.to_source_map().await?;
let mut map_str = vec![];
(*map).to_writer(&mut map_str)?;
Some(String::from_utf8(map_str)?)
let map = map.to_rope().await?;
// Cloning a rope is cheap.
Some(map.clone_value())
}
None => None,
};

Ok(Self::new(id, code.await?, map, chunk_path))
}

fn new(id: &ModuleId, code: ReadRef<Code>, map: Option<String>, chunk_path: &str) -> Self {
fn new(id: &ModuleId, code: ReadRef<Code>, map: Option<Rope>, chunk_path: &str) -> Self {
/// serde_qs can't serialize a lone enum when it's [serde::untagged].
#[derive(Serialize)]
struct Id<'a> {
Expand Down

0 comments on commit 2496107

Please sign in to comment.