@module-federation/webpack-bundler-runtime 0.0.0-next-20231220075455 → 0.0.0-next-20231220081137

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/project.json DELETED
@@ -1,73 +0,0 @@
1
- {
2
- "name": "webpack-bundler-runtime",
3
- "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
- "sourceRoot": "packages/webpack-bundler-runtime/src",
5
- "projectType": "library",
6
- "targets": {
7
- "build": {
8
- "executor": "@nx/rollup:rollup",
9
- "outputs": ["{options.outputPath}"],
10
- "options": {
11
- "parallel": false,
12
- "outputPath": "packages/webpack-bundler-runtime/dist",
13
- "main": "packages/webpack-bundler-runtime/src/index.ts",
14
- "tsConfig": "packages/webpack-bundler-runtime/tsconfig.lib.json",
15
- "assets": [],
16
- "project": "packages/webpack-bundler-runtime/package.json",
17
- "compiler": "swc",
18
- "format": ["cjs", "esm"],
19
- "external": ["@module-federation/*"],
20
- "buildableProjectDepsInPackageJsonType": "dependencies",
21
- "updateBuildableProjectDepsInPackageJson": true,
22
- "rollupConfig": "packages/webpack-bundler-runtime/rollup.config.js"
23
- },
24
- "dependsOn": [
25
- {
26
- "target": "build",
27
- "dependencies": true
28
- }
29
- ]
30
- },
31
- "lint": {
32
- "executor": "@nx/linter:eslint",
33
- "outputs": ["{options.outputFile}"],
34
- "options": {
35
- "lintFilePatterns": [
36
- "packages/webpack-bundler-runtime/**/*.ts",
37
- "packages/webpack-bundler-runtime/package.json"
38
- ]
39
- }
40
- },
41
- "test": {
42
- "executor": "@nx/jest:jest",
43
- "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
44
- "options": {
45
- "jestConfig": "packages/webpack-bundler-runtime/jest.config.js",
46
- "passWithNoTests": true
47
- },
48
- "configurations": {
49
- "ci": {
50
- "ci": true,
51
- "codeCoverage": true
52
- }
53
- }
54
- },
55
- "pre-release": {
56
- "executor": "nx:run-commands",
57
- "options": {
58
- "parallel": false,
59
- "commands": [
60
- {
61
- "command": "nx run webpack-bundler-runtime:test",
62
- "forwardAllArgs": false
63
- },
64
- {
65
- "command": "nx run webpack-bundler-runtime:build",
66
- "forwardAllArgs": false
67
- }
68
- ]
69
- }
70
- }
71
- },
72
- "tags": ["package"]
73
- }
package/rollup.config.js DELETED
@@ -1,16 +0,0 @@
1
- const copy = require('rollup-plugin-copy');
2
-
3
- module.exports = (rollupConfig, projectOptions) => {
4
- rollupConfig.plugins.push(
5
- copy({
6
- targets: [
7
- {
8
- src: 'packages/webpack-bundler-runtime/LICENSE',
9
- dest: 'dist/packages/webpack-bundler-runtime',
10
- },
11
- ],
12
- }),
13
- );
14
-
15
- return rollupConfig;
16
- };
package/src/consumes.ts DELETED
@@ -1,62 +0,0 @@
1
- import { ConsumesOptions } from './types';
2
- import { proxyShareScopeMap } from './proxyShareScopeMap';
3
-
4
- export function consumes(options: ConsumesOptions) {
5
- const {
6
- chunkId,
7
- promises,
8
- chunkMapping,
9
- installedModules,
10
- moduleToHandlerMapping,
11
- webpackRequire,
12
- } = options;
13
- proxyShareScopeMap(webpackRequire);
14
- if (webpackRequire.o(chunkMapping, chunkId)) {
15
- chunkMapping[chunkId].forEach((id) => {
16
- if (webpackRequire.o(installedModules, id)) {
17
- return promises.push(installedModules[id] as Promise<any>);
18
- }
19
- const onFactory = (factory: () => any) => {
20
- installedModules[id] = 0;
21
- webpackRequire.m[id] = (module) => {
22
- delete webpackRequire.c[id];
23
- module.exports = factory();
24
- };
25
- };
26
- const onError = (error: unknown) => {
27
- delete installedModules[id];
28
- webpackRequire.m[id] = (module) => {
29
- delete webpackRequire.c[id];
30
- throw error;
31
- };
32
- };
33
- try {
34
- const federationInstance = webpackRequire.federation.instance;
35
- if (!federationInstance) {
36
- throw new Error('Federation instance not found!');
37
- }
38
- const { shareKey, getter, shareInfo } = moduleToHandlerMapping[id];
39
-
40
- const promise = federationInstance
41
- .loadShare(shareKey, shareInfo)
42
- .then((factory: any) => {
43
- if (factory === false) {
44
- return getter();
45
- }
46
- return factory;
47
- });
48
-
49
- if (promise.then) {
50
- promises.push(
51
- (installedModules[id] = promise.then(onFactory).catch(onError)),
52
- );
53
- } else {
54
- // @ts-ignore maintain previous logic
55
- onFactory(promise);
56
- }
57
- } catch (e) {
58
- onError(e);
59
- }
60
- });
61
- }
62
- }
package/src/index.ts DELETED
@@ -1,20 +0,0 @@
1
- import * as runtime from '@module-federation/runtime';
2
- import { Federation } from './types';
3
- import { remotes } from './remotes';
4
- import { consumes } from './consumes';
5
- import { initializeSharing } from './initializeSharing';
6
- import { installInitialConsumes } from './installInitialConsumes';
7
-
8
- const federation: Federation = {
9
- runtime,
10
- instance: undefined,
11
- initOptions: undefined,
12
- bundlerRuntime: {
13
- remotes,
14
- consumes,
15
- I: initializeSharing,
16
- S: {},
17
- installInitialConsumes,
18
- },
19
- };
20
- export default federation;
@@ -1,8 +0,0 @@
1
- import { WebpackRequire } from './types';
2
-
3
- export function initializeSharing(
4
- shareScopeName: string,
5
- webpackRequire: WebpackRequire,
6
- ): Promise<boolean> | boolean {
7
- return webpackRequire.federation.instance!.initializeSharing(shareScopeName);
8
- }
@@ -1,48 +0,0 @@
1
- import {
2
- HandleInitialConsumesOptions,
3
- InstallInitialConsumesOptions,
4
- } from './types';
5
- function handleInitialConsumes(options: HandleInitialConsumesOptions) {
6
- const { moduleId, moduleToHandlerMapping, webpackRequire } = options;
7
-
8
- const federationInstance = webpackRequire.federation.instance;
9
- if (!federationInstance) {
10
- throw new Error('Federation instance not found!');
11
- }
12
- const { shareKey, shareInfo } = moduleToHandlerMapping[moduleId];
13
-
14
- if (!shareInfo.shareConfig.eager) {
15
- throw new Error(
16
- `Shared: "${shareKey}" cannot be loaded synchronously unless "eager:true" is set or async entry is enabled.`,
17
- );
18
- }
19
- return federationInstance.loadShareSync(shareKey);
20
- }
21
-
22
- export function installInitialConsumes(options: InstallInitialConsumesOptions) {
23
- const {
24
- moduleToHandlerMapping,
25
- webpackRequire,
26
- installedModules,
27
- initialConsumes,
28
- } = options;
29
-
30
- initialConsumes.forEach((id) => {
31
- webpackRequire.m[id] = (module) => {
32
- // Handle scenario when module is used synchronously
33
- installedModules[id] = 0;
34
- delete webpackRequire.c[id];
35
- const factory = handleInitialConsumes({
36
- moduleId: id,
37
- moduleToHandlerMapping,
38
- webpackRequire,
39
- });
40
- if (typeof factory !== 'function') {
41
- throw new Error(
42
- `Shared module is not available for eager consumption: ${id}`,
43
- );
44
- }
45
- module.exports = factory();
46
- };
47
- });
48
- }
@@ -1,17 +0,0 @@
1
- import { WebpackRequire } from './types';
2
-
3
- export function proxyShareScopeMap(__webpack_require__: WebpackRequire) {
4
- if (!__webpack_require__.S) {
5
- return;
6
- }
7
- // @ts-ignore FIXME: ideal situation is import type from @module-federation/runtime/type ,but the compile will throw error
8
- __webpack_require__.S = new Proxy(globalThis.__VMOK__.__SHARE__, {
9
- get(target, prop: string, receiver) {
10
- return globalThis.__VMOK__.__SHARE__[prop];
11
- },
12
- set(target, prop: string, value) {
13
- globalThis.__VMOK__.__SHARE__[prop] = value;
14
- return true;
15
- },
16
- });
17
- }
package/src/remotes.ts DELETED
@@ -1,113 +0,0 @@
1
- import type { RemoteEntryExports } from './types';
2
- import { RemotesOptions } from './types';
3
-
4
- export function remotes(options: RemotesOptions) {
5
- const {
6
- chunkId,
7
- promises,
8
- chunkMapping,
9
- idToExternalAndNameMapping,
10
- webpackRequire,
11
- } = options;
12
-
13
- if (webpackRequire.o(chunkMapping, chunkId)) {
14
- chunkMapping[chunkId].forEach((id) => {
15
- let getScope = webpackRequire.R;
16
- if (!getScope) {
17
- getScope = [];
18
- }
19
- const data = idToExternalAndNameMapping[id];
20
- // @ts-ignore seems not work
21
- if (getScope.indexOf(data) >= 0) {
22
- return;
23
- }
24
- // @ts-ignore seems not work
25
- getScope.push(data);
26
- if (data.p) {
27
- return promises.push(data.p as Promise<any>);
28
- }
29
- const onError = (error?: Error) => {
30
- if (!error) {
31
- error = new Error('Container missing');
32
- }
33
- if (typeof error.message === 'string') {
34
- error.message += `\nwhile loading "${data[1]}" from ${data[2]}`;
35
- }
36
- webpackRequire.m[id] = () => {
37
- throw error;
38
- };
39
- data.p = 0;
40
- };
41
- const handleFunction = (
42
- fn: (...args: any[]) => any,
43
- arg1: any,
44
- arg2: any,
45
- d: any,
46
- next: (...args: any[]) => any,
47
- first: 1 | 0,
48
- ) => {
49
- try {
50
- const promise = fn(arg1, arg2);
51
- if (promise && promise.then) {
52
- const p = promise.then((result: any) => next(result, d), onError);
53
- if (first) {
54
- promises.push((data.p = p));
55
- } else {
56
- return p;
57
- }
58
- } else {
59
- return next(promise, d, first);
60
- }
61
- } catch (error) {
62
- onError(error as Error);
63
- }
64
- };
65
- const onExternal = (
66
- external: (...args: any[]) => any,
67
- _: any,
68
- first: 1 | 0,
69
- ) =>
70
- external
71
- ? handleFunction(
72
- webpackRequire.I,
73
- data[0],
74
- 0,
75
- external,
76
- onInitialized,
77
- first,
78
- )
79
- : onError();
80
-
81
- // eslint-disable-next-line no-var
82
- var onInitialized = (
83
- _: any,
84
- external: RemoteEntryExports,
85
- first: 1 | 0,
86
- ) => handleFunction(external.get, data[1], getScope, 0, onFactory, first);
87
- const useRuntimeLoad = ['script'].includes(data[3]) && data[4];
88
- // eslint-disable-next-line no-var
89
- var onFactory = (factory: () => any) => {
90
- data.p = 1;
91
- webpackRequire.m[id] = (module) => {
92
- module.exports = factory();
93
- };
94
- };
95
- const onRemoteLoaded = () => {
96
- try {
97
- const remoteModuleName = data[4] + data[1].slice(1);
98
- return webpackRequire.federation.instance!.loadRemote(
99
- remoteModuleName,
100
- { loadFactory: false },
101
- );
102
- } catch (error) {
103
- onError(error as Error);
104
- }
105
- };
106
- if (useRuntimeLoad) {
107
- handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1);
108
- } else {
109
- handleFunction(webpackRequire, data[2], 0, 0, onExternal, 1);
110
- }
111
- });
112
- }
113
- }
package/src/types.ts DELETED
@@ -1,115 +0,0 @@
1
- import * as runtime from '@module-federation/runtime';
2
- import { initializeSharing } from './initializeSharing';
3
-
4
- // FIXME: ideal situation => import { GlobalShareScope,UserOptions } from '@module-federation/runtime/type'
5
- type ExcludeUndefined<T> = T extends undefined ? never : T;
6
- type NonUndefined<T = Shared> = ExcludeUndefined<T>;
7
-
8
- type InitOptions = Parameters<typeof runtime.init>[0];
9
- type Shared = InitOptions['shared'];
10
-
11
- type SharedConfig = NonUndefined<NonUndefined[string]['shareConfig']>;
12
-
13
- type ModuleCache = runtime.FederationHost['moduleCache'];
14
- type InferModule<T> = T extends Map<string, infer U> ? U : never;
15
- type InferredModule = InferModule<ModuleCache>;
16
- export type RemoteEntryExports = NonUndefined<
17
- InferredModule['remoteEntryExports']
18
- >;
19
-
20
- type ExtractInitParameters<T> = T extends {
21
- init: (shareScope: infer U, ...args: any[]) => void;
22
- }
23
- ? U
24
- : never;
25
-
26
- type InferredShareScope = ExtractInitParameters<RemoteEntryExports>;
27
- type InferredGlobalShareScope = {
28
- [scope: string]: InferredShareScope;
29
- };
30
-
31
- // shareScope, name, externalModuleId, externalType, remoteName
32
- type IdToExternalAndNameMappingItem = [
33
- string,
34
- string,
35
- string | number,
36
- string,
37
- string,
38
- ];
39
-
40
- interface IdToExternalAndNameMappingItemWithPromise
41
- extends IdToExternalAndNameMappingItem {
42
- p?: Promise<any> | number;
43
- }
44
-
45
- export interface WebpackRequire {
46
- (moduleId: string | number): any;
47
- o: (obj: Record<string, any>, key: string | number) => boolean;
48
- R: Array<string | number>;
49
- m: Record<string, (mod: any) => any>;
50
- c: Record<string, any>;
51
- I: typeof initializeSharing;
52
- S?: InferredGlobalShareScope;
53
- federation: Federation;
54
- }
55
-
56
- interface ShareInfo {
57
- shareConfig: SharedConfig;
58
- scope: Array<string>;
59
- }
60
-
61
- interface ModuleToHandlerMappingItem {
62
- // handler: (shareInfo: ShareInfo, type?: 'syn' | 'async') => Promise<any>;
63
- getter: () => Promise<any>;
64
- shareInfo: ShareInfo;
65
- shareKey: string;
66
- }
67
-
68
- export interface RemotesOptions {
69
- chunkId: string | number;
70
- promises: Promise<any>[];
71
- chunkMapping: Record<string, Array<string | number>>;
72
- idToExternalAndNameMapping: Record<
73
- string,
74
- IdToExternalAndNameMappingItemWithPromise
75
- >;
76
- webpackRequire: WebpackRequire;
77
- }
78
-
79
- export interface HandleInitialConsumesOptions {
80
- moduleId: string | number;
81
- moduleToHandlerMapping: Record<string, ModuleToHandlerMappingItem>;
82
- webpackRequire: WebpackRequire;
83
- }
84
- export interface InstallInitialConsumesOptions {
85
- moduleToHandlerMapping: Record<string, ModuleToHandlerMappingItem>;
86
- webpackRequire: WebpackRequire;
87
- installedModules: Record<string, Promise<any> | 0>;
88
- initialConsumes: Array<string | number>;
89
- }
90
-
91
- export interface ConsumesOptions {
92
- chunkId: string | number;
93
- promises: Promise<any>[];
94
- chunkMapping: Record<string, Array<string | number>>;
95
- installedModules: Record<string, Promise<any> | 0>;
96
- moduleToHandlerMapping: Record<string, ModuleToHandlerMappingItem>;
97
- webpackRequire: WebpackRequire;
98
- }
99
-
100
- export interface Federation {
101
- runtime?: typeof runtime;
102
- instance?: runtime.FederationHost;
103
- initOptions?: InitOptions;
104
- installInitialConsumes?: (options: InstallInitialConsumesOptions) => any;
105
- bundlerRuntime?: {
106
- remotes: (options: RemotesOptions) => void;
107
- consumes: (options: ConsumesOptions) => void;
108
- I: (
109
- name: string,
110
- webpackRequire: WebpackRequire,
111
- ) => Promise<boolean> | boolean;
112
- S: InferredGlobalShareScope;
113
- installInitialConsumes: (options: InstallInitialConsumesOptions) => any;
114
- };
115
- }
package/tsconfig.json DELETED
@@ -1,28 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "module": "commonjs",
5
- "forceConsistentCasingInFileNames": true,
6
- "strict": true,
7
- "noImplicitOverride": true,
8
- "noPropertyAccessFromIndexSignature": true,
9
- "noFallthroughCasesInSwitch": true,
10
- "moduleResolution": "node",
11
- "resolveJsonModule": true,
12
- "esModuleInterop": true,
13
- "skipLibCheck": true,
14
- "skipDefaultLibCheck": true,
15
- "declaration": true,
16
- "noImplicitReturns": false
17
- },
18
- "files": [],
19
- "include": [],
20
- "references": [
21
- {
22
- "path": "./tsconfig.lib.json"
23
- },
24
- {
25
- "path": "./tsconfig.spec.json"
26
- }
27
- ]
28
- }
package/tsconfig.lib.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "../../dist/out-tsc",
5
- "declaration": true,
6
- "types": ["node"]
7
- },
8
- "include": ["src/**/*.ts"],
9
- "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
10
- }
@@ -1,14 +0,0 @@
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
- }