@module-federation/sdk 0.0.0-next-20231219090402 → 0.0.0-next-20231219093556
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 +2189 -2
- package/README.md +99 -0
- package/__tests__/decodeName.spec.ts +11 -0
- package/__tests__/encodeName.spec.ts +10 -0
- package/__tests__/generateSnapshotFromManifest.spec.ts +147 -0
- package/__tests__/parseEntry.spec.ts +36 -0
- package/__tests__/resources/constant.ts +6 -0
- package/__tests__/resources/getId.ts +5 -0
- package/__tests__/resources/manifestSnapshotMap.ts +1541 -0
- package/__tests__/simpleJoinRemoteEntry.spec.ts +53 -0
- package/dist/LICENSE +21 -0
- package/dist/package.json +40 -0
- package/jest.config.js +27 -0
- package/package.json +10 -11
- package/project.json +65 -0
- package/rollup.config.js +11 -0
- package/src/constant.ts +24 -0
- package/src/dom.ts +115 -0
- package/src/env.ts +25 -0
- package/src/generateSnapshotFromManifest.ts +176 -0
- package/src/index.ts +10 -0
- package/src/logger.ts +59 -0
- package/src/normalize-webpack-path.ts +33 -0
- package/src/types/common.ts +18 -0
- package/src/types/index.ts +4 -0
- package/src/types/manifest.ts +44 -0
- package/src/types/snapshot.ts +87 -0
- package/src/types/stats.ts +101 -0
- package/src/utils.ts +213 -0
- package/tsconfig.json +29 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +14 -0
- package/index.cjs.default.js +0 -1
- package/index.cjs.mjs +0 -2
- package/normalize-webpack-path.cjs.default.js +0 -1
- package/normalize-webpack-path.cjs.mjs +0 -2
- /package/{index.cjs.d.ts → dist/index.cjs.d.ts} +0 -0
- /package/{index.cjs.js → dist/index.cjs.js} +0 -0
- /package/{index.esm.js → dist/index.esm.js} +0 -0
- /package/{normalize-webpack-path.cjs.d.ts → dist/normalize-webpack-path.cjs.d.ts} +0 -0
- /package/{normalize-webpack-path.cjs.js → dist/normalize-webpack-path.cjs.js} +0 -0
- /package/{normalize-webpack-path.esm.js → dist/normalize-webpack-path.esm.js} +0 -0
- /package/{src → dist/src}/constant.d.ts +0 -0
- /package/{src → dist/src}/dom.d.ts +0 -0
- /package/{src → dist/src}/env.d.ts +0 -0
- /package/{src → dist/src}/generateSnapshotFromManifest.d.ts +0 -0
- /package/{src → dist/src}/index.d.ts +0 -0
- /package/{src → dist/src}/logger.d.ts +0 -0
- /package/{src → dist/src}/normalize-webpack-path.d.ts +0 -0
- /package/{src → dist/src}/types/common.d.ts +0 -0
- /package/{src → dist/src}/types/index.d.ts +0 -0
- /package/{src → dist/src}/types/manifest.d.ts +0 -0
- /package/{src → dist/src}/types/snapshot.d.ts +0 -0
- /package/{src → dist/src}/types/stats.d.ts +0 -0
- /package/{src → dist/src}/utils.d.ts +0 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { simpleJoinRemoteEntry } from '../src/generateSnapshotFromManifest';
|
|
2
|
+
|
|
3
|
+
describe('simpleJoinRemoteEntry', () => {
|
|
4
|
+
it('return remoteEntryName while remoteEntryPath is empty', () => {
|
|
5
|
+
const remoteEntry = {
|
|
6
|
+
name: 'vmok-remote-entry.js',
|
|
7
|
+
path: '',
|
|
8
|
+
};
|
|
9
|
+
expect(simpleJoinRemoteEntry(remoteEntry.path, remoteEntry.name)).toEqual(
|
|
10
|
+
remoteEntry.name,
|
|
11
|
+
);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('normalize remoteEntryPath while remoteEntryPath startsWith "."', () => {
|
|
15
|
+
const remoteEntry = {
|
|
16
|
+
name: 'vmok-remote-entry.js',
|
|
17
|
+
path: '.',
|
|
18
|
+
};
|
|
19
|
+
expect(simpleJoinRemoteEntry(remoteEntry.path, remoteEntry.name)).toEqual(
|
|
20
|
+
remoteEntry.name,
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('normalize remoteEntryPath while remoteEntryPath startsWith "./"', () => {
|
|
25
|
+
const remoteEntry = {
|
|
26
|
+
name: 'vmok-remote-entry.js',
|
|
27
|
+
path: './dist/vmok',
|
|
28
|
+
};
|
|
29
|
+
expect(simpleJoinRemoteEntry(remoteEntry.path, remoteEntry.name)).toEqual(
|
|
30
|
+
`dist/vmok/${remoteEntry.name}`,
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('normalize remoteEntryPath while remoteEntryPath startsWith "/"', () => {
|
|
35
|
+
const remoteEntry = {
|
|
36
|
+
name: 'vmok-remote-entry.js',
|
|
37
|
+
path: '/dist/vmok',
|
|
38
|
+
};
|
|
39
|
+
expect(simpleJoinRemoteEntry(remoteEntry.path, remoteEntry.name)).toEqual(
|
|
40
|
+
`dist/vmok/${remoteEntry.name}`,
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('normalize remoteEntryPath while remoteEntryPath endsWith "/"', () => {
|
|
45
|
+
const remoteEntry = {
|
|
46
|
+
name: 'vmok-remote-entry.js',
|
|
47
|
+
path: '/dist/vmok/',
|
|
48
|
+
};
|
|
49
|
+
expect(simpleJoinRemoteEntry(remoteEntry.path, remoteEntry.name)).toEqual(
|
|
50
|
+
`dist/vmok/${remoteEntry.name}`,
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
});
|
package/dist/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present zhanghang(2heal1)
|
|
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.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@module-federation/sdk",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "A sdk for support module federation",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"Module Federation",
|
|
8
|
+
"sdk"
|
|
9
|
+
],
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"author": "zhanghang <hanric.zhang@gmail.com>",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"main": "./index.cjs.js",
|
|
16
|
+
"module": "./index.esm.js",
|
|
17
|
+
"types": "./dist/index.cjs.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": "./dist/index.esm.js",
|
|
21
|
+
"require": "./dist/index.cjs.js",
|
|
22
|
+
"types": "./dist/index.cjs.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"./normalize-webpack-path": {
|
|
25
|
+
"import": "./dist/normalize-webpack-path.esm.js",
|
|
26
|
+
"require": "./dist/normalize-webpack-path.cjs.js",
|
|
27
|
+
"types": "./dist/src/normalize-webpack-path.cjs.d.ts"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"typesVersions": {
|
|
31
|
+
"*": {
|
|
32
|
+
".": [
|
|
33
|
+
"./dist/index.cjs.d.ts"
|
|
34
|
+
],
|
|
35
|
+
"normalize-webpack-path": [
|
|
36
|
+
"./dist/normalize-webpack-path.cjs.d.ts"
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
+
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
clearMocks: true,
|
|
6
|
+
testEnvironment: 'jsdom',
|
|
7
|
+
coveragePathIgnorePatterns: ['__tests__', '/node_modules/'],
|
|
8
|
+
coverageProvider: 'v8',
|
|
9
|
+
coverageReporters: ['cobertura', 'clover', 'json', 'lcov', 'text'],
|
|
10
|
+
globals: {
|
|
11
|
+
__DEV__: true,
|
|
12
|
+
__TEST__: true,
|
|
13
|
+
__BROWSER__: false,
|
|
14
|
+
__VERSION__: '"unknow"',
|
|
15
|
+
},
|
|
16
|
+
preset: 'ts-jest',
|
|
17
|
+
transformIgnorePatterns: [
|
|
18
|
+
// Change MODULE_NAME_HERE to your module that isn't being compiled
|
|
19
|
+
'/node_modules/(?!((@byted/garfish-)|(byted-tea-sdk))).+\\.js$',
|
|
20
|
+
],
|
|
21
|
+
transform: {
|
|
22
|
+
'^.+\\.(t|j)sx?$': ['@swc/jest'],
|
|
23
|
+
},
|
|
24
|
+
rootDir: __dirname,
|
|
25
|
+
testMatch: ['<rootDir>__tests__/**/**.spec.[jt]s?(x)'],
|
|
26
|
+
testPathIgnorePatterns: ['/node_modules/'],
|
|
27
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/sdk",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20231219093556",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "A sdk for support module federation",
|
|
6
6
|
"keywords": [
|
|
@@ -12,21 +12,20 @@
|
|
|
12
12
|
},
|
|
13
13
|
"author": "zhanghang <hanric.zhang@gmail.com>",
|
|
14
14
|
"sideEffects": false,
|
|
15
|
-
"main": "./index.cjs.js",
|
|
16
|
-
"module": "./index.esm.js",
|
|
15
|
+
"main": "./dist/index.cjs.js",
|
|
16
|
+
"module": "./dist/index.esm.js",
|
|
17
17
|
"types": "./dist/index.cjs.d.ts",
|
|
18
18
|
"exports": {
|
|
19
19
|
".": {
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
20
|
+
"import": "./dist/index.esm.js",
|
|
21
|
+
"require": "./dist/index.cjs.js",
|
|
22
|
+
"types": "./dist/index.cjs.d.ts"
|
|
23
23
|
},
|
|
24
24
|
"./normalize-webpack-path": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
}
|
|
29
|
-
"./package.json": "./package.json"
|
|
25
|
+
"import": "./dist/normalize-webpack-path.esm.js",
|
|
26
|
+
"require": "./dist/normalize-webpack-path.cjs.js",
|
|
27
|
+
"types": "./dist/src/normalize-webpack-path.cjs.d.ts"
|
|
28
|
+
}
|
|
30
29
|
},
|
|
31
30
|
"typesVersions": {
|
|
32
31
|
"*": {
|
package/project.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sdk",
|
|
3
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "packages/sdk/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"targets": {
|
|
7
|
+
"build": {
|
|
8
|
+
"executor": "@nx/rollup:rollup",
|
|
9
|
+
"outputs": ["{options.outputPath}"],
|
|
10
|
+
"options": {
|
|
11
|
+
"outputPath": "packages/sdk/dist",
|
|
12
|
+
"main": "packages/sdk/src/index.ts",
|
|
13
|
+
"tsConfig": "packages/sdk/tsconfig.lib.json",
|
|
14
|
+
"assets": [],
|
|
15
|
+
"external": ["@module-federation/*"],
|
|
16
|
+
"project": "packages/sdk/package.json",
|
|
17
|
+
"additionalEntryPoints": ["packages/sdk/src/normalize-webpack-path.ts"],
|
|
18
|
+
"rollupConfig": "packages/sdk/rollup.config.js",
|
|
19
|
+
"compiler": "swc",
|
|
20
|
+
"format": ["cjs", "esm"]
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"lint": {
|
|
24
|
+
"executor": "@nx/linter:eslint",
|
|
25
|
+
"outputs": ["{options.outputFile}"],
|
|
26
|
+
"options": {
|
|
27
|
+
"lintFilePatterns": [
|
|
28
|
+
"packages/sdk/**/*.ts",
|
|
29
|
+
"packages/sdk/package.json"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"test": {
|
|
34
|
+
"executor": "@nx/jest:jest",
|
|
35
|
+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
|
36
|
+
"options": {
|
|
37
|
+
"jestConfig": "packages/sdk/jest.config.js",
|
|
38
|
+
"passWithNoTests": true
|
|
39
|
+
},
|
|
40
|
+
"configurations": {
|
|
41
|
+
"ci": {
|
|
42
|
+
"ci": true,
|
|
43
|
+
"codeCoverage": true
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"pre-release": {
|
|
48
|
+
"executor": "nx:run-commands",
|
|
49
|
+
"options": {
|
|
50
|
+
"parallel": false,
|
|
51
|
+
"commands": [
|
|
52
|
+
{
|
|
53
|
+
"command": "nx run sdk:test",
|
|
54
|
+
"forwardAllArgs": false
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"command": "nx run sdk:build",
|
|
58
|
+
"forwardAllArgs": false
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"tags": ["package"]
|
|
65
|
+
}
|
package/rollup.config.js
ADDED
package/src/constant.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const FederationModuleManifest = 'federation-manifest.json';
|
|
2
|
+
export const MANIFEST_EXT = '.json';
|
|
3
|
+
|
|
4
|
+
export const BROWSER_LOG_KEY = 'FEDERATION_DEBUG';
|
|
5
|
+
export const BROWSER_LOG_VALUE = '1';
|
|
6
|
+
|
|
7
|
+
export const NameTransformSymbol = {
|
|
8
|
+
AT: '@',
|
|
9
|
+
HYPHEN: '-',
|
|
10
|
+
SLASH: '/',
|
|
11
|
+
};
|
|
12
|
+
export const NameTransformMap = {
|
|
13
|
+
[NameTransformSymbol.AT]: 'scope_',
|
|
14
|
+
[NameTransformSymbol.HYPHEN]: '_',
|
|
15
|
+
[NameTransformSymbol.SLASH]: '__',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const EncodedNameTransformMap = {
|
|
19
|
+
[NameTransformMap[NameTransformSymbol.AT]]: NameTransformSymbol.AT,
|
|
20
|
+
[NameTransformMap[NameTransformSymbol.HYPHEN]]: NameTransformSymbol.HYPHEN,
|
|
21
|
+
[NameTransformMap[NameTransformSymbol.SLASH]]: NameTransformSymbol.SLASH,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const SEPARATOR = ':';
|
package/src/dom.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { warn } from './utils';
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
+
export async function safeWrapper<T extends (...args: Array<any>) => any>(
|
|
5
|
+
callback: T,
|
|
6
|
+
disableWarn?: boolean,
|
|
7
|
+
): Promise<ReturnType<T> | undefined> {
|
|
8
|
+
try {
|
|
9
|
+
const res = await callback();
|
|
10
|
+
return res;
|
|
11
|
+
} catch (e) {
|
|
12
|
+
!disableWarn && warn(e);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isStaticResourcesEqual(url1: string, url2: string): boolean {
|
|
18
|
+
const REG_EXP = /^(https?:)?\/\//i;
|
|
19
|
+
// Transform url1 and url2 into relative paths
|
|
20
|
+
const relativeUrl1 = url1.replace(REG_EXP, '').replace(/\/$/, '');
|
|
21
|
+
const relativeUrl2 = url2.replace(REG_EXP, '').replace(/\/$/, '');
|
|
22
|
+
// Check if the relative paths are identical
|
|
23
|
+
return relativeUrl1 === relativeUrl2;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createScript(
|
|
27
|
+
url: string,
|
|
28
|
+
cb: (value: void | PromiseLike<void>) => void,
|
|
29
|
+
attrs?: Record<string, any>,
|
|
30
|
+
createScriptHook?: (url: string) => HTMLScriptElement | void,
|
|
31
|
+
): { script: HTMLScriptElement; needAttach: boolean } {
|
|
32
|
+
// Retrieve the existing script element by its src attribute
|
|
33
|
+
let script: HTMLScriptElement | null = null;
|
|
34
|
+
let needAttach = true;
|
|
35
|
+
const scripts = document.getElementsByTagName('script');
|
|
36
|
+
for (let i = 0; i < scripts.length; i++) {
|
|
37
|
+
const s = scripts[i];
|
|
38
|
+
const scriptSrc = s.getAttribute('src');
|
|
39
|
+
if (scriptSrc && isStaticResourcesEqual(scriptSrc, url)) {
|
|
40
|
+
script = s;
|
|
41
|
+
needAttach = false;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!script) {
|
|
47
|
+
script = document.createElement('script');
|
|
48
|
+
script.type = 'text/javascript';
|
|
49
|
+
script.src = url;
|
|
50
|
+
if (createScriptHook) {
|
|
51
|
+
const createScriptRes = createScriptHook(url);
|
|
52
|
+
if (createScriptRes instanceof HTMLScriptElement) {
|
|
53
|
+
script = createScriptRes;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (attrs) {
|
|
59
|
+
Object.keys(attrs).forEach((name) => {
|
|
60
|
+
if (script) {
|
|
61
|
+
if (name === 'async' || name === 'defer') {
|
|
62
|
+
script[name] = attrs[name];
|
|
63
|
+
} else {
|
|
64
|
+
script.setAttribute(name, attrs[name]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const onScriptComplete = (
|
|
71
|
+
prev: OnErrorEventHandler | GlobalEventHandlers['onload'] | null,
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
|
+
event: any,
|
|
74
|
+
): void => {
|
|
75
|
+
// Prevent memory leaks in IE.
|
|
76
|
+
if (script) {
|
|
77
|
+
script.onerror = null;
|
|
78
|
+
script.onload = null;
|
|
79
|
+
safeWrapper(() => {
|
|
80
|
+
script?.parentNode && script.parentNode.removeChild(script);
|
|
81
|
+
});
|
|
82
|
+
if (prev) {
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
const res = (prev as any)(event);
|
|
85
|
+
cb();
|
|
86
|
+
return res;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
cb();
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
script.onerror = onScriptComplete.bind(null, script.onerror);
|
|
93
|
+
script.onload = onScriptComplete.bind(null, script.onload);
|
|
94
|
+
|
|
95
|
+
return { script, needAttach };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function loadScript(
|
|
99
|
+
url: string,
|
|
100
|
+
info: {
|
|
101
|
+
attrs?: Record<string, any>;
|
|
102
|
+
createScriptHook?: (url: string) => HTMLScriptElement | void;
|
|
103
|
+
},
|
|
104
|
+
) {
|
|
105
|
+
const { attrs, createScriptHook } = info;
|
|
106
|
+
return new Promise<void>((resolve, _reject) => {
|
|
107
|
+
const { script, needAttach } = createScript(
|
|
108
|
+
url,
|
|
109
|
+
resolve,
|
|
110
|
+
attrs,
|
|
111
|
+
createScriptHook,
|
|
112
|
+
);
|
|
113
|
+
needAttach && document.getElementsByTagName('head')[0].appendChild(script);
|
|
114
|
+
});
|
|
115
|
+
}
|
package/src/env.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
declare global {
|
|
2
|
+
// eslint-disable-next-line no-var
|
|
3
|
+
var FEDERATION_DEBUG: string | undefined;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
function isBrowserEnv(): boolean {
|
|
7
|
+
return typeof window !== 'undefined';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function isDebugMode(): boolean {
|
|
11
|
+
if (
|
|
12
|
+
typeof process !== 'undefined' &&
|
|
13
|
+
process.env &&
|
|
14
|
+
process.env['FEDERATION_DEBUG']
|
|
15
|
+
) {
|
|
16
|
+
return Boolean(process.env['FEDERATION_DEBUG']);
|
|
17
|
+
}
|
|
18
|
+
return typeof FEDERATION_DEBUG !== 'undefined' && Boolean(FEDERATION_DEBUG);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const getProcessEnv = function (): Record<string, string | undefined> {
|
|
22
|
+
return typeof process !== 'undefined' && process.env ? process.env : {};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export { isBrowserEnv, isDebugMode, getProcessEnv };
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Manifest,
|
|
3
|
+
ProviderModuleInfo,
|
|
4
|
+
ModuleInfo,
|
|
5
|
+
BasicProviderModuleInfo,
|
|
6
|
+
ConsumerModuleInfo,
|
|
7
|
+
ManifestProvider,
|
|
8
|
+
} from './types';
|
|
9
|
+
import { MANIFEST_EXT } from './constant';
|
|
10
|
+
|
|
11
|
+
interface IOptions {
|
|
12
|
+
remotes?: Record<string, string>;
|
|
13
|
+
overrides?: Record<string, string>;
|
|
14
|
+
version?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const simpleJoinRemoteEntry = (rPath: string, rName: string): string => {
|
|
18
|
+
if (!rPath) {
|
|
19
|
+
return rName;
|
|
20
|
+
}
|
|
21
|
+
const transformPath = (str: string) => {
|
|
22
|
+
if (str === '.') {
|
|
23
|
+
return '';
|
|
24
|
+
}
|
|
25
|
+
if (str.startsWith('./')) {
|
|
26
|
+
return str.replace('./', '');
|
|
27
|
+
}
|
|
28
|
+
if (str.startsWith('/')) {
|
|
29
|
+
const strWithoutSlash = str.slice(1);
|
|
30
|
+
if (strWithoutSlash.endsWith('/')) {
|
|
31
|
+
return strWithoutSlash.slice(0, -1);
|
|
32
|
+
}
|
|
33
|
+
return strWithoutSlash;
|
|
34
|
+
}
|
|
35
|
+
return str;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const transformedPath = transformPath(rPath);
|
|
39
|
+
|
|
40
|
+
if (!transformedPath) {
|
|
41
|
+
return rName;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (transformedPath.endsWith('/')) {
|
|
45
|
+
return `${transformedPath}${rName}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return `${transformedPath}/${rName}`;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Priority: overrides > remotes
|
|
52
|
+
// eslint-disable-next-line max-lines-per-function
|
|
53
|
+
export function generateSnapshotFromManifest(
|
|
54
|
+
manifest: Manifest,
|
|
55
|
+
options: IOptions = {},
|
|
56
|
+
): ProviderModuleInfo {
|
|
57
|
+
const { remotes = {}, overrides = {}, version } = options;
|
|
58
|
+
let remoteSnapshot: ProviderModuleInfo;
|
|
59
|
+
|
|
60
|
+
const getPublicPath = (): string => {
|
|
61
|
+
if ('publicPath' in manifest.metaData) {
|
|
62
|
+
return manifest.metaData.publicPath;
|
|
63
|
+
} else {
|
|
64
|
+
return manifest.metaData.getPublicPath;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const overridesKeys = Object.keys(overrides);
|
|
69
|
+
|
|
70
|
+
let remotesInfo: ConsumerModuleInfo['remotesInfo'] = {};
|
|
71
|
+
|
|
72
|
+
// If remotes are not provided, only the remotes in the manifest will be read
|
|
73
|
+
if (!Object.keys(remotes).length) {
|
|
74
|
+
remotesInfo =
|
|
75
|
+
manifest.remotes?.reduce(
|
|
76
|
+
(res, next) => {
|
|
77
|
+
let matchedVersion: string;
|
|
78
|
+
const name = next.federationContainerName;
|
|
79
|
+
// overrides have higher priority
|
|
80
|
+
if (overridesKeys.includes(name)) {
|
|
81
|
+
matchedVersion = overrides[name];
|
|
82
|
+
} else {
|
|
83
|
+
if ('version' in next) {
|
|
84
|
+
matchedVersion = next.version;
|
|
85
|
+
} else {
|
|
86
|
+
matchedVersion = next.entry;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
res[name] = {
|
|
90
|
+
matchedVersion,
|
|
91
|
+
};
|
|
92
|
+
return res;
|
|
93
|
+
},
|
|
94
|
+
{} as ConsumerModuleInfo['remotesInfo'],
|
|
95
|
+
) || {};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// If remotes (deploy scenario) are specified, they need to be traversed again
|
|
99
|
+
Object.keys(remotes).forEach(
|
|
100
|
+
(key) =>
|
|
101
|
+
(remotesInfo[key] = {
|
|
102
|
+
// overrides will override dependencies
|
|
103
|
+
matchedVersion: overridesKeys.includes(key)
|
|
104
|
+
? overrides[key]
|
|
105
|
+
: remotes[key],
|
|
106
|
+
}),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const {
|
|
110
|
+
remoteEntry: {
|
|
111
|
+
path: remoteEntryPath,
|
|
112
|
+
name: remoteEntryName,
|
|
113
|
+
type: remoteEntryType,
|
|
114
|
+
},
|
|
115
|
+
types: remoteTypes,
|
|
116
|
+
buildInfo: { buildVersion },
|
|
117
|
+
globalName,
|
|
118
|
+
} = manifest.metaData;
|
|
119
|
+
const { exposes } = manifest;
|
|
120
|
+
|
|
121
|
+
let basicRemoteSnapshot: BasicProviderModuleInfo = {
|
|
122
|
+
version: version ? version : '',
|
|
123
|
+
buildVersion,
|
|
124
|
+
globalName,
|
|
125
|
+
remoteEntry: simpleJoinRemoteEntry(remoteEntryPath, remoteEntryName),
|
|
126
|
+
remoteEntryType,
|
|
127
|
+
remoteTypes: simpleJoinRemoteEntry(remoteTypes.path, remoteTypes.name),
|
|
128
|
+
remotesInfo,
|
|
129
|
+
shared: manifest?.shared.map((item) => ({
|
|
130
|
+
assets: item.assets,
|
|
131
|
+
sharedName: item.name,
|
|
132
|
+
})),
|
|
133
|
+
modules: exposes?.map((expose) => ({
|
|
134
|
+
moduleName: expose.name,
|
|
135
|
+
modulePath: expose.path,
|
|
136
|
+
assets: expose.assets,
|
|
137
|
+
})),
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
if (manifest.metaData?.prefetchEntry) {
|
|
141
|
+
const { path, name, type } = manifest.metaData.prefetchEntry;
|
|
142
|
+
|
|
143
|
+
basicRemoteSnapshot = {
|
|
144
|
+
...basicRemoteSnapshot,
|
|
145
|
+
prefetchEntry: simpleJoinRemoteEntry(path, name),
|
|
146
|
+
prefetchEntryType: type,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if ('publicPath' in manifest.metaData) {
|
|
151
|
+
remoteSnapshot = {
|
|
152
|
+
...basicRemoteSnapshot,
|
|
153
|
+
publicPath: getPublicPath(),
|
|
154
|
+
};
|
|
155
|
+
} else {
|
|
156
|
+
remoteSnapshot = {
|
|
157
|
+
...basicRemoteSnapshot,
|
|
158
|
+
getPublicPath: getPublicPath(),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return remoteSnapshot;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function isManifestProvider(
|
|
166
|
+
moduleInfo: ModuleInfo | ManifestProvider,
|
|
167
|
+
): moduleInfo is ManifestProvider {
|
|
168
|
+
if (
|
|
169
|
+
'remoteEntry' in moduleInfo &&
|
|
170
|
+
moduleInfo.remoteEntry.endsWith(MANIFEST_EXT)
|
|
171
|
+
) {
|
|
172
|
+
return true;
|
|
173
|
+
} else {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './constant';
|
|
2
|
+
export * from './types';
|
|
3
|
+
export * from './utils';
|
|
4
|
+
export {
|
|
5
|
+
generateSnapshotFromManifest,
|
|
6
|
+
isManifestProvider,
|
|
7
|
+
} from './generateSnapshotFromManifest';
|
|
8
|
+
export * from './logger';
|
|
9
|
+
export * from './env';
|
|
10
|
+
export * from './dom';
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { BROWSER_LOG_KEY, BROWSER_LOG_VALUE } from './constant';
|
|
2
|
+
import { isBrowserEnv, isDebugMode } from './env';
|
|
3
|
+
|
|
4
|
+
function safeToString(info: any): string {
|
|
5
|
+
try {
|
|
6
|
+
return JSON.stringify(info, null, 2);
|
|
7
|
+
} catch (e) {
|
|
8
|
+
return '';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const DEBUG_LOG = '[ FEDERATION DEBUG ]';
|
|
13
|
+
|
|
14
|
+
class Logger {
|
|
15
|
+
enable = false;
|
|
16
|
+
identifier: string;
|
|
17
|
+
constructor(identifier?: string) {
|
|
18
|
+
this.identifier = identifier || DEBUG_LOG;
|
|
19
|
+
if (
|
|
20
|
+
isBrowserEnv() &&
|
|
21
|
+
localStorage.getItem(BROWSER_LOG_KEY) === BROWSER_LOG_VALUE
|
|
22
|
+
) {
|
|
23
|
+
this.enable = true;
|
|
24
|
+
} else if (isDebugMode()) {
|
|
25
|
+
this.enable = true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
info(msg: string, info?: any): void {
|
|
29
|
+
if (this.enable) {
|
|
30
|
+
const argsToString = safeToString(info) || '';
|
|
31
|
+
if (isBrowserEnv()) {
|
|
32
|
+
console.info(
|
|
33
|
+
`%c ${this.identifier}: ${msg} ${argsToString}`,
|
|
34
|
+
'color:#3300CC',
|
|
35
|
+
);
|
|
36
|
+
} else {
|
|
37
|
+
console.info(
|
|
38
|
+
'\x1b[34m%s',
|
|
39
|
+
`${this.identifier}: ${msg} ${
|
|
40
|
+
argsToString ? `\n${argsToString}` : ''
|
|
41
|
+
}`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
logOriginalInfo(...args: unknown[]) {
|
|
47
|
+
if (this.enable) {
|
|
48
|
+
if (isBrowserEnv()) {
|
|
49
|
+
console.info(`%c ${this.identifier}: OriginalInfo`, 'color:#3300CC');
|
|
50
|
+
console.log(...args);
|
|
51
|
+
} else {
|
|
52
|
+
console.info(`%c ${this.identifier}: OriginalInfo`, 'color:#3300CC');
|
|
53
|
+
console.log(...args);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { Logger };
|