@lynx-js/externals-loading-webpack-plugin-canary 0.1.1 → 0.2.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/CHANGELOG.md +6 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +50 -5
- package/package.json +4 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @lynx-js/externals-loading-webpack-plugin
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- feat: support retrying `fetchBundle` on timeout via a new `retries` option (defaults to `0`). ([#2681](https://github.com/lynx-family/lynx-stack/pull/2681))
|
|
8
|
+
|
|
3
9
|
## 0.1.1
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/lib/index.d.ts
CHANGED
|
@@ -80,6 +80,12 @@ export interface ExternalsLoadingPluginOptions {
|
|
|
80
80
|
* @defaultValue 2000
|
|
81
81
|
*/
|
|
82
82
|
timeout?: number | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* The number of additional attempts when fetching the external bundle times out (`response.code === -2`).
|
|
85
|
+
*
|
|
86
|
+
* @defaultValue 0
|
|
87
|
+
*/
|
|
88
|
+
retries?: number | undefined;
|
|
83
89
|
}
|
|
84
90
|
/**
|
|
85
91
|
* The value item of the externals.
|
|
@@ -204,6 +210,12 @@ export interface ExternalValue {
|
|
|
204
210
|
* @defaultValue `2000`
|
|
205
211
|
*/
|
|
206
212
|
timeout?: number;
|
|
213
|
+
/**
|
|
214
|
+
* The number of additional attempts when fetching the external bundle times out (`response.code === -2`).
|
|
215
|
+
*
|
|
216
|
+
* @defaultValue `0`
|
|
217
|
+
*/
|
|
218
|
+
retries?: number;
|
|
207
219
|
}
|
|
208
220
|
/**
|
|
209
221
|
* The options of the background or main-thread layer.
|
package/lib/index.js
CHANGED
|
@@ -73,6 +73,7 @@ export class ExternalsLoadingPlugin {
|
|
|
73
73
|
}
|
|
74
74
|
#genExternalsLoadingCode(chunkLayer) {
|
|
75
75
|
const url2fetchCode = new Map();
|
|
76
|
+
const url2retries = new Map();
|
|
76
77
|
const loadCode = new Set();
|
|
77
78
|
// filter duplicate externals by libraryName or package name to avoid loading the same external multiple times. We keep the last one.
|
|
78
79
|
const externalsMap = new Map();
|
|
@@ -97,6 +98,48 @@ export class ExternalsLoadingPlugin {
|
|
|
97
98
|
const lynxExternalGlobal = getLynxExternalGlobal(externalsLoadingPluginOptions.globalObject);
|
|
98
99
|
const runtimeGlobalsInit = `${lynxExternalGlobal} = ${lynxExternalGlobal} === undefined ? {} : ${lynxExternalGlobal};`;
|
|
99
100
|
const loadExternalFunc = `
|
|
101
|
+
function createRetryingHandler(fetchBundle, retries) {
|
|
102
|
+
let cachedResponse = null
|
|
103
|
+
let asyncPromise = null
|
|
104
|
+
let currentHandler = fetchBundle()
|
|
105
|
+
let attemptsUsed = 0
|
|
106
|
+
return {
|
|
107
|
+
then(onFulfilled, onRejected) {
|
|
108
|
+
if (asyncPromise === null) {
|
|
109
|
+
asyncPromise = new Promise((resolve, reject) => {
|
|
110
|
+
const tryNext = (response) => {
|
|
111
|
+
if (cachedResponse !== null) {
|
|
112
|
+
resolve(cachedResponse)
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
if (response.code === -2 && attemptsUsed < retries) {
|
|
116
|
+
attemptsUsed++
|
|
117
|
+
currentHandler = fetchBundle()
|
|
118
|
+
currentHandler.then(tryNext, reject)
|
|
119
|
+
} else {
|
|
120
|
+
if (response.code !== -2) cachedResponse = response
|
|
121
|
+
resolve(response)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
currentHandler.then(tryNext, reject)
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
return asyncPromise.then(onFulfilled, onRejected)
|
|
128
|
+
},
|
|
129
|
+
wait(timeout) {
|
|
130
|
+
if (cachedResponse !== null) return cachedResponse
|
|
131
|
+
let response = currentHandler.wait(timeout)
|
|
132
|
+
while (response.code === -2 && attemptsUsed < retries) {
|
|
133
|
+
attemptsUsed++
|
|
134
|
+
currentHandler = fetchBundle()
|
|
135
|
+
response = currentHandler.wait(timeout)
|
|
136
|
+
}
|
|
137
|
+
if (response.code !== -2) cachedResponse = response
|
|
138
|
+
return response
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
100
143
|
function createLoadExternalAsync(handler, sectionPath) {
|
|
101
144
|
return new Promise((resolve, reject) => {
|
|
102
145
|
handler.then((response) => {
|
|
@@ -165,7 +208,7 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
|
|
|
165
208
|
const sectionLoadTracker = new Map();
|
|
166
209
|
for (const [pkgName, external] of finalExternals) {
|
|
167
210
|
const { libraryName, url, bundlePath, async = true, timeout: timeoutInMs = externalsLoadingPluginOptions.timeout
|
|
168
|
-
?? 2000, } = external;
|
|
211
|
+
?? 2000, retries = externalsLoadingPluginOptions.retries ?? 0, } = external;
|
|
169
212
|
const layerOptions = external[layer];
|
|
170
213
|
// Lynx fetchBundle timeout is in seconds
|
|
171
214
|
const timeout = timeoutInMs / 1000;
|
|
@@ -191,7 +234,8 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
|
|
|
191
234
|
const fetchExpr = normalizedExternal.url
|
|
192
235
|
? JSON.stringify(normalizedExternal.url)
|
|
193
236
|
: `${compiler.webpack.RuntimeGlobals.publicPath} + ${JSON.stringify(normalizedExternal.bundlePath)}`;
|
|
194
|
-
url2fetchCode.set(urlKey, `lynx.fetchBundle(${fetchExpr}, {})
|
|
237
|
+
url2fetchCode.set(urlKey, `() => lynx.fetchBundle(${fetchExpr}, {})`);
|
|
238
|
+
url2retries.set(urlKey, Math.max(url2retries.get(urlKey) ?? 0, retries));
|
|
195
239
|
const mountVar = `${getLynxExternalGlobal(externalsLoadingPluginOptions.globalObject)}[${JSON.stringify(libraryNameStr)}]`;
|
|
196
240
|
// If another external already generated a loadScript call for this exact
|
|
197
241
|
// (bundle, section, async) triple, reuse its result instead of calling
|
|
@@ -207,16 +251,17 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
|
|
|
207
251
|
continue;
|
|
208
252
|
}
|
|
209
253
|
sectionLoadTracker.set(sectionKey, mountVar);
|
|
254
|
+
const handlerIndex = [...url2fetchCode.keys()].indexOf(urlKey);
|
|
210
255
|
if (async) {
|
|
211
|
-
loadCode.add(`${mountVar} = ${mountVar} === undefined ? createLoadExternalAsync(handler${
|
|
256
|
+
loadCode.add(`${mountVar} = ${mountVar} === undefined ? createLoadExternalAsync(handler${handlerIndex}, ${JSON.stringify(layerOptions.sectionPath)}) : ${mountVar};`);
|
|
212
257
|
continue;
|
|
213
258
|
}
|
|
214
|
-
loadCode.add(`${mountVar} = ${mountVar} === undefined ? createLoadExternalSync(handler${
|
|
259
|
+
loadCode.add(`${mountVar} = ${mountVar} === undefined ? createLoadExternalSync(handler${handlerIndex}, ${JSON.stringify(layerOptions.sectionPath)}, ${timeout}) : ${mountVar};`);
|
|
215
260
|
}
|
|
216
261
|
return [
|
|
217
262
|
runtimeGlobalsInit,
|
|
218
263
|
loadExternalFunc,
|
|
219
|
-
...[...url2fetchCode.
|
|
264
|
+
...[...url2fetchCode.entries()].map(([urlKey, fetchCode], index) => `const handler${index} = createRetryingHandler(${fetchCode}, ${url2retries.get(urlKey) ?? 0});`),
|
|
220
265
|
...loadCode,
|
|
221
266
|
].join('\n');
|
|
222
267
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lynx-js/externals-loading-webpack-plugin-canary",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A webpack plugin to load lynx external bundles.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"webpack",
|
|
@@ -33,6 +33,8 @@
|
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@rspack/core": "1.7.9",
|
|
36
|
+
"@rspack/test-tools": "1.5.6",
|
|
37
|
+
"@rstest/core": "0.8.1",
|
|
36
38
|
"bar": "./test/helpers/external-bundle-mock/bar",
|
|
37
39
|
"baz": "./test/helpers/external-bundle-mock/baz",
|
|
38
40
|
"foo": "./test/helpers/external-bundle-mock/foo",
|
|
@@ -43,6 +45,6 @@
|
|
|
43
45
|
},
|
|
44
46
|
"scripts": {
|
|
45
47
|
"api-extractor": "api-extractor run --verbose",
|
|
46
|
-
"test": "
|
|
48
|
+
"test": "rstest"
|
|
47
49
|
}
|
|
48
50
|
}
|