@rsbuild/plugin-assets-retry 1.2.0 → 1.2.2

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.
package/README.md CHANGED
@@ -94,13 +94,21 @@ const defaultAssetsRetryOptions = {
94
94
  - **Type:** `string[]`
95
95
  - **Default:** `[]`
96
96
 
97
- Specifies the retry domain when assets fail to load. In the `domain` array, the first item is the currently used domain, and the following items are backup domains. When a asset request for a domain fails, Rsbuild will find that domain in the array and replace it with the next domain in the array.
97
+ Specifies the retry domain when assets fail to load. In the `domain` array, the first item is the default domain of static assets, and the following items are backup domains. When a asset request for a domain fails, Rsbuild will find that domain in the array and replace it with the next domain in the array.
98
98
 
99
99
  For example:
100
100
 
101
101
  ```js
102
- pluginAssetsRetry({
103
- domain: ["https://cdn1.com", "https://cdn2.com", "https://cdn3.com"],
102
+ // rsbuild.config.ts
103
+ defineConfig({
104
+ plugins: [
105
+ pluginAssetsRetry({
106
+ domain: ["cdn1.com", "cdn2.com", "cdn3.com"],
107
+ })
108
+ ],
109
+ output: {
110
+ assetPrefix: "https://cdn1.com", // or "//cdn1.com"
111
+ },
104
112
  });
105
113
  ```
106
114
 
package/README.zh-CN.md CHANGED
@@ -92,13 +92,21 @@ const defaultOptions = {
92
92
  - **类型:** `string[]`
93
93
  - **默认值:** `[]`
94
94
 
95
- 指定资源加载失败时的重试域名列表。在 `domain` 数组中,第一项是当前使用的域名,后面几项为备用域名。当某个域名的资源请求失败时,Rsbuild 会在数组中找到该域名,并替换为数组的下一个域名。
95
+ 指定资源加载失败时的重试域名列表。在 `domain` 数组中,第一项是静态资源默认所在的域名,后面几项为备用域名。当某个域名的资源请求失败时,Rsbuild 会在数组中找到该域名,并替换为数组的下一个域名。
96
96
 
97
97
  比如:
98
98
 
99
99
  ```js
100
- pluginAssetsRetry({
101
- domain: ["https://cdn1.com", "https://cdn2.com", "https://cdn3.com"],
100
+ // rsbuild.config.ts
101
+ defineConfig({
102
+ plugins: [
103
+ pluginAssetsRetry({
104
+ domain: ["cdn1.com", "cdn2.com", "cdn3.com"],
105
+ })
106
+ ],
107
+ output: {
108
+ assetPrefix: "https://cdn1.com", // 或者 "//cdn1.com"
109
+ },
102
110
  });
103
111
  ```
104
112
 
package/dist/index.cjs CHANGED
@@ -135,12 +135,8 @@ function __webpack_require__(moduleId) {
135
135
  return module.exports;
136
136
  }
137
137
  (()=>{
138
- __webpack_require__.n = function(module) {
139
- var getter = module && module.__esModule ? function() {
140
- return module['default'];
141
- } : function() {
142
- return module;
143
- };
138
+ __webpack_require__.n = (module)=>{
139
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
144
140
  __webpack_require__.d(getter, {
145
141
  a: getter
146
142
  });
@@ -148,7 +144,7 @@ function __webpack_require__(moduleId) {
148
144
  };
149
145
  })();
150
146
  (()=>{
151
- __webpack_require__.d = function(exports1, definition) {
147
+ __webpack_require__.d = (exports1, definition)=>{
152
148
  for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
153
149
  enumerable: true,
154
150
  get: definition[key]
@@ -156,12 +152,10 @@ function __webpack_require__(moduleId) {
156
152
  };
157
153
  })();
158
154
  (()=>{
159
- __webpack_require__.o = function(obj, prop) {
160
- return Object.prototype.hasOwnProperty.call(obj, prop);
161
- };
155
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
162
156
  })();
163
157
  (()=>{
164
- __webpack_require__.r = function(exports1) {
158
+ __webpack_require__.r = (exports1)=>{
165
159
  if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
166
160
  value: 'Module'
167
161
  });
@@ -175,8 +169,8 @@ var __webpack_exports__ = {};
175
169
  "use strict";
176
170
  __webpack_require__.r(__webpack_exports__);
177
171
  __webpack_require__.d(__webpack_exports__, {
178
- PLUGIN_ASSETS_RETRY_NAME: ()=>PLUGIN_ASSETS_RETRY_NAME,
179
- pluginAssetsRetry: ()=>pluginAssetsRetry
172
+ pluginAssetsRetry: ()=>pluginAssetsRetry,
173
+ PLUGIN_ASSETS_RETRY_NAME: ()=>PLUGIN_ASSETS_RETRY_NAME
180
174
  });
181
175
  const external_node_fs_namespaceObject = require("node:fs");
182
176
  var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
@@ -238,12 +232,9 @@ var __webpack_exports__ = {};
238
232
  compilation.hooks.runtimeModule.tap(this.name, (module)=>{
239
233
  var _module_constructor;
240
234
  const { isRspack } = this.options;
241
- const constructorName = isRspack ? module.constructorName : null === (_module_constructor = module.constructor) || void 0 === _module_constructor ? void 0 : _module_constructor.name;
235
+ const constructorName = isRspack ? module.constructorName : null == (_module_constructor = module.constructor) ? void 0 : _module_constructor.name;
242
236
  const isCssLoadingRuntimeModule = 'CssLoadingRuntimeModule' === constructorName;
243
- if (isCssLoadingRuntimeModule) {
244
- modifyRuntimeModule(module, (originSource)=>originSource.replace('var fullhref = __webpack_require__.p + href;', 'var fullhref = __webpack_require__.rbLoadStyleSheet ? __webpack_require__.rbLoadStyleSheet(href, chunkId) : (__webpack_require__.p + href);'), isRspack);
245
- return;
246
- }
237
+ if (isCssLoadingRuntimeModule) return void modifyRuntimeModule(module, (originSource)=>originSource.replace('var fullhref = __webpack_require__.p + href;', 'var fullhref = __webpack_require__.rbLoadStyleSheet ? __webpack_require__.rbLoadStyleSheet(href, chunkId) : (__webpack_require__.p + href);'), isRspack);
247
238
  const isPublicPathModule = 'publicPath' === module.name || 'PublicPathRuntimeModule' === constructorName || 'AutoPublicPathRuntimeModule' === constructorName;
248
239
  if (isPublicPathModule) {
249
240
  const runtimeCode = this.getRawRuntimeRetryCode();
@@ -306,7 +297,7 @@ var __webpack_exports__ = {};
306
297
  const { inlineScript = true } = userOptions;
307
298
  const getScriptPath = (environment)=>{
308
299
  const distDir = environment.config.output.distPath.js;
309
- return external_node_path_default().posix.join(distDir, "assets-retry.1-2-0.js");
300
+ return external_node_path_default().posix.join(distDir, "assets-retry.1-2-2.js");
310
301
  };
311
302
  const formatOptions = (config)=>{
312
303
  const options = {
@@ -315,7 +306,7 @@ var __webpack_exports__ = {};
315
306
  if (void 0 === options.crossOrigin) options.crossOrigin = config.html.crossorigin;
316
307
  if (void 0 === options.minify) {
317
308
  var _config_output_minify;
318
- const minify = 'boolean' == typeof config.output.minify ? config.output.minify : null === (_config_output_minify = config.output.minify) || void 0 === _config_output_minify ? void 0 : _config_output_minify.js;
309
+ const minify = 'boolean' == typeof config.output.minify ? config.output.minify : null == (_config_output_minify = config.output.minify) ? void 0 : _config_output_minify.js;
319
310
  options.minify = minify && 'production' === config.mode;
320
311
  }
321
312
  return options;
@@ -370,8 +361,12 @@ var __webpack_exports__ = {};
370
361
  }
371
362
  });
372
363
  })();
373
- var __webpack_export_target__ = exports;
374
- for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
375
- if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
364
+ exports.PLUGIN_ASSETS_RETRY_NAME = __webpack_exports__.PLUGIN_ASSETS_RETRY_NAME;
365
+ exports.pluginAssetsRetry = __webpack_exports__.pluginAssetsRetry;
366
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
367
+ "PLUGIN_ASSETS_RETRY_NAME",
368
+ "pluginAssetsRetry"
369
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
370
+ Object.defineProperty(exports, '__esModule', {
376
371
  value: true
377
372
  });
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as __WEBPACK_EXTERNAL_MODULE_crypto__ from "crypto";
2
- import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs";
3
- import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path";
4
- import * as __WEBPACK_EXTERNAL_MODULE_node_url_e96de089__ from "node:url";
5
- import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__ from "@rsbuild/core";
2
+ import node_fs from "node:fs";
3
+ import node_path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import { ensureAssetPrefix, rspack } from "@rsbuild/core";
6
6
  var __webpack_modules__ = {
7
7
  "./node_modules/.pnpm/randombytes@2.1.0/node_modules/randombytes/index.js": function(module, __unused_webpack_exports, __webpack_require__) {
8
8
  module.exports = __webpack_require__("crypto").randomBytes;
@@ -135,12 +135,8 @@ function __webpack_require__(moduleId) {
135
135
  return module.exports;
136
136
  }
137
137
  (()=>{
138
- __webpack_require__.n = function(module) {
139
- var getter = module && module.__esModule ? function() {
140
- return module['default'];
141
- } : function() {
142
- return module;
143
- };
138
+ __webpack_require__.n = (module)=>{
139
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
144
140
  __webpack_require__.d(getter, {
145
141
  a: getter
146
142
  });
@@ -148,7 +144,7 @@ function __webpack_require__(moduleId) {
148
144
  };
149
145
  })();
150
146
  (()=>{
151
- __webpack_require__.d = function(exports, definition) {
147
+ __webpack_require__.d = (exports, definition)=>{
152
148
  for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) Object.defineProperty(exports, key, {
153
149
  enumerable: true,
154
150
  get: definition[key]
@@ -156,9 +152,7 @@ function __webpack_require__(moduleId) {
156
152
  };
157
153
  })();
158
154
  (()=>{
159
- __webpack_require__.o = function(obj, prop) {
160
- return Object.prototype.hasOwnProperty.call(obj, prop);
161
- };
155
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
162
156
  })();
163
157
  var serialize_javascript = __webpack_require__("./node_modules/.pnpm/serialize-javascript@6.0.2/node_modules/serialize-javascript/index.js");
164
158
  var serialize_javascript_default = /*#__PURE__*/ __webpack_require__.n(serialize_javascript);
@@ -172,7 +166,7 @@ function _define_property(obj, key, value) {
172
166
  else obj[key] = value;
173
167
  return obj;
174
168
  }
175
- const AsyncChunkRetryPlugin_dirname = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].dirname((0, __WEBPACK_EXTERNAL_MODULE_node_url_e96de089__.fileURLToPath)(import.meta.url));
169
+ const AsyncChunkRetryPlugin_dirname = node_path.dirname(fileURLToPath(import.meta.url));
176
170
  function modifyWebpackRuntimeModule(module, modifier) {
177
171
  try {
178
172
  const originSource = module.getGeneratedCode();
@@ -203,10 +197,10 @@ function pick(obj, keys) {
203
197
  }
204
198
  class AsyncChunkRetryPlugin {
205
199
  getRawRuntimeRetryCode() {
206
- const { RuntimeGlobals } = __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.rspack;
200
+ const { RuntimeGlobals } = rspack;
207
201
  const filename = 'asyncChunkRetry';
208
- const runtimeFilePath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(AsyncChunkRetryPlugin_dirname, 'runtime', this.options.minify ? `${filename}.min.js` : `${filename}.js`);
209
- const rawText = __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].readFileSync(runtimeFilePath, 'utf-8');
202
+ const runtimeFilePath = node_path.join(AsyncChunkRetryPlugin_dirname, 'runtime', this.options.minify ? `${filename}.min.js` : `${filename}.js`);
203
+ const rawText = node_fs.readFileSync(runtimeFilePath, 'utf-8');
210
204
  return rawText.replaceAll('__RUNTIME_GLOBALS_REQUIRE__', RuntimeGlobals.require).replaceAll('__RUNTIME_GLOBALS_ENSURE_CHUNK__', RuntimeGlobals.ensureChunk).replaceAll('__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__', RuntimeGlobals.getChunkScriptFilename).replaceAll('__RUNTIME_GLOBALS_GET_CSS_FILENAME__', RuntimeGlobals.getChunkCssFilename).replaceAll('__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__', '__webpack_require__.miniCssF').replaceAll('__RUNTIME_GLOBALS_RSBUILD_LOAD_STYLESHEET__', '__webpack_require__.rbLoadStyleSheet').replaceAll('__RUNTIME_GLOBALS_PUBLIC_PATH__', RuntimeGlobals.publicPath).replaceAll('__RUNTIME_GLOBALS_LOAD_SCRIPT__', RuntimeGlobals.loadScript).replaceAll('__RETRY_OPTIONS__', serialize_javascript_default()(this.runtimeOptions));
211
205
  }
212
206
  apply(compiler) {
@@ -214,12 +208,9 @@ class AsyncChunkRetryPlugin {
214
208
  compilation.hooks.runtimeModule.tap(this.name, (module)=>{
215
209
  var _module_constructor;
216
210
  const { isRspack } = this.options;
217
- const constructorName = isRspack ? module.constructorName : null === (_module_constructor = module.constructor) || void 0 === _module_constructor ? void 0 : _module_constructor.name;
211
+ const constructorName = isRspack ? module.constructorName : null == (_module_constructor = module.constructor) ? void 0 : _module_constructor.name;
218
212
  const isCssLoadingRuntimeModule = 'CssLoadingRuntimeModule' === constructorName;
219
- if (isCssLoadingRuntimeModule) {
220
- modifyRuntimeModule(module, (originSource)=>originSource.replace('var fullhref = __webpack_require__.p + href;', 'var fullhref = __webpack_require__.rbLoadStyleSheet ? __webpack_require__.rbLoadStyleSheet(href, chunkId) : (__webpack_require__.p + href);'), isRspack);
221
- return;
222
- }
213
+ if (isCssLoadingRuntimeModule) return void modifyRuntimeModule(module, (originSource)=>originSource.replace('var fullhref = __webpack_require__.p + href;', 'var fullhref = __webpack_require__.rbLoadStyleSheet ? __webpack_require__.rbLoadStyleSheet(href, chunkId) : (__webpack_require__.p + href);'), isRspack);
223
214
  const isPublicPathModule = 'publicPath' === module.name || 'PublicPathRuntimeModule' === constructorName || 'AutoPublicPathRuntimeModule' === constructorName;
224
215
  if (isPublicPathModule) {
225
216
  const runtimeCode = this.getRawRuntimeRetryCode();
@@ -245,7 +236,7 @@ class AsyncChunkRetryPlugin {
245
236
  ]);
246
237
  }
247
238
  }
248
- const src_dirname = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].dirname((0, __WEBPACK_EXTERNAL_MODULE_node_url_e96de089__.fileURLToPath)(import.meta.url));
239
+ const src_dirname = node_path.dirname(fileURLToPath(import.meta.url));
249
240
  const PLUGIN_ASSETS_RETRY_NAME = 'rsbuild:assets-retry';
250
241
  function getRuntimeOptions(userOptions) {
251
242
  const defaultOptions = {
@@ -271,8 +262,8 @@ function getRuntimeOptions(userOptions) {
271
262
  async function getRetryCode(options) {
272
263
  const filename = 'initialChunkRetry';
273
264
  const { minify, inlineScript: _, ...restOptions } = options;
274
- const runtimeFilePath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(src_dirname, 'runtime', minify ? `${filename}.min.js` : `${filename}.js`);
275
- const runtimeCode = await __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].promises.readFile(runtimeFilePath, 'utf-8');
265
+ const runtimeFilePath = node_path.join(src_dirname, 'runtime', minify ? `${filename}.min.js` : `${filename}.js`);
266
+ const runtimeCode = await node_fs.promises.readFile(runtimeFilePath, 'utf-8');
276
267
  const runtimeOptions = getRuntimeOptions(restOptions);
277
268
  return `(function(){${runtimeCode}})()`.replace('__RUNTIME_GLOBALS_OPTIONS__', serialize_javascript_default()(runtimeOptions));
278
269
  }
@@ -282,7 +273,7 @@ const pluginAssetsRetry = (userOptions = {})=>({
282
273
  const { inlineScript = true } = userOptions;
283
274
  const getScriptPath = (environment)=>{
284
275
  const distDir = environment.config.output.distPath.js;
285
- return __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].posix.join(distDir, "assets-retry.1-2-0.js");
276
+ return node_path.posix.join(distDir, "assets-retry.1-2-2.js");
286
277
  };
287
278
  const formatOptions = (config)=>{
288
279
  const options = {
@@ -291,7 +282,7 @@ const pluginAssetsRetry = (userOptions = {})=>({
291
282
  if (void 0 === options.crossOrigin) options.crossOrigin = config.html.crossorigin;
292
283
  if (void 0 === options.minify) {
293
284
  var _config_output_minify;
294
- const minify = 'boolean' == typeof config.output.minify ? config.output.minify : null === (_config_output_minify = config.output.minify) || void 0 === _config_output_minify ? void 0 : _config_output_minify.js;
285
+ const minify = 'boolean' == typeof config.output.minify ? config.output.minify : null == (_config_output_minify = config.output.minify) ? void 0 : _config_output_minify.js;
295
286
  options.minify = minify && 'production' === config.mode;
296
287
  }
297
288
  return options;
@@ -311,7 +302,7 @@ const pluginAssetsRetry = (userOptions = {})=>({
311
302
  else {
312
303
  api.modifyHTMLTags(async ({ headTags, bodyTags }, { assetPrefix, environment })=>{
313
304
  const scriptPath = getScriptPath(environment);
314
- const url = (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.ensureAssetPrefix)(scriptPath, assetPrefix);
305
+ const url = ensureAssetPrefix(scriptPath, assetPrefix);
315
306
  headTags.unshift({
316
307
  tag: "script",
317
308
  attrs: {
@@ -53,16 +53,15 @@ function getCurrentRetry(chunkId, existRetryTimes, isCssAsyncChunk) {
53
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];
54
54
  }
55
55
  function initRetry(chunkId, isCssAsyncChunk) {
56
- var _config_domain;
57
56
  var originalScriptFilename = isCssAsyncChunk ? originalGetCssFilename(chunkId) : originalGetChunkScriptFilename(chunkId);
58
57
  if (!originalScriptFilename) {
59
58
  throw new Error('only support cssExtract');
60
59
  }
61
60
  var originalPublicPath = __RUNTIME_GLOBALS_PUBLIC_PATH__;
62
- var originalSrcUrl = originalPublicPath.startsWith('/') ? window.origin + originalPublicPath + originalScriptFilename : originalPublicPath + originalScriptFilename;
61
+ var originalSrcUrl = originalPublicPath[0] === '/' && originalPublicPath[1] !== '/' ? window.origin + originalPublicPath + originalScriptFilename : originalPublicPath + originalScriptFilename;
63
62
  var originalQuery = getQueryFromUrl(originalSrcUrl);
64
63
  var existRetryTimes = 0;
65
- var nextDomain = ((_config_domain = config.domain) === null || _config_domain === void 0 ? void 0 : _config_domain[0]) || window.origin;
64
+ var nextDomain = findCurrentDomain(originalSrcUrl);
66
65
  return {
67
66
  nextDomain: nextDomain,
68
67
  nextRetryUrl: getNextRetryUrl(originalSrcUrl, nextDomain, nextDomain, existRetryTimes, originalQuery),
@@ -1 +1 @@
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={},l=__RUNTIME_GLOBALS_ENSURE_CHUNK__,c=__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 y=d[10],S=l.apply(null,d);try{var v=c(t),R=u(t);"undefined"!=typeof window&&(v&&(window.__RB_ASYNC_CHUNKS__[v]=!0),R&&(window.__RB_ASYNC_CHUNKS__[R]=!0))}catch(n){console.error(s,"get original script or CSS filename error",n)}return y&&"number"==typeof y.count&&"number"==typeof y.cssFailedCount?(y.count+=1,S.catch(function(l){var S,v,R,p,E,U,L=y.count-1,m=y.cssFailedCount,g=!!(null==l?void 0:null===(S=l.message)||void 0===S?void 0:S.includes("CSS chunk"));g&&(y.cssFailedCount+=1);var N=g?m:L-m;try{var A=function(r,t,l){var d,s,f,y=l?null===(d=i[r])||void 0===d?void 0:d[t]:null===(s=e[r])||void 0===s?void 0:s[t],S=t+1;if(0===t||void 0===y)f=function(r,e){var i,t,_=e?u(r):c(r);if(!_)throw Error("only support cssExtract");var a=__RUNTIME_GLOBALS_PUBLIC_PATH__,l=a.startsWith("/")?window.origin+a+_:a+_,d=(i=l.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(l,s,s,0,d),originalScriptFilename:_,originalSrcUrl:l,originalQuery:d}}(r,l),l?i[r]=[]:e[r]=[];else{var v,R,p,E,U=y.originalScriptFilename,L=y.originalSrcUrl,m=y.originalQuery,g=(v=y.nextDomain,R=n.domain||[],p=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}(v),E=R.indexOf(p),R[(E+1)%R.length]||v);f={nextDomain:g,nextRetryUrl:o(y.nextRetryUrl,y.nextDomain,g,t,m),originalScriptFilename:U,originalSrcUrl:L,originalQuery:m}}return l?(i[r][S]=f,a[r]=f):(e[r][S]=f,_[r]=f),f}(t,N,g);v=A.originalScriptFilename,R=A.nextRetryUrl,p=A.nextDomain}catch(n){throw console.error(s,"failed to get nextRetryUrl",n),l}var C=function(n){return{times:n,domain:p,url:R,tagName:g?"link":"script",isAsyncChunk:!0}},T=C(N);if(N>=r)throw l.message=(null===(E=l.message)||void 0===E?void 0:E.includes("retries:"))?l.message:"Loading chunk ".concat(t,' from "').concat(v,'" failed after ').concat(r,' retries: "').concat(l.message,'"'),"function"==typeof n.onFail&&n.onFail(T),l;var w=n.test;if(w){if("string"==typeof w){var I=new RegExp(w);w=function(n){return I.test(n)}}if("function"!=typeof w||!w(R))throw l}if(n.domain&&-1===n.domain.indexOf(p))throw l;"function"==typeof n.onRetry&&n.onRetry(T);var O="function"==typeof n.delay?n.delay(T):null!==(U=n.delay)&&void 0!==U?U:0;return(O>0?new Promise(function(n){return setTimeout(n,O)}):Promise.resolve()).then(function(){return f.apply(f,d)}).then(function(r){var e=(null==y?void 0:y.count)===L+2;if("function"==typeof n.onSuccess&&e){var i=C(N+1);n.onSuccess(i)}return r})})):S}function y(){var n=Array.prototype.slice.call(arguments),r=_[n[3]];return r&&(n[0]=r.nextRetryUrl),d.apply(null,n)}function S(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__=y,__RUNTIME_GLOBALS_RSBUILD_LOAD_STYLESHEET__=S}catch(n){console.error(s,"Register async chunk retry runtime failed",n)}}();
1
+ var n=__RETRY_OPTIONS__,r=n.max||3,e={},t={};function i(r){for(var e=n.domain||[],t="",i=0;i<e.length;i++)if(-1!==r.indexOf(e[i])){t=e[i];break}return t||window.origin}var o=/[?#].*$/;function _(r,e,t,i,_){var a;return r.replace(e,t).replace(o,"")+(a=i+1,!0===n.addQuery?""!==_?"".concat(_,"&retry=").concat(a):"?retry=".concat(a):"function"==typeof n.addQuery?n.addQuery({times:a,originalQuery:_}):"")}var a={},l={},c=__RUNTIME_GLOBALS_ENSURE_CHUNK__,u=__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__,s=__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__||__RUNTIME_GLOBALS_GET_CSS_FILENAME__||function(){return null},f=__RUNTIME_GLOBALS_LOAD_SCRIPT__,d="[@rsbuild/plugin-assets-retry] ";function y(o){var f=Array.prototype.slice.call(arguments);f[10]||(f[10]={count:0,cssFailedCount:0});var S=f[10],R=c.apply(null,f);try{var p=u(o),E=s(o);"undefined"!=typeof window&&(p&&(window.__RB_ASYNC_CHUNKS__[p]=!0),E&&(window.__RB_ASYNC_CHUNKS__[E]=!0))}catch(n){console.error(d,"get original script or CSS filename error",n)}return S&&"number"==typeof S.count&&"number"==typeof S.cssFailedCount?(S.count+=1,R.catch(function(c){var R,p,E,U,L,m,v=S.count-1,N=S.cssFailedCount,g=!!(null==c||null==(R=c.message)?void 0:R.includes("CSS chunk"));g&&(S.cssFailedCount+=1);var A=g?N:v-N;try{var C=function(r,o,c){var f,d,y,S=c?null==(f=t[r])?void 0:f[o]:null==(d=e[r])?void 0:d[o],R=o+1;if(0===o||void 0===S)y=function(n,r){var e,t=r?s(n):u(n);if(!t)throw Error("only support cssExtract");var o=__RUNTIME_GLOBALS_PUBLIC_PATH__,a="/"===o[0]&&"/"!==o[1]?window.origin+o+t:o+t,l=(e=a.split("?")[1])?"?".concat(e.split("#")[0]):"",c=i(a);return{nextDomain:c,nextRetryUrl:_(a,c,c,0,l),originalScriptFilename:t,originalSrcUrl:a,originalQuery:l}}(r,c),c?t[r]=[]:e[r]=[];else{var p,E,U,L,m=S.originalScriptFilename,v=S.originalSrcUrl,N=S.originalQuery,g=(p=S.nextDomain,E=n.domain||[],U=i(p),L=E.indexOf(U),E[(L+1)%E.length]||p);y={nextDomain:g,nextRetryUrl:_(S.nextRetryUrl,S.nextDomain,g,o,N),originalScriptFilename:m,originalSrcUrl:v,originalQuery:N}}return c?(t[r][R]=y,l[r]=y):(e[r][R]=y,a[r]=y),y}(o,A,g);p=C.originalScriptFilename,E=C.nextRetryUrl,U=C.nextDomain}catch(n){throw console.error(d,"failed to get nextRetryUrl",n),c}var T=function(n){return{times:n,domain:U,url:E,tagName:g?"link":"script",isAsyncChunk:!0}},I=T(A);if(A>=r)throw c.message=(null==(L=c.message)?void 0:L.includes("retries:"))?c.message:"Loading chunk ".concat(o,' from "').concat(p,'" failed after ').concat(r,' retries: "').concat(c.message,'"'),"function"==typeof n.onFail&&n.onFail(I),c;var w=n.test;if(w){if("string"==typeof w){var O=new RegExp(w);w=function(n){return O.test(n)}}if("function"!=typeof w||!w(E))throw c}if(n.domain&&-1===n.domain.indexOf(U))throw c;"function"==typeof n.onRetry&&n.onRetry(I);var x="function"==typeof n.delay?n.delay(I):null!=(m=n.delay)?m:0;return(x>0?new Promise(function(n){return setTimeout(n,x)}):Promise.resolve()).then(function(){return y.apply(y,f)}).then(function(r){var e=(null==S?void 0:S.count)===v+2;if("function"==typeof n.onSuccess&&e){var t=T(A+1);n.onSuccess(t)}return r})})):R}if("undefined"==typeof window||window.__RB_ASYNC_CHUNKS__||(window.__RB_ASYNC_CHUNKS__={}),"undefined"!=typeof __RUNTIME_GLOBALS_REQUIRE__)try{__RUNTIME_GLOBALS_ENSURE_CHUNK__=y,__RUNTIME_GLOBALS_LOAD_SCRIPT__=function(){var n=Array.prototype.slice.call(arguments),r=a[n[3]];return r&&(n[0]=r.nextRetryUrl),f.apply(null,n)},__RUNTIME_GLOBALS_RSBUILD_LOAD_STYLESHEET__=function(n,r){var e=l[r];return e&&e.nextRetryUrl||__RUNTIME_GLOBALS_PUBLIC_PATH__+n}}catch(n){console.error(d,"Register async chunk retry runtime failed",n)}
@@ -30,9 +30,18 @@ function findNextDomain(url, domains) {
30
30
  }
31
31
  function getRequestUrl(element) {
32
32
  if (_instanceof(element, HTMLScriptElement) || _instanceof(element, HTMLImageElement)) {
33
+ // For <script src="" /> or <img src="" />
34
+ // element.getAttribute('src') === '' but element.src === baseURI
35
+ if (!element.getAttribute('src')) {
36
+ return null;
37
+ }
33
38
  return element.src;
34
39
  }
35
40
  if (_instanceof(element, HTMLLinkElement)) {
41
+ // For <link href="" />
42
+ if (!element.getAttribute('href')) {
43
+ return null;
44
+ }
36
45
  return element.href;
37
46
  }
38
47
  return null;
@@ -1 +1 @@
1
- function e(e,t){return null!=t&&"undefined"!=typeof Symbol&&t[Symbol.hasInstance]?!!t[Symbol.hasInstance](e):e instanceof t}var t={link:HTMLLinkElement,script:HTMLScriptElement,img:HTMLImageElement};function n(e,t){for(var n="",r=0;r<t.length;r++)if(-1!==e.indexOf(t[r])){n=t[r];break}return n||window.origin}function r(n,r){var i=r.target,a=i.tagName.toLocaleLowerCase(),o=n.type,c=e(i,HTMLScriptElement)||e(i,HTMLImageElement)?i.src:e(i,HTMLLinkElement)?i.href:null;return!!(a&&-1!==o.indexOf(a)&&t[a]&&e(i,t[a]))&&!!c&&{target:i,tagName:a,url:c}}var i=/[?#].*$/;function a(t,n,r){e(t,HTMLScriptElement)&&(r.isAsync?document.body.appendChild(n.element):document.write(n.str)),e(t,HTMLLinkElement)&&document.getElementsByTagName("head")[0].appendChild(n.element),e(t,HTMLImageElement)&&(t.src=r.url,t.dataset.rbRetryTimes=String(r.times),t.dataset.rbOriginalQuery=String(r.originalQuery))}"undefined"==typeof window||window.__RB_ASYNC_CHUNKS__||(window.__RB_ASYNC_CHUNKS__={});try{var o,c,s=__RUNTIME_GLOBALS_OPTIONS__;o=function(t){try{!function(t,o){var c,s,l,u,d,m,y,f=r(t,o);if(!1!==f){var g=f.target,p=f.tagName,v=f.url;if(!("undefined"!=typeof window&&Object.keys(window.__RB_ASYNC_CHUNKS__||{}).some(function(e){return -1!==v.indexOf(e)}))){var _=t.test;if(_){if("string"==typeof _){var b=new RegExp(_);_=function(e){return b.test(e)}}if("function"!=typeof _||!_(v))return}var L=n(v,t.domain);if(!t.domain||!(t.domain.length>0)||-1!==t.domain.indexOf(L)){var S=Number(g.dataset.rbRetryTimes)||0;if(S===t.max){"function"==typeof t.onFail&&t.onFail({times:S,domain:L,url:v,tagName:p,isAsyncChunk:!1});return}var E=(s=n(L,c=t.domain),l=c.indexOf(s),c[(l+1)%c.length]||L),T=null!==(m=g.dataset.rbOriginalQuery)&&void 0!==m?m:(u=v.split("?")[1])?"?".concat(u.split("#")[0]):"",w=!!g.dataset.rbAsync||g.async||g.defer,O={url:v.replace(L,E).replace(i,"")+(d=S+1,!0===t.addQuery?""!==T?"".concat(T,"&retry=").concat(d):"?retry=".concat(d):"function"==typeof t.addQuery?t.addQuery({times:d,originalQuery:T}):""),times:S+1,crossOrigin:t.crossOrigin,isAsync:w,originalQuery:T},h=function(t,n){var r=!0===n.crossOrigin?"anonymous":n.crossOrigin,i=r?'crossorigin="'.concat(r,'"'):"",a=n.times?'data-rb-retry-times="'.concat(n.times,'"'):"",o=n.originalQuery?'data-rb-original-query="'.concat(n.originalQuery,'"'):"",c=n.isAsync?"data-rb-async":"";if(e(t,HTMLScriptElement)){var s=document.createElement("script");return s.src=n.url,r&&(s.crossOrigin=r),n.times&&(s.dataset.rbRetryTimes=String(n.times)),n.isAsync&&(s.dataset.rbAsync=""),void 0!==n.originalQuery&&(s.dataset.rbOriginalQuery=n.originalQuery),{element:s,str:'<script src="'.concat(n.url,'" ').concat(i," ").concat(a," ").concat(c," ").concat(o,">")+"<\/script>"}}if(e(t,HTMLLinkElement)){var l=document.createElement("link");return l.rel=t.rel||"stylesheet",t.as&&(l.as=t.as),l.href=n.url,r&&(l.crossOrigin=r),n.times&&(l.dataset.rbRetryTimes=String(n.times)),void 0!==n.originalQuery&&(l.dataset.rbOriginalQuery=n.originalQuery),{element:l,str:'<link rel="'.concat(l.rel,'" href="').concat(n.url,'" ').concat(i," ").concat(a," ").concat(l.as?'as="'.concat(l.as,'"'):""," ").concat(o,"></link>")}}}(g,O),N={times:S,domain:L,url:v,tagName:p,isAsyncChunk:!1};"function"==typeof t.onRetry&&t.onRetry(N);var Q="function"==typeof t.delay?t.delay(N):null!==(y=t.delay)&&void 0!==y?y:0;Q>0?setTimeout(function(){a(g,h,O)},Q):a(g,h,O)}}}}(s,t)}catch(e){console.error("retry error captured",e)}},c=function(e){try{!function(e,t){var i=r(e,t);if(!1!==i){var a=i.target,o=i.tagName,c=i.url,s=n(c,e.domain),l=Number(a.dataset.rbRetryTimes)||0;0!==l&&"function"==typeof e.onSuccess&&e.onSuccess({times:l,domain:s,url:c,tagName:o,isAsyncChunk:!1})}}(s,e)}catch(e){console.error("load error captured",e)}},"undefined"!=typeof window&&void 0!==window.document&&(document.addEventListener("error",function(t){t&&e(t.target,Element)&&o(t)},!0),document.addEventListener("load",function(t){t&&e(t.target,Element)&&c(t)},!0))}catch(e){console.error("monitor error captured",e)}
1
+ function e(e,t){return null!=t&&"undefined"!=typeof Symbol&&t[Symbol.hasInstance]?!!t[Symbol.hasInstance](e):e instanceof t}var t={link:HTMLLinkElement,script:HTMLScriptElement,img:HTMLImageElement};function n(e,t){for(var n="",r=0;r<t.length;r++)if(-1!==e.indexOf(t[r])){n=t[r];break}return n||window.origin}function r(n,r){var i,a=r.target,o=a.tagName.toLocaleLowerCase(),c=n.type,s=e(i=a,HTMLScriptElement)||e(i,HTMLImageElement)?i.getAttribute("src")?i.src:null:e(i,HTMLLinkElement)?i.getAttribute("href")?i.href:null:null;return!!o&&-1!==c.indexOf(o)&&!!t[o]&&!!e(a,t[o])&&!!s&&{target:a,tagName:o,url:s}}var i=/[?#].*$/;function a(t,n,r){e(t,HTMLScriptElement)&&(r.isAsync?document.body.appendChild(n.element):document.write(n.str)),e(t,HTMLLinkElement)&&document.getElementsByTagName("head")[0].appendChild(n.element),e(t,HTMLImageElement)&&(t.src=r.url,t.dataset.rbRetryTimes=String(r.times),t.dataset.rbOriginalQuery=String(r.originalQuery))}"undefined"==typeof window||window.__RB_ASYNC_CHUNKS__||(window.__RB_ASYNC_CHUNKS__={});try{var o,c,s=__RUNTIME_GLOBALS_OPTIONS__;o=function(t){try{!function(t,o){var c,s,l=r(t,o);if(!1!==l){var u=l.target,d=l.tagName,m=l.url;if(!("undefined"!=typeof window&&Object.keys(window.__RB_ASYNC_CHUNKS__||{}).some(function(e){return -1!==m.indexOf(e)}))){var y=t.test;if(y){if("string"==typeof y){var f=new RegExp(y);y=function(e){return f.test(e)}}if("function"!=typeof y||!y(m))return}var g=n(m,t.domain);if(!t.domain||!(t.domain.length>0)||-1!==t.domain.indexOf(g)){var p,b,_,v,L,S,E,T,h,w=Number(u.dataset.rbRetryTimes)||0;if(w===t.max){"function"==typeof t.onFail&&t.onFail({times:w,domain:g,url:m,tagName:d,isAsyncChunk:!1});return}var O=(E=n(g,S=t.domain),T=S.indexOf(E),S[(T+1)%S.length]||g),N=null!=(c=u.dataset.rbOriginalQuery)?c:(h=m.split("?")[1])?"?".concat(h.split("#")[0]):"",Q=!!u.dataset.rbAsync||u.async||u.defer,A={url:(p=m,b=g,_=O,v=w,p.replace(b,_).replace(i,"")+(L=v+1,!0===t.addQuery?""!==N?"".concat(N,"&retry=").concat(L):"?retry=".concat(L):"function"==typeof t.addQuery?t.addQuery({times:L,originalQuery:N}):"")),times:w+1,crossOrigin:t.crossOrigin,isAsync:Q,originalQuery:N},H=function(t,n){var r=!0===n.crossOrigin?"anonymous":n.crossOrigin,i=r?'crossorigin="'.concat(r,'"'):"",a=n.times?'data-rb-retry-times="'.concat(n.times,'"'):"",o=n.originalQuery?'data-rb-original-query="'.concat(n.originalQuery,'"'):"",c=n.isAsync?"data-rb-async":"";if(e(t,HTMLScriptElement)){var s=document.createElement("script");return s.src=n.url,r&&(s.crossOrigin=r),n.times&&(s.dataset.rbRetryTimes=String(n.times)),n.isAsync&&(s.dataset.rbAsync=""),void 0!==n.originalQuery&&(s.dataset.rbOriginalQuery=n.originalQuery),{element:s,str:'<script src="'.concat(n.url,'" ').concat(i," ").concat(a," ").concat(c," ").concat(o,">")+"<\/script>"}}if(e(t,HTMLLinkElement)){var l=document.createElement("link");return l.rel=t.rel||"stylesheet",t.as&&(l.as=t.as),l.href=n.url,r&&(l.crossOrigin=r),n.times&&(l.dataset.rbRetryTimes=String(n.times)),void 0!==n.originalQuery&&(l.dataset.rbOriginalQuery=n.originalQuery),{element:l,str:'<link rel="'.concat(l.rel,'" href="').concat(n.url,'" ').concat(i," ").concat(a," ").concat(l.as?'as="'.concat(l.as,'"'):""," ").concat(o,"></link>")}}}(u,A),k={times:w,domain:g,url:m,tagName:d,isAsyncChunk:!1};"function"==typeof t.onRetry&&t.onRetry(k);var C="function"==typeof t.delay?t.delay(k):null!=(s=t.delay)?s:0;C>0?setTimeout(function(){a(u,H,A)},C):a(u,H,A)}}}}(s,t)}catch(e){console.error("retry error captured",e)}},c=function(e){try{!function(e,t){var i=r(e,t);if(!1!==i){var a=i.target,o=i.tagName,c=i.url,s=n(c,e.domain),l=Number(a.dataset.rbRetryTimes)||0;0!==l&&"function"==typeof e.onSuccess&&e.onSuccess({times:l,domain:s,url:c,tagName:o,isAsyncChunk:!1})}}(s,e)}catch(e){console.error("load error captured",e)}},"undefined"!=typeof window&&void 0!==window.document&&(document.addEventListener("error",function(t){t&&e(t.target,Element)&&o(t)},!0),document.addEventListener("load",function(t){t&&e(t.target,Element)&&c(t)},!0))}catch(e){console.error("monitor error captured",e)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-assets-retry",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "repository": "https://github.com/rspack-contrib/rsbuild-plugin-assets-retry",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -29,21 +29,21 @@
29
29
  },
30
30
  "devDependencies": {
31
31
  "@biomejs/biome": "^1.9.4",
32
- "@playwright/test": "^1.50.1",
33
- "@rsbuild/core": "^1.2.3",
34
- "@rsbuild/plugin-react": "^1.1.1",
35
- "@rslib/core": "^0.4.0",
36
- "@swc/core": "^1.10.18",
37
- "@types/node": "^22.13.0",
38
- "@types/react": "^19.0.10",
39
- "@types/react-dom": "^19.0.4",
32
+ "@playwright/test": "^1.53.0",
33
+ "@rsbuild/core": "^1.3.22",
34
+ "@rsbuild/plugin-react": "^1.3.2",
35
+ "@rslib/core": "^0.10.0",
36
+ "@swc/core": "^1.12.1",
37
+ "@types/node": "^22.15.32",
38
+ "@types/react": "^19.1.8",
39
+ "@types/react-dom": "^19.1.6",
40
40
  "@types/serialize-javascript": "^5.0.4",
41
- "playwright": "^1.50.1",
42
- "react": "^19.0.0",
43
- "react-dom": "^19.0.0",
41
+ "playwright": "^1.53.0",
42
+ "react": "^19.1.0",
43
+ "react-dom": "^19.1.0",
44
44
  "serialize-javascript": "^6.0.2",
45
- "simple-git-hooks": "^2.11.1",
46
- "typescript": "^5.7.3"
45
+ "simple-git-hooks": "^2.13.0",
46
+ "typescript": "^5.8.3"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "@rsbuild/core": "1.x"
@@ -53,7 +53,7 @@
53
53
  "optional": true
54
54
  }
55
55
  },
56
- "packageManager": "pnpm@9.15.4",
56
+ "packageManager": "pnpm@9.15.9",
57
57
  "publishConfig": {
58
58
  "access": "public",
59
59
  "registry": "https://registry.npmjs.org/"