@mpxjs/webpack-plugin 2.10.4-beta.9 → 2.10.5-beta.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.
Files changed (70) hide show
  1. package/lib/index.js +10 -1
  2. package/lib/json-compiler/helper.js +1 -4
  3. package/lib/platform/style/wx/index.js +22 -21
  4. package/lib/platform/template/wx/index.js +21 -1
  5. package/lib/react/processScript.js +9 -1
  6. package/lib/react/script-helper.js +5 -1
  7. package/lib/resolver/PackageEntryPlugin.js +3 -1
  8. package/lib/runtime/components/react/dist/mpx-button.jsx +9 -4
  9. package/lib/runtime/components/react/dist/mpx-canvas/Image.js +2 -4
  10. package/lib/runtime/components/react/dist/mpx-canvas/index.jsx +20 -17
  11. package/lib/runtime/components/react/dist/mpx-checkbox-group.jsx +7 -2
  12. package/lib/runtime/components/react/dist/mpx-checkbox.jsx +7 -2
  13. package/lib/runtime/components/react/dist/mpx-icon/index.jsx +7 -2
  14. package/lib/runtime/components/react/dist/mpx-image.jsx +9 -2
  15. package/lib/runtime/components/react/dist/mpx-input.jsx +7 -2
  16. package/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view.jsx +1 -1
  17. package/lib/runtime/components/react/dist/mpx-label.jsx +7 -2
  18. package/lib/runtime/components/react/dist/mpx-movable-area.jsx +7 -2
  19. package/lib/runtime/components/react/dist/mpx-movable-view.jsx +76 -42
  20. package/lib/runtime/components/react/dist/mpx-picker/index.jsx +11 -13
  21. package/lib/runtime/components/react/dist/mpx-picker-view/index.jsx +8 -7
  22. package/lib/runtime/components/react/dist/mpx-picker-view-column/index.jsx +28 -10
  23. package/lib/runtime/components/react/dist/mpx-radio-group.jsx +9 -2
  24. package/lib/runtime/components/react/dist/mpx-radio.jsx +7 -2
  25. package/lib/runtime/components/react/dist/mpx-rich-text/index.jsx +7 -2
  26. package/lib/runtime/components/react/dist/mpx-scroll-view.jsx +13 -4
  27. package/lib/runtime/components/react/dist/mpx-sticky-header.jsx +6 -3
  28. package/lib/runtime/components/react/dist/mpx-swiper-item.jsx +11 -9
  29. package/lib/runtime/components/react/dist/mpx-swiper.jsx +82 -36
  30. package/lib/runtime/components/react/dist/mpx-switch.jsx +7 -2
  31. package/lib/runtime/components/react/dist/mpx-text.jsx +7 -2
  32. package/lib/runtime/components/react/dist/mpx-video.jsx +7 -2
  33. package/lib/runtime/components/react/dist/mpx-view.jsx +13 -8
  34. package/lib/runtime/components/react/dist/useAnimationHooks.js +27 -4
  35. package/lib/runtime/components/react/dist/utils.jsx +87 -97
  36. package/lib/runtime/components/react/mpx-button.tsx +12 -3
  37. package/lib/runtime/components/react/mpx-canvas/Image.ts +4 -4
  38. package/lib/runtime/components/react/mpx-canvas/index.tsx +24 -17
  39. package/lib/runtime/components/react/mpx-checkbox-group.tsx +9 -1
  40. package/lib/runtime/components/react/mpx-checkbox.tsx +9 -1
  41. package/lib/runtime/components/react/mpx-icon/index.tsx +9 -1
  42. package/lib/runtime/components/react/mpx-image.tsx +38 -19
  43. package/lib/runtime/components/react/mpx-input.tsx +10 -1
  44. package/lib/runtime/components/react/mpx-keyboard-avoiding-view.tsx +1 -1
  45. package/lib/runtime/components/react/mpx-label.tsx +9 -1
  46. package/lib/runtime/components/react/mpx-movable-area.tsx +7 -1
  47. package/lib/runtime/components/react/mpx-movable-view.tsx +75 -42
  48. package/lib/runtime/components/react/mpx-picker/index.tsx +18 -16
  49. package/lib/runtime/components/react/mpx-picker-view/index.tsx +22 -8
  50. package/lib/runtime/components/react/mpx-picker-view-column/index.tsx +36 -32
  51. package/lib/runtime/components/react/mpx-radio-group.tsx +20 -9
  52. package/lib/runtime/components/react/mpx-radio.tsx +9 -1
  53. package/lib/runtime/components/react/mpx-rich-text/index.tsx +10 -2
  54. package/lib/runtime/components/react/mpx-scroll-view.tsx +14 -3
  55. package/lib/runtime/components/react/mpx-sticky-header.tsx +7 -4
  56. package/lib/runtime/components/react/mpx-swiper-item.tsx +11 -19
  57. package/lib/runtime/components/react/mpx-swiper.tsx +95 -38
  58. package/lib/runtime/components/react/mpx-switch.tsx +10 -2
  59. package/lib/runtime/components/react/mpx-text.tsx +10 -2
  60. package/lib/runtime/components/react/mpx-video.tsx +7 -2
  61. package/lib/runtime/components/react/mpx-view.tsx +23 -9
  62. package/lib/runtime/components/react/useAnimationHooks.ts +30 -9
  63. package/lib/runtime/components/react/utils.tsx +95 -102
  64. package/lib/runtime/components/web/mpx-web-view.vue +1 -1
  65. package/lib/runtime/mpxGlobal.js +1 -0
  66. package/lib/runtime/optionProcessor.d.ts +5 -0
  67. package/lib/template-compiler/bind-this.js +8 -7
  68. package/lib/template-compiler/compiler.js +1 -1
  69. package/lib/wxs/pre-loader.js +1 -0
  70. package/package.json +2 -2
