Skip to content

Commit afd0186

Browse files
authored
Merge branch 'next' into refactor/catalog-edge-resolve
2 parents 17ecf3d + 990c436 commit afd0186

22 files changed

Lines changed: 348 additions & 1209 deletions

File tree

biome.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"!packages/utoo-web/src/webpackLoaders/loaders/**",
1919
"!packages/utoo-web/src/utoo/**",
2020
"crates/pack-core/js/**/*.ts",
21+
"!crates/pack-core/js/src/umd/**",
2122
"!**/examples/multi-entries-heavy/*",
2223
"!**/packages/**/binding.*",
2324
"!next.js/**",

crates/pack-core/js/src/umd/base-externals-utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars */
22

3-
/// <reference path="./runtime-utils.ts" />
4-
53
/// A 'base' utilities to support runtime can have externals.
64
/// Currently this is for node.js / edge runtime both.
75
/// If a fn requires node.js specific behavior, it should be placed in `node-external-utils` instead.

crates/pack-core/js/src/umd/build-base.ts

Lines changed: 0 additions & 98 deletions
This file was deleted.

crates/pack-core/js/src/umd/runtime-backend-dom.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ const chunkResolvers: Map<ChunkUrl, ChunkResolver> = new Map();
5858
*/
5959
loadChunkCached(
6060
sourceType: SourceType,
61-
sourceData: SourceData,
6261
chunkUrl: ChunkUrl,
6362
) {
64-
return doLoadChunk(sourceType, sourceData, chunkUrl);
63+
return doLoadChunk(sourceType, chunkUrl);
6564
},
6665
};
6766
function getOrCreateResolver(chunkUrl: ChunkUrl): ChunkResolver {
@@ -94,7 +93,6 @@ const chunkResolvers: Map<ChunkUrl, ChunkResolver> = new Map();
9493
*/
9594
function doLoadChunk(
9695
sourceType: SourceType,
97-
_sourceData: SourceData,
9896
chunkUrl: ChunkUrl,
9997
) {
10098
const resolver = getOrCreateResolver(chunkUrl);

crates/pack-core/js/src/umd/runtime-backend-node.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ let BACKEND: RuntimeBackend;
3737
*/
3838
loadChunkCached(
3939
_sourceType: SourceType,
40-
_sourceData: SourceData,
4140
_chunkUrl: ChunkUrl,
4241
) {
4342
return Promise.resolve();

crates/pack-core/js/src/umd/runtime-base.ts

Lines changed: 33 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
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
1516
declare 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+
4349
type ChunkRegistration = [
44-
chunkPath: ChunkScript,
45-
chunkModules: CompressedModuleFactories,
46-
params: RuntimeParams | undefined,
50+
chunkPath: ChunkRegistrationChunk,
51+
...([RuntimeParams] | CompressedModuleFactories),
4752
];
4853

4954
type 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
7661
interface 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

9377
const 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-
12279
const loadedChunk = Promise.resolve(undefined);
12380
const 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

309250
const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
310251
/**
@@ -313,3 +254,22 @@ const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
313254
function 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

Comments
 (0)