@tarojs/plugin-framework-react 4.0.0-beta.6 → 4.0.0-beta.60
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/dist/api-loader.js.map +1 -1
- package/dist/index.js +125 -30
- package/dist/index.js.map +1 -1
- package/dist/reconciler.js +347 -0
- package/dist/reconciler.js.map +1 -0
- package/dist/runtime.js +284 -53
- package/dist/runtime.js.map +1 -1
- package/package.json +16 -11
package/dist/runtime.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { EMPTY_OBJ, isFunction, isArray, hooks, ensure, isUndefined } from '@tarojs/shared';
|
|
2
|
-
import { Current, getPageInstance, injectPageInstance, incrementId, document, getPath, window, CONTEXT_ACTIONS, safeExecute, removePageInstance, ON_READY, requestAnimationFrame, eventCenter, getOnReadyEventKey, ON_SHOW, getOnShowEventKey, ON_HIDE, getOnHideEventKey, eventHandler, addLeadingSlash } from '@tarojs/runtime';
|
|
2
|
+
import { Current, getPageInstance, injectPageInstance, incrementId, document, perf, PAGE_INIT, getPath, window, CONTEXT_ACTIONS, safeExecute, removePageInstance, ON_READY, requestAnimationFrame, eventCenter, getOnReadyEventKey, ON_SHOW, getOnShowEventKey, ON_HIDE, getOnHideEventKey, eventHandler, addLeadingSlash } from '@tarojs/runtime';
|
|
3
|
+
import * as Solid from 'solid-js';
|
|
4
|
+
import { createContext, createMemo, useContext, createRenderEffect, onCleanup } from 'solid-js';
|
|
5
|
+
import * as SolidReconciler from '@tarojs/plugin-framework-react/dist/reconciler';
|
|
3
6
|
|
|
4
7
|
const reactMeta = {
|
|
5
8
|
PageContext: EMPTY_OBJ,
|
|
6
|
-
R: EMPTY_OBJ
|
|
9
|
+
R: EMPTY_OBJ,
|
|
10
|
+
};
|
|
11
|
+
const solidMeta = {
|
|
12
|
+
PageContext: createContext(''),
|
|
7
13
|
};
|
|
8
14
|
|
|
9
15
|
const HOOKS_APP_ID = 'taro-app';
|
|
@@ -44,49 +50,87 @@ function setRouterParams(options) {
|
|
|
44
50
|
|
|
45
51
|
const createTaroHook = (lifecycle) => {
|
|
46
52
|
return (fn) => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
fnRef.current
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
// callback is immutable but inner function is up to date
|
|
63
|
-
const callback = (...args) => fnRef.current(...args);
|
|
64
|
-
if (isFunction(inst[lifecycle])) {
|
|
65
|
-
(inst[lifecycle]) = [inst[lifecycle], callback];
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
(inst[lifecycle]) = [
|
|
69
|
-
...((inst[lifecycle]) || []),
|
|
70
|
-
callback
|
|
71
|
-
];
|
|
72
|
-
}
|
|
73
|
-
if (first) {
|
|
74
|
-
injectPageInstance(inst, id);
|
|
75
|
-
}
|
|
76
|
-
return () => {
|
|
77
|
-
const inst = instRef.current;
|
|
78
|
-
if (!inst)
|
|
79
|
-
return;
|
|
80
|
-
const list = inst[lifecycle];
|
|
81
|
-
if (list === callback) {
|
|
82
|
-
(inst[lifecycle]) = undefined;
|
|
53
|
+
if (process.env.FRAMEWORK !== 'solid') {
|
|
54
|
+
const { R: React, PageContext } = reactMeta;
|
|
55
|
+
const id = React.useContext(PageContext) || HOOKS_APP_ID;
|
|
56
|
+
const instRef = React.useRef();
|
|
57
|
+
// hold fn ref and keep up to date
|
|
58
|
+
const fnRef = React.useRef(fn);
|
|
59
|
+
if (fnRef.current !== fn)
|
|
60
|
+
fnRef.current = fn;
|
|
61
|
+
React.useLayoutEffect(() => {
|
|
62
|
+
let inst = instRef.current = getPageInstance(id);
|
|
63
|
+
let first = false;
|
|
64
|
+
if (!inst) {
|
|
65
|
+
first = true;
|
|
66
|
+
instRef.current = Object.create(null);
|
|
67
|
+
inst = instRef.current;
|
|
83
68
|
}
|
|
84
|
-
|
|
85
|
-
|
|
69
|
+
// callback is immutable but inner function is up to date
|
|
70
|
+
const callback = (...args) => fnRef.current(...args);
|
|
71
|
+
if (isFunction(inst[lifecycle])) {
|
|
72
|
+
(inst[lifecycle]) = [inst[lifecycle], callback];
|
|
86
73
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
74
|
+
else {
|
|
75
|
+
(inst[lifecycle]) = [
|
|
76
|
+
...((inst[lifecycle]) || []),
|
|
77
|
+
callback
|
|
78
|
+
];
|
|
79
|
+
}
|
|
80
|
+
if (first) {
|
|
81
|
+
injectPageInstance(inst, id);
|
|
82
|
+
}
|
|
83
|
+
return () => {
|
|
84
|
+
const inst = instRef.current;
|
|
85
|
+
if (!inst)
|
|
86
|
+
return;
|
|
87
|
+
const list = inst[lifecycle];
|
|
88
|
+
if (list === callback) {
|
|
89
|
+
(inst[lifecycle]) = undefined;
|
|
90
|
+
}
|
|
91
|
+
else if (isArray(list)) {
|
|
92
|
+
(inst[lifecycle]) = list.filter(item => item !== callback);
|
|
93
|
+
}
|
|
94
|
+
instRef.current = undefined;
|
|
95
|
+
};
|
|
96
|
+
}, []);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const context = useContext(solidMeta.PageContext);
|
|
100
|
+
const id = context || HOOKS_APP_ID;
|
|
101
|
+
createRenderEffect(() => {
|
|
102
|
+
let inst = getPageInstance(id);
|
|
103
|
+
let first = false;
|
|
104
|
+
if (!inst) {
|
|
105
|
+
first = true;
|
|
106
|
+
inst = Object.create({
|
|
107
|
+
id: id,
|
|
108
|
+
type: 'page',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
if (isFunction(inst[lifecycle])) {
|
|
112
|
+
inst[lifecycle] = [inst[lifecycle], fn];
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
inst[lifecycle] = [
|
|
116
|
+
...((inst[lifecycle]) || []),
|
|
117
|
+
fn
|
|
118
|
+
];
|
|
119
|
+
}
|
|
120
|
+
if (first) {
|
|
121
|
+
injectPageInstance(inst, id);
|
|
122
|
+
}
|
|
123
|
+
onCleanup(() => {
|
|
124
|
+
const list = inst[lifecycle];
|
|
125
|
+
if (list === fn) {
|
|
126
|
+
(inst[lifecycle]) = undefined;
|
|
127
|
+
}
|
|
128
|
+
else if (isArray(list)) {
|
|
129
|
+
(inst[lifecycle]) = list.filter(item => item !== fn);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
90
134
|
};
|
|
91
135
|
};
|
|
92
136
|
/** LifeCycle */
|
|
@@ -115,8 +159,13 @@ const useTitleClick = createTaroHook('onTitleClick');
|
|
|
115
159
|
/** Router */
|
|
116
160
|
const useReady = createTaroHook('onReady');
|
|
117
161
|
const useRouter = (dynamic = false) => {
|
|
118
|
-
|
|
119
|
-
|
|
162
|
+
if (process.env.FRAMEWORK !== 'solid') {
|
|
163
|
+
const React = reactMeta.R;
|
|
164
|
+
return dynamic ? Current.router : React.useMemo(() => Current.router, []);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
return dynamic ? Current.router : createMemo(() => Current.router);
|
|
168
|
+
}
|
|
120
169
|
};
|
|
121
170
|
const useTabItemTap = createTaroHook('onTabItemTap');
|
|
122
171
|
const useScope = () => undefined;
|
|
@@ -164,7 +213,12 @@ function setReconciler(ReactDOM) {
|
|
|
164
213
|
});
|
|
165
214
|
});
|
|
166
215
|
hooks.tap('batchedEventUpdates', function (cb) {
|
|
167
|
-
|
|
216
|
+
if (process.env.FRAMEWORK !== 'solid') {
|
|
217
|
+
ReactDOM === null || ReactDOM === void 0 ? void 0 : ReactDOM.unstable_batchedUpdates(cb);
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
Solid.batch(cb);
|
|
221
|
+
}
|
|
168
222
|
});
|
|
169
223
|
hooks.tap('mergePageInstance', function (prev, next) {
|
|
170
224
|
if (!prev || !next)
|
|
@@ -179,7 +233,8 @@ function setReconciler(ReactDOM) {
|
|
|
179
233
|
next[item] = nextList.concat(prevList);
|
|
180
234
|
});
|
|
181
235
|
});
|
|
182
|
-
|
|
236
|
+
// TODO 使用 solid 时,暂不支持以下事件
|
|
237
|
+
if (process.env.TARO_PLATFORM === 'web' && process.env.FRAMEWORK !== 'solid') {
|
|
183
238
|
hooks.tap('createPullDownComponent', (el, _, R, customWrapper) => {
|
|
184
239
|
const isReactComponent = isClassComponent(R, el);
|
|
185
240
|
return R.forwardRef((props, ref) => {
|
|
@@ -192,8 +247,19 @@ function setReconciler(ReactDOM) {
|
|
|
192
247
|
return h$1(customWrapper || 'taro-pull-to-refresh-core', null, h$1(el, Object.assign(Object.assign({}, newProps), refs)));
|
|
193
248
|
});
|
|
194
249
|
});
|
|
195
|
-
hooks.tap('getDOMNode', inst => {
|
|
196
|
-
|
|
250
|
+
hooks.tap('getDOMNode', (inst) => {
|
|
251
|
+
// 由于react 18移除了ReactDOM.findDOMNode方法,修复H5端 Taro.createSelectorQuery设置in(scope)时,报错问题
|
|
252
|
+
// https://zh-hans.react.dev/reference/react-dom/findDOMNode
|
|
253
|
+
if (!inst) {
|
|
254
|
+
return document;
|
|
255
|
+
}
|
|
256
|
+
else if (inst instanceof HTMLElement) {
|
|
257
|
+
return inst;
|
|
258
|
+
}
|
|
259
|
+
else if (inst.$taroPath) {
|
|
260
|
+
const el = document.getElementById(inst.$taroPath);
|
|
261
|
+
return el !== null && el !== void 0 ? el : document;
|
|
262
|
+
}
|
|
197
263
|
});
|
|
198
264
|
}
|
|
199
265
|
}
|
|
@@ -254,7 +320,7 @@ function connectReactPage(R, id) {
|
|
|
254
320
|
*/
|
|
255
321
|
function createReactApp(App, react, dom, config) {
|
|
256
322
|
if (process.env.NODE_ENV !== 'production') {
|
|
257
|
-
ensure(!!dom, '构建 React/Nerv 项目请把 process.env.FRAMEWORK 设置为 \'react\'/\'nerv\' ');
|
|
323
|
+
ensure(!!dom, '构建 React/Nerv 项目请把 process.env.FRAMEWORK 设置为 \'react\'/\'preact\'/\'nerv\' ');
|
|
258
324
|
}
|
|
259
325
|
reactMeta.R = react;
|
|
260
326
|
h$1 = react.createElement;
|
|
@@ -302,7 +368,10 @@ function createReactApp(App, react, dom, config) {
|
|
|
302
368
|
const key = id + pageKeyId();
|
|
303
369
|
const page = () => h$1(pageWrapper, { key, tid: id });
|
|
304
370
|
this.pages.push(page);
|
|
305
|
-
this.forceUpdate(
|
|
371
|
+
this.forceUpdate((...args) => {
|
|
372
|
+
perf.stop(PAGE_INIT);
|
|
373
|
+
return cb(...args);
|
|
374
|
+
});
|
|
306
375
|
}
|
|
307
376
|
unmount(id, cb) {
|
|
308
377
|
const elements = this.elements;
|
|
@@ -469,6 +538,161 @@ function createReactApp(App, react, dom, config) {
|
|
|
469
538
|
Current.app = appObj;
|
|
470
539
|
return appObj;
|
|
471
540
|
}
|
|
541
|
+
function createSolidApp(App, config) {
|
|
542
|
+
setReconciler();
|
|
543
|
+
const appRef = {
|
|
544
|
+
mount: () => { },
|
|
545
|
+
unmount: () => { },
|
|
546
|
+
};
|
|
547
|
+
function getAppInstance() {
|
|
548
|
+
return appRef;
|
|
549
|
+
}
|
|
550
|
+
function AppWrapper() {
|
|
551
|
+
const [pages, setPages] = Solid.createSignal([]);
|
|
552
|
+
appRef.mount = (component, id) => {
|
|
553
|
+
setPages((old) => {
|
|
554
|
+
return [...old, { id, component }];
|
|
555
|
+
});
|
|
556
|
+
};
|
|
557
|
+
appRef.unmount = (id) => {
|
|
558
|
+
setPages(pages().filter((item) => {
|
|
559
|
+
return item.id !== id;
|
|
560
|
+
}));
|
|
561
|
+
};
|
|
562
|
+
return SolidReconciler.createComponent(App, {
|
|
563
|
+
ref: appRef,
|
|
564
|
+
children: SolidReconciler.createComponent(Solid.For, {
|
|
565
|
+
get each() {
|
|
566
|
+
return pages();
|
|
567
|
+
},
|
|
568
|
+
children: ({ id, component }) => {
|
|
569
|
+
const children = () => SolidReconciler.createComponent(solidMeta.PageContext.Provider, {
|
|
570
|
+
value: id,
|
|
571
|
+
children: () => {
|
|
572
|
+
injectPageInstance({ id: id, type: 'page' }, id);
|
|
573
|
+
return SolidReconciler.createComponent(component, {
|
|
574
|
+
tid: id,
|
|
575
|
+
});
|
|
576
|
+
},
|
|
577
|
+
});
|
|
578
|
+
const root = process.env.TARO_PLATFORM === 'web'
|
|
579
|
+
? document.createElement('div')
|
|
580
|
+
: SolidReconciler.createElement('root');
|
|
581
|
+
if (process.env.TARO_PLATFORM === 'web') {
|
|
582
|
+
root.setAttribute('id', id);
|
|
583
|
+
root.classList.add('taro_page');
|
|
584
|
+
}
|
|
585
|
+
else {
|
|
586
|
+
SolidReconciler.setProp(root, 'id', id);
|
|
587
|
+
}
|
|
588
|
+
SolidReconciler.insert(root, children);
|
|
589
|
+
return root;
|
|
590
|
+
},
|
|
591
|
+
}),
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
function renderSolidRoot() {
|
|
595
|
+
let appId = 'app';
|
|
596
|
+
if (process.env.TARO_PLATFORM === 'web') {
|
|
597
|
+
appId = (config === null || config === void 0 ? void 0 : config.appId) || appId;
|
|
598
|
+
}
|
|
599
|
+
const container = document.getElementById(appId);
|
|
600
|
+
SolidReconciler.render(AppWrapper, container);
|
|
601
|
+
}
|
|
602
|
+
if (process.env.TARO_PLATFORM !== 'web') {
|
|
603
|
+
renderSolidRoot();
|
|
604
|
+
}
|
|
605
|
+
const [ONLAUNCH, ONSHOW, ONHIDE] = hooks.call('getMiniLifecycleImpl').app;
|
|
606
|
+
const appObj = Object.create({
|
|
607
|
+
mount(component, id, cb) {
|
|
608
|
+
const appInstance = getAppInstance();
|
|
609
|
+
appInstance === null || appInstance === void 0 ? void 0 : appInstance.mount(component, id);
|
|
610
|
+
Solid.batch((...args) => {
|
|
611
|
+
perf.stop(PAGE_INIT);
|
|
612
|
+
return cb(...args);
|
|
613
|
+
});
|
|
614
|
+
},
|
|
615
|
+
unmount(id, cb) {
|
|
616
|
+
const appInstance = getAppInstance();
|
|
617
|
+
appInstance === null || appInstance === void 0 ? void 0 : appInstance.unmount(id);
|
|
618
|
+
Solid.batch(cb);
|
|
619
|
+
},
|
|
620
|
+
}, {
|
|
621
|
+
config: setDefaultDescriptor({
|
|
622
|
+
configurable: true,
|
|
623
|
+
value: config,
|
|
624
|
+
}),
|
|
625
|
+
[ONLAUNCH]: setDefaultDescriptor({
|
|
626
|
+
value(options) {
|
|
627
|
+
setRouterParams(options);
|
|
628
|
+
if (process.env.TARO_PLATFORM === 'web') {
|
|
629
|
+
renderSolidRoot();
|
|
630
|
+
}
|
|
631
|
+
const onLaunch = () => {
|
|
632
|
+
var _a;
|
|
633
|
+
const app = getAppInstance();
|
|
634
|
+
if (app) {
|
|
635
|
+
// 把 App Class 上挂载的额外属性同步到全局 app 对象中
|
|
636
|
+
if (app.taroGlobalData) {
|
|
637
|
+
const globalData = app.taroGlobalData;
|
|
638
|
+
const keys = Object.keys(globalData);
|
|
639
|
+
const descriptors = Object.getOwnPropertyDescriptors(globalData);
|
|
640
|
+
keys.forEach(key => {
|
|
641
|
+
Object.defineProperty(this, key, {
|
|
642
|
+
configurable: true,
|
|
643
|
+
enumerable: true,
|
|
644
|
+
get() {
|
|
645
|
+
return globalData[key];
|
|
646
|
+
},
|
|
647
|
+
set(value) {
|
|
648
|
+
globalData[key] = value;
|
|
649
|
+
}
|
|
650
|
+
});
|
|
651
|
+
});
|
|
652
|
+
Object.defineProperties(this, descriptors);
|
|
653
|
+
}
|
|
654
|
+
(_a = app.onCreate) === null || _a === void 0 ? void 0 : _a.call(app);
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
onLaunch();
|
|
658
|
+
triggerAppHook('onLaunch', options);
|
|
659
|
+
},
|
|
660
|
+
}),
|
|
661
|
+
[ONSHOW]: setDefaultDescriptor({
|
|
662
|
+
value(options) {
|
|
663
|
+
setRouterParams(options);
|
|
664
|
+
triggerAppHook('onShow', options);
|
|
665
|
+
},
|
|
666
|
+
}),
|
|
667
|
+
[ONHIDE]: setDefaultDescriptor({
|
|
668
|
+
value() {
|
|
669
|
+
triggerAppHook('onHide');
|
|
670
|
+
},
|
|
671
|
+
}),
|
|
672
|
+
onError: setDefaultDescriptor({
|
|
673
|
+
value(error) {
|
|
674
|
+
triggerAppHook('onError', error);
|
|
675
|
+
},
|
|
676
|
+
}),
|
|
677
|
+
onPageNotFound: setDefaultDescriptor({
|
|
678
|
+
value(res) {
|
|
679
|
+
triggerAppHook('onPageNotFound', res);
|
|
680
|
+
},
|
|
681
|
+
}),
|
|
682
|
+
});
|
|
683
|
+
function triggerAppHook(lifecycle, ...option) {
|
|
684
|
+
const instance = getPageInstance(HOOKS_APP_ID);
|
|
685
|
+
if (instance) {
|
|
686
|
+
const app = getAppInstance();
|
|
687
|
+
const func = hooks.call('getLifecycle', instance, lifecycle);
|
|
688
|
+
if (Array.isArray(func)) {
|
|
689
|
+
func.forEach((cb) => cb.apply(app, option));
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
Current.app = appObj;
|
|
694
|
+
return appObj;
|
|
695
|
+
}
|
|
472
696
|
|
|
473
697
|
const getNativeCompId = incrementId();
|
|
474
698
|
let h;
|
|
@@ -728,6 +952,13 @@ function createNativePageConfig(Component, pageName, data, react, reactDOM, page
|
|
|
728
952
|
hooks.call('modifyPageObject', pageObj);
|
|
729
953
|
return pageObj;
|
|
730
954
|
}
|
|
955
|
+
function createH5NativeComponentConfig(Component, react, reactdom) {
|
|
956
|
+
reactMeta.R = react;
|
|
957
|
+
h = react.createElement;
|
|
958
|
+
ReactDOM = reactdom;
|
|
959
|
+
setReconciler(ReactDOM);
|
|
960
|
+
return Component;
|
|
961
|
+
}
|
|
731
962
|
function createNativeComponentConfig(Component, react, reactdom, componentConfig) {
|
|
732
963
|
var _a, _b;
|
|
733
964
|
reactMeta.R = react;
|
|
@@ -867,7 +1098,7 @@ hooks.tap('initNativeApi', function (taro) {
|
|
|
867
1098
|
taro[hook] = taroHooks[hook];
|
|
868
1099
|
}
|
|
869
1100
|
});
|
|
870
|
-
if (
|
|
1101
|
+
if (process.env.FRAMEWORK === 'preact' && process.env.TARO_PLATFORM === 'mini') {
|
|
871
1102
|
const options = require('preact').options;
|
|
872
1103
|
const oldVNodeHook = options.vnode;
|
|
873
1104
|
const oldDiffedHook = options.diffed;
|
|
@@ -930,5 +1161,5 @@ if (__TARO_FRAMEWORK__ === 'preact' && process.env.TARO_PLATFORM === 'mini') {
|
|
|
930
1161
|
// })
|
|
931
1162
|
}
|
|
932
1163
|
|
|
933
|
-
export { connectReactPage, createNativeComponentConfig, createNativePageConfig, createReactApp, setReconciler, useAddToFavorites, useDidHide, useDidShow, useError, useLaunch, useLoad, useOptionMenuClick, usePageNotFound, usePageScroll, usePullDownRefresh, usePullIntercept, useReachBottom, useReady, useResize, useRouter, useSaveExitState, useScope, useShareAppMessage, useShareTimeline, useTabItemTap, useTitleClick, useUnhandledRejection, useUnload };
|
|
1164
|
+
export { connectReactPage, createH5NativeComponentConfig, createNativeComponentConfig, createNativePageConfig, createReactApp, createSolidApp, setReconciler, useAddToFavorites, useDidHide, useDidShow, useError, useLaunch, useLoad, useOptionMenuClick, usePageNotFound, usePageScroll, usePullDownRefresh, usePullIntercept, useReachBottom, useReady, useResize, useRouter, useSaveExitState, useScope, useShareAppMessage, useShareTimeline, useTabItemTap, useTitleClick, useUnhandledRejection, useUnload };
|
|
934
1165
|
//# sourceMappingURL=runtime.js.map
|