@mpxjs/webpack-plugin 2.9.62 → 2.9.65

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 (95) hide show
  1. package/lib/config.js +38 -10
  2. package/lib/index.js +1 -3
  3. package/lib/platform/style/wx/index.js +115 -66
  4. package/lib/platform/template/wx/index.js +12 -8
  5. package/lib/react/processStyles.js +1 -0
  6. package/lib/react/processTemplate.js +2 -3
  7. package/lib/react/style-helper.js +9 -7
  8. package/lib/runtime/components/react/context.ts +9 -7
  9. package/lib/runtime/components/react/dist/context.js +1 -0
  10. package/lib/runtime/components/react/dist/getInnerListeners.js +12 -1
  11. package/lib/runtime/components/react/dist/mpx-button.jsx +53 -74
  12. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +20 -18
  13. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +30 -42
  14. package/lib/runtime/components/react/dist/mpx-form.jsx +18 -15
  15. package/lib/runtime/components/react/dist/mpx-icon.jsx +15 -17
  16. package/lib/runtime/components/react/dist/mpx-image/index.jsx +36 -34
  17. package/lib/runtime/components/react/dist/mpx-image/svg.jsx +3 -1
  18. package/lib/runtime/components/react/dist/mpx-input.jsx +36 -31
  19. package/lib/runtime/components/react/dist/mpx-label.jsx +30 -37
  20. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +15 -19
  21. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +10 -9
  22. package/lib/runtime/components/react/dist/mpx-picker/date.jsx +2 -1
  23. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +11 -10
  24. package/lib/runtime/components/react/dist/mpx-picker/multiSelector.jsx +9 -5
  25. package/lib/runtime/components/react/dist/mpx-picker/region.jsx +13 -8
  26. package/lib/runtime/components/react/dist/mpx-picker/selector.jsx +3 -2
  27. package/lib/runtime/components/react/dist/mpx-picker/time.jsx +22 -20
  28. package/lib/runtime/components/react/dist/mpx-picker-view-column.jsx +103 -10
  29. package/lib/runtime/components/react/dist/mpx-picker-view.jsx +149 -54
  30. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +20 -18
  31. package/lib/runtime/components/react/dist/mpx-radio.jsx +29 -43
  32. package/lib/runtime/components/react/dist/mpx-root-portal.jsx +8 -4
  33. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +36 -27
  34. package/lib/runtime/components/react/dist/mpx-swiper/carouse.jsx +141 -75
  35. package/lib/runtime/components/react/dist/mpx-swiper/index.jsx +16 -7
  36. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +20 -11
  37. package/lib/runtime/components/react/dist/mpx-switch.jsx +18 -14
  38. package/lib/runtime/components/react/dist/mpx-text.jsx +20 -35
  39. package/lib/runtime/components/react/dist/mpx-textarea.jsx +1 -1
  40. package/lib/runtime/components/react/dist/mpx-view.jsx +296 -210
  41. package/lib/runtime/components/react/dist/mpx-web-view.jsx +11 -7
  42. package/lib/runtime/components/react/dist/parser.js +218 -0
  43. package/lib/runtime/components/react/dist/useNodesRef.js +1 -5
  44. package/lib/runtime/components/react/dist/utils.jsx +445 -0
  45. package/lib/runtime/components/react/getInnerListeners.ts +18 -8
  46. package/lib/runtime/components/react/mpx-button.tsx +83 -91
  47. package/lib/runtime/components/react/mpx-checkbox-group.tsx +50 -43
  48. package/lib/runtime/components/react/mpx-checkbox.tsx +56 -64
  49. package/lib/runtime/components/react/mpx-form.tsx +51 -22
  50. package/lib/runtime/components/react/mpx-icon.tsx +31 -27
  51. package/lib/runtime/components/react/mpx-image/index.tsx +54 -47
  52. package/lib/runtime/components/react/mpx-image/svg.tsx +5 -3
  53. package/lib/runtime/components/react/mpx-input.tsx +59 -38
  54. package/lib/runtime/components/react/mpx-label.tsx +55 -59
  55. package/lib/runtime/components/react/mpx-movable-area.tsx +40 -25
  56. package/lib/runtime/components/react/mpx-movable-view.tsx +29 -29
  57. package/lib/runtime/components/react/mpx-navigator.tsx +2 -2
  58. package/lib/runtime/components/react/mpx-picker/date.tsx +4 -4
  59. package/lib/runtime/components/react/mpx-picker/index.tsx +12 -11
  60. package/lib/runtime/components/react/mpx-picker/multiSelector.tsx +17 -13
  61. package/lib/runtime/components/react/mpx-picker/region.tsx +23 -19
  62. package/lib/runtime/components/react/mpx-picker/selector.tsx +7 -7
  63. package/lib/runtime/components/react/mpx-picker/time.tsx +29 -31
  64. package/lib/runtime/components/react/mpx-picker/type.ts +1 -1
  65. package/lib/runtime/components/react/mpx-picker-view-column.tsx +149 -20
  66. package/lib/runtime/components/react/mpx-picker-view.tsx +180 -63
  67. package/lib/runtime/components/react/mpx-radio-group.tsx +51 -47
  68. package/lib/runtime/components/react/mpx-radio.tsx +57 -72
  69. package/lib/runtime/components/react/mpx-root-portal.tsx +10 -8
  70. package/lib/runtime/components/react/mpx-scroll-view.tsx +136 -104
  71. package/lib/runtime/components/react/mpx-swiper/carouse.tsx +175 -96
  72. package/lib/runtime/components/react/mpx-swiper/index.tsx +21 -9
  73. package/lib/runtime/components/react/mpx-swiper/type.ts +16 -5
  74. package/lib/runtime/components/react/mpx-swiper-item.tsx +48 -14
  75. package/lib/runtime/components/react/mpx-switch.tsx +46 -24
  76. package/lib/runtime/components/react/mpx-text.tsx +38 -45
  77. package/lib/runtime/components/react/mpx-textarea.tsx +1 -1
  78. package/lib/runtime/components/react/mpx-view.tsx +401 -241
  79. package/lib/runtime/components/react/mpx-web-view.tsx +22 -22
  80. package/lib/runtime/components/react/parser.ts +245 -0
  81. package/lib/runtime/components/react/types/common.ts +4 -4
  82. package/lib/runtime/components/react/types/global.d.ts +24 -2
  83. package/lib/runtime/components/react/useNodesRef.ts +1 -7
  84. package/lib/runtime/components/react/utils.tsx +524 -0
  85. package/lib/runtime/components/web/mpx-scroll-view.vue +25 -5
  86. package/lib/style-compiler/index.js +5 -4
  87. package/lib/template-compiler/compiler.js +133 -161
  88. package/lib/template-compiler/gen-node-react.js +1 -3
  89. package/lib/utils/const.js +2 -1
  90. package/lib/web/processStyles.js +2 -1
  91. package/lib/web/processTemplate.js +2 -3
  92. package/lib/wxml/loader.js +1 -1
  93. package/package.json +7 -4
  94. package/lib/runtime/components/react/dist/utils.js +0 -148
  95. package/lib/runtime/components/react/utils.ts +0 -170
