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 = {
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
;(() => {
BACKEND = {
......
......@@ -56,17 +56,24 @@ function loadRuntimeChunk(sourcePath: ChunkPath, chunkData: ChunkData): void {
}
}
const loadedChunks = new Set<ChunkPath>()
const chunkErrors = new Map<ChunkPath, Error>()
// null = loaded successfully, Error = load failed (cached for future requests)
const loadedChunks = new Map<ChunkPath, Error | null>()
function clearChunkCache() {
loadedChunks.clear()
chunkErrors.clear()
}
function loadRuntimeChunkPath(
sourcePath: ChunkPath,
chunkPath: ChunkPath
/**
* Synchronously loads a chunk using require(). Caches results so a chunk is
* 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 {
if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js.
......@@ -74,7 +81,9 @@ function loadRuntimeChunkPath(
return
}
if (loadedChunks.has(chunkPath)) {
const cached = loadedChunks.get(chunkPath)
if (cached !== undefined) {
if (cached !== null) throw cached
return
}
......@@ -82,78 +91,36 @@ function loadRuntimeChunkPath(
const resolved = path.resolve(RUNTIME_ROOT, chunkPath)
const chunkModules: CompressedModuleFactories = require(resolved)
installCompressedModuleFactories(chunkModules, 0, moduleFactories)
loadedChunks.add(chunkPath)
loadedChunks.set(chunkPath, null)
} catch (cause) {
let errorMessage = `Failed to load chunk ${chunkPath}`
if (sourcePath) {
errorMessage += ` from runtime for chunk ${sourcePath}`
}
const error = new Error(errorMessage, { cause })
const error = new Error(
sourceIsModule
? `Failed to load chunk ${chunkPath} from module ${sourceId}`
: `Failed to load chunk ${chunkPath} from runtime for chunk ${sourceId}`,
{ cause }
)
error.name = 'ChunkLoadError'
loadedChunks.set(chunkPath, error)
throw error
}
}
/**
* Synchronously loads a chunk using require(). On Node.js, chunk loading is
* always synchronous (file I/O via require), so there's no need for promises.
* Throws on failure.
*/
function loadRuntimeChunkPath(
sourcePath: ChunkPath,
chunkPath: ChunkPath
): void {
loadChunkPath(chunkPath, sourcePath, false)
}
function loadChunkSync<TModule extends Module>(
this: TurbopackBaseContext<TModule>,
chunkData: ChunkData
): void {
const chunkPath = typeof chunkData === 'string' ? chunkData : chunkData.path
if (!isJs(chunkPath)) {
// 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
}
loadChunkPath(chunkPath, this.m.id, true)
}
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>(
this: TurbopackBaseContext<TModule>,
chunkUrl: string
......
......@@ -56,3 +56,18 @@ externalRequire.resolve = (
return require.resolve(id, options)
}
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(
}
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(
this: TurbopackBaseContext<Module>,
moduleId: ModuleId
......
......@@ -195,12 +195,12 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule {
.map(|chunk_data| EcmascriptChunkData::new(chunk_data))
.collect();
let is_sync = matches!(
let is_sync_chunk_loading = matches!(
*chunking_context.environment().chunk_loading().await?,
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,
// pre-bundled on Edge), so we can avoid all promise overhead.
match (id, chunks_data.is_empty()) {
......@@ -218,7 +218,10 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule {
formatdoc! {
r#"
{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});
}});
"#,
......@@ -237,7 +240,10 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule {
formatdoc! {
r#"
{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),
......@@ -251,10 +257,8 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule {
(Some(id), 0) => {
formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.resolve().then(() => {{
return parentImport({id});
}});
{TURBOPACK_EXPORT_VALUE}(async (parentImport) => {{
return parentImport({id});
}});
"#,
id = StringifyModuleId(id),
......@@ -287,9 +291,7 @@ impl EcmascriptChunkPlaceable for AsyncLoaderModule {
(None, 0) => {
formatdoc! {
r#"
{TURBOPACK_EXPORT_VALUE}((parentImport) => {{
return Promise.resolve();
}});
{TURBOPACK_EXPORT_VALUE}(async (parentImport) => {{}});
"#,
}
}
......
......@@ -160,12 +160,12 @@ impl EcmascriptChunkPlaceable for ManifestLoaderModule {
.map(|chunk_data| EcmascriptChunkData::new(chunk_data))
.collect();
let is_sync = matches!(
let is_sync_chunk_loading = matches!(
*chunking_context.environment().chunk_loading().await?,
ChunkLoading::NodeJs | ChunkLoading::Edge
);
if is_sync {
if is_sync_chunk_loading {
// Node.js/Edge: all chunk loading is synchronous (require() on Node.js,
// pre-bundled on Edge), so we can avoid all promise overhead. Load
// chunks, require manifest, load dynamic chunks, import target — all
......@@ -174,7 +174,10 @@ impl EcmascriptChunkPlaceable for ManifestLoaderModule {
code,
r#"
{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});
for (var i = 0; i < chunks.length; 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