@module-federation/sdk 0.0.0-next-20231219090402 → 0.0.0-next-20231219093413

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.
Files changed (55) hide show
  1. package/CHANGELOG.md +2189 -2
  2. package/README.md +99 -0
  3. package/__tests__/decodeName.spec.ts +11 -0
  4. package/__tests__/encodeName.spec.ts +10 -0
  5. package/__tests__/generateSnapshotFromManifest.spec.ts +147 -0
  6. package/__tests__/parseEntry.spec.ts +36 -0
  7. package/__tests__/resources/constant.ts +6 -0
  8. package/__tests__/resources/getId.ts +5 -0
  9. package/__tests__/resources/manifestSnapshotMap.ts +1541 -0
  10. package/__tests__/simpleJoinRemoteEntry.spec.ts +53 -0
  11. package/dist/LICENSE +21 -0
  12. package/dist/package.json +40 -0
  13. package/jest.config.js +27 -0
  14. package/package.json +10 -11
  15. package/project.json +65 -0
  16. package/rollup.config.js +11 -0
  17. package/src/constant.ts +24 -0
  18. package/src/dom.ts +115 -0
  19. package/src/env.ts +25 -0
  20. package/src/generateSnapshotFromManifest.ts +176 -0
  21. package/src/index.ts +10 -0
  22. package/src/logger.ts +59 -0
  23. package/src/normalize-webpack-path.ts +33 -0
  24. package/src/types/common.ts +18 -0
  25. package/src/types/index.ts +4 -0
  26. package/src/types/manifest.ts +44 -0
  27. package/src/types/snapshot.ts +87 -0
  28. package/src/types/stats.ts +101 -0
  29. package/src/utils.ts +213 -0
  30. package/tsconfig.json +29 -0
  31. package/tsconfig.lib.json +10 -0
  32. package/tsconfig.spec.json +14 -0
  33. package/index.cjs.default.js +0 -1
  34. package/index.cjs.mjs +0 -2
  35. package/normalize-webpack-path.cjs.default.js +0 -1
  36. package/normalize-webpack-path.cjs.mjs +0 -2
  37. /package/{index.cjs.d.ts → dist/index.cjs.d.ts} +0 -0
  38. /package/{index.cjs.js → dist/index.cjs.js} +0 -0
  39. /package/{index.esm.js → dist/index.esm.js} +0 -0
  40. /package/{normalize-webpack-path.cjs.d.ts → dist/normalize-webpack-path.cjs.d.ts} +0 -0
  41. /package/{normalize-webpack-path.cjs.js → dist/normalize-webpack-path.cjs.js} +0 -0
  42. /package/{normalize-webpack-path.esm.js → dist/normalize-webpack-path.esm.js} +0 -0
  43. /package/{src → dist/src}/constant.d.ts +0 -0
  44. /package/{src → dist/src}/dom.d.ts +0 -0
  45. /package/{src → dist/src}/env.d.ts +0 -0
  46. /package/{src → dist/src}/generateSnapshotFromManifest.d.ts +0 -0
  47. /package/{src → dist/src}/index.d.ts +0 -0
  48. /package/{src → dist/src}/logger.d.ts +0 -0
  49. /package/{src → dist/src}/normalize-webpack-path.d.ts +0 -0
  50. /package/{src → dist/src}/types/common.d.ts +0 -0
  51. /package/{src → dist/src}/types/index.d.ts +0 -0
  52. /package/{src → dist/src}/types/manifest.d.ts +0 -0
  53. /package/{src → dist/src}/types/snapshot.d.ts +0 -0
  54. /package/{src → dist/src}/types/stats.d.ts +0 -0
  55. /package/{src → dist/src}/utils.d.ts +0 -0
