@module-federation/utilities 3.0.4 → 3.0.6

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/README.md ADDED
@@ -0,0 +1,146 @@
1
+ # utils
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build utils` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test utils` to execute the unit tests via [Jest](https://jestjs.io).
12
+
13
+ ## React utilities
14
+
15
+ ---
16
+
17
+ `FederatedBoundary`
18
+
19
+ A component wrapper that provides a fallback for safe imports if something were to fail when grabbing a module off of a remote host.
20
+
21
+ This wrapper also exposes an optional property for a custom react error boundary component.
22
+
23
+ Any extra props will be passed directly to the imported module.
24
+
25
+ Usage looks something like this:
26
+
27
+ ```js
28
+ import { FederationBoundary } from '@module-federation/utilities/src/utils/react';
29
+
30
+ // defining dynamicImport and fallback outside the Component to keep the component identity
31
+ // another alternative would be to use useMemo
32
+ const dynamicImport = () => import('some_remote_host_name').then((m) => m.Component);
33
+ const fallback = () => import('@npm/backup').then((m) => m.Component);
34
+
35
+ const MyPage = () => {
36
+ return <FederationBoundary dynamicImporter={dynamicImport} fallback={fallback} customBoundary={CustomErrorBoundary} />;
37
+ };
38
+ ```
39
+
40
+ ---
41
+
42
+ `ImportRemote`
43
+
44
+ A function which will enable dynamic imports of remotely exposed modules using the Module Federation plugin. It uses the method described in the official Webpack configuration under <a href="https://webpack.js.org/concepts/module-federation/#dynamic-remote-containers" target="_blank">Dynamic Remote Containers</a>.
45
+
46
+ This function will allow you to provide a static url or an async method to retrieve a url from a configuration service.
47
+
48
+ Usage looks something like this:
49
+
50
+ ```js
51
+ import { importRemote } from '@module-federation/utilities';
52
+
53
+ // --
54
+ // If it's a regular js module:
55
+ // --
56
+ importRemote({
57
+ url: 'http://localhost:3001',
58
+ scope: 'Foo',
59
+ module: 'Bar',
60
+ }).then(
61
+ (
62
+ {
63
+ /* list of Bar exports */
64
+ },
65
+ ) => {
66
+ // Use Bar exports
67
+ },
68
+ );
69
+
70
+ // --
71
+ // If it's a ESM module (this is currently the default for NX):
72
+ // --
73
+ const Bar = lazy(() => importRemote({ url: 'http://localhost:3001', scope: 'Foo', module: 'Bar', esm: true }));
74
+
75
+ return (
76
+ <Suspense fallback={<div>Loading Bar...</div>}>
77
+ <Bar />
78
+ </Suspense>
79
+ );
80
+
81
+ // --
82
+ // If Bar is a React component you can use it with lazy and Suspense just like a dynamic import:
83
+ // --
84
+ const Bar = lazy(() => importRemote({ url: 'http://localhost:3001', scope: 'Foo', module: 'Bar' }));
85
+
86
+ return (
87
+ <Suspense fallback={<div>Loading Bar...</div>}>
88
+ <Bar />
89
+ </Suspense>
90
+ );
91
+ ```
92
+
93
+ ```js
94
+ import { importRemote } from '@module-federation/utilities';
95
+
96
+ // If it's a regular js module:
97
+ importRemote({
98
+ url: () => MyAsyncMethod('remote_name'),
99
+ scope: 'Foo',
100
+ module: 'Bar',
101
+ }).then(
102
+ (
103
+ {
104
+ /* list of Bar exports */
105
+ },
106
+ ) => {
107
+ // Use Bar exports
108
+ },
109
+ );
110
+
111
+ // If Bar is a React component you can use it with lazy and Suspense just like a dynamic import:
112
+ const Bar = lazy(() =>
113
+ importRemote({
114
+ url: () => MyAsyncMethod('remote_name'),
115
+ scope: 'Foo',
116
+ module: 'Bar',
117
+ }),
118
+ );
119
+
120
+ return (
121
+ <Suspense fallback={<div>Loading Bar...</div>}>
122
+ <Bar />
123
+ </Suspense>
124
+ );
125
+ ```
126
+
127
+ ```js
128
+ // You can also combine importRemote and FederationBoundary to have a dynamic remote URL and a fallback when there is an error on the remote
129
+
130
+ const dynamicImporter = () =>
131
+ importRemote({
132
+ url: 'http://localhost:3001',
133
+ scope: 'Foo',
134
+ module: 'Bar',
135
+ });
136
+ const fallback = () => import('@npm/backup').then((m) => m.Component);
137
+
138
+ const Bar = () => {
139
+ return <FederationBoundary dynamicImporter={dynamicImporter} fallback={fallback} />;
140
+ };
141
+ ```
142
+
143
+ Apart from **url**, **scope** and **module** you can also pass additional options to the **importRemote()** function:
144
+
145
+ - **remoteEntryFileName**: The name of the remote entry file. Defaults to "remoteEntry.js".
146
+ - **bustRemoteEntryCache**: Whether to add a cache busting query parameter to the remote entry file URL. Defaults to **true**. You can disable it if cachebusting is handled by the server.
@@ -372,34 +372,6 @@ function _ts_generator$2(thisArg, body) {
372
372
  };
373
373
  }
