@lynx-js/externals-loading-webpack-plugin-canary 0.0.1-canary-20251217-27dea6e6 → 0.0.1-canary-20251222-4d689a71
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 +2 -4
- package/lib/index.d.ts +4 -12
- package/lib/index.js +37 -25
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# @lynx-js/externals-loading-webpack-plugin
|
|
2
2
|
|
|
3
|
-
## 0.0.1-canary-
|
|
3
|
+
## 0.0.1-canary-20251222035207-4d689a7194cc9b431e6055e87792c1961a44af4d
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
- Introduce `@lynx-js/externals-loading-webpack-plugin`. ([#1924](https://github.com/lynx-family/lynx-stack/pull/1924))
|
|
7
|
+
- Introduce `@lynx-js/externals-loading-webpack-plugin`. It will help you to load externals built by `@lynx-js/lynx-bundle-rslib-config`. ([#1924](https://github.com/lynx-family/lynx-stack/pull/1924))
|
|
8
8
|
|
|
9
9
|
```js
|
|
10
10
|
// webpack.config.js
|
|
@@ -13,8 +13,6 @@
|
|
|
13
13
|
export default {
|
|
14
14
|
plugins: [
|
|
15
15
|
new ExternalsLoadingPlugin({
|
|
16
|
-
mainThreadChunks: ["index__main-thread"],
|
|
17
|
-
backgroundChunks: ["index"],
|
|
18
16
|
mainThreadLayer: "main-thread",
|
|
19
17
|
backgroundLayer: "background",
|
|
20
18
|
externals: {
|
package/lib/index.d.ts
CHANGED
|
@@ -13,14 +13,6 @@ import type { Compiler } from '@rspack/core';
|
|
|
13
13
|
* @public
|
|
14
14
|
*/
|
|
15
15
|
export interface ExternalsLoadingPluginOptions {
|
|
16
|
-
/**
|
|
17
|
-
* The chunk names to be considered as main thread chunks.
|
|
18
|
-
*/
|
|
19
|
-
mainThreadChunks: string[];
|
|
20
|
-
/**
|
|
21
|
-
* The chunk names to be considered as background chunks.
|
|
22
|
-
*/
|
|
23
|
-
backgroundChunks: string[];
|
|
24
16
|
/**
|
|
25
17
|
* The name of the main thread layer.
|
|
26
18
|
*/
|
|
@@ -97,7 +89,7 @@ export interface ExternalsLoadingPluginOptions {
|
|
|
97
89
|
*
|
|
98
90
|
* ```js
|
|
99
91
|
* externals: {
|
|
100
|
-
* lodash: '
|
|
92
|
+
* lodash: 'lynx[Symbol.for("__LYNX_EXTERNAL_GLOBAL__")].lodash',
|
|
101
93
|
* }
|
|
102
94
|
* ```
|
|
103
95
|
*
|
|
@@ -121,8 +113,8 @@ export interface ExternalsLoadingPluginOptions {
|
|
|
121
113
|
*
|
|
122
114
|
* ```js
|
|
123
115
|
* externals: {
|
|
124
|
-
* lodash: '
|
|
125
|
-
* 'lodash-es': '
|
|
116
|
+
* lodash: 'lynx[Symbol.for("__LYNX_EXTERNAL_GLOBAL__")].Lodash',
|
|
117
|
+
* 'lodash-es': 'lynx[Symbol.for("__LYNX_EXTERNAL_GLOBAL__")].Lodash',
|
|
126
118
|
* }
|
|
127
119
|
* ```
|
|
128
120
|
*
|
|
@@ -143,7 +135,7 @@ export interface ExternalsLoadingPluginOptions {
|
|
|
143
135
|
*
|
|
144
136
|
* ```js
|
|
145
137
|
* externals: {
|
|
146
|
-
* preact: '
|
|
138
|
+
* preact: 'lynx[Symbol.for("__LYNX_EXTERNAL_GLOBAL__")].ReactLynx.Preact',
|
|
147
139
|
* }
|
|
148
140
|
* ```
|
|
149
141
|
*
|
package/lib/index.js
CHANGED
|
@@ -1,9 +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
|
-
|
|
6
|
-
return `${layer === 'background' ? 'lynxCoreInject.tt.lynx_ex' : 'globalThis.lynx_ex'}`;
|
|
4
|
+
function getLynxExternalGlobal() {
|
|
5
|
+
return `lynx[Symbol.for('__LYNX_EXTERNAL_GLOBAL__')]`;
|
|
7
6
|
}
|
|
8
7
|
/**
|
|
9
8
|
* The webpack plugin to load lynx external bundles.
|
|
@@ -48,30 +47,32 @@ export class ExternalsLoadingPlugin {
|
|
|
48
47
|
const { RuntimeModule } = compiler.webpack;
|
|
49
48
|
const externalsLoadingPluginOptions = this.options;
|
|
50
49
|
class ExternalsLoadingRuntimeModule extends RuntimeModule {
|
|
51
|
-
|
|
50
|
+
options;
|
|
51
|
+
constructor(options) {
|
|
52
52
|
super('externals-loading-runtime');
|
|
53
|
+
this.options = options;
|
|
53
54
|
}
|
|
54
55
|
generate() {
|
|
55
|
-
if (!this.chunk?.name) {
|
|
56
|
+
if (!this.chunk?.name || !externalsLoadingPluginOptions.externals) {
|
|
56
57
|
return '';
|
|
57
58
|
}
|
|
58
|
-
|
|
59
|
-
return '';
|
|
60
|
-
}
|
|
61
|
-
if (externalsLoadingPluginOptions.backgroundChunks.some(name => name === this.chunk.name)) {
|
|
62
|
-
return this.#genFetchAndLoadCode('background');
|
|
63
|
-
}
|
|
64
|
-
if (externalsLoadingPluginOptions.mainThreadChunks.some(name => name === this.chunk.name)) {
|
|
65
|
-
return this.#genFetchAndLoadCode('mainThread');
|
|
66
|
-
}
|
|
67
|
-
return '';
|
|
59
|
+
return this.#genExternalsLoadingCode(this.options.layer);
|
|
68
60
|
}
|
|
69
|
-
#
|
|
61
|
+
#genExternalsLoadingCode(chunkLayer) {
|
|
70
62
|
const fetchCode = [];
|
|
71
|
-
const
|
|
72
|
-
const syncLoadCode = [];
|
|
63
|
+
const loadCode = [];
|
|
73
64
|
// filter duplicate externals by libraryName or package name to avoid loading the same external multiple times. We keep the last one.
|
|
74
65
|
const externalsMap = new Map();
|
|
66
|
+
let layer;
|
|
67
|
+
if (chunkLayer === externalsLoadingPluginOptions.backgroundLayer) {
|
|
68
|
+
layer = 'background';
|
|
69
|
+
}
|
|
70
|
+
else if (chunkLayer === externalsLoadingPluginOptions.mainThreadLayer) {
|
|
71
|
+
layer = 'mainThread';
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
return '';
|
|
75
|
+
}
|
|
75
76
|
for (const [pkgName, external] of Object.entries(externalsLoadingPluginOptions.externals)) {
|
|
76
77
|
externalsMap.set(external.libraryName ?? pkgName, external);
|
|
77
78
|
}
|
|
@@ -79,7 +80,7 @@ export class ExternalsLoadingPlugin {
|
|
|
79
80
|
if (externals.length === 0) {
|
|
80
81
|
return '';
|
|
81
82
|
}
|
|
82
|
-
const runtimeGlobalsInit = `${getLynxExternalGlobal(
|
|
83
|
+
const runtimeGlobalsInit = `${getLynxExternalGlobal()} = {};`;
|
|
83
84
|
const loadExternalFunc = `
|
|
84
85
|
function createLoadExternalAsync(handler, sectionPath) {
|
|
85
86
|
return new Promise((resolve, reject) => {
|
|
@@ -132,17 +133,16 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
|
|
|
132
133
|
hasUrlLibraryNamePairInjected.add(hash);
|
|
133
134
|
fetchCode.push(`const handler${i} = lynx.fetchBundle(${JSON.stringify(url)}, {});`);
|
|
134
135
|
if (async) {
|
|
135
|
-
|
|
136
|
+
loadCode.push(`${getLynxExternalGlobal()}[${JSON.stringify(libraryNameStr)}] = createLoadExternalAsync(handler${i}, ${JSON.stringify(layerOptions.sectionPath)});`);
|
|
136
137
|
continue;
|
|
137
138
|
}
|
|
138
|
-
|
|
139
|
+
loadCode.push(`${getLynxExternalGlobal()}[${JSON.stringify(libraryNameStr)}] = createLoadExternalSync(handler${i}, ${JSON.stringify(layerOptions.sectionPath)}, ${timeout});`);
|
|
139
140
|
}
|
|
140
141
|
return [
|
|
141
142
|
runtimeGlobalsInit,
|
|
142
143
|
loadExternalFunc,
|
|
143
144
|
fetchCode,
|
|
144
|
-
|
|
145
|
-
syncLoadCode,
|
|
145
|
+
loadCode,
|
|
146
146
|
].flat().join('\n');
|
|
147
147
|
}
|
|
148
148
|
}
|
|
@@ -159,7 +159,19 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
|
|
|
159
159
|
compiler.hooks.compilation.tap(ExternalsLoadingRuntimeModule.name, compilation => {
|
|
160
160
|
compilation.hooks.additionalTreeRuntimeRequirements
|
|
161
161
|
.tap(ExternalsLoadingRuntimeModule.name, (chunk) => {
|
|
162
|
-
compilation.
|
|
162
|
+
const modules = compilation.chunkGraph.getChunkModulesIterable(chunk);
|
|
163
|
+
let layer;
|
|
164
|
+
for (const module of modules) {
|
|
165
|
+
if (module.layer) {
|
|
166
|
+
layer = module.layer;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (!layer) {
|
|
171
|
+
// Skip chunks without a layer
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
compilation.addRuntimeModule(chunk, new ExternalsLoadingRuntimeModule({ layer }));
|
|
163
175
|
});
|
|
164
176
|
});
|
|
165
177
|
}
|
|
@@ -182,7 +194,7 @@ function createLoadExternalSync(handler, sectionPath, timeout) {
|
|
|
182
194
|
const isAsync = externals[request]?.async ?? true;
|
|
183
195
|
const libraryName = externals[request]?.libraryName ?? request;
|
|
184
196
|
return callback(undefined, [
|
|
185
|
-
getLynxExternalGlobal(
|
|
197
|
+
getLynxExternalGlobal(),
|
|
186
198
|
...(Array.isArray(libraryName) ? libraryName : [libraryName]),
|
|
187
199
|
], isAsync ? 'promise' : undefined);
|
|
188
200
|
}
|
package/package.json
CHANGED