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 {
#[turbo_tasks::function]
async fn client_hmr_content(
self: Vc<Self>,
identifier: RcStr,
chunk_name: RcStr,
) -> Result<Vc<OptionVersionedContent>> {
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)
} else {
bail!("must be in dev mode to hmr")
......@@ -2050,8 +2050,8 @@ impl Project {
}
#[turbo_tasks::function]
async fn client_hmr_version(self: Vc<Self>, identifier: RcStr) -> Result<Vc<Box<dyn Version>>> {
let content = self.client_hmr_content(identifier).await?;
async fn client_hmr_version(self: Vc<Self>, chunk_name: RcStr) -> Result<Vc<Box<dyn Version>>> {
let content = self.client_hmr_content(chunk_name).await?;
if let Some(content) = &*content {
Ok(content.version())
} else {
......@@ -2064,10 +2064,10 @@ impl Project {
#[turbo_tasks::function]
pub async fn client_hmr_version_state(
self: Vc<Self>,
identifier: RcStr,
chunk_name: RcStr,
session: TransientInstance<()>,
) -> 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
// sessions.
......@@ -2088,15 +2088,15 @@ impl Project {
}
/// 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]
pub async fn client_hmr_update(
self: Vc<Self>,
identifier: RcStr,
chunk_name: RcStr,
from: Vc<VersionState>,
) -> Result<Vc<Update>> {
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 {
Ok(content.update(from))
} else {
......@@ -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`.
#[turbo_tasks::function]
async fn server_hmr_content(
self: Vc<Self>,
identifier: RcStr,
chunk_name: RcStr,
) -> Result<Vc<OptionVersionedContent>> {
if let Some(map) = self.await?.versioned_content_map {
// 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)
} else {
bail!("must be in dev mode to hmr")
......@@ -2134,8 +2134,8 @@ impl Project {
/// Get server module version.
/// This is the server-side equivalent of `hmr_version`.
#[turbo_tasks::function]
async fn server_hmr_version(self: Vc<Self>, identifier: RcStr) -> Result<Vc<Box<dyn Version>>> {
let content = self.server_hmr_content(identifier).await?;
async fn server_hmr_version(self: Vc<Self>, chunk_name: RcStr) -> Result<Vc<Box<dyn Version>>> {
let content = self.server_hmr_content(chunk_name).await?;
if let Some(content) = &*content {
Ok(content.version())
} else {
......@@ -2149,10 +2149,10 @@ impl Project {
#[turbo_tasks::function]
pub async fn server_hmr_version_state(
self: Vc<Self>,
identifier: RcStr,
chunk_name: RcStr,
session: TransientInstance<()>,
) -> 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
// sessions.
......@@ -2173,16 +2173,16 @@ impl Project {
}
/// 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`.
#[turbo_tasks::function]
pub async fn server_hmr_update(
self: Vc<Self>,
identifier: RcStr,
chunk_name: RcStr,
from: Vc<VersionState>,
) -> Result<Vc<Update>> {
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 {
Ok(content.update(from))
} else {
......
......@@ -1229,31 +1229,31 @@ struct HmrUpdateWithIssues {
#[turbo_tasks::function(operation)]
fn project_client_hmr_update_operation(
project: ResolvedVc<Project>,
identifier: RcStr,
chunk_name: RcStr,
state: ResolvedVc<VersionState>,
) -> Vc<Update> {
project.client_hmr_update(identifier, *state)
project.client_hmr_update(chunk_name, *state)
}
#[turbo_tasks::function(operation)]
fn project_server_hmr_update_operation(
project: ResolvedVc<Project>,
identifier: RcStr,
chunk_name: RcStr,
state: ResolvedVc<VersionState>,
) -> Vc<Update> {
project.server_hmr_update(identifier, *state)
project.server_hmr_update(chunk_name, *state)
}
#[turbo_tasks::function(operation)]
async fn hmr_update_with_issues_operation(
project: ResolvedVc<Project>,
identifier: RcStr,
chunk_name: RcStr,
state: ResolvedVc<VersionState>,
target: HmrTarget,
) -> Result<Vc<HmrUpdateWithIssues>> {
let update_op = match target {
HmrTarget::Client => project_client_hmr_update_operation(project, identifier, state),
HmrTarget::Server => project_server_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, chunk_name, state),
};
let update = update_op.read_strongly_consistent().await?;
let issues = get_issues(update_op, NEXT_ISSUE_FILTER).await?;
......@@ -1272,7 +1272,7 @@ async fn hmr_update_with_issues_operation(
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_hmr_events(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
identifier: RcStr,
chunk_name: RcStr,
target: String,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
......@@ -1286,23 +1286,23 @@ pub fn project_hmr_events(
project.turbopack_ctx.clone(),
func,
{
let outer_identifier = identifier.clone();
let outer_chunk_name = chunk_name.clone();
let session = session.clone();
move || {
let identifier: RcStr = outer_identifier.clone();
let chunk_name: RcStr = outer_chunk_name.clone();
let session = session.clone();
async move {
let project = container.project().to_resolved().await?;
let state = match hmr_target {
HmrTarget::Client => {
project
.client_hmr_version_state(identifier.clone(), session)
.client_hmr_version_state(chunk_name.clone(), session)
.to_resolved()
.await?
}
HmrTarget::Server => {
project
.server_hmr_version_state(identifier.clone(), session)
.server_hmr_version_state(chunk_name.clone(), session)
.to_resolved()
.await?
}
......@@ -1310,7 +1310,7 @@ pub fn project_hmr_events(
let update_op = hmr_update_with_issues_operation(
project,
identifier.clone(),
chunk_name.clone(),
state,
hmr_target,
);
......@@ -1348,7 +1348,7 @@ pub fn project_hmr_events(
.collect::<Vec<_>>();
let identifier = ResourceIdentifier {
path: identifier.clone(),
path: chunk_name.clone(),
headers: None,
};
let update = match update.as_deref() {
......
......@@ -315,12 +315,12 @@ export declare function projectEntrypointsSubscribe(
): { __napiType: 'RootTask' }
export declare function projectHmrEvents(
project: { __napiType: 'Project' },
identifier: RcStr,
chunkName: RcStr,
target: string,
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export interface HmrIdentifiers {
identifiers: Array<RcStr>
chunkPaths: Array<RcStr>
}
export declare function projectHmrIdentifiersSubscribe(
project: { __napiType: 'Project' },
......
......@@ -742,18 +742,18 @@ function bindingToApi(
}
hmrEvents(
identifier: string,
chunkName: string,
target: HmrTarget.Client
): AsyncIterableIterator<TurbopackResult<Update>>
hmrEvents(
identifier: string,
chunkName: string,
target: HmrTarget.Server
): AsyncIterableIterator<TurbopackResult<NodeJsHmrUpdate>>
hmrEvents(identifier: string, target: HmrTarget.Client | HmrTarget.Server) {
hmrEvents(chunkName: string, target: HmrTarget.Client | HmrTarget.Server) {
return subscribe(true, async (callback) =>
binding.projectHmrEvents(
this._nativeProject,
identifier,
chunkName,
target,
callback
)
......
......@@ -195,6 +195,13 @@ interface PartialUpdate extends BaseUpdate {
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 {
type: 'partial'
instruction: {
......
import type { NodeJsPartialHmrUpdate } from '../../build/swc/types'
// eslint-disable-next-line import/no-extraneous-dependencies
export {
createTemporaryReferenceSet,
......
......@@ -211,7 +211,7 @@ function setupServerHmr(project: Project) {
// Process chunk paths (both initial and subsequent updates)
for await (const data of serverHmrChunkPaths) {
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)
......
......@@ -21,12 +21,3 @@ let DEV_BACKEND: DevRuntimeBackend
function _eval(_: EcmascriptModuleEntry) {
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 */
/// <reference path="./hmr-types.d.ts" />
/// <reference path="../shared/runtime-utils.ts" />
/// <reference path="../shared-node/base-externals-utils.ts" />
/// <reference path="../shared-node/node-externals-utils.ts" />
/// <reference path="../shared-node/node-wasm-utils.ts" />
import type { NodeJsPartialHmrUpdate } from '../../../../../../packages/next/src/build/swc/types'
enum SourceType {
/**
* The module was instantiated because it was included in an evaluated chunk's
......
......@@ -585,7 +585,8 @@ async function instantiateWebAssemblyFromPath(path, importsObj) {
const { instance } = await WebAssembly.instantiateStreaming(response, importsObj);
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/node-externals-utils.ts" />
/// <reference path="../shared-node/node-wasm-utils.ts" />
......
......@@ -585,7 +585,8 @@ async function instantiateWebAssemblyFromPath(path, importsObj) {
const { instance } = await WebAssembly.instantiateStreaming(response, importsObj);
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/node-externals-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