@module-federation/modern-js 0.0.0-next-20240529103203 → 0.0.0-next-20240530085615

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.
@@ -29,7 +29,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var MFReactComponent_exports = {};
30
30
  __export(MFReactComponent_exports, {
31
31
  MFReactComponent: () => MFReactComponent,
32
- collectLinks: () => collectLinks
32
+ collectAssets: () => collectAssets
33
33
  });
34
34
  module.exports = __toCommonJS(MFReactComponent_exports);
35
35
  var import_jsx_runtime = require("react/jsx-runtime");
@@ -51,30 +51,58 @@ function getLoadedRemoteInfos(instance, id) {
51
51
  expose
52
52
  };
53
53
  }
54
- function collectLinks(id) {
55
- const links = [];
54
+ function getTargetModuleInfo(id) {
56
55
  const instance = (0, import_runtime.getInstance)();
57
56
  if (!instance) {
58
- return links;
57
+ return;
59
58
  }
60
59
  const loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
61
60
  if (!loadedRemoteInfo) {
62
- return links;
61
+ return;
63
62
  }
64
63
  const snapshot = loadedRemoteInfo.snapshot;
65
64
  if (!snapshot) {
66
- return links;
65
+ return;
67
66
  }
68
67
  const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
69
68
  if (!publicPath) {
70
- return links;
69
+ return;
71
70
  }
72
71
  const modules = "modules" in snapshot ? snapshot.modules : [];
73
- if (modules) {
74
- const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
75
- if (!targetModule) {
76
- return links;
77
- }
72
+ const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
73
+ if (!targetModule) {
74
+ return;
75
+ }
76
+ const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
77
+ if (!remoteEntry) {
78
+ return;
79
+ }
80
+ return {
81
+ module: targetModule,
82
+ publicPath,
83
+ remoteEntry
84
+ };
85
+ }
86
+ function collectAssets(options) {
87
+ const { id, injectLink = true, injectScript = true } = options;
88
+ const links = [];
89
+ const scripts = [];
90
+ const instance = (0, import_runtime.getInstance)();
91
+ if (!instance || !injectLink && !injectScript) {
92
+ return [
93
+ ...scripts,
94
+ ...links
95
+ ];
96
+ }
97
+ const moduleAndPublicPath = getTargetModuleInfo(id);
98
+ if (!moduleAndPublicPath) {
99
+ return [
100
+ ...scripts,
101
+ ...links
102
+ ];
103
+ }
104
+ const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
105
+ if (injectLink) {
78
106
  [
79
107
  ...targetModule.assets.css.sync,
80
108
  ...targetModule.assets.css.async
@@ -83,23 +111,42 @@ function collectLinks(id) {
83
111
  href: `${publicPath}${file}`,
84
112
  rel: "stylesheet",
85
113
  type: "text/css"
86
- }, index));
114
+ }, `${file.split(".")[0]}_${index}`));
115
+ });
116
+ }
117
+ if (injectScript) {
118
+ scripts.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
119
+ async: true,
120
+ src: `${publicPath}${remoteEntry}`,
121
+ crossOrigin: "anonymous"
122
+ }, remoteEntry.split(".")[0]));
123
+ [
124
+ ...targetModule.assets.js.sync
125
+ ].forEach((file, index) => {
126
+ scripts.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
127
+ async: true,
128
+ src: `${publicPath}${file}`,
129
+ crossOrigin: "anonymous"
130
+ }, `${file.split(".")[0]}_${index}`));
87
131
  });
88
132
  }
89
- return links;
133
+ return [
134
+ ...scripts,
135
+ ...links
136
+ ];
90
137
  }
