@empjs/adapter-react 0.0.1 → 4.0.0-alpha.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@empjs/adapter-react",
3
- "version": "0.0.1",
3
+ "version": "4.0.0-alpha.1",
4
4
  "description": "emp react adapter",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -36,7 +36,7 @@
36
36
  }
37
37
  },
38
38
  "engines": {
39
- "node": ">=16.0.0"
39
+ "node": "^20.19.0 || >=22.12.0"
40
40
  },
41
41
  "author": "Ken",
42
42
  "dependencies": {},
package/dist/index.cjs DELETED
@@ -1,109 +0,0 @@
1
- "use strict";
2
- var __webpack_require__ = {};
3
- (()=>{
4
- __webpack_require__.d = (exports1, definition)=>{
5
- for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
- enumerable: true,
7
- get: definition[key]
8
- });
9
- };
10
- })();
11
- (()=>{
12
- __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
- })();
14
- (()=>{
15
- __webpack_require__.r = (exports1)=>{
16
- if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
- value: 'Module'
18
- });
19
- Object.defineProperty(exports1, '__esModule', {
20
- value: true
21
- });
22
- };
23
- })();
24
- var __webpack_exports__ = {};
25
- __webpack_require__.r(__webpack_exports__);
26
- __webpack_require__.d(__webpack_exports__, {
27
- ReactAdapter: ()=>ReactAdapter,
28
- reactAdapter: ()=>reactAdapter
29
- });
30
- const checkVersion = (version)=>version ? Number(version.split('.')[0]) : 0;
31
- const isPromise = (p)=>p && '[object Promise]' === Object.prototype.toString.call(p);
32
- class ReactAdapter {
33
- libs = {
34
- scope: 'default'
35
- };
36
- constructor(op = {}){
37
- this.libs = {
38
- ...this.libs,
39
- ...op
40
- };
41
- }
42
- adapter(component, scope = this.libs.scope || 'default', React = this.libs.React, ReactDOM = this.libs.ReactDOM) {
43
- const reactVersion = checkVersion(React?.version || '18.0.0');
44
- const self = this;
45
- class WrappedComponent extends React.Component {
46
- containerRef;
47
- root;
48
- resolvedComponent = null;
49
- constructor(props){
50
- super(props);
51
- this.containerRef = React.createRef();
52
- }
53
- componentDidMount() {
54
- this.mountOriginalComponent(true);
55
- }
56
- componentDidUpdate() {
57
- this.mountOriginalComponent(false);
58
- }
59
- componentWillUnmount() {
60
- this.unmountOriginalComponent();
61
- }
62
- unmountOriginalComponent() {
63
- if (!this.containerRef.current) return;
64
- try {
65
- if (reactVersion < 18) ReactDOM.unmountComponentAtNode(this.containerRef.current);
66
- else if (this.root) this.root.unmount();
67
- } catch (error) {
68
- console.error("[ReactAdapter] \u5378\u8F7D\u7EC4\u4EF6\u65F6\u51FA\u9519:", error);
69
- }
70
- }
71
- async mountOriginalComponent(shouldRender = false) {
72
- try {
73
- if (!this.resolvedComponent) {
74
- let resolvedComp = component;
75
- if (isPromise(component)) resolvedComp = await component.then((m)=>m[scope]);
76
- this.resolvedComponent = resolvedComp;
77
- }
78
- const element = React.createElement(this.resolvedComponent, this.props);
79
- if (reactVersion < 18) {
80
- const renderMethod = shouldRender ? ReactDOM.render : ReactDOM.hydrate;
81
- renderMethod(element, this.containerRef.current);
82
- } else if (shouldRender) {
83
- const { createRoot } = self.libs;
84
- this.root = createRoot(this.containerRef.current);
85
- this.root.render(element);
86
- } else if (this.root) this.root.render(element);
87
- } catch (error) {
88
- console.error("[ReactAdapter] \u6302\u8F7D\u7EC4\u4EF6\u65F6\u51FA\u9519:", error);
89
- }
90
- }
91
- render() {
92
- return React.createElement('div', {
93
- ref: this.containerRef
94
- });
95
- }
96
- }
97
- return WrappedComponent;
98
- }
99
- }
100
- const reactAdapter = new ReactAdapter();
101
- exports.ReactAdapter = __webpack_exports__.ReactAdapter;
102
- exports.reactAdapter = __webpack_exports__.reactAdapter;
103
- for(var __webpack_i__ in __webpack_exports__)if (-1 === [
104
- "ReactAdapter",
105
- "reactAdapter"
106
- ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
107
- Object.defineProperty(exports, '__esModule', {
108
- value: true
109
- });
package/dist/index.d.ts DELETED
@@ -1,44 +0,0 @@
1
- /**
2
- * React 适配器
3
- * 这个模块提供了一个适配器,用于在不同版本的 React 环境中渲染组件
4
- * 支持 React 16/17 和 React 18+
5
- */
6
- /**
7
- * React 适配器配置选项
8
- */
9
- export interface ReactAdapterOptions {
10
- /** React 库实例 */
11
- React?: any;
12
- /** ReactDOM 库实例 */
13
- ReactDOM?: any;
14
- /** React 18+ createRoot 方法 */
15
- createRoot?: any;
16
- /** 组件导出的作用域名称 */
17
- scope?: string;
18
- }
19
- /**
20
- * React 适配器类
21
- * 用于在不同版本的 React 环境中适配组件渲染
22
- */
23
- export declare class ReactAdapter {
24
- /** 适配器配置 */
25
- libs: ReactAdapterOptions;
26
- /**
27
- * 构造函数
28
- * @param op - 适配器配置选项
29
- */
30
- constructor(op?: ReactAdapterOptions);
31
- /**
32
- * 适配器核心方法 - 使用类组件实现
33
- * @param component - 要适配的组件
34
- * @param scope - 组件导出的作用域名称
35
- * @param React - React 库实例
36
- * @param ReactDOM - ReactDOM 库实例
37
- * @returns 包装后的组件
38
- */
39
- adapter<P = any>(component: any, scope?: string, React?: any, ReactDOM?: any): P;
40
- }
41
- /**
42
- * 默认导出的 React 适配器实例
43
- */
44
- export declare const reactAdapter: ReactAdapter;
package/dist/index.js DELETED
@@ -1,72 +0,0 @@
1
- const checkVersion = (version)=>version ? Number(version.split('.')[0]) : 0;
2
- const isPromise = (p)=>p && '[object Promise]' === Object.prototype.toString.call(p);
3
- class ReactAdapter {
4
- libs = {
5
- scope: 'default'
6
- };
7
- constructor(op = {}){
8
- this.libs = {
9
- ...this.libs,
10
- ...op
11
- };
12
- }
13
- adapter(component, scope = this.libs.scope || 'default', React = this.libs.React, ReactDOM = this.libs.ReactDOM) {
14
- const reactVersion = checkVersion(React?.version || '18.0.0');
15
- const self = this;
16
- class WrappedComponent extends React.Component {
17
- containerRef;
18
- root;
19
- resolvedComponent = null;
20
- constructor(props){
21
- super(props);
22
- this.containerRef = React.createRef();
23
- }
24
- componentDidMount() {
25
- this.mountOriginalComponent(true);
26
- }
27
- componentDidUpdate() {
28
- this.mountOriginalComponent(false);
29
- }
30
- componentWillUnmount() {
31
- this.unmountOriginalComponent();
32
- }
33
- unmountOriginalComponent() {
34
- if (!this.containerRef.current) return;
35
- try {
36
- if (reactVersion < 18) ReactDOM.unmountComponentAtNode(this.containerRef.current);
37
- else if (this.root) this.root.unmount();
38
- } catch (error) {
39
- console.error("[ReactAdapter] \u5378\u8F7D\u7EC4\u4EF6\u65F6\u51FA\u9519:", error);
40
- }
41
- }
42
- async mountOriginalComponent(shouldRender = false) {
43
- try {
44
- if (!this.resolvedComponent) {
45
- let resolvedComp = component;
46
- if (isPromise(component)) resolvedComp = await component.then((m)=>m[scope]);
47
- this.resolvedComponent = resolvedComp;
48
- }
49
- const element = React.createElement(this.resolvedComponent, this.props);
50
- if (reactVersion < 18) {
51
- const renderMethod = shouldRender ? ReactDOM.render : ReactDOM.hydrate;
52
- renderMethod(element, this.containerRef.current);
53
- } else if (shouldRender) {
54
- const { createRoot } = self.libs;
55
- this.root = createRoot(this.containerRef.current);
56
- this.root.render(element);
57
- } else if (this.root) this.root.render(element);
58
- } catch (error) {
59
- console.error("[ReactAdapter] \u6302\u8F7D\u7EC4\u4EF6\u65F6\u51FA\u9519:", error);
60
- }
61
- }
62
- render() {
63
- return React.createElement('div', {
64
- ref: this.containerRef
65
- });
66
- }
67
- }
68
- return WrappedComponent;
69
- }
70
- }
71
- const reactAdapter = new ReactAdapter();
72
- export { ReactAdapter, reactAdapter };