package/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # `@module-federation/sdk` Documentation
2
+
3
+ - This SDK provides utilities and tools to support the implementation of Module Federation in your projects.
4
+ - It contains utility functions for parsing, encoding, and decoding module names, as well as generating filenames for exposed modules and shared packages.
5
+ - It also includes a logger for debugging and environment detection utilities.
6
+ - Additionally, it provides a function to generate a snapshot from a manifest and environment detection utilities.
7
+
8
+ ## Usage
9
+
10
+ ```javascript
11
+ // The SDK can be used to parse entry strings, encode and decode module names, and generate filenames for exposed modules and shared packages.
12
+ // It also includes a logger for debugging and environment detection utilities.
13
+ // Additionally, it provides a function to generate a snapshot from a manifest and environment detection utilities.
14
+ import { parseEntry, encodeName, decodeName, generateExposeFilename, generateShareFilename, Logger, isBrowserEnv, isDebugMode, getProcessEnv, generateSnapshotFromManifest } from '@module-federation/sdk';
15
+
16
+ // Parse an entry string into a RemoteEntryInfo object
17
+ parseEntry('entryString');
18
+
19
+ // Encode a module name with a prefix and optional extension
20
+ encodeName('moduleName', 'prefix');
21
+
22
+ // Decode a module name with a prefix and optional extension
23
+ decodeName('encodedModuleName', 'prefix');
24
+
25
+ // Generate a filename for an exposed module
26
+ generateExposeFilename('exposeName', true);
27
+
28
+ // Generate a filename for a shared package
29
+ generateShareFilename('packageName', true);
30
+
31
+ // Create a new logger
32
+ const logger = new Logger('identifier');
33
+
34
+ // Check if the current environment is a browser
35
+ isBrowserEnv();
36
+
37
+ // Check if the current environment is in debug mode
38
+ isDebugMode();
39
+
40
+ // Get the process environment
41
+ getProcessEnv();
42
+
43
+ // Generate a snapshot from a manifest
44
+ generateSnapshotFromManifest(manifest, options);
45
+ ```
46
+
47
+ ### parseEntry
48
+
49
+ - Type: `parseEntry(str: string, devVerOrUrl?: string)`
50
+ - Parses a string into a RemoteEntryInfo object.
51
+
52
+ ### encodeName
53
+
54
+ - Type: `encodeName(name: string, prefix = '', withExt = false)`
55
+ - Encodes a name with a prefix and optional extension.
56
+
57
+ ### decodeName
58
+
59
+ - Type: `decodeName(name: string, prefix?: string, withExt?: boolean)`
60
+ - Decodes a name with a prefix and optional extension.
61
+
62
+ ### generateExposeFilename
63
+
64
+ - Type: `generateExposeFilename(exposeName: string, withExt: boolean)`
65
+ - Generates a filename for an expose.
66
+
67
+ ### generateShareFilename
68
+
69
+ - Type: `generateShareFilename(pkgName: string, withExt: boolean)`
70
+ - Generates a filename for a shared package.
71
+
72
+ ### Logger
73
+
74
+ - Type: `new Logger(identifier?: string)`
75
+ - Creates a new logger for debugging.
76
+
77
+ ### isBrowserEnv
78
+
79
+ - Type: `isBrowserEnv()`
80
+ - Checks if the current environment is a browser.
81
+
82
+ ### isDebugMode
83
+
84
+ - Type: `isDebugMode()`
85
+ - Checks if the current environment is in debug mode.
86
+
87
+ ### getProcessEnv
88
+
89
+ - Type: `getProcessEnv()`
90
+ - Gets the process environment.
91
+
92
+ ### generateSnapshotFromManifest
93
+
94
+ - Type: `generateSnapshotFromManifest(manifest: Manifest, options: IOptions = {})`
95
+ - Generates a snapshot from a manifest.
96
+
97
+ ## Testing
98
+
99
+ The SDK uses Jest for testing. The configuration can be found in `jest.config.js`. The tests are located in the **tests** directory.
@@ -0,0 +1,11 @@
1
+ import { nameMap } from './resources/constant';
2
+ import { decodeName } from '../src';
3
+
4
+ describe('decodeName', () => {
5
+ it('should correct decode transformed name', () => {
6
+ Object.keys(nameMap).forEach((name) => {
7
+ const transformedName = nameMap[name];
8
+ expect(decodeName(transformedName)).toBe(name);
9
+ });
10
+ });
11
+ });
@@ -0,0 +1,10 @@
1
+ import { nameMap } from './resources/constant';
2
+ import { encodeName } from '../src';
3
+
4
+ describe('encodeName', () => {
5
+ it('should correct transform name', () => {
6
+ Object.keys(nameMap).forEach((name) => {
7
+ expect(encodeName(name)).toBe(nameMap[name]);
8
+ });
9
+ });
10
+ });
@@ -0,0 +1,147 @@
1
+ import { manifest, snapshot } from './resources/manifestSnapshotMap';
2
+ import { generateSnapshotFromManifest } from '../src/generateSnapshotFromManifest';
3
+
4
+ describe('generateSnapshotFromManifest', () => {
5
+ it('return basic app snapshot with only manifest params in dev', () => {
6
+ const remoteSnapshot = generateSnapshotFromManifest(
7
+ manifest.devAppManifest,
8
+ );
9
+ expect(remoteSnapshot).toEqual(snapshot.devAppSnapshot);
10
+ });
11
+
12
+ it('return modulePath while expose.path existed', () => {
13
+ const remoteSnapshot = generateSnapshotFromManifest(
14
+ manifest.devAppManifest,
15
+ );
16
+ expect(remoteSnapshot).toEqual(snapshot.devAppSnapshot);
17
+ manifest.devAppManifest.exposes.forEach((expose) => {
18
+ if (expose.path) {
19
+ expect(
20
+ Boolean(
21
+ remoteSnapshot.modules.find((m) => m.modulePath === expose.path),
22
+ ),
23
+ ).toEqual(true);
24
+ }
25
+ });
26
+ });
27
+
28
+ it('return basic app snapshot with manifest params and version in dev', () => {
29
+ const remoteSnapshot = generateSnapshotFromManifest(
30
+ manifest.devAppManifest,
31
+ { version: 'http://localhost:2006/vmok-manifest.json' },
32
+ );
33
+ expect(remoteSnapshot).toEqual(snapshot.devAppSnapshotWithVersion);
34
+ });
35
+
36
+ it('return basic app snapshot with only manifest params in dev with getPublicPath', () => {
37
+ const remoteSnapshot = generateSnapshotFromManifest(
38
+ manifest.devAppManifestWithGetPublicPath,
39
+ );
40
+ expect(remoteSnapshot).toEqual(snapshot.devAppSnapshotWithGetPublicPath);
41
+ });
42
+
43
+ it('return app snapshot with manifest params in dev with overrides', () => {
44
+ const remoteSnapshot = generateSnapshotFromManifest(
45
+ manifest.devAppManifest,
46
+ {
47
+ overrides: {
48
+ '@garfish/micro-app-sub4': 'http://localhost:8080/vmok-manifest.json',
49
+ '@garfish/micro-app-sub3': '1.0.3',
50
+ },
51
+ },
52
+ );
53
+ expect(remoteSnapshot).toEqual(snapshot.devAppSnapshotWithOverrides);
54
+ });
55
+
56
+ it('return app snapshot with manifest params in dev with part remotes', () => {
57
+ const remoteSnapshot = generateSnapshotFromManifest(
58
+ manifest.devAppManifest,
59
+ {
60
+ remotes: {
61
+ '@garfish/micro-app-sub3': '1.0.3',
62
+ },
63
+ },
64
+ );
65
+ expect(remoteSnapshot).toEqual(snapshot.devAppSnapshotWithPartRemotes);
66
+ });
67
+
68
+ it('return app snapshot with manifest params in dev with remotes', () => {
69
+ const remoteSnapshot = generateSnapshotFromManifest(
70
+ manifest.devAppManifest,
71
+ {
72
+ remotes: {
73
+ '@garfish/micro-app-sub4': '1.2.3',
74
+ '@garfish/micro-app-sub3': '1.0.3',
75
+ },
76
+ },
77
+ );
78
+ expect(remoteSnapshot).toEqual(snapshot.devAppSnapshotWithRemotes);
79
+ });
80
+
81
+ it('return app snapshot with manifest params in dev with remotes and overrides', () => {
82
+ const remoteSnapshot = generateSnapshotFromManifest(
83
+ manifest.devAppManifest,
84
+ {
85
+ remotes: {
86
+ '@garfish/micro-app-sub4': '1.2.3',
87
+ '@garfish/micro-app-sub3': '1.0.3',
88
+ },
89
+ overrides: {
90
+ '@garfish/micro-app-sub4': 'http://localhost:8080/vmok-manifest.json',
91
+ '@garfish/micro-app-sub3': '1.0.4',
92
+ },
93
+ },
94
+ );
95
+ expect(remoteSnapshot).toEqual(
96
+ snapshot.devAppSnapshotWithRemotesAndOverrides,
97
+ );
98
+ });
99
+
100
+ it('return app snapshot with manifest params in dev with all params', () => {
101
+ const remoteSnapshot = generateSnapshotFromManifest(
102
+ manifest.devAppManifest,
103
+ {
104
+ remotes: {
105
+ '@garfish/micro-app-sub4': '1.2.3',
106
+ '@garfish/micro-app-sub3': '1.0.3',
107
+ },
108
+ overrides: {
109
+ '@garfish/micro-app-sub4': 'http://localhost:8080/vmok-manifest.json',
110
+ '@garfish/micro-app-sub3': '1.0.4',
111
+ },
112
+ },
113
+ );
114
+ expect(remoteSnapshot).toEqual(snapshot.devAppSnapshotWithAllParams);
115
+ });
116
+
117
+ it('return basic app snapshot with only manifest params in prod', () => {
118
+ const remoteSnapshot = generateSnapshotFromManifest(
119
+ manifest.prodAppManifest,
120
+ );
121
+ expect(remoteSnapshot).toEqual(snapshot.prodAppSnapshot);
122
+ });
123
+
124
+ it('return basic app snapshot with only manifest params in prod with getPublicPath', () => {
125
+ const remoteSnapshot = generateSnapshotFromManifest(
126
+ manifest.prodAppManifestWithGetPublicPath,
127
+ );
128
+ expect(remoteSnapshot).toEqual(snapshot.prodAppSnapshotWithGetPublicPath);
129
+ });
130
+
131
+ it('return app snapshot with manifest params in prod with all params', () => {
132
+ const remoteSnapshot = generateSnapshotFromManifest(
133
+ manifest.prodAppManifest,
134
+ {
135
+ remotes: {
136
+ '@garfish/micro-app-sub4': '1.2.3',
137
+ '@garfish/micro-app-sub3': '1.0.3',
138
+ },
139
+ overrides: {
140
+ '@garfish/micro-app-sub4': 'http://localhost:8080/vmok-manifest.json',
141
+ '@garfish/micro-app-sub3': '1.0.4',
142
+ },
143
+ },
144
+ );
145
+ expect(remoteSnapshot).toEqual(snapshot.prodAppSnapshotWithAllParams);
146
+ });
147
+ });
@@ -0,0 +1,36 @@
1
+ import { parseEntry } from '../src';
2
+
3
+ describe('parseEntry', () => {
4
+ it('get correct entryInfo by parsing normal entry', () => {
5
+ const entry = '@byted/app1:1.2.0';
6
+
7
+ const entryInfo = parseEntry(entry);
8
+
9
+ expect(entryInfo).toMatchObject({
10
+ version: '1.2.0',
11
+ name: '@byted/app1',
12
+ });
13
+ });
14
+
15
+ it('get correct entryInfo by parsing none version entry', () => {
16
+ const entry = '@byted/app1';
17
+
18
+ const entryInfo = parseEntry(entry);
19
+
20
+ expect(entryInfo).toMatchObject({
21
+ version: '*',
22
+ name: '@byted/app1',
23
+ });
24
+ });
25
+
26
+ it('get correct entryInfo by parsing local version entry', () => {
27
+ const entry = '@byted/app1:http://localhost:8080/vmok-manifest.json';
28
+
29
+ const entryInfo = parseEntry(entry);
30
+
31
+ expect(entryInfo).toMatchObject({
32
+ name: '@byted/app1',
33
+ entry: 'http://localhost:8080/vmok-manifest.json',
34
+ });
35
+ });
36
+ });
@@ -0,0 +1,6 @@
1
+ export const nameMap = {
2
+ '@byted/app1': 'scope_byted__app1',
3
+ '@byted-garfish/app1': 'scope_byted_garfish__app1',
4
+ '@byted-garfish/app1-demo': 'scope_byted_garfish__app1_demo',
5
+ '@byted-garfish/module-1': 'scope_byted_garfish__module_1',
6
+ };
@@ -0,0 +1,5 @@
1
+ function getId(): number {
2
+ return Math.random();
3
+ }
4
+
5
+ export default getId;