Skip to content

Commit dbea40c

Browse files
committed
fix(plugin-assets-retry): avoid __webpack_require__.e pollution
1 parent 327f460 commit dbea40c

File tree

1 file changed

+68
-16
lines changed

1 file changed

+68
-16
lines changed

packages/plugin-assets-retry/src/runtime/asyncChunkRetry.ts

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,30 @@ declare global {
1818
// RuntimeGlobals.require
1919
var __RUNTIME_GLOBALS_REQUIRE__: unknown;
2020
// RuntimeGlobals.ensure
21-
var __RUNTIME_GLOBALS_ENSURE_CHUNK__: (chunkId: ChunkId) => Promise<unknown>;
21+
var __RUNTIME_GLOBALS_ENSURE_CHUNK__: (
22+
chunkId: ChunkId,
23+
...args: unknown[]
24+
) => Promise<unknown>;
2225
// RuntimeGlobals.getChunkScriptFilename
2326
var __RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__: (
2427
chunkId: ChunkId,
28+
...args: unknown[]
2529
) => string;
2630
// RuntimeGlobals.getChunkCssFilename
2731
var __RUNTIME_GLOBALS_GET_CSS_FILENAME__:
28-
| ((chunkId: ChunkId) => string)
32+
| ((chunkId: ChunkId, ...args: unknown[]) => string)
2933
| undefined;
3034
// RuntimeGlobals.getChunkCssFilename when using Rspack.CssExtractPlugin
3135
var __RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__:
32-
| ((chunkId: ChunkId) => string)
36+
| ((chunkId: ChunkId, ...args: unknown[]) => string)
3337
| undefined;
3438
// RuntimeGlobals.loadScript
3539
var __RUNTIME_GLOBALS_LOAD_SCRIPT__: (
3640
url: ChunkSrcUrl,
3741
done: unknown,
3842
key: string,
3943
chunkId: ChunkId,
44+
...args: unknown[]
4045
) => void;
4146
// RuntimeGlobals.publicPath
4247
var __RUNTIME_GLOBALS_PUBLIC_PATH__: string;
@@ -199,21 +204,52 @@ const originalLoadScript = __RUNTIME_GLOBALS_LOAD_SCRIPT__;
199204
// if users want to support es5, add Promise polyfill first https://github.com/webpack/webpack/issues/12877
200205
function ensureChunk(
201206
chunkId: string,
207+
// args placeholder, to avoid that other webpack runtime would add arg for __webpack_require__.e
208+
arg0: unknown,
209+
arg1: unknown,
210+
arg2: unknown,
211+
arg3: unknown,
212+
arg4: unknown,
213+
arg5: unknown,
214+
arg6: unknown,
202215
callingCounter: { count: number } = { count: 0 },
216+
...args: unknown[]
203217
): Promise<unknown> {
204-
const result = originalEnsureChunk(chunkId);
205-
const originalScriptFilename = originalGetChunkScriptFilename(chunkId);
206-
const originalCssFilename = originalGetCssFilename(chunkId);
218+
const result = originalEnsureChunk(
219+
chunkId,
220+
arg0,
221+
arg1,
222+
arg2,
223+
arg3,
224+
arg4,
225+
arg5,
226+
arg6,
227+
callingCounter,
228+
...args,
229+
);
207230

208-
// mark the async chunk name in the global variables and share it with initial chunk retry to avoid duplicate retrying
209-
if (typeof window !== 'undefined') {
210-
if (originalScriptFilename) {
211-
window.__RB_ASYNC_CHUNKS__[originalScriptFilename] = true;
212-
}
213-
if (originalCssFilename) {
214-
window.__RB_ASYNC_CHUNKS__[originalCssFilename] = true;
231+
try {
232+
const originalScriptFilename = originalGetChunkScriptFilename(chunkId);
233+
const originalCssFilename = originalGetCssFilename(chunkId);
234+
235+
// mark the async chunk name in the global variables and share it with initial chunk retry to avoid duplicate retrying
236+
if (typeof window !== 'undefined') {
237+
if (originalScriptFilename) {
238+
window.__RB_ASYNC_CHUNKS__[originalScriptFilename] = true;
239+
}
240+
if (originalCssFilename) {
241+
window.__RB_ASYNC_CHUNKS__[originalCssFilename] = true;
242+
}
215243
}
244+
} catch (e) {
245+
console.log('[@rsbuild/plugin-assets-retry] get original script or css filename error', e);
246+
}
247+
248+
// if __webpack_require__.e is polluted by other runtime codes, fallback to originalEnsureChunk
249+
if (!callingCounter || typeof callingCounter.count !== 'number') {
250+
return result;
216251
}
252+
217253
callingCounter.count += 1;
218254

219255
return result.catch((error: Error) => {
@@ -278,12 +314,23 @@ function ensureChunk(
278314
config.onRetry(context);
279315
}
280316

281-
const nextPromise = ensureChunk(chunkId, callingCounter);
317+
const nextPromise = ensureChunk(
318+
chunkId,
319+
arg0,
320+
arg1,
321+
arg2,
322+
arg3,
323+
arg4,
324+
arg5,
325+
arg6,
326+
callingCounter,
327+
...args,
328+
);
282329
return nextPromise.then((result) => {
283330
// when after retrying the third time
284331
// ensureChunk(chunkId, { count: 3 }), at that time, existRetryTimes === 2
285332
// after all, callingCounter.count is 4
286-
const isLastSuccessRetry = callingCounter.count === existRetryTimes + 2;
333+
const isLastSuccessRetry = callingCounter?.count === existRetryTimes + 2;
287334
if (typeof config.onSuccess === 'function' && isLastSuccessRetry) {
288335
const context = createContext(existRetryTimes + 1);
289336
config.onSuccess(context);
@@ -298,13 +345,15 @@ function loadScript(
298345
done: unknown,
299346
key: string,
300347
chunkId: ChunkId,
348+
...args: unknown[]
301349
) {
302350
const retry = globalCurrRetrying[chunkId];
303351
return originalLoadScript(
304352
retry ? retry.nextRetryUrl : originalUrl,
305353
done,
306354
key,
307355
chunkId,
356+
...args,
308357
);
309358
}
310359

@@ -316,7 +365,10 @@ function registerAsyncChunkRetry() {
316365

317366
if (typeof __RUNTIME_GLOBALS_REQUIRE__ !== 'undefined') {
318367
try {
319-
__RUNTIME_GLOBALS_ENSURE_CHUNK__ = ensureChunk;
368+
__RUNTIME_GLOBALS_ENSURE_CHUNK__ = ensureChunk as (
369+
chunkId: string,
370+
...args: unknown[]
371+
) => Promise<unknown>;
320372
__RUNTIME_GLOBALS_LOAD_SCRIPT__ = loadScript;
321373
} catch (e) {
322374
console.error(

0 commit comments

Comments
 (0)