@lynx-js/externals-loading-webpack-plugin-canary 0.3.0 → 0.4.0-canary-20261117-d35c6eff

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,14 @@
1
1
  # @lynx-js/externals-loading-webpack-plugin
2
2
 
3
+ ## 0.4.0-canary-20261117112233-d35c6effebdb71a82acbac6f4e92cfbc0b85d664
4
+
5
+ ### Minor Changes
6
+
7
+ - Fetch external bundles on first import instead of prefetching unused bundles during startup. Preserve synchronous exports and asynchronous Promise semantics, including runtime-selected loading modes. ([#4363](https://github.com/lynx-family/lynx-stack/pull/4363))
8
+
9
+ Propagate fetch failures to the importing module instead of leaving its loading Promise pending.
10
+ - Support standalone runtime URL resolvers evaluated on first import. String results preserve native synchronous loading, while Promise results support asynchronous URL delivery. Keep fixed URLs and bundlePath loading unchanged. ([#4370](https://github.com/lynx-family/lynx-stack/pull/4370))
11
+
3
12
  ## 0.3.0
4
13
 
5
14
  ### Minor Changes
package/README.md CHANGED
@@ -11,8 +11,11 @@ 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,
14
+ External bundles are fetched and their sections evaluated on the first import in
15
+ every loading mode. Configuring an external does not fetch it until it is used.
16
+ Static imports in the entry still trigger loading during startup. Synchronous
17
+ externals fetch and wait on first import; asynchronous externals return a Promise.
18
+ This preserves module initialization order,
16
19
  including initializing a page's worklet runtime before importing a legacy
17
20
  component external. Synchronous exports and asynchronous loading Promises are
18
21
  cached on first access; host-provided exports are preserved. Repeated access does
@@ -67,3 +70,12 @@ It runs in the generated runtime with `SystemInfo` (falling back to
67
70
  cannot capture build-time imports or variables. Use an arrow or function
68
71
  expression. Callback externals always use promise module semantics, including
69
72
  when synchronous loading is selected. Boolean options retain their behavior.
73
+
74
+ ## Runtime URLs
75
+
76
+ The `url` option also accepts a standalone function returning a
77
+ string or `Promise<string>`. It runs on first import in the consuming thread, so
78
+ the host can provide the address after startup. The function is serialized and
79
+ must not capture build-time variables. A synchronous resolver preserves the native
80
+ `fetchBundle` handler, including `wait()` for synchronous external loading. A
81
+ resolver returning a Promise requires asynchronous loading.
package/lib/index.d.ts CHANGED
@@ -1,11 +1,3 @@
1
- /**
2
- * @packageDocumentation
3
- *
4
- * A webpack plugin to load externals in Lynx. Use `lynx.fetchBundle()` and `lynx.loadScript()` API to load and parse the externals.
5
- *
6
- * @remarks
7
- * Requires Lynx version 3.5 or later.
8
- */
9
1
  import type { Compiler } from '@rspack/core';
10
2
  /**
11
3
  * The options of the `ExternalsLoadingPlugin`.
@@ -98,13 +90,16 @@ export interface ExternalsLoadingPluginOptions {
98
90
  */
99
91
  export interface ExternalValue {
100
92
  /**
101
- * The final bundle URL to fetch directly at runtime.
93
+ * A bundle URL or a standalone resolver evaluated on first import.
94
+ *
95
+ * Resolvers must not capture build-time variables. A string result preserves
96
+ * synchronous loading; a Promise result requires asynchronous loading.
102
97
  *
103
98
  * Use this when the external bundle is hosted outside the current build
104
99
  * output, such as on a CDN. If both `url` and `bundlePath` are provided,
105
100
  * `url` takes precedence because it is already the fully resolved address.
106
101
  */
107
- url?: string;
102
+ url?: string | (() => string | Promise<string>);
108
103
  /**
109
104
  * The bundle path resolved against the runtime public path.
110
105
  *
package/lib/index.js CHANGED
@@ -1,6 +1,27 @@
1
1
  // Copyright 2025 The Lynx Authors. All rights reserved.
2
2
  // Licensed under the Apache License Version 2.0 that can be found in the
3
3
  // LICENSE file in the root directory of this source tree.
4
+ /**
5
+ * @packageDocumentation
6
+ *
7
+ * A webpack plugin to load externals in Lynx. Use `lynx.fetchBundle()` and `lynx.loadScript()` API to load and parse the externals.
8
+ *
9
+ * @remarks
10
+ * Requires Lynx version 3.5 or later.
11
+ */
12
+ import { compileFunction } from 'node:vm';
13
+ function serializeUrlResolver(resolver) {
14
+ const source = resolver.toString();
15
+ try {
16
+ compileFunction(`return (${source});`);
17
+ return source;
18
+ }
19
+ catch {
20
+ const expression = `Object.values({${source}})[0]`;
21
+ compileFunction(`return (${expression});`);
22
+ return expression;
23
+ }
24
+ }
4
25
  function getLynxExternalGlobal(globalObject) {
5
26
  return `${globalObject ?? 'lynx'}[Symbol.for('__LYNX_EXTERNAL_GLOBAL__')]`;
6
27
  }
@@ -117,7 +138,7 @@ function deferExternal(name, load) {
117
138
  function createRetryingHandler(fetchBundle, retries) {
118
139
  let cachedResponse = null
119
140
  let asyncPromise = null
120
- let currentHandler = fetchBundle()
141
+ let currentHandler = null
121
142
  let attemptsUsed = 0
122
143
  return {
123
144
  then(onFulfilled, onRejected) {
@@ -130,13 +151,18 @@ function createRetryingHandler(fetchBundle, retries) {
130
151
  }
131
152
  if (response.code === -2 && attemptsUsed < retries) {
132
153
  attemptsUsed++
133
- currentHandler = fetchBundle()
134
- currentHandler.then(tryNext, reject)
154
+ try {
155
+ currentHandler = fetchBundle()
156
+ currentHandler.then(tryNext, reject)
157
+ } catch (error) {
158
+ reject(error)
159
+ }
135
160
  } else {
136
161
  if (response.code !== -2) cachedResponse = response
137
162
  resolve(response)
138
163
  }
139
164
  }
165
+ if (currentHandler === null) currentHandler = fetchBundle()
140
166
  currentHandler.then(tryNext, reject)
141
167
  })
142
168
  }
@@ -144,6 +170,10 @@ function createRetryingHandler(fetchBundle, retries) {
144
170
  },
145
171
  wait(timeout) {
146
172
  if (cachedResponse !== null) return cachedResponse
173
+ if (currentHandler === null) currentHandler = fetchBundle()
174
+ if (typeof currentHandler.wait !== 'function') {
175
+ throw new Error('Synchronous external loading requires a synchronous URL resolver and a fetchBundle handler with wait(). Use async: true for asynchronous loading.')
176
+ }
147
177
  let response = currentHandler.wait(timeout)
148
178
  while (response.code === -2 && attemptsUsed < retries) {
149
179
  attemptsUsed++
@@ -184,7 +214,7 @@ function createLoadExternalAsync(handler, sectionPath) {
184
214
  } else {
185
215
  reject(new Error('Failed to fetch external source ' + response.url + ' . The response is ' + JSON.stringify(response), { cause: response }));
186
216
  }
187
- })
217
+ }, reject)
188
218
  })
189
219
  }
190
220
 
@@ -242,7 +272,12 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
242
272
  bundlePath: bundlePath.replace(/^\/+/, ''),
243
273
  }
