@rsbuild/plugin-assets-retry 1.2.3 → 1.4.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.
- package/README.md +137 -23
- package/README.zh-CN.md +137 -23
- package/dist/index.cjs +59 -56
- package/dist/index.d.ts +100 -5
- package/dist/index.js +57 -57
- package/dist/runtime/asyncChunkRetry.js +189 -222
- package/dist/runtime/asyncChunkRetry.min.js +1 -1
- package/dist/runtime/initialChunkRetry.js +205 -267
- package/dist/runtime/initialChunkRetry.min.js +1 -1
- package/package.json +9 -8
- package/dist/AsyncChunkRetryPlugin.d.ts +0 -15
- package/dist/runtime/asyncChunkRetry.d.ts +0 -19
- package/dist/runtime/initialChunkRetry.d.ts +0 -5
- package/dist/types.d.ts +0 -72
package/dist/index.cjs
CHANGED
|
@@ -170,6 +170,7 @@ var __webpack_exports__ = {};
|
|
|
170
170
|
__webpack_require__.r(__webpack_exports__);
|
|
171
171
|
__webpack_require__.d(__webpack_exports__, {
|
|
172
172
|
pluginAssetsRetry: ()=>pluginAssetsRetry,
|
|
173
|
+
ASSETS_RETRY_DATA_ATTRIBUTE: ()=>ASSETS_RETRY_DATA_ATTRIBUTE,
|
|
173
174
|
PLUGIN_ASSETS_RETRY_NAME: ()=>PLUGIN_ASSETS_RETRY_NAME
|
|
174
175
|
});
|
|
175
176
|
const external_node_fs_namespaceObject = require("node:fs");
|
|
@@ -213,25 +214,19 @@ var __webpack_exports__ = {};
|
|
|
213
214
|
if (isRspack) modifyRspackRuntimeModule(module, modifier);
|
|
214
215
|
else modifyWebpackRuntimeModule(module, modifier);
|
|
215
216
|
}
|
|
216
|
-
function pick(obj, keys) {
|
|
217
|
-
return keys.reduce((ret, key)=>{
|
|
218
|
-
if (void 0 !== obj[key]) ret[key] = obj[key];
|
|
219
|
-
return ret;
|
|
220
|
-
}, {});
|
|
221
|
-
}
|
|
222
217
|
class AsyncChunkRetryPlugin {
|
|
223
218
|
getRawRuntimeRetryCode() {
|
|
224
219
|
const { RuntimeGlobals } = core_namespaceObject.rspack;
|
|
225
220
|
const filename = 'asyncChunkRetry';
|
|
226
|
-
const runtimeFilePath = external_node_path_default().join(AsyncChunkRetryPlugin_dirname, 'runtime', this.
|
|
221
|
+
const runtimeFilePath = external_node_path_default().join(AsyncChunkRetryPlugin_dirname, 'runtime', this.minify ? `${filename}.min.js` : `${filename}.js`);
|
|
227
222
|
const rawText = external_node_fs_default().readFileSync(runtimeFilePath, 'utf-8');
|
|
228
223
|
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));
|
|
229
224
|
}
|
|
230
225
|
apply(compiler) {
|
|
231
226
|
compiler.hooks.thisCompilation.tap(this.name, (compilation)=>{
|
|
227
|
+
const isRspack = this.isRspack;
|
|
232
228
|
compilation.hooks.runtimeModule.tap(this.name, (module)=>{
|
|
233
229
|
var _module_constructor;
|
|
234
|
-
const { isRspack } = this.options;
|
|
235
230
|
const constructorName = isRspack ? module.constructorName : null == (_module_constructor = module.constructor) ? void 0 : _module_constructor.name;
|
|
236
231
|
const isCssLoadingRuntimeModule = 'CssLoadingRuntimeModule' === constructorName;
|
|
237
232
|
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);
|
|
@@ -243,26 +238,21 @@ var __webpack_exports__ = {};
|
|
|
243
238
|
});
|
|
244
239
|
});
|
|
245
240
|
}
|
|
246
|
-
constructor(options){
|
|
241
|
+
constructor(options, isRspack, minify){
|
|
247
242
|
_define_property(this, "name", 'ASYNC_CHUNK_RETRY_PLUGIN');
|
|
248
|
-
_define_property(this, "
|
|
243
|
+
_define_property(this, "isRspack", void 0);
|
|
244
|
+
_define_property(this, "minify", void 0);
|
|
249
245
|
_define_property(this, "runtimeOptions", void 0);
|
|
250
|
-
this.
|
|
251
|
-
this.
|
|
252
|
-
|
|
253
|
-
'max',
|
|
254
|
-
'onRetry',
|
|
255
|
-
'onSuccess',
|
|
256
|
-
'onFail',
|
|
257
|
-
'addQuery',
|
|
258
|
-
'test',
|
|
259
|
-
'delay'
|
|
260
|
-
]);
|
|
246
|
+
this.runtimeOptions = options;
|
|
247
|
+
this.isRspack = isRspack;
|
|
248
|
+
this.minify = minify;
|
|
261
249
|
}
|
|
262
250
|
}
|
|
263
251
|
const src_dirname = external_node_path_default().dirname((0, external_node_url_namespaceObject.fileURLToPath)(__rslib_import_meta_url__));
|
|
264
252
|
const PLUGIN_ASSETS_RETRY_NAME = 'rsbuild:assets-retry';
|
|
265
|
-
|
|
253
|
+
const ASSETS_RETRY_DATA_ATTRIBUTE = 'data-rsbuild-assets-retry';
|
|
254
|
+
function getRuntimeOptions(userOptions, defaultCrossOrigin) {
|
|
255
|
+
const { inlineScript, minify, ...runtimeOptions } = userOptions;
|
|
266
256
|
const defaultOptions = {
|
|
267
257
|
max: 3,
|
|
268
258
|
type: [
|
|
@@ -271,25 +261,33 @@ var __webpack_exports__ = {};
|
|
|
271
261
|
'img'
|
|
272
262
|
],
|
|
273
263
|
domain: [],
|
|
274
|
-
crossOrigin:
|
|
275
|
-
delay: 0
|
|
276
|
-
|
|
277
|
-
const result = {
|
|
278
|
-
...defaultOptions,
|
|
279
|
-
...userOptions
|
|
264
|
+
crossOrigin: defaultCrossOrigin,
|
|
265
|
+
delay: 0,
|
|
266
|
+
addQuery: false
|
|
280
267
|
};
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
268
|
+
function normalizeOption(options) {
|
|
269
|
+
const result = {
|
|
270
|
+
...defaultOptions,
|
|
271
|
+
...options
|
|
272
|
+
};
|
|
273
|
+
if (!Array.isArray(result.type) || 0 === result.type.length) result.type = defaultOptions.type;
|
|
274
|
+
if (!Array.isArray(result.domain) || 0 === result.domain.length) result.domain = defaultOptions.domain;
|
|
275
|
+
if (Array.isArray(result.domain)) result.domain = result.domain.filter(Boolean);
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
if ('rules' in runtimeOptions) {
|
|
279
|
+
const result = runtimeOptions.rules.map((i)=>normalizeOption(i));
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
return [
|
|
283
|
+
normalizeOption(runtimeOptions)
|
|
284
|
+
];
|
|
285
285
|
}
|
|
286
|
-
async function getRetryCode(
|
|
286
|
+
async function getRetryCode(runtimeOptions, minify) {
|
|
287
287
|
const filename = 'initialChunkRetry';
|
|
288
|
-
const { minify, inlineScript: _, ...restOptions } = options;
|
|
289
288
|
const runtimeFilePath = external_node_path_default().join(src_dirname, 'runtime', minify ? `${filename}.min.js` : `${filename}.js`);
|
|
290
289
|
const runtimeCode = await external_node_fs_default().promises.readFile(runtimeFilePath, 'utf-8');
|
|
291
|
-
|
|
292
|
-
return `(function(){${runtimeCode}})()`.replace('__RUNTIME_GLOBALS_OPTIONS__', serialize_javascript_default()(runtimeOptions));
|
|
290
|
+
return runtimeCode.replace('__RETRY_OPTIONS__', serialize_javascript_default()(runtimeOptions));
|
|
293
291
|
}
|
|
294
292
|
const pluginAssetsRetry = (userOptions = {})=>({
|
|
295
293
|
name: PLUGIN_ASSETS_RETRY_NAME,
|
|
@@ -297,25 +295,25 @@ var __webpack_exports__ = {};
|
|
|
297
295
|
const { inlineScript = true } = userOptions;
|
|
298
296
|
const getScriptPath = (environment)=>{
|
|
299
297
|
const distDir = environment.config.output.distPath.js;
|
|
300
|
-
return external_node_path_default().posix.join(distDir, "assets-retry.1-
|
|
298
|
+
return external_node_path_default().posix.join(distDir, "assets-retry.1-4-0.js");
|
|
301
299
|
};
|
|
302
|
-
const
|
|
303
|
-
|
|
304
|
-
|
|
300
|
+
const getDefaultValueFromRsbuildConfig = (config)=>{
|
|
301
|
+
var _config_output_minify;
|
|
302
|
+
const minify = 'boolean' == typeof config.output.minify ? config.output.minify : null == (_config_output_minify = config.output.minify) ? void 0 : _config_output_minify.js;
|
|
303
|
+
return {
|
|
304
|
+
crossorigin: config.html.crossorigin,
|
|
305
|
+
minify: Boolean(minify) && 'production' === config.mode
|
|
305
306
|
};
|
|
306
|
-
if (void 0 === options.crossOrigin) options.crossOrigin = config.html.crossorigin;
|
|
307
|
-
if (void 0 === options.minify) {
|
|
308
|
-
var _config_output_minify;
|
|
309
|
-
const minify = 'boolean' == typeof config.output.minify ? config.output.minify : null == (_config_output_minify = config.output.minify) ? void 0 : _config_output_minify.js;
|
|
310
|
-
options.minify = minify && 'production' === config.mode;
|
|
311
|
-
}
|
|
312
|
-
return options;
|
|
313
307
|
};
|
|
314
308
|
if (inlineScript) api.modifyHTMLTags(async ({ headTags, bodyTags }, { environment })=>{
|
|
315
|
-
const
|
|
309
|
+
const { minify, crossorigin } = getDefaultValueFromRsbuildConfig(environment.config);
|
|
310
|
+
const runtimeOptions = getRuntimeOptions(userOptions, crossorigin);
|
|
311
|
+
const code = await getRetryCode(runtimeOptions, minify);
|
|
316
312
|
headTags.unshift({
|
|
317
313
|
tag: "script",
|
|
318
|
-
attrs: {
|
|
314
|
+
attrs: {
|
|
315
|
+
[ASSETS_RETRY_DATA_ATTRIBUTE]: 'inline'
|
|
316
|
+
},
|
|
319
317
|
children: code
|
|
320
318
|
});
|
|
321
319
|
return {
|
|
@@ -330,7 +328,8 @@ var __webpack_exports__ = {};
|
|
|
330
328
|
headTags.unshift({
|
|
331
329
|
tag: "script",
|
|
332
330
|
attrs: {
|
|
333
|
-
src: url
|
|
331
|
+
src: url,
|
|
332
|
+
[ASSETS_RETRY_DATA_ATTRIBUTE]: 'external'
|
|
334
333
|
}
|
|
335
334
|
});
|
|
336
335
|
return {
|
|
@@ -342,28 +341,32 @@ var __webpack_exports__ = {};
|
|
|
342
341
|
stage: 'additional'
|
|
343
342
|
}, async ({ sources, compilation, environment })=>{
|
|
344
343
|
const scriptPath = getScriptPath(environment);
|
|
345
|
-
const
|
|
344
|
+
const { crossorigin, minify } = getDefaultValueFromRsbuildConfig(environment.config);
|
|
345
|
+
const runtimeOptions = getRuntimeOptions(userOptions, crossorigin);
|
|
346
|
+
const code = await getRetryCode(runtimeOptions, minify);
|
|
346
347
|
compilation.emitAsset(scriptPath, new sources.RawSource(code));
|
|
347
348
|
});
|
|
348
349
|
}
|
|
349
350
|
api.modifyBundlerChain(async (chain, { environment })=>{
|
|
350
351
|
const { config, htmlPaths } = environment;
|
|
351
352
|
if (!userOptions || 0 === Object.keys(htmlPaths).length) return;
|
|
352
|
-
const
|
|
353
|
+
const { crossorigin, minify } = getDefaultValueFromRsbuildConfig(config);
|
|
354
|
+
const runtimeOptions = getRuntimeOptions(userOptions, crossorigin);
|
|
353
355
|
const isRspack = 'rspack' === api.context.bundlerType;
|
|
354
356
|
chain.plugin('async-chunk-retry').use(AsyncChunkRetryPlugin, [
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
}
|
|
357
|
+
runtimeOptions,
|
|
358
|
+
isRspack,
|
|
359
|
+
minify
|
|
359
360
|
]);
|
|
360
361
|
});
|
|
361
362
|
}
|
|
362
363
|
});
|
|
363
364
|
})();
|
|
365
|
+
exports.ASSETS_RETRY_DATA_ATTRIBUTE = __webpack_exports__.ASSETS_RETRY_DATA_ATTRIBUTE;
|
|
364
366
|
exports.PLUGIN_ASSETS_RETRY_NAME = __webpack_exports__.PLUGIN_ASSETS_RETRY_NAME;
|
|
365
367
|
exports.pluginAssetsRetry = __webpack_exports__.pluginAssetsRetry;
|
|
366
368
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
369
|
+
"ASSETS_RETRY_DATA_ATTRIBUTE",
|
|
367
370
|
"PLUGIN_ASSETS_RETRY_NAME",
|
|
368
371
|
"pluginAssetsRetry"
|
|
369
372
|
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,100 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
export declare const
|
|
5
|
-
|
|
1
|
+
import type { CrossOrigin as CrossOrigin_2 } from '@rsbuild/core';
|
|
2
|
+
import type { RsbuildPlugin } from '@rsbuild/core';
|
|
3
|
+
|
|
4
|
+
export declare const ASSETS_RETRY_DATA_ATTRIBUTE = "data-rsbuild-assets-retry";
|
|
5
|
+
|
|
6
|
+
declare type AssetsRetryHookContext_2 = {
|
|
7
|
+
url: string;
|
|
8
|
+
times: number;
|
|
9
|
+
domain: string;
|
|
10
|
+
tagName: string;
|
|
11
|
+
isAsyncChunk: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
declare type CompileTimeRetryOptions = {
|
|
15
|
+
/**
|
|
16
|
+
* Whether to inline the runtime JavaScript code of Assets Retry plugin into the HTML file.
|
|
17
|
+
* @default true
|
|
18
|
+
*/
|
|
19
|
+
inlineScript?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Whether to minify the runtime JavaScript code of Assets Retry plugin.
|
|
22
|
+
* @default rsbuildConfig.mode === 'production'
|
|
23
|
+
*/
|
|
24
|
+
minify?: boolean;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export declare const PLUGIN_ASSETS_RETRY_NAME = "rsbuild:assets-retry";
|
|
28
|
+
|
|
29
|
+
export declare const pluginAssetsRetry: (userOptions?: PluginAssetsRetryOptions) => RsbuildPlugin;
|
|
30
|
+
|
|
31
|
+
export declare type PluginAssetsRetryOptions = (RuntimeRetryOptions & CompileTimeRetryOptions) | ({
|
|
32
|
+
rules: RuntimeRetryOptions[];
|
|
33
|
+
} & CompileTimeRetryOptions);
|
|
34
|
+
|
|
35
|
+
declare type RuntimeRetryOptions = RuntimeRetryOptionsWithDefaultValue & RuntimeRetryOptionsWithoutDefaultValue;
|
|
36
|
+
|
|
37
|
+
declare type RuntimeRetryOptionsWithDefaultValue = {
|
|
38
|
+
/**
|
|
39
|
+
* The maximum number of retries for a single asset.
|
|
40
|
+
* @default 3
|
|
41
|
+
*/
|
|
42
|
+
max?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Used to specify the HTML tag types that need to be retried.
|
|
45
|
+
* @default ['script', 'link', 'img']
|
|
46
|
+
*/
|
|
47
|
+
type?: string[];
|
|
48
|
+
/**
|
|
49
|
+
* Specifies the retry domain when assets fail to load.
|
|
50
|
+
*/
|
|
51
|
+
domain?: string[];
|
|
52
|
+
/**
|
|
53
|
+
* Set the `crossorigin` attribute for tags.
|
|
54
|
+
* @default rsbuildConfig.html.crossorigin
|
|
55
|
+
*/
|
|
56
|
+
crossOrigin?: boolean | CrossOrigin_2;
|
|
57
|
+
/**
|
|
58
|
+
* The delay time between retries. Unit: ms
|
|
59
|
+
* @default 0
|
|
60
|
+
*/
|
|
61
|
+
delay?: number | ((context: AssetsRetryHookContext_2) => number);
|
|
62
|
+
/**
|
|
63
|
+
* The function to add query parameters to the URL of the asset being retried.
|
|
64
|
+
* @param times e.g: 1 -> 2 -> 3
|
|
65
|
+
* @param originalQuery initial request url's query e.g: <script src="https://cdn.com/a.js?version=1"></script> -> "?version=1"
|
|
66
|
+
* @default false
|
|
67
|
+
* @description
|
|
68
|
+
*
|
|
69
|
+
* if set to `true`, `?retry=${times}` will be added to the url.
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* ({ times, originalQuery }) => hasQuery(originalQuery) ? `${getQuery(originalQuery)}&retry=${times}` : `?retry=${times}`
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
addQuery?: boolean | ((context: {
|
|
76
|
+
times: number;
|
|
77
|
+
originalQuery: string;
|
|
78
|
+
}) => string);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
declare type RuntimeRetryOptionsWithoutDefaultValue = {
|
|
82
|
+
/**
|
|
83
|
+
* The test function of the asset to be retried.
|
|
84
|
+
*/
|
|
85
|
+
test?: string | RegExp | ((url: string) => boolean);
|
|
86
|
+
/**
|
|
87
|
+
* The callback function when the asset is failed to be retried.
|
|
88
|
+
*/
|
|
89
|
+
onFail?: (context: AssetsRetryHookContext_2) => void;
|
|
90
|
+
/**
|
|
91
|
+
* The callback function when the asset is being retried.
|
|
92
|
+
*/
|
|
93
|
+
onRetry?: (context: AssetsRetryHookContext_2) => void;
|
|
94
|
+
/**
|
|
95
|
+
* The callback function when the asset is successfully retried.
|
|
96
|
+
*/
|
|
97
|
+
onSuccess?: (context: AssetsRetryHookContext_2) => void;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export { }
|
package/dist/index.js
CHANGED
|
@@ -189,25 +189,19 @@ function modifyRuntimeModule(module, modifier, isRspack) {
|
|
|
189
189
|
if (isRspack) modifyRspackRuntimeModule(module, modifier);
|
|
190
190
|
else modifyWebpackRuntimeModule(module, modifier);
|
|
191
191
|
}
|
|
192
|
-
function pick(obj, keys) {
|
|
193
|
-
return keys.reduce((ret, key)=>{
|
|
194
|
-
if (void 0 !== obj[key]) ret[key] = obj[key];
|
|
195
|
-
return ret;
|
|
196
|
-
}, {});
|
|
197
|
-
}
|
|
198
192
|
class AsyncChunkRetryPlugin {
|
|
199
193
|
getRawRuntimeRetryCode() {
|
|
200
194
|
const { RuntimeGlobals } = rspack;
|
|
201
195
|
const filename = 'asyncChunkRetry';
|
|
202
|
-
const runtimeFilePath = node_path.join(AsyncChunkRetryPlugin_dirname, 'runtime', this.
|
|
196
|
+
const runtimeFilePath = node_path.join(AsyncChunkRetryPlugin_dirname, 'runtime', this.minify ? `${filename}.min.js` : `${filename}.js`);
|
|
203
197
|
const rawText = node_fs.readFileSync(runtimeFilePath, 'utf-8');
|
|
204
198
|
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));
|
|
205
199
|
}
|
|
206
200
|
apply(compiler) {
|
|
207
201
|
compiler.hooks.thisCompilation.tap(this.name, (compilation)=>{
|
|
202
|
+
const isRspack = this.isRspack;
|
|
208
203
|
compilation.hooks.runtimeModule.tap(this.name, (module)=>{
|
|
209
204
|
var _module_constructor;
|
|
210
|
-
const { isRspack } = this.options;
|
|
211
205
|
const constructorName = isRspack ? module.constructorName : null == (_module_constructor = module.constructor) ? void 0 : _module_constructor.name;
|
|
212
206
|
const isCssLoadingRuntimeModule = 'CssLoadingRuntimeModule' === constructorName;
|
|
213
207
|
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);
|
|
@@ -219,26 +213,21 @@ class AsyncChunkRetryPlugin {
|
|
|
219
213
|
});
|
|
220
214
|
});
|
|
221
215
|
}
|
|
222
|
-
constructor(options){
|
|
216
|
+
constructor(options, isRspack, minify){
|
|
223
217
|
_define_property(this, "name", 'ASYNC_CHUNK_RETRY_PLUGIN');
|
|
224
|
-
_define_property(this, "
|
|
218
|
+
_define_property(this, "isRspack", void 0);
|
|
219
|
+
_define_property(this, "minify", void 0);
|
|
225
220
|
_define_property(this, "runtimeOptions", void 0);
|
|
226
|
-
this.
|
|
227
|
-
this.
|
|
228
|
-
|
|
229
|
-
'max',
|
|
230
|
-
'onRetry',
|
|
231
|
-
'onSuccess',
|
|
232
|
-
'onFail',
|
|
233
|
-
'addQuery',
|
|
234
|
-
'test',
|
|
235
|
-
'delay'
|
|
236
|
-
]);
|
|
221
|
+
this.runtimeOptions = options;
|
|
222
|
+
this.isRspack = isRspack;
|
|
223
|
+
this.minify = minify;
|
|
237
224
|
}
|
|
238
225
|
}
|
|
239
226
|
const src_dirname = node_path.dirname(fileURLToPath(import.meta.url));
|
|
240
227
|
const PLUGIN_ASSETS_RETRY_NAME = 'rsbuild:assets-retry';
|
|
241
|
-
|
|
228
|
+
const ASSETS_RETRY_DATA_ATTRIBUTE = 'data-rsbuild-assets-retry';
|
|
229
|
+
function getRuntimeOptions(userOptions, defaultCrossOrigin) {
|
|
230
|
+
const { inlineScript, minify, ...runtimeOptions } = userOptions;
|
|
242
231
|
const defaultOptions = {
|
|
243
232
|
max: 3,
|
|
244
233
|
type: [
|
|
@@ -247,25 +236,33 @@ function getRuntimeOptions(userOptions) {
|
|
|
247
236
|
'img'
|
|
248
237
|
],
|
|
249
238
|
domain: [],
|
|
250
|
-
crossOrigin:
|
|
251
|
-
delay: 0
|
|
252
|
-
|
|
253
|
-
const result = {
|
|
254
|
-
...defaultOptions,
|
|
255
|
-
...userOptions
|
|
239
|
+
crossOrigin: defaultCrossOrigin,
|
|
240
|
+
delay: 0,
|
|
241
|
+
addQuery: false
|
|
256
242
|
};
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
243
|
+
function normalizeOption(options) {
|
|
244
|
+
const result = {
|
|
245
|
+
...defaultOptions,
|
|
246
|
+
...options
|
|
247
|
+
};
|
|
248
|
+
if (!Array.isArray(result.type) || 0 === result.type.length) result.type = defaultOptions.type;
|
|
249
|
+
if (!Array.isArray(result.domain) || 0 === result.domain.length) result.domain = defaultOptions.domain;
|
|
250
|
+
if (Array.isArray(result.domain)) result.domain = result.domain.filter(Boolean);
|
|
251
|
+
return result;
|
|
252
|
+
}
|
|
253
|
+
if ('rules' in runtimeOptions) {
|
|
254
|
+
const result = runtimeOptions.rules.map((i)=>normalizeOption(i));
|
|
255
|
+
return result;
|
|
256
|
+
}
|
|
257
|
+
return [
|
|
258
|
+
normalizeOption(runtimeOptions)
|
|
259
|
+
];
|
|
261
260
|
}
|
|
262
|
-
async function getRetryCode(
|
|
261
|
+
async function getRetryCode(runtimeOptions, minify) {
|
|
263
262
|
const filename = 'initialChunkRetry';
|
|
264
|
-
const { minify, inlineScript: _, ...restOptions } = options;
|
|
265
263
|
const runtimeFilePath = node_path.join(src_dirname, 'runtime', minify ? `${filename}.min.js` : `${filename}.js`);
|
|
266
264
|
const runtimeCode = await node_fs.promises.readFile(runtimeFilePath, 'utf-8');
|
|
267
|
-
|
|
268
|
-
return `(function(){${runtimeCode}})()`.replace('__RUNTIME_GLOBALS_OPTIONS__', serialize_javascript_default()(runtimeOptions));
|
|
265
|
+
return runtimeCode.replace('__RETRY_OPTIONS__', serialize_javascript_default()(runtimeOptions));
|
|
269
266
|
}
|
|
270
267
|
const pluginAssetsRetry = (userOptions = {})=>({
|
|
271
268
|
name: PLUGIN_ASSETS_RETRY_NAME,
|
|
@@ -273,25 +270,25 @@ const pluginAssetsRetry = (userOptions = {})=>({
|
|
|
273
270
|
const { inlineScript = true } = userOptions;
|
|
274
271
|
const getScriptPath = (environment)=>{
|
|
275
272
|
const distDir = environment.config.output.distPath.js;
|
|
276
|
-
return node_path.posix.join(distDir, "assets-retry.1-
|
|
273
|
+
return node_path.posix.join(distDir, "assets-retry.1-4-0.js");
|
|
277
274
|
};
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
275
|
+
const getDefaultValueFromRsbuildConfig = (config)=>{
|
|
276
|
+
var _config_output_minify;
|
|
277
|
+
const minify = 'boolean' == typeof config.output.minify ? config.output.minify : null == (_config_output_minify = config.output.minify) ? void 0 : _config_output_minify.js;
|
|
278
|
+
return {
|
|
279
|
+
crossorigin: config.html.crossorigin,
|
|
280
|
+
minify: Boolean(minify) && 'production' === config.mode
|
|
281
281
|
};
|
|
282
|
-
if (void 0 === options.crossOrigin) options.crossOrigin = config.html.crossorigin;
|
|
283
|
-
if (void 0 === options.minify) {
|
|
284
|
-
var _config_output_minify;
|
|
285
|
-
const minify = 'boolean' == typeof config.output.minify ? config.output.minify : null == (_config_output_minify = config.output.minify) ? void 0 : _config_output_minify.js;
|
|
286
|
-
options.minify = minify && 'production' === config.mode;
|
|
287
|
-
}
|
|
288
|
-
return options;
|
|
289
282
|
};
|
|
290
283
|
if (inlineScript) api.modifyHTMLTags(async ({ headTags, bodyTags }, { environment })=>{
|
|
291
|
-
const
|
|
284
|
+
const { minify, crossorigin } = getDefaultValueFromRsbuildConfig(environment.config);
|
|
285
|
+
const runtimeOptions = getRuntimeOptions(userOptions, crossorigin);
|
|
286
|
+
const code = await getRetryCode(runtimeOptions, minify);
|
|
292
287
|
headTags.unshift({
|
|
293
288
|
tag: "script",
|
|
294
|
-
attrs: {
|
|
289
|
+
attrs: {
|
|
290
|
+
[ASSETS_RETRY_DATA_ATTRIBUTE]: 'inline'
|
|
291
|
+
},
|
|
295
292
|
children: code
|
|
296
293
|
});
|
|
297
294
|
return {
|
|
@@ -306,7 +303,8 @@ const pluginAssetsRetry = (userOptions = {})=>({
|
|
|
306
303
|
headTags.unshift({
|
|
307
304
|
tag: "script",
|
|
308
305
|
attrs: {
|
|
309
|
-
src: url
|
|
306
|
+
src: url,
|
|
307
|
+
[ASSETS_RETRY_DATA_ATTRIBUTE]: 'external'
|
|
310
308
|
}
|
|
311
309
|
});
|
|
312
310
|
return {
|
|
@@ -318,22 +316,24 @@ const pluginAssetsRetry = (userOptions = {})=>({
|
|
|
318
316
|
stage: 'additional'
|
|
319
317
|
}, async ({ sources, compilation, environment })=>{
|
|
320
318
|
const scriptPath = getScriptPath(environment);
|
|
321
|
-
const
|
|
319
|
+
const { crossorigin, minify } = getDefaultValueFromRsbuildConfig(environment.config);
|
|
320
|
+
const runtimeOptions = getRuntimeOptions(userOptions, crossorigin);
|
|
321
|
+
const code = await getRetryCode(runtimeOptions, minify);
|
|
322
322
|
compilation.emitAsset(scriptPath, new sources.RawSource(code));
|
|
323
323
|
});
|
|
324
324
|
}
|
|
325
325
|
api.modifyBundlerChain(async (chain, { environment })=>{
|
|
326
326
|
const { config, htmlPaths } = environment;
|
|
327
327
|
if (!userOptions || 0 === Object.keys(htmlPaths).length) return;
|
|
328
|
-
const
|
|
328
|
+
const { crossorigin, minify } = getDefaultValueFromRsbuildConfig(config);
|
|
329
|
+
const runtimeOptions = getRuntimeOptions(userOptions, crossorigin);
|
|
329
330
|
const isRspack = 'rspack' === api.context.bundlerType;
|
|
330
331
|
chain.plugin('async-chunk-retry').use(AsyncChunkRetryPlugin, [
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
}
|
|
332
|
+
runtimeOptions,
|
|
333
|
+
isRspack,
|
|
334
|
+
minify
|
|
335
335
|
]);
|
|
336
336
|
});
|
|
337
337
|
}
|
|
338
338
|
});
|
|
339
|
-
export { PLUGIN_ASSETS_RETRY_NAME, pluginAssetsRetry };
|
|
339
|
+
export { ASSETS_RETRY_DATA_ATTRIBUTE, PLUGIN_ASSETS_RETRY_NAME, pluginAssetsRetry };
|