@mpxjs/webpack-plugin 2.10.7-beta.9 → 2.10.8

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.
Files changed (42) hide show
  1. package/LICENSE +433 -0
  2. package/lib/dependencies/RequireExternalDependency.js +61 -0
  3. package/lib/file-loader.js +3 -2
  4. package/lib/index.js +60 -15
  5. package/lib/json-compiler/index.js +1 -0
  6. package/lib/parser.js +1 -1
  7. package/lib/platform/json/wx/index.js +43 -25
  8. package/lib/platform/template/wx/component-config/fix-component-name.js +2 -2
  9. package/lib/platform/template/wx/component-config/movable-view.js +10 -1
  10. package/lib/platform/template/wx/index.js +2 -1
  11. package/lib/react/LoadAsyncChunkModule.js +74 -0
  12. package/lib/react/index.js +3 -1
  13. package/lib/react/processJSON.js +74 -13
  14. package/lib/react/processScript.js +6 -6
  15. package/lib/react/script-helper.js +100 -41
  16. package/lib/runtime/components/react/context.ts +2 -12
  17. package/lib/runtime/components/react/dist/context.js +1 -1
  18. package/lib/runtime/components/react/dist/getInnerListeners.js +1 -1
  19. package/lib/runtime/components/react/dist/mpx-async-suspense.jsx +135 -0
  20. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +9 -63
  21. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +58 -301
  22. package/lib/runtime/components/react/dist/mpx-swiper.jsx +27 -53
  23. package/lib/runtime/components/react/dist/mpx-web-view.jsx +14 -28
  24. package/lib/runtime/components/react/dist/useAnimationHooks.js +2 -87
  25. package/lib/runtime/components/react/getInnerListeners.ts +1 -1
  26. package/lib/runtime/components/react/mpx-async-suspense.tsx +180 -0
  27. package/lib/runtime/components/react/mpx-movable-area.tsx +11 -98
  28. package/lib/runtime/components/react/mpx-movable-view.tsx +60 -350
  29. package/lib/runtime/components/react/mpx-swiper.tsx +25 -53
  30. package/lib/runtime/components/react/mpx-web-view.tsx +13 -33
  31. package/lib/runtime/components/react/types/global.d.ts +15 -0
  32. package/lib/runtime/components/react/useAnimationHooks.ts +2 -85
  33. package/lib/runtime/optionProcessorReact.d.ts +18 -0
  34. package/lib/runtime/optionProcessorReact.js +30 -0
  35. package/lib/script-setup-compiler/index.js +27 -5
  36. package/lib/template-compiler/compiler.js +4 -3
  37. package/lib/utils/dom-tag-config.js +17 -3
  38. package/lib/utils/trans-async-sub-rules.js +19 -0
  39. package/lib/web/script-helper.js +1 -1
  40. package/package.json +4 -4
  41. package/lib/runtime/components/react/AsyncContainer.tsx +0 -189
  42. package/lib/runtime/components/react/dist/AsyncContainer.jsx +0 -141
@@ -51,24 +51,14 @@ export interface RouteContextValue {
51
51
  pageId: number
52
52
  navigation: Record<string, any>
53
53
  }
54
- export interface MovableAreaContextValue {
55
- width: number
56
- height: number
57
- scaleArea: boolean
58
- onAreaScale?: (scaleInfo: { scale: number }) => void
59
- registerMovableView?: (id: string, callbacks: {
60
- onScale: (scaleInfo: { scale: number }) => void
61
- onScaleEnd?: () => void
62
- }) => void
63
- unregisterMovableView?: (id: string) => void
64
- }
65
54
 
66
- export const MovableAreaContext = createContext<MovableAreaContextValue>({ width: 0, height: 0, scaleArea: false })
67
55
  export interface StickyContextValue {
68
56
  registerStickyHeader: Function,
69
57
  unregisterStickyHeader: Function
70
58
  }
