@granite-js/plugin-micro-frontend 0.0.0-dev-20250725013859
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/LICENSE +202 -0
- package/README.md +99 -0
- package/dist/index.cjs +203 -0
- package/dist/index.d.cts +49 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +187 -0
- package/dist/runtime/index.cjs +75 -0
- package/dist/runtime/index.d.cts +77 -0
- package/dist/runtime/index.d.ts +77 -0
- package/dist/runtime/index.js +69 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2025 Viva Republica, Inc
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# @granite-js/plugin-micro-frontend
|
|
2
|
+
|
|
3
|
+
Plugin for sharing modules
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# NPM
|
|
9
|
+
npm install @granite-js/plugin-micro-frontend
|
|
10
|
+
|
|
11
|
+
# pnpm
|
|
12
|
+
pnpm add @granite-js/plugin-micro-frontend
|
|
13
|
+
|
|
14
|
+
# yarn
|
|
15
|
+
yarn add @granite-js/plugin-micro-frontend
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { defineConfig } from '@granite-js/react-native/config';
|
|
22
|
+
import { microFrontend } from '@granite-js/plugin-micro-frontend';
|
|
23
|
+
|
|
24
|
+
// Example 1. Host container
|
|
25
|
+
export default defineConfig({
|
|
26
|
+
plugins: [
|
|
27
|
+
microFrontend({
|
|
28
|
+
/**
|
|
29
|
+
* Container name
|
|
30
|
+
*/
|
|
31
|
+
name: 'host',
|
|
32
|
+
/**
|
|
33
|
+
* Configuration for the remote field to emulate the internal `importLazy` behavior
|
|
34
|
+
*/
|
|
35
|
+
remote: {
|
|
36
|
+
host: 'localhost',
|
|
37
|
+
port: 8082,
|
|
38
|
+
},
|
|
39
|
+
/**
|
|
40
|
+
* Shared modules config
|
|
41
|
+
*/
|
|
42
|
+
shared: {
|
|
43
|
+
react: {
|
|
44
|
+
/**
|
|
45
|
+
* Whether the module is eager
|
|
46
|
+
*
|
|
47
|
+
* Specifies whether the module is eager; if true, it's bundled with the host, otherwise loaded from the shared registry
|
|
48
|
+
*/
|
|
49
|
+
eager: true,
|
|
50
|
+
},
|
|
51
|
+
'react-native': {
|
|
52
|
+
eager: true,
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Example 2. Remote container
|
|
60
|
+
export default defineConfig({
|
|
61
|
+
plugins: [
|
|
62
|
+
microFrontend({
|
|
63
|
+
/**
|
|
64
|
+
* Container name
|
|
65
|
+
*/
|
|
66
|
+
name: 'remote',
|
|
67
|
+
/**
|
|
68
|
+
* Shared modules config
|
|
69
|
+
*/
|
|
70
|
+
shared: [
|
|
71
|
+
// Libraries are loaded from the shared registry
|
|
72
|
+
'react',
|
|
73
|
+
'react-native',
|
|
74
|
+
],
|
|
75
|
+
}),
|
|
76
|
+
],
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
This software is licensed under the [Apache 2 license](LICENSE), quoted below.
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
Copyright 2025 Viva Republica, Inc
|
|
86
|
+
|
|
87
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
88
|
+
you may not use this file except in compliance with the License.
|
|
89
|
+
You may obtain a copy of the License at:
|
|
90
|
+
|
|
91
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
92
|
+
|
|
93
|
+
Unless required by applicable law or agreed to in writing, software
|
|
94
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
95
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
96
|
+
See the License for the specific language governing permissions and
|
|
97
|
+
limitations under the License.
|
|
98
|
+
|
|
99
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
const fs = __toESM(require("fs"));
|
|
25
|
+
const path = __toESM(require("path"));
|
|
26
|
+
const __granite_js_utils = __toESM(require("@granite-js/utils"));
|
|
27
|
+
const picocolors = __toESM(require("picocolors"));
|
|
28
|
+
|
|
29
|
+
//#region src/prelude.ts
|
|
30
|
+
function getPreludeConfig(options) {
|
|
31
|
+
const sharedEntries = Object.entries(options.shared ?? {});
|
|
32
|
+
const eagerEntries = sharedEntries.filter(([_, config]) => config.eager === true);
|
|
33
|
+
const registerStatements = eagerEntries.map(([libName], index) => {
|
|
34
|
+
const identifier = `__mod${index}`;
|
|
35
|
+
return `
|
|
36
|
+
// ${libName}
|
|
37
|
+
import * as ${identifier} from '${libName}';
|
|
38
|
+
registerShared('${libName}', ${identifier});
|
|
39
|
+
`;
|
|
40
|
+
});
|
|
41
|
+
const exposeStatements = Object.entries(options.exposes ?? {}).map(([exposeName, modulePath], index) => {
|
|
42
|
+
const identifier = `__expose${index}`;
|
|
43
|
+
const resolvedModulePath = path.default.resolve(modulePath);
|
|
44
|
+
return `
|
|
45
|
+
import * as ${identifier} from '${resolvedModulePath}';
|
|
46
|
+
exposeModule(__container, '${exposeName}', ${identifier});
|
|
47
|
+
`;
|
|
48
|
+
});
|
|
49
|
+
const preludeScript = [
|
|
50
|
+
`import { registerShared, createContainer, exposeModule } from '@granite-js/plugin-micro-frontend/runtime';`,
|
|
51
|
+
`const __container = createContainer('${options.name}', ${JSON.stringify({
|
|
52
|
+
remote: options.remote,
|
|
53
|
+
shared: options.shared
|
|
54
|
+
})});`,
|
|
55
|
+
...registerStatements,
|
|
56
|
+
...exposeStatements
|
|
57
|
+
].join("\n");
|
|
58
|
+
return {
|
|
59
|
+
banner: `
|
|
60
|
+
if (global.__MICRO_FRONTEND__ == null) {
|
|
61
|
+
global.__MICRO_FRONTEND__ = {
|
|
62
|
+
__SHARED__: {},
|
|
63
|
+
__INSTANCES__: [],
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
`,
|
|
67
|
+
preludeScript
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/log.ts
|
|
73
|
+
const tag = picocolors.default.bold(picocolors.default.bgCyan(picocolors.default.black(" MICRO FRONTEND ")));
|
|
74
|
+
function log(...args) {
|
|
75
|
+
console.log(tag, ...args);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/remote.ts
|
|
80
|
+
const FALLBACK_SCRIPT = `console.warn('[MICRO FRONTEND] Failed to fetch remote bundles. Please check if the remote dev server is running')`;
|
|
81
|
+
async function fetchRemoteBundle(remote) {
|
|
82
|
+
globalThis.remoteBundles = {
|
|
83
|
+
android: FALLBACK_SCRIPT,
|
|
84
|
+
ios: FALLBACK_SCRIPT
|
|
85
|
+
};
|
|
86
|
+
try {
|
|
87
|
+
log("Prefetching remote bundles for development environment...");
|
|
88
|
+
const [androidBundle, iosBundle] = await Promise.all([fetchBundle(remote, "android"), fetchBundle(remote, "ios")]);
|
|
89
|
+
globalThis.remoteBundles = {
|
|
90
|
+
android: androidBundle,
|
|
91
|
+
ios: iosBundle
|
|
92
|
+
};
|
|
93
|
+
log("Fetch complete");
|
|
94
|
+
} catch {
|
|
95
|
+
log("Failed to fetch remote bundles. Please check if the remote dev server is running");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function fetchBundle(remote, platform) {
|
|
99
|
+
const response = await fetch(`http://${remote.host}:${remote.port}/index.bundle?dev=true&platform=${platform}`);
|
|
100
|
+
const bundle = await response.text();
|
|
101
|
+
return bundle;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/resolver.ts
|
|
106
|
+
const VIRTUAL_INITIALIZE_CORE_PROTOCOL = "virtual-initialize-core";
|
|
107
|
+
const VIRTUAL_SHARED_PROTOCOL = "virtual-shared";
|
|
108
|
+
function virtualInitializeCoreConfig() {
|
|
109
|
+
const reactNativePath = path.dirname(require.resolve("react-native/package.json", { paths: [process.cwd()] }));
|
|
110
|
+
const initializeCorePath = path.join(reactNativePath, "Libraries/Core/InitializeCore.js");
|
|
111
|
+
const alias = [{
|
|
112
|
+
from: `prelude:${initializeCorePath}`,
|
|
113
|
+
to: `${VIRTUAL_INITIALIZE_CORE_PROTOCOL}:noop`,
|
|
114
|
+
exact: false
|
|
115
|
+
}];
|
|
116
|
+
const protocols = { [VIRTUAL_INITIALIZE_CORE_PROTOCOL]: { load: function virtualInitializeCoreProtocolLoader() {
|
|
117
|
+
return {
|
|
118
|
+
loader: "js",
|
|
119
|
+
contents: `// noop`
|
|
120
|
+
};
|
|
121
|
+
} } };
|
|
122
|
+
return {
|
|
123
|
+
alias,
|
|
124
|
+
protocols
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function virtualSharedConfig(moduleEntries) {
|
|
128
|
+
const alias = moduleEntries.map(([libName]) => ({
|
|
129
|
+
from: libName,
|
|
130
|
+
to: `${VIRTUAL_SHARED_PROTOCOL}:${libName}`,
|
|
131
|
+
exact: true
|
|
132
|
+
}));
|
|
133
|
+
const protocols = { [VIRTUAL_SHARED_PROTOCOL]: { load: function virtualSharedProtocolLoader(args) {
|
|
134
|
+
return {
|
|
135
|
+
loader: "js",
|
|
136
|
+
contents: `
|
|
137
|
+
var sharedModule = global.__MICRO_FRONTEND__.__SHARED__['${args.path}'];
|
|
138
|
+
|
|
139
|
+
if (sharedModule == null) {
|
|
140
|
+
throw new Error("'${args.path}' is not registered in the shared registry");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
module.exports = sharedModule.get();
|
|
144
|
+
`
|
|
145
|
+
};
|
|
146
|
+
} } };
|
|
147
|
+
return {
|
|
148
|
+
alias,
|
|
149
|
+
protocols: alias.length > 0 ? protocols : void 0
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region src/utils/intoShared.ts
|
|
155
|
+
function intoShared(shared) {
|
|
156
|
+
if (Array.isArray(shared)) return shared.reduce((acc, lib) => {
|
|
157
|
+
acc[lib] = {};
|
|
158
|
+
return acc;
|
|
159
|
+
}, {});
|
|
160
|
+
return shared;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region src/microFrontendPlugin.ts
|
|
165
|
+
const microFrontendPlugin = async (options) => {
|
|
166
|
+
const sharedEntries = Object.entries(intoShared(options.shared) ?? {});
|
|
167
|
+
const nonEagerEntries = sharedEntries.filter(([_, config]) => config.eager !== true);
|
|
168
|
+
const rootDir = process.cwd();
|
|
169
|
+
const preludeConfig = getPreludeConfig(options);
|
|
170
|
+
const localDir = (0, __granite_js_utils.prepareLocalDirectory)(rootDir);
|
|
171
|
+
const preludePath = path.join(localDir, "micro-frontend-runtime.js");
|
|
172
|
+
fs.writeFileSync(preludePath, preludeConfig.preludeScript);
|
|
173
|
+
/**
|
|
174
|
+
* @TODO `MPACK_DEV_SERVER` flag should be removed after next version of bundle loader is released and load bundle dynamically at JS runtime.
|
|
175
|
+
*/
|
|
176
|
+
if (process.env.MPACK_DEV_SERVER === "true" && options.remote) await fetchRemoteBundle(options.remote);
|
|
177
|
+
/**
|
|
178
|
+
* If importing `react-native` from the shared registry,
|
|
179
|
+
* `InitializeCore.js` must be excluded from the bundle to ensure the core is loaded only once per runtime.
|
|
180
|
+
*/
|
|
181
|
+
const shouldExcludeReactNativeInitializeCore = Boolean(nonEagerEntries.find(([libName]) => libName === "react-native"));
|
|
182
|
+
const virtualInitializeCore = shouldExcludeReactNativeInitializeCore ? virtualInitializeCoreConfig() : void 0;
|
|
183
|
+
const virtualShared = virtualSharedConfig(nonEagerEntries);
|
|
184
|
+
return {
|
|
185
|
+
name: "micro-frontend-plugin",
|
|
186
|
+
config: {
|
|
187
|
+
resolver: {
|
|
188
|
+
alias: [...virtualInitializeCore?.alias ?? [], ...virtualShared.alias],
|
|
189
|
+
protocols: {
|
|
190
|
+
...virtualInitializeCore?.protocols,
|
|
191
|
+
...virtualShared.protocols
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
esbuild: {
|
|
195
|
+
prelude: [preludePath],
|
|
196
|
+
banner: { js: preludeConfig.banner }
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
//#endregion
|
|
203
|
+
exports.microFrontend = microFrontendPlugin;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { GranitePluginCore } from "@granite-js/plugin-core";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
interface MicroFrontendPluginOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Container name
|
|
7
|
+
*/
|
|
8
|
+
name: string;
|
|
9
|
+
/**
|
|
10
|
+
* Configuration for the remote field to emulate the internal `importLazy` behavior
|
|
11
|
+
*/
|
|
12
|
+
remote?: RemoteConfig;
|
|
13
|
+
/**
|
|
14
|
+
* Shared modules config
|
|
15
|
+
*/
|
|
16
|
+
shared?: SharedConfig | string[];
|
|
17
|
+
/**
|
|
18
|
+
* Configuration for exposing modules to other containers
|
|
19
|
+
*/
|
|
20
|
+
exposes?: ExposeConfig;
|
|
21
|
+
}
|
|
22
|
+
interface RemoteConfig {
|
|
23
|
+
/**
|
|
24
|
+
* Host name
|
|
25
|
+
*/
|
|
26
|
+
host: string;
|
|
27
|
+
/**
|
|
28
|
+
* Port number
|
|
29
|
+
*/
|
|
30
|
+
port: number;
|
|
31
|
+
}
|
|
32
|
+
interface SharedConfig {
|
|
33
|
+
[lib: string]: {
|
|
34
|
+
/**
|
|
35
|
+
* Whether the module is eager
|
|
36
|
+
*
|
|
37
|
+
* Specifies whether the module is eager; if true, it's bundled with the host, otherwise loaded from the shared registry
|
|
38
|
+
*/
|
|
39
|
+
eager?: boolean;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interface ExposeConfig {
|
|
43
|
+
[exposePath: string]: string;
|
|
44
|
+
} //#endregion
|
|
45
|
+
//#region src/microFrontendPlugin.d.ts
|
|
46
|
+
declare const microFrontendPlugin: (options: MicroFrontendPluginOptions) => Promise<GranitePluginCore>;
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
export { MicroFrontendPluginOptions, microFrontendPlugin as microFrontend };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { GranitePluginCore } from "@granite-js/plugin-core";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
interface MicroFrontendPluginOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Container name
|
|
7
|
+
*/
|
|
8
|
+
name: string;
|
|
9
|
+
/**
|
|
10
|
+
* Configuration for the remote field to emulate the internal `importLazy` behavior
|
|
11
|
+
*/
|
|
12
|
+
remote?: RemoteConfig;
|
|
13
|
+
/**
|
|
14
|
+
* Shared modules config
|
|
15
|
+
*/
|
|
16
|
+
shared?: SharedConfig | string[];
|
|
17
|
+
/**
|
|
18
|
+
* Configuration for exposing modules to other containers
|
|
19
|
+
*/
|
|
20
|
+
exposes?: ExposeConfig;
|
|
21
|
+
}
|
|
22
|
+
interface RemoteConfig {
|
|
23
|
+
/**
|
|
24
|
+
* Host name
|
|
25
|
+
*/
|
|
26
|
+
host: string;
|
|
27
|
+
/**
|
|
28
|
+
* Port number
|
|
29
|
+
*/
|
|
30
|
+
port: number;
|
|
31
|
+
}
|
|
32
|
+
interface SharedConfig {
|
|
33
|
+
[lib: string]: {
|
|
34
|
+
/**
|
|
35
|
+
* Whether the module is eager
|
|
36
|
+
*
|
|
37
|
+
* Specifies whether the module is eager; if true, it's bundled with the host, otherwise loaded from the shared registry
|
|
38
|
+
*/
|
|
39
|
+
eager?: boolean;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interface ExposeConfig {
|
|
43
|
+
[exposePath: string]: string;
|
|
44
|
+
} //#endregion
|
|
45
|
+
//#region src/microFrontendPlugin.d.ts
|
|
46
|
+
declare const microFrontendPlugin: (options: MicroFrontendPluginOptions) => Promise<GranitePluginCore>;
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
export { MicroFrontendPluginOptions, microFrontendPlugin as microFrontend };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path$2 from "path";
|
|
4
|
+
import * as path$1 from "path";
|
|
5
|
+
import path from "path";
|
|
6
|
+
import { prepareLocalDirectory } from "@granite-js/utils";
|
|
7
|
+
import pc from "picocolors";
|
|
8
|
+
|
|
9
|
+
//#region rolldown:runtime
|
|
10
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/prelude.ts
|
|
14
|
+
function getPreludeConfig(options) {
|
|
15
|
+
const sharedEntries = Object.entries(options.shared ?? {});
|
|
16
|
+
const eagerEntries = sharedEntries.filter(([_, config]) => config.eager === true);
|
|
17
|
+
const registerStatements = eagerEntries.map(([libName], index) => {
|
|
18
|
+
const identifier = `__mod${index}`;
|
|
19
|
+
return `
|
|
20
|
+
// ${libName}
|
|
21
|
+
import * as ${identifier} from '${libName}';
|
|
22
|
+
registerShared('${libName}', ${identifier});
|
|
23
|
+
`;
|
|
24
|
+
});
|
|
25
|
+
const exposeStatements = Object.entries(options.exposes ?? {}).map(([exposeName, modulePath], index) => {
|
|
26
|
+
const identifier = `__expose${index}`;
|
|
27
|
+
const resolvedModulePath = path.resolve(modulePath);
|
|
28
|
+
return `
|
|
29
|
+
import * as ${identifier} from '${resolvedModulePath}';
|
|
30
|
+
exposeModule(__container, '${exposeName}', ${identifier});
|
|
31
|
+
`;
|
|
32
|
+
});
|
|
33
|
+
const preludeScript = [
|
|
34
|
+
`import { registerShared, createContainer, exposeModule } from '@granite-js/plugin-micro-frontend/runtime';`,
|
|
35
|
+
`const __container = createContainer('${options.name}', ${JSON.stringify({
|
|
36
|
+
remote: options.remote,
|
|
37
|
+
shared: options.shared
|
|
38
|
+
})});`,
|
|
39
|
+
...registerStatements,
|
|
40
|
+
...exposeStatements
|
|
41
|
+
].join("\n");
|
|
42
|
+
return {
|
|
43
|
+
banner: `
|
|
44
|
+
if (global.__MICRO_FRONTEND__ == null) {
|
|
45
|
+
global.__MICRO_FRONTEND__ = {
|
|
46
|
+
__SHARED__: {},
|
|
47
|
+
__INSTANCES__: [],
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
`,
|
|
51
|
+
preludeScript
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/log.ts
|
|
57
|
+
const tag = pc.bold(pc.bgCyan(pc.black(" MICRO FRONTEND ")));
|
|
58
|
+
function log(...args) {
|
|
59
|
+
console.log(tag, ...args);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/remote.ts
|
|
64
|
+
const FALLBACK_SCRIPT = `console.warn('[MICRO FRONTEND] Failed to fetch remote bundles. Please check if the remote dev server is running')`;
|
|
65
|
+
async function fetchRemoteBundle(remote) {
|
|
66
|
+
globalThis.remoteBundles = {
|
|
67
|
+
android: FALLBACK_SCRIPT,
|
|
68
|
+
ios: FALLBACK_SCRIPT
|
|
69
|
+
};
|
|
70
|
+
try {
|
|
71
|
+
log("Prefetching remote bundles for development environment...");
|
|
72
|
+
const [androidBundle, iosBundle] = await Promise.all([fetchBundle(remote, "android"), fetchBundle(remote, "ios")]);
|
|
73
|
+
globalThis.remoteBundles = {
|
|
74
|
+
android: androidBundle,
|
|
75
|
+
ios: iosBundle
|
|
76
|
+
};
|
|
77
|
+
log("Fetch complete");
|
|
78
|
+
} catch {
|
|
79
|
+
log("Failed to fetch remote bundles. Please check if the remote dev server is running");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function fetchBundle(remote, platform) {
|
|
83
|
+
const response = await fetch(`http://${remote.host}:${remote.port}/index.bundle?dev=true&platform=${platform}`);
|
|
84
|
+
const bundle = await response.text();
|
|
85
|
+
return bundle;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/resolver.ts
|
|
90
|
+
const VIRTUAL_INITIALIZE_CORE_PROTOCOL = "virtual-initialize-core";
|
|
91
|
+
const VIRTUAL_SHARED_PROTOCOL = "virtual-shared";
|
|
92
|
+
function virtualInitializeCoreConfig() {
|
|
93
|
+
const reactNativePath = path$2.dirname(__require.resolve("react-native/package.json", { paths: [process.cwd()] }));
|
|
94
|
+
const initializeCorePath = path$2.join(reactNativePath, "Libraries/Core/InitializeCore.js");
|
|
95
|
+
const alias = [{
|
|
96
|
+
from: `prelude:${initializeCorePath}`,
|
|
97
|
+
to: `${VIRTUAL_INITIALIZE_CORE_PROTOCOL}:noop`,
|
|
98
|
+
exact: false
|
|
99
|
+
}];
|
|
100
|
+
const protocols = { [VIRTUAL_INITIALIZE_CORE_PROTOCOL]: { load: function virtualInitializeCoreProtocolLoader() {
|
|
101
|
+
return {
|
|
102
|
+
loader: "js",
|
|
103
|
+
contents: `// noop`
|
|
104
|
+
};
|
|
105
|
+
} } };
|
|
106
|
+
return {
|
|
107
|
+
alias,
|
|
108
|
+
protocols
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function virtualSharedConfig(moduleEntries) {
|
|
112
|
+
const alias = moduleEntries.map(([libName]) => ({
|
|
113
|
+
from: libName,
|
|
114
|
+
to: `${VIRTUAL_SHARED_PROTOCOL}:${libName}`,
|
|
115
|
+
exact: true
|
|
116
|
+
}));
|
|
117
|
+
const protocols = { [VIRTUAL_SHARED_PROTOCOL]: { load: function virtualSharedProtocolLoader(args) {
|
|
118
|
+
return {
|
|
119
|
+
loader: "js",
|
|
120
|
+
contents: `
|
|
121
|
+
var sharedModule = global.__MICRO_FRONTEND__.__SHARED__['${args.path}'];
|
|
122
|
+
|
|
123
|
+
if (sharedModule == null) {
|
|
124
|
+
throw new Error("'${args.path}' is not registered in the shared registry");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
module.exports = sharedModule.get();
|
|
128
|
+
`
|
|
129
|
+
};
|
|
130
|
+
} } };
|
|
131
|
+
return {
|
|
132
|
+
alias,
|
|
133
|
+
protocols: alias.length > 0 ? protocols : void 0
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src/utils/intoShared.ts
|
|
139
|
+
function intoShared(shared) {
|
|
140
|
+
if (Array.isArray(shared)) return shared.reduce((acc, lib) => {
|
|
141
|
+
acc[lib] = {};
|
|
142
|
+
return acc;
|
|
143
|
+
}, {});
|
|
144
|
+
return shared;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/microFrontendPlugin.ts
|
|
149
|
+
const microFrontendPlugin = async (options) => {
|
|
150
|
+
const sharedEntries = Object.entries(intoShared(options.shared) ?? {});
|
|
151
|
+
const nonEagerEntries = sharedEntries.filter(([_, config]) => config.eager !== true);
|
|
152
|
+
const rootDir = process.cwd();
|
|
153
|
+
const preludeConfig = getPreludeConfig(options);
|
|
154
|
+
const localDir = prepareLocalDirectory(rootDir);
|
|
155
|
+
const preludePath = path$1.join(localDir, "micro-frontend-runtime.js");
|
|
156
|
+
fs.writeFileSync(preludePath, preludeConfig.preludeScript);
|
|
157
|
+
/**
|
|
158
|
+
* @TODO `MPACK_DEV_SERVER` flag should be removed after next version of bundle loader is released and load bundle dynamically at JS runtime.
|
|
159
|
+
*/
|
|
160
|
+
if (process.env.MPACK_DEV_SERVER === "true" && options.remote) await fetchRemoteBundle(options.remote);
|
|
161
|
+
/**
|
|
162
|
+
* If importing `react-native` from the shared registry,
|
|
163
|
+
* `InitializeCore.js` must be excluded from the bundle to ensure the core is loaded only once per runtime.
|
|
164
|
+
*/
|
|
165
|
+
const shouldExcludeReactNativeInitializeCore = Boolean(nonEagerEntries.find(([libName]) => libName === "react-native"));
|
|
166
|
+
const virtualInitializeCore = shouldExcludeReactNativeInitializeCore ? virtualInitializeCoreConfig() : void 0;
|
|
167
|
+
const virtualShared = virtualSharedConfig(nonEagerEntries);
|
|
168
|
+
return {
|
|
169
|
+
name: "micro-frontend-plugin",
|
|
170
|
+
config: {
|
|
171
|
+
resolver: {
|
|
172
|
+
alias: [...virtualInitializeCore?.alias ?? [], ...virtualShared.alias],
|
|
173
|
+
protocols: {
|
|
174
|
+
...virtualInitializeCore?.protocols,
|
|
175
|
+
...virtualShared.protocols
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
esbuild: {
|
|
179
|
+
prelude: [preludePath],
|
|
180
|
+
banner: { js: preludeConfig.banner }
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
//#endregion
|
|
187
|
+
export { microFrontendPlugin as microFrontend };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/runtime/createContainer.ts
|
|
3
|
+
function createContainer(name, config) {
|
|
4
|
+
if (typeof global.__MICRO_FRONTEND__.__INSTANCES__[name] === "number") throw new Error(`'${name}' container already registered`);
|
|
5
|
+
const containerIndex = global.__MICRO_FRONTEND__.__INSTANCES__.length;
|
|
6
|
+
const container = {
|
|
7
|
+
name,
|
|
8
|
+
config,
|
|
9
|
+
exposeMap: {}
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(global.__MICRO_FRONTEND__.__INSTANCES__, name, {
|
|
12
|
+
value: containerIndex,
|
|
13
|
+
enumerable: false,
|
|
14
|
+
writable: false
|
|
15
|
+
});
|
|
16
|
+
global.__MICRO_FRONTEND__.__INSTANCES__.push(container);
|
|
17
|
+
return container;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/runtime/registerShared.ts
|
|
22
|
+
function registerShared(libName, module$1) {
|
|
23
|
+
if (global.__MICRO_FRONTEND__.__SHARED__[libName]) throw new Error(`'${libName}' already registered as a shared module`);
|
|
24
|
+
global.__MICRO_FRONTEND__.__SHARED__[libName] = {
|
|
25
|
+
get: () => Object.assign(module$1, { __esModule: true }),
|
|
26
|
+
loaded: true
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/runtime/utils.ts
|
|
32
|
+
function getContainer(instanceName) {
|
|
33
|
+
const containerIndex = __MICRO_FRONTEND__.__INSTANCES__[instanceName];
|
|
34
|
+
return typeof containerIndex === "number" ? __MICRO_FRONTEND__.__INSTANCES__[containerIndex] : null;
|
|
35
|
+
}
|
|
36
|
+
function normalizePath(path) {
|
|
37
|
+
if (path.startsWith("./")) return path.slice(2);
|
|
38
|
+
return path;
|
|
39
|
+
}
|
|
40
|
+
function parseRemotePath(remotePath) {
|
|
41
|
+
const [remoteName, ...rest] = remotePath.split("/");
|
|
42
|
+
if (remoteName && rest.length > 0) return {
|
|
43
|
+
remoteName,
|
|
44
|
+
modulePath: rest.join("/"),
|
|
45
|
+
fullRequest: remotePath
|
|
46
|
+
};
|
|
47
|
+
throw new Error(`Invalid remote request: ${remotePath}`);
|
|
48
|
+
}
|
|
49
|
+
function importRemoteModule(remoteRequestPath) {
|
|
50
|
+
const { remoteName, modulePath } = parseRemotePath(remoteRequestPath);
|
|
51
|
+
const container = getContainer(remoteName);
|
|
52
|
+
if (container == null) throw new Error(`${remoteName} container not found`);
|
|
53
|
+
const module$1 = container.exposeMap[normalizePath(modulePath)];
|
|
54
|
+
if (module$1 == null) throw new Error(`Could not resolve '${modulePath}' in ${remoteName} container`);
|
|
55
|
+
return module$1;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/runtime/exposeModule.ts
|
|
60
|
+
function exposeModule(container, exposeName, module$1) {
|
|
61
|
+
const normalizedExposeName = normalizePath(exposeName);
|
|
62
|
+
if (container.exposeMap[normalizedExposeName]) throw new Error(`'${exposeName}' is already exposed in ${container.name} container`);
|
|
63
|
+
Object.defineProperty(container.exposeMap, normalizedExposeName, {
|
|
64
|
+
get: () => Object.assign(module$1, { __esModule: true }),
|
|
65
|
+
enumerable: true
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
exports.createContainer = createContainer;
|
|
71
|
+
exports.exposeModule = exposeModule;
|
|
72
|
+
exports.getContainer = getContainer;
|
|
73
|
+
exports.importRemoteModule = importRemoteModule;
|
|
74
|
+
exports.parseRemotePath = parseRemotePath;
|
|
75
|
+
exports.registerShared = registerShared;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
|
|
3
|
+
interface RemoteConfig {
|
|
4
|
+
/**
|
|
5
|
+
* Host name
|
|
6
|
+
*/
|
|
7
|
+
host: string;
|
|
8
|
+
/**
|
|
9
|
+
* Port number
|
|
10
|
+
*/
|
|
11
|
+
port: number;
|
|
12
|
+
}
|
|
13
|
+
interface SharedConfig {
|
|
14
|
+
[lib: string]: {
|
|
15
|
+
/**
|
|
16
|
+
* Whether the module is eager
|
|
17
|
+
*
|
|
18
|
+
* Specifies whether the module is eager; if true, it's bundled with the host, otherwise loaded from the shared registry
|
|
19
|
+
*/
|
|
20
|
+
eager?: boolean;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
interface ExposeConfig {
|
|
24
|
+
[exposePath: string]: string;
|
|
25
|
+
} //#endregion
|
|
26
|
+
//#region src/runtime/types.d.ts
|
|
27
|
+
declare global {
|
|
28
|
+
var __MICRO_FRONTEND__: RuntimeContext;
|
|
29
|
+
}
|
|
30
|
+
interface RuntimeContext {
|
|
31
|
+
__INSTANCES__: Container[] & Record<string, number>;
|
|
32
|
+
__SHARED__: SharedModuleRegistry;
|
|
33
|
+
}
|
|
34
|
+
interface Container {
|
|
35
|
+
name: string;
|
|
36
|
+
exposeMap: Record<string, Module>;
|
|
37
|
+
config: {
|
|
38
|
+
remote?: RemoteConfig;
|
|
39
|
+
shared?: SharedConfig;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interface SharedModuleRegistry {
|
|
43
|
+
[libName: string]: {
|
|
44
|
+
get: () => Module;
|
|
45
|
+
loaded: boolean;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
type Module = any;
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/runtime/createContainer.d.ts
|
|
52
|
+
declare function createContainer(name: string, config: {
|
|
53
|
+
remote?: RemoteConfig;
|
|
54
|
+
shared?: SharedConfig;
|
|
55
|
+
exposes?: ExposeConfig;
|
|
56
|
+
}): Container;
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/runtime/registerShared.d.ts
|
|
60
|
+
declare function registerShared(libName: string, module: any): void;
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/runtime/exposeModule.d.ts
|
|
64
|
+
declare function exposeModule(container: Container, exposeName: string, module: Module): void;
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/runtime/utils.d.ts
|
|
68
|
+
declare function getContainer(instanceName: string): Container | null;
|
|
69
|
+
declare function parseRemotePath(remotePath: string): {
|
|
70
|
+
remoteName: string;
|
|
71
|
+
modulePath: string;
|
|
72
|
+
fullRequest: string;
|
|
73
|
+
};
|
|
74
|
+
declare function importRemoteModule(remoteRequestPath: string): any;
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
export { Container, Module, RuntimeContext, SharedModuleRegistry, createContainer, exposeModule, getContainer, importRemoteModule, parseRemotePath, registerShared };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
|
|
3
|
+
interface RemoteConfig {
|
|
4
|
+
/**
|
|
5
|
+
* Host name
|
|
6
|
+
*/
|
|
7
|
+
host: string;
|
|
8
|
+
/**
|
|
9
|
+
* Port number
|
|
10
|
+
*/
|
|
11
|
+
port: number;
|
|
12
|
+
}
|
|
13
|
+
interface SharedConfig {
|
|
14
|
+
[lib: string]: {
|
|
15
|
+
/**
|
|
16
|
+
* Whether the module is eager
|
|
17
|
+
*
|
|
18
|
+
* Specifies whether the module is eager; if true, it's bundled with the host, otherwise loaded from the shared registry
|
|
19
|
+
*/
|
|
20
|
+
eager?: boolean;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
interface ExposeConfig {
|
|
24
|
+
[exposePath: string]: string;
|
|
25
|
+
} //#endregion
|
|
26
|
+
//#region src/runtime/types.d.ts
|
|
27
|
+
declare global {
|
|
28
|
+
var __MICRO_FRONTEND__: RuntimeContext;
|
|
29
|
+
}
|
|
30
|
+
interface RuntimeContext {
|
|
31
|
+
__INSTANCES__: Container[] & Record<string, number>;
|
|
32
|
+
__SHARED__: SharedModuleRegistry;
|
|
33
|
+
}
|
|
34
|
+
interface Container {
|
|
35
|
+
name: string;
|
|
36
|
+
exposeMap: Record<string, Module>;
|
|
37
|
+
config: {
|
|
38
|
+
remote?: RemoteConfig;
|
|
39
|
+
shared?: SharedConfig;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interface SharedModuleRegistry {
|
|
43
|
+
[libName: string]: {
|
|
44
|
+
get: () => Module;
|
|
45
|
+
loaded: boolean;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
type Module = any;
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/runtime/createContainer.d.ts
|
|
52
|
+
declare function createContainer(name: string, config: {
|
|
53
|
+
remote?: RemoteConfig;
|
|
54
|
+
shared?: SharedConfig;
|
|
55
|
+
exposes?: ExposeConfig;
|
|
56
|
+
}): Container;
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/runtime/registerShared.d.ts
|
|
60
|
+
declare function registerShared(libName: string, module: any): void;
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/runtime/exposeModule.d.ts
|
|
64
|
+
declare function exposeModule(container: Container, exposeName: string, module: Module): void;
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/runtime/utils.d.ts
|
|
68
|
+
declare function getContainer(instanceName: string): Container | null;
|
|
69
|
+
declare function parseRemotePath(remotePath: string): {
|
|
70
|
+
remoteName: string;
|
|
71
|
+
modulePath: string;
|
|
72
|
+
fullRequest: string;
|
|
73
|
+
};
|
|
74
|
+
declare function importRemoteModule(remoteRequestPath: string): any;
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
export { Container, Module, RuntimeContext, SharedModuleRegistry, createContainer, exposeModule, getContainer, importRemoteModule, parseRemotePath, registerShared };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//#region src/runtime/createContainer.ts
|
|
2
|
+
function createContainer(name, config) {
|
|
3
|
+
if (typeof global.__MICRO_FRONTEND__.__INSTANCES__[name] === "number") throw new Error(`'${name}' container already registered`);
|
|
4
|
+
const containerIndex = global.__MICRO_FRONTEND__.__INSTANCES__.length;
|
|
5
|
+
const container = {
|
|
6
|
+
name,
|
|
7
|
+
config,
|
|
8
|
+
exposeMap: {}
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(global.__MICRO_FRONTEND__.__INSTANCES__, name, {
|
|
11
|
+
value: containerIndex,
|
|
12
|
+
enumerable: false,
|
|
13
|
+
writable: false
|
|
14
|
+
});
|
|
15
|
+
global.__MICRO_FRONTEND__.__INSTANCES__.push(container);
|
|
16
|
+
return container;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/runtime/registerShared.ts
|
|
21
|
+
function registerShared(libName, module) {
|
|
22
|
+
if (global.__MICRO_FRONTEND__.__SHARED__[libName]) throw new Error(`'${libName}' already registered as a shared module`);
|
|
23
|
+
global.__MICRO_FRONTEND__.__SHARED__[libName] = {
|
|
24
|
+
get: () => Object.assign(module, { __esModule: true }),
|
|
25
|
+
loaded: true
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/runtime/utils.ts
|
|
31
|
+
function getContainer(instanceName) {
|
|
32
|
+
const containerIndex = __MICRO_FRONTEND__.__INSTANCES__[instanceName];
|
|
33
|
+
return typeof containerIndex === "number" ? __MICRO_FRONTEND__.__INSTANCES__[containerIndex] : null;
|
|
34
|
+
}
|
|
35
|
+
function normalizePath(path) {
|
|
36
|
+
if (path.startsWith("./")) return path.slice(2);
|
|
37
|
+
return path;
|
|
38
|
+
}
|
|
39
|
+
function parseRemotePath(remotePath) {
|
|
40
|
+
const [remoteName, ...rest] = remotePath.split("/");
|
|
41
|
+
if (remoteName && rest.length > 0) return {
|
|
42
|
+
remoteName,
|
|
43
|
+
modulePath: rest.join("/"),
|
|
44
|
+
fullRequest: remotePath
|
|
45
|
+
};
|
|
46
|
+
throw new Error(`Invalid remote request: ${remotePath}`);
|
|
47
|
+
}
|
|
48
|
+
function importRemoteModule(remoteRequestPath) {
|
|
49
|
+
const { remoteName, modulePath } = parseRemotePath(remoteRequestPath);
|
|
50
|
+
const container = getContainer(remoteName);
|
|
51
|
+
if (container == null) throw new Error(`${remoteName} container not found`);
|
|
52
|
+
const module = container.exposeMap[normalizePath(modulePath)];
|
|
53
|
+
if (module == null) throw new Error(`Could not resolve '${modulePath}' in ${remoteName} container`);
|
|
54
|
+
return module;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/runtime/exposeModule.ts
|
|
59
|
+
function exposeModule(container, exposeName, module) {
|
|
60
|
+
const normalizedExposeName = normalizePath(exposeName);
|
|
61
|
+
if (container.exposeMap[normalizedExposeName]) throw new Error(`'${exposeName}' is already exposed in ${container.name} container`);
|
|
62
|
+
Object.defineProperty(container.exposeMap, normalizedExposeName, {
|
|
63
|
+
get: () => Object.assign(module, { __esModule: true }),
|
|
64
|
+
enumerable: true
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
export { createContainer, exposeModule, getContainer, importRemoteModule, parseRemotePath, registerShared };
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@granite-js/plugin-micro-frontend",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0-dev-20250725013859",
|
|
5
|
+
"description": "Plugin for micro frontend",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"prepack": "yarn build",
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"test": "vitest --run",
|
|
10
|
+
"build": "tsdown"
|
|
11
|
+
},
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"require": "./dist/index.cjs"
|
|
18
|
+
},
|
|
19
|
+
"./runtime": {
|
|
20
|
+
"types": "./dist/runtime/index.d.ts",
|
|
21
|
+
"import": "./dist/runtime/index.js",
|
|
22
|
+
"require": "./dist/runtime/index.cjs"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"tsdown": "^0.11.11",
|
|
31
|
+
"typescript": "^5.8.3",
|
|
32
|
+
"vitest": "^3.2.4"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@granite-js/plugin-core": "workspace:*",
|
|
36
|
+
"@granite-js/utils": "workspace:*",
|
|
37
|
+
"es-toolkit": "^1.38.0",
|
|
38
|
+
"picocolors": "^1.1.1"
|
|
39
|
+
},
|
|
40
|
+
"sideEffects": false
|
|
41
|
+
}
|