@lynx-js/externals-loading-webpack-plugin-canary 0.0.2 → 0.0.3-canary-20260126-c691f87a

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.0.3-canary-20260126040139-c691f87a04693fedfdd15d2f7841e2b61339112b
4
+
5
+ ### Patch Changes
6
+
7
+ - Add [`globalObject`](https://webpack.js.org/configuration/output/#outputglobalobject) config for external bundle loading, user can configure it to `globalThis` for BTS external bundle sharing. ([#2123](https://github.com/lynx-family/lynx-stack/pull/2123))
8
+
3
9
  ## 0.0.2
4
10
 
5
11
  ### Patch Changes
@@ -14,18 +20,18 @@
14
20
 
15
21
  ```js
16
22
  // webpack.config.js
17
- import { ExternalsLoadingPlugin } from '@lynx-js/externals-loading-webpack-plugin';
23
+ import { ExternalsLoadingPlugin } from "@lynx-js/externals-loading-webpack-plugin";
18
24
 
19
25
  export default {
20
26
  plugins: [
21
27
  new ExternalsLoadingPlugin({
22
- mainThreadLayer: 'main-thread',
23
- backgroundLayer: 'background',
28
+ mainThreadLayer: "main-thread",
29
+ backgroundLayer: "background",
24
30
  externals: {
25
31
  lodash: {
26
- url: 'http://lodash.lynx.bundle',
27
- background: { sectionPath: 'background' },
28
- mainThread: { sectionPath: 'main-thread' },
32
+ url: "http://lodash.lynx.bundle",
33
+ background: { sectionPath: "background" },
34
+ mainThread: { sectionPath: "main-thread" },
29
35
  },
30
36
  },
31
37
  }),
package/lib/index.d.ts CHANGED
@@ -64,6 +64,16 @@ export interface ExternalsLoadingPluginOptions {
64
64
  * ```
65
65
  */
66
66
  externals: Record<string, ExternalValue>;
67
+ /**
68
+ * This option indicates what global object will be used to mount the library.
69
+ *
70
+ * In Lynx, the library will be mounted to `lynx[Symbol.for("__LYNX_EXTERNAL_GLOBAL__")]` by default.
71
+ *
72
+ * If you have enabled share js context and want to reuse the library by mounting to the global object, you can set this option to `'globalThis'`.
73
+ *
74
+ * @default 'lynx'
75
+ */
76
+ globalObject?: 'lynx' | 'globalThis' | undefined;
67
77
  }
68
78
  /**
69
79
  * The value item of the externals.
package/lib/index.js CHANGED
@@ -1,8 +1,8 @@
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
- function getLynxExternalGlobal() {
5
- return `lynx[Symbol.for('__LYNX_EXTERNAL_GLOBAL__')]`;
4
+ function getLynxExternalGlobal(globalObject) {
5
+ return `${globalObject ?? 'lynx'}[Symbol.for('__LYNX_EXTERNAL_GLOBAL__')]`;
6
6
  }
7
7
  /**
8
8
  * The webpack plugin to load lynx external bundles.
@@ -80,7 +80,8 @@ export class ExternalsLoadingPlugin {
80
80
  if (externals.length === 0) {
81
81
  return '';
82
82
  }
83
- const runtimeGlobalsInit = `${getLynxExternalGlobal()} = {};`;
83
+ const lynxExternalGlobal = getLynxExternalGlobal(externalsLoadingPluginOptions.globalObject);
84
+ const runtimeGlobalsInit = `${lynxExternalGlobal} = ${lynxExternalGlobal} === undefined ? {} : ${lynxExternalGlobal};`;
84
85
  const loadExternalFunc = `
85
86
  function createLoadExternalAsync(handler, sectionPath) {
86
87
  return new Promise((resolve, reject) => {
@@ -132,11 +133,12 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
132
133
  }
133
134
  hasUrlLibraryNamePairInjected.add(hash);
134
135
  fetchCode.push(`const handler${i} = lynx.fetchBundle(${JSON.stringify(url)}, {});`);
136
+ const mountVar = `${getLynxExternalGlobal(externalsLoadingPluginOptions.globalObject)}[${JSON.stringify(libraryNameStr)}]`;
135
137
  if (async) {
136
- loadCode.push(`${getLynxExternalGlobal()}[${JSON.stringify(libraryNameStr)}] = createLoadExternalAsync(handler${i}, ${JSON.stringify(layerOptions.sectionPath)});`);
138
+ loadCode.push(`${mountVar} = ${mountVar} === undefined ? createLoadExternalAsync(handler${i}, ${JSON.stringify(layerOptions.sectionPath)}) : ${mountVar};`);
137
139
  continue;
138
140
  }
139
- loadCode.push(`${getLynxExternalGlobal()}[${JSON.stringify(libraryNameStr)}] = createLoadExternalSync(handler${i}, ${JSON.stringify(layerOptions.sectionPath)}, ${timeout});`);
141
+ loadCode.push(`${mountVar} = ${mountVar} === undefined ? createLoadExternalSync(handler${i}, ${JSON.stringify(layerOptions.sectionPath)}, ${timeout}) : ${mountVar};`);
140
142
  }
141
143
  return [
142
144
  runtimeGlobalsInit,
@@ -153,7 +155,7 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
153
155
  : (typeof compiler.options.externals === 'undefined'
154
156
  ? []
155
157
  : [compiler.options.externals])),
156
- this.#genExternalsConfig(),
158
+ this.#genExternalsConfig(externalsLoadingPluginOptions.globalObject),
157
159
  ];
158
160
  });
159
161
  compiler.hooks.compilation.tap(ExternalsLoadingRuntimeModule.name, compilation => {
@@ -178,7 +180,7 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
178
180
  /**
179
181
  * If the external is async, use `promise` external type; otherwise, use `var` external type.
180
182
  */
181
- #genExternalsConfig() {
183
+ #genExternalsConfig(globalObject) {
182
184
  const { externals, backgroundLayer, mainThreadLayer } = this.options;
183
185
  const externalDeps = new Set(Object.keys(externals));
184
186
  return ({ request, contextInfo }, callback) => {
@@ -194,7 +196,7 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
194
196
  const isAsync = externals[request]?.async ?? true;
195
197
  const libraryName = externals[request]?.libraryName ?? request;
196
198
  return callback(undefined, [
197
- getLynxExternalGlobal(),
199
+ getLynxExternalGlobal(globalObject),
198
200
  ...(Array.isArray(libraryName) ? libraryName : [libraryName]),
199
201
  ], isAsync ? 'promise' : undefined);
200
202
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/externals-loading-webpack-plugin-canary",
3
- "version": "0.0.2",
3
+ "version": "0.0.3-canary-20260126-c691f87a",
4
4
  "description": "A webpack plugin to load lynx external bundles.",
5
5
  "keywords": [
6
6
  "webpack",
@@ -32,7 +32,7 @@
32
32
  "README.md"
33
33
  ],
34
34
  "devDependencies": {
35
- "@rspack/core": "1.6.6",
35
+ "@rspack/core": "1.7.3",
36
36
  "foo": "./test/foo",
37
37
  "@lynx-js/test-tools": "0.0.0"
38
38
  },