@@ -1,12 +1,11 @@
1
- import { forwardRef, JSX, useEffect } from 'react'
2
- // @ts-ignore
3
- import { noop } from '@mpxjs/utils'
1
+ import { forwardRef, JSX, useEffect, useRef } from 'react'
2
+ import { noop, warn } from '@mpxjs/utils'
4
3
  import { Portal } from '@ant-design/react-native'
5
4
  import { getCustomEvent } from './getInnerListeners'
6
5
  import { promisify, redirectTo, navigateTo, navigateBack, reLaunch, switchTab } from '@mpxjs/api-proxy'
7
- // @ts-ignore
8
6
  import { WebView } from 'react-native-webview'
9
7
  import useNodesRef, { HandlerRef } from './useNodesRef'
8
+ import { WebViewNavigationEvent, WebViewErrorEvent, WebViewMessageEvent } from 'react-native-webview/lib/WebViewTypes'
10
9
 
11
10
  type OnMessageCallbackEvent = {
12
11
  detail: {
@@ -21,10 +20,11 @@ type CommonCallbackEvent = {
21
20
  }
22
21
 
23
22
  interface WebViewProps {
24
- src: string
23
+ src?: string
25
24
  bindmessage?: (event: OnMessageCallbackEvent) => void
26
25
  bindload?: (event: CommonCallbackEvent) => void
27
26
  binderror?: (event: CommonCallbackEvent) => void
27
+ [x: string]: any
28
28
  }
29
29
 
30
30
  interface PayloadData {
@@ -42,27 +42,25 @@ interface NativeEvent {
42
42
  data: string
43
43
  }
44
44
 
45
- interface LoadRes {
46
- timeStamp: string,
47
- nativeEvent: NativeEvent
48
- }
49
-
50
45
  interface FormRef {
51
46
  postMessage: (value: any) => void;
52
47
  }
53
48
 
54
49
  const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((props, ref): JSX.Element => {
55
50
  const { src, bindmessage = noop, bindload = noop, binderror = noop } = props
56
-
51
+ if (props.style) {
52
+ warn('The web-view component does not support the style prop.')
53
+ }
57
54
  const defaultWebViewStyle = {
58
- position: 'absolute',
59
- left: 0,
60
- right: 0,
61
- top: 0,
62
- bottom: 0
55
+ position: 'absolute' as 'absolute' | 'relative' | 'static',
56
+ left: 0 as number,
57
+ right: 0 as number,
58
+ top: 0 as number,
59
+ bottom: 0 as number
63
60
  }
64
61
 
65
- const { nodeRef: webViewRef } = useNodesRef<WebView, WebViewProps>(props, ref, {
62
+ const webViewRef = useRef<WebView>(null)
63
+ useNodesRef<WebView, WebViewProps>(props, ref, webViewRef, {
66
64
  defaultStyle: defaultWebViewStyle
67
65
  })
68
66
 
@@ -83,7 +81,7 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
83
81
  handleUnload()
84
82
  }
85
83
  }, [])
86
- const _load = function (res: LoadRes) {
84
+ const _load = function (res: WebViewNavigationEvent) {
87
85
  const result = {
88
86
  type: 'load',
89
87
  timeStamp: res.timeStamp,
@@ -93,7 +91,7 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
93
91
  }
94
92
  bindload(result)
95
93
  }
96
- const _error = function (res: LoadRes) {
94
+ const _error = function (res: WebViewErrorEvent) {
97
95
  const result = {
98
96
  type: 'error',
99
97
  timeStamp: res.timeStamp,
@@ -103,13 +101,15 @@ const _WebView = forwardRef<HandlerRef<WebView, WebViewProps>, WebViewProps>((pr
103
101
  }
104
102
  binderror(result)
105
103
  }
106
- const _message = function (res: LoadRes) {
107
- let data: MessageData
104
+ const _message = function (res: WebViewMessageEvent) {
105
+ let data: MessageData = {}
108
106
  let asyncCallback
109
107
  const navObj = promisify({ redirectTo, navigateTo, navigateBack, reLaunch, switchTab })
110
108
  try {
111
109
  const nativeEventData = res.nativeEvent?.data
112
- data = JSON.parse(nativeEventData)
110
+ if (typeof nativeEventData === 'string') {
111
+ data = JSON.parse(nativeEventData)
112
+ }
113
113
  } catch (e) {
114
114
  data = {}
115
115
  }
@@ -0,0 +1,245 @@
1
+ interface Token {
2
+ type: string
3
+ value: string | number
4
+ }
5
+
6
+ interface ExpressionNode {
7
+ type: 'NUMBER'
8
+ value: number
9
+ }
10
+
11
+ export class ExpressionParser {
12
+ private tokens: Token[]
13
+ private formatter: (val: string) => number
14
+ private functions: { [key: string]: (...args: number[]) => number }
15
+ private current: number
16
+
17
+ constructor (input: string, formatter: (val: string) => number = val => parseFloat(val), functions: { [key: string]: (...args: number[]) => number } = {}) {
18
+ this.tokens = this.tokenize(input)
19
+ this.formatter = formatter
20
+ this.functions = functions
21
+ this.current = 0
22
+ }
23
+
24
+ tokenize (input: string): Token[] {
25
+ const tokens: Token[] = []
26
+ const regex = /(\d+\.?\d*(?:px|rpx|%|vw|vh)?|[+\-*/(),]|\b[a-zA-Z_][a-zA-Z0-9_]*\b)/g
27
+ let match: RegExpExecArray | null
28
+ while ((match = regex.exec(input))) {
29
+ if (/^\d+\.?\d*(?:px|rpx|%|vw|vh)?$/.test(match[0])) {
30
+ const lastToken = tokens[tokens.length - 1]
31
+ const last2Token = tokens[tokens.length - 2]
32
+ if (lastToken?.type === '-' && (!last2Token || /^[+\-*/(,]$/.test(last2Token?.type))) {
33
+ tokens.pop()
34
+ tokens.push({
35
+ type: 'NUMBER',
36
+ value: '-' + match[0]
37
+ })
38
+ } else {
39
+ tokens.push({
40
+ type: 'NUMBER',
41
+ value: match[0]
42
+ })
43
+ }
44
+ } else {
45
+ tokens.push({
46
+ type: match[0],
47
+ value: match[0]
48
+ })
49
+ }
50
+ }
51
+ return tokens
52
+ }
53
+
54
+ parse (): ExpressionNode {
55
+ return this.expression()
56
+ }
57
+
58
+ private expression (): ExpressionNode {
59
+ let node = this.term()
60
+ while (this.current < this.tokens.length &&
61
+ (this.tokens[this.current].type === '+' || this.tokens[this.current].type === '-')) {
62
+ const operator = this.tokens[this.current].type
63
+ this.current++
64
+ const right = this.term()
65
+ node = this.applyOperator(operator, node, right)
66
+ }
67
+ return node
68
+ }
69
+
70
+ private term (): ExpressionNode {
71
+ let node = this.factor()
72
+ while (this.current < this.tokens.length &&
73
+ (this.tokens[this.current].type === '*' || this.tokens[this.current].type === '/')) {
74
+ const operator = this.tokens[this.current].type
75
+ this.current++
76
+ const right = this.factor()
77
+ node = this.applyOperator(operator, node, right)
78
+ }
79
+ return node
80
+ }
81
+
82
+ private factor (): ExpressionNode {
83
+ const token = this.tokens[this.current]
84
+ if (token.type === 'NUMBER') {
85
+ this.current++
86
+ const numericValue = this.formatter(token.value as string)
87
+ return { type: 'NUMBER', value: numericValue }
88
+ } else if (token.type === '(') {
89
+ this.current++
90
+ const node = this.expression()
91
+ if (this.tokens[this.current].type !== ')') {
92
+ throw new Error('Expected closing parenthesis')
93
+ }
94
+ this.current++
95
+ return node
96
+ } else if (this.functions[token.type]) {
97
+ this.current++
98
+ if (this.tokens[this.current].type !== '(') {
99
+ throw new Error('Expected opening parenthesis after function')
100
+ }
101
+ this.current++
102
+ const args = this.parseArguments()
103
+ if (this.tokens[this.current].type !== ')') {
104
+ throw new Error('Expected closing parenthesis')
105
+ }
106
+ this.current++
107
+ return this.applyFunction(token.type, args)
108
+ }
109
+ throw new Error(`Unexpected token: ${token.type}`)
110
+ }
111
+
112
+ private parseArguments (): ExpressionNode[] {
113
+ const args: ExpressionNode[] = []
114
+ while (this.current < this.tokens.length && this.tokens[this.current].type !== ')') {
115
+ args.push(this.expression())
116
+ if (this.tokens[this.current].type === ',') {
117
+ this.current++
118
+ }
119
+ }
120
+ return args
121
+ }
122
+
123
+ private applyOperator (operator: string, left: ExpressionNode, right: ExpressionNode): ExpressionNode {
124
+ const leftVal = left.value
125
+ const rightVal = right.value
126
+ let result: number
127
+ switch (operator) {
128
+ case '+': result = leftVal + rightVal; break
129
+ case '-': result = leftVal - rightVal; break
130
+ case '*': result = leftVal * rightVal; break
131
+ case '/': result = leftVal / rightVal; break
132
+ default: throw new Error(`Unknown operator: ${operator}`)
133
+ }
134
+ return { type: 'NUMBER', value: result }
135
+ }
136
+
137
+ private applyFunction (func: string, args: ExpressionNode[]): ExpressionNode {
138
+ if (args.some(arg => arg.type !== 'NUMBER')) {
139
+ throw new Error('Function arguments must be numbers')
140
+ }
141
+ const numericArgs = args.map(arg => arg.value)
142
+ if (this.functions[func]) {
143
+ return { type: 'NUMBER', value: this.functions[func].apply(null, numericArgs) }
144
+ } else {
145
+ throw new Error(`Unknown function: ${func}`)
146
+ }
147
+ }
148
+ }
149
+
150
+ interface FuncInfo {
151
+ start: number
152
+ end: number
153
+ args: string[]
154
+ }
155
+
156
+ export function parseFunc (str: string, funcName: string): FuncInfo[] {
157
+ const regex = new RegExp(`${funcName}\\(`, 'g')
158
+ const result: FuncInfo[] = []
159
+ let match: RegExpExecArray | null
160
+
161
+ while ((match = regex.exec(str)) !== null) {
162
+ const start = match.index
163
+ let i = start + funcName.length + 1
164
+ let depth = 1
165
+ const args: string[] = []
166
+ let arg = ''
167
+
168
+ while (depth && i < str.length) {
169
+ if (depth === 1 && (str[i] === ',' || str[i] === ')')) {
170
+ args.push(arg.trim())
171
+ arg = ''
172
+ } else {
173
+ arg += str[i]
174
+ }
175
+ switch (str[i]) {
176
+ case '(':
177
+ depth++
178
+ break
179
+ case ')':
180
+ depth--
181
+ break
182
+ default:
183
+ // Do nothing
184
+ }
185
+ i++
186
+ }
187
+
188
+ const end = regex.lastIndex = i
189
+ result.push({
190
+ start,
191
+ end,
192
+ args
193
+ })
194
+ }
195
+
196
+ return result
197
+ }
198
+
199
+ interface Replacement {
200
+ start: number
201
+ end: number
202
+ content: string
203
+ }
204
+
205
+ export class ReplaceSource {
206
+ private _source: string
207
+ private _replacements: Replacement[]
208
+
209
+ constructor (source: string) {
210
+ this._source = source
211
+ this._replacements = []
212
+ }
213
+
214
+ replace (start: number, end: number, content: string): void {
215
+ this._replacements.push({ start, end, content })
216
+ }
217
+
218
+ source (): string {
219
+ if (this._replacements.length === 0) {
220
+ return this._source
221
+ }
222
+ let current = this._source
223
+ let pos = 0
224
+ const result: string[] = []
225
+
226
+ for (const replacement of this._replacements) {
227
+ const start = Math.floor(replacement.start)
228
+ const end = Math.floor(replacement.end) + 1
229
+ if (pos < start) {
230
+ const offset = start - pos
231
+ result.push(current.slice(0, offset))
232
+ current = current.slice(offset)
233
+ pos = start
234
+ }
235
+ result.push(replacement.content)
236
+ if (pos < end) {
237
+ const offset = end - pos
238
+ current = current.slice(offset)
239
+ pos = end
240
+ }
241
+ }
242
+ result.push(current)
243
+ return result.join('')
244
+ }
245
+ }
@@ -1,12 +1,12 @@
1
- import { ViewStyle, ImageResizeMode} from 'react-native'
1
+ import { ViewStyle, ImageResizeMode } from 'react-native'
2
2
 
3
3
  type NumberVal = number | `${number}%`
4
- type backgroundPositionList = [ 'left'| 'right', NumberVal, 'top' | 'bottom', NumberVal ] | []
4
+ type backgroundPositionList = [ 'left'| 'right', NumberVal, 'top' | 'bottom', NumberVal ] | []
5
5
 
6
6
  export type ExtendedViewStyle = ViewStyle & {
7
7
  backgroundImage?: string
8
- backgroundSize?: Array<ImageResizeMode | string> | ImageResizeMode | string
8
+ backgroundSize?: Array<NumberVal | 'auto' | 'contain' | 'cover'>
9
9
  borderRadius?: string | number
10
10
  backgroundPosition?: backgroundPositionList
11
11
  [key: string]: any
12
- }
12
+ }
@@ -15,5 +15,27 @@ declare module 'react-native-svg/css' {
15
15
  }
16
16
 
17
17
  declare module '@mpxjs/utils' {
18
- export function isEmptyObject(obj: Record<string, any>): boolean;
19
- }
18
+ export function isEmptyObject (obj: Object): boolean
19
+ export function hasOwn (obj: Object, key: string): boolean
20
+ export function noop (...arg: any): void
21
+ export function diffAndCloneA<A, B> (a: A, b?: B): {
22
+ clone: A
23
+ diff: boolean
24
+ diffData: Object | null
25
+ }
26
+ export function isObject (value): value is Object
27
+ export function error (msg: string, location?: string, e?: any): void
28
+ export function warn (msg: string, location?: string, e?: any): void
29
+ export function getFocusedNavigation (): {
30
+ insets: {
31
+ top: number
32
+ bottom: number
33
+ left: number
34
+ right: number
35
+ }
36
+ } | undefined
37
+ }
38
+
39
+ declare let global: {
40
+ __formatValue (value: string): string | number
41
+ } & Record<string, any>
@@ -1,6 +1,5 @@
1
1
  import { useRef, useImperativeHandle, RefObject, ForwardedRef } from 'react'
2
2
 
3
-
4
3
  type Obj = Record<string, any>
5
4
 
6
5
  export type HandlerRef<T, P> = {
@@ -11,8 +10,7 @@ export type HandlerRef<T, P> = {
11
10
  }
12
11
  }
13
12
 
14
- export default function useNodesRef<T, P>(props: P, ref: ForwardedRef<HandlerRef<T, P>>, instance:Obj = {} ) {
15
- const nodeRef = useRef<T>(null)
13
+ export default function useNodesRef<T, P> (props: P, ref: ForwardedRef<HandlerRef<T, P>>, nodeRef: RefObject<T>, instance:Obj = {}) {
16
14
  const _props = useRef<P | null>(null)
17
15
  _props.current = props
18
16
 
@@ -27,8 +25,4 @@ export default function useNodesRef<T, P>(props: P, ref: ForwardedRef<HandlerRef
27
25
  }
28
26
  }
29
27
  })
30
-
31
- return {
32
- nodeRef
33
- }
34
28
  }