Skip to content

Commit

Permalink
add getChunkPath method (vercel/turborepo#4600)
Browse files Browse the repository at this point in the history
### Description

follow up changes from vercel/turborepo#4488
  • Loading branch information
sokra authored Apr 19, 2023
1 parent e60ce2e commit 679a139
Show file tree
Hide file tree
Showing 36 changed files with 380 additions and 292 deletions.
5 changes: 1 addition & 4 deletions crates/turbopack-dev/js/src/runtime.dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ let BACKEND;
}

for (const otherChunkData of params.otherChunks) {
const otherChunkPath =
typeof otherChunkData === "string"
? otherChunkData
: otherChunkData.path;
const otherChunkPath = getChunkPath(otherChunkData);
if (otherChunkPath.endsWith(".css")) {
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
// They are just injected as <link> tag and have to way to communicate completion.
Expand Down
16 changes: 11 additions & 5 deletions crates/turbopack-dev/js/src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
}

/**
* Returns the path of a chunk defined by its data.
*
* @param {ChunkData} chunkData
* @returns {ChunkPath} the chunk path
*/
function getChunkPath(chunkData) {
return typeof chunkData === "string" ? chunkData : chunkData.path;
}

/**
* Subscribes to chunk list updates from the update server and applies them.
*
Expand All @@ -1347,11 +1357,7 @@ function registerChunkList(chunkList) {
]);

// Adding chunks to chunk lists and vice versa.
const chunks = new Set(
chunkList.chunks.map((chunkData) =>
typeof chunkData === "string" ? chunkData : chunkData.path
)
);
const chunks = new Set(chunkList.chunks.map(getChunkPath));
chunkListChunksMap.set(chunkList.path, chunks);
for (const chunkPath of chunks) {
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
Expand Down
13 changes: 4 additions & 9 deletions crates/turbopack-dev/js/src/runtime.nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ let BACKEND;

if (params.runtimeModuleIds.length > 0) {
for (const otherChunkData of params.otherChunks) {
loadChunk(
typeof otherChunkData === "string"
? otherChunkData
: otherChunkData.path,
{
type: SourceTypeRuntime,
chunkPath,
}
);
loadChunk(getChunkPath(otherChunkData), {
type: SourceTypeRuntime,
chunkPath,
});
}

for (const moduleId of params.runtimeModuleIds) {
Expand Down
5 changes: 1 addition & 4 deletions crates/turbopack-dev/js/src/runtime.none.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ let BACKEND;
};

for (const otherChunkData of otherChunks) {
const otherChunkPath =
typeof otherChunkData === "string"
? otherChunkData
: otherChunkData.path;
const otherChunkPath = getChunkPath(otherChunkData);
if (registeredChunks.has(otherChunkPath)) {
continue;
}
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-dev/js/types/backend.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare global {
declare const RUNTIME_PARAMS: DevRuntimeParams;
declare const getFirstModuleChunk: GetFirstModuleChunk;
declare const getOrInstantiateRuntimeModule: GetOrInstantiateRuntimeModule;
declare const getChunkPath: GetChunkPath;
declare const loadChunk: InternalLoadChunk;
declare const SourceTypeRuntime: SourceType.Runtime;
declare const SourceTypeParent: SourceType.Parent;
Expand Down
2 changes: 2 additions & 0 deletions crates/turbopack-dev/js/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export interface TurbopackGlobals {
TURBOPACK_CHUNK_LISTS?: ChunkList[];
}

export type GetChunkPath = (chunkData: ChunkData) => ChunkPath;

export type GetFirstModuleChunk = (moduleId: ModuleId) => ChunkPath | null;
export type GetOrInstantiateRuntimeModule = (
moduleId: ModuleId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
}

/**
* Returns the path of a chunk defined by its data.
*
* @param {ChunkData} chunkData
* @returns {ChunkPath} the chunk path
*/
function getChunkPath(chunkData) {
return typeof chunkData === "string" ? chunkData : chunkData.path;
}

/**
* Subscribes to chunk list updates from the update server and applies them.
*
Expand All @@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
]);

// Adding chunks to chunk lists and vice versa.
const chunks = new Set(
chunkList.chunks.map((chunkData) =>
typeof chunkData === "string" ? chunkData : chunkData.path
)
);
const chunks = new Set(chunkList.chunks.map(getChunkPath));
chunkListChunksMap.set(chunkList.path, chunks);
for (const chunkPath of chunks) {
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
Expand Down Expand Up @@ -1436,10 +1442,7 @@ let BACKEND;
}

for (const otherChunkData of params.otherChunks) {
const otherChunkPath =
typeof otherChunkData === "string"
? otherChunkData
: otherChunkData.path;
const otherChunkPath = getChunkPath(otherChunkData);
if (otherChunkPath.endsWith(".css")) {
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
// They are just injected as <link> tag and have to way to communicate completion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
}

/**
* Returns the path of a chunk defined by its data.
*
* @param {ChunkData} chunkData
* @returns {ChunkPath} the chunk path
*/
function getChunkPath(chunkData) {
return typeof chunkData === "string" ? chunkData : chunkData.path;
}

/**
* Subscribes to chunk list updates from the update server and applies them.
*
Expand All @@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
]);

// Adding chunks to chunk lists and vice versa.
const chunks = new Set(
chunkList.chunks.map((chunkData) =>
typeof chunkData === "string" ? chunkData : chunkData.path
)
);
const chunks = new Set(chunkList.chunks.map(getChunkPath));
chunkListChunksMap.set(chunkList.path, chunks);
for (const chunkPath of chunks) {
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
Expand Down Expand Up @@ -1436,10 +1442,7 @@ let BACKEND;
}

for (const otherChunkData of params.otherChunks) {
const otherChunkPath =
typeof otherChunkData === "string"
? otherChunkData
: otherChunkData.path;
const otherChunkPath = getChunkPath(otherChunkData);
if (otherChunkPath.endsWith(".css")) {
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
// They are just injected as <link> tag and have to way to communicate completion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
}

/**
* Returns the path of a chunk defined by its data.
*
* @param {ChunkData} chunkData
* @returns {ChunkPath} the chunk path
*/
function getChunkPath(chunkData) {
return typeof chunkData === "string" ? chunkData : chunkData.path;
}

/**
* Subscribes to chunk list updates from the update server and applies them.
*
Expand All @@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
]);

// Adding chunks to chunk lists and vice versa.
const chunks = new Set(
chunkList.chunks.map((chunkData) =>
typeof chunkData === "string" ? chunkData : chunkData.path
)
);
const chunks = new Set(chunkList.chunks.map(getChunkPath));
chunkListChunksMap.set(chunkList.path, chunks);
for (const chunkPath of chunks) {
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
Expand Down Expand Up @@ -1436,10 +1442,7 @@ let BACKEND;
}

for (const otherChunkData of params.otherChunks) {
const otherChunkPath =
typeof otherChunkData === "string"
? otherChunkData
: otherChunkData.path;
const otherChunkPath = getChunkPath(otherChunkData);
if (otherChunkPath.endsWith(".css")) {
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
// They are just injected as <link> tag and have to way to communicate completion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
}

/**
* Returns the path of a chunk defined by its data.
*
* @param {ChunkData} chunkData
* @returns {ChunkPath} the chunk path
*/
function getChunkPath(chunkData) {
return typeof chunkData === "string" ? chunkData : chunkData.path;
}

/**
* Subscribes to chunk list updates from the update server and applies them.
*
Expand All @@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
]);

// Adding chunks to chunk lists and vice versa.
const chunks = new Set(
chunkList.chunks.map((chunkData) =>
typeof chunkData === "string" ? chunkData : chunkData.path
)
);
const chunks = new Set(chunkList.chunks.map(getChunkPath));
chunkListChunksMap.set(chunkList.path, chunks);
for (const chunkPath of chunks) {
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
Expand Down Expand Up @@ -1436,10 +1442,7 @@ let BACKEND;
}

for (const otherChunkData of params.otherChunks) {
const otherChunkPath =
typeof otherChunkData === "string"
? otherChunkData
: otherChunkData.path;
const otherChunkPath = getChunkPath(otherChunkData);
if (otherChunkPath.endsWith(".css")) {
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
// They are just injected as <link> tag and have to way to communicate completion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
}

/**
* Returns the path of a chunk defined by its data.
*
* @param {ChunkData} chunkData
* @returns {ChunkPath} the chunk path
*/
function getChunkPath(chunkData) {
return typeof chunkData === "string" ? chunkData : chunkData.path;
}

/**
* Subscribes to chunk list updates from the update server and applies them.
*
Expand All @@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
]);

// Adding chunks to chunk lists and vice versa.
const chunks = new Set(
chunkList.chunks.map((chunkData) =>
typeof chunkData === "string" ? chunkData : chunkData.path
)
);
const chunks = new Set(chunkList.chunks.map(getChunkPath));
chunkListChunksMap.set(chunkList.path, chunks);
for (const chunkPath of chunks) {
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
Expand Down Expand Up @@ -1436,10 +1442,7 @@ let BACKEND;
}

for (const otherChunkData of params.otherChunks) {
const otherChunkPath =
typeof otherChunkData === "string"
? otherChunkData
: otherChunkData.path;
const otherChunkPath = getChunkPath(otherChunkData);
if (otherChunkPath.endsWith(".css")) {
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
// They are just injected as <link> tag and have to way to communicate completion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
}

/**
* Returns the path of a chunk defined by its data.
*
* @param {ChunkData} chunkData
* @returns {ChunkPath} the chunk path
*/
function getChunkPath(chunkData) {
return typeof chunkData === "string" ? chunkData : chunkData.path;
}

/**
* Subscribes to chunk list updates from the update server and applies them.
*
Expand All @@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
]);

// Adding chunks to chunk lists and vice versa.
const chunks = new Set(
chunkList.chunks.map((chunkData) =>
typeof chunkData === "string" ? chunkData : chunkData.path
)
);
const chunks = new Set(chunkList.chunks.map(getChunkPath));
chunkListChunksMap.set(chunkList.path, chunks);
for (const chunkPath of chunks) {
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
Expand Down Expand Up @@ -1436,10 +1442,7 @@ let BACKEND;
}

for (const otherChunkData of params.otherChunks) {
const otherChunkPath =
typeof otherChunkData === "string"
? otherChunkData
: otherChunkData.path;
const otherChunkPath = getChunkPath(otherChunkData);
if (otherChunkPath.endsWith(".css")) {
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
// They are just injected as <link> tag and have to way to communicate completion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
}

/**
* Returns the path of a chunk defined by its data.
*
* @param {ChunkData} chunkData
* @returns {ChunkPath} the chunk path
*/
function getChunkPath(chunkData) {
return typeof chunkData === "string" ? chunkData : chunkData.path;
}

/**
* Subscribes to chunk list updates from the update server and applies them.
*
Expand All @@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
]);

// Adding chunks to chunk lists and vice versa.
const chunks = new Set(
chunkList.chunks.map((chunkData) =>
typeof chunkData === "string" ? chunkData : chunkData.path
)
);
const chunks = new Set(chunkList.chunks.map(getChunkPath));
chunkListChunksMap.set(chunkList.path, chunks);
for (const chunkPath of chunks) {
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
Expand Down Expand Up @@ -1436,10 +1442,7 @@ let BACKEND;
}

for (const otherChunkData of params.otherChunks) {
const otherChunkPath =
typeof otherChunkData === "string"
? otherChunkData
: otherChunkData.path;
const otherChunkPath = getChunkPath(otherChunkData);
if (otherChunkPath.endsWith(".css")) {
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
// They are just injected as <link> tag and have to way to communicate completion.
Expand Down
Loading

0 comments on commit 679a139

Please sign in to comment.