@lynx-js/externals-loading-webpack-plugin-canary 0.1.1 → 0.2.1-canary-20260708-34318ea3
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 +18 -6
- package/lib/index.d.ts +12 -0
- package/lib/index.js +65 -9
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @lynx-js/externals-loading-webpack-plugin
|
|
2
2
|
|
|
3
|
+
## 0.2.1-canary-20260708031426-34318ea3432b6484a383707458ed9c4ee19e2097
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Fix `async: true` externals with a subpath `libraryName` (e.g. `['ReactLynx', 'React']`) resolving to `undefined`. The generated `promise` external now picks all subpath segments after the mounted namespace promise resolves, instead of reading them synchronously off the pending promise. ([#2928](https://github.com/lynx-family/lynx-stack/pull/2928))
|
|
8
|
+
|
|
9
|
+
## 0.2.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- feat: support retrying `fetchBundle` on timeout via a new `retries` option (defaults to `0`). ([#2681](https://github.com/lynx-family/lynx-stack/pull/2681))
|
|
14
|
+
|
|
3
15
|
## 0.1.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -55,18 +67,18 @@
|
|
|
55
67
|
|
|
56
68
|
```js
|
|
57
69
|
// webpack.config.js
|
|
58
|
-
import { ExternalsLoadingPlugin } from
|
|
70
|
+
import { ExternalsLoadingPlugin } from "@lynx-js/externals-loading-webpack-plugin";
|
|
59
71
|
|
|
60
72
|
export default {
|
|
61
73
|
plugins: [
|
|
62
74
|
new ExternalsLoadingPlugin({
|
|
63
|
-
mainThreadLayer:
|
|
64
|
-
backgroundLayer:
|
|
75
|
+
mainThreadLayer: "main-thread",
|
|
76
|
+
backgroundLayer: "background",
|
|
65
77
|
externals: {
|
|
66
78
|
lodash: {
|
|
67
|
-
url:
|
|
68
|
-
background: { sectionPath:
|
|
69
|
-
mainThread: { sectionPath:
|
|
79
|
+
url: "http://lodash.lynx.bundle",
|
|
80
|
+
background: { sectionPath: "background" },
|
|
81
|
+
mainThread: { sectionPath: "main-thread" },
|
|
70
82
|
},
|
|
71
83
|
},
|
|
72
84
|
}),
|
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
|
}
|
|
@@ -271,10 +316,21 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
|
|
|
271
316
|
&& externals[request]?.[currentLayer]) {
|
|
272
317
|
const isAsync = externals[request]?.async ?? true;
|
|
273
318
|
const libraryName = externals[request]?.libraryName ?? request;
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
319
|
+
const names = Array.isArray(libraryName) ? libraryName : [libraryName];
|
|
320
|
+
if (isAsync) {
|
|
321
|
+
// A `promise` external must be a single expression evaluating to a
|
|
322
|
+
// promise whose resolved value becomes the module exports. The
|
|
323
|
+
// runtime module mounts one promise per library that resolves to
|
|
324
|
+
// the whole namespace, so subpaths have to be picked after that
|
|
325
|
+
// promise resolves — an array request would apply the property
|
|
326
|
+
// access synchronously on the pending promise and yield undefined.
|
|
327
|
+
const mount = `${getLynxExternalGlobal(globalObject)}[${JSON.stringify(names[0])}]`;
|
|
328
|
+
const accessor = names.slice(1).map((name) => `[${JSON.stringify(name)}]`).join('');
|
|
329
|
+
return callback(undefined, accessor
|
|
330
|
+
? `Promise.resolve(${mount}).then(function (m) { return m${accessor}; })`
|
|
331
|
+
: mount, 'promise');
|
|
332
|
+
}
|
|
333
|
+
return callback(undefined, [getLynxExternalGlobal(globalObject), ...names], undefined);
|
|
278
334
|
}
|
|
279
335
|
// Continue without externalizing the import
|
|
280
336
|
callback();
|
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.1-canary-20260708-34318ea3",
|
|
4
4
|
"description": "A webpack plugin to load lynx external bundles.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"webpack",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"README.md"
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@rspack/core": "
|
|
35
|
+
"@rspack/core": "2.0.8",
|
|
36
|
+
"@rstest/core": "0.10.5",
|
|
36
37
|
"bar": "./test/helpers/external-bundle-mock/bar",
|
|
37
38
|
"baz": "./test/helpers/external-bundle-mock/baz",
|
|
38
39
|
"foo": "./test/helpers/external-bundle-mock/foo",
|
|
@@ -43,6 +44,6 @@
|
|
|
43
44
|
},
|
|
44
45
|
"scripts": {
|
|
45
46
|
"api-extractor": "api-extractor run --verbose",
|
|
46
|
-
"test": "
|
|
47
|
+
"test": "rstest"
|
|
47
48
|
}
|
|
48
49
|
}
|