Commit ec1c363d authored by Luke Sandberg's avatar Luke Sandberg Committed by Luke Sandberg

simplify server runtime

parent c8db3497
...@@ -17,22 +17,6 @@ type ChunkRunner = { ...@@ -17,22 +17,6 @@ type ChunkRunner = {
runtimeModuleIds: ModuleId[] runtimeModuleIds: ModuleId[]
} }
// Override the shared asyncLoader with an async version for Edge.
// In the Edge runtime, chunk loading is synchronous (all chunks are pre-bundled),
// so the loader may return a plain value or throw. Since dynamic import() must
// always return a Promise per the spec, wrapping in async ensures both success
// values and errors are properly wrapped in a Promise.
async function asyncLoaderEdge(
this: TurbopackBaseContext<Module>,
moduleId: ModuleId
): Promise<Exports> {
const loader = this.r(moduleId) as (
importFunction: EsmImport
) => Exports | Promise<Exports>
return loader(esmImport.bind(this))
}
contextPrototype.A = asyncLoaderEdge
let BACKEND: RuntimeBackend let BACKEND: RuntimeBackend
;(() => { ;(() => {
BACKEND = { BACKEND = {
......
...@@ -56,17 +56,24 @@ function loadRuntimeChunk(sourcePath: ChunkPath, chunkData: ChunkData): void { ...@@ -56,17 +56,24 @@ function loadRuntimeChunk(sourcePath: ChunkPath, chunkData: ChunkData): void {
} }
} }
const loadedChunks = new Set<ChunkPath>() // null = loaded successfully, Error = load failed (cached for future requests)
const chunkErrors = new Map<ChunkPath, Error>() const loadedChunks = new Map<ChunkPath, Error | null>()
function clearChunkCache() { function clearChunkCache() {
loadedChunks.clear() loadedChunks.clear()
chunkErrors.clear()
} }
function loadRuntimeChunkPath( /**
sourcePath: ChunkPath, * Synchronously loads a chunk using require(). Caches results so a chunk is
chunkPath: ChunkPath * only loaded once; cached errors are re-thrown on subsequent calls.
*
* @param sourceId - identifier for error messages (chunk path or module id)
* @param sourceIsModule - true: "from module {sourceId}", false: "from runtime for chunk {sourceId}"
*/
function loadChunkPath(
chunkPath: ChunkPath,
sourceId: ModuleId,
sourceIsModule: boolean
): void { ): void {
if (!isJs(chunkPath)) { if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js. // We only support loading JS chunks in Node.js.
...@@ -74,7 +81,9 @@ function loadRuntimeChunkPath( ...@@ -74,7 +81,9 @@ function loadRuntimeChunkPath(
return return
} }
if (loadedChunks.has(chunkPath)) { const cached = loadedChunks.get(chunkPath)
if (cached !== undefined) {
if (cached !== null) throw cached
return return
} }
...@@ -82,78 +91,36 @@ function loadRuntimeChunkPath( ...@@ -82,78 +91,36 @@ function loadRuntimeChunkPath(
const resolved = path.resolve(RUNTIME_ROOT, chunkPath) const resolved = path.resolve(RUNTIME_ROOT, chunkPath)
const chunkModules: CompressedModuleFactories = require(resolved) const chunkModules: CompressedModuleFactories = require(resolved)
installCompressedModuleFactories(chunkModules, 0, moduleFactories) installCompressedModuleFactories(chunkModules, 0, moduleFactories)
loadedChunks.add(chunkPath) loadedChunks.set(chunkPath, null)
} catch (cause) { } catch (cause) {
let errorMessage = `Failed to load chunk ${chunkPath}` const error = new Error(
sourceIsModule
if (sourcePath) { ? `Failed to load chunk ${chunkPath} from module ${sourceId}`
errorMessage += ` from runtime for chunk ${sourcePath}` : `Failed to load chunk ${chunkPath} from runtime for chunk ${sourceId}`,
} { cause }
)
const error = new Error(errorMessage, { cause })
error.name = 'ChunkLoadError' error.name = 'ChunkLoadError'
loadedChunks.set(chunkPath, error)
throw error throw error
} }
} }
/** function loadRuntimeChunkPath(
* Synchronously loads a chunk using require(). On Node.js, chunk loading is sourcePath: ChunkPath,
* always synchronous (file I/O via require), so there's no need for promises. chunkPath: ChunkPath
* Throws on failure. ): void {
*/ loadChunkPath(chunkPath, sourcePath, false)
}
function loadChunkSync<TModule extends Module>( function loadChunkSync<TModule extends Module>(
this: TurbopackBaseContext<TModule>, this: TurbopackBaseContext<TModule>,
chunkData: ChunkData chunkData: ChunkData
): void { ): void {
const chunkPath = typeof chunkData === 'string' ? chunkData : chunkData.path const chunkPath = typeof chunkData === 'string' ? chunkData : chunkData.path
if (!isJs(chunkPath)) { loadChunkPath(chunkPath, this.m.id, true)
// We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk.
return
}
if (loadedChunks.has(chunkPath)) {
return
}
const cachedError = chunkErrors.get(chunkPath)
if (cachedError !== undefined) {
throw cachedError
}
try {
const resolved = path.resolve(RUNTIME_ROOT, chunkPath)
const chunkModules: CompressedModuleFactories = require(resolved)
installCompressedModuleFactories(chunkModules, 0, moduleFactories)
loadedChunks.add(chunkPath)
} catch (cause) {
const errorMessage = `Failed to load chunk ${chunkPath} from module ${this.m.id}`
const error = new Error(errorMessage, { cause })
error.name = 'ChunkLoadError'
// Cache the failure, future requests will also get this same error
chunkErrors.set(chunkPath, error)
throw error
}
} }
contextPrototype.l = loadChunkSync contextPrototype.l = loadChunkSync
// Override the shared asyncLoader with an async version for Node.js.
// In the Node.js runtime, chunk loading and module evaluation are synchronous,
// so the loader may return a plain value or throw. Since dynamic import() must
// always return a Promise per the spec, wrapping in async ensures both success
// values and errors are properly wrapped in a Promise.
async function asyncLoaderNodeJs(
this: TurbopackBaseContext<Module>,
moduleId: ModuleId
): Promise<Exports> {
const loader = this.r(moduleId) as (
importFunction: EsmImport
) => Exports | Promise<Exports>
return loader(esmImport.bind(this))
}
contextPrototype.A = asyncLoaderNodeJs
function loadChunkSyncByUrl<TModule extends Module>( function loadChunkSyncByUrl<TModule extends Module>(
this: TurbopackBaseContext<TModule>, this: TurbopackBaseContext<TModule>,
chunkUrl: string chunkUrl: string
......
...@@ -56,3 +56,18 @@ externalRequire.resolve = ( ...@@ -56,3 +56,18 @@ externalRequire.resolve = (
return require.resolve(id, options) return require.resolve(id, options)
} }
contextPrototype.x = externalRequire contextPrototype.x = externalRequire
// Override the shared asyncLoader with an async version for Node.js/Edge.
// Chunk loading is synchronous in these runtimes, so loaders may return plain
// values or throw. The async keyword ensures dynamic import() always returns
// a Promise per spec without an extra microtask in the browser path.
async function asyncLoaderSync(
this: TurbopackBaseContext<Module>,
moduleId: ModuleId
): Promise<Exports> {
const loader = this.r(moduleId) as (
importFunction: EsmImport
) => Exports | Promise<Exports>
return loader(esmImport.bind(this))
}
contextPrototype.A = asyncLoaderSync
...@@ -410,6 +410,10 @@ function esmImport( ...@@ -410,6 +410,10 @@ function esmImport(
} }
contextPrototype.i = esmImport contextPrototype.i = esmImport
// Base async loader for dynamic import(). In the browser, chunk loading is
// naturally async so loaders already return Promises — no extra wrapping needed.
// Node.js/Edge runtimes override this with an async version (in
// base-externals-utils.ts) to enforce Promise semantics over sync loaders.
function asyncLoader( function asyncLoader(
this: TurbopackBaseContext<Module>, this: TurbopackBaseContext<Module>,
moduleId: ModuleId moduleId: ModuleId
......
...@@ -195,12 +195,12 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule { ...@@ -195,12 +195,12 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule {
.map(|chunk_data| EcmascriptChunkData::new(chunk_data)) .map(|chunk_data| EcmascriptChunkData::new(chunk_data))
.collect(); .collect();
let is_sync = matches!( let is_sync_chunk_loading = matches!(
*chunking_context.environment().chunk_loading().await?, *chunking_context.environment().chunk_loading().await?,
ChunkLoading::NodeJs | ChunkLoading::Edge ChunkLoading::NodeJs | ChunkLoading::Edge
); );
let code = if is_sync { let code = if is_sync_chunk_loading {
// Node.js/Edge: chunk loading is synchronous (require() on Node.js, // Node.js/Edge: chunk loading is synchronous (require() on Node.js,
// pre-bundled on Edge), so we can avoid all promise overhead. // pre-bundled on Edge), so we can avoid all promise overhead.
match (id, chunks_data.is_empty()) { match (id, chunks_data.is_empty()) {
...@@ -218,7 +218,10 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule { ...@@ -218,7 +218,10 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule {
formatdoc! { formatdoc! {
r#" r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{ {TURBOPACK_EXPORT_VALUE}((parentImport) => {{
{chunks:#}.forEach((chunk) => {TURBOPACK_LOAD}(chunk)); var chunks = {chunks:#};
for (var i = 0; i < chunks.length; i++) {{
{TURBOPACK_LOAD}(chunks[i]);
}}
return parentImport({id}); return parentImport({id});
}}); }});
"#, "#,
...@@ -237,7 +240,10 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule { ...@@ -237,7 +240,10 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule {
formatdoc! { formatdoc! {
r#" r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{ {TURBOPACK_EXPORT_VALUE}((parentImport) => {{
{chunks:#}.forEach((chunk) => {TURBOPACK_LOAD}(chunk)); var chunks = {chunks:#};
for (var i = 0; i < chunks.length; i++) {{
{TURBOPACK_LOAD}(chunks[i]);
}}
}}); }});
"#, "#,
chunks = StringifyJs(&chunks_data), chunks = StringifyJs(&chunks_data),
...@@ -251,11 +257,9 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule { ...@@ -251,11 +257,9 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule {
(Some(id), 0) => { (Some(id), 0) => {
formatdoc! { formatdoc! {
r#" r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{ {TURBOPACK_EXPORT_VALUE}(async (parentImport) => {{
return Promise.resolve().then(() => {{
return parentImport({id}); return parentImport({id});
}}); }});
}});
"#, "#,
id = StringifyModuleId(id), id = StringifyModuleId(id),
} }
...@@ -287,9 +291,7 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule { ...@@ -287,9 +291,7 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule {
(None, 0) => { (None, 0) => {
formatdoc! { formatdoc! {
r#" r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{ {TURBOPACK_EXPORT_VALUE}(async (parentImport) => {{}});
return Promise.resolve();
}});
"#, "#,
} }
} }
......
...@@ -160,12 +160,12 @@ impl EcmascriptChunkPlaceable for ManifestLoaderModule { ...@@ -160,12 +160,12 @@ impl EcmascriptChunkPlaceable for ManifestLoaderModule {
.map(|chunk_data| EcmascriptChunkData::new(chunk_data)) .map(|chunk_data| EcmascriptChunkData::new(chunk_data))
.collect(); .collect();
let is_sync = matches!( let is_sync_chunk_loading = matches!(
*chunking_context.environment().chunk_loading().await?, *chunking_context.environment().chunk_loading().await?,
ChunkLoading::NodeJs | ChunkLoading::Edge ChunkLoading::NodeJs | ChunkLoading::Edge
); );
if is_sync { if is_sync_chunk_loading {
// Node.js/Edge: all chunk loading is synchronous (require() on Node.js, // Node.js/Edge: all chunk loading is synchronous (require() on Node.js,
// pre-bundled on Edge), so we can avoid all promise overhead. Load // pre-bundled on Edge), so we can avoid all promise overhead. Load
// chunks, require manifest, load dynamic chunks, import target — all // chunks, require manifest, load dynamic chunks, import target — all
...@@ -174,7 +174,10 @@ impl EcmascriptChunkPlaceable for ManifestLoaderModule { ...@@ -174,7 +174,10 @@ impl EcmascriptChunkPlaceable for ManifestLoaderModule {
code, code,
r#" r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{ {TURBOPACK_EXPORT_VALUE}((parentImport) => {{
{chunks_server_data:#}.forEach((chunk) => {TURBOPACK_LOAD}(chunk)); var serverChunks = {chunks_server_data:#};
for (var i = 0; i < serverChunks.length; i++) {{
{TURBOPACK_LOAD}(serverChunks[i]);
}}
var chunks = {TURBOPACK_REQUIRE}({item_id}); var chunks = {TURBOPACK_REQUIRE}({item_id});
for (var i = 0; i < chunks.length; i++) {{ for (var i = 0; i < chunks.length; i++) {{
{TURBOPACK_LOAD}(chunks[i]); {TURBOPACK_LOAD}(chunks[i]);
......
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