91
138
  function MFReactComponent(props) {
92
139
  const { loading = "loading...", id, fallback } = props;
93
140
  const Component = /* @__PURE__ */ import_react.default.lazy(() => (0, import_runtime.loadRemote)(id).then((mod) => {
94
- const links = collectLinks(id);
141
+ const assets = collectAssets(props);
95
142
  if (!mod) {
96
143
  throw new Error("load remote failed");
97
144
  }
98
145
  const Com = typeof mod === "object" ? "default" in mod ? mod.default : mod : mod;
99
146
  return {
100
- default: () => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
147
+ default: () => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
101
148
  children: [
102
- links,
149
+ assets,
103
150
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Com, {})
104
151
  ]
105
152
  })
@@ -127,5 +174,5 @@ function MFReactComponent(props) {
127
174
  // Annotate the CommonJS export names for ESM import in node:
128
175
  0 && (module.exports = {
129
176
  MFReactComponent,
130
- collectLinks
177
+ collectAssets
131
178
  });
@@ -20,7 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var runtime_exports = {};
21
21
  __export(runtime_exports, {
22
22
  MFReactComponent: () => import_MFReactComponent.MFReactComponent,
23
- collectLinks: () => import_MFReactComponent.collectLinks
23
+ collectAssets: () => import_MFReactComponent.collectAssets
24
24
  });
25
25
  module.exports = __toCommonJS(runtime_exports);
26
26
  __reExport(runtime_exports, require("@module-federation/enhanced/runtime"), module.exports);
@@ -28,6 +28,6 @@ var import_MFReactComponent = require("./MFReactComponent");
28
28
  // Annotate the CommonJS export names for ESM import in node:
29
29
  0 && (module.exports = {
30
30
  MFReactComponent,
31
- collectLinks,
31
+ collectAssets,
32
32
  ...require("@module-federation/enhanced/runtime")
33
33
  });
@@ -1,7 +1,7 @@
1
1
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
2
  import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
3
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
4
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
4
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
5
5
  import React from "react";
6
6
  import { loadRemote, getInstance } from "@module-federation/enhanced/runtime";
7
7
  function getLoadedRemoteInfos(instance, id) {
@@ -19,56 +19,92 @@ function getLoadedRemoteInfos(instance, id) {
19
19
  expose
20
20
  });
21
21
  }
22
- function collectLinks(id) {
23
- var links = [];
22
+ function getTargetModuleInfo(id) {
24
23
  var instance = getInstance();
25
24
  if (!instance) {
26
- return links;
25
+ return;
27
26
  }
28
27
  var loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
29
28
  if (!loadedRemoteInfo) {
30
- return links;
29
+ return;
31
30
  }
32
31
  var snapshot = loadedRemoteInfo.snapshot;
33
32
  if (!snapshot) {
34
- return links;
33
+ return;
35
34
  }
36
35
  var publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
37
36
  if (!publicPath) {
38
- return links;
37
+ return;
39
38
  }
40
39
  var modules = "modules" in snapshot ? snapshot.modules : [];
41
- if (modules) {
42
- var targetModule = modules.find(function(m) {
43
- return m.modulePath === loadedRemoteInfo.expose;
44
- });
45
- if (!targetModule) {
46
- return links;
47
- }
40
+ var targetModule = modules.find(function(m) {
41
+ return m.modulePath === loadedRemoteInfo.expose;
42
+ });
43
+ if (!targetModule) {
44
+ return;
45
+ }
46
+ var remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
47
+ if (!remoteEntry) {
48
+ return;
49
+ }
50
+ return {
51
+ module: targetModule,
52
+ publicPath,
53
+ remoteEntry
54
+ };
55
+ }
56
+ function collectAssets(options) {
57
+ var id = options.id, _options_injectLink = options.injectLink, injectLink = _options_injectLink === void 0 ? true : _options_injectLink, _options_injectScript = options.injectScript, injectScript = _options_injectScript === void 0 ? true : _options_injectScript;
58
+ var links = [];
59
+ var scripts = [];
60
+ var instance = getInstance();
61
+ if (!instance || !injectLink && !injectScript) {
62
+ return _to_consumable_array(scripts).concat(_to_consumable_array(links));
63
+ }
64
+ var moduleAndPublicPath = getTargetModuleInfo(id);
65
+ if (!moduleAndPublicPath) {
66
+ return _to_consumable_array(scripts).concat(_to_consumable_array(links));
67
+ }
68
+ var targetModule = moduleAndPublicPath.module, publicPath = moduleAndPublicPath.publicPath, remoteEntry = moduleAndPublicPath.remoteEntry;
69
+ if (injectLink) {
48
70
  _to_consumable_array(targetModule.assets.css.sync).concat(_to_consumable_array(targetModule.assets.css.async)).forEach(function(file, index) {
49
71
  links.push(/* @__PURE__ */ _jsx("link", {
50
72
  href: "".concat(publicPath).concat(file),
51
73
  rel: "stylesheet",
52
74
  type: "text/css"
53
- }, index));
75
+ }, "".concat(file.split(".")[0], "_").concat(index)));
76
+ });
77
+ }
78
+ if (injectScript) {
79
+ scripts.push(/* @__PURE__ */ _jsx("script", {
80
+ async: true,
81
+ src: "".concat(publicPath).concat(remoteEntry),
82
+ crossOrigin: "anonymous"
83
+ }, remoteEntry.split(".")[0]));
84
+ _to_consumable_array(targetModule.assets.js.sync).forEach(function(file, index) {
85
+ scripts.push(/* @__PURE__ */ _jsx("script", {
86
+ async: true,
87
+ src: "".concat(publicPath).concat(file),
88
+ crossOrigin: "anonymous"
89
+ }, "".concat(file.split(".")[0], "_").concat(index)));
54
90
  });
55
91
  }
56
- return links;
92
+ return _to_consumable_array(scripts).concat(_to_consumable_array(links));
57
93
  }
58
94
  function MFReactComponent(props) {
59
95
  var _props_loading = props.loading, loading = _props_loading === void 0 ? "loading..." : _props_loading, id = props.id, fallback = props.fallback;
60
96
  var Component = /* @__PURE__ */ React.lazy(function() {
61
97
  return loadRemote(id).then(function(mod) {
62
- var links = collectLinks(id);
98
+ var assets = collectAssets(props);
63
99
  if (!mod) {
64
100
  throw new Error("load remote failed");
65
101
  }
66
102
  var Com = typeof mod === "object" ? "default" in mod ? mod.default : mod : mod;
67
103
  return {
68
104
  default: function() {
69
- return /* @__PURE__ */ _jsxs("div", {
105
+ return /* @__PURE__ */ _jsxs(_Fragment, {
70
106
  children: [
71
- links,
107
+ assets,
72
108
  /* @__PURE__ */ _jsx(Com, {})
73
109
  ]
74
110
  });
@@ -101,5 +137,5 @@ function MFReactComponent(props) {
101
137
  }
102
138
  export {
103
139
  MFReactComponent,
104
- collectLinks
140
+ collectAssets
105
141
  };
@@ -1,6 +1,6 @@
1
1
  export * from "@module-federation/enhanced/runtime";
2
- import { MFReactComponent, collectLinks } from "./MFReactComponent";
2
+ import { MFReactComponent, collectAssets } from "./MFReactComponent";
3
3
  export {
4
4
  MFReactComponent,
5
- collectLinks
5
+ collectAssets
6
6
  };
@@ -1,4 +1,4 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import React from "react";
3
3
  import { loadRemote, getInstance } from "@module-federation/enhanced/runtime";
4
4
  function getLoadedRemoteInfos(instance, id) {
@@ -17,30 +17,58 @@ function getLoadedRemoteInfos(instance, id) {
17
17
  expose
18
18
  };
19
19
  }
20
- function collectLinks(id) {
21
- const links = [];
20
+ function getTargetModuleInfo(id) {
22
21
  const instance = getInstance();
23
22
  if (!instance) {
24
- return links;
23
+ return;
25
24
  }
26
25
  const loadedRemoteInfo = getLoadedRemoteInfos(instance, id);
27
26
  if (!loadedRemoteInfo) {
28
- return links;
27
+ return;
29
28
  }
30
29
  const snapshot = loadedRemoteInfo.snapshot;
31
30
  if (!snapshot) {
32
- return links;
31
+ return;
33
32
  }
34
33
  const publicPath = "publicPath" in snapshot ? snapshot.publicPath : "getPublicPath" in snapshot ? new Function(snapshot.getPublicPath)() : "";
35
34
  if (!publicPath) {
36
- return links;
35
+ return;
37
36
  }
38
37
  const modules = "modules" in snapshot ? snapshot.modules : [];
39
- if (modules) {
40
- const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
41
- if (!targetModule) {
42
- return links;
43
- }
38
+ const targetModule = modules.find((m) => m.modulePath === loadedRemoteInfo.expose);
39
+ if (!targetModule) {
40
+ return;
41
+ }
42
+ const remoteEntry = "remoteEntry" in snapshot ? snapshot.remoteEntry : "";
43
+ if (!remoteEntry) {
44
+ return;
45
+ }
46
+ return {
47
+ module: targetModule,
48
+ publicPath,
49
+ remoteEntry
50
+ };
51
+ }
52
+ function collectAssets(options) {
53
+ const { id, injectLink = true, injectScript = true } = options;
54
+ const links = [];
55
+ const scripts = [];
56
+ const instance = getInstance();
57
+ if (!instance || !injectLink && !injectScript) {
58
+ return [
59
+ ...scripts,
60
+ ...links
61
+ ];
62
+ }
63
+ const moduleAndPublicPath = getTargetModuleInfo(id);
64
+ if (!moduleAndPublicPath) {
65
+ return [
66
+ ...scripts,
67
+ ...links
68
+ ];
69
+ }
70
+ const { module: targetModule, publicPath, remoteEntry } = moduleAndPublicPath;
71
+ if (injectLink) {
44
72
  [
45
73
  ...targetModule.assets.css.sync,
46
74
  ...targetModule.assets.css.async
@@ -49,23 +77,42 @@ function collectLinks(id) {
49
77
  href: `${publicPath}${file}`,
50
78
  rel: "stylesheet",
51
79
  type: "text/css"
52
- }, index));
80
+ }, `${file.split(".")[0]}_${index}`));
81
+ });
82
+ }
83
+ if (injectScript) {
84
+ scripts.push(/* @__PURE__ */ _jsx("script", {
85
+ async: true,
86
+ src: `${publicPath}${remoteEntry}`,
87
+ crossOrigin: "anonymous"
88
+ }, remoteEntry.split(".")[0]));
89
+ [
90
+ ...targetModule.assets.js.sync
91
+ ].forEach((file, index) => {
92
+ scripts.push(/* @__PURE__ */ _jsx("script", {
93
+ async: true,
94
+ src: `${publicPath}${file}`,
95
+ crossOrigin: "anonymous"
96
+ }, `${file.split(".")[0]}_${index}`));
53
97
  });
54
98
  }
55
- return links;
99
+ return [
100
+ ...scripts,
101
+ ...links
102
+ ];
56
103
  }
57
104
  function MFReactComponent(props) {
58
105
  const { loading = "loading...", id, fallback } = props;
59
106
  const Component = /* @__PURE__ */ React.lazy(() => loadRemote(id).then((mod) => {
60
- const links = collectLinks(id);
107
+ const assets = collectAssets(props);
61
108
  if (!mod) {
62
109
  throw new Error("load remote failed");
63
110
  }
64
111
  const Com = typeof mod === "object" ? "default" in mod ? mod.default : mod : mod;
65
112
  return {
66
- default: () => /* @__PURE__ */ _jsxs("div", {
113
+ default: () => /* @__PURE__ */ _jsxs(_Fragment, {
67
114
  children: [
68
- links,
115
+ assets,
69
116
  /* @__PURE__ */ _jsx(Com, {})
70
117
  ]
71
118
  })
@@ -92,5 +139,5 @@ function MFReactComponent(props) {
92
139
  }
93
140
  export {
94
141
  MFReactComponent,
95
- collectLinks
142
+ collectAssets
96
143
  };
@@ -1,6 +1,6 @@
1
1
  export * from "@module-federation/enhanced/runtime";
2
- import { MFReactComponent, collectLinks } from "./MFReactComponent";
2
+ import { MFReactComponent, collectAssets } from "./MFReactComponent";
3
3
  export {
4
4
  MFReactComponent,
5
- collectLinks
5
+ collectAssets
6
6
  };
@@ -1,9 +1,11 @@
1
1
  import React from 'react';
2
2
  interface IProps {
3
3
  id: string;
4
+ injectScript?: boolean;
5
+ injectLink?: boolean;
4
6
  loading?: React.ReactNode;
5
7
  fallback?: ((err: Error) => React.FC | React.ReactElement) | React.FC | React.ReactElement;
6
8
  }
7
- declare function collectLinks(id: string): React.ReactNode[];
9
+ declare function collectAssets(options: IProps): React.ReactNode[];
8
10
  declare function MFReactComponent(props: IProps): React.JSX.Element;
9
- export { MFReactComponent, collectLinks };
11
+ export { MFReactComponent, collectAssets };
@@ -1,2 +1,2 @@
1
1
  export * from '@module-federation/enhanced/runtime';
2
- export { MFReactComponent, collectLinks } from './MFReactComponent';
2
+ export { MFReactComponent, collectAssets } from './MFReactComponent';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/modern-js",
3
- "version": "0.0.0-next-20240529103203",
3
+ "version": "0.0.0-next-20240530085615",
4
4
  "files": [
5
5
  "dist/",
6
6
  "types.d.ts",
@@ -46,10 +46,10 @@
46
46
  "@modern-js/utils": "^2.49.2",
47
47
  "@modern-js/node-bundle-require": "^2.49.2",
48
48
  "node-fetch": "~3.3.0",
49
- "@module-federation/sdk": "0.0.0-next-20240529103203",
50
- "@module-federation/enhanced": "0.0.0-next-20240529103203",
51
- "@module-federation/node": "0.0.0-next-20240529103203",
52
- "@module-federation/dts-plugin": "0.0.0-next-20240529103203"
49
+ "@module-federation/sdk": "0.0.0-next-20240530085615",
50
+ "@module-federation/enhanced": "0.0.0-next-20240530085615",
51
+ "@module-federation/node": "0.0.0-next-20240530085615",
52
+ "@module-federation/dts-plugin": "0.0.0-next-20240530085615"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@modern-js/app-tools": "^2.49.2",
@@ -57,7 +57,7 @@
57
57
  "@modern-js/runtime": "^2.49.2",
58
58
  "@modern-js/module-tools": "^2.35.0",
59
59
  "@modern-js/tsconfig": "^2.35.0",
60
- "@module-federation/manifest": "0.0.0-next-20240529103203"
60
+ "@module-federation/manifest": "0.0.0-next-20240530085615"
61
61
  },
62
62
  "peerDependencies": {
63
63
  "react": ">=17",