Skip to content

Commit

Permalink
Extract shared HMR utils to their own modules/crates (vercel/turborep…
Browse files Browse the repository at this point in the history
…o#5576)

### Description

These will also be used in Next.js by the Nexturbo dev API.

### Testing Instructions

N/A
  • Loading branch information
alexkirsz authored Jul 20, 2023
1 parent 12d95e0 commit 8b841e9
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 40 deletions.
9 changes: 9 additions & 0 deletions crates/turbopack-binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ __turbo_tasks_hash = ["__turbo", "turbo-tasks-hash"]
__turbo_tasks_macros = ["__turbo", "turbo-tasks-macros"]
__turbo_tasks_macros_shared = ["__turbo", "turbo-tasks-macros-shared"]
__turbo_tasks_memory = ["__turbo", "turbo-tasks-memory"]
__turbo_tasks_memory_print_task_invalidation = [
"__turbo_tasks_memory",
"turbo-tasks-memory/print_task_invalidation",
]
__turbo_tasks_testing = ["__turbo", "turbo-tasks-testing"]
__turbo_updater = ["__turbo", "turbo-updater"]

Expand Down Expand Up @@ -128,6 +132,10 @@ __turbopack_ecmascript_plugin = [
"turbopack-ecmascript-plugins/transform_emotion",
]
__turbopack_ecmascript_runtime = ["__turbopack", "turbopack-ecmascript-runtime"]
__turbopack_ecmascript_hmr_protocol = [
"__turbopack",
"turbopack-ecmascript-hmr-protocol",
]

__turbopack_env = ["__turbopack", "turbopack-env"]
__turbopack_image = ["__turbopack", "turbopack-image"]
Expand Down Expand Up @@ -209,6 +217,7 @@ turbopack-css = { optional = true, workspace = true }
turbopack-dev = { optional = true, workspace = true }
turbopack-dev-server = { optional = true, workspace = true }
turbopack-ecmascript = { optional = true, workspace = true }
turbopack-ecmascript-hmr-protocol = { optional = true, workspace = true }
turbopack-ecmascript-plugins = { optional = true, workspace = true, default-features = false }
turbopack-ecmascript-runtime = { optional = true, workspace = true }
turbopack-env = { optional = true, workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions crates/turbopack-binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub mod turbopack {
pub use turbopack_dev_server as dev_server;
#[cfg(feature = "__turbopack_ecmascript")]
pub use turbopack_ecmascript as ecmascript;
#[cfg(feature = "__turbopack_ecmascript_hmr_protocol")]
pub use turbopack_ecmascript_hmr_protocol as ecmascript_hmr_protocol;
#[cfg(feature = "__turbopack_ecmascript_plugin")]
pub use turbopack_ecmascript_plugins as ecmascript_plugin;
#[cfg(feature = "__turbopack_ecmascript_runtime")]
Expand Down
32 changes: 31 additions & 1 deletion crates/turbopack-core/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use turbo_tasks::{
debug::ValueDebugFormat, trace::TraceRawVcs, IntoTraitRef, ReadRef, TraitRef, Vc,
debug::ValueDebugFormat, trace::TraceRawVcs, IntoTraitRef, ReadRef, State, TraitRef, Vc,
};
use turbo_tasks_fs::{FileContent, LinkType};
use turbo_tasks_hash::{encode_hex, hash_xxh3_hash64};
Expand Down Expand Up @@ -232,3 +232,33 @@ impl Version for FileHashVersion {
Ok(Vc::cell(self.hash.clone()))
}
}

#[turbo_tasks::value]
pub struct VersionState {
#[turbo_tasks(trace_ignore)]
version: State<TraitRef<Box<dyn Version>>>,
}

#[turbo_tasks::value_impl]
impl VersionState {
#[turbo_tasks::function]
pub async fn get(self: Vc<Self>) -> Result<Vc<Box<dyn Version>>> {
let this = self.await?;
let version = TraitRef::cell(this.version.get().clone());
Ok(version)
}
}

impl VersionState {
pub async fn new(version: TraitRef<Box<dyn Version>>) -> Result<Vc<Self>> {
Ok(Self::cell(VersionState {
version: State::new(version),
}))
}

pub async fn set(self: Vc<Self>, new_version: TraitRef<Box<dyn Version>>) -> Result<()> {
let this = self.await?;
this.version.set(new_version);
Ok(())
}
}
1 change: 1 addition & 0 deletions crates/turbopack-dev-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ turbo-tasks-fs = { workspace = true }
turbo-tasks-hash = { workspace = true }
turbopack-core = { workspace = true }
turbopack-ecmascript = { workspace = true }
turbopack-ecmascript-hmr-protocol = { workspace = true }
# TODO remove this dependency
turbopack-cli-utils = { workspace = true }

Expand Down
1 change: 0 additions & 1 deletion crates/turbopack-dev-server/src/update/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod protocol;
pub mod server;
pub mod stream;

Expand Down
8 changes: 4 additions & 4 deletions crates/turbopack-dev-server/src/update/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use tracing::{instrument, Level};
use turbo_tasks::{TransientInstance, TurboTasksApi, Vc};
use turbo_tasks_fs::json::parse_json_with_source_context;
use turbopack_core::{error::PrettyPrintError, issue::IssueReporter, version::Update};

use super::{
protocol::{ClientMessage, ClientUpdateInstruction, Issue, ResourceIdentifier},
stream::UpdateStream,
use turbopack_ecmascript_hmr_protocol::{
ClientMessage, ClientUpdateInstruction, Issue, ResourceIdentifier,
};

use super::stream::UpdateStream;
use crate::{
source::{request::SourceRequest, resolve::resolve_source_request, Body},
update::stream::UpdateStreamItem,
Expand Down
37 changes: 5 additions & 32 deletions crates/turbopack-dev-server/src/update/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ use futures::{prelude::*, Stream};
use tokio::sync::mpsc::Sender;
use tokio_stream::wrappers::ReceiverStream;
use tracing::Instrument;
use turbo_tasks::{unit, IntoTraitRef, ReadRef, State, TraitRef, TransientInstance, Vc};
use turbo_tasks::{unit, IntoTraitRef, ReadRef, TransientInstance, Vc};
use turbo_tasks_fs::{FileSystem, FileSystemPath};
use turbopack_core::{
error::PrettyPrintError,
issue::{Issue, IssueContextExt, IssueSeverity, OptionIssueProcessingPathItems, PlainIssue},
server_fs::ServerFileSystem,
version::{NotFoundVersion, PartialUpdate, TotalUpdate, Update, Version, VersionedContent},
version::{
NotFoundVersion, PartialUpdate, TotalUpdate, Update, Version, VersionState,
VersionedContent,
},
};

use crate::source::{resolve::ResolveSourceRequestResult, ProxyResult};
Expand Down Expand Up @@ -167,36 +170,6 @@ async fn compute_update_stream(
Ok(unit())
}

#[turbo_tasks::value]
struct VersionState {
#[turbo_tasks(trace_ignore)]
version: State<TraitRef<Box<dyn Version>>>,
}

#[turbo_tasks::value_impl]
impl VersionState {
#[turbo_tasks::function]
async fn get(self: Vc<Self>) -> Result<Vc<Box<dyn Version>>> {
let this = self.await?;
let version = TraitRef::cell(this.version.get().clone());
Ok(version)
}
}

impl VersionState {
async fn new(version: TraitRef<Box<dyn Version>>) -> Result<Vc<Self>> {
Ok(Self::cell(VersionState {
version: State::new(version),
}))
}

async fn set(self: Vc<Self>, new_version: TraitRef<Box<dyn Version>>) -> Result<()> {
let this = self.await?;
this.version.set(new_version);
Ok(())
}
}

pub(super) struct UpdateStream(
Pin<Box<dyn Stream<Item = Result<ReadRef<UpdateStreamItem>>> + Send + Sync>>,
);
Expand Down
17 changes: 17 additions & 0 deletions crates/turbopack-ecmascript-hmr-protocol/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "turbopack-ecmascript-hmr-protocol"
version = "0.1.0"
description = "TBD"
license = "MPL-2.0"
edition = "2021"
autobenches = false

[lib]
bench = false

[dependencies]
serde = { workspace = true }
serde_json = { workspace = true }

turbopack-cli-utils = { workspace = true }
turbopack-core = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use turbopack_core::{
source_pos::SourcePos,
};

#[turbo_tasks::value(serialization = "auto_for_input")]
#[derive(Debug, Clone, Hash, PartialOrd, Ord)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ResourceIdentifier {
pub path: String,
pub headers: Option<BTreeMap<String, String>>,
Expand Down

0 comments on commit 8b841e9

Please sign in to comment.