Commit 51f55989 authored by Will Binns-Smith's avatar Will Binns-Smith

Add Node.js dev runtime with HMR infrastructure

Creates a new Node.js development runtime (dev-runtime.ts) that includes
HMR support, mirroring the browser's dev-base.ts functionality:

- HotModule interface with module.hot API (accept, decline, dispose, etc.)
- createModuleHot() for creating the hot API on modules
- disposeModule() for cleaning up modules during HMR
- getAffectedModuleEffects() for traversing the module graph
- applyEcmascriptMergedUpdate() for applying HMR updates
- Exposes globalThis.__turbopack_server_hmr_apply__ for the hot-reloader

Updates the Rust side:
- nodejs_runtime.rs now takes RuntimeType parameter
- Uses dev-runtime.ts for development, runtime.ts for production
parent 1f135625
...@@ -6,12 +6,15 @@ use turbopack_core::{ ...@@ -6,12 +6,15 @@ use turbopack_core::{
environment::Environment, environment::Environment,
}; };
use crate::{asset_context::get_runtime_asset_context, embed_js::embed_static_code}; use crate::{RuntimeType, asset_context::get_runtime_asset_context, embed_js::embed_static_code};
/// Returns the code for the Node.js production ECMAScript runtime. /// Returns the code for the Node.js ECMAScript runtime.
/// In development mode, includes HMR infrastructure.
/// In production mode, uses a minimal runtime.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_nodejs_runtime_code( pub async fn get_nodejs_runtime_code(
environment: ResolvedVc<Environment>, environment: ResolvedVc<Environment>,
runtime_type: RuntimeType,
generate_source_map: bool, generate_source_map: bool,
) -> Result<Vc<Code>> { ) -> Result<Vc<Code>> {
let asset_context = get_runtime_asset_context(*environment).resolve().await?; let asset_context = get_runtime_asset_context(*environment).resolve().await?;
...@@ -36,11 +39,24 @@ pub async fn get_nodejs_runtime_code( ...@@ -36,11 +39,24 @@ pub async fn get_nodejs_runtime_code(
rcstr!("shared-node/node-wasm-utils.ts"), rcstr!("shared-node/node-wasm-utils.ts"),
generate_source_map, generate_source_map,
); );
let runtime_code = embed_static_code(
asset_context, // Use dev runtime (with HMR support) in development, production runtime otherwise
rcstr!("nodejs/runtime.ts"), let runtime_code = match runtime_type {
generate_source_map, RuntimeType::Development => embed_static_code(
); asset_context,
rcstr!("nodejs/dev-runtime.ts"),
generate_source_map,
),
RuntimeType::Production => embed_static_code(
asset_context,
rcstr!("nodejs/runtime.ts"),
generate_source_map,
),
#[cfg(feature = "test")]
RuntimeType::Dummy => {
panic!("Dummy runtime type is not supported for Node.js runtime")
}
};
let mut code = CodeBuilder::default(); let mut code = CodeBuilder::default();
code.push_code(&*shared_runtime_utils_code.await?); code.push_code(&*shared_runtime_utils_code.await?);
......
...@@ -65,17 +65,12 @@ impl EcmascriptBuildNodeRuntimeChunk { ...@@ -65,17 +65,12 @@ impl EcmascriptBuildNodeRuntimeChunk {
StringifyJs(asset_prefix), StringifyJs(asset_prefix),
)?; )?;
match *this.chunking_context.runtime_type().await? { let runtime_type = *this.chunking_context.runtime_type().await?;
RuntimeType::Development => { match runtime_type {
let runtime_code = turbopack_ecmascript_runtime::get_nodejs_runtime_code( RuntimeType::Development | RuntimeType::Production => {
this.chunking_context.environment(),
generate_source_map,
);
code.push_code(&*runtime_code.await?);
}
RuntimeType::Production => {
let runtime_code = turbopack_ecmascript_runtime::get_nodejs_runtime_code( let runtime_code = turbopack_ecmascript_runtime::get_nodejs_runtime_code(
this.chunking_context.environment(), this.chunking_context.environment(),
runtime_type,
generate_source_map, generate_source_map,
); );
code.push_code(&*runtime_code.await?); code.push_code(&*runtime_code.await?);
......
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