@rsbuild/plugin-assets-retry 0.6.14 → 0.7.0-beta.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.
@@ -0,0 +1,19 @@
1
+ import { type Rspack } from '@rsbuild/shared';
2
+ import type HtmlWebpackPlugin from 'html-webpack-plugin';
3
+ import type { PluginAssetsRetryOptions } from './types';
4
+ export declare class AssetsRetryPlugin implements Rspack.RspackPluginInstance {
5
+ #private;
6
+ readonly name: string;
7
+ readonly distDir: string;
8
+ readonly inlineScript: boolean;
9
+ readonly minify?: boolean;
10
+ readonly HtmlPlugin: typeof HtmlWebpackPlugin;
11
+ scriptPath: string;
12
+ constructor(options: PluginAssetsRetryOptions & {
13
+ distDir: string;
14
+ HtmlPlugin: typeof HtmlWebpackPlugin;
15
+ });
16
+ getRetryCode(): Promise<string>;
17
+ getScriptPath(): Promise<string>;
18
+ apply(compiler: Rspack.Compiler): void;
19
+ }
@@ -0,0 +1,15 @@
1
+ import { type Rspack } from '@rsbuild/core';
2
+ import type { PluginAssetsRetryOptions, RuntimeRetryOptions } from './types';
3
+ declare class AsyncChunkRetryPlugin implements Rspack.RspackPluginInstance {
4
+ readonly name = "ASYNC_CHUNK_RETRY_PLUGIN";
5
+ readonly options: PluginAssetsRetryOptions & {
6
+ isRspack: boolean;
7
+ };
8
+ readonly runtimeOptions: RuntimeRetryOptions;
9
+ constructor(options: PluginAssetsRetryOptions & {
10
+ isRspack: boolean;
11
+ });
12
+ getRawRuntimeRetryCode(): string;
13
+ apply(compiler: Rspack.Compiler): void;
14
+ }
15
+ export { AsyncChunkRetryPlugin };
@@ -1,8 +1,10 @@
1
- import { createRequire } from 'module';
2
- var require = createRequire(import.meta['url']);
3
-
1
+ "use strict";
2
+ var __create = Object.create;
4
3
  var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
