Commit fdbe45aa authored by Will Binns-Smith's avatar Will Binns-Smith

Remove unnecessary Edge Runtime server HMR stub

The __turbopack_server_hmr_apply__ function is only called from the dev
server (hot-reloader-turbopack.ts) which runs in Node.js, not in Edge
Runtime. Edge Runtime (API routes/middleware) never calls this function,
so the stub is unnecessary.

The typeof check in hot-reloader-turbopack.ts handles undefined safely.
parent 63bad550
...@@ -2039,10 +2039,10 @@ impl Project { ...@@ -2039,10 +2039,10 @@ impl Project {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn client_hmr_content( async fn client_hmr_content(
self: Vc<Self>, self: Vc<Self>,
identifier: RcStr, chunk_name: RcStr,
) -> Result<Vc<OptionVersionedContent>> { ) -> Result<Vc<OptionVersionedContent>> {
if let Some(map) = self.await?.versioned_content_map { if let Some(map) = self.await?.versioned_content_map {
let content = map.get(self.client_relative_path().await?.join(&identifier)?); let content = map.get(self.client_relative_path().await?.join(&chunk_name)?);
Ok(content) Ok(content)
} else { } else {
bail!("must be in dev mode to hmr") bail!("must be in dev mode to hmr")
...@@ -2050,8 +2050,8 @@ impl Project { ...@@ -2050,8 +2050,8 @@ impl Project {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn client_hmr_version(self: Vc<Self>, identifier: RcStr) -> Result<Vc<Box<dyn Version>>> { async fn client_hmr_version(self: Vc<Self>, chunk_name: RcStr) -> Result<Vc<Box<dyn Version>>> {
let content = self.client_hmr_content(identifier).await?; let content = self.client_hmr_content(chunk_name).await?;
if let Some(content) = &*content { if let Some(content) = &*content {
Ok(content.version()) Ok(content.version())
} else { } else {
...@@ -2064,10 +2064,10 @@ impl Project { ...@@ -2064,10 +2064,10 @@ impl Project {
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn client_hmr_version_state( pub async fn client_hmr_version_state(
self: Vc<Self>, self: Vc<Self>,
identifier: RcStr, chunk_name: RcStr,
session: TransientInstance<()>, session: TransientInstance<()>,
) -> Result<Vc<VersionState>> { ) -> Result<Vc<VersionState>> {
let version = self.client_hmr_version(identifier); let version = self.client_hmr_version(chunk_name);
// The session argument is important to avoid caching this function between // The session argument is important to avoid caching this function between
// sessions. // sessions.
...@@ -2088,15 +2088,15 @@ impl Project { ...@@ -2088,15 +2088,15 @@ impl Project {
} }
/// Emits opaque HMR events whenever a change is detected in the chunk group /// Emits opaque HMR events whenever a change is detected in the chunk group
/// internally known as `identifier`. /// internally known as `chunk_name`.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn client_hmr_update( pub async fn client_hmr_update(
self: Vc<Self>, self: Vc<Self>,
identifier: RcStr, chunk_name: RcStr,
from: Vc<VersionState>, from: Vc<VersionState>,
) -> Result<Vc<Update>> { ) -> Result<Vc<Update>> {
let from = from.get(); let from = from.get();
let content = self.client_hmr_content(identifier).await?; let content = self.client_hmr_content(chunk_name).await?;
if let Some(content) = *content { if let Some(content) = *content {
Ok(content.update(from)) Ok(content.update(from))
} else { } else {
...@@ -2115,16 +2115,16 @@ impl Project { ...@@ -2115,16 +2115,16 @@ impl Project {
} }
} }
/// Get server module content by identifier (path relative to node_root). /// Get server module content by chunk_name (path relative to node_root).
/// This is the server-side equivalent of `hmr_content`. /// This is the server-side equivalent of `hmr_content`.
#[turbo_tasks::function] #[turbo_tasks::function]
async fn server_hmr_content( async fn server_hmr_content(
self: Vc<Self>, self: Vc<Self>,
identifier: RcStr, chunk_name: RcStr,
) -> Result<Vc<OptionVersionedContent>> { ) -> Result<Vc<OptionVersionedContent>> {
if let Some(map) = self.await?.versioned_content_map { if let Some(map) = self.await?.versioned_content_map {
// Key difference from hmr_content: use node_root instead of client_relative_path // Key difference from hmr_content: use node_root instead of client_relative_path
let content = map.get(self.node_root().await?.join(&identifier)?); let content = map.get(self.node_root().await?.join(&chunk_name)?);
Ok(content) Ok(content)
} else { } else {
bail!("must be in dev mode to hmr") bail!("must be in dev mode to hmr")
...@@ -2134,8 +2134,8 @@ impl Project { ...@@ -2134,8 +2134,8 @@ impl Project {
/// Get server module version. /// Get server module version.
/// This is the server-side equivalent of `hmr_version`. /// This is the server-side equivalent of `hmr_version`.
#[turbo_tasks::function] #[turbo_tasks::function]
async fn server_hmr_version(self: Vc<Self>, identifier: RcStr) -> Result<Vc<Box<dyn Version>>> { async fn server_hmr_version(self: Vc<Self>, chunk_name: RcStr) -> Result<Vc<Box<dyn Version>>> {
let content = self.server_hmr_content(identifier).await?; let content = self.server_hmr_content(chunk_name).await?;
if let Some(content) = &*content { if let Some(content) = &*content {
Ok(content.version()) Ok(content.version())
} else { } else {
...@@ -2149,10 +2149,10 @@ impl Project { ...@@ -2149,10 +2149,10 @@ impl Project {
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn server_hmr_version_state( pub async fn server_hmr_version_state(
self: Vc<Self>, self: Vc<Self>,
identifier: RcStr, chunk_name: RcStr,
session: TransientInstance<()>, session: TransientInstance<()>,
) -> Result<Vc<VersionState>> { ) -> Result<Vc<VersionState>> {
let version = self.server_hmr_version(identifier); let version = self.server_hmr_version(chunk_name);
// The session argument is important to avoid caching this function between // The session argument is important to avoid caching this function between
// sessions. // sessions.
...@@ -2173,16 +2173,16 @@ impl Project { ...@@ -2173,16 +2173,16 @@ impl Project {
} }
/// Emits opaque HMR events whenever a change is detected in server-side modules /// Emits opaque HMR events whenever a change is detected in server-side modules
/// internally known as `identifier`. /// internally known as `chunk_name`.
/// This is the server-side equivalent of `hmr_update`. /// This is the server-side equivalent of `hmr_update`.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn server_hmr_update( pub async fn server_hmr_update(
self: Vc<Self>, self: Vc<Self>,
identifier: RcStr, chunk_name: RcStr,
from: Vc<VersionState>, from: Vc<VersionState>,
) -> Result<Vc<Update>> { ) -> Result<Vc<Update>> {
let from = from.get(); let from = from.get();
let content = self.server_hmr_content(identifier).await?; let content = self.server_hmr_content(chunk_name).await?;
if let Some(content) = *content { if let Some(content) = *content {
Ok(content.update(from)) Ok(content.update(from))
} else { } else {
......
...@@ -1229,31 +1229,31 @@ struct HmrUpdateWithIssues { ...@@ -1229,31 +1229,31 @@ struct HmrUpdateWithIssues {
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
fn project_client_hmr_update_operation( fn project_client_hmr_update_operation(
project: ResolvedVc<Project>, project: ResolvedVc<Project>,
identifier: RcStr, chunk_name: RcStr,
state: ResolvedVc<VersionState>, state: ResolvedVc<VersionState>,
) -> Vc<Update> { ) -> Vc<Update> {
project.client_hmr_update(identifier, *state) project.client_hmr_update(chunk_name, *state)
} }
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
fn project_server_hmr_update_operation( fn project_server_hmr_update_operation(
project: ResolvedVc<Project>, project: ResolvedVc<Project>,
identifier: RcStr, chunk_name: RcStr,
state: ResolvedVc<VersionState>, state: ResolvedVc<VersionState>,
) -> Vc<Update> { ) -> Vc<Update> {
project.server_hmr_update(identifier, *state) project.server_hmr_update(chunk_name, *state)
} }
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
async fn hmr_update_with_issues_operation( async fn hmr_update_with_issues_operation(
project: ResolvedVc<Project>, project: ResolvedVc<Project>,
identifier: RcStr, chunk_name: RcStr,
state: ResolvedVc<VersionState>, state: ResolvedVc<VersionState>,
target: HmrTarget, target: HmrTarget,
) -> Result<Vc<HmrUpdateWithIssues>> { ) -> Result<Vc<HmrUpdateWithIssues>> {
let update_op = match target { let update_op = match target {
HmrTarget::Client => project_client_hmr_update_operation(project, identifier, state), HmrTarget::Client => project_client_hmr_update_operation(project, chunk_name, state),
HmrTarget::Server => project_server_hmr_update_operation(project, identifier, state), HmrTarget::Server => project_server_hmr_update_operation(project, chunk_name, state),
}; };
let update = update_op.read_strongly_consistent().await?; let update = update_op.read_strongly_consistent().await?;
let issues = get_issues(update_op, NEXT_ISSUE_FILTER).await?; let issues = get_issues(update_op, NEXT_ISSUE_FILTER).await?;
...@@ -1272,7 +1272,7 @@ async fn hmr_update_with_issues_operation( ...@@ -1272,7 +1272,7 @@ async fn hmr_update_with_issues_operation(
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")] #[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_hmr_events( pub fn project_hmr_events(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>, #[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
identifier: RcStr, chunk_name: RcStr,
target: String, target: String,
func: JsFunction, func: JsFunction,
) -> napi::Result<External<RootTask>> { ) -> napi::Result<External<RootTask>> {
...@@ -1286,23 +1286,23 @@ pub fn project_hmr_events( ...@@ -1286,23 +1286,23 @@ pub fn project_hmr_events(
project.turbopack_ctx.clone(), project.turbopack_ctx.clone(),
func, func,
{ {
let outer_identifier = identifier.clone(); let outer_chunk_name = chunk_name.clone();
let session = session.clone(); let session = session.clone();
move || { move || {
let identifier: RcStr = outer_identifier.clone(); let chunk_name: RcStr = outer_chunk_name.clone();
let session = session.clone(); let session = session.clone();
async move { async move {
let project = container.project().to_resolved().await?; let project = container.project().to_resolved().await?;
let state = match hmr_target { let state = match hmr_target {
HmrTarget::Client => { HmrTarget::Client => {
project project
.client_hmr_version_state(identifier.clone(), session) .client_hmr_version_state(chunk_name.clone(), session)
.to_resolved() .to_resolved()
.await? .await?
} }
HmrTarget::Server => { HmrTarget::Server => {
project project
.server_hmr_version_state(identifier.clone(), session) .server_hmr_version_state(chunk_name.clone(), session)
.to_resolved() .to_resolved()
.await? .await?
} }
...@@ -1310,7 +1310,7 @@ pub fn project_hmr_events( ...@@ -1310,7 +1310,7 @@ pub fn project_hmr_events(
let update_op = hmr_update_with_issues_operation( let update_op = hmr_update_with_issues_operation(
project, project,
identifier.clone(), chunk_name.clone(),
state, state,
hmr_target, hmr_target,
); );
...@@ -1348,7 +1348,7 @@ pub fn project_hmr_events( ...@@ -1348,7 +1348,7 @@ pub fn project_hmr_events(
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let identifier = ResourceIdentifier { let identifier = ResourceIdentifier {
path: identifier.clone(), path: chunk_name.clone(),
headers: None, headers: None,
}; };
let update = match update.as_deref() { let update = match update.as_deref() {
......
...@@ -315,12 +315,12 @@ export declare function projectEntrypointsSubscribe( ...@@ -315,12 +315,12 @@ export declare function projectEntrypointsSubscribe(
): { __napiType: 'RootTask' } ): { __napiType: 'RootTask' }
export declare function projectHmrEvents( export declare function projectHmrEvents(
project: { __napiType: 'Project' }, project: { __napiType: 'Project' },
identifier: RcStr, chunkName: RcStr,
target: string, target: string,
func: (...args: any[]) => any func: (...args: any[]) => any
): { __napiType: 'RootTask' } ): { __napiType: 'RootTask' }
export interface HmrIdentifiers { export interface HmrIdentifiers {
identifiers: Array<RcStr> chunkPaths: Array<RcStr>
} }
export declare function projectHmrIdentifiersSubscribe( export declare function projectHmrIdentifiersSubscribe(
project: { __napiType: 'Project' }, project: { __napiType: 'Project' },
......
...@@ -742,18 +742,18 @@ function bindingToApi( ...@@ -742,18 +742,18 @@ function bindingToApi(
} }
hmrEvents( hmrEvents(
identifier: string, chunkName: string,
target: HmrTarget.Client target: HmrTarget.Client
): AsyncIterableIterator<TurbopackResult<Update>> ): AsyncIterableIterator<TurbopackResult<Update>>
hmrEvents( hmrEvents(
identifier: string, chunkName: string,
target: HmrTarget.Server target: HmrTarget.Server
): AsyncIterableIterator<TurbopackResult<NodeJsHmrUpdate>> ): AsyncIterableIterator<TurbopackResult<NodeJsHmrUpdate>>
hmrEvents(identifier: string, target: HmrTarget.Client | HmrTarget.Server) { hmrEvents(chunkName: string, target: HmrTarget.Client | HmrTarget.Server) {
return subscribe(true, async (callback) => return subscribe(true, async (callback) =>
binding.projectHmrEvents( binding.projectHmrEvents(
this._nativeProject, this._nativeProject,
identifier, chunkName,
target, target,
callback callback
) )
......
...@@ -195,6 +195,13 @@ interface PartialUpdate extends BaseUpdate { ...@@ -195,6 +195,13 @@ interface PartialUpdate extends BaseUpdate {
export type Update = IssuesUpdate | PartialUpdate export type Update = IssuesUpdate | PartialUpdate
/**
* IMPORTANT: This type is duplicated in:
* turbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs/hmr-types.d.ts
*
* The runtime file cannot import from this ES module without triggering module semantics,
* so we maintain a copy there. Please keep both definitions in sync.
*/
export interface NodeJsPartialHmrUpdate extends BaseUpdate { export interface NodeJsPartialHmrUpdate extends BaseUpdate {
type: 'partial' type: 'partial'
instruction: { instruction: {
......
import type { NodeJsPartialHmrUpdate } from '../../build/swc/types'
// eslint-disable-next-line import/no-extraneous-dependencies // eslint-disable-next-line import/no-extraneous-dependencies
export { export {
createTemporaryReferenceSet, createTemporaryReferenceSet,
......
...@@ -211,7 +211,7 @@ function setupServerHmr(project: Project) { ...@@ -211,7 +211,7 @@ function setupServerHmr(project: Project) {
// Process chunk paths (both initial and subsequent updates) // Process chunk paths (both initial and subsequent updates)
for await (const data of serverHmrChunkPaths) { for await (const data of serverHmrChunkPaths) {
const currentChunkPaths = new Set( const currentChunkPaths = new Set(
data.chunk_paths.filter((path) => path.endsWith('.js')) data.chunkPaths.filter((path) => path.endsWith('.js'))
) )
// Clean up subscriptions for removed chunk paths (like when pages are deleted) // Clean up subscriptions for removed chunk paths (like when pages are deleted)
......
...@@ -21,12 +21,3 @@ let DEV_BACKEND: DevRuntimeBackend ...@@ -21,12 +21,3 @@ let DEV_BACKEND: DevRuntimeBackend
function _eval(_: EcmascriptModuleEntry) { function _eval(_: EcmascriptModuleEntry) {
throw new Error('HMR evaluation is not implemented on this backend') throw new Error('HMR evaluation is not implemented on this backend')
} }
function __turbopack_server_hmr_apply__(_update: any): boolean {
// Server-side HMR is not supported in Edge Runtime (for API routes/middleware).
// Edge Runtime has limited Node.js APIs and doesn't support the full HMR implementation.
// Return false to indicate the update was not applied.
return false
}
globalThis.__turbopack_server_hmr_apply__ = __turbopack_server_hmr_apply__
/**
* Type definitions for Node.js HMR updates.
*
* IMPORTANT: This is a duplicate of the types in packages/next/src/build/swc/types.ts
* to avoid importing from an ES module, which makes TypeScript treat the file as an ES module.
*
* Keep NodeJsPartialHmrUpdate in sync with the copy in packages/next/src/build/swc/types.ts.
*/
interface NodeJsPartialHmrUpdate {
type: 'partial'
instruction: {
type: 'EcmascriptMergedUpdate'
entries: Record<
string,
{ code: string; url: string; map?: string | undefined }
>
chunks?: Record<string, { type: 'partial' }>
}
}
/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-unused-vars */
/// <reference path="./hmr-types.d.ts" />
/// <reference path="../shared/runtime-utils.ts" /> /// <reference path="../shared/runtime-utils.ts" />
/// <reference path="../shared-node/base-externals-utils.ts" /> /// <reference path="../shared-node/base-externals-utils.ts" />
/// <reference path="../shared-node/node-externals-utils.ts" /> /// <reference path="../shared-node/node-externals-utils.ts" />
/// <reference path="../shared-node/node-wasm-utils.ts" /> /// <reference path="../shared-node/node-wasm-utils.ts" />
import type { NodeJsPartialHmrUpdate } from '../../../../../../packages/next/src/build/swc/types'
enum SourceType { enum SourceType {
/** /**
* The module was instantiated because it was included in an evaluated chunk's * The module was instantiated because it was included in an evaluated chunk's
......
...@@ -585,7 +585,8 @@ async function instantiateWebAssemblyFromPath(path, importsObj) { ...@@ -585,7 +585,8 @@ async function instantiateWebAssemblyFromPath(path, importsObj) {
const { instance } = await WebAssembly.instantiateStreaming(response, importsObj); const { instance } = await WebAssembly.instantiateStreaming(response, importsObj);
return instance.exports; return instance.exports;
} }
/* eslint-disable @typescript-eslint/no-unused-vars */ /// <reference path="../shared/runtime-utils.ts" /> /* eslint-disable @typescript-eslint/no-unused-vars */ /// <reference path="./hmr-types.d.ts" />
/// <reference path="../shared/runtime-utils.ts" />
/// <reference path="../shared-node/base-externals-utils.ts" /> /// <reference path="../shared-node/base-externals-utils.ts" />
/// <reference path="../shared-node/node-externals-utils.ts" /> /// <reference path="../shared-node/node-externals-utils.ts" />
/// <reference path="../shared-node/node-wasm-utils.ts" /> /// <reference path="../shared-node/node-wasm-utils.ts" />
......
...@@ -585,7 +585,8 @@ async function instantiateWebAssemblyFromPath(path, importsObj) { ...@@ -585,7 +585,8 @@ async function instantiateWebAssemblyFromPath(path, importsObj) {
const { instance } = await WebAssembly.instantiateStreaming(response, importsObj); const { instance } = await WebAssembly.instantiateStreaming(response, importsObj);
return instance.exports; return instance.exports;
} }
/* eslint-disable @typescript-eslint/no-unused-vars */ /// <reference path="../shared/runtime-utils.ts" /> /* eslint-disable @typescript-eslint/no-unused-vars */ /// <reference path="./hmr-types.d.ts" />
/// <reference path="../shared/runtime-utils.ts" />
/// <reference path="../shared-node/base-externals-utils.ts" /> /// <reference path="../shared-node/base-externals-utils.ts" />
/// <reference path="../shared-node/node-externals-utils.ts" /> /// <reference path="../shared-node/node-externals-utils.ts" />
/// <reference path="../shared-node/node-wasm-utils.ts" /> /// <reference path="../shared-node/node-wasm-utils.ts" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment