From 0c826f885ef2b8de6ff9d9e607f2fda2eaa8cf29 Mon Sep 17 00:00:00 2001 From: Alex Kirszenberg Date: Thu, 20 Jul 2023 17:54:03 +0200 Subject: [PATCH] Extract shared HMR utils to their own modules/crates --- Cargo.lock | 12 ++++++ Cargo.toml | 1 + crates/turbopack-binding/Cargo.toml | 9 +++++ crates/turbopack-binding/src/lib.rs | 2 + crates/turbopack-core/src/version.rs | 32 +++++++++++++++- crates/turbopack-dev-server/Cargo.toml | 1 + crates/turbopack-dev-server/src/update/mod.rs | 1 - .../turbopack-dev-server/src/update/server.rs | 8 ++-- .../turbopack-dev-server/src/update/stream.rs | 37 +++---------------- .../Cargo.toml | 17 +++++++++ .../src/lib.rs} | 3 +- 11 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 crates/turbopack-ecmascript-hmr-protocol/Cargo.toml rename crates/{turbopack-dev-server/src/update/protocol.rs => turbopack-ecmascript-hmr-protocol/src/lib.rs} (97%) diff --git a/Cargo.lock b/Cargo.lock index 5b7b87eb474dd..52590a257dc76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9086,6 +9086,7 @@ dependencies = [ "turbopack-dev", "turbopack-dev-server", "turbopack-ecmascript", + "turbopack-ecmascript-hmr-protocol", "turbopack-ecmascript-plugins", "turbopack-ecmascript-runtime", "turbopack-env", @@ -9315,6 +9316,7 @@ dependencies = [ "turbopack-cli-utils", "turbopack-core", "turbopack-ecmascript", + "turbopack-ecmascript-hmr-protocol", "urlencoding", ] @@ -9354,6 +9356,16 @@ dependencies = [ "url", ] +[[package]] +name = "turbopack-ecmascript-hmr-protocol" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "turbopack-cli-utils", + "turbopack-core", +] + [[package]] name = "turbopack-ecmascript-plugins" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index a9355060e2e1d..29538330dd5f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -116,6 +116,7 @@ turbopack-dev-server = { path = "crates/turbopack-dev-server" } turbopack-ecmascript = { path = "crates/turbopack-ecmascript" } turbopack-ecmascript-plugins = { path = "crates/turbopack-ecmascript-plugins", default-features = false } turbopack-ecmascript-runtime = { path = "crates/turbopack-ecmascript-runtime" } +turbopack-ecmascript-hmr-protocol = { path = "crates/turbopack-ecmascript-hmr-protocol" } turbopack-env = { path = "crates/turbopack-env" } turbopack-image = { path = "crates/turbopack-image" } turbopack-json = { path = "crates/turbopack-json" } diff --git a/crates/turbopack-binding/Cargo.toml b/crates/turbopack-binding/Cargo.toml index 7d5ce9d95a21f..b5edcd29716f7 100644 --- a/crates/turbopack-binding/Cargo.toml +++ b/crates/turbopack-binding/Cargo.toml @@ -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"] @@ -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"] @@ -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 } diff --git a/crates/turbopack-binding/src/lib.rs b/crates/turbopack-binding/src/lib.rs index af2ed45149c5e..8de64853108b9 100644 --- a/crates/turbopack-binding/src/lib.rs +++ b/crates/turbopack-binding/src/lib.rs @@ -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")] diff --git a/crates/turbopack-core/src/version.rs b/crates/turbopack-core/src/version.rs index 2865d52a5bf45..bb891a42dc6ef 100644 --- a/crates/turbopack-core/src/version.rs +++ b/crates/turbopack-core/src/version.rs @@ -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}; @@ -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>>, +} + +#[turbo_tasks::value_impl] +impl VersionState { + #[turbo_tasks::function] + pub async fn get(self: Vc) -> Result>> { + let this = self.await?; + let version = TraitRef::cell(this.version.get().clone()); + Ok(version) + } +} + +impl VersionState { + pub async fn new(version: TraitRef>) -> Result> { + Ok(Self::cell(VersionState { + version: State::new(version), + })) + } + + pub async fn set(self: Vc, new_version: TraitRef>) -> Result<()> { + let this = self.await?; + this.version.set(new_version); + Ok(()) + } +} diff --git a/crates/turbopack-dev-server/Cargo.toml b/crates/turbopack-dev-server/Cargo.toml index 26dac7f51132f..48cd17caf8575 100644 --- a/crates/turbopack-dev-server/Cargo.toml +++ b/crates/turbopack-dev-server/Cargo.toml @@ -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 } diff --git a/crates/turbopack-dev-server/src/update/mod.rs b/crates/turbopack-dev-server/src/update/mod.rs index 781760161f5a7..f07e04a8f32de 100644 --- a/crates/turbopack-dev-server/src/update/mod.rs +++ b/crates/turbopack-dev-server/src/update/mod.rs @@ -1,4 +1,3 @@ -pub mod protocol; pub mod server; pub mod stream; diff --git a/crates/turbopack-dev-server/src/update/server.rs b/crates/turbopack-dev-server/src/update/server.rs index 7d1e8586081ea..dbff868f5a20a 100644 --- a/crates/turbopack-dev-server/src/update/server.rs +++ b/crates/turbopack-dev-server/src/update/server.rs @@ -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, diff --git a/crates/turbopack-dev-server/src/update/stream.rs b/crates/turbopack-dev-server/src/update/stream.rs index ec7aafb07a4cd..56e3832d895c7 100644 --- a/crates/turbopack-dev-server/src/update/stream.rs +++ b/crates/turbopack-dev-server/src/update/stream.rs @@ -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}; @@ -167,36 +170,6 @@ async fn compute_update_stream( Ok(unit()) } -#[turbo_tasks::value] -struct VersionState { - #[turbo_tasks(trace_ignore)] - version: State>>, -} - -#[turbo_tasks::value_impl] -impl VersionState { - #[turbo_tasks::function] - async fn get(self: Vc) -> Result>> { - let this = self.await?; - let version = TraitRef::cell(this.version.get().clone()); - Ok(version) - } -} - -impl VersionState { - async fn new(version: TraitRef>) -> Result> { - Ok(Self::cell(VersionState { - version: State::new(version), - })) - } - - async fn set(self: Vc, new_version: TraitRef>) -> Result<()> { - let this = self.await?; - this.version.set(new_version); - Ok(()) - } -} - pub(super) struct UpdateStream( Pin>> + Send + Sync>>, ); diff --git a/crates/turbopack-ecmascript-hmr-protocol/Cargo.toml b/crates/turbopack-ecmascript-hmr-protocol/Cargo.toml new file mode 100644 index 0000000000000..85494f687cc30 --- /dev/null +++ b/crates/turbopack-ecmascript-hmr-protocol/Cargo.toml @@ -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 } diff --git a/crates/turbopack-dev-server/src/update/protocol.rs b/crates/turbopack-ecmascript-hmr-protocol/src/lib.rs similarity index 97% rename from crates/turbopack-dev-server/src/update/protocol.rs rename to crates/turbopack-ecmascript-hmr-protocol/src/lib.rs index ebdb95cbdb1f7..2235aef62d770 100644 --- a/crates/turbopack-dev-server/src/update/protocol.rs +++ b/crates/turbopack-ecmascript-hmr-protocol/src/lib.rs @@ -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>,