9
  var __esm = (fn, res) => function __init() {
8
10
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
@@ -11,6 +13,23 @@ var __export = (target, all) => {
11
13
  for (var name in all)
12
14
  __defProp(target, name, { get: all[name], enumerable: true });
13
15
  };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
+ // If the importer is in node compatibility mode or this is not an ESM
26
+ // file that has been converted to a CommonJS file using a Babel-
27
+ // compatible transform (i.e. "__esModule" has not been set), then set
28
+ // "default" to the CommonJS "module.exports" for node compatibility.
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
+ mod
31
+ ));
32
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
33
  var __publicField = (obj, key, value) => {
15
34
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
16
35
  return value;
@@ -34,36 +53,17 @@ var __privateSet = (obj, member, value, setter) => {
34
53
  return value;
35
54
  };
36
55
 
37
- // ../../node_modules/.pnpm/@modern-js+module-tools@2.49.2_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js
38
- import { fileURLToPath } from "url";
39
- import path from "path";
40
- var getFilename, getDirname, __dirname;
41
- var init_esm = __esm({
42
- "../../node_modules/.pnpm/@modern-js+module-tools@2.49.2_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js"() {
43
- "use strict";
44
- getFilename = () => fileURLToPath(import.meta.url);
45
- getDirname = () => path.dirname(getFilename());
46
- __dirname = /* @__PURE__ */ getDirname();
47
- }
48
- });
49
-
50
56
  // src/AssetsRetryPlugin.ts
51
57
  var AssetsRetryPlugin_exports = {};
52
58
  __export(AssetsRetryPlugin_exports, {
53
59
  AssetsRetryPlugin: () => AssetsRetryPlugin
54
60
  });
55
- import path2 from "path";
56
- import {
57
- fse,
58
- generateScriptTag,
59
- getPublicPathFromCompiler,
60
- withPublicPath
61
- } from "@rsbuild/shared";
62
- var _retryOptions, AssetsRetryPlugin;
61
+ var import_node_path, import_shared, _retryOptions, AssetsRetryPlugin;
63
62
  var init_AssetsRetryPlugin = __esm({
64
63
  "src/AssetsRetryPlugin.ts"() {
65
64
  "use strict";
66
- init_esm();
65
+ import_node_path = __toESM(require("path"));
66
+ import_shared = require("@rsbuild/shared");
67
67
  AssetsRetryPlugin = class {
68
68
  constructor(options) {
69
69
  __publicField(this, "name");
@@ -91,21 +91,21 @@ var init_AssetsRetryPlugin = __esm({
91
91
  async getRetryCode() {
92
92
  const { default: serialize2 } = await import("serialize-javascript");
93
93
  const filename = "initialChunkRetry";
94
- const runtimeFilePath = path2.join(
94
+ const runtimeFilePath = import_node_path.default.join(
95
95
  __dirname,
96
96
  "runtime",
97
97
  this.minify ? `${filename}.min.js` : `${filename}.js`
98
98
  );
99
- const runtimeCode = await fse.readFile(runtimeFilePath, "utf-8");
99
+ const runtimeCode = await import_shared.fse.readFile(runtimeFilePath, "utf-8");
100
100
  return `(function(){${runtimeCode};init(${serialize2(
101
101
  __privateGet(this, _retryOptions)
102
102
  )});})()`;
103
103
  }
104
104
  async getScriptPath() {
105
105
  if (!this.scriptPath) {
106
- this.scriptPath = path2.posix.join(
106
+ this.scriptPath = import_node_path.default.posix.join(
107
107
  this.distDir,
108
- `assets-retry.${"0.6.14"}.js`
108
+ `assets-retry.${"0.7.0-beta.0"}.js`
109
109
  );
110
110
  }
111
111
  return this.scriptPath;
@@ -136,15 +136,15 @@ var init_AssetsRetryPlugin = __esm({
136
136
  this.HtmlPlugin.getHooks(compilation).alterAssetTagGroups.tapPromise(
137
137
  this.name,
138
138
  async (data) => {
139
- const scriptTag = generateScriptTag();
139
+ const scriptTag = (0, import_shared.generateScriptTag)();
140
140
  if (this.inlineScript) {
141
141
  data.headTags.unshift({
142
142
  ...scriptTag,
143
143
  innerHTML: await this.getRetryCode()
144
144
  });
145
145
  } else {
146
- const publicPath = getPublicPathFromCompiler(compiler);
147
- const url = withPublicPath(await this.getScriptPath(), publicPath);
146
+ const publicPath = (0, import_shared.getPublicPathFromCompiler)(compiler);
147
+ const url = (0, import_shared.withPublicPath)(await this.getScriptPath(), publicPath);
148
148
  data.headTags.unshift({
149
149
  ...scriptTag,
150
150
  attributes: {
@@ -168,42 +168,41 @@ var AsyncChunkRetryPlugin_exports = {};
168
168
  __export(AsyncChunkRetryPlugin_exports, {
169
169
  AsyncChunkRetryPlugin: () => AsyncChunkRetryPlugin
170
170
  });
171
- import path3 from "path";
172
- import { rspack } from "@rsbuild/core";
173
- import { fse as fse2, pick } from "@rsbuild/shared";
174
- import serialize from "serialize-javascript";
175
- function appendWebpackScript(module, appendSource) {
171
+ function appendWebpackScript(module2, appendSource) {
176
172
  try {
177
- const originSource = module.getGeneratedCode();
178
- module.getGeneratedCode = () => `${originSource}
173
+ const originSource = module2.getGeneratedCode();
174
+ module2.getGeneratedCode = () => `${originSource}
179
175
  ${appendSource}`;
180
176
  } catch (err) {
181
177
  console.error("Failed to modify Webpack RuntimeModule");
182
178
  throw err;
183
179
  }
184
180
  }
185
- function appendRspackScript(module, appendSource) {
181
+ function appendRspackScript(module2, appendSource) {
186
182
  try {
187
- const source = module.source.source.toString();
188
- module.source.source = Buffer.from(`${source}
183
+ const source = module2.source.source.toString();
184
+ module2.source.source = Buffer.from(`${source}
189
185
  ${appendSource}`, "utf-8");
190
186
  } catch (err) {
191
187
  console.error("Failed to modify Rspack RuntimeModule");
192
188
  throw err;
193
189
  }
194
190
  }
195
- var AsyncChunkRetryPlugin;
191
+ var import_node_path2, import_core, import_shared2, import_serialize_javascript, AsyncChunkRetryPlugin;
196
192
  var init_AsyncChunkRetryPlugin = __esm({
197
193
  "src/AsyncChunkRetryPlugin.ts"() {
198
194
  "use strict";
199
- init_esm();
195
+ import_node_path2 = __toESM(require("path"));
196
+ import_core = require("@rsbuild/core");
197
+ import_shared2 = require("@rsbuild/shared");
198
+ import_serialize_javascript = __toESM(require("serialize-javascript"));
200
199
  AsyncChunkRetryPlugin = class {
201
200
  constructor(options) {
202
201
  __publicField(this, "name", "ASYNC_CHUNK_RETRY_PLUGIN");
203
202
  __publicField(this, "options");
204
203
  __publicField(this, "runtimeOptions");
205
204
  this.options = options;
206
- this.runtimeOptions = pick(options, [
205
+ this.runtimeOptions = (0, import_shared2.pick)(options, [
207
206
  "domain",
208
207
  "max",
209
208
  "onRetry",
@@ -214,36 +213,36 @@ var init_AsyncChunkRetryPlugin = __esm({
214
213
  ]);
215
214
  }
216
215
  getRawRuntimeRetryCode() {
217
- const { RuntimeGlobals } = rspack;
216
+ const { RuntimeGlobals } = import_core.rspack;
218
217
  const filename = "asyncChunkRetry";
219
- const runtimeFilePath = path3.join(
218
+ const runtimeFilePath = import_node_path2.default.join(
220
219
  __dirname,
221
220
  "runtime",
222
221
  this.options.minify ? `${filename}.min.js` : `${filename}.js`
223
222
  );
224
- const rawText = fse2.readFileSync(runtimeFilePath, "utf-8");
223
+ const rawText = import_shared2.fse.readFileSync(runtimeFilePath, "utf-8");
225
224
  return rawText.replaceAll("__RUNTIME_GLOBALS_REQUIRE__", RuntimeGlobals.require).replaceAll(
226
225
  "__RUNTIME_GLOBALS_ENSURE_CHUNK__",
227
226
  RuntimeGlobals.ensureChunk
228
227
  ).replaceAll(
229
228
  "__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__",
230
229
  RuntimeGlobals.getChunkScriptFilename
231
- ).replaceAll("__RUNTIME_GLOBALS_PUBLIC_PATH__", RuntimeGlobals.publicPath).replaceAll("__RUNTIME_GLOBALS_LOAD_SCRIPT__", RuntimeGlobals.loadScript).replaceAll("__RETRY_OPTIONS__", serialize(this.runtimeOptions));
230
+ ).replaceAll("__RUNTIME_GLOBALS_PUBLIC_PATH__", RuntimeGlobals.publicPath).replaceAll("__RUNTIME_GLOBALS_LOAD_SCRIPT__", RuntimeGlobals.loadScript).replaceAll("__RETRY_OPTIONS__", (0, import_serialize_javascript.default)(this.runtimeOptions));
232
231
  }
233
232
  apply(compiler) {
234
233
  compiler.hooks.thisCompilation.tap(this.name, (compilation) => {
235
- compilation.hooks.runtimeModule.tap(this.name, (module) => {
234
+ compilation.hooks.runtimeModule.tap(this.name, (module2) => {
236
235
  const { isRspack } = this.options;
237
- const constructorName = isRspack ? module.constructorName : module.constructor?.name;
238
- const isPublicPathModule = module.name === "publicPath" || constructorName === "PublicPathRuntimeModule";
236
+ const constructorName = isRspack ? module2.constructorName : module2.constructor?.name;
237
+ const isPublicPathModule = module2.name === "publicPath" || constructorName === "PublicPathRuntimeModule";
239
238
  if (!isPublicPathModule) {
240
239
  return;
241
240
  }
242
241
  const runtimeCode = this.getRawRuntimeRetryCode();
243
242
  if (isRspack) {
244
- appendRspackScript(module, runtimeCode);
243
+ appendRspackScript(module2, runtimeCode);
245
244
  } else {
246
- appendWebpackScript(module, runtimeCode);
245
+ appendWebpackScript(module2, runtimeCode);
247
246
  }
248
247
  });
249
248
  });
@@ -253,20 +252,24 @@ var init_AsyncChunkRetryPlugin = __esm({
253
252
  });
254
253
 
255
254
  // src/index.ts
256
- init_esm();
257
- import { getDistPath, isHtmlDisabled } from "@rsbuild/shared";
255
+ var src_exports = {};
256
+ __export(src_exports, {
257
+ pluginAssetsRetry: () => pluginAssetsRetry
258
+ });
259
+ module.exports = __toCommonJS(src_exports);
260
+ var import_shared3 = require("@rsbuild/shared");
258
261
  var pluginAssetsRetry = (options = {}) => ({
259
262
  name: "rsbuild:assets-retry",
260
263
  setup(api) {
261
264
  api.modifyBundlerChain(
262
265
  async (chain, { CHAIN_ID, target, HtmlPlugin, isProd }) => {
263
266
  const config = api.getNormalizedConfig();
264
- if (!options || isHtmlDisabled(config, target)) {
267
+ if (!options || (0, import_shared3.isHtmlDisabled)(config, target)) {
265
268
  return;
266
269
  }
267
270
  const { AssetsRetryPlugin: AssetsRetryPlugin2 } = await Promise.resolve().then(() => (init_AssetsRetryPlugin(), AssetsRetryPlugin_exports));
268
271
  const { AsyncChunkRetryPlugin: AsyncChunkRetryPlugin2 } = await Promise.resolve().then(() => (init_AsyncChunkRetryPlugin(), AsyncChunkRetryPlugin_exports));
269
- const distDir = getDistPath(config, "js");
272
+ const distDir = (0, import_shared3.getDistPath)(config, "js");
270
273
  if (options.crossOrigin === void 0) {
271
274
  options.crossOrigin = config.html.crossorigin;
272
275
  }
@@ -287,6 +290,7 @@ var pluginAssetsRetry = (options = {}) => ({
287
290
  );
288
291
  }
289
292
  });
290
- export {
293
+ // Annotate the CommonJS export names for ESM import in node:
294
+ 0 && (module.exports = {
291
295
  pluginAssetsRetry
292
- };
296
+ });
package/dist/index.d.ts CHANGED
@@ -1,71 +1,4 @@
1
- import { RsbuildPlugin } from '@rsbuild/core';
2
- import { CrossOrigin } from '@rsbuild/shared';
3
-
4
- type PluginAssetsRetryOptions = {
5
- /**
6
- * The maximum number of retries for a single asset.
7
- * @default 3
8
- */
9
- max?: number;
10
- /**
11
- * Used to specify the HTML tag types that need to be retried.
12
- * @default ['script', 'link', 'img']
13
- */
14
- type?: string[];
15
- /**
16
- * The test function of the asset to be retried.
17
- */
18
- test?: string | ((url: string) => boolean);
19
- /**
20
- * Specifies the retry domain when assets fail to load.
21
- */
22
- domain?: string[];
23
- /**
24
- * Set the `crossorigin` attribute for tags.
25
- * @default config.html.crossorigin
26
- */
27
- crossOrigin?: boolean | CrossOrigin;
28
- /**
29
- * The callback function when the asset is failed to be retried.
30
- */
31
- onFail?: (context: AssetsRetryHookContext) => void;
32
- /**
33
- * The callback function when the asset is being retried.
34
- */
35
- onRetry?: (context: AssetsRetryHookContext) => void;
36
- /**
37
- * The callback function when the asset is successfully retried.
38
- */
39
- onSuccess?: (context: AssetsRetryHookContext) => void;
40
- /**
41
- * The function to add query parameters to the URL of the asset being retried.
42
- * @param times e.g: 1 -> 2 -> 3
43
- * @param originalQuery initial request url's query e.g: <script src="https://cdn.com/a.js?version=1"></script> -> "?version=1"
44
- * @default false
45
- * @description true -> hasQuery(originalQuery) ? `${getQuery(originalQuery)}&retry=${existRetryTimes}` : `?retry=${existRetryTimes}`
46
- */
47
- addQuery?: boolean | ((context: {
48
- times: number;
49
- originalQuery: string;
50
- }) => string);
51
- /**
52
- * Whether to inline the runtime JavaScript code of Assets Retry plugin into the HTML file.
53
- * @default true
54
- */
55
- inlineScript?: boolean;
56
- /**
57
- * Whether to minify the runtime JavaScript code of Assets Retry plugin.
58
- * @default process.env.NODE_ENV === 'production'
59
- */
60
- minify?: boolean;
61
- };
62
- type AssetsRetryHookContext = {
63
- url: string;
64
- times: number;
65
- domain: string;
66
- tagName: string;
67
- };
68
-
69
- declare const pluginAssetsRetry: (options?: PluginAssetsRetryOptions) => RsbuildPlugin;
70
-
71
- export { type PluginAssetsRetryOptions, pluginAssetsRetry };
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ import type { PluginAssetsRetryOptions } from './types';
3
+ export type { PluginAssetsRetryOptions };
4
+ export declare const pluginAssetsRetry: (options?: PluginAssetsRetryOptions) => RsbuildPlugin;
package/dist/index.js CHANGED
@@ -1,10 +1,8 @@
1
- "use strict";
2
- var __create = Object.create;
1
+ import { createRequire } from 'module';
2
+ var require = createRequire(import.meta['url']);
3
+
3
4
  var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
7
  var __esm = (fn, res) => function __init() {
10
8
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
@@ -13,23 +11,6 @@ var __export = (target, all) => {
13
11
  for (var name in all)
14
12
  __defProp(target, name, { get: all[name], enumerable: true });
15
13
  };
16
- var __copyProps = (to, from, except, desc) => {
17
- if (from && typeof from === "object" || typeof from === "function") {
18
- for (let key of __getOwnPropNames(from))
19
- if (!__hasOwnProp.call(to, key) && key !== except)
20
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
- }
22
- return to;
23
- };
24
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
- // If the importer is in node compatibility mode or this is not an ESM
26
- // file that has been converted to a CommonJS file using a Babel-
27
- // compatible transform (i.e. "__esModule" has not been set), then set
28
- // "default" to the CommonJS "module.exports" for node compatibility.
29
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
- mod
31
- ));
32
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
14
  var __publicField = (obj, key, value) => {
34
15
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
35
16
  return value;
@@ -53,17 +34,36 @@ var __privateSet = (obj, member, value, setter) => {
53
34
  return value;
54
35
  };
55
36
 
37
+ // ../../node_modules/.pnpm/@modern-js+module-tools@2.49.3_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js
38
+ import { fileURLToPath } from "url";
39
+ import path from "path";
40
+ var getFilename, getDirname, __dirname;
41
+ var init_esm = __esm({
42
+ "../../node_modules/.pnpm/@modern-js+module-tools@2.49.3_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js"() {
43
+ "use strict";
44
+ getFilename = () => fileURLToPath(import.meta.url);
45
+ getDirname = () => path.dirname(getFilename());
46
+ __dirname = /* @__PURE__ */ getDirname();
47
+ }
48
+ });
49
+
56
50
  // src/AssetsRetryPlugin.ts
57
51
  var AssetsRetryPlugin_exports = {};
58
52
  __export(AssetsRetryPlugin_exports, {
59
53
  AssetsRetryPlugin: () => AssetsRetryPlugin
60
54
  });
61
- var import_node_path, import_shared, _retryOptions, AssetsRetryPlugin;
55
+ import path2 from "path";
56
+ import {
57
+ fse,
58
+ generateScriptTag,
59
+ getPublicPathFromCompiler,
60
+ withPublicPath
61
+ } from "@rsbuild/shared";
62
+ var _retryOptions, AssetsRetryPlugin;
62
63
  var init_AssetsRetryPlugin = __esm({
63
64
  "src/AssetsRetryPlugin.ts"() {
64
65
  "use strict";
65
- import_node_path = __toESM(require("path"));
66
- import_shared = require("@rsbuild/shared");
66
+ init_esm();
67
67
  AssetsRetryPlugin = class {
68
68
  constructor(options) {
69
69
  __publicField(this, "name");
@@ -91,21 +91,21 @@ var init_AssetsRetryPlugin = __esm({
91
91
  async getRetryCode() {
92
92
  const { default: serialize2 } = await import("serialize-javascript");
93
93
  const filename = "initialChunkRetry";
94
- const runtimeFilePath = import_node_path.default.join(
94
+ const runtimeFilePath = path2.join(
95
95
  __dirname,
96
96
  "runtime",
97
97
  this.minify ? `${filename}.min.js` : `${filename}.js`
98
98
  );
99
- const runtimeCode = await import_shared.fse.readFile(runtimeFilePath, "utf-8");
99
+ const runtimeCode = await fse.readFile(runtimeFilePath, "utf-8");
100
100
  return `(function(){${runtimeCode};init(${serialize2(
101
101
  __privateGet(this, _retryOptions)
102
102
  )});})()`;
103
103
  }
104
104
  async getScriptPath() {
105
105
  if (!this.scriptPath) {
106
- this.scriptPath = import_node_path.default.posix.join(
106
+ this.scriptPath = path2.posix.join(
107
107
  this.distDir,
108
- `assets-retry.${"0.6.14"}.js`
108
+ `assets-retry.${"0.7.0-beta.0"}.js`
109
109
  );
110
110
  }
111
111
  return this.scriptPath;
@@ -136,15 +136,15 @@ var init_AssetsRetryPlugin = __esm({
136
136
  this.HtmlPlugin.getHooks(compilation).alterAssetTagGroups.tapPromise(
137
137
  this.name,
138
138
  async (data) => {
139
- const scriptTag = (0, import_shared.generateScriptTag)();
139
+ const scriptTag = generateScriptTag();
140
140
  if (this.inlineScript) {
141
141
  data.headTags.unshift({
142
142
  ...scriptTag,
143
143
  innerHTML: await this.getRetryCode()
144
144
  });
145
145
  } else {
146
- const publicPath = (0, import_shared.getPublicPathFromCompiler)(compiler);
147
- const url = (0, import_shared.withPublicPath)(await this.getScriptPath(), publicPath);
146
+ const publicPath = getPublicPathFromCompiler(compiler);
147
+ const url = withPublicPath(await this.getScriptPath(), publicPath);
148
148
  data.headTags.unshift({
149
149
  ...scriptTag,
150
150
  attributes: {
@@ -168,41 +168,42 @@ var AsyncChunkRetryPlugin_exports = {};
168
168
  __export(AsyncChunkRetryPlugin_exports, {
169
169
  AsyncChunkRetryPlugin: () => AsyncChunkRetryPlugin
170
170
  });
171
- function appendWebpackScript(module2, appendSource) {
171
+ import path3 from "path";
172
+ import { rspack } from "@rsbuild/core";
173
+ import { fse as fse2, pick } from "@rsbuild/shared";
174
+ import serialize from "serialize-javascript";
175
+ function appendWebpackScript(module, appendSource) {
172
176
  try {
173
- const originSource = module2.getGeneratedCode();
174
- module2.getGeneratedCode = () => `${originSource}
177
+ const originSource = module.getGeneratedCode();
178
+ module.getGeneratedCode = () => `${originSource}
175
179
  ${appendSource}`;
176
180
  } catch (err) {
177
181
  console.error("Failed to modify Webpack RuntimeModule");
178
182
  throw err;
179
183
  }
180
184
  }
181
- function appendRspackScript(module2, appendSource) {
185
+ function appendRspackScript(module, appendSource) {
182
186
  try {
183
- const source = module2.source.source.toString();
184
- module2.source.source = Buffer.from(`${source}
187
+ const source = module.source.source.toString();
188
+ module.source.source = Buffer.from(`${source}
185
189
  ${appendSource}`, "utf-8");
186
190
  } catch (err) {
187
191
  console.error("Failed to modify Rspack RuntimeModule");
188
192
  throw err;
189
193
  }
190
194
  }
191
- var import_node_path2, import_core, import_shared2, import_serialize_javascript, AsyncChunkRetryPlugin;
195
+ var AsyncChunkRetryPlugin;
192
196
  var init_AsyncChunkRetryPlugin = __esm({
193
197
  "src/AsyncChunkRetryPlugin.ts"() {
194
198
  "use strict";
195
- import_node_path2 = __toESM(require("path"));
196
- import_core = require("@rsbuild/core");
197
- import_shared2 = require("@rsbuild/shared");
198
- import_serialize_javascript = __toESM(require("serialize-javascript"));
199
+ init_esm();
199
200
  AsyncChunkRetryPlugin = class {
200
201
  constructor(options) {
201
202
  __publicField(this, "name", "ASYNC_CHUNK_RETRY_PLUGIN");
202
203
  __publicField(this, "options");
203
204
  __publicField(this, "runtimeOptions");
204
205
  this.options = options;
205
- this.runtimeOptions = (0, import_shared2.pick)(options, [
206
+ this.runtimeOptions = pick(options, [
206
207
  "domain",
207
208
  "max",
208
209
  "onRetry",
@@ -213,36 +214,36 @@ var init_AsyncChunkRetryPlugin = __esm({
213
214
  ]);
214
215
  }
215
216
  getRawRuntimeRetryCode() {
216
- const { RuntimeGlobals } = import_core.rspack;
217
+ const { RuntimeGlobals } = rspack;
217
218
  const filename = "asyncChunkRetry";
218
- const runtimeFilePath = import_node_path2.default.join(
219
+ const runtimeFilePath = path3.join(
219
220
  __dirname,
220
221
  "runtime",
221
222
  this.options.minify ? `${filename}.min.js` : `${filename}.js`
222
223
  );
223
- const rawText = import_shared2.fse.readFileSync(runtimeFilePath, "utf-8");
224
+ const rawText = fse2.readFileSync(runtimeFilePath, "utf-8");
224
225
  return rawText.replaceAll("__RUNTIME_GLOBALS_REQUIRE__", RuntimeGlobals.require).replaceAll(
225
226
  "__RUNTIME_GLOBALS_ENSURE_CHUNK__",
226
227
  RuntimeGlobals.ensureChunk
227
228
  ).replaceAll(
228
229
  "__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__",
229
230
  RuntimeGlobals.getChunkScriptFilename
230
- ).replaceAll("__RUNTIME_GLOBALS_PUBLIC_PATH__", RuntimeGlobals.publicPath).replaceAll("__RUNTIME_GLOBALS_LOAD_SCRIPT__", RuntimeGlobals.loadScript).replaceAll("__RETRY_OPTIONS__", (0, import_serialize_javascript.default)(this.runtimeOptions));
231
+ ).replaceAll("__RUNTIME_GLOBALS_PUBLIC_PATH__", RuntimeGlobals.publicPath).replaceAll("__RUNTIME_GLOBALS_LOAD_SCRIPT__", RuntimeGlobals.loadScript).replaceAll("__RETRY_OPTIONS__", serialize(this.runtimeOptions));
231
232
  }
232
233
  apply(compiler) {
233
234
  compiler.hooks.thisCompilation.tap(this.name, (compilation) => {
234
- compilation.hooks.runtimeModule.tap(this.name, (module2) => {
235
+ compilation.hooks.runtimeModule.tap(this.name, (module) => {
235
236
  const { isRspack } = this.options;
236
- const constructorName = isRspack ? module2.constructorName : module2.constructor?.name;
237
- const isPublicPathModule = module2.name === "publicPath" || constructorName === "PublicPathRuntimeModule";
237
+ const constructorName = isRspack ? module.constructorName : module.constructor?.name;
238
+ const isPublicPathModule = module.name === "publicPath" || constructorName === "PublicPathRuntimeModule";
238
239
  if (!isPublicPathModule) {
239
240
  return;
240
241
  }
241
242
  const runtimeCode = this.getRawRuntimeRetryCode();
242
243
  if (isRspack) {
243
- appendRspackScript(module2, runtimeCode);
244
+ appendRspackScript(module, runtimeCode);
244
245
  } else {
245
- appendWebpackScript(module2, runtimeCode);
246
+ appendWebpackScript(module, runtimeCode);
246
247
  }
247
248
  });
248
249
  });
@@ -252,24 +253,20 @@ var init_AsyncChunkRetryPlugin = __esm({
252
253
  });
253
254
 
254
255
  // src/index.ts
255
- var src_exports = {};
256
- __export(src_exports, {
257
- pluginAssetsRetry: () => pluginAssetsRetry
258
- });
259
- module.exports = __toCommonJS(src_exports);
260
- var import_shared3 = require("@rsbuild/shared");
256
+ init_esm();
257
+ import { getDistPath, isHtmlDisabled } from "@rsbuild/shared";
261
258
  var pluginAssetsRetry = (options = {}) => ({
262
259
  name: "rsbuild:assets-retry",
263
260
  setup(api) {
264
261
  api.modifyBundlerChain(
265
262
  async (chain, { CHAIN_ID, target, HtmlPlugin, isProd }) => {
266
263
  const config = api.getNormalizedConfig();
267
- if (!options || (0, import_shared3.isHtmlDisabled)(config, target)) {
264
+ if (!options || isHtmlDisabled(config, target)) {
268
265
  return;
269
266
  }
270
267
  const { AssetsRetryPlugin: AssetsRetryPlugin2 } = await Promise.resolve().then(() => (init_AssetsRetryPlugin(), AssetsRetryPlugin_exports));
271
268
  const { AsyncChunkRetryPlugin: AsyncChunkRetryPlugin2 } = await Promise.resolve().then(() => (init_AsyncChunkRetryPlugin(), AsyncChunkRetryPlugin_exports));
272
- const distDir = (0, import_shared3.getDistPath)(config, "js");
269
+ const distDir = getDistPath(config, "js");
273
270
  if (options.crossOrigin === void 0) {
274
271
  options.crossOrigin = config.html.crossorigin;
275
272
  }
@@ -290,7 +287,6 @@ var pluginAssetsRetry = (options = {}) => ({
290
287
  );
291
288
  }
292
289
  });
293
- // Annotate the CommonJS export names for ESM import in node:
294
- 0 && (module.exports = {
290
+ export {
295
291
  pluginAssetsRetry
296
- });
292
+ };
@@ -0,0 +1,14 @@
1
+ import type { RuntimeRetryOptions } from '../types';
2
+ type ChunkId = string;
3
+ type ChunkFilename = string;
4
+ type ChunkSrcUrl = string;
5
+ declare global {
6
+ var __RUNTIME_GLOBALS_REQUIRE__: unknown;
7
+ var __RUNTIME_GLOBALS_ENSURE_CHUNK__: (chunkId: ChunkId) => Promise<unknown>;
8
+ var __RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__: (chunkId: ChunkId) => string;
9
+ var __RUNTIME_GLOBALS_LOAD_SCRIPT__: (url: ChunkSrcUrl, done: unknown, key: string, chunkId: ChunkId) => void;
10
+ var __RUNTIME_GLOBALS_PUBLIC_PATH__: string;
11
+ var __RETRY_OPTIONS__: RuntimeRetryOptions;
12
+ var __RB_ASYNC_CHUNKS__: Record<ChunkFilename, boolean>;
13
+ }
14
+ export {};
@@ -0,0 +1,4 @@
1
+ declare global {
2
+ var __RB_ASYNC_CHUNKS__: Record<string, boolean>;
3
+ }
4
+ export {};
@@ -0,0 +1,66 @@
1
+ import type { CrossOrigin } from '@rsbuild/shared';
2
+ export type PluginAssetsRetryOptions = {
3
+ /**
4
+ * The maximum number of retries for a single asset.
5
+ * @default 3
6
+ */
7
+ max?: number;
8
+ /**
9
+ * Used to specify the HTML tag types that need to be retried.
10
+ * @default ['script', 'link', 'img']
11
+ */
12
+ type?: string[];
13
+ /**
14
+ * The test function of the asset to be retried.
15
+ */
16
+ test?: string | ((url: string) => boolean);
17
+ /**
18
+ * Specifies the retry domain when assets fail to load.
19
+ */
20
+ domain?: string[];
21
+ /**
22
+ * Set the `crossorigin` attribute for tags.
23
+ * @default config.html.crossorigin
24
+ */
25
+ crossOrigin?: boolean | CrossOrigin;
26
+ /**
27
+ * The callback function when the asset is failed to be retried.
28
+ */
29
+ onFail?: (context: AssetsRetryHookContext) => void;
30
+ /**
31
+ * The callback function when the asset is being retried.
32
+ */
33
+ onRetry?: (context: AssetsRetryHookContext) => void;
34
+ /**
35
+ * The callback function when the asset is successfully retried.
36
+ */
37
+ onSuccess?: (context: AssetsRetryHookContext) => void;
38
+ /**
39
+ * The function to add query parameters to the URL of the asset being retried.
40
+ * @param times e.g: 1 -> 2 -> 3
41
+ * @param originalQuery initial request url's query e.g: <script src="https://cdn.com/a.js?version=1"></script> -> "?version=1"
42
+ * @default false
43
+ * @description true -> hasQuery(originalQuery) ? `${getQuery(originalQuery)}&retry=${existRetryTimes}` : `?retry=${existRetryTimes}`
44
+ */
45
+ addQuery?: boolean | ((context: {
46
+ times: number;
47
+ originalQuery: string;
48
+ }) => string);
49
+ /**
50
+ * Whether to inline the runtime JavaScript code of Assets Retry plugin into the HTML file.
51
+ * @default true
52
+ */
53
+ inlineScript?: boolean;
54
+ /**
55
+ * Whether to minify the runtime JavaScript code of Assets Retry plugin.
56
+ * @default process.env.NODE_ENV === 'production'
57
+ */
58
+ minify?: boolean;
59
+ };
60
+ export type RuntimeRetryOptions = Omit<PluginAssetsRetryOptions, 'inlineScript' | 'minify'>;
61
+ export type AssetsRetryHookContext = {
62
+ url: string;
63
+ times: number;
64
+ domain: string;
65
+ tagName: string;
66
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-assets-retry",
3
- "version": "0.6.14",
3
+ "version": "0.7.0-beta.0",
4
4
  "description": "Assets retry plugin for Rsbuild",
5
5
  "homepage": "https://rsbuild.dev",
6
6
  "repository": {
@@ -9,22 +9,22 @@
9
9
  "directory": "packages/plugin-assets-retry"
10
10
  },
11
11
  "license": "MIT",
12
- "type": "commonjs",
12
+ "type": "module",
13
13
  "exports": {
14
14
  ".": {
15
15
  "types": "./dist/index.d.ts",
16
- "import": "./dist/index.mjs",
17
- "default": "./dist/index.js"
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.cjs"
18
18
  }
19
19
  },
20
- "main": "./dist/index.js",
20
+ "main": "./dist/index.cjs",
21
21
  "types": "./dist/index.d.ts",
22
22
  "files": [
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
26
  "serialize-javascript": "^6.0.2",
27
- "@rsbuild/shared": "0.6.14"
27
+ "@rsbuild/shared": "0.7.0-beta.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@babel/core": "^7.24.5",
@@ -34,11 +34,11 @@
34
34
  "html-webpack-plugin": "npm:html-rspack-plugin@5.7.2",
35
35
  "terser": "5.31.0",
36
36
  "typescript": "^5.4.2",
37
- "@rsbuild/core": "0.6.14",
38
- "@scripts/test-helper": "0.6.14"
37
+ "@rsbuild/core": "0.7.0-beta.0",
38
+ "@scripts/test-helper": "0.7.0-beta.0"
39
39
  },
40
40
  "peerDependencies": {
41
- "@rsbuild/core": "^0.6.14"
41
+ "@rsbuild/core": "^0.7.0-beta.0"
42
42
  },
43
43
  "publishConfig": {
44
44
  "access": "public",
@@ -46,7 +46,7 @@
46
46
  "registry": "https://registry.npmjs.org/"
47
47
  },
48
48
  "scripts": {
49
- "build": "modern build && node scripts/postCompile.js",
49
+ "build": "modern build && node scripts/postCompile.mjs",
50
50
  "dev": "modern build --watch"
51
51
  }
52
52
  }