Skip to content

Commit

Permalink
fix chunk loading in build runtime (vercel/turborepo#6180)
Browse files Browse the repository at this point in the history
### Description

fix handling of ChunkData in build runtime

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->


Closes WEB-1788
  • Loading branch information
sokra committed Oct 17, 2023
1 parent 750f81e commit 5e5cc19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
17 changes: 14 additions & 3 deletions crates/turbopack-ecmascript-runtime/js/src/build/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ const RUNTIME_ROOT = path.resolve(__filename, relativePathToRuntimeRoot);
const moduleFactories: ModuleFactories = Object.create(null);
const moduleCache: ModuleCache = Object.create(null);

function loadChunk(chunkPath: ChunkPath) {
function loadChunk(chunkData: ChunkData): void {
if (typeof chunkData === "string") {
return loadChunkPath(chunkData);
} else {
return loadChunkPath(chunkData.path);
}
}

function loadChunkPath(chunkPath: ChunkPath): void {
if (!chunkPath.endsWith(".js")) {
// We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk.
Expand All @@ -62,10 +70,13 @@ function loadChunk(chunkPath: ChunkPath) {
}
}

function loadChunkAsync(source: SourceInfo, chunkPath: string): Promise<void> {
async function loadChunkAsync(
source: SourceInfo,
chunkData: ChunkData
): Promise<any> {
return new Promise<void>((resolve, reject) => {
try {
loadChunk(chunkPath);
loadChunk(chunkData);
} catch (err) {
reject(err);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,14 @@ const relativePathToRuntimeRoot = path.relative(RUNTIME_PUBLIC_PATH, ".");
const RUNTIME_ROOT = path.resolve(__filename, relativePathToRuntimeRoot);
const moduleFactories = Object.create(null);
const moduleCache = Object.create(null);
function loadChunk(chunkPath) {
function loadChunk(chunkData) {
if (typeof chunkData === "string") {
return loadChunkPath(chunkData);
} else {
return loadChunkPath(chunkData.path);
}
}
function loadChunkPath(chunkPath) {
if (!chunkPath.endsWith(".js")) {
// We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk.
Expand All @@ -333,10 +340,10 @@ function loadChunk(chunkPath) {
}
}
}
function loadChunkAsync(source, chunkPath) {
async function loadChunkAsync(source, chunkData) {
return new Promise((resolve, reject)=>{
try {
loadChunk(chunkPath);
loadChunk(chunkData);
} catch (err) {
reject(err);
return;
Expand Down
Loading

0 comments on commit 5e5cc19

Please sign in to comment.