@empjs/share 0.0.1 → 0.1.0
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 +34 -47
- package/dist/adapter.cjs +2 -0
- package/dist/adapter.cjs.map +1 -0
- package/dist/adapter.d.cts +14 -0
- package/dist/adapter.d.ts +14 -0
- package/dist/adapter.js +2 -0
- package/dist/adapter.js.map +1 -0
- package/dist/index-CLt6ciOL.d.cts +21 -0
- package/dist/index-CLt6ciOL.d.ts +21 -0
- package/dist/index.cjs +2 -108
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +2 -81
- package/dist/index.js.map +1 -1
- package/dist/rspack.cjs +2 -104
- package/dist/rspack.cjs.map +1 -1
- package/dist/rspack.d.cts +5 -4
- package/dist/rspack.d.ts +5 -4
- package/dist/rspack.js +2 -81
- package/dist/rspack.js.map +1 -1
- package/dist/runtime.cjs +1 -210
- package/dist/runtime.cjs.map +1 -1
- package/dist/runtime.d.cts +8 -63
- package/dist/runtime.d.ts +8 -63
- package/dist/runtime.js +1 -187
- package/dist/runtime.js.map +1 -1
- package/package.json +33 -6
package/README.md
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
# @empjs/share
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
> @empjs/share/rspack
|
|
2
|
+
## 01 @empjs/share/rspack - Emp Share Plugin
|
|
3
|
+
>
|
|
5
4
|
使用方法
|
|
6
5
|
```js
|
|
7
|
-
import {pluginRspackEmpShare} from '@empjs/share'
|
|
8
|
-
// import pluginRspackEmpShare from '@empjs/share/rspack' // or
|
|
6
|
+
import {pluginRspackEmpShare} from '@empjs/share/rspack'
|
|
9
7
|
import {defineConfig} from '@empjs/cli'
|
|
10
8
|
export default defineConfig(store => {
|
|
11
9
|
return {
|
|
@@ -30,67 +28,56 @@ export default defineConfig(store => {
|
|
|
30
28
|
],
|
|
31
29
|
}
|
|
32
30
|
})
|
|
33
|
-
|
|
34
|
-
|
|
35
31
|
```
|
|
36
32
|
|
|
37
|
-
## 02
|
|
38
|
-
> @empjs/share/library
|
|
39
|
-
|
|
40
|
-
## 03 Runtime
|
|
41
|
-
> @empjs/share/runtime
|
|
42
|
-
|
|
33
|
+
## 02 @empjs/share/library - Module Federation SDK 封装
|
|
43
34
|
|
|
35
|
+
## 03 @empjs/share/runtime - 运行时引用
|
|
44
36
|
### 项目调用
|
|
45
37
|
|
|
46
38
|
```js
|
|
47
|
-
import React, {
|
|
48
|
-
import ReactDOM from
|
|
49
|
-
|
|
50
|
-
import {
|
|
39
|
+
import React, {useEffect, useState, version} from 'react'
|
|
40
|
+
import ReactDOM from 'react-dom'
|
|
41
|
+
import {CountComp as CountComp16, ShowCountComp as ShowCountComp16, Card} from './CountComp'
|
|
42
|
+
import {reactAdapter} from '@empjs/share/adapter' // 使用 react
|
|
43
|
+
import empRuntime from '@empjs/share/runtime' // runtime 加载器
|
|
44
|
+
const entry = process.env.mfhost as string
|
|
51
45
|
// 实例化远程 emp
|
|
52
46
|
empRuntime.init({
|
|
47
|
+
shared: reactAdapter.shared,
|
|
53
48
|
remotes: [
|
|
54
49
|
{
|
|
55
|
-
name:
|
|
56
|
-
entry
|
|
50
|
+
name: 'mfHost',
|
|
51
|
+
entry,
|
|
57
52
|
},
|
|
58
53
|
],
|
|
59
|
-
name:
|
|
60
|
-
})
|
|
54
|
+
name: 'federationRuntimeDemo',
|
|
55
|
+
})
|
|
61
56
|
// 封装 React 18的组件 以便插入到 React 16
|
|
62
|
-
const
|
|
57
|
+
const MfApp = reactAdapter.adapter(empRuntime.load('mfHost/App'))
|
|
58
|
+
const CountComp = reactAdapter.adapter(empRuntime.load('mfHost/CountComp'), 'CountComp')
|
|
59
|
+
|
|
63
60
|
// 创建 React 16 组件
|
|
64
61
|
const ParentComponent = () => {
|
|
65
|
-
const [count, setCount] = useState(0);
|
|
66
62
|
return (
|
|
67
|
-
<
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
>
|
|
74
|
-
<h1>React Version {React.version}!</h1>
|
|
75
|
-
<button onClick={() => setCount(count + 1)}>Click Count {count}</button>
|
|
76
|
-
</div>
|
|
77
|
-
);
|
|
78
|
-
};
|
|
63
|
+
<Card title={() => <>App Inject mfHost</>}>
|
|
64
|
+
<CountComp name="a" />
|
|
65
|
+
<CountComp16 />
|
|
66
|
+
</Card>
|
|
67
|
+
)
|
|
68
|
+
}
|
|
79
69
|
// 封装 React 16的组件 以便插入到 React 18
|
|
80
|
-
const
|
|
81
|
-
ParentComponent,
|
|
82
|
-
"default",
|
|
83
|
-
React,
|
|
84
|
-
ReactDOM
|
|
85
|
-
);
|
|
86
|
-
//
|
|
70
|
+
const ParentComponentAdepter = reactAdapter.adapter(ParentComponent, 'default', React, ReactDOM)
|
|
87
71
|
const App = () => {
|
|
88
72
|
return (
|
|
89
73
|
<>
|
|
90
|
-
<h1>
|
|
91
|
-
<
|
|
74
|
+
<h1>App React Version {version}</h1>
|
|
75
|
+
<ShowCountComp16 />
|
|
76
|
+
<Card title="EMP From mfhost">
|
|
77
|
+
<MfApp component={ParentComponentAdepter} />
|
|
78
|
+
</Card>
|
|
92
79
|
</>
|
|
93
|
-
)
|
|
94
|
-
}
|
|
95
|
-
export default App
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
export default App
|
|
96
83
|
```
|
package/dist/adapter.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var c=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var x=(t,e)=>{for(var r in e)c(t,r,{get:e[r],enumerable:!0})},C=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of P(e))!v.call(t,i)&&i!==r&&c(t,i,{get:()=>e[i],enumerable:!(n=g(e,i))||n.enumerable});return t};var O=t=>C(c({},"__esModule",{value:!0}),t);var M={};x(M,{default:()=>w,reactAdapter:()=>l});module.exports=O(M);var m=t=>t?Number(t.split(".")[0]):0,d=t=>t&&Object.prototype.toString.call(t)==="[object Promise]";var y={},b=window,{EMPShareGlobalVal:p}=b||{};p&&p.frameworkLib&&(y=b[p.frameworkLib]);var u=class{libs={scope:"default",...y};constructor(e){e&&this.setup(e)}setup(e){e&&(typeof e=="string"&&(e=window[e]),this.libs={...this.libs,...e})}get shared(){let{React:e,ReactDOM:r,scope:n}=this.libs;return{react:{lib:()=>e,version:e.version,scope:n,shareConfig:{singleton:!0,requiredVersion:`^${e.version}`}},"react-dom":{lib:()=>r,version:r.version,scope:n,shareConfig:{singleton:!0,requiredVersion:`^${e.version}`}}}}adapter(e,r=this.libs.scope,n=this.libs.React,i=this.libs.ReactDOM){let h=m(n.version),f=this;class R extends n.Component{containerRef;root;constructor(s){super(s),this.containerRef=n.createRef()}componentDidMount(){this.mountOriginalComponent(!0)}componentDidUpdate(){this.mountOriginalComponent()}componentWillUnmount(){this.unMountOriginalComponent()}unMountOriginalComponent(){this.containerRef.current&&(h<18?i.unmountComponentAtNode(this.containerRef.current):this.root.unmount())}async mountOriginalComponent(s){d(e)&&(e=await e.then(o=>o[r]));let a=n.createElement(e,this.props);if(h<18)(s?i.render:i.hydrate)(a,this.containerRef.current);else if(s){let{createRoot:o}=f.libs;this.root=o(this.containerRef.current),this.root.render(a)}else{let{hydrateRoot:o}=f.libs;this.root=o(this.containerRef.current,a)}}render(){return n.createElement("div",{ref:this.containerRef})}}return R}},l=new u;var w=l;0&&(module.exports={reactAdapter});
|
|
2
|
+
//# sourceMappingURL=adapter.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/adapter/index.ts","../src/helper/index.ts","../src/adapter/react.tsx"],"sourcesContent":["import reactAdapter from 'src/adapter/react'\nexport {reactAdapter}\nexport default reactAdapter\n","export const importJsVm = (content: string) => `data:text/javascript,${content}`\n\nexport function deepAssign<T>(target: any, ...sources: any): T {\n for (const source of sources) {\n for (const k in source) {\n const vs = source[k],\n vt = target[k]\n if (Object(vs) == vs && Object(vt) === vt) {\n target[k] = deepAssign(vt, vs)\n continue\n }\n target[k] = source[k]\n }\n }\n return target\n}\n\nexport const checkVersion = (version: string) => (version ? Number(version.split('.')[0]) : 0)\nexport const isPromise = (p: any) => p && Object.prototype.toString.call(p) === '[object Promise]'\n","import {checkVersion, isPromise} from 'src/helper'\nimport type {EMPShareRuntimeAdapterReactType, InitOptionsType} from 'src/types'\n//\nlet globalLib = {}\nconst win: any = window\nconst {EMPShareGlobalVal} = win || {}\nif (EMPShareGlobalVal && EMPShareGlobalVal.frameworkLib) {\n globalLib = win[EMPShareGlobalVal.frameworkLib]\n}\n//\nexport class ReactAdapter {\n libs: EMPShareRuntimeAdapterReactType = {\n scope: 'default',\n ...globalLib,\n }\n constructor(op?: EMPShareRuntimeAdapterReactType) {\n if (op) this.setup(op)\n }\n public setup(o?: EMPShareRuntimeAdapterReactType | string) {\n if (o) {\n if (typeof o === 'string') o = window[o]\n this.libs = {...this.libs, ...(o as any)}\n }\n }\n get shared(): InitOptionsType['shared'] {\n const {React, ReactDOM, scope} = this.libs as EMPShareRuntimeAdapterReactType\n return {\n react: {\n lib: () => React,\n version: React.version,\n scope,\n shareConfig: {\n singleton: true,\n requiredVersion: `^${React.version}`,\n },\n },\n 'react-dom': {\n lib: () => ReactDOM,\n version: ReactDOM.version,\n scope,\n shareConfig: {\n singleton: true,\n requiredVersion: `^${React.version}`,\n },\n },\n }\n }\n adapter<P = any>(\n component: any,\n scope: string = this.libs.scope,\n React: any = this.libs.React,\n ReactDOM: any = this.libs.ReactDOM,\n ): P {\n const reactVersion = checkVersion(React.version)\n const self = this\n class WrappedComponent extends React.Component {\n public containerRef: any\n public root: any\n constructor(props: P) {\n super(props as any)\n this.containerRef = React.createRef()\n }\n\n componentDidMount() {\n this.mountOriginalComponent(true)\n }\n\n componentDidUpdate() {\n this.mountOriginalComponent()\n }\n\n componentWillUnmount() {\n this.unMountOriginalComponent()\n }\n unMountOriginalComponent() {\n if (!this.containerRef.current) return\n if (reactVersion < 18) {\n ReactDOM.unmountComponentAtNode(this.containerRef.current)\n } else {\n this.root.unmount()\n }\n }\n async mountOriginalComponent(shouldRender?: boolean) {\n if (isPromise(component))\n component = await component.then((m: any) => {\n return m[scope]\n })\n const element = React.createElement(component, this.props)\n if (reactVersion < 18) {\n const Render = shouldRender ? ReactDOM.render : ReactDOM.hydrate\n Render(element, this.containerRef.current)\n } else {\n if (shouldRender) {\n const {createRoot} = self.libs\n this.root = createRoot(this.containerRef.current!)\n this.root.render(element)\n } else {\n const {hydrateRoot} = self.libs\n this.root = hydrateRoot(this.containerRef.current!, element)\n }\n }\n }\n\n render() {\n return <div ref={this.containerRef} />\n }\n }\n return WrappedComponent as any\n }\n}\n\nexport default new ReactAdapter()\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,iBAAAC,IAAA,eAAAC,EAAAJ,GCiBO,IAAMK,EAAgBC,GAAqBA,EAAU,OAAOA,EAAQ,MAAM,GAAG,EAAE,CAAC,CAAC,EAAI,EAC/EC,EAAaC,GAAWA,GAAK,OAAO,UAAU,SAAS,KAAKA,CAAC,IAAM,mBCfhF,IAAIC,EAAY,CAAC,EACXC,EAAW,OACX,CAAC,kBAAAC,CAAiB,EAAID,GAAO,CAAC,EAChCC,GAAqBA,EAAkB,eACzCF,EAAYC,EAAIC,EAAkB,YAAY,GAGzC,IAAMC,EAAN,KAAmB,CACxB,KAAwC,CACtC,MAAO,UACP,GAAGH,CACL,EACA,YAAYI,EAAsC,CAC5CA,GAAI,KAAK,MAAMA,CAAE,CACvB,CACO,MAAMC,EAA8C,CACrDA,IACE,OAAOA,GAAM,WAAUA,EAAI,OAAOA,CAAC,GACvC,KAAK,KAAO,CAAC,GAAG,KAAK,KAAM,GAAIA,CAAS,EAE5C,CACA,IAAI,QAAoC,CACtC,GAAM,CAAC,MA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,CAAC,YAAAE,CAAW,EAAIR,EAAK,KAC3B,KAAK,KAAOQ,EAAY,KAAK,aAAa,QAAUF,CAAO,CAC7D,CAEJ,CAEA,QAAS,CACP,OAAOZ,EAAA,cAAC,OAAI,IAAK,KAAK,aAAc,CACtC,CACF,CACA,OAAOO,CACT,CACF,EAEOQ,EAAQ,IAAIlB,EF7GnB,IAAOmB,EAAQC","names":["adapter_exports","__export","adapter_default","react_default","__toCommonJS","checkVersion","version","isPromise","p","globalLib","win","EMPShareGlobalVal","ReactAdapter","op","o","React","ReactDOM","scope","component","reactVersion","checkVersion","self","WrappedComponent","props","shouldRender","isPromise","m","element","createRoot","hydrateRoot","react_default","adapter_default","react_default"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { b as EMPShareRuntimeAdapterReactType, I as InitOptionsType } from './index-CLt6ciOL.cjs';
|
|
2
|
+
import '@module-federation/sdk';
|
|
3
|
+
import '@module-federation/enhanced/runtime';
|
|
4
|
+
|
|
5
|
+
declare class ReactAdapter {
|
|
6
|
+
libs: EMPShareRuntimeAdapterReactType;
|
|
7
|
+
constructor(op?: EMPShareRuntimeAdapterReactType);
|
|
8
|
+
setup(o?: EMPShareRuntimeAdapterReactType | string): void;
|
|
9
|
+
get shared(): InitOptionsType['shared'];
|
|
10
|
+
adapter<P = any>(component: any, scope?: string, React?: any, ReactDOM?: any): P;
|
|
11
|
+
}
|
|
12
|
+
declare const _default: ReactAdapter;
|
|
13
|
+
|
|
14
|
+
export { _default as default, _default as reactAdapter };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { b as EMPShareRuntimeAdapterReactType, I as InitOptionsType } from './index-CLt6ciOL.js';
|
|
2
|
+
import '@module-federation/sdk';
|
|
3
|
+
import '@module-federation/enhanced/runtime';
|
|
4
|
+
|
|
5
|
+
declare class ReactAdapter {
|
|
6
|
+
libs: EMPShareRuntimeAdapterReactType;
|
|
7
|
+
constructor(op?: EMPShareRuntimeAdapterReactType);
|
|
8
|
+
setup(o?: EMPShareRuntimeAdapterReactType | string): void;
|
|
9
|
+
get shared(): InitOptionsType['shared'];
|
|
10
|
+
adapter<P = any>(component: any, scope?: string, React?: any, ReactDOM?: any): P;
|
|
11
|
+
}
|
|
12
|
+
declare const _default: ReactAdapter;
|
|
13
|
+
|
|
14
|
+
export { _default as default, _default as reactAdapter };
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var h=n=>n?Number(n.split(".")[0]):0,f=n=>n&&Object.prototype.toString.call(n)==="[object Promise]";var m={},d=window,{EMPShareGlobalVal:c}=d||{};c&&c.frameworkLib&&(m=d[c.frameworkLib]);var p=class{libs={scope:"default",...m};constructor(e){e&&this.setup(e)}setup(e){e&&(typeof e=="string"&&(e=window[e]),this.libs={...this.libs,...e})}get shared(){let{React:e,ReactDOM:i,scope:t}=this.libs;return{react:{lib:()=>e,version:e.version,scope:t,shareConfig:{singleton:!0,requiredVersion:`^${e.version}`}},"react-dom":{lib:()=>i,version:i.version,scope:t,shareConfig:{singleton:!0,requiredVersion:`^${e.version}`}}}}adapter(e,i=this.libs.scope,t=this.libs.React,s=this.libs.ReactDOM){let u=h(t.version),l=this;class b extends t.Component{containerRef;root;constructor(o){super(o),this.containerRef=t.createRef()}componentDidMount(){this.mountOriginalComponent(!0)}componentDidUpdate(){this.mountOriginalComponent()}componentWillUnmount(){this.unMountOriginalComponent()}unMountOriginalComponent(){this.containerRef.current&&(u<18?s.unmountComponentAtNode(this.containerRef.current):this.root.unmount())}async mountOriginalComponent(o){f(e)&&(e=await e.then(r=>r[i]));let a=t.createElement(e,this.props);if(u<18)(o?s.render:s.hydrate)(a,this.containerRef.current);else if(o){let{createRoot:r}=l.libs;this.root=r(this.containerRef.current),this.root.render(a)}else{let{hydrateRoot:r}=l.libs;this.root=r(this.containerRef.current,a)}}render(){return t.createElement("div",{ref:this.containerRef})}}return b}},y=new p;var C=y;export{C as default,y as reactAdapter};
|
|
2
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/helper/index.ts","../src/adapter/react.tsx","../src/adapter/index.ts"],"sourcesContent":["export const importJsVm = (content: string) => `data:text/javascript,${content}`\n\nexport function deepAssign<T>(target: any, ...sources: any): T {\n for (const source of sources) {\n for (const k in source) {\n const vs = source[k],\n vt = target[k]\n if (Object(vs) == vs && Object(vt) === vt) {\n target[k] = deepAssign(vt, vs)\n continue\n }\n target[k] = source[k]\n }\n }\n return target\n}\n\nexport const checkVersion = (version: string) => (version ? Number(version.split('.')[0]) : 0)\nexport const isPromise = (p: any) => p && Object.prototype.toString.call(p) === '[object Promise]'\n","import {checkVersion, isPromise} from 'src/helper'\nimport type {EMPShareRuntimeAdapterReactType, InitOptionsType} from 'src/types'\n//\nlet globalLib = {}\nconst win: any = window\nconst {EMPShareGlobalVal} = win || {}\nif (EMPShareGlobalVal && EMPShareGlobalVal.frameworkLib) {\n globalLib = win[EMPShareGlobalVal.frameworkLib]\n}\n//\nexport class ReactAdapter {\n libs: EMPShareRuntimeAdapterReactType = {\n scope: 'default',\n ...globalLib,\n }\n constructor(op?: EMPShareRuntimeAdapterReactType) {\n if (op) this.setup(op)\n }\n public setup(o?: EMPShareRuntimeAdapterReactType | string) {\n if (o) {\n if (typeof o === 'string') o = window[o]\n this.libs = {...this.libs, ...(o as any)}\n }\n }\n get shared(): InitOptionsType['shared'] {\n const {React, ReactDOM, scope} = this.libs as EMPShareRuntimeAdapterReactType\n return {\n react: {\n lib: () => React,\n version: React.version,\n scope,\n shareConfig: {\n singleton: true,\n requiredVersion: `^${React.version}`,\n },\n },\n 'react-dom': {\n lib: () => ReactDOM,\n version: ReactDOM.version,\n scope,\n shareConfig: {\n singleton: true,\n requiredVersion: `^${React.version}`,\n },\n },\n }\n }\n adapter<P = any>(\n component: any,\n scope: string = this.libs.scope,\n React: any = this.libs.React,\n ReactDOM: any = this.libs.ReactDOM,\n ): P {\n const reactVersion = checkVersion(React.version)\n const self = this\n class WrappedComponent extends React.Component {\n public containerRef: any\n public root: any\n constructor(props: P) {\n super(props as any)\n this.containerRef = React.createRef()\n }\n\n componentDidMount() {\n this.mountOriginalComponent(true)\n }\n\n componentDidUpdate() {\n this.mountOriginalComponent()\n }\n\n componentWillUnmount() {\n this.unMountOriginalComponent()\n }\n unMountOriginalComponent() {\n if (!this.containerRef.current) return\n if (reactVersion < 18) {\n ReactDOM.unmountComponentAtNode(this.containerRef.current)\n } else {\n this.root.unmount()\n }\n }\n async mountOriginalComponent(shouldRender?: boolean) {\n if (isPromise(component))\n component = await component.then((m: any) => {\n return m[scope]\n })\n const element = React.createElement(component, this.props)\n if (reactVersion < 18) {\n const Render = shouldRender ? ReactDOM.render : ReactDOM.hydrate\n Render(element, this.containerRef.current)\n } else {\n if (shouldRender) {\n const {createRoot} = self.libs\n this.root = createRoot(this.containerRef.current!)\n this.root.render(element)\n } else {\n const {hydrateRoot} = self.libs\n this.root = hydrateRoot(this.containerRef.current!, element)\n }\n }\n }\n\n render() {\n return <div ref={this.containerRef} />\n }\n }\n return WrappedComponent as any\n }\n}\n\nexport default new ReactAdapter()\n","import reactAdapter from 'src/adapter/react'\nexport {reactAdapter}\nexport default reactAdapter\n"],"mappings":"AAiBO,IAAMA,EAAgBC,GAAqBA,EAAU,OAAOA,EAAQ,MAAM,GAAG,EAAE,CAAC,CAAC,EAAI,EAC/EC,EAAaC,GAAWA,GAAK,OAAO,UAAU,SAAS,KAAKA,CAAC,IAAM,mBCfhF,IAAIC,EAAY,CAAC,EACXC,EAAW,OACX,CAAC,kBAAAC,CAAiB,EAAID,GAAO,CAAC,EAChCC,GAAqBA,EAAkB,eACzCF,EAAYC,EAAIC,EAAkB,YAAY,GAGzC,IAAMC,EAAN,KAAmB,CACxB,KAAwC,CACtC,MAAO,UACP,GAAGH,CACL,EACA,YAAYI,EAAsC,CAC5CA,GAAI,KAAK,MAAMA,CAAE,CACvB,CACO,MAAMC,EAA8C,CACrDA,IACE,OAAOA,GAA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AC,WAAAI,CAAU,EAAIP,EAAK,KAC1B,KAAK,KAAOO,EAAW,KAAK,aAAa,OAAQ,EACjD,KAAK,KAAK,OAAOD,CAAO,CAC1B,KAAO,CACL,GAAM,CAAC,YAAAE,CAAW,EAAIR,EAAK,KAC3B,KAAK,KAAOQ,EAAY,KAAK,aAAa,QAAUF,CAAO,CAC7D,CAEJ,CAEA,QAAS,CACP,OAAOZ,EAAA,cAAC,OAAI,IAAK,KAAK,aAAc,CACtC,CACF,CACA,OAAOO,CACT,CACF,EAEOQ,EAAQ,IAAIlB,EC7GnB,IAAOmB,EAAQC","names":["checkVersion","version","isPromise","p","globalLib","win","EMPShareGlobalVal","ReactAdapter","op","o","React","ReactDOM","scope","component","reactVersion","checkVersion","self","WrappedComponent","props","shouldRender","isPromise","m","element","createRoot","hydrateRoot","react_default","adapter_default","react_default"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import MFSDK from '@module-federation/sdk';
|
|
2
|
+
import MFRuntime__default from '@module-federation/enhanced/runtime';
|
|
3
|
+
|
|
4
|
+
type EMPShareRuntimeType = {
|
|
5
|
+
MFRuntime: Required<typeof MFRuntime__default>;
|
|
6
|
+
MFSDK: Required<typeof MFSDK>;
|
|
7
|
+
};
|
|
8
|
+
type EMPShareRuntimeAdapterReactType = {
|
|
9
|
+
React?: any;
|
|
10
|
+
ReactDOM?: any;
|
|
11
|
+
createRoot?: any;
|
|
12
|
+
hydrateRoot?: any;
|
|
13
|
+
scope: string;
|
|
14
|
+
};
|
|
15
|
+
type InitOptionsType = Parameters<typeof MFRuntime__default.init>[0];
|
|
16
|
+
type LoadRemoteType = Parameters<typeof MFRuntime__default.loadRemote>;
|
|
17
|
+
type EmpRuntimeOptions = {
|
|
18
|
+
showLog?: boolean;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type { EMPShareRuntimeType as E, InitOptionsType as I, LoadRemoteType as L, EmpRuntimeOptions as a, EMPShareRuntimeAdapterReactType as b };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import MFSDK from '@module-federation/sdk';
|
|
2
|
+
import MFRuntime__default from '@module-federation/enhanced/runtime';
|
|
3
|
+
|
|
4
|
+
type EMPShareRuntimeType = {
|
|
5
|
+
MFRuntime: Required<typeof MFRuntime__default>;
|
|
6
|
+
MFSDK: Required<typeof MFSDK>;
|
|
7
|
+
};
|
|
8
|
+
type EMPShareRuntimeAdapterReactType = {
|
|
9
|
+
React?: any;
|
|
10
|
+
ReactDOM?: any;
|
|
11
|
+
createRoot?: any;
|
|
12
|
+
hydrateRoot?: any;
|
|
13
|
+
scope: string;
|
|
14
|
+
};
|
|
15
|
+
type InitOptionsType = Parameters<typeof MFRuntime__default.init>[0];
|
|
16
|
+
type LoadRemoteType = Parameters<typeof MFRuntime__default.loadRemote>;
|
|
17
|
+
type EmpRuntimeOptions = {
|
|
18
|
+
showLog?: boolean;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type { EMPShareRuntimeType as E, InitOptionsType as I, LoadRemoteType as L, EmpRuntimeOptions as a, EMPShareRuntimeAdapterReactType as b };
|
package/dist/index.cjs
CHANGED
|
@@ -1,109 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
pluginRspackEmpShare: () => rspack_default
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
|
|
27
|
-
// src/plugin/rspack/index.ts
|
|
28
|
-
var import_rspack = require("@module-federation/enhanced/rspack");
|
|
29
|
-
|
|
30
|
-
// src/helper/config.ts
|
|
31
|
-
var shareGlobalName = "EMP_SHARE_RUNTIME";
|
|
32
|
-
|
|
33
|
-
// src/helper/index.ts
|
|
34
|
-
var importJsVm = (content) => `data:text/javascript,${content}`;
|
|
35
|
-
|
|
36
|
-
// src/plugin/rspack/plugin.ts
|
|
37
|
-
var getShareRuntimeEntry = () => {
|
|
38
|
-
const content = [`import '@empjs/share/library'`].join("\n");
|
|
39
|
-
return importJsVm(content);
|
|
40
|
-
};
|
|
41
|
-
var EmpShareRuntimePlugin = class {
|
|
42
|
-
constructor() {
|
|
43
|
-
}
|
|
44
|
-
apply(compiler) {
|
|
45
|
-
const { webpack } = compiler;
|
|
46
|
-
const entry = getShareRuntimeEntry();
|
|
47
|
-
new webpack.EntryPlugin(compiler.context, entry, {
|
|
48
|
-
name: void 0
|
|
49
|
-
}).apply(compiler);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// src/plugin/rspack/index.ts
|
|
54
|
-
var rspack_default = (o = {}) => {
|
|
55
|
-
return {
|
|
56
|
-
name: "@empjs/share",
|
|
57
|
-
async rsConfig(store) {
|
|
58
|
-
console.log(o);
|
|
59
|
-
if (o.name) {
|
|
60
|
-
const op = store.deepAssign(
|
|
61
|
-
{
|
|
62
|
-
filename: "emp.js",
|
|
63
|
-
manifest: false,
|
|
64
|
-
dts: false,
|
|
65
|
-
dev: {
|
|
66
|
-
disableDynamicRemoteTypeHints: true
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
o
|
|
70
|
-
);
|
|
71
|
-
delete op.empRuntime;
|
|
72
|
-
store.chain.plugin("plugin-emp-share").use(import_rspack.ModuleFederationPlugin, [op]);
|
|
73
|
-
}
|
|
74
|
-
const { empRuntime } = o;
|
|
75
|
-
if (empRuntime) {
|
|
76
|
-
if (!empRuntime.runtimeLib) {
|
|
77
|
-
store.chain.plugin("plugin-emp-runtime").use(EmpShareRuntimePlugin);
|
|
78
|
-
}
|
|
79
|
-
empRuntime.runtimeGlobal = empRuntime.frameworkGlobal || shareGlobalName;
|
|
80
|
-
const externalRuntime = {
|
|
81
|
-
"@module-federation/runtime": `MFRuntime`,
|
|
82
|
-
"@module-federation/sdk": `MFSDK`
|
|
83
|
-
};
|
|
84
|
-
const externals = [];
|
|
85
|
-
for (const [key, value] of Object.entries(externalRuntime)) {
|
|
86
|
-
externals[key] = `${empRuntime.runtimeGlobal}.${value}`;
|
|
87
|
-
}
|
|
88
|
-
empRuntime.framework = empRuntime.framework || "react";
|
|
89
|
-
empRuntime.frameworkGlobal = empRuntime.frameworkGlobal || empRuntime.runtimeGlobal;
|
|
90
|
-
if (empRuntime.framework === "react") {
|
|
91
|
-
const externalReact = {
|
|
92
|
-
react: `React`,
|
|
93
|
-
"react-dom": `ReactDOM`
|
|
94
|
-
};
|
|
95
|
-
for (const [key, value] of Object.entries(externalReact)) {
|
|
96
|
-
externals[key] = `${empRuntime.frameworkGlobal}.${value}`;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
if (empRuntime.setExternals) empRuntime.setExternals(externals);
|
|
100
|
-
store.merge({ externals });
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
};
|
|
105
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
106
|
-
0 && (module.exports = {
|
|
107
|
-
pluginRspackEmpShare
|
|
108
|
-
});
|
|
1
|
+
"use strict";var u=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var S=(t,e)=>{for(var n in e)u(t,n,{get:e[n],enumerable:!0})},P=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of k(e))!x.call(t,r)&&r!==n&&u(t,r,{get:()=>e[r],enumerable:!(o=h(e,r))||o.enumerable});return t};var M=t=>P(u({},"__esModule",{value:!0}),t);var H={};S(H,{default:()=>G,pluginRspackEmpShare:()=>f});module.exports=M(H);var E=require("@module-federation/enhanced/rspack");var y="EMP_SHARE_RUNTIME";var d=t=>`data:text/javascript,${t}`;var v=t=>{let e=[`import '${t}'`].join(`
|
|
2
|
+
`);return d(e)},m=class{options;constructor(e){this.options=e}apply(e){let{webpack:n}=e,{runtimeLib:o}=this.options;if(!o){let r=v("@empjs/share/library");new n.EntryPlugin(e.context,r,{name:void 0}).apply(e)}}};function R(t,e){return typeof t=="string"?`${t}/runtime${e==="development"?".development":""}.umd.js`:typeof t=="object"?e==="development"?t.dev:t.prod:""}var w=(t={})=>({name:"@empjs/share",async rsConfig(e){if(t.name){let o=e.deepAssign({filename:"emp.js",manifest:!1,dts:!1,dev:{disableDynamicRemoteTypeHints:!0}},t);delete o.empRuntime,e.chain.plugin("plugin-emp-share").use(E.ModuleFederationPlugin,[o])}let{empRuntime:n}=t;if(n){let o=[],r=n.framework||"react",{frameworkLib:b,runtimeLib:p}=n,l=n.runtimeGlobal||y,a=n.frameworkGlobal||"";if(n.injectGlobalValToHtml!==!1&&o.push({type:"script",value:`EMPShareGlobalVal={frameworkLib:'${a}',runtimeLib:'${l}',framework:'${r}'}`}),b){let i=R(b,e.mode);o.push({type:"js",value:i})}p?p==="useFrameworkLib"?l=a:o.push({type:"js",value:p}):e.chain.plugin("plugin-emp-share-framework").use(m,[n]),o.length>0&&e.injectHeadTags(o,"EMPShare");let g={"@module-federation/runtime":"MFRuntime","@module-federation/sdk":"MFSDK"},s={};for(let[i,c]of Object.entries(g))s[i]=`${l}.${c}`;if(r==="react"&&a){let i={react:"React","react-dom":"ReactDOM"};for(let[c,j]of Object.entries(i))s[c]=`${a}.${j}`}n.setExternals&&n.setExternals(s),e.chain.merge({externals:s})}}}),f=w;var G=f;0&&(module.exports={pluginRspackEmpShare});
|
|
109
3
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/plugins/rspack/index.ts","../src/helper/config.ts","../src/helper/index.ts","../src/plugins/rspack/plugin.ts","../src/plugins/rspack/utils.ts"],"sourcesContent":["import {default as pluginRspackEmpShare} from 'src/plugins/rspack'\nexport default pluginRspackEmpShare\nexport {pluginRspackEmpShare}\n","import type {GlobalStore} from '@empjs/cli'\nimport {ModuleFederationPlugin} from '@module-federation/enhanced/rspack'\nimport {shareGlobalName} from 'src/helper/config'\nimport {EmpShareRemoteLibPlugin} from './plugin'\nimport type {EMPPluginShareType} from './types'\nimport {getRuntimeLib} from './utils'\n//\nexport const pluginRspackEmpShare = (o: EMPPluginShareType = {}) => {\n return {\n name: '@empjs/share',\n async rsConfig(store: GlobalStore) {\n if (o.name) {\n const op: any = store.deepAssign(\n {\n filename: 'emp.js',\n manifest: false,\n dts: false,\n dev: {\n disableDynamicRemoteTypeHints: true,\n },\n },\n o,\n )\n delete op.empRuntime\n store.chain.plugin('plugin-emp-share').use(ModuleFederationPlugin, [op])\n }\n const {empRuntime} = o\n if (empRuntime) {\n const injectHtml: any[] = []\n const framework = empRuntime.framework || 'react'\n const {frameworkLib, runtimeLib} = empRuntime\n //\n let runtimeGlobal = empRuntime.runtimeGlobal || shareGlobalName\n const frameworkGlobal = empRuntime.frameworkGlobal || ''\n const injectGlobalValToHtml = empRuntime.injectGlobalValToHtml === false ? false : true\n // 插入全局引用变量 让 empRuntime 可以提前实例化 不需要在页面重复实例化\n if (injectGlobalValToHtml) {\n injectHtml.push({\n type: 'script',\n value: `EMPShareGlobalVal={frameworkLib:'${frameworkGlobal}',runtimeLib:'${runtimeGlobal}',framework:'${framework}'}`,\n })\n }\n /**\n * frameworkLib & runtimeLib 的全局引用\n */\n if (frameworkLib) {\n const url = getRuntimeLib(frameworkLib, store.mode)\n injectHtml.push({\n type: 'js',\n value: url,\n })\n }\n if (!runtimeLib) {\n // 内置 runtimeLib\n store.chain.plugin('plugin-emp-share-framework').use(EmpShareRemoteLibPlugin, [empRuntime])\n }\n // 统一封装到 frameworkLib\n else if (runtimeLib === 'useFrameworkLib') {\n runtimeGlobal = frameworkGlobal\n } else {\n // 插入 runtime\n injectHtml.push({type: 'js', value: runtimeLib})\n }\n if (injectHtml.length > 0) {\n store.injectHeadTags(injectHtml, 'EMPShare')\n }\n /**\n * 设置 externals\n */\n const externalRuntime = {\n '@module-federation/runtime': `MFRuntime`,\n '@module-federation/sdk': `MFSDK`,\n }\n const externals = {}\n for (const [key, value] of Object.entries(externalRuntime)) {\n externals[key] = `${runtimeGlobal}.${value}`\n }\n if (framework === 'react' && frameworkGlobal) {\n const externalReact = {\n react: `React`,\n 'react-dom': `ReactDOM`,\n }\n for (const [key, value] of Object.entries(externalReact)) {\n externals[key] = `${frameworkGlobal}.${value}`\n }\n }\n if (empRuntime.setExternals) empRuntime.setExternals(externals)\n //\n // console.log('externals in plugin', externals)\n store.chain.merge({externals})\n }\n },\n }\n}\n\nexport default pluginRspackEmpShare\n","export const shareGlobalName = 'EMP_SHARE_RUNTIME'\n","export const importJsVm = (content: string) => `data:text/javascript,${content}`\n\nexport function deepAssign<T>(target: any, ...sources: any): T {\n for (const source of sources) {\n for (const k in source) {\n const vs = source[k],\n vt = target[k]\n if (Object(vs) == vs && Object(vt) === vt) {\n target[k] = deepAssign(vt, vs)\n continue\n }\n target[k] = source[k]\n }\n }\n return target\n}\n\nexport const checkVersion = (version: string) => (version ? Number(version.split('.')[0]) : 0)\nexport const isPromise = (p: any) => p && Object.prototype.toString.call(p) === '[object Promise]'\n","import type {Compiler} from '@rspack/core'\nimport {importJsVm} from 'src/helper'\nimport type {EMPSHARERuntimeOptions} from './types'\nconst getShareRuntimeEntry = url => {\n const content = [`import '${url}'`].join('\\n')\n return importJsVm(content)\n}\nexport class EmpShareRemoteLibPlugin {\n options: EMPSHARERuntimeOptions\n constructor(op: EMPSHARERuntimeOptions) {\n this.options = op\n }\n apply(compiler: Compiler) {\n const {webpack} = compiler\n const {runtimeLib} = this.options\n if (!runtimeLib) {\n const entry = getShareRuntimeEntry('@empjs/share/library')\n new webpack.EntryPlugin(compiler.context, entry, {\n name: undefined,\n }).apply(compiler)\n }\n }\n}\n","import type {EMPSHARERuntimeOptions} from './types'\nexport function getRuntimeLib(host: EMPSHARERuntimeOptions['frameworkLib'], mode: string) {\n if (typeof host === 'string') return `${host}/runtime${mode === 'development' ? '.development' : ''}.umd.js`\n else if (typeof host === 'object') {\n if (mode === 'development') return host.dev\n else return host.prod\n }\n return ''\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,yBAAAC,IAAA,eAAAC,EAAAJ,GCCA,IAAAK,EAAqC,8CCD9B,IAAMC,EAAkB,oBCAxB,IAAMC,EAAcC,GAAoB,wBAAwBA,CAAO,GCG9E,IAAMC,EAAuBC,GAAO,CAClC,IAAMC,EAAU,CAAC,WAAWD,CAAG,GAAG,EAAE,KAAK;AAAA,CAAI,EAC7C,OAAOE,EAAWD,CAAO,CAC3B,EACaE,EAAN,KAA8B,CACnC,QACA,YAAYC,EAA4B,CACtC,KAAK,QAAUA,CACjB,CACA,MAAMC,EAAoB,CACxB,GAAM,CAAC,QAAAC,CAAO,EAAID,EACZ,CAAC,WAAAE,CAAU,EAAI,KAAK,QAC1B,GAAI,CAACA,EAAY,CACf,IAAMC,EAAQT,EAAqB,sBAAsB,EACzD,IAAIO,EAAQ,YAAYD,EAAS,QAASG,EAAO,CAC/C,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AU,EAK7C,IAAMU,EAAkB,CACtB,6BAA8B,YAC9B,yBAA0B,OAC5B,EACMC,EAAY,CAAC,EACnB,OAAW,CAACC,EAAKC,CAAK,IAAK,OAAO,QAAQH,CAAe,EACvDC,EAAUC,CAAG,EAAI,GAAGR,CAAa,IAAIS,CAAK,GAE5C,GAAIZ,IAAc,SAAWK,EAAiB,CAC5C,IAAMQ,EAAgB,CACpB,MAAO,QACP,YAAa,UACf,EACA,OAAW,CAACF,EAAKC,CAAK,IAAK,OAAO,QAAQC,CAAa,EACrDH,EAAUC,CAAG,EAAI,GAAGN,CAAe,IAAIO,CAAK,EAEhD,CACId,EAAW,cAAcA,EAAW,aAAaY,CAAS,EAG9Dd,EAAM,MAAM,MAAM,CAAC,UAAAc,CAAS,CAAC,CAC/B,CACF,CACF,GAGKI,EAAQpB,ED9Ff,IAAOqB,EAAQC","names":["src_exports","__export","src_default","rspack_default","__toCommonJS","import_rspack","shareGlobalName","importJsVm","content","getShareRuntimeEntry","url","content","importJsVm","EmpShareRemoteLibPlugin","op","compiler","webpack","runtimeLib","entry","getRuntimeLib","host","mode","pluginRspackEmpShare","o","store","op","empRuntime","injectHtml","framework","frameworkLib","runtimeLib","runtimeGlobal","shareGlobalName","frameworkGlobal","url","getRuntimeLib","EmpShareRemoteLibPlugin","externalRuntime","externals","key","value","externalReact","rspack_default","src_default","rspack_default"]}
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,82 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// src/helper/config.ts
|
|
5
|
-
var shareGlobalName = "EMP_SHARE_RUNTIME";
|
|
6
|
-
|
|
7
|
-
// src/helper/index.ts
|
|
8
|
-
var importJsVm = (content) => `data:text/javascript,${content}`;
|
|
9
|
-
|
|
10
|
-
// src/plugin/rspack/plugin.ts
|
|
11
|
-
var getShareRuntimeEntry = () => {
|
|
12
|
-
const content = [`import '@empjs/share/library'`].join("\n");
|
|
13
|
-
return importJsVm(content);
|
|
14
|
-
};
|
|
15
|
-
var EmpShareRuntimePlugin = class {
|
|
16
|
-
constructor() {
|
|
17
|
-
}
|
|
18
|
-
apply(compiler) {
|
|
19
|
-
const { webpack } = compiler;
|
|
20
|
-
const entry = getShareRuntimeEntry();
|
|
21
|
-
new webpack.EntryPlugin(compiler.context, entry, {
|
|
22
|
-
name: void 0
|
|
23
|
-
}).apply(compiler);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
// src/plugin/rspack/index.ts
|
|
28
|
-
var rspack_default = (o = {}) => {
|
|
29
|
-
return {
|
|
30
|
-
name: "@empjs/share",
|
|
31
|
-
async rsConfig(store) {
|
|
32
|
-
console.log(o);
|
|
33
|
-
if (o.name) {
|
|
34
|
-
const op = store.deepAssign(
|
|
35
|
-
{
|
|
36
|
-
filename: "emp.js",
|
|
37
|
-
manifest: false,
|
|
38
|
-
dts: false,
|
|
39
|
-
dev: {
|
|
40
|
-
disableDynamicRemoteTypeHints: true
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
o
|
|
44
|
-
);
|
|
45
|
-
delete op.empRuntime;
|
|
46
|
-
store.chain.plugin("plugin-emp-share").use(ModuleFederationPlugin, [op]);
|
|
47
|
-
}
|
|
48
|
-
const { empRuntime } = o;
|
|
49
|
-
if (empRuntime) {
|
|
50
|
-
if (!empRuntime.runtimeLib) {
|
|
51
|
-
store.chain.plugin("plugin-emp-runtime").use(EmpShareRuntimePlugin);
|
|
52
|
-
}
|
|
53
|
-
empRuntime.runtimeGlobal = empRuntime.frameworkGlobal || shareGlobalName;
|
|
54
|
-
const externalRuntime = {
|
|
55
|
-
"@module-federation/runtime": `MFRuntime`,
|
|
56
|
-
"@module-federation/sdk": `MFSDK`
|
|
57
|
-
};
|
|
58
|
-
const externals = [];
|
|
59
|
-
for (const [key, value] of Object.entries(externalRuntime)) {
|
|
60
|
-
externals[key] = `${empRuntime.runtimeGlobal}.${value}`;
|
|
61
|
-
}
|
|
62
|
-
empRuntime.framework = empRuntime.framework || "react";
|
|
63
|
-
empRuntime.frameworkGlobal = empRuntime.frameworkGlobal || empRuntime.runtimeGlobal;
|
|
64
|
-
if (empRuntime.framework === "react") {
|
|
65
|
-
const externalReact = {
|
|
66
|
-
react: `React`,
|
|
67
|
-
"react-dom": `ReactDOM`
|
|
68
|
-
};
|
|
69
|
-
for (const [key, value] of Object.entries(externalReact)) {
|
|
70
|
-
externals[key] = `${empRuntime.frameworkGlobal}.${value}`;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
if (empRuntime.setExternals) empRuntime.setExternals(externals);
|
|
74
|
-
store.merge({ externals });
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
};
|
|
79
|
-
export {
|
|
80
|
-
rspack_default as pluginRspackEmpShare
|
|
81
|
-
};
|
|
1
|
+
import{ModuleFederationPlugin as j}from"@module-federation/enhanced/rspack";var f="EMP_SHARE_RUNTIME";var b=t=>`data:text/javascript,${t}`;var g=t=>{let e=[`import '${t}'`].join(`
|
|
2
|
+
`);return b(e)},m=class{options;constructor(e){this.options=e}apply(e){let{webpack:n}=e,{runtimeLib:o}=this.options;if(!o){let i=g("@empjs/share/library");new n.EntryPlugin(e.context,i,{name:void 0}).apply(e)}}};function y(t,e){return typeof t=="string"?`${t}/runtime${e==="development"?".development":""}.umd.js`:typeof t=="object"?e==="development"?t.dev:t.prod:""}var h=(t={})=>({name:"@empjs/share",async rsConfig(e){if(t.name){let o=e.deepAssign({filename:"emp.js",manifest:!1,dts:!1,dev:{disableDynamicRemoteTypeHints:!0}},t);delete o.empRuntime,e.chain.plugin("plugin-emp-share").use(j,[o])}let{empRuntime:n}=t;if(n){let o=[],i=n.framework||"react",{frameworkLib:u,runtimeLib:p}=n,l=n.runtimeGlobal||f,a=n.frameworkGlobal||"";if(n.injectGlobalValToHtml!==!1&&o.push({type:"script",value:`EMPShareGlobalVal={frameworkLib:'${a}',runtimeLib:'${l}',framework:'${i}'}`}),u){let r=y(u,e.mode);o.push({type:"js",value:r})}p?p==="useFrameworkLib"?l=a:o.push({type:"js",value:p}):e.chain.plugin("plugin-emp-share-framework").use(m,[n]),o.length>0&&e.injectHeadTags(o,"EMPShare");let R={"@module-federation/runtime":"MFRuntime","@module-federation/sdk":"MFSDK"},s={};for(let[r,c]of Object.entries(R))s[r]=`${l}.${c}`;if(i==="react"&&a){let r={react:"React","react-dom":"ReactDOM"};for(let[c,E]of Object.entries(r))s[c]=`${a}.${E}`}n.setExternals&&n.setExternals(s),e.chain.merge({externals:s})}}}),d=h;var L=d;export{L as default,d as pluginRspackEmpShare};
|
|
82
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/plugins/rspack/index.ts","../src/helper/config.ts","../src/helper/index.ts","../src/plugins/rspack/plugin.ts","../src/plugins/rspack/utils.ts","../src/index.ts"],"sourcesContent":["import type {GlobalStore} from '@empjs/cli'\nimport {ModuleFederationPlugin} from '@module-federation/enhanced/rspack'\nimport {shareGlobalName} from 'src/helper/config'\nimport {EmpShareRemoteLibPlugin} from './plugin'\nimport type {EMPPluginShareType} from './types'\nimport {getRuntimeLib} from './utils'\n//\nexport const pluginRspackEmpShare = (o: EMPPluginShareType = {}) => {\n return {\n name: '@empjs/share',\n async rsConfig(store: GlobalStore) {\n if (o.name) {\n const op: any = store.deepAssign(\n {\n filename: 'emp.js',\n manifest: false,\n dts: false,\n dev: {\n disableDynamicRemoteTypeHints: true,\n },\n },\n o,\n )\n delete op.empRuntime\n store.chain.plugin('plugin-emp-share').use(ModuleFederationPlugin, [op])\n }\n const {empRuntime} = o\n if (empRuntime) {\n const injectHtml: any[] = []\n const framework = empRuntime.framework || 'react'\n const {frameworkLib, runtimeLib} = empRuntime\n //\n let runtimeGlobal = empRuntime.runtimeGlobal || shareGlobalName\n const frameworkGlobal = empRuntime.frameworkGlobal || ''\n const injectGlobalValToHtml = empRuntime.injectGlobalValToHtml === false ? false : true\n // 插入全局引用变量 让 empRuntime 可以提前实例化 不需要在页面重复实例化\n if (injectGlobalValToHtml) {\n injectHtml.push({\n type: 'script',\n value: `EMPShareGlobalVal={frameworkLib:'${frameworkGlobal}',runtimeLib:'${runtimeGlobal}',framework:'${framework}'}`,\n })\n }\n /**\n * frameworkLib & runtimeLib 的全局引用\n */\n if (frameworkLib) {\n const url = getRuntimeLib(frameworkLib, store.mode)\n injectHtml.push({\n type: 'js',\n value: url,\n })\n }\n if (!runtimeLib) {\n // 内置 runtimeLib\n store.chain.plugin('plugin-emp-share-framework').use(EmpShareRemoteLibPlugin, [empRuntime])\n }\n // 统一封装到 frameworkLib\n else if (runtimeLib === 'useFrameworkLib') {\n runtimeGlobal = frameworkGlobal\n } else {\n // 插入 runtime\n injectHtml.push({type: 'js', value: runtimeLib})\n }\n if (injectHtml.length > 0) {\n store.injectHeadTags(injectHtml, 'EMPShare')\n }\n /**\n * 设置 externals\n */\n const externalRuntime = {\n '@module-federation/runtime': `MFRuntime`,\n '@module-federation/sdk': `MFSDK`,\n }\n const externals = {}\n for (const [key, value] of Object.entries(externalRuntime)) {\n externals[key] = `${runtimeGlobal}.${value}`\n }\n if (framework === 'react' && frameworkGlobal) {\n const externalReact = {\n react: `React`,\n 'react-dom': `ReactDOM`,\n }\n for (const [key, value] of Object.entries(externalReact)) {\n externals[key] = `${frameworkGlobal}.${value}`\n }\n }\n if (empRuntime.setExternals) empRuntime.setExternals(externals)\n //\n // console.log('externals in plugin', externals)\n store.chain.merge({externals})\n }\n },\n }\n}\n\nexport default pluginRspackEmpShare\n","export const shareGlobalName = 'EMP_SHARE_RUNTIME'\n","export const importJsVm = (content: string) => `data:text/javascript,${content}`\n\nexport function deepAssign<T>(target: any, ...sources: any): T {\n for (const source of sources) {\n for (const k in source) {\n const vs = source[k],\n vt = target[k]\n if (Object(vs) == vs && Object(vt) === vt) {\n target[k] = deepAssign(vt, vs)\n continue\n }\n target[k] = source[k]\n }\n }\n return target\n}\n\nexport const checkVersion = (version: string) => (version ? Number(version.split('.')[0]) : 0)\nexport const isPromise = (p: any) => p && Object.prototype.toString.call(p) === '[object Promise]'\n","import type {Compiler} from '@rspack/core'\nimport {importJsVm} from 'src/helper'\nimport type {EMPSHARERuntimeOptions} from './types'\nconst getShareRuntimeEntry = url => {\n const content = [`import '${url}'`].join('\\n')\n return importJsVm(content)\n}\nexport class EmpShareRemoteLibPlugin {\n options: EMPSHARERuntimeOptions\n constructor(op: EMPSHARERuntimeOptions) {\n this.options = op\n }\n apply(compiler: Compiler) {\n const {webpack} = compiler\n const {runtimeLib} = this.options\n if (!runtimeLib) {\n const entry = getShareRuntimeEntry('@empjs/share/library')\n new webpack.EntryPlugin(compiler.context, entry, {\n name: undefined,\n }).apply(compiler)\n }\n }\n}\n","import type {EMPSHARERuntimeOptions} from './types'\nexport function getRuntimeLib(host: EMPSHARERuntimeOptions['frameworkLib'], mode: string) {\n if (typeof host === 'string') return `${host}/runtime${mode === 'development' ? '.development' : ''}.umd.js`\n else if (typeof host === 'object') {\n if (mode === 'development') return host.dev\n else return host.prod\n }\n return ''\n}\n","import {default as pluginRspackEmpShare} from 'src/plugins/rspack'\nexport default pluginRspackEmpShare\nexport {pluginRspackEmpShare}\n"],"mappings":"AACA,OAAQ,0BAAAA,MAA6B,qCCD9B,IAAMC,EAAkB,oBCAxB,IAAMC,EAAcC,GAAoB,wBAAwBA,CAAO,GCG9E,IAAMC,EAAuBC,GAAO,CAClC,IAAMC,EAAU,CAAC,WAAWD,CAAG,GAAG,EAAE,KAAK;AAAA,CAAI,EAC7C,OAAOE,EAAWD,CAAO,CAC3B,EACaE,EAAN,KAA8B,CACnC,QACA,YAAYC,EAA4B,CACtC,KAAK,QAAUA,CACjB,CACA,MAAMC,EAAoB,CACxB,GAAM,CAAC,QAAAC,CAAO,EAAID,EACZ,CAAC,WAAAE,CAAU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AAC,EAP/CP,EAAM,MAAM,OAAO,4BAA4B,EAAE,IAAIa,EAAyB,CAACV,CAAU,CAAC,EASxFC,EAAW,OAAS,GACtBJ,EAAM,eAAeI,EAAY,UAAU,EAK7C,IAAMU,EAAkB,CACtB,6BAA8B,YAC9B,yBAA0B,OAC5B,EACMC,EAAY,CAAC,EACnB,OAAW,CAACC,EAAKC,CAAK,IAAK,OAAO,QAAQH,CAAe,EACvDC,EAAUC,CAAG,EAAI,GAAGR,CAAa,IAAIS,CAAK,GAE5C,GAAIZ,IAAc,SAAWK,EAAiB,CAC5C,IAAMQ,EAAgB,CACpB,MAAO,QACP,YAAa,UACf,EACA,OAAW,CAACF,EAAKC,CAAK,IAAK,OAAO,QAAQC,CAAa,EACrDH,EAAUC,CAAG,EAAI,GAAGN,CAAe,IAAIO,CAAK,EAEhD,CACId,EAAW,cAAcA,EAAW,aAAaY,CAAS,EAG9Df,EAAM,MAAM,MAAM,CAAC,UAAAe,CAAS,CAAC,CAC/B,CACF,CACF,GAGKI,EAAQrB,EK9Ff,IAAOsB,EAAQC","names":["ModuleFederationPlugin","shareGlobalName","importJsVm","content","getShareRuntimeEntry","url","content","importJsVm","EmpShareRemoteLibPlugin","op","compiler","webpack","runtimeLib","entry","getRuntimeLib","host","mode","pluginRspackEmpShare","o","store","op","ModuleFederationPlugin","empRuntime","injectHtml","framework","frameworkLib","runtimeLib","runtimeGlobal","shareGlobalName","frameworkGlobal","url","getRuntimeLib","EmpShareRemoteLibPlugin","externalRuntime","externals","key","value","externalReact","rspack_default","src_default","rspack_default"]}
|