244
274
  : external;
245
- const urlKey = url ?? `bundlePath:${normalizedExternal.bundlePath}`;
275
+ const resolver = typeof url === 'function'
276
+ ? serializeUrlResolver(url)
277
+ : undefined;
278
+ const urlKey = typeof url === 'function'
279
+ ? `resolver:${resolver}`
280
+ : url ?? `bundlePath:${normalizedExternal.bundlePath}`;
246
281
  const hash = `${urlKey}-${libraryNameStr}`;
247
282
  if (hasUrlLibraryNamePairInjected.has(hash)) {
248
283
  continue;
@@ -251,7 +286,10 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
251
286
  const fetchExpr = normalizedExternal.url
252
287
  ? JSON.stringify(normalizedExternal.url)
253
288
  : `${compiler.webpack.RuntimeGlobals.publicPath} + ${JSON.stringify(normalizedExternal.bundlePath)}`;
254
- url2fetchCode.set(urlKey, `() => lynx.fetchBundle(${fetchExpr}, {})`);
289
+ const fetchCode = resolver
290
+ ? `() => { const url = (${resolver})(); return typeof url === "string" ? lynx.fetchBundle(url, {}) : url.then(url => lynx.fetchBundle(url, {})); }`
291
+ : `() => lynx.fetchBundle(${fetchExpr}, {})`;
292
+ url2fetchCode.set(urlKey, fetchCode);
255
293
  url2retries.set(urlKey, Math.max(url2retries.get(urlKey) ?? 0, retries));
256
294
  const mountVar = `${getLynxExternalGlobal(externalsLoadingPluginOptions.globalObject)}[${externalName}]`;
257
295
  // If another external already generated a loadScript call for this exact
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/externals-loading-webpack-plugin-canary",
3
- "version": "0.3.0",
3
+ "version": "0.4.0-canary-20261117-d35c6eff",
4
4
  "description": "A webpack plugin to load lynx external bundles.",
5
5
  "keywords": [
6
6
  "webpack",