@rsbuild/plugin-assets-retry 1.0.7 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,243 +1,252 @@
1
- "use strict";
2
-
3
- var _ref, _RUNTIME_GLOBALS_GET;
4
1
  // rsbuild/runtime/async-chunk-retry
5
-
6
- // e.g: src_AsyncCompTest_tsx
7
- // e.g: static/js/async/src_AsyncCompTest_tsx.js
8
- // publicPath + ChunkFilename e.g: http://localhost:3000/static/js/async/src_AsyncCompTest_tsx.js
9
-
10
2
  // init retryCollector and nextRetry function
11
3
  var config = __RETRY_OPTIONS__;
12
4
  var maxRetries = config.max || 3;
13
5
  var retryCollector = {};
6
+ var retryCssCollector = {};
14
7
  function findCurrentDomain(url) {
15
- var _config$domain;
16
- var domainList = (_config$domain = config.domain) !== null && _config$domain !== void 0 ? _config$domain : [];
17
- var domain = '';
18
- for (var i = 0; i < domainList.length; i++) {
19
- if (url.indexOf(domainList[i]) !== -1) {
20
- domain = domainList[i];
21
- break;
8
+ var domains = config.domain || [];
9
+ var domain = '';
10
+ for(var i = 0; i < domains.length; i++){
11
+ if (url.indexOf(domains[i]) !== -1) {
12
+ domain = domains[i];
13
+ break;
14
+ }
22
15
  }
23
- }
24
- return domain || window.origin;
16
+ return domain || window.origin;
25
17
  }
26
18
  function findNextDomain(url) {
27
- var _config$domain2;
28
- var domainList = (_config$domain2 = config.domain) !== null && _config$domain2 !== void 0 ? _config$domain2 : [];
29
- var currentDomain = findCurrentDomain(url);
30
- var index = domainList.indexOf(currentDomain);
31
- return domainList[(index + 1) % domainList.length] || url;
19
+ var domains = config.domain || [];
20
+ var currentDomain = findCurrentDomain(url);
21
+ var index = domains.indexOf(currentDomain);
22
+ return domains[(index + 1) % domains.length] || url;
32
23
  }
33
24
  var postfixRE = /[?#].*$/;
34
25
  function cleanUrl(url) {
35
- return url.replace(postfixRE, '');
26
+ return url.replace(postfixRE, '');
36
27
  }
37
28
  function getQueryFromUrl(url) {
38
- var parts = url.split('?')[1];
39
- return parts ? "?".concat(parts.split('#')[0]) : '';
29
+ var parts = url.split('?')[1];
30
+ return parts ? "?".concat(parts.split('#')[0]) : '';
40
31
  }
41
32
  function getUrlRetryQuery(existRetryTimes, originalQuery) {
42
- if (config.addQuery === true) {
43
- return originalQuery !== '' ? "".concat(originalQuery, "&retry=").concat(existRetryTimes) : "?retry=".concat(existRetryTimes);
44
- }
45
- if (typeof config.addQuery === 'function') {
46
- return config.addQuery({
47
- times: existRetryTimes,
48
- originalQuery: originalQuery
49
- });
50
- }
51
- return '';
33
+ if (config.addQuery === true) {
34
+ return originalQuery !== '' ? "".concat(originalQuery, "&retry=").concat(existRetryTimes) : "?retry=".concat(existRetryTimes);
35
+ }
36
+ if (typeof config.addQuery === 'function') {
37
+ return config.addQuery({
38
+ times: existRetryTimes,
39
+ originalQuery: originalQuery
40
+ });
41
+ }
42
+ return '';
52
43
  }
53
44
  function getNextRetryUrl(currRetryUrl, domain, nextDomain, existRetryTimes, originalQuery) {
54
- return cleanUrl(currRetryUrl.replace(domain, nextDomain)) + getUrlRetryQuery(existRetryTimes + 1, originalQuery);
45
+ return cleanUrl(currRetryUrl.replace(domain, nextDomain)) + getUrlRetryQuery(existRetryTimes + 1, originalQuery);
55
46
  }
56
-
57
47
  // shared between ensureChunk and loadScript
58
48
  var globalCurrRetrying = {};
59
- function getCurrentRetry(chunkId, existRetryTimes) {
60
- var _retryCollector$chunk;
61
- return (_retryCollector$chunk = retryCollector[chunkId]) === null || _retryCollector$chunk === void 0 ? void 0 : _retryCollector$chunk[existRetryTimes];
49
+ // shared between ensureChunk and loadStyleSheet
50
+ var globalCurrRetryingCss = {};
51
+ function getCurrentRetry(chunkId, existRetryTimes, isCssAsyncChunk) {
52
+ var _retryCssCollector_chunkId, _retryCollector_chunkId;
53
+ return isCssAsyncChunk ? (_retryCssCollector_chunkId = retryCssCollector[chunkId]) === null || _retryCssCollector_chunkId === void 0 ? void 0 : _retryCssCollector_chunkId[existRetryTimes] : (_retryCollector_chunkId = retryCollector[chunkId]) === null || _retryCollector_chunkId === void 0 ? void 0 : _retryCollector_chunkId[existRetryTimes];
62
54
  }
63
- function initRetry(chunkId) {
64
- var _config$domain$, _config$domain3;
65
- var originalScriptFilename = originalGetChunkScriptFilename(chunkId);
66
- var originalPublicPath = __RUNTIME_GLOBALS_PUBLIC_PATH__;
67
- var originalSrcUrl = originalPublicPath.startsWith('/') ? window.origin + originalPublicPath + originalScriptFilename : originalPublicPath + originalScriptFilename;
68
- var originalQuery = getQueryFromUrl(originalSrcUrl);
69
- var existRetryTimes = 0;
70
- var nextDomain = (_config$domain$ = (_config$domain3 = config.domain) === null || _config$domain3 === void 0 ? void 0 : _config$domain3[0]) !== null && _config$domain$ !== void 0 ? _config$domain$ : window.origin;
71
- return {
72
- nextDomain: nextDomain,
73
- nextRetryUrl: getNextRetryUrl(originalSrcUrl, nextDomain, nextDomain, existRetryTimes, originalQuery),
74
- originalScriptFilename: originalScriptFilename,
75
- originalSrcUrl: originalSrcUrl,
76
- originalQuery: originalQuery
77
- };
78
- }
79
- function nextRetry(chunkId, existRetryTimes) {
80
- var currRetry = getCurrentRetry(chunkId, existRetryTimes);
81
- var nextRetry;
82
- var nextExistRetryTimes = existRetryTimes + 1;
83
- if (existRetryTimes === 0 || currRetry === undefined) {
84
- nextRetry = initRetry(chunkId);
85
- retryCollector[chunkId] = [];
86
- } else {
87
- var originalScriptFilename = currRetry.originalScriptFilename,
88
- originalSrcUrl = currRetry.originalSrcUrl,
89
- originalQuery = currRetry.originalQuery;
90
- var nextDomain = findNextDomain(currRetry.nextDomain);
91
- nextRetry = {
92
- nextDomain: nextDomain,
93
- nextRetryUrl: getNextRetryUrl(currRetry.nextRetryUrl, currRetry.nextDomain, nextDomain, existRetryTimes, originalQuery),
94
- originalScriptFilename: originalScriptFilename,
95
- originalSrcUrl: originalSrcUrl,
96
- originalQuery: originalQuery
55
+ function initRetry(chunkId, isCssAsyncChunk) {
56
+ var _config_domain;
57
+ var originalScriptFilename = isCssAsyncChunk ? originalGetCssFilename(chunkId) : originalGetChunkScriptFilename(chunkId);
58
+ if (!originalScriptFilename) {
59
+ throw new Error('only support cssExtract');
60
+ }
61
+ var originalPublicPath = __RUNTIME_GLOBALS_PUBLIC_PATH__;
62
+ var originalSrcUrl = originalPublicPath.startsWith('/') ? window.origin + originalPublicPath + originalScriptFilename : originalPublicPath + originalScriptFilename;
63
+ var originalQuery = getQueryFromUrl(originalSrcUrl);
64
+ var existRetryTimes = 0;
65
+ var nextDomain = ((_config_domain = config.domain) === null || _config_domain === void 0 ? void 0 : _config_domain[0]) || window.origin;
66
+ return {
67
+ nextDomain: nextDomain,
68
+ nextRetryUrl: getNextRetryUrl(originalSrcUrl, nextDomain, nextDomain, existRetryTimes, originalQuery),
69
+ originalScriptFilename: originalScriptFilename,
70
+ originalSrcUrl: originalSrcUrl,
71
+ originalQuery: originalQuery
97
72
  };
98
- }
99
- retryCollector[chunkId][nextExistRetryTimes] = nextRetry;
100
- globalCurrRetrying[chunkId] = nextRetry;
101
- return nextRetry;
102
73
  }
103
-
74
+ function nextRetry(chunkId, existRetryTimes, isCssAsyncChunk) {
75
+ var currRetry = getCurrentRetry(chunkId, existRetryTimes, isCssAsyncChunk);
76
+ var nextRetry;
77
+ var nextExistRetryTimes = existRetryTimes + 1;
78
+ if (existRetryTimes === 0 || currRetry === undefined) {
79
+ nextRetry = initRetry(chunkId, isCssAsyncChunk);
80
+ if (isCssAsyncChunk) {
81
+ retryCssCollector[chunkId] = [];
82
+ } else {
83
+ retryCollector[chunkId] = [];
84
+ }
85
+ } else {
86
+ var originalScriptFilename = currRetry.originalScriptFilename, originalSrcUrl = currRetry.originalSrcUrl, originalQuery = currRetry.originalQuery;
87
+ var nextDomain = findNextDomain(currRetry.nextDomain);
88
+ nextRetry = {
89
+ nextDomain: nextDomain,
90
+ nextRetryUrl: getNextRetryUrl(currRetry.nextRetryUrl, currRetry.nextDomain, nextDomain, existRetryTimes, originalQuery),
91
+ originalScriptFilename: originalScriptFilename,
92
+ originalSrcUrl: originalSrcUrl,
93
+ originalQuery: originalQuery
94
+ };
95
+ }
96
+ if (isCssAsyncChunk) {
97
+ retryCssCollector[chunkId][nextExistRetryTimes] = nextRetry;
98
+ globalCurrRetryingCss[chunkId] = nextRetry;
99
+ } else {
100
+ retryCollector[chunkId][nextExistRetryTimes] = nextRetry;
101
+ globalCurrRetrying[chunkId] = nextRetry;
102
+ }
103
+ return nextRetry;
104
+ }
104
105
  // rewrite webpack runtime with nextRetry()
105
106
  var originalEnsureChunk = __RUNTIME_GLOBALS_ENSURE_CHUNK__;
106
107
  var originalGetChunkScriptFilename = __RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__;
107
- var originalGetCssFilename = (_ref = (_RUNTIME_GLOBALS_GET = __RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__) !== null && _RUNTIME_GLOBALS_GET !== void 0 ? _RUNTIME_GLOBALS_GET : __RUNTIME_GLOBALS_GET_CSS_FILENAME__) !== null && _ref !== void 0 ? _ref : function () {
108
- return null;
108
+ var originalGetCssFilename = __RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__ || __RUNTIME_GLOBALS_GET_CSS_FILENAME__ || function() {
109
+ return null;
109
110
  };
110
111
  var originalLoadScript = __RUNTIME_GLOBALS_LOAD_SCRIPT__;
111
112
  var ERROR_PREFIX = '[@rsbuild/plugin-assets-retry] ';
112
-
113
113
  // if users want to support es5, add Promise polyfill first https://github.com/webpack/webpack/issues/12877
114
- function ensureChunk(chunkId,
115
- // args placeholder, to avoid that other webpack runtime would add arg for __webpack_require__.e
116
- arg0, arg1, arg2, arg3, arg4, arg5, arg6) {
117
- var callingCounter = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : {
118
- count: 0
119
- };
120
- for (var _len = arguments.length, args = new Array(_len > 9 ? _len - 9 : 0), _key = 9; _key < _len; _key++) {
121
- args[_key - 9] = arguments[_key];
122
- }
123
- var result = originalEnsureChunk.apply(void 0, [chunkId, arg0, arg1, arg2, arg3, arg4, arg5, arg6, callingCounter].concat(args));
124
- try {
125
- var originalScriptFilename = originalGetChunkScriptFilename(chunkId);
126
- var originalCssFilename = originalGetCssFilename(chunkId);
127
-
128
- // mark the async chunk name in the global variables and share it with initial chunk retry to avoid duplicate retrying
129
- if (typeof window !== 'undefined') {
130
- if (originalScriptFilename) {
131
- window.__RB_ASYNC_CHUNKS__[originalScriptFilename] = true;
132
- }
133
- if (originalCssFilename) {
134
- window.__RB_ASYNC_CHUNKS__[originalCssFilename] = true;
135
- }
114
+ function ensureChunk(chunkId) {
115
+ // biome-ignore lint/style/noArguments: allowed
116
+ var args = Array.prototype.slice.call(arguments);
117
+ // Other webpack runtimes would add arguments for `__webpack_require__.e`,
118
+ // So we use `arguments[10]` to avoid conflicts with other runtimes
119
+ if (!args[10]) {
120
+ args[10] = {
121
+ count: 0,
122
+ cssFailedCount: 0
123
+ };
136
124
  }
137
- } catch (e) {
138
- console.error(ERROR_PREFIX, 'get original script or css filename error', e);
139
- }
140
-
141
- // if __webpack_require__.e is polluted by other runtime codes, fallback to originalEnsureChunk
142
- if (typeof (callingCounter === null || callingCounter === void 0 ? void 0 : callingCounter.count) !== 'number') {
143
- return result;
144
- }
145
- callingCounter.count += 1;
146
- return result.catch(function (error) {
147
- var _error$message;
148
- // the first calling is not retry
149
- // if the failed request is 4 in network panel, callingCounter.count === 4, the first one is the normal request, and existRetryTimes is 3, retried 3 times
150
- var existRetryTimes = callingCounter.count - 1;
151
- var originalScriptFilename;
152
- var nextRetryUrl;
153
- var nextDomain;
125
+ var callingCounter = args[10];
126
+ var result = originalEnsureChunk.apply(null, args);
154
127
  try {
155
- var retryResult = nextRetry(chunkId, existRetryTimes);
156
- originalScriptFilename = retryResult.originalScriptFilename;
157
- nextRetryUrl = retryResult.nextRetryUrl;
158
- nextDomain = retryResult.nextDomain;
128
+ var originalScriptFilename = originalGetChunkScriptFilename(chunkId);
129
+ var originalCssFilename = originalGetCssFilename(chunkId);
130
+ // mark the async chunk name in the global variables and share it with initial chunk retry to avoid duplicate retrying
131
+ if (typeof window !== 'undefined') {
132
+ if (originalScriptFilename) {
133
+ window.__RB_ASYNC_CHUNKS__[originalScriptFilename] = true;
134
+ }
135
+ if (originalCssFilename) {
136
+ window.__RB_ASYNC_CHUNKS__[originalCssFilename] = true;
137
+ }
138
+ }
159
139
  } catch (e) {
160
- console.error(ERROR_PREFIX, 'failed to get nextRetryUrl', e);
161
- throw error;
140
+ console.error(ERROR_PREFIX, 'get original script or CSS filename error', e);
162
141
  }
163
-
164
- // At present, we don't consider the switching domain and addQuery of async CSS chunk
165
- // 1. Async js chunk will be requested first. It is rare for async CSS chunk to fail alone.
166
- // 2. the code of loading CSS in webpack runtime is complex and it may be modified by cssExtractPlugin, increase the complexity of this plugin.
167
- var isCssAsyncChunkLoadFailed = Boolean(error === null || error === void 0 || (_error$message = error.message) === null || _error$message === void 0 ? void 0 : _error$message.includes('CSS chunk'));
168
- var createContext = function createContext(times) {
169
- return {
170
- times: times,
171
- domain: nextDomain,
172
- url: nextRetryUrl,
173
- tagName: isCssAsyncChunkLoadFailed ? 'link' : 'script',
174
- isAsyncChunk: true
175
- };
176
- };
177
- var context = createContext(existRetryTimes);
178
- if (existRetryTimes >= maxRetries) {
179
- var _error$message2;
180
- error.message = (_error$message2 = error.message) !== null && _error$message2 !== void 0 && _error$message2.includes('retries:') ? error.message : "Loading chunk ".concat(chunkId, " from \"").concat(originalScriptFilename, "\" failed after ").concat(maxRetries, " retries: \"").concat(error.message, "\"");
181
- if (typeof config.onFail === 'function') {
182
- config.onFail(context);
183
- }
184
- throw error;
142
+ // if __webpack_require__.e is polluted by other runtime codes, fallback to originalEnsureChunk
143
+ if (!callingCounter || typeof callingCounter.count !== 'number' || typeof callingCounter.cssFailedCount !== 'number') {
144
+ return result;
185
145
  }
186
-
187
- // Filter by config.test and config.domain
188
- var tester = config.test;
189
- if (tester) {
190
- if (typeof tester === 'string') {
191
- var regexp = new RegExp(tester);
192
- tester = function tester(str) {
193
- return regexp.test(str);
146
+ callingCounter.count += 1;
147
+ return result.catch(function(error) {
148
+ var _error_message;
149
+ // the first calling is not retry
150
+ // if the failed request is 4 in network panel, callingCounter.count === 4, the first one is the normal request, and existRetryTimes is 3, retried 3 times
151
+ var existRetryTimesAll = callingCounter.count - 1;
152
+ var cssExistRetryTimes = callingCounter.cssFailedCount;
153
+ var jsExistRetryTimes = existRetryTimesAll - cssExistRetryTimes;
154
+ var originalScriptFilename;
155
+ var nextRetryUrl;
156
+ var nextDomain;
157
+ var isCssAsyncChunkLoadFailed = Boolean(error === null || error === void 0 ? void 0 : (_error_message = error.message) === null || _error_message === void 0 ? void 0 : _error_message.includes('CSS chunk'));
158
+ if (isCssAsyncChunkLoadFailed) {
159
+ callingCounter.cssFailedCount += 1;
160
+ }
161
+ var existRetryTimes = isCssAsyncChunkLoadFailed ? cssExistRetryTimes : jsExistRetryTimes;
162
+ try {
163
+ var retryResult = nextRetry(chunkId, existRetryTimes, isCssAsyncChunkLoadFailed);
164
+ originalScriptFilename = retryResult.originalScriptFilename;
165
+ nextRetryUrl = retryResult.nextRetryUrl;
166
+ nextDomain = retryResult.nextDomain;
167
+ } catch (e) {
168
+ console.error(ERROR_PREFIX, 'failed to get nextRetryUrl', e);
169
+ throw error;
170
+ }
171
+ var createContext = function(times) {
172
+ return {
173
+ times: times,
174
+ domain: nextDomain,
175
+ url: nextRetryUrl,
176
+ tagName: isCssAsyncChunkLoadFailed ? 'link' : 'script',
177
+ isAsyncChunk: true
178
+ };
194
179
  };
195
- }
196
- if (typeof tester !== 'function' || !tester(nextRetryUrl)) {
197
- throw error;
198
- }
199
- }
200
- if (config.domain && config.domain.length > 0 && config.domain.indexOf(nextDomain) === -1) {
201
- throw error;
202
- }
203
-
204
- // Start retry
205
- if (typeof config.onRetry === 'function') {
206
- config.onRetry(context);
207
- }
208
- var nextPromise = ensureChunk.apply(void 0, [chunkId, arg0, arg1, arg2, arg3, arg4, arg5, arg6, callingCounter].concat(args));
209
- return nextPromise.then(function (result) {
210
- // when after retrying the third time
211
- // ensureChunk(chunkId, { count: 3 }), at that time, existRetryTimes === 2
212
- // after all, callingCounter.count is 4
213
- var isLastSuccessRetry = (callingCounter === null || callingCounter === void 0 ? void 0 : callingCounter.count) === existRetryTimes + 2;
214
- if (typeof config.onSuccess === 'function' && isLastSuccessRetry) {
215
- var _context = createContext(existRetryTimes + 1);
216
- config.onSuccess(_context);
217
- }
218
- return result;
180
+ var context = createContext(existRetryTimes);
181
+ if (existRetryTimes >= maxRetries) {
182
+ var _error_message1;
183
+ error.message = ((_error_message1 = error.message) === null || _error_message1 === void 0 ? void 0 : _error_message1.includes('retries:')) ? error.message : "Loading chunk ".concat(chunkId, ' from "').concat(originalScriptFilename, '" failed after ').concat(maxRetries, ' retries: "').concat(error.message, '"');
184
+ if (typeof config.onFail === 'function') {
185
+ config.onFail(context);
186
+ }
187
+ throw error;
188
+ }
189
+ // Filter by config.test and config.domain
190
+ var tester = config.test;
191
+ if (tester) {
192
+ if (typeof tester === 'string') {
193
+ var regexp = new RegExp(tester);
194
+ tester = function(str) {
195
+ return regexp.test(str);
196
+ };
197
+ }
198
+ if (typeof tester !== 'function' || !tester(nextRetryUrl)) {
199
+ throw error;
200
+ }
201
+ }
202
+ if (config.domain && config.domain.indexOf(nextDomain) === -1) {
203
+ throw error;
204
+ }
205
+ // Start retry
206
+ if (typeof config.onRetry === 'function') {
207
+ config.onRetry(context);
208
+ }
209
+ var nextPromise = ensureChunk.apply(ensureChunk, args);
210
+ return nextPromise.then(function(result) {
211
+ // when after retrying the third time
212
+ // ensureChunk(chunkId, { count: 3 }), at that time, existRetryTimes === 2
213
+ // at the end, callingCounter.count is 4
214
+ var isLastSuccessRetry = (callingCounter === null || callingCounter === void 0 ? void 0 : callingCounter.count) === existRetryTimesAll + 2;
215
+ if (typeof config.onSuccess === 'function' && isLastSuccessRetry) {
216
+ var context = createContext(existRetryTimes + 1);
217
+ config.onSuccess(context);
218
+ }
219
+ return result;
220
+ });
219
221
  });
220
- });
221
222
  }
222
- function loadScript(originalUrl, done, key, chunkId) {
223
- var retry = globalCurrRetrying[chunkId];
224
- for (var _len2 = arguments.length, args = new Array(_len2 > 4 ? _len2 - 4 : 0), _key2 = 4; _key2 < _len2; _key2++) {
225
- args[_key2 - 4] = arguments[_key2];
226
- }
227
- return originalLoadScript.apply(void 0, [retry ? retry.nextRetryUrl : originalUrl, done, key, chunkId].concat(args));
223
+ function loadScript() {
224
+ // biome-ignore lint/style/noArguments: allowed
225
+ var args = Array.prototype.slice.call(arguments);
226
+ var retry = globalCurrRetrying[args[3]];
227
+ if (retry) {
228
+ args[0] = retry.nextRetryUrl;
229
+ }
230
+ return originalLoadScript.apply(null, args);
231
+ }
232
+ function loadStyleSheet(href, chunkId) {
233
+ var retry = globalCurrRetryingCss[chunkId];
234
+ return(// biome-ignore lint/complexity/useOptionalChain: for less code
235
+ retry && retry.nextRetryUrl || __RUNTIME_GLOBALS_PUBLIC_PATH__ + href);
228
236
  }
229
237
  function registerAsyncChunkRetry() {
230
- // init global variables shared between initial-chunk-retry and async-chunk-retry
231
- if (typeof window !== 'undefined' && !window.__RB_ASYNC_CHUNKS__) {
232
- window.__RB_ASYNC_CHUNKS__ = {};
233
- }
234
- if (typeof __RUNTIME_GLOBALS_REQUIRE__ !== 'undefined') {
235
- try {
236
- __RUNTIME_GLOBALS_ENSURE_CHUNK__ = ensureChunk;
237
- __RUNTIME_GLOBALS_LOAD_SCRIPT__ = loadScript;
238
- } catch (e) {
239
- console.error(ERROR_PREFIX, 'Register async chunk retry runtime failed', e);
238
+ // init global variables shared between initial-chunk-retry and async-chunk-retry
239
+ if (typeof window !== 'undefined' && !window.__RB_ASYNC_CHUNKS__) {
240
+ window.__RB_ASYNC_CHUNKS__ = {};
241
+ }
242
+ if (typeof __RUNTIME_GLOBALS_REQUIRE__ !== 'undefined') {
243
+ try {
244
+ __RUNTIME_GLOBALS_ENSURE_CHUNK__ = ensureChunk;
245
+ __RUNTIME_GLOBALS_LOAD_SCRIPT__ = loadScript;
246
+ __RUNTIME_GLOBALS_RSBUILD_LOAD_STYLESHEET__ = loadStyleSheet;
247
+ } catch (e) {
248
+ console.error(ERROR_PREFIX, 'Register async chunk retry runtime failed', e);
249
+ }
240
250
  }
241
- }
242
251
  }
243
- registerAsyncChunkRetry();
252
+ registerAsyncChunkRetry();
@@ -1 +1 @@
1
- "use strict";var _ref,_RUNTIME_GLOBALS_GET,config=__RETRY_OPTIONS__,maxRetries=config.max||3,retryCollector={};function findCurrentDomain(n){for(var r,e=null!==(r=config.domain)&&void 0!==r?r:[],i="",t=0;t<e.length;t++)if(-1!==n.indexOf(e[t])){i=e[t];break}return i||window.origin}function findNextDomain(n){var r,e=null!==(r=config.domain)&&void 0!==r?r:[],i=findCurrentDomain(n),t=e.indexOf(i);return e[(t+1)%e.length]||n}var postfixRE=/[?#].*$/;function cleanUrl(n){return n.replace(postfixRE,"")}function getQueryFromUrl(n){var r=n.split("?")[1];return r?"?".concat(r.split("#")[0]):""}function getUrlRetryQuery(n,r){return!0===config.addQuery?""!==r?"".concat(r,"&retry=").concat(n):"?retry=".concat(n):"function"==typeof config.addQuery?config.addQuery({times:n,originalQuery:r}):""}function getNextRetryUrl(n,r,e,i,t){return cleanUrl(n.replace(r,e))+getUrlRetryQuery(i+1,t)}var globalCurrRetrying={};function getCurrentRetry(n,r){var e;return null===(e=retryCollector[n])||void 0===e?void 0:e[r]}function initRetry(n){var r,e,i=originalGetChunkScriptFilename(n),t=__RUNTIME_GLOBALS_PUBLIC_PATH__,o=t.startsWith("/")?window.origin+t+i:t+i,a=getQueryFromUrl(o),l=null!==(r=null===(e=config.domain)||void 0===e?void 0:e[0])&&void 0!==r?r:window.origin;return{nextDomain:l,nextRetryUrl:getNextRetryUrl(o,l,l,0,a),originalScriptFilename:i,originalSrcUrl:o,originalQuery:a}}function nextRetry(n,r){var e,i=getCurrentRetry(n,r),t=r+1;if(0===r||void 0===i)e=initRetry(n),retryCollector[n]=[];else{var o=i.originalScriptFilename,a=i.originalSrcUrl,l=i.originalQuery,c=findNextDomain(i.nextDomain);e={nextDomain:c,nextRetryUrl:getNextRetryUrl(i.nextRetryUrl,i.nextDomain,c,r,l),originalScriptFilename:o,originalSrcUrl:a,originalQuery:l}}return retryCollector[n][t]=e,globalCurrRetrying[n]=e,e}var originalEnsureChunk=__RUNTIME_GLOBALS_ENSURE_CHUNK__,originalGetChunkScriptFilename=__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__,originalGetCssFilename=null!==(_ref=null!==(_RUNTIME_GLOBALS_GET=__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__)&&void 0!==_RUNTIME_GLOBALS_GET?_RUNTIME_GLOBALS_GET:__RUNTIME_GLOBALS_GET_CSS_FILENAME__)&&void 0!==_ref?_ref:function(){return null},originalLoadScript=__RUNTIME_GLOBALS_LOAD_SCRIPT__,ERROR_PREFIX="[@rsbuild/plugin-assets-retry] ";function ensureChunk(n,r,e,i,t,o,a,l){for(var c=arguments.length>8&&void 0!==arguments[8]?arguments[8]:{count:0},_=arguments.length,u=new Array(_>9?_-9:0),f=9;f<_;f++)u[f-9]=arguments[f];var g=originalEnsureChunk.apply(void 0,[n,r,e,i,t,o,a,l,c].concat(u));try{var R=originalGetChunkScriptFilename(n),d=originalGetCssFilename(n);"undefined"!=typeof window&&(R&&(window.__RB_ASYNC_CHUNKS__[R]=!0),d&&(window.__RB_ASYNC_CHUNKS__[d]=!0))}catch(n){console.error(ERROR_PREFIX,"get original script or css filename error",n)}return"number"!=typeof(null==c?void 0:c.count)?g:(c.count+=1,g.catch((function(_){var f,g,R,d,y=c.count-1;try{var s=nextRetry(n,y);g=s.originalScriptFilename,R=s.nextRetryUrl,d=s.nextDomain}catch(n){throw console.error(ERROR_PREFIX,"failed to get nextRetryUrl",n),_}var S,E=Boolean(null==_||null===(f=_.message)||void 0===f?void 0:f.includes("CSS chunk")),m=function(n){return{times:n,domain:d,url:R,tagName:E?"link":"script",isAsyncChunk:!0}},C=m(y);if(y>=maxRetries)throw _.message=null!==(S=_.message)&&void 0!==S&&S.includes("retries:")?_.message:"Loading chunk ".concat(n,' from "').concat(g,'" failed after ').concat(maxRetries,' retries: "').concat(_.message,'"'),"function"==typeof config.onFail&&config.onFail(C),_;var U=config.test;if(U){if("string"==typeof U){var v=new RegExp(U);U=function(n){return v.test(n)}}if("function"!=typeof U||!U(R))throw _}if(config.domain&&config.domain.length>0&&-1===config.domain.indexOf(d))throw _;return"function"==typeof config.onRetry&&config.onRetry(C),ensureChunk.apply(void 0,[n,r,e,i,t,o,a,l,c].concat(u)).then((function(n){var r=(null==c?void 0:c.count)===y+2;if("function"==typeof config.onSuccess&&r){var e=m(y+1);config.onSuccess(e)}return n}))})))}function loadScript(n,r,e,i){for(var t=globalCurrRetrying[i],o=arguments.length,a=new Array(o>4?o-4:0),l=4;l<o;l++)a[l-4]=arguments[l];return originalLoadScript.apply(void 0,[t?t.nextRetryUrl:n,r,e,i].concat(a))}function registerAsyncChunkRetry(){if("undefined"==typeof window||window.__RB_ASYNC_CHUNKS__||(window.__RB_ASYNC_CHUNKS__={}),"undefined"!=typeof __RUNTIME_GLOBALS_REQUIRE__)try{__RUNTIME_GLOBALS_ENSURE_CHUNK__=ensureChunk,__RUNTIME_GLOBALS_LOAD_SCRIPT__=loadScript}catch(n){console.error(ERROR_PREFIX,"Register async chunk retry runtime failed",n)}}registerAsyncChunkRetry();
1
+ var n=__RETRY_OPTIONS__,r=n.max||3,e={},i={},t=/[?#].*$/;function o(r,e,i,o,_){var a;return r.replace(e,i).replace(t,"")+(a=o+1,!0===n.addQuery?""!==_?"".concat(_,"&retry=").concat(a):"?retry=".concat(a):"function"==typeof n.addQuery?n.addQuery({times:a,originalQuery:_}):"")}var _={},a={},c=__RUNTIME_GLOBALS_ENSURE_CHUNK__,l=__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__,u=__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__||__RUNTIME_GLOBALS_GET_CSS_FILENAME__||function(){return null},d=__RUNTIME_GLOBALS_LOAD_SCRIPT__,s="[@rsbuild/plugin-assets-retry] ";function f(t){var d=Array.prototype.slice.call(arguments);d[10]||(d[10]={count:0,cssFailedCount:0});var S=d[10],y=c.apply(null,d);try{var R=l(t),p=u(t);"undefined"!=typeof window&&(R&&(window.__RB_ASYNC_CHUNKS__[R]=!0),p&&(window.__RB_ASYNC_CHUNKS__[p]=!0))}catch(n){console.error(s,"get original script or CSS filename error",n)}return S&&"number"==typeof S.count&&"number"==typeof S.cssFailedCount?(S.count+=1,y.catch(function(c){var y,R,p,v,E,U=S.count-1,L=S.cssFailedCount,g=!!(null==c?void 0:null===(y=c.message)||void 0===y?void 0:y.includes("CSS chunk"));g&&(S.cssFailedCount+=1);var N=g?L:U-L;try{var m=function(r,t,c){var d,s,f,S=c?null===(d=i[r])||void 0===d?void 0:d[t]:null===(s=e[r])||void 0===s?void 0:s[t],y=t+1;if(0===t||void 0===S)f=function(r,e){var i,t,_=e?u(r):l(r);if(!_)throw Error("only support cssExtract");var a=__RUNTIME_GLOBALS_PUBLIC_PATH__,c=a.startsWith("/")?window.origin+a+_:a+_,d=(i=c.split("?")[1])?"?".concat(i.split("#")[0]):"",s=(null===(t=n.domain)||void 0===t?void 0:t[0])||window.origin;return{nextDomain:s,nextRetryUrl:o(c,s,s,0,d),originalScriptFilename:_,originalSrcUrl:c,originalQuery:d}}(r,c),c?i[r]=[]:e[r]=[];else{var R,p,v,E,U=S.originalScriptFilename,L=S.originalSrcUrl,g=S.originalQuery,N=(R=S.nextDomain,p=n.domain||[],v=function(r){for(var e=n.domain||[],i="",t=0;t<e.length;t++)if(-1!==r.indexOf(e[t])){i=e[t];break}return i||window.origin}(R),E=p.indexOf(v),p[(E+1)%p.length]||R);f={nextDomain:N,nextRetryUrl:o(S.nextRetryUrl,S.nextDomain,N,t,g),originalScriptFilename:U,originalSrcUrl:L,originalQuery:g}}return c?(i[r][y]=f,a[r]=f):(e[r][y]=f,_[r]=f),f}(t,N,g);R=m.originalScriptFilename,p=m.nextRetryUrl,v=m.nextDomain}catch(n){throw console.error(s,"failed to get nextRetryUrl",n),c}var A=function(n){return{times:n,domain:v,url:p,tagName:g?"link":"script",isAsyncChunk:!0}},C=A(N);if(N>=r)throw c.message=(null===(E=c.message)||void 0===E?void 0:E.includes("retries:"))?c.message:"Loading chunk ".concat(t,' from "').concat(R,'" failed after ').concat(r,' retries: "').concat(c.message,'"'),"function"==typeof n.onFail&&n.onFail(C),c;var T=n.test;if(T){if("string"==typeof T){var w=new RegExp(T);T=function(n){return w.test(n)}}if("function"!=typeof T||!T(p))throw c}if(n.domain&&-1===n.domain.indexOf(v))throw c;return"function"==typeof n.onRetry&&n.onRetry(C),f.apply(f,d).then(function(r){var e=(null==S?void 0:S.count)===U+2;if("function"==typeof n.onSuccess&&e){var i=A(N+1);n.onSuccess(i)}return r})})):y}function S(){var n=Array.prototype.slice.call(arguments),r=_[n[3]];return r&&(n[0]=r.nextRetryUrl),d.apply(null,n)}function y(n,r){var e=a[r];return e&&e.nextRetryUrl||__RUNTIME_GLOBALS_PUBLIC_PATH__+n}!function(){if("undefined"==typeof window||window.__RB_ASYNC_CHUNKS__||(window.__RB_ASYNC_CHUNKS__={}),"undefined"!=typeof __RUNTIME_GLOBALS_REQUIRE__)try{__RUNTIME_GLOBALS_ENSURE_CHUNK__=f,__RUNTIME_GLOBALS_LOAD_SCRIPT__=S,__RUNTIME_GLOBALS_RSBUILD_LOAD_STYLESHEET__=y}catch(n){console.error(s,"Register async chunk retry runtime failed",n)}}();
@@ -1,4 +1,5 @@
1
1
  declare global {
2
2
  var __RB_ASYNC_CHUNKS__: Record<string, boolean>;
3
+ var __RUNTIME_GLOBALS_OPTIONS__: RuntimeRetryOptions;
3
4
  }
4
5
  export {};