Skip to content

Commit 1aa554f

Browse files
authored
add getChunkPath method (vercel/turborepo#4600)
### Description follow up changes from vercel/turborepo#4488
1 parent 06cb98b commit 1aa554f

File tree

36 files changed

+380
-292
lines changed

36 files changed

+380
-292
lines changed

crates/turbopack-dev/js/src/runtime.dom.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ let BACKEND;
1717
}
1818

1919
for (const otherChunkData of params.otherChunks) {
20-
const otherChunkPath =
21-
typeof otherChunkData === "string"
22-
? otherChunkData
23-
: otherChunkData.path;
20+
const otherChunkPath = getChunkPath(otherChunkData);
2421
if (otherChunkPath.endsWith(".css")) {
2522
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
2623
// They are just injected as <link> tag and have to way to communicate completion.

crates/turbopack-dev/js/src/runtime.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
13351335
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
13361336
}
13371337

1338+
/**
1339+
* Returns the path of a chunk defined by its data.
1340+
*
1341+
* @param {ChunkData} chunkData
1342+
* @returns {ChunkPath} the chunk path
1343+
*/
1344+
function getChunkPath(chunkData) {
1345+
return typeof chunkData === "string" ? chunkData : chunkData.path;
1346+
}
1347+
13381348
/**
13391349
* Subscribes to chunk list updates from the update server and applies them.
13401350
*
@@ -1347,11 +1357,7 @@ function registerChunkList(chunkList) {
13471357
]);
13481358

13491359
// Adding chunks to chunk lists and vice versa.
1350-
const chunks = new Set(
1351-
chunkList.chunks.map((chunkData) =>
1352-
typeof chunkData === "string" ? chunkData : chunkData.path
1353-
)
1354-
);
1360+
const chunks = new Set(chunkList.chunks.map(getChunkPath));
13551361
chunkListChunksMap.set(chunkList.path, chunks);
13561362
for (const chunkPath of chunks) {
13571363
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);

crates/turbopack-dev/js/src/runtime.nodejs.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,10 @@ let BACKEND;
1414

1515
if (params.runtimeModuleIds.length > 0) {
1616
for (const otherChunkData of params.otherChunks) {
17-
loadChunk(
18-
typeof otherChunkData === "string"
19-
? otherChunkData
20-
: otherChunkData.path,
21-
{
22-
type: SourceTypeRuntime,
23-
chunkPath,
24-
}
25-
);
17+
loadChunk(getChunkPath(otherChunkData), {
18+
type: SourceTypeRuntime,
19+
chunkPath,
20+
});
2621
}
2722

2823
for (const moduleId of params.runtimeModuleIds) {

crates/turbopack-dev/js/src/runtime.none.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ let BACKEND;
6868
};
6969

7070
for (const otherChunkData of otherChunks) {
71-
const otherChunkPath =
72-
typeof otherChunkData === "string"
73-
? otherChunkData
74-
: otherChunkData.path;
71+
const otherChunkPath = getChunkPath(otherChunkData);
7572
if (registeredChunks.has(otherChunkPath)) {
7673
continue;
7774
}

crates/turbopack-dev/js/types/backend.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare global {
1212
declare const RUNTIME_PARAMS: DevRuntimeParams;
1313
declare const getFirstModuleChunk: GetFirstModuleChunk;
1414
declare const getOrInstantiateRuntimeModule: GetOrInstantiateRuntimeModule;
15+
declare const getChunkPath: GetChunkPath;
1516
declare const loadChunk: InternalLoadChunk;
1617
declare const SourceTypeRuntime: SourceType.Runtime;
1718
declare const SourceTypeParent: SourceType.Parent;

crates/turbopack-dev/js/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ export interface TurbopackGlobals {
157157
TURBOPACK_CHUNK_LISTS?: ChunkList[];
158158
}
159159

160+
export type GetChunkPath = (chunkData: ChunkData) => ChunkPath;
161+
160162
export type GetFirstModuleChunk = (moduleId: ModuleId) => ChunkPath | null;
161163
export type GetOrInstantiateRuntimeModule = (
162164
moduleId: ModuleId,

crates/turbopack-tests/tests/snapshot/basic/async_chunk/output/crates_turbopack-tests_tests_snapshot_basic_async_chunk_input_index_0d348e.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
13441344
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
13451345
}
13461346

1347+
/**
1348+
* Returns the path of a chunk defined by its data.
1349+
*
1350+
* @param {ChunkData} chunkData
1351+
* @returns {ChunkPath} the chunk path
1352+
*/
1353+
function getChunkPath(chunkData) {
1354+
return typeof chunkData === "string" ? chunkData : chunkData.path;
1355+
}
1356+
13471357
/**
13481358
* Subscribes to chunk list updates from the update server and applies them.
13491359
*
@@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
13561366
]);
13571367

13581368
// Adding chunks to chunk lists and vice versa.
1359-
const chunks = new Set(
1360-
chunkList.chunks.map((chunkData) =>
1361-
typeof chunkData === "string" ? chunkData : chunkData.path
1362-
)
1363-
);
1369+
const chunks = new Set(chunkList.chunks.map(getChunkPath));
13641370
chunkListChunksMap.set(chunkList.path, chunks);
13651371
for (const chunkPath of chunks) {
13661372
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
@@ -1436,10 +1442,7 @@ let BACKEND;
14361442
}
14371443

14381444
for (const otherChunkData of params.otherChunks) {
1439-
const otherChunkPath =
1440-
typeof otherChunkData === "string"
1441-
? otherChunkData
1442-
: otherChunkData.path;
1445+
const otherChunkPath = getChunkPath(otherChunkData);
14431446
if (otherChunkPath.endsWith(".css")) {
14441447
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
14451448
// They are just injected as <link> tag and have to way to communicate completion.

crates/turbopack-tests/tests/snapshot/basic/chunked/output/crates_turbopack-tests_tests_snapshot_basic_chunked_input_index_e77e9f.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
13441344
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
13451345
}
13461346

1347+
/**
1348+
* Returns the path of a chunk defined by its data.
1349+
*
1350+
* @param {ChunkData} chunkData
1351+
* @returns {ChunkPath} the chunk path
1352+
*/
1353+
function getChunkPath(chunkData) {
1354+
return typeof chunkData === "string" ? chunkData : chunkData.path;
1355+
}
1356+
13471357
/**
13481358
* Subscribes to chunk list updates from the update server and applies them.
13491359
*
@@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
13561366
]);
13571367

13581368
// Adding chunks to chunk lists and vice versa.
1359-
const chunks = new Set(
1360-
chunkList.chunks.map((chunkData) =>
1361-
typeof chunkData === "string" ? chunkData : chunkData.path
1362-
)
1363-
);
1369+
const chunks = new Set(chunkList.chunks.map(getChunkPath));
13641370
chunkListChunksMap.set(chunkList.path, chunks);
13651371
for (const chunkPath of chunks) {
13661372
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
@@ -1436,10 +1442,7 @@ let BACKEND;
14361442
}
14371443

14381444
for (const otherChunkData of params.otherChunks) {
1439-
const otherChunkPath =
1440-
typeof otherChunkData === "string"
1441-
? otherChunkData
1442-
: otherChunkData.path;
1445+
const otherChunkPath = getChunkPath(otherChunkData);
14431446
if (otherChunkPath.endsWith(".css")) {
14441447
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
14451448
// They are just injected as <link> tag and have to way to communicate completion.

crates/turbopack-tests/tests/snapshot/basic/shebang/output/crates_turbopack-tests_tests_snapshot_basic_shebang_input_index_b1f0c2.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
13441344
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
13451345
}
13461346

1347+
/**
1348+
* Returns the path of a chunk defined by its data.
1349+
*
1350+
* @param {ChunkData} chunkData
1351+
* @returns {ChunkPath} the chunk path
1352+
*/
1353+
function getChunkPath(chunkData) {
1354+
return typeof chunkData === "string" ? chunkData : chunkData.path;
1355+
}
1356+
13471357
/**
13481358
* Subscribes to chunk list updates from the update server and applies them.
13491359
*
@@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
13561366
]);
13571367

13581368
// Adding chunks to chunk lists and vice versa.
1359-
const chunks = new Set(
1360-
chunkList.chunks.map((chunkData) =>
1361-
typeof chunkData === "string" ? chunkData : chunkData.path
1362-
)
1363-
);
1369+
const chunks = new Set(chunkList.chunks.map(getChunkPath));
13641370
chunkListChunksMap.set(chunkList.path, chunks);
13651371
for (const chunkPath of chunks) {
13661372
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
@@ -1436,10 +1442,7 @@ let BACKEND;
14361442
}
14371443

14381444
for (const otherChunkData of params.otherChunks) {
1439-
const otherChunkPath =
1440-
typeof otherChunkData === "string"
1441-
? otherChunkData
1442-
: otherChunkData.path;
1445+
const otherChunkPath = getChunkPath(otherChunkData);
14431446
if (otherChunkPath.endsWith(".css")) {
14441447
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
14451448
// They are just injected as <link> tag and have to way to communicate completion.

crates/turbopack-tests/tests/snapshot/comptime/define/output/crates_turbopack-tests_tests_snapshot_comptime_define_input_index_6b0d2b.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,16 @@ function getOrInstantiateRuntimeModule(moduleId, chunkPath) {
13441344
return instantiateModule(moduleId, { type: SourceTypeRuntime, chunkPath });
13451345
}
13461346

1347+
/**
1348+
* Returns the path of a chunk defined by its data.
1349+
*
1350+
* @param {ChunkData} chunkData
1351+
* @returns {ChunkPath} the chunk path
1352+
*/
1353+
function getChunkPath(chunkData) {
1354+
return typeof chunkData === "string" ? chunkData : chunkData.path;
1355+
}
1356+
13471357
/**
13481358
* Subscribes to chunk list updates from the update server and applies them.
13491359
*
@@ -1356,11 +1366,7 @@ function registerChunkList(chunkList) {
13561366
]);
13571367

13581368
// Adding chunks to chunk lists and vice versa.
1359-
const chunks = new Set(
1360-
chunkList.chunks.map((chunkData) =>
1361-
typeof chunkData === "string" ? chunkData : chunkData.path
1362-
)
1363-
);
1369+
const chunks = new Set(chunkList.chunks.map(getChunkPath));
13641370
chunkListChunksMap.set(chunkList.path, chunks);
13651371
for (const chunkPath of chunks) {
13661372
let chunkChunkLists = chunkChunkListsMap.get(chunkPath);
@@ -1436,10 +1442,7 @@ let BACKEND;
14361442
}
14371443

14381444
for (const otherChunkData of params.otherChunks) {
1439-
const otherChunkPath =
1440-
typeof otherChunkData === "string"
1441-
? otherChunkData
1442-
: otherChunkData.path;
1445+
const otherChunkPath = getChunkPath(otherChunkData);
14431446
if (otherChunkPath.endsWith(".css")) {
14441447
// Mark all CSS chunks within the same chunk group as this chunk as loaded.
14451448
// They are just injected as <link> tag and have to way to communicate completion.

0 commit comments

Comments
 (0)