@module-federation/runtime-tools 0.0.0-next-20231221061836

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present zhouxiao(zhoushaw)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,301 @@
1
+ # `@module-federation/runtime`
2
+
3
+ - Can be combined with the build plug-in to share basic dependencies according to policies to reduce the number of module downloads and improve the loading speed of modules.
4
+ - Only consume part of the export of the remote module and will not fully download the remote module
5
+ - The runtime calling process can be extended through the module-runtime plug-in mechanism
6
+
7
+ ## Usage
8
+
9
+ ```javascript
10
+ // Can load modules using only the runtime SDK without relying on build plugins
11
+ // When not using build plugins, shared dependencies cannot be automatically reused
12
+ import { init, loadRemote } from '@module-federation/runtime';
13
+
14
+ init({
15
+ name: '@demo/app-main',
16
+ remotes: [
17
+ {
18
+ name: "@demo/app2",
19
+ entry: "http://localhost:3006/remoteEntry.js"
20
+ },
21
+ {
22
+ name: "@demo/app3",
23
+ alias: "app3",
24
+ entry: "http://localhost:2001/module-federation-manifest.json"
25
+ },
26
+ ],
27
+ });
28
+
29
+ // Load by alias
30
+ loadRemote<{add: (...args: Array<number>)=> number }>("app3/util").then((md)=>{
31
+ md.add(1,2,3);
32
+ });
33
+ ```
34
+
35
+ ### init
36
+
37
+ - Type: `init(options: InitOptions): void`
38
+ - It is used to dynamically register the module at runtime
39
+ - InitOptions:
40
+
41
+ ```ts
42
+ type InitOptions {
43
+ name: string;
44
+ version?: string;
45
+ shared?: ShareInfos;
46
+ };
47
+
48
+ type RemoteInfo = (RemotesWithEntry | RemotesWithVersion) & {
49
+ alias?: string;
50
+ };
51
+
52
+ interface RemotesWithVersion {
53
+ name: string;
54
+ version: string;
55
+ }
56
+
57
+ interface RemotesWithEntry {
58
+ name: string;
59
+ entry: string;
60
+ }
61
+
62
+ type ShareInfos = {
63
+ // Package name and dependency basic information and sharing strategy
64
+ [pkgName: string]: Share;
65
+ };
66
+
67
+ type Share = {
68
+ // Versions of shared dependencies
69
+ version: string;
70
+ // Which modules consume the current shared dependencies?
71
+ useIn?: Array<string>;
72
+ // Which module does the shared dependency come from?
73
+ from?: string;
74
+ // Get the factory function of the shared dependency instance. When the cached shared instance cannot be loaded, its own shared dependency will be loaded.
75
+ lib: () => Module;
76
+ // Sharing strategy, what strategy will be used to determine the reuse of shared dependencies
77
+ shareConfig?: SharedConfig;
78
+ // Dependencies between shares
79
+ deps?: Array<string>;
80
+ // The scope under which the current shared dependencies are placed, the default is default
81
+ scope?: string | Array<string>;
82
+ };
83
+ ```
84
+
85
+ - Example
86
+
87
+ ```js
88
+ import { init, loadRemote } from '@module-federation/runtime';
89
+
90
+ init({
91
+ name: '@demo/main-app',
92
+ remotes: [
93
+ {
94
+ name: '@demo/app2',
95
+ entry: 'http://localhost:3006/remoteEntry.js',
96
+ },
97
+ {
98
+ name: '@demo/app3',
99
+ alias: 'app3',
100
+ entry: 'http://localhost:2001/module-federation-manifest.json',
101
+ },
102
+ ],
103
+ });
104
+ ```
105
+
106
+ ### loadRemote
107
+
108
+ - Type: `loadRemote(id: string)`
109
+ - Used to load initialized remote modules, when used with build plugins, it can be loaded directly through native `import("remoteName/expose")` syntax, and the build plugin will automatically convert it into `loadRemote` usage.
110
+
111
+ - Example
112
+
113
+ ```javascript
114
+ import { init, loadRemote } from '@module-federation/runtime';
115
+
116
+ init({
117
+ name: '@demo/main-app',
118
+ remotes: [
119
+ {
120
+ name: '@demo/app3',
121
+ alias: 'app3',
122
+ entry: 'http://localhost:2001/module-federation-manifest.json',
123
+ },
124
+ ],
125
+ });
126
+
127
+ // remoteName + expose
128
+ loadRemote('@demo/app3/util').then((m) => m.add(1, 2, 3));
129
+
130
+ // alias + expose
131
+ loadRemote('app3/util').then((m) => m.add(1, 2, 3));
132
+ ```
133
+
134
+ ### loadShare
135
+
136
+ - Type: `loadShare(pkgName: string)`
137
+ - Gets the `share` dependency. When there are `share` dependencies that match the current `host` in the global environment, the existing and satisfying `share` dependencies will be reused first. Otherwise, load its own dependencies and store them in the global cache.
138
+ - This `API` is generally not called directly by users, but is used by build plugins to convert their own dependencies.
139
+
140
+ - Example
141
+
142
+ ```js
143
+ import { init, loadRemote, loadShare } from '@module-federation/runtime';
144
+ import React from 'react';
145
+ import ReactDOM from 'react-dom';
146
+
147
+ init({
148
+ name: '@demo/main-app',
149
+ remotes: [],
150
+ shared: {
151
+ react: {
152
+ version: '17.0.0',
153
+ scope: 'default',
154
+ lib: () => React,
155
+ shareConfig: {
156
+ singleton: true,
157
+ requiredVersion: '^17.0.0',
158
+ },
159
+ },
160
+ 'react-dom': {
161
+ version: '17.0.0',
162
+ scope: 'default',
163
+ lib: () => ReactDOM,
164
+ shareConfig: {
165
+ singleton: true,
166
+ requiredVersion: '^17.0.0',
167
+ },
168
+ },
169
+ },
170
+ });
171
+
172
+ loadShare('react').then((reactFactory) => {
173
+ console.log(reactFactory());
174
+ });
175
+ ```
176
+
177
+ ### usePlugin
178
+
179
+ Used to extend the internal loading process of `ModuleFederation`, affecting the entire loading process through hook triggers and return values.
180
+
181
+ - Example
182
+
183
+ ```ts
184
+ import { init } from '@module-federation/runtime';
185
+
186
+ // mock get remote data remotes config
187
+ function getDataConfig() {
188
+ return new Promise((resolve) => {
189
+ setTimeout(() => {
190
+ resolve({
191
+ remotes: [
192
+ {
193
+ name: '@demo/sub',
194
+ alias: 'sub',
195
+ entry: 'http://localhost:2001/module-federation-manifest.json',
196
+ },
197
+ ],
198
+ });
199
+ }, 2000);
200
+ });
201
+ }
202
+
203
+ function RemotesDataPlugin() {
204
+ return {
205
+ name: 'data-config',
206
+ async beforeRequest(args) {
207
+ const remotes = await getDataConfig();
208
+ origin.initOptions({
209
+ remotes,
210
+ });
211
+ return args;
212
+ },
213
+ };
214
+ }
215
+
216
+ init({
217
+ name: '@demo/micro-app',
218
+ remotes: [],
219
+ pluigns: [RemotesDataPlugin()],
220
+ });
221
+
222
+ loadRemote('sub/utils').then((m) => {
223
+ m.add(1, 2, 3, 4);
224
+ });
225
+ ```
226
+
227
+ - Type
228
+
229
+ ```typescript
230
+ type Plugin = {
231
+ name: string;
232
+ core: {
233
+ beforeRequest: AsyncWaterfallHook<{
234
+ id: string;
235
+ options: Options;
236
+ origin: VmokHost;
237
+ }>;
238
+ };
239
+ snapshot: {};
240
+ };
241
+ type usePlugin = (plugin: Plugin) => void;
242
+ ```
243
+
244
+ ### preloadRemote
245
+
246
+ - Type
247
+
248
+ ```typescript
249
+ async function preloadRemote(preloadOptions: Array<PreloadRemoteArgs>) {}
250
+
251
+ type depsPreloadArg = Omit<PreloadRemoteArgs, 'depsRemote'>;
252
+ type PreloadRemoteArgs = {
253
+ // Preload the name and alias of the remote
254
+ nameOrAlias: string;
255
+ // Expose to be preloaded
256
+ // Default to preload all exposes
257
+ // Only the required expose will be loaded when an expose is provided
258
+ exposes?: Array<string>; // Default request
259
+ // Default is sync, only load synchronous code referenced in the expose
260
+ // Set to all to load synchronous and asynchronous references
261
+ resourceCategory?: 'all' | 'sync';
262
+ // Load all dependencies when no value is configured
263
+ // Only the resources needed will be loaded after configuring the dependency
264
+ depsRemote?: boolean | Array<depsPreloadArg>;
265
+ // No filtering of resources when not configured
266
+ // Will filter out unwanted resources after configuring
267
+ filter?: (assetUrl: string) => boolean;
268
+ };
269
+ ```
270
+
271
+ - Details
272
+
273
+ Through `preloadRemote`, module resources can be preloaded at an earlier stage to avoid waterfall requests. `preloadRemote` can preload the following content:
274
+
275
+ - The `remoteEntry` of `remote`
276
+ - `expose` of `remote`
277
+ - Synchronous resources or asynchronous resources of `remote`
278
+ - `remote` resources that `remote` depends on
279
+
280
+ * Example
281
+
282
+ ```ts
283
+ import { init, preloadRemote } from '@module-federation/runtime';
284
+ init({
285
+ name: '@demo/preload-remote',
286
+ remotes: [
287
+ {
288
+ name: '@demo/sub1',
289
+ entry: 'http://localhost:2001/vmok-manifest.json',
290
+ },
291
+ {
292
+ name: '@demo/sub2',
293
+ entry: 'http://localhost:2001/vmok-manifest.json',
294
+ },
295
+ {
296
+ name: '@demo/sub3',
297
+ entry: 'http://localhost:2001/vmok-manifest.json',
298
+ },
299
+ ],
300
+ });
301
+ ```
@@ -0,0 +1 @@
1
+ export * from "./src/index";
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var runtime = require('@module-federation/runtime');
6
+
7
+
8
+
9
+ Object.keys(runtime).forEach(function (k) {
10
+ if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
11
+ enumerable: true,
12
+ get: function () { return runtime[k]; }
13
+ });
14
+ });
@@ -0,0 +1 @@
1
+ export * from '@module-federation/runtime';
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@module-federation/runtime-tools",
3
+ "version": "0.0.4",
4
+ "author": "zhanghang <hanric.zhang@gmail.com>",
5
+ "main": "./index.cjs.js",
6
+ "module": "./index.esm.js",
7
+ "types": "./dist/index.cjs.d.ts",
8
+ "license": "MIT",
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/index.esm.js",
15
+ "require": "./dist/index.cjs.js"
16
+ },
17
+ "./runtime": {
18
+ "import": "./dist/runtime.esm.js",
19
+ "require": "./dist/runtime.cjs.js"
20
+ },
21
+ "./webpack-bundler-runtime": {
22
+ "import": "./dist/webpack-bundler-runtime.esm.js",
23
+ "require": "./dist/webpack-bundler-runtime.cjs.js"
24
+ },
25
+ "./*": "./*"
26
+ },
27
+ "typesVersions": {
28
+ "*": {
29
+ ".": [
30
+ "./dist/index.cjs.d.ts"
31
+ ],
32
+ "runtime": [
33
+ "./dist/runtime.cjs.d.ts"
34
+ ],
35
+ "webpack-bundler-runtime": [
36
+ "./dist/webpack-bundler-runtime.cjs.d.ts"
37
+ ]
38
+ }
39
+ },
40
+ "dependencies": {
41
+ "@module-federation/runtime": "workspace:*",
42
+ "@module-federation/webpack-bundler-runtime": "workspace:*"
43
+ }
44
+ }
@@ -0,0 +1 @@
1
+ export * from "./src/runtime";
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var runtime = require('@module-federation/runtime');
6
+
7
+
8
+
9
+ Object.keys(runtime).forEach(function (k) {
10
+ if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
11
+ enumerable: true,
12
+ get: function () { return runtime[k]; }
13
+ });
14
+ });
@@ -0,0 +1 @@
1
+ export * from '@module-federation/runtime';
@@ -0,0 +1 @@
1
+ export * from '@module-federation/runtime';
@@ -0,0 +1 @@
1
+ export * from '@module-federation/runtime';
@@ -0,0 +1 @@
1
+ export * from '@module-federation/webpack-bundler-runtime';
@@ -0,0 +1 @@
1
+ export * from "./src/webpack-bundler-runtime";
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var webpackBundlerRuntime = require('@module-federation/webpack-bundler-runtime');
6
+
7
+
8
+
9
+ Object.keys(webpackBundlerRuntime).forEach(function (k) {
10
+ if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
11
+ enumerable: true,
12
+ get: function () { return webpackBundlerRuntime[k]; }
13
+ });
14
+ });
@@ -0,0 +1 @@
1
+ export * from '@module-federation/webpack-bundler-runtime';
package/jest.config.ts ADDED
@@ -0,0 +1,30 @@
1
+ /* eslint-disable */
2
+ import { readFileSync } from 'fs';
3
+
4
+ // Reading the SWC compilation config and remove the "exclude"
5
+ // for the test files to be compiled by SWC
6
+ const { exclude: _, ...swcJestConfig } = JSON.parse(
7
+ readFileSync(`${__dirname}/.swcrc`, 'utf-8'),
8
+ );
9
+
10
+ // disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves.
11
+ // If we do not disable this, SWC Core will read .swcrc and won't transform our test files due to "exclude"
12
+ if (swcJestConfig.swcrc === undefined) {
13
+ swcJestConfig.swcrc = false;
14
+ }
15
+
16
+ // Uncomment if using global setup/teardown files being transformed via swc
17
+ // https://nx.dev/packages/jest/documents/overview#global-setup/teardown-with-nx-libraries
18
+ // jest needs EsModule Interop to find the default exported setup/teardown functions
19
+ // swcJestConfig.module.noInterop = false;
20
+
21
+ export default {
22
+ displayName: 'runtime',
23
+ preset: '../../jest.preset.js',
24
+ transform: {
25
+ '^.+\\.[tj]s$': ['@swc/jest', swcJestConfig],
26
+ },
27
+ moduleFileExtensions: ['ts', 'js', 'html'],
28
+ testEnvironment: 'node',
29
+ coverageDirectory: '../../coverage/packages/runtime',
30
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@module-federation/runtime-tools",
3
+ "version": "0.0.0-next-20231221061836",
4
+ "author": "zhanghang <hanric.zhang@gmail.com>",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.esm.js",
7
+ "types": "./dist/index.cjs.d.ts",
8
+ "license": "MIT",
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/index.esm.js",
15
+ "require": "./dist/index.cjs.js"
16
+ },
17
+ "./runtime": {
18
+ "import": "./dist/runtime.esm.js",
19
+ "require": "./dist/runtime.cjs.js"
20
+ },
21
+ "./webpack-bundler-runtime": {
22
+ "import": "./dist/webpack-bundler-runtime.esm.js",
23
+ "require": "./dist/webpack-bundler-runtime.cjs.js"
24
+ },
25
+ "./*": "./*"
26
+ },
27
+ "typesVersions": {
28
+ "*": {
29
+ ".": [
30
+ "./dist/index.cjs.d.ts"
31
+ ],
32
+ "runtime": [
33
+ "./dist/runtime.cjs.d.ts"
34
+ ],
35
+ "webpack-bundler-runtime": [
36
+ "./dist/webpack-bundler-runtime.cjs.d.ts"
37
+ ]
38
+ }
39
+ },
40
+ "dependencies": {
41
+ "@module-federation/runtime": "0.0.0-next-20231221061836",
42
+ "@module-federation/webpack-bundler-runtime": "0.0.0-next-20231221061836"
43
+ }
44
+ }
package/project.json ADDED
@@ -0,0 +1,83 @@
1
+ {
2
+ "name": "runtime-tools",
3
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "packages/runtime-tools/src",
5
+ "projectType": "library",
6
+ "targets": {
7
+ "build": {
8
+ "executor": "@nx/rollup:rollup",
9
+ "outputs": ["{workspaceRoot}/packages/runtime-tools/dist"],
10
+ "options": {
11
+ "parallel": false,
12
+ "outputPath": "packages/runtime-tools/dist",
13
+ "main": "packages/runtime-tools/src/index.ts",
14
+ "additionalEntryPoints": [
15
+ "packages/runtime-tools/src/runtime.ts",
16
+ "packages/runtime-tools/src/webpack-bundler-runtime.ts"
17
+ ],
18
+ "tsConfig": "packages/runtime-tools/tsconfig.lib.json",
19
+ "assets": [],
20
+ "project": "packages/runtime-tools/package.json",
21
+ "compiler": "swc",
22
+ "rollupConfig": "packages/runtime-tools/rollup.config.js",
23
+ "format": ["cjs", "esm"],
24
+ "external": ["@module-federation/*"],
25
+ "buildableProjectDepsInPackageJsonType": "dependencies",
26
+ "updateBuildableProjectDepsInPackageJson": true
27
+ },
28
+ "dependsOn": [
29
+ {
30
+ "target": "build",
31
+ "dependencies": true
32
+ }
33
+ ]
34
+ },
35
+ "lint": {
36
+ "executor": "@nx/linter:eslint",
37
+ "outputs": ["{options.outputFile}"],
38
+ "options": {
39
+ "lintFilePatterns": [
40
+ "packages/runtime-tools/**/*.ts",
41
+ "packages/runtime-tools/package.json"
42
+ ]
43
+ }
44
+ },
45
+ "release": {
46
+ "executor": "nx:run-commands",
47
+ "options": {
48
+ "parallel": false,
49
+ "commands": [
50
+ {
51
+ "command": "nx run runtime:test",
52
+ "forwardAllArgs": false
53
+ },
54
+ {
55
+ "command": "nx run runtime:build",
56
+ "forwardAllArgs": false
57
+ },
58
+ {
59
+ "command": "nx run runtime:semantic-release",
60
+ "forwardAllArgs": true
61
+ }
62
+ ]
63
+ }
64
+ },
65
+ "semantic-release": {
66
+ "executor": "@goestav/nx-semantic-release:semantic-release"
67
+ },
68
+ "test": {
69
+ "executor": "@nx/vite:test",
70
+ "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
71
+ "options": {
72
+ "config": "packages/runtime-tools/vitest.config.ts"
73
+ },
74
+ "configurations": {
75
+ "ci": {
76
+ "ci": true,
77
+ "codeCoverage": true
78
+ }
79
+ }
80
+ }
81
+ },
82
+ "tags": ["package"]
83
+ }
@@ -0,0 +1,10 @@
1
+ module.exports = (rollupConfig) => {
2
+ rollupConfig.input = {
3
+ index: 'packages/runtime-tools/src/index.ts',
4
+ runtime: 'packages/runtime-tools/src/runtime.ts',
5
+ 'webpack-bundler-runtime':
6
+ 'packages/runtime-tools/src/webpack-bundler-runtime.ts',
7
+ };
8
+
9
+ return rollupConfig;
10
+ };
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from '@module-federation/runtime';
package/src/runtime.ts ADDED
@@ -0,0 +1 @@
1
+ export * from '@module-federation/runtime';
@@ -0,0 +1 @@
1
+ export * from '@module-federation/webpack-bundler-runtime';
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "module": "commonjs",
5
+ "forceConsistentCasingInFileNames": true,
6
+ "strict": true,
7
+ "noImplicitOverride": true,
8
+ "noPropertyAccessFromIndexSignature": true,
9
+ "noImplicitReturns": true,
10
+ "noFallthroughCasesInSwitch": true
11
+ },
12
+ "files": [],
13
+ "include": [],
14
+ "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
15
+ "references": [
16
+ {
17
+ "path": "./tsconfig.lib.json"
18
+ },
19
+ {
20
+ "path": "./tsconfig.spec.json"
21
+ }
22
+ ]
23
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../dist/out-tsc",
5
+ "declaration": true,
6
+ "types": ["node"]
7
+ },
8
+ "include": ["src/**/*.ts", "global.d.ts"],
9
+ "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
10
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../dist/out-tsc",
5
+ "module": "commonjs",
6
+ "types": ["jest", "node"]
7
+ },
8
+ "include": [
9
+ "jest.config.ts",
10
+ "src/**/*.test.ts",
11
+ "src/**/*.spec.ts",
12
+ "src/**/*.d.ts"
13
+ ]
14
+ }