71
59
 
60
+ export const MovableAreaContext = createContext({ width: 0, height: 0 })
61
+
72
62
  export const FormContext = createContext<FormContextValue | null>(null)
73
63
 
74
64
  export const CheckboxGroupContext = createContext<GroupContextValue | null>(null)
@@ -1,7 +1,7 @@
1
1
  import { createContext } from 'react';
2
2
  import { Animated } from 'react-native';
3
3
  import { noop } from '@mpxjs/utils';
4
- export const MovableAreaContext = createContext({ width: 0, height: 0, scaleArea: false });
4
+ export const MovableAreaContext = createContext({ width: 0, height: 0 });
5
5
  export const FormContext = createContext(null);
6
6
  export const CheckboxGroupContext = createContext(null);
7
7
  export const RadioGroupContext = createContext(null);
@@ -8,7 +8,7 @@ const globalEventState = {
8
8
  const getTouchEvent = (type, event, config) => {
9
9
  const { navigation, propsRef, layoutRef } = config;
10
10
  const props = propsRef.current;
11
- const { y: navigationY = 0 } = navigation?.layout || {};
11
+ const { top: navigationY = 0 } = navigation?.layout || {};
12
12
  const nativeEvent = event.nativeEvent;
13
13
  const { timestamp, pageX, pageY, touches, changedTouches } = nativeEvent;
14
14
  const { id } = props;
@@ -0,0 +1,135 @@
1
+ import { useState, useEffect, useCallback, useRef, createElement } from 'react';
2
+ import { View, Image, StyleSheet, Text, TouchableOpacity } from 'react-native';
3
+ import FastImage from '@d11/react-native-fast-image';
4
+ const asyncChunkMap = new Map();
5
+ const styles = StyleSheet.create({
6
+ container: {
7
+ flex: 1,
8
+ padding: 20,
9
+ backgroundColor: '#fff'
10
+ },
11
+ loadingImage: {
12
+ width: 100,
13
+ height: 100,
14
+ marginTop: 220,
15
+ alignSelf: 'center'
16
+ },
17
+ buttonText: {
18
+ color: '#fff',
19
+ fontSize: 16,
20
+ fontWeight: '500',
21
+ textAlign: 'center'
22
+ },
23
+ errorImage: {
24
+ marginTop: 80,
25
+ width: 220,
26
+ aspectRatio: 1,
27
+ alignSelf: 'center'
28
+ },
29
+ errorText: {
30
+ fontSize: 16,
31
+ textAlign: 'center',
32
+ color: '#333',
33
+ marginBottom: 20
34
+ },
35
+ retryButton: {
36
+ position: 'absolute',
37
+ bottom: 54,
38
+ left: 20,
39
+ right: 20,
40
+ backgroundColor: '#fff',
41
+ paddingVertical: 15,
42
+ borderRadius: 30,
43
+ marginTop: 40,
44
+ borderWidth: 1,
45
+ borderColor: '#FF5F00'
46
+ },
47
+ retryButtonText: {
48
+ color: '#FF5F00',
49
+ fontSize: 16,
50
+ fontWeight: '500',
51
+ textAlign: 'center'
52
+ }
53
+ });
54
+ const DefaultFallback = ({ onReload }) => {
55
+ return (<View style={styles.container}>
56
+ <Image source={{
57
+ uri: 'https://dpubstatic.udache.com/static/dpubimg/Vak5mZvezPpKV5ZJI6P9b_drn-fallbak.png'
58
+ }} style={styles.errorImage} resizeMode="contain"/>
59
+ <Text style={styles.errorText}>网络出了点问题,请查看网络环境</Text>
60
+ <TouchableOpacity style={styles.retryButton} onPress={onReload} activeOpacity={0.7}>
61
+ <Text style={styles.retryButtonText}>点击重试</Text>
62
+ </TouchableOpacity>
63
+ </View>);
64
+ };
65
+ const DefaultLoading = () => {
66
+ return (<View style={styles.container}>
67
+ <FastImage style={styles.loadingImage} source={{
68
+ uri: 'https://dpubstatic.udache.com/static/dpubimg/439jiCVOtNOnEv9F2LaDs_loading.gif'
69
+ }} resizeMode={FastImage.resizeMode.contain}></FastImage>
70
+ </View>);
71
+ };
72
+ const AsyncSuspense = ({ type, innerProps, chunkName, moduleId, loading, fallback, getChildren }) => {
73
+ const [status, setStatus] = useState('pending');
74
+ const chunkLoaded = asyncChunkMap.has(moduleId);
75
+ const loadChunkPromise = useRef(null);
76
+ const reloadPage = useCallback(() => {
77
+ setStatus('pending');
78
+ }, []);
79
+ useEffect(() => {
80
+ let cancelled = false;
81
+ if (!chunkLoaded && status === 'pending') {
82
+ if (loadChunkPromise.current) {
83
+ loadChunkPromise
84
+ .current.then((res) => {
85
+ if (cancelled)
86
+ return;
87
+ asyncChunkMap.set(moduleId, res);
88
+ setStatus('loaded');
89
+ })
90
+ .catch((e) => {
91
+ if (cancelled)
92
+ return;
93
+ if (type === 'component') {
94
+ global.onLazyLoadError({
95
+ type: 'subpackage',
96
+ subpackage: [chunkName],
97
+ errMsg: `loadSubpackage: ${e.type}`
98
+ });
99
+ }
100
+ loadChunkPromise.current = null;
101
+ setStatus('error');
102
+ });
103
+ }
104
+ }
105
+ return () => {
106
+ cancelled = true;
107
+ };
108
+ }, [status]);
109
+ if (chunkLoaded) {
110
+ const Comp = asyncChunkMap.get(moduleId);
111
+ return createElement(Comp, innerProps);
112
+ }
113
+ else if (status === 'error') {
114
+ if (type === 'page') {
115
+ fallback = fallback || DefaultFallback;
116
+ return createElement(fallback, { onReload: reloadPage });
117
+ }
118
+ else {
119
+ return fallback ? createElement(fallback, innerProps) : null;
120
+ }
121
+ }
122
+ else {
123
+ if (!loadChunkPromise.current) {
124
+ loadChunkPromise.current = getChildren();
125
+ }
126
+ if (type === 'page') {
127
+ return createElement(loading || DefaultLoading);
128
+ }
129
+ else {
130
+ return fallback ? createElement(fallback, innerProps) : null;
131
+ }
132
+ }
133
+ };
134
+ AsyncSuspense.displayName = 'MpxAsyncSuspense';
135
+ export default AsyncSuspense;
@@ -1,87 +1,33 @@
1
1
  /**
2
- * scale-area
2
+ * scale-area
3
3
  */
4
4
  import { View } from 'react-native';
5
- import { forwardRef, useRef, useMemo, useCallback, createElement } from 'react';
6
- import { GestureDetector, Gesture } from 'react-native-gesture-handler';
7
- import { useSharedValue } from 'react-native-reanimated';
5
+ import { forwardRef, useRef, useMemo, createElement } from 'react';
8
6
  import useNodesRef from './useNodesRef';
9
7
  import useInnerProps from './getInnerListeners';
10
8
  import { MovableAreaContext } from './context';
11
9
  import { useTransformStyle, wrapChildren, useLayout, extendObject } from './utils';
12
10
  import Portal from './mpx-portal';
13
11
  const _MovableArea = forwardRef((props, ref) => {
14
- const { style = {}, 'scale-area': scaleArea = false, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight } = props;
12
+ const { style = {}, 'enable-var': enableVar, 'external-var-context': externalVarContext, 'parent-font-size': parentFontSize, 'parent-width': parentWidth, 'parent-height': parentHeight } = props;
15
13
  const { hasSelfPercent, normalStyle, hasVarDec, varContextRef, hasPositionFixed, setWidth, setHeight } = useTransformStyle(style, { enableVar, externalVarContext, parentFontSize, parentWidth, parentHeight });
16
- const movableAreaRef = useRef(null);
17
- const movableViewsValue = useSharedValue({});
18
- useNodesRef(props, ref, movableAreaRef, {
14
+ const movableViewRef = useRef(null);
15
+ useNodesRef(props, ref, movableViewRef, {
19
16
  style: normalStyle
20
17
  });
21
- // 注册/注销 MovableView 的回调
22
- const registerMovableView = useCallback((id, callbacks) => {
23
- movableViewsValue.value = extendObject(movableViewsValue.value, { [id]: callbacks });
24
- }, []);
25
- const unregisterMovableView = useCallback((id) => {
26
- delete movableViewsValue.value[id];
27
- }, []);
28
- // 处理区域缩放手势
29
- const handleAreaScale = useCallback((scaleInfo) => {
30
- 'worklet';
31
- if (scaleArea) {
32
- // 将缩放信息广播给所有注册的 MovableView
33
- Object.values(movableViewsValue.value).forEach((callbacks) => {
34
- callbacks.onScale && callbacks.onScale(scaleInfo);
35
- });
36
- }
37
- }, [scaleArea]);
38
- // 处理区域缩放结束
39
- const handleAreaScaleEnd = useCallback(() => {
40
- 'worklet';
41
- if (scaleArea) {
42
- // 通知所有注册的 MovableView 缩放结束
43
- Object.values(movableViewsValue.value).forEach((callbacks) => {
44
- callbacks.onScaleEnd && callbacks.onScaleEnd();
45
- });
46
- }
47
- }, [scaleArea]);
48
18
  const contextValue = useMemo(() => ({
49
19
  height: normalStyle.height || 10,
50
- width: normalStyle.width || 10,
51
- scaleArea,
52
- registerMovableView,
53
- unregisterMovableView
54
- }), [normalStyle.width, normalStyle.height, scaleArea]);
55
- const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: movableAreaRef });
56
- // 创建缩放手势
57
- const scaleGesture = useMemo(() => {
58
- if (!scaleArea)
59
- return null;
60
- return Gesture.Pinch()
61
- .onUpdate((e) => {
62
- 'worklet';
63
- handleAreaScale(e);
64
- })
65
- .onEnd(() => {
66
- 'worklet';
67
- handleAreaScaleEnd();
68
- });
69
- }, [scaleArea]);
20
+ width: normalStyle.width || 10
21
+ }), [normalStyle.width, normalStyle.height]);
22
+ const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: movableViewRef });
70
23
  const innerProps = useInnerProps(extendObject({}, props, layoutProps, {
71
24
  style: extendObject({ height: contextValue.height, width: contextValue.width }, normalStyle, layoutStyle),
72
- ref: movableAreaRef
25
+ ref: movableViewRef
73
26
  }), [], { layoutRef });
74
27
  let movableComponent = createElement(MovableAreaContext.Provider, { value: contextValue }, createElement(View, innerProps, wrapChildren(props, {
75
28
  hasVarDec,
76
29
  varContext: varContextRef.current
77
30
  })));
78
- // 如果启用了 scale-area,包装一个 GestureDetector
79
- if (scaleArea && scaleGesture) {
80
- movableComponent = createElement(MovableAreaContext.Provider, { value: contextValue }, createElement(GestureDetector, { gesture: scaleGesture }, createElement(View, innerProps, wrapChildren(props, {
81
- hasVarDec,
82
- varContext: varContextRef.current
83
- }))));
84
- }
85
31
  if (hasPositionFixed) {
86
32
  movableComponent = createElement(Portal, null, movableComponent);
87
33
  }