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

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
@@ -86,20 +114,37 @@ function collectLinks(id) {
86
114
  }, index));
87
115
  });
88
116
  }
89
- return links;
117
+ if (injectScript) {
118
+ scripts.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
119
+ src: `${publicPath}${remoteEntry}`,
120
+ crossOrigin: "anonymous"
121
+ }, "remote-entry"));
122
+ [
123
+ ...targetModule.assets.js.sync
124
+ ].forEach((file, index) => {
125
+ scripts.push(/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", {
126
+ src: `${publicPath}${file}`,
127
+ crossOrigin: "anonymous"
128
+ }, index));
129
+ });
130
+ }
131
+ return [
132
+ ...scripts,
133
+ ...links
134
+ ];
90
135
  }
91
136
  function MFReactComponent(props) {
92
137
  const { loading = "loading...", id, fallback } = props;
93
138
  const Component = /* @__PURE__ */ import_react.default.lazy(() => (0, import_runtime.loadRemote)(id).then((mod) => {
94
- const links = collectLinks(id);
139
+ const assets = collectAssets(props);
95
140
  if (!mod) {
96
141
  throw new Error("load remote failed");
97
142
  }
98
143
  const Com = typeof mod === "object" ? "default" in mod ? mod.default : mod : mod;
99
144
  return {
100
- default: () => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
145
+ default: () => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
101
146
  children: [
102
- links,
147
+ assets,
103
148
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Com, {})
104
149
  ]
105
150
  })
@@ -127,5 +172,5 @@ function MFReactComponent(props) {
127
172
  // Annotate the CommonJS export names for ESM import in node:
128
173
  0 && (module.exports = {
129
174
  MFReactComponent,
130
- collectLinks
175
+ collectAssets
131
176
  });
@@ -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,32 +19,54 @@ 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),
@@ -53,22 +75,34 @@ function collectLinks(id) {
53
75
  }, index));
54
76
  });
55
77
  }
56
- return links;
78
+ if (injectScript) {
79
+ scripts.push(/* @__PURE__ */ _jsx("script", {
80
+ src: "".concat(publicPath).concat(remoteEntry),
81
+ crossOrigin: "anonymous"
82
+ }, "remote-entry"));
83
+ _to_consumable_array(targetModule.assets.js.sync).forEach(function(file, index) {
84
+ scripts.push(/* @__PURE__ */ _jsx("script", {
85
+ src: "".concat(publicPath).concat(file),
86
+ crossOrigin: "anonymous"
87
+ }, index));
88
+ });
89
+ }
90
+ return _to_consumable_array(scripts).concat(_to_consumable_array(links));
57
91
  }
58
92
  function MFReactComponent(props) {
59
93
  var _props_loading = props.loading, loading = _props_loading === void 0 ? "loading..." : _props_loading, id = props.id, fallback = props.fallback;
60
94
  var Component = /* @__PURE__ */ React.lazy(function() {
61
95
  return loadRemote(id).then(function(mod) {
62
- var links = collectLinks(id);
96
+ var assets = collectAssets(props);
63
97
  if (!mod) {
64
98
  throw new Error("load remote failed");
65
99
  }
66
100
  var Com = typeof mod === "object" ? "default" in mod ? mod.default : mod : mod;
67
101
  return {
68
102
  default: function() {
69
- return /* @__PURE__ */ _jsxs("div", {
103
+ return /* @__PURE__ */ _jsxs(_Fragment, {
70
104
  children: [
71
- links,
105
+ assets,
72
106
  /* @__PURE__ */ _jsx(Com, {})
73
107
  ]
74
108
  });
@@ -101,5 +135,5 @@ function MFReactComponent(props) {
101
135
  }
102
136
  export {
103
137
  MFReactComponent,
104
- collectLinks
138
+ collectAssets
105
139
  };
@@ -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
@@ -52,20 +80,37 @@ function collectLinks(id) {
52
80
  }, index));
53
81
  });
54
82
  }
55
- return links;
83
+ if (injectScript) {
84
+ scripts.push(/* @__PURE__ */ _jsx("script", {
85
+ src: `${publicPath}${remoteEntry}`,
86
+ crossOrigin: "anonymous"
87
+ }, "remote-entry"));
88
+ [
89
+ ...targetModule.assets.js.sync
90
+ ].forEach((file, index) => {
91
+ scripts.push(/* @__PURE__ */ _jsx("script", {
92
+ src: `${publicPath}${file}`,
93
+ crossOrigin: "anonymous"
94
+ }, index));
95
+ });
96
+ }
97
+ return [
98
+ ...scripts,
99
+ ...links
100
+ ];
56
101
  }
57
102
  function MFReactComponent(props) {
58
103
  const { loading = "loading...", id, fallback } = props;
59
104
  const Component = /* @__PURE__ */ React.lazy(() => loadRemote(id).then((mod) => {
60
- const links = collectLinks(id);
105
+ const assets = collectAssets(props);
61
106
  if (!mod) {
62
107
  throw new Error("load remote failed");
63
108
  }
64
109
  const Com = typeof mod === "object" ? "default" in mod ? mod.default : mod : mod;
65
110
  return {
66
- default: () => /* @__PURE__ */ _jsxs("div", {
111
+ default: () => /* @__PURE__ */ _jsxs(_Fragment, {
67
112
  children: [
68
- links,
113
+ assets,
69
114
  /* @__PURE__ */ _jsx(Com, {})
70
115
  ]
71
116
  })
@@ -92,5 +137,5 @@ function MFReactComponent(props) {
92
137
  }
93
138
  export {
94
139
  MFReactComponent,
95
- collectLinks
140
+ collectAssets
96
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,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-20240530073428",
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-20240530073428",
50
+ "@module-federation/enhanced": "0.0.0-next-20240530073428",
51
+ "@module-federation/node": "0.0.0-next-20240530073428",
52
+ "@module-federation/dts-plugin": "0.0.0-next-20240530073428"
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-20240530073428"
61
61
  },
62
62
  "peerDependencies": {
63
63
  "react": ">=17",