@lynx-js/externals-loading-webpack-plugin-canary 0.2.1 → 0.2.2-canary-20261029-74719e6c

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @lynx-js/externals-loading-webpack-plugin
2
2
 
3
+ ## 0.2.2-canary-20261029092850-74719e6cfdede5b9834e0d4fa1e67cc8a62b908b
4
+
5
+ ### Patch Changes
6
+
7
+ - Evaluate synchronous and asynchronous external sections when their modules are imported instead of during runtime startup. This lets a page initialize its worklet runtime before importing legacy component externals, while preserving bundle prefetching and caching exports or the loading Promise on first access. ([#4262](https://github.com/lynx-family/lynx-stack/pull/4262))
8
+
3
9
  ## 0.2.1
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -11,6 +11,15 @@ development serving and build-time asset emission.
11
11
  Use `@lynx-js/externals-loading-webpack-plugin` directly only when you need the
12
12
  low-level webpack/Rspack runtime integration.
13
13
 
14
+ External bundles are prefetched during startup, but their sections
15
+ are evaluated on the first import. This preserves module initialization order,
16
+ including initializing a page's worklet runtime before importing a legacy
17
+ component external. Synchronous exports and asynchronous loading Promises are
18
+ cached on first access; host-provided exports are preserved. Repeated access does
19
+ not execute the section again, including while an asynchronous load is pending.
20
+ This does not serialize asynchronous sibling modules: if an initializer awaits
21
+ another module, consumers must explicitly depend on its completion.
22
+
14
23
  ## Usage
15
24
 
16
25
  ```ts
package/lib/index.d.ts CHANGED
@@ -32,6 +32,8 @@ export interface ExternalsLoadingPluginOptions {
32
32
  * module.exports = {
33
33
  * plugins: [
34
34
  * new ExternalsLoadingPlugin({
35
+ * mainThreadLayer: 'main-thread',
36
+ * backgroundLayer: 'background',
35
37
  * externals: {
36
38
  * lodash: {
37
39
  * url: 'http://lodash.lynx.bundle',
@@ -52,6 +54,8 @@ export interface ExternalsLoadingPluginOptions {
52
54
  * module.exports = {
53
55
  * plugins: [
54
56
  * new ExternalsLoadingPlugin({
57
+ * mainThreadLayer: 'main-thread',
58
+ * backgroundLayer: 'background',
55
59
  * externals: {
56
60
  * lodash: {
57
61
  * url: 'http://lodash.lynx.bundle',
@@ -168,6 +172,7 @@ export interface ExternalValue {
168
172
  * preact: {
169
173
  * libraryName: ['ReactLynx', 'Preact'],
170
174
  * url: '……',
175
+ * async: false,
171
176
  * },
172
177
  * }
173
178
  * })
@@ -181,6 +186,15 @@ export interface ExternalValue {
181
186
  * }
182
187
  * ```
183
188
  *
189
+ * With the default `async: true`, a `promise` external is generated instead,
190
+ * which picks the subpath after the library promise resolves:
191
+ *
192
+ * ```js
193
+ * externals: {
194
+ * preact: 'promise Promise.resolve(lynx[Symbol.for("__LYNX_EXTERNAL_GLOBAL__")]["ReactLynx"]).then(function (m) { return m["Preact"]; })',
195
+ * }
196
+ * ```
197
+ *
184
198
  * @defaultValue `undefined`
185
199
  *
186
200
  * @example `Lodash`
@@ -241,8 +255,6 @@ export interface LayerOptions {
241
255
  * new ExternalsLoadingPlugin({
242
256
  * mainThreadLayer: 'main-thread',
243
257
  * backgroundLayer: 'background',
244
- * mainThreadChunks: ['index__main-thread'],
245
- * backgroundChunks: ['index'],
246
258
  * externals: {
247
259
  * 'lodash': {
248
260
  * url: 'http://lodash.lynx.bundle',
package/lib/index.js CHANGED
@@ -25,8 +25,6 @@ function validateExternals(externals) {
25
25
  * new ExternalsLoadingPlugin({
26
26
  * mainThreadLayer: 'main-thread',
27
27
  * backgroundLayer: 'background',
28
- * mainThreadChunks: ['index__main-thread'],
29
- * backgroundChunks: ['index'],
30
28
  * externals: {
31
29
  * 'lodash': {
32
30
  * url: 'http://lodash.lynx.bundle',
@@ -98,6 +96,24 @@ export class ExternalsLoadingPlugin {
98
96
  const lynxExternalGlobal = getLynxExternalGlobal(externalsLoadingPluginOptions.globalObject);
99
97
  const runtimeGlobalsInit = `${lynxExternalGlobal} = ${lynxExternalGlobal} === undefined ? {} : ${lynxExternalGlobal};`;
100
98
  const loadExternalFunc = `
99
+ function deferExternal(name, load) {
100
+ const globals = ${lynxExternalGlobal};
101
+ const descriptor = Object.getOwnPropertyDescriptor(globals, name);
102
+ if ((descriptor && descriptor.get) || globals[name] !== undefined) return;
103
+ function mount(value) {
104
+ Object.defineProperty(globals, name, {
105
+ value, writable: true, enumerable: true, configurable: true
106
+ });
107
+ return value;
108
+ }
109
+ Object.defineProperty(globals, name, {
110
+ configurable: true,
111
+ enumerable: true,
112
+ get() { return mount(load()); },
113
+ set: mount
114
+ });
115
+ }
116
+
101
117
  function createRetryingHandler(fetchBundle, retries) {
102
118
  let cachedResponse = null
103
119
  let asyncPromise = null
@@ -219,6 +235,7 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
219
235
  const libraryNameStr = Array.isArray(libraryNameWithDefault)
220
236
  ? libraryNameWithDefault[0]
221
237
  : libraryNameWithDefault;
238
+ const externalName = JSON.stringify(libraryNameStr);
222
239
  const normalizedExternal = bundlePath
223
240
  ? {
224
241
  ...external,
@@ -236,7 +253,7 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
236
253
  : `${compiler.webpack.RuntimeGlobals.publicPath} + ${JSON.stringify(normalizedExternal.bundlePath)}`;
237
254
  url2fetchCode.set(urlKey, `() => lynx.fetchBundle(${fetchExpr}, {})`);
238
255
  url2retries.set(urlKey, Math.max(url2retries.get(urlKey) ?? 0, retries));
239
- const mountVar = `${getLynxExternalGlobal(externalsLoadingPluginOptions.globalObject)}[${JSON.stringify(libraryNameStr)}]`;
256
+ const mountVar = `${getLynxExternalGlobal(externalsLoadingPluginOptions.globalObject)}[${externalName}]`;
240
257
  // If another external already generated a loadScript call for this exact
241
258
  // (bundle, section, async) triple, reuse its result instead of calling
242
259
  // loadScript again. async is included in the key because sync and async
@@ -247,16 +264,19 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
247
264
  if (existingMountVar !== undefined) {
248
265
  // Preserve any value the host may have pre-populated for this global,
249
266
  // matching the same === undefined guard used on the primary load path.
250
- loadCode.add(`${mountVar} = ${mountVar} === undefined ? ${existingMountVar} : ${mountVar};`);
267
+ const alias = `deferExternal(${externalName}, () => ${existingMountVar});`;
268
+ loadCode.add(alias);
251
269
  continue;
252
270
  }
253
271
  sectionLoadTracker.set(sectionKey, mountVar);
254
272
  const handlerIndex = [...url2fetchCode.keys()].indexOf(urlKey);
255
- if (async) {
256
- loadCode.add(`${mountVar} = ${mountVar} === undefined ? createLoadExternalAsync(handler${handlerIndex}, ${JSON.stringify(layerOptions.sectionPath)}) : ${mountVar};`);
257
- continue;
258
- }
259
- loadCode.add(`${mountVar} = ${mountVar} === undefined ? createLoadExternalSync(handler${handlerIndex}, ${JSON.stringify(layerOptions.sectionPath)}, ${timeout}) : ${mountVar};`);
273
+ const loader = async
274
+ ? 'createLoadExternalAsync'
275
+ : 'createLoadExternalSync';
276
+ const sectionPath = JSON.stringify(layerOptions.sectionPath);
277
+ const timeoutArgument = async ? '' : `, ${timeout}`;
278
+ const load = `deferExternal(${externalName}, () => ${loader}(handler${handlerIndex}, ${sectionPath}${timeoutArgument}));`;
279
+ loadCode.add(load);
260
280
  }
261
281
  return [
262
282
  runtimeGlobalsInit,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/externals-loading-webpack-plugin-canary",
3
- "version": "0.2.1",
3
+ "version": "0.2.2-canary-20261029-74719e6c",
4
4
  "description": "A webpack plugin to load lynx external bundles.",
5
5
  "keywords": [
6
6
  "webpack",
@@ -32,12 +32,12 @@
32
32
  "README.md"
33
33
  ],
34
34
  "devDependencies": {
35
- "@rspack/core": "2.1.3",
36
- "@rstest/core": "0.11.0",
35
+ "@lynx-js/test-tools": "0.0.0",
36
+ "@rspack/core": "2.2.7",
37
+ "@rstest/core": "0.12.1",
37
38
  "bar": "./test/helpers/external-bundle-mock/bar",
38
39
  "baz": "./test/helpers/external-bundle-mock/baz",
39
- "foo": "./test/helpers/external-bundle-mock/foo",
40
- "@lynx-js/test-tools": "0.0.0"
40
+ "foo": "./test/helpers/external-bundle-mock/foo"
41
41
  },
42
42
  "engines": {
43
43
  "node": ">=18"