@@ -1,4 +1,4 @@
1
- import { useEffect, useCallback, useMemo, useRef, ReactNode, ReactElement, isValidElement, useContext, useState, Dispatch, SetStateAction, Children, cloneElement } from 'react'
1
+ import { useEffect, useCallback, useMemo, useRef, ReactNode, ReactElement, isValidElement, useContext, useState, Dispatch, SetStateAction, Children, cloneElement, createElement } from 'react'
2
2
  import { LayoutChangeEvent, TextStyle, ImageProps, Image } from 'react-native'
3
3
  import { isObject, isFunction, isNumber, hasOwn, diffAndCloneA, error, warn } from '@mpxjs/utils'
4
4
  import { VarContext, ScrollViewContext, RouteContext } from './context'
@@ -32,6 +32,7 @@ const unoVarDecRegExp = /^--un-/
32
32
  const unoVarUseRegExp = /var\(--un-/
33
33
  const calcUseRegExp = /calc\(/
34
34
  const envUseRegExp = /env\(/
35
+ const filterRegExp = /(calc|env|%)/
35
36
 
36
37
  const safeAreaInsetMap: Record<string, 'top' | 'right' | 'bottom' | 'left'> = {
37
38
  'safe-area-inset-top': 'top',
@@ -168,6 +169,10 @@ interface PercentConfig {
168
169
  parentHeight?: number
169
170
  }
170
171
 
172
+ interface PositionMeta {
173
+ hasPositionFixed: boolean
174
+ }
175
+
171
176
  function resolvePercent (value: string | number | undefined, key: string, percentConfig: PercentConfig): string | number | undefined {
172
177
  if (!(typeof value === 'string' && PERCENT_REGEX.test(value))) return value
173
178
  let base
@@ -222,10 +227,11 @@ function resolveVar (input: string, varContext: Record<string, any>) {
222
227
  return global.__formatValue(replaced.source())
223
228
  }
224
229
 
225
- function transformVar (styleObj: Record<string, any>, varKeyPaths: Array<Array<string>>, varContext: Record<string, any>) {
230
+ function transformVar (styleObj: Record<string, any>, varKeyPaths: Array<Array<string>>, varContext: Record<string, any>, visitOther: (arg: VisitorArg) => void) {
226
231
  varKeyPaths.forEach((varKeyPath) => {
227
232
  setStyle(styleObj, varKeyPath, ({ target, key, value }) => {
228
233
  target[key] = resolveVar(value, varContext)
234
+ visitOther({ target, key, value: target[key], keyPath: varKeyPath })
229
235
  })
230
236
  })
231
237
  }
@@ -267,13 +273,17 @@ function transformCalc (styleObj: Record<string, any>, calcKeyPaths: Array<Array
267
273
  })
268
274
  }
269
275
 
270
- const stringifyProps = ['fontWeight']
271
276
  function transformStringify (styleObj: Record<string, any>) {
272
- stringifyProps.forEach((prop) => {
273
- if (isNumber(styleObj[prop])) {
274
- styleObj[prop] = '' + styleObj[prop]
275
- }
276
- })
277
+ if (isNumber(styleObj.fontWeight)) {
278
+ styleObj.fontWeight = '' + styleObj.fontWeight
279
+ }
280
+ }
281
+
282
+ function transformPosition (styleObj: Record<string, any>, meta: PositionMeta) {
283
+ if (styleObj.position === 'fixed') {
284
+ styleObj.position = 'absolute'
285
+ meta.hasPositionFixed = true
286
+ }
277
287
  }
278
288
 
279
289
  interface TransformStyleConfig {
@@ -288,17 +298,19 @@ export function useTransformStyle (styleObj: Record<string, any> = {}, { enableV
288
298
  const varStyle: Record<string, any> = {}
289
299
  const unoVarStyle: Record<string, any> = {}
290
300
  const normalStyle: Record<string, any> = {}
291
- const normalStyleRef = useRef<Record<string, any>>({})
292
- const normalStyleChangedRef = useRef(false)
293
301
  let hasVarDec = false
294
302
  let hasVarUse = false
303
+ let hasSelfPercent = false
295
304
  const varKeyPaths: Array<Array<string>> = []
296
305
  const unoVarKeyPaths: Array<Array<string>> = []
306
+ const percentKeyPaths: Array<Array<string>> = []
307
+ const calcKeyPaths: Array<Array<string>> = []
308
+ const envKeyPaths: Array<Array<string>> = []
297
309
  const [width, setWidth] = useState(0)
298
310
  const [height, setHeight] = useState(0)
299
311
  const navigation = useNavigation()
300
312
 
301
- function varVisitor ({ key, value, keyPath }: VisitorArg) {
313
+ function varVisitor ({ target, key, value, keyPath }: VisitorArg) {
302
314
  if (keyPath.length === 1) {
303
315
  if (unoVarDecRegExp.test(key)) {
304
316
  unoVarStyle[key] = value
@@ -318,10 +330,39 @@ export function useTransformStyle (styleObj: Record<string, any> = {}, { enableV
318
330
  } else if (varUseRegExp.test(value)) {
319
331
  hasVarUse = true
320
332
  varKeyPaths.push(keyPath.slice())
333
+ } else {
334
+ visitOther({ target, key, value, keyPath })
321
335
  }
322
336
  }
323
337
  }
324
338
 
339
+ function envVisitor ({ value, keyPath }: VisitorArg) {
340
+ if (envUseRegExp.test(value)) {
341
+ envKeyPaths.push(keyPath.slice())
342
+ }
343
+ }
344
+
345
+ function calcVisitor ({ value, keyPath }: VisitorArg) {
346
+ if (calcUseRegExp.test(value)) {
347
+ calcKeyPaths.push(keyPath.slice())
348
+ }
349
+ }
350
+
351
+ function percentVisitor ({ key, value, keyPath }: VisitorArg) {
352
+ if (hasOwn(selfPercentRule, key) && PERCENT_REGEX.test(value)) {
353
+ hasSelfPercent = true
354
+ percentKeyPaths.push(keyPath.slice())
355
+ } else if ((key === 'fontSize' || key === 'lineHeight') && PERCENT_REGEX.test(value)) {
356
+ percentKeyPaths.push(keyPath.slice())
357
+ }
358
+ }
359
+
360
+ function visitOther ({ target, key, value, keyPath }: VisitorArg) {
361
+ if (filterRegExp.test(value)) {
362
+ [envVisitor, percentVisitor, calcVisitor].forEach(visitor => visitor({ target, key, value, keyPath }))
363
+ }
364
+ }
365
+
325
366
  // traverse var & generate normalStyle
326
367
  traverseStyle(styleObj, [varVisitor])
327
368
 
@@ -341,104 +382,61 @@ export function useTransformStyle (styleObj: Record<string, any> = {}, { enableV
341
382
  if (diffAndCloneA(varContextRef.current, newVarContext).diff) {
342
383
  varContextRef.current = newVarContext
343
384
  }
344
- transformVar(normalStyle, varKeyPaths, varContextRef.current)
385
+ transformVar(normalStyle, varKeyPaths, varContextRef.current, visitOther)
345
386
  }
346
387
 
347
388
  // apply unocss var
348
389
  if (unoVarKeyPaths.length) {
349
- transformVar(normalStyle, unoVarKeyPaths, unoVarStyle)
390
+ transformVar(normalStyle, unoVarKeyPaths, unoVarStyle, visitOther)
350
391
  }
351
392
 
352
- const { clone, diff } = diffAndCloneA(normalStyle, normalStyleRef.current)
353
- if (diff) {
354
- normalStyleRef.current = clone
355
- normalStyleChangedRef.current = !normalStyleChangedRef.current
393
+ const percentConfig = {
394
+ width,
395
+ height,
396
+ fontSize: normalStyle.fontSize,
397
+ parentWidth,
398
+ parentHeight,
399
+ parentFontSize
356
400
  }
357
401
 
358
- const memoResult = useMemo(() => {
359
- let hasSelfPercent = false
360
- let hasPositionFixed = false
361
- const percentKeyPaths: Array<Array<string>> = []
362
- const calcKeyPaths: Array<Array<string>> = []
363
- const envKeyPaths: Array<Array<string>> = []
364
- // transform can be memoized
365
- function envVisitor ({ value, keyPath }: VisitorArg) {
366
- if (envUseRegExp.test(value)) {
367
- envKeyPaths.push(keyPath.slice())
368
- }
369
- }
370
-
371
- function calcVisitor ({ value, keyPath }: VisitorArg) {
372
- if (calcUseRegExp.test(value)) {
373
- calcKeyPaths.push(keyPath.slice())
374
- }
375
- }
376
-
377
- function percentVisitor ({ key, value, keyPath }: VisitorArg) {
378
- if (hasOwn(selfPercentRule, key) && PERCENT_REGEX.test(value)) {
379
- hasSelfPercent = true
380
- percentKeyPaths.push(keyPath.slice())
381
- } else if ((key === 'fontSize' || key === 'lineHeight') && PERCENT_REGEX.test(value)) {
382
- percentKeyPaths.push(keyPath.slice())
383
- }
384
- }
385
-
386
- function transformPosition (styleObj: Record<string, any>) {
387
- if (styleObj.position === 'fixed') {
388
- hasPositionFixed = true
389
- styleObj.position = 'absolute'
390
- }
391
- }
392
-
393
- // traverse env & calc & percent
394
- traverseStyle(normalStyle, [envVisitor, percentVisitor, calcVisitor])
395
-
396
- const percentConfig = {
397
- width,
398
- height,
399
- fontSize: normalStyle.fontSize,
400
- parentWidth,
401
- parentHeight,
402
- parentFontSize
403
- }
402
+ const positionMeta = {
403
+ hasPositionFixed: false
404
+ }
404
405
 
405
- // apply env
406
- transformEnv(normalStyle, envKeyPaths, navigation)
407
- // apply percent
408
- transformPercent(normalStyle, percentKeyPaths, percentConfig)
409
- // apply calc
410
- transformCalc(normalStyle, calcKeyPaths, (value: string, key: string) => {
411
- if (PERCENT_REGEX.test(value)) {
412
- const resolved = resolvePercent(value, key, percentConfig)
413
- return typeof resolved === 'number' ? resolved : 0
406
+ // apply env
407
+ transformEnv(normalStyle, envKeyPaths, navigation)
408
+ // apply percent
409
+ transformPercent(normalStyle, percentKeyPaths, percentConfig)
410
+ // apply calc
411
+ transformCalc(normalStyle, calcKeyPaths, (value: string, key: string) => {
412
+ if (PERCENT_REGEX.test(value)) {
413
+ const resolved = resolvePercent(value, key, percentConfig)
414
+ return typeof resolved === 'number' ? resolved : 0
415
+ } else {
416
+ const formatted = global.__formatValue(value)
417
+ if (typeof formatted === 'number') {
418
+ return formatted
414
419
  } else {
415
- const formatted = global.__formatValue(value)
416
- if (typeof formatted === 'number') {
417
- return formatted
418
- } else {
419
- warn('calc() only support number, px, rpx, % temporarily.')
420
- return 0
421
- }
420
+ warn('calc() only support number, px, rpx, % temporarily.')
421
+ return 0
422
422
  }
423
- })
424
- // apply position
425
- transformPosition(normalStyle)
426
- // transform number enum stringify
427
- transformStringify(normalStyle)
428
-
429
- return {
430
- normalStyle,
431
- hasSelfPercent,
432
- hasPositionFixed
433
423
  }
434
- }, [normalStyleChangedRef.current, width, height, parentWidth, parentHeight, parentFontSize])
424
+ })
425
+
426
+ // apply position
427
+ transformPosition(normalStyle, positionMeta)
428
+ // transform number enum stringify
429
+ transformStringify(normalStyle)
435
430
 
436
- return extendObject({
431
+ return {
437
432
  hasVarDec,
438
433
  varContextRef,
439
434
  setWidth,
440
- setHeight
441
- }, memoResult)
435
+ setHeight,
436
+ normalStyle,
437
+ hasSelfPercent,
438
+ hasPositionFixed: positionMeta.hasPositionFixed
439
+ }
442
440
  }
443
441
 
444
442
  export interface VisitorArg {
@@ -455,12 +453,7 @@ export function traverseStyle (styleObj: Record<string, any>, visitors: Array<(a
455
453
  target.forEach((value, index) => {
456
454
  const key = String(index)
457
455
  keyPath.push(key)
458
- visitors.forEach(visitor => visitor({
459
- target,
460
- key,
461
- value,
462
- keyPath
463
- }))
456
+ visitors.forEach(visitor => visitor({ target, key, value, keyPath }))
464
457
  traverse(value)
465
458
  keyPath.pop()
466
459
  })
@@ -510,8 +503,8 @@ export function splitProps<T extends Record<string, any>> (props: T): {
510
503
  interface LayoutConfig {
511
504
  props: Record<string, any>
512
505
  hasSelfPercent: boolean
513
- setWidth: Dispatch<SetStateAction<number>>
514
- setHeight: Dispatch<SetStateAction<number>>
506
+ setWidth?: Dispatch<SetStateAction<number>>
507
+ setHeight?: Dispatch<SetStateAction<number>>
515
508
  onLayout?: (event?: LayoutChangeEvent) => void
516
509
  nodeRef: React.RefObject<any>
517
510
  }
@@ -654,7 +647,7 @@ export function renderImage (
654
647
  enableFastImage = false
655
648
  ) {
656
649
  const Component: React.ComponentType<ImageProps | FastImageProps> = enableFastImage ? FastImage : Image
657
- return <Component {...imageProps} />
650
+ return createElement(Component, imageProps)
658
651
  }
659
652
 
660
653
  export function pickStyle (styleObj: Record<string, any> = {}, pickedKeys: Array<string>, callback?: (key: string, val: number | string) => number | string) {
@@ -197,4 +197,4 @@ export default {
197
197
  right: 0;
198
198
  bottom: 0;
199
199
  }
200
- </style>
200
+ </style>
@@ -0,0 +1 @@
1
+ module.exports = Object.create(global)
@@ -4,8 +4,13 @@ declare global {
4
4
  [key: string]: any
5
5
  }
6
6
  }
7
+ interface Window {
8
+ mpxGlobal: Record<string, any>
9
+ }
7
10
  }
8
11
 
12
+ declare const mpxGlobal: Record<string, any>
13
+
9
14
  export function processComponentOption (...args: any): object
10
15
 
11
16
  export function getComponent (...args: any): object
@@ -70,7 +70,7 @@ function getCollectPath (path) {
70
70
  function checkDelAndGetPath (path) {
71
71
  let current = path
72
72
  let delPath = path
73
- let canDel = true
73
+ let canDel = true // 是否可删除,优先级比replace高
74
74
  let ignore = false
75
75
  let replace = false
76
76
 
@@ -80,8 +80,13 @@ function checkDelAndGetPath (path) {
80
80
  if (t.isUnaryExpression(current.parent) && current.key === 'argument') {
81
81
  delPath = current.parentPath
82
82
  } else if (t.isCallExpression(current.parent)) {
83
- const args = current.node.arguments || current.parent.arguments || []
84
- if (args.length === 1) { // case: String(a) || this._p(a)
83
+ const args = current.parent.arguments || []
84
+ if (
85
+ // case: String(a) || this._p(a)
86
+ args.length === 1 ||
87
+ // 除了自身,参数列表里只能是数字或字符串才能删
88
+ (args.every(node => node === current.node || t.isNumericLiteral(node) || t.isStringLiteral(node)))
89
+ ) {
85
90
  delPath = current.parentPath
86
91
  } else {
87
92
  break
@@ -123,24 +128,20 @@ function checkDelAndGetPath (path) {
123
128
 
124
129
  if (t.isCallExpression(parent) && listKey === 'arguments') {
125
130
  canDel = false
126
- break
127
131
  }
128
132
 
129
133
  if (t.isMemberExpression(parent) && parent.computed) {
130
134
  canDel = false
131
- break
132
135
  }
133
136
 
134
137
  if (t.isLogicalExpression(parent)) { // case: a || ((b || c) && d)
135
138
  canDel = false
136
139
  ignore = true
137
- break
138
140
  }
139
141
 
140
142
  if (t.isConditionalExpression(parent)) {
141
143
  if (key === 'test') {
142
144
  canDel = false
143
- break
144
145
  } else {
145
146
  ignore = true
146
147
  replace = true // 继续往上找,判断是否存在if条件等
@@ -2725,8 +2725,8 @@ function processElement (el, root, options, meta) {
2725
2725
  processIf(el)
2726
2726
  processFor(el)
2727
2727
  processRefReact(el, meta)
2728
+ processStyleReact(el, options)
2728
2729
  if (!pass) {
2729
- processStyleReact(el, options)
2730
2730
  processEventReact(el, options)
2731
2731
  processComponentGenerics(el, meta)
2732
2732
  processComponentIs(el, options)
@@ -54,6 +54,7 @@ module.exports = function (content) {
54
54
  ExportNamedDeclaration (path) {
55
55
  if (
56
56
  path.node.declaration &&
57
+ path.node.declaration.declarations &&
57
58
  path.node.declaration.declarations.length === 1 &&
58
59
  path.node.declaration.declarations[0].id.name === path.node.declaration.declarations[0].init.name
59
60
  ) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/webpack-plugin",
3
- "version": "2.10.4-beta.9",
3
+ "version": "2.10.5-beta.1",
4
4
  "description": "mpx compile core",
5
5
  "keywords": [
6
6
  "mpx"
@@ -83,7 +83,7 @@
83
83
  },
84
84
  "devDependencies": {
85
85
  "@d11/react-native-fast-image": "^8.6.12",
86
- "@mpxjs/api-proxy": "^2.10.4",
86
+ "@mpxjs/api-proxy": "^2.10.5 | ^2.10.5-beta.1",
87
87
  "@types/babel-traverse": "^6.25.4",
88
88
  "@types/babel-types": "^7.0.4",
89
89
  "@types/react": "^18.2.79",