374
374
  }
375
- /**
376
- * Creates a module that can be shared across different builds.
377
- * @param {string} delegate - The delegate string.
378
- * @param {Object} params - The parameters for the module.
379
- * @returns {string} - The created module.
380
- * @throws Will throw an error if the params are an array or object.
381
- */ var createDelegatedModule = function(delegate, params) {
382
- var queries = [];
383
- var processParam = function(key, value) {
384
- if (Array.isArray(value)) {
385
- value.forEach(function(v, i) {
386
- return processParam("".concat(key, "[").concat(i, "]"), v);
387
- });
388
- } else if (typeof value === "object" && value !== null) {
389
- Object.entries(value).forEach(function(param) {
390
- var _param = _sliced_to_array$2(param, 2), k = _param[0], v = _param[1];
391
- return processParam("".concat(key, ".").concat(k), v);
392
- });
393
- } else {
394
- queries.push("".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value)));
395
- }
396
- };
397
- Object.entries(params).forEach(function(param) {
398
- var _param = _sliced_to_array$2(param, 2), key = _param[0], value = _param[1];
399
- return processParam(key, value);
400
- });
401
- return queries.length === 0 ? "internal ".concat(delegate) : "internal ".concat(delegate, "?").concat(queries.join("&"));
402
- };
403
375
  var createContainerSharingScope = function(asyncContainer) {
404
376
  // @ts-ignore
405
377
  return asyncContainer.then(function(container) {
@@ -1413,7 +1385,6 @@ var importDelegatedModule = function() {
1413
1385
  }();
1414
1386
 
1415
1387
  exports.Logger = Logger;
1416
- exports.createDelegatedModule = createDelegatedModule;
1417
1388
  exports.createRuntimeVariables = createRuntimeVariables;
1418
1389
  exports.extractUrlAndGlobal = extractUrlAndGlobal;
1419
1390
  exports.getContainer = getContainer;
@@ -350,34 +350,6 @@ function _ts_generator$2(thisArg, body) {
350
350
  };
351
351
  }
352
352
  }
353
- /**
354
- * Creates a module that can be shared across different builds.
355
- * @param {string} delegate - The delegate string.
356
- * @param {Object} params - The parameters for the module.
357
- * @returns {string} - The created module.
358
- * @throws Will throw an error if the params are an array or object.
359
- */ var createDelegatedModule = function(delegate, params) {
360
- var queries = [];
361
- var processParam = function(key, value) {
362
- if (Array.isArray(value)) {
363
- value.forEach(function(v, i) {
364
- return processParam("".concat(key, "[").concat(i, "]"), v);
365
- });
366
- } else if (typeof value === "object" && value !== null) {
367
- Object.entries(value).forEach(function(param) {
368
- var _param = _sliced_to_array$2(param, 2), k = _param[0], v = _param[1];
369
- return processParam("".concat(key, ".").concat(k), v);
370
- });
371
- } else {
372
- queries.push("".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value)));
373
- }
374
- };
375
- Object.entries(params).forEach(function(param) {
376
- var _param = _sliced_to_array$2(param, 2), key = _param[0], value = _param[1];
377
- return processParam(key, value);
378
- });
379
- return queries.length === 0 ? "internal ".concat(delegate) : "internal ".concat(delegate, "?").concat(queries.join("&"));
380
- };
381
353
  var createContainerSharingScope = function(asyncContainer) {
382
354
  // @ts-ignore
383
355
  return asyncContainer.then(function(container) {
@@ -1390,4 +1362,4 @@ var importDelegatedModule = function() {
1390
1362
  };
1391
1363
  }();
1392
1364
 
1393
- export { Logger, createDelegatedModule, createRuntimeVariables, extractUrlAndGlobal, getContainer, getModule, getRuntimeRemotes, importDelegatedModule, importRemote, injectScript, isObjectEmpty, loadScript };
1365
+ export { Logger, createRuntimeVariables, extractUrlAndGlobal, getContainer, getModule, getRuntimeRemotes, importDelegatedModule, importRemote, injectScript, isObjectEmpty, loadScript };
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@module-federation/utilities",
3
+ "version": "3.0.6",
4
+ "type": "commonjs",
5
+ "main": "./index.cjs.js",
6
+ "types": "./dist/index.cjs.d.ts",
7
+ "license": "MIT",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "files": [
12
+ "dist/",
13
+ "README.md"
14
+ ],
15
+ "repository": "https://github.com/module-federation/universe/tree/main/packages/utilities",
16
+ "devDependencies": {
17
+ "react": "18.2.0"
18
+ },
19
+ "peerDependencies": {
20
+ "webpack": "^5.40.0",
21
+ "react-dom": "^16 || ^17 || ^18",
22
+ "react": "^16 || ^17 || ^18"
23
+ },
24
+ "peerDependenciesMeta": {
25
+ "react": {
26
+ "optional": true
27
+ },
28
+ "react-dom": {
29
+ "optional": true
30
+ },
31
+ "next": {
32
+ "optional": true
33
+ }
34
+ },
35
+ "exports": {
36
+ ".": {
37
+ "module": "./index.esm.js",
38
+ "import": "./index.cjs.mjs",
39
+ "default": "./index.cjs.js"
40
+ },
41
+ "./package.json": "./package.json"
42
+ },
43
+ "typesVersions": {
44
+ "*": {
45
+ ".": [
46
+ "./dist/index.cjs.d.ts"
47
+ ],
48
+ "type": [
49
+ "./dist/type.cjs.d.ts"
50
+ ]
51
+ }
52
+ },
53
+ "module": "./index.esm.js"
54
+ }
@@ -1,7 +1,7 @@
1
1
  export * from './types';
2
2
  export type { ImportRemoteOptions } from './utils/importRemote';
3
3
  export type { LoggerInstance } from './Logger';
4
- export { createDelegatedModule, createRuntimeVariables, getContainer, injectScript, getModule, } from './utils/common';
4
+ export { createRuntimeVariables, getContainer, injectScript, getModule, } from './utils/common';
5
5
  export { isObjectEmpty } from './utils/isEmpty';
6
6
  export { importRemote } from './utils/importRemote';
7
7
  export { Logger } from './Logger';
@@ -1,14 +1,4 @@
1
1
  import type { GetModuleOptions, RemoteData, Remotes, RuntimeRemote, WebpackRemoteContainer } from '../types';
2
- /**
3
- * Creates a module that can be shared across different builds.
4
- * @param {string} delegate - The delegate string.
5
- * @param {Object} params - The parameters for the module.
6
- * @returns {string} - The created module.
7
- * @throws Will throw an error if the params are an array or object.
8
- */
9
- export declare const createDelegatedModule: (delegate: string, params: {
10
- [key: string]: any;
11
- }) => string;
12
2
  /**
13
3
  * Return initialized remote container by remote's key or its runtime remote item data.
14
4
  *
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "name": "@module-federation/utilities",
3
- "version": "3.0.4",
3
+ "version": "3.0.6",
4
4
  "type": "commonjs",
5
- "main": "./index.cjs.js",
5
+ "main": "./dist/index.cjs.js",
6
6
  "types": "./dist/index.cjs.d.ts",
7
7
  "license": "MIT",
8
8
  "publishConfig": {
9
9
  "access": "public"
10
10
  },
11
+ "files": [
12
+ "dist/",
13
+ "README.md"
14
+ ],
11
15
  "repository": "https://github.com/module-federation/universe/tree/main/packages/utilities",
12
16
  "devDependencies": {
13
17
  "react": "18.2.0"
@@ -29,11 +33,7 @@
29
33
  }
30
34
  },
31
35
  "exports": {
32
- ".": {
33
- "module": "./index.esm.js",
34
- "import": "./index.cjs.mjs",
35
- "default": "./index.cjs.js"
36
- },
36
+ ".": "./dist/index.cjs.js",
37
37
  "./package.json": "./package.json"
38
38
  },
39
39
  "typesVersions": {
@@ -45,6 +45,5 @@
45
45
  "./dist/type.cjs.d.ts"
46
46
  ]
47
47
  }
48
- },
49
- "module": "./index.esm.js"
48
+ }
50
49
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes