@rsbuild/plugin-assets-retry 0.7.9 → 1.0.0-alpha.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/core';
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 };
package/dist/index.cjs CHANGED
@@ -58,13 +58,13 @@ var AssetsRetryPlugin_exports = {};
58
58
  __export(AssetsRetryPlugin_exports, {
59
59
  AssetsRetryPlugin: () => AssetsRetryPlugin
60
60
  });
61
- var import_node_path, import_core, import_shared, _retryOptions, AssetsRetryPlugin;
61
+ var import_node_fs, import_node_path, import_core, _retryOptions, AssetsRetryPlugin;
62
62
  var init_AssetsRetryPlugin = __esm({
63
63
  "src/AssetsRetryPlugin.ts"() {
64
64
  "use strict";
65
+ import_node_fs = __toESM(require("fs"));
65
66
  import_node_path = __toESM(require("path"));
66
67
  import_core = require("@rsbuild/core");
67
- import_shared = require("@rsbuild/shared");
68
68
  AssetsRetryPlugin = class {
69
69
  constructor(options) {
70
70
  __publicField(this, "name");
@@ -97,7 +97,7 @@ var init_AssetsRetryPlugin = __esm({
97
97
  "runtime",
98
98
  this.minify ? `${filename}.min.js` : `${filename}.js`
99
99
  );
100
- const runtimeCode = await import_shared.fse.readFile(runtimeFilePath, "utf-8");
100
+ const runtimeCode = await import_node_fs.default.promises.readFile(runtimeFilePath, "utf-8");
101
101
  return `(function(){${runtimeCode};init(${serialize2(
102
102
  __privateGet(this, _retryOptions)
103
103
  )});})()`;
@@ -106,7 +106,7 @@ var init_AssetsRetryPlugin = __esm({
106
106
  if (!this.scriptPath) {
107
107
  this.scriptPath = import_node_path.default.posix.join(
108
108
  this.distDir,
109
- `assets-retry.${"0.7.9"}.js`
109
+ `assets-retry.${"1.0.0-alpha.0"}.js`
110
110
  );
111
111
  }
112
112
  return this.scriptPath;
@@ -151,7 +151,7 @@ var init_AssetsRetryPlugin = __esm({
151
151
  innerHTML: await this.getRetryCode()
152
152
  });
153
153
  } else {
154
- const publicPath = (0, import_shared.getPublicPathFromCompiler)(compiler);
154
+ const { publicPath } = compilation.outputOptions;
155
155
  const url = (0, import_core.ensureAssetPrefix)(
156
156
  await this.getScriptPath(),
157
157
  publicPath
@@ -210,13 +210,13 @@ function pick(obj, keys) {
210
210
  {}
211
211
  );
212
212
  }
213
- var import_node_path2, import_core2, import_shared2, import_serialize_javascript, AsyncChunkRetryPlugin;
213
+ var import_node_fs2, import_node_path2, import_core2, import_serialize_javascript, AsyncChunkRetryPlugin;
214
214
  var init_AsyncChunkRetryPlugin = __esm({
215
215
  "src/AsyncChunkRetryPlugin.ts"() {
216
216
  "use strict";
217
+ import_node_fs2 = __toESM(require("fs"));
217
218
  import_node_path2 = __toESM(require("path"));
218
219
  import_core2 = require("@rsbuild/core");
219
- import_shared2 = require("@rsbuild/shared");
220
220
  import_serialize_javascript = __toESM(require("serialize-javascript"));
221
221
  AsyncChunkRetryPlugin = class {
222
222
  constructor(options) {
@@ -242,7 +242,7 @@ var init_AsyncChunkRetryPlugin = __esm({
242
242
  "runtime",
243
243
  this.options.minify ? `${filename}.min.js` : `${filename}.js`
244
244
  );
245
- const rawText = import_shared2.fse.readFileSync(runtimeFilePath, "utf-8");
245
+ const rawText = import_node_fs2.default.readFileSync(runtimeFilePath, "utf-8");
246
246
  return rawText.replaceAll("__RUNTIME_GLOBALS_REQUIRE__", RuntimeGlobals.require).replaceAll(
247
247
  "__RUNTIME_GLOBALS_ENSURE_CHUNK__",
248
248
  RuntimeGlobals.ensureChunk
@@ -280,15 +280,15 @@ __export(src_exports, {
280
280
  pluginAssetsRetry: () => pluginAssetsRetry
281
281
  });
282
282
  module.exports = __toCommonJS(src_exports);
283
- var import_shared3 = require("@rsbuild/shared");
284
283
  var PLUGIN_ASSETS_RETRY_NAME = "rsbuild:assets-retry";
285
284
  var pluginAssetsRetry = (options = {}) => ({
286
285
  name: PLUGIN_ASSETS_RETRY_NAME,
287
286
  setup(api) {
288
287
  api.modifyBundlerChain(
289
- async (chain, { CHAIN_ID, target, HtmlPlugin, isProd }) => {
290
- const config = api.getNormalizedConfig();
291
- if (!options || (0, import_shared3.isHtmlDisabled)(config, target)) {
288
+ async (chain, { CHAIN_ID, HtmlPlugin, isProd, environment }) => {
289
+ const { config } = environment;
290
+ const htmlPaths = api.getHTMLPaths({ environment: environment.name });
291
+ if (!options || Object.keys(htmlPaths).length === 0) {
292
292
  return;
293
293
  }
294
294
  const { AssetsRetryPlugin: AssetsRetryPlugin2 } = await Promise.resolve().then(() => (init_AssetsRetryPlugin(), AssetsRetryPlugin_exports));
package/dist/index.d.ts CHANGED
@@ -1,71 +1,5 @@
1
- import { CrossOrigin, RsbuildPlugin } from '@rsbuild/core';
2
-
3
- type PluginAssetsRetryOptions = {
4
- /**
5
- * The maximum number of retries for a single asset.
6
- * @default 3
7
- */
8
- max?: number;
9
- /**
10
- * Used to specify the HTML tag types that need to be retried.
11
- * @default ['script', 'link', 'img']
12
- */
13
- type?: string[];
14
- /**
15
- * The test function of the asset to be retried.
16
- */
17
- test?: string | ((url: string) => boolean);
18
- /**
19
- * Specifies the retry domain when assets fail to load.
20
- */
21
- domain?: string[];
22
- /**
23
- * Set the `crossorigin` attribute for tags.
24
- * @default config.html.crossorigin
25
- */
26
- crossOrigin?: boolean | CrossOrigin;
27
- /**
28
- * The callback function when the asset is failed to be retried.
29
- */
30
- onFail?: (context: AssetsRetryHookContext) => void;
31
- /**
32
- * The callback function when the asset is being retried.
33
- */
34
- onRetry?: (context: AssetsRetryHookContext) => void;
35
- /**
36
- * The callback function when the asset is successfully retried.
37
- */
38
- onSuccess?: (context: AssetsRetryHookContext) => void;
39
- /**
40
- * The function to add query parameters to the URL of the asset being retried.
41
- * @param times e.g: 1 -> 2 -> 3
42
- * @param originalQuery initial request url's query e.g: <script src="https://cdn.com/a.js?version=1"></script> -> "?version=1"
43
- * @default false
44
- * @description true -> hasQuery(originalQuery) ? `${getQuery(originalQuery)}&retry=${existRetryTimes}` : `?retry=${existRetryTimes}`
45
- */
46
- addQuery?: boolean | ((context: {
47
- times: number;
48
- originalQuery: string;
49
- }) => string);
50
- /**
51
- * Whether to inline the runtime JavaScript code of Assets Retry plugin into the HTML file.
52
- * @default true
53
- */
54
- inlineScript?: boolean;
55
- /**
56
- * Whether to minify the runtime JavaScript code of Assets Retry plugin.
57
- * @default process.env.NODE_ENV === 'production'
58
- */
59
- minify?: boolean;
60
- };
61
- type AssetsRetryHookContext = {
62
- url: string;
63
- times: number;
64
- domain: string;
65
- tagName: string;
66
- };
67
-
68
- declare const PLUGIN_ASSETS_RETRY_NAME = "rsbuild:assets-retry";
69
- declare const pluginAssetsRetry: (options?: PluginAssetsRetryOptions) => RsbuildPlugin;
70
-
71
- export { PLUGIN_ASSETS_RETRY_NAME, type PluginAssetsRetryOptions, pluginAssetsRetry };
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ import type { PluginAssetsRetryOptions } from './types';
3
+ export type { PluginAssetsRetryOptions };
4
+ export declare const PLUGIN_ASSETS_RETRY_NAME = "rsbuild:assets-retry";
5
+ export declare const pluginAssetsRetry: (options?: PluginAssetsRetryOptions) => RsbuildPlugin;
package/dist/index.js CHANGED
@@ -34,12 +34,12 @@ var __privateSet = (obj, member, value, setter) => {
34
34
  return value;
35
35
  };
36
36
 
37
- // ../../node_modules/.pnpm/@modern-js+module-tools@2.52.0_eslint@9.5.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js
37
+ // ../../node_modules/.pnpm/@modern-js+module-tools@2.54.5_eslint@9.6.0_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js
38
38
  import { fileURLToPath } from "url";
39
39
  import path from "path";
40
40
  var getFilename, getDirname, __dirname;
41
41
  var init_esm = __esm({
42
- "../../node_modules/.pnpm/@modern-js+module-tools@2.52.0_eslint@9.5.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js"() {
42
+ "../../node_modules/.pnpm/@modern-js+module-tools@2.54.5_eslint@9.6.0_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js"() {
43
43
  "use strict";
44
44
  getFilename = () => fileURLToPath(import.meta.url);
45
45
  getDirname = () => path.dirname(getFilename());
@@ -52,9 +52,9 @@ var AssetsRetryPlugin_exports = {};
52
52
  __export(AssetsRetryPlugin_exports, {
53
53
  AssetsRetryPlugin: () => AssetsRetryPlugin
54
54
  });
55
+ import fs from "fs";
55
56
  import path2 from "path";
56
57
  import { ensureAssetPrefix } from "@rsbuild/core";
57
- import { fse, getPublicPathFromCompiler } from "@rsbuild/shared";
58
58
  var _retryOptions, AssetsRetryPlugin;
59
59
  var init_AssetsRetryPlugin = __esm({
60
60
  "src/AssetsRetryPlugin.ts"() {
@@ -92,7 +92,7 @@ var init_AssetsRetryPlugin = __esm({
92
92
  "runtime",
93
93
  this.minify ? `${filename}.min.js` : `${filename}.js`
94
94
  );
95
- const runtimeCode = await fse.readFile(runtimeFilePath, "utf-8");
95
+ const runtimeCode = await fs.promises.readFile(runtimeFilePath, "utf-8");
96
96
  return `(function(){${runtimeCode};init(${serialize2(
97
97
  __privateGet(this, _retryOptions)
98
98
  )});})()`;
@@ -101,7 +101,7 @@ var init_AssetsRetryPlugin = __esm({
101
101
  if (!this.scriptPath) {
102
102
  this.scriptPath = path2.posix.join(
103
103
  this.distDir,
104
- `assets-retry.${"0.7.9"}.js`
104
+ `assets-retry.${"1.0.0-alpha.0"}.js`
105
105
  );
106
106
  }
107
107
  return this.scriptPath;
@@ -146,7 +146,7 @@ var init_AssetsRetryPlugin = __esm({
146
146
  innerHTML: await this.getRetryCode()
147
147
  });
148
148
  } else {
149
- const publicPath = getPublicPathFromCompiler(compiler);
149
+ const { publicPath } = compilation.outputOptions;
150
150
  const url = ensureAssetPrefix(
151
151
  await this.getScriptPath(),
152
152
  publicPath
@@ -174,9 +174,9 @@ var AsyncChunkRetryPlugin_exports = {};
174
174
  __export(AsyncChunkRetryPlugin_exports, {
175
175
  AsyncChunkRetryPlugin: () => AsyncChunkRetryPlugin
176
176
  });
177
+ import fs2 from "fs";
177
178
  import path3 from "path";
178
179
  import { rspack } from "@rsbuild/core";
179
- import { fse as fse2 } from "@rsbuild/shared";
180
180
  import serialize from "serialize-javascript";
181
181
  function appendWebpackScript(module, appendSource) {
182
182
  try {
@@ -238,7 +238,7 @@ var init_AsyncChunkRetryPlugin = __esm({
238
238
  "runtime",
239
239
  this.options.minify ? `${filename}.min.js` : `${filename}.js`
240
240
  );
241
- const rawText = fse2.readFileSync(runtimeFilePath, "utf-8");
241
+ const rawText = fs2.readFileSync(runtimeFilePath, "utf-8");
242
242
  return rawText.replaceAll("__RUNTIME_GLOBALS_REQUIRE__", RuntimeGlobals.require).replaceAll(
243
243
  "__RUNTIME_GLOBALS_ENSURE_CHUNK__",
244
244
  RuntimeGlobals.ensureChunk
@@ -271,15 +271,15 @@ var init_AsyncChunkRetryPlugin = __esm({
271
271
 
272
272
  // src/index.ts
273
273
  init_esm();
274
- import { isHtmlDisabled } from "@rsbuild/shared";
275
274
  var PLUGIN_ASSETS_RETRY_NAME = "rsbuild:assets-retry";
276
275
  var pluginAssetsRetry = (options = {}) => ({
277
276
  name: PLUGIN_ASSETS_RETRY_NAME,
278
277
  setup(api) {
279
278
  api.modifyBundlerChain(
280
- async (chain, { CHAIN_ID, target, HtmlPlugin, isProd }) => {
281
- const config = api.getNormalizedConfig();
282
- if (!options || isHtmlDisabled(config, target)) {
279
+ async (chain, { CHAIN_ID, HtmlPlugin, isProd, environment }) => {
280
+ const { config } = environment;
281
+ const htmlPaths = api.getHTMLPaths({ environment: environment.name });
282
+ if (!options || Object.keys(htmlPaths).length === 0) {
283
283
  return;
284
284
  }
285
285
  const { AssetsRetryPlugin: AssetsRetryPlugin2 } = await Promise.resolve().then(() => (init_AssetsRetryPlugin(), AssetsRetryPlugin_exports));
@@ -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/core';
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.7.9",
3
+ "version": "1.0.0-alpha.0",
4
4
  "description": "Assets retry plugin for Rsbuild",
5
5
  "homepage": "https://rsbuild.dev",
6
6
  "repository": {
@@ -24,7 +24,7 @@
24
24
  ],
25
25
  "dependencies": {
26
26
  "serialize-javascript": "^6.0.2",
27
- "@rsbuild/shared": "0.7.9"
27
+ "@rsbuild/shared": "1.0.0-alpha.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@babel/core": "^7.24.7",
@@ -33,11 +33,11 @@
33
33
  "@types/serialize-javascript": "^5.0.4",
34
34
  "html-webpack-plugin": "npm:html-rspack-plugin@5.7.2",
35
35
  "terser": "5.31.1",
36
- "typescript": "^5.4.2",
37
- "@rsbuild/core": "0.7.9"
36
+ "typescript": "^5.5.2",
37
+ "@rsbuild/core": "1.0.0-alpha.0"
38
38
  },
39
39
  "peerDependencies": {
40
- "@rsbuild/core": "^0.7.9"
40
+ "@rsbuild/core": "^1.0.0-alpha.0"
41
41
  },
42
42
  "publishConfig": {
43
43
  "access": "public",