99/* eslint-disable @typescript-eslint/no-unused-vars */
1010
1111/// <reference path="./globals.d.ts" />
12- /// <reference path="./runtime-utils.ts" />
12+ /// <reference path="../../../../../next.js/turbopack/crates/turbopack-ecmascript-runtime/js/src/shared/runtime/runtime-utils.ts" />
13+
1314
1415// Used in WebWorkers to tell the runtime about the chunk base path
1516declare var TURBOPACK_WORKER_LOCATION : string ;
@@ -40,47 +41,30 @@ type RuntimeParams = {
4041 runtimeModuleIds : ModuleId [ ] ;
4142} ;
4243
44+ type ChunkRegistrationChunk =
45+ | ChunkPath
46+ | { getAttribute : ( name : string ) => string | null }
47+ | undefined ;
48+
4349type ChunkRegistration = [
44- chunkPath : ChunkScript ,
45- chunkModules : CompressedModuleFactories ,
46- params : RuntimeParams | undefined ,
50+ chunkPath : ChunkRegistrationChunk ,
51+ ...( [ RuntimeParams ] | CompressedModuleFactories ) ,
4752] ;
4853
4954type ChunkList = {
50- script : ChunkListScript ;
55+ script : ChunkRegistrationChunk ;
5156 chunks : ChunkData [ ] ;
5257 source : "entry" | "dynamic" ;
5358} ;
5459
55- enum SourceType {
56- /**
57- * The module was instantiated because it was included in an evaluated chunk's
58- * runtime.
59- * SourceData is a ChunkPath.
60- */
61- Runtime = 0 ,
62- /**
63- * The module was instantiated because a parent module imported it.
64- * SourceData is a ModuleId.
65- */
66- Parent = 1 ,
67- /**
68- * The module was instantiated because it was included in a chunk's hot module
69- * update.
70- * SourceData is an array of ModuleIds or undefined.
71- */
72- Update = 2 ,
73- }
74-
75- type SourceData = ChunkPath | ModuleId | ModuleId [ ] | undefined ;
60+ // SourceType and SourceData are provided by shared/runtime/runtime-utils.ts
7661interface RuntimeBackend {
77- registerChunk : ( chunkPath : ChunkPath , params ?: RuntimeParams ) => void ;
62+ registerChunk : ( chunkPath : ChunkPath | ChunkScript , params ?: RuntimeParams ) => void ;
7863 /**
7964 * Returns the same Promise for the same chunk URL.
8065 */
8166 loadChunkCached : (
8267 sourceType : SourceType ,
83- sourceData : SourceData ,
8468 chunkUrl : ChunkUrl ,
8569 ) => Promise < void > ;
8670}
@@ -92,33 +76,6 @@ const availableModules: Map<ModuleId, Promise<any> | true> = new Map();
9276
9377const availableModuleChunks : Map < ChunkPath , Promise < any > | true > = new Map ( ) ;
9478
95- function factoryNotAvailable (
96- moduleId : ModuleId ,
97- sourceType : SourceType ,
98- sourceData : SourceData ,
99- ) {
100- let instantiationReason ;
101- switch ( sourceType ) {
102- case SourceType . Runtime :
103- instantiationReason = `as a runtime entry of chunk ${ sourceData } ` ;
104- break ;
105- case SourceType . Parent :
106- instantiationReason = `because it was required from module ${ sourceData } ` ;
107- break ;
108- case SourceType . Update :
109- instantiationReason = "because of an HMR update" ;
110- break ;
111- default :
112- invariant (
113- sourceType ,
114- ( sourceType ) => `Unknown source type: ${ sourceType } ` ,
115- ) ;
116- }
117- throw new Error (
118- `Module ${ moduleId } was instantiated ${ instantiationReason } , but the module factory is not available. It might have been deleted in an HMR update.` ,
119- ) ;
120- }
121-
12279const loadedChunk = Promise . resolve ( undefined ) ;
12380const instrumentedBackendLoadChunks = new WeakMap <
12481 Promise < any > ,
@@ -169,7 +126,7 @@ function loadChunkByUrlInternal(
169126 sourceData : SourceData ,
170127 chunkUrl : ChunkUrl ,
171128) : Promise < any > {
172- const thenable = BACKEND . loadChunkCached ( sourceType , sourceData , chunkUrl ) ;
129+ const thenable = BACKEND . loadChunkCached ( sourceType , chunkUrl ) ;
173130 let entry = instrumentedBackendLoadChunks . get ( thenable ) ;
174131 if ( entry === undefined ) {
175132 const resolve = instrumentedBackendLoadChunks . set . bind (
@@ -278,7 +235,7 @@ function getPathFromScript(
278235 const chunkUrl =
279236 typeof TURBOPACK_NEXT_CHUNK_URLS !== "undefined"
280237 ? TURBOPACK_NEXT_CHUNK_URLS . pop ( ) !
281- : chunkScript . getAttribute ( " src" ) ! ;
238+ : chunkScript . src ! ;
282239 const src = decodeURIComponent ( chunkUrl . replace ( / [ ? # ] .* $ / , "" ) ) ;
283240 let path = src . startsWith ( CHUNK_BASE_PATH )
284241 ? src . slice ( CHUNK_BASE_PATH . length )
@@ -289,22 +246,6 @@ function getPathFromScript(
289246 return path as ChunkPath | ChunkListPath ;
290247}
291248
292- function registerCompressedModuleFactory (
293- moduleId : ModuleId ,
294- moduleFactory : Function | [ Function , ModuleId [ ] ] ,
295- ) {
296- if ( ! moduleFactories . has ( moduleId ) ) {
297- if ( Array . isArray ( moduleFactory ) ) {
298- let [ moduleFactoryFn , otherIds ] = moduleFactory ;
299- moduleFactories . set ( moduleId , moduleFactoryFn ) ;
300- for ( const otherModuleId of otherIds ) {
301- moduleFactories . set ( otherModuleId , moduleFactoryFn ) ;
302- }
303- } else {
304- moduleFactories . set ( moduleId , moduleFactory ) ;
305- }
306- }
307- }
308249
309250const regexJsUrl = / \. j s (?: \? [ ^ # ] * ) ? (?: # .* ) ? $ / ;
310251/**
@@ -313,3 +254,22 @@ const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
313254function isJs ( chunkUrlOrPath : ChunkUrl | ChunkPath ) : boolean {
314255 return regexJsUrl . test ( chunkUrlOrPath ) ;
315256}
257+
258+ /**
259+ * Determine the chunk to register. Note that this function has side-effects!
260+ */
261+ function getChunkFromRegistration (
262+ chunk : ChunkRegistrationChunk ,
263+ ) : ChunkPath | CurrentScript {
264+ if ( typeof chunk === "string" ) {
265+ return chunk ;
266+ } else if ( ! chunk ) {
267+ if ( typeof TURBOPACK_NEXT_CHUNK_URLS !== "undefined" ) {
268+ return { src : TURBOPACK_NEXT_CHUNK_URLS . pop ( ) ! } as CurrentScript ;
269+ } else {
270+ throw new Error ( "chunk path empty but not in a worker" ) ;
271+ }
272+ } else {
273+ return { src : chunk . getAttribute ( "src" ) ! } as CurrentScript ;
274+ }
275+ }
0 commit comments