@dolphinweex/weex-harmony 0.1.7 → 0.1.9

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": "@dolphinweex/weex-harmony",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "weex harmony adapter",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -42,6 +42,7 @@ export default {
42
42
  width: this.width,
43
43
  height: this.height,
44
44
  data: this.data,
45
+ handleProgresscycleTap: this.handleProgresscycleTap,
45
46
  };
46
47
  },
47
48
  },
@@ -51,6 +52,9 @@ export default {
51
52
  },
52
53
 
53
54
  methods: {
55
+ handleProgresscycleTap (res){
56
+ this.$emit("progresscycleTap", res);
57
+ },
54
58
  },
55
59
  };
56
60
  </script>
@@ -3,7 +3,7 @@ const parser = require('@babel/parser')
3
3
  const traverse = require('@babel/traverse').default
4
4
  const generator = require('@babel/generator').default
5
5
  const t = require('@babel/types')
6
- const { parse } = require('@dolphinweex/himalaya')
6
+ const { parse, stringify } = require('@dolphinweex/himalaya')
7
7
  const path = require('path')
8
8
 
9
9
  let currentModulePath = ''
@@ -21,6 +21,7 @@ const baseUrl = '../src/components/'
21
21
  const componentMap = require("./index")
22
22
 
23
23
  const componentSet = new Set()
24
+ const vBindMap = new Map()
24
25
 
25
26
  /**
26
27
  * 判断是否为windows环境
@@ -66,16 +67,60 @@ function hasTargetElement(tagName) {
66
67
  * 遍历html节点,将需要引入的组件添加到componentSet中
67
68
  * @param {*} node html节点
68
69
  */
69
- function traverseNode(node) {
70
+ function traverseNode(node, parent = null) {
70
71
  if (node.type === 'element') {
71
- const {hasElement, componentName} = hasTargetElement(node.tagName)
72
+ const { hasElement, componentName } = hasTargetElement(node.tagName);
72
73
  if (hasElement) {
73
- componentSet.add(componentName)
74
+ componentSet.add(componentName);
74
75
  }
76
+
77
+ // 判断父节点是否有 v-for,若父节点是 <template> 且有 v-for,则跳过
78
+ const isInsideVFor = parent && parent.attributes && parent.attributes.some(attr => attr.key === 'v-for');
79
+
80
+ if (!isInsideVFor) { // 如果不在 v-for 中,才处理 v-bind
81
+ if (node.attributes) {
82
+ node.attributes.forEach((attr, index) => {
83
+ if (attr.key === 'v-bind') {
84
+ // 如果是对象绑定,处理 style 属性
85
+ if (attr.value.trim().startsWith('{') && attr.value.trim().endsWith('}')) {
86
+ const parsedAst = parser.parseExpression(attr.value);
87
+ const styleProperty = parsedAst.properties.find(prop => prop.key && prop.key.name === 'style');
88
+ if (styleProperty) {
89
+ const styleValue = styleProperty.value;
90
+
91
+ // style 的值套上 _px2rem 方法
92
+ styleProperty.value = {
93
+ type: 'CallExpression',
94
+ callee: {
95
+ type: 'Identifier',
96
+ name: '_px2rem'
97
+ },
98
+ arguments: [styleValue] // 将原始的 style 对象作为参数传递给 _px2rem
99
+ };
100
+ }
101
+ node.attributes[index] = {
102
+ key: 'v-bind',
103
+ value: generator(parsedAst).code
104
+ };
105
+ } else {
106
+ const customComputedName = `customVBindComputed${index}`; // 自定义的名字
107
+ vBindMap.set(customComputedName, {
108
+ computedName: attr.value,
109
+ customComputedName
110
+ });
111
+ // 更新属性值为自定义名字
112
+ node.attributes[index].value = customComputedName;
113
+ }
114
+ }
115
+ });
116
+ }
117
+ }
118
+
119
+ // 继续递归处理子节点
75
120
  if (node.children) {
76
121
  node.children.forEach((child) => {
77
- traverseNode(child)
78
- })
122
+ traverseNode(child, node); // 将当前节点作为 parent 传递给子节点
123
+ });
79
124
  }
80
125
  }
81
126
  }
@@ -128,6 +173,56 @@ function getComponentLoc(name) {
128
173
  return getAbsPath(address)
129
174
  }
130
175
  }
176
+ function createComputedProperty(vBindValue, computedName) {
177
+ return t.objectProperty(
178
+ t.identifier('computed'), // 为对象添加 computed 属性
179
+ t.objectExpression([ // 计算属性
180
+ t.objectProperty(
181
+ t.identifier(computedName), // 计算属性的名称
182
+ t.functionExpression( // 计算属性的值是一个函数
183
+ null, //不需要传参
184
+ [],
185
+ t.blockStatement([ // 函数体是一个块语句,包含多个语句
186
+ t.ifStatement(// if (this.scrollerWrapProps && this.scrollerWrapProps.style)
187
+ t.logicalExpression(
188
+ '&&', // 使用逻辑“与”操作符
189
+ t.memberExpression(t.thisExpression(), t.identifier(vBindValue)), // this.scrollerWrapProps
190
+ t.memberExpression(
191
+ t.memberExpression(t.thisExpression(), t.identifier(vBindValue)),
192
+ t.identifier('style') // this.scrollerWrapProps.style
193
+ )
194
+ ),
195
+ t.blockStatement([ // 如果条件符合妖气
196
+ t.expressionStatement(
197
+ t.assignmentExpression(
198
+ '=', // 赋值操作
199
+ t.memberExpression(
200
+ t.memberExpression(t.thisExpression(), t.identifier(vBindValue)),
201
+ t.identifier('style') // this.scrollerWrapProps.style
202
+ ),
203
+ t.callExpression(
204
+ t.identifier('this._px2rem'), // weex._px2remFn 函数
205
+ [//函数传参
206
+ t.memberExpression(
207
+ t.memberExpression(t.thisExpression(), t.identifier(vBindValue)),
208
+ t.identifier('style')
209
+ )
210
+ ]
211
+ )
212
+ )
213
+ )
214
+ ]),
215
+ ),
216
+ t.returnStatement( //return 值
217
+ t.memberExpression(t.thisExpression(), t.identifier(vBindValue)) // this.scrollerWrapProps
218
+ )
219
+ ])
220
+ )
221
+ )
222
+ ])
223
+ );
224
+ }
225
+
131
226
 
132
227
  /**
133
228
  * 根据引入的路径获取组件导入的路径
@@ -198,24 +293,70 @@ function addComponentsProperty(data) {
198
293
  })
199
294
  }
200
295
  }
296
+ function addComputedPropertyToScript(scriptAst, vBindMap) {
297
+ vBindMap.forEach((obj,customComputedName) => {
298
+ const computedProperty = createComputedProperty(obj.computedName, customComputedName);
299
+ traverse(scriptAst, {
300
+ ObjectExpression(path) { //对象key value
301
+ const computedNode = path.node.properties.find(
302
+ (property) => property.key && property.key.name === 'computed'
303
+ );
304
+ if (computedNode) { // 如果找到了computed,检查是否已经存在该计算属性
305
+ const existingProperty = computedNode.value.properties.find(
306
+ (property) => property.key && property.key.name === customComputedName
307
+ );
308
+
309
+ if (!existingProperty) { // 如果没有相同名称的计算属性,插入新计算属性
310
+ computedNode.value.properties.push(computedProperty.value.properties[0]);
311
+ }
312
+ }
313
+ },
314
+ // 判断computed是否存在
315
+ ExportDefaultDeclaration(path) {
316
+ if (t.isObjectExpression(path.node.declaration)) {
317
+ const properties = path.node.declaration.properties;
318
+ let computedPropertyNode = properties.find((item) => item.key.name === 'computed');
319
+ if (!computedPropertyNode) {//创建
320
+ computedPropertyNode = t.objectProperty(t.identifier('computed'), t.objectExpression([]));
321
+ properties.push(computedPropertyNode);
322
+ }
323
+ // 继续检查是否已存在相同名称的计算属性
324
+ const existingProperty = computedPropertyNode.value.properties.find(
325
+ (property) => property.key && property.key.name === customComputedName
326
+ );
327
+
328
+ if (!existingProperty) {
329
+ computedPropertyNode.value.properties.push(computedProperty.value.properties[0]);
330
+ }
331
+ }
332
+ }
333
+ });
334
+ });
335
+ }
201
336
 
202
337
  module.exports = function (source) {
203
338
  if (process.env.ISHARMONY === 'true') {
204
339
  currentModulePath = this.resourcePath
205
340
  const moduleResoveRet = compiler.parseComponent(source)
206
- const { template, script, styles } = moduleResoveRet
341
+ let {
342
+ template,
343
+ script,
344
+ styles
345
+ } = moduleResoveRet
207
346
  const tempAst = parse(template.content)
208
347
  tempAst.forEach((node) => {
209
348
  traverseNode(node)
210
349
  })
211
-
212
- // 单模块中引入了自定义组件
350
+
351
+ moduleResoveRet.template.content = stringify(tempAst);
352
+
353
+ const scriptAst = parser.parse(script.content, {
354
+ sourceType: 'module',
355
+ // 在此添加需要解析的插件
356
+ })
357
+
358
+ // 单模块中引入了自定义组件 if (componentSet.size !== 0) {
213
359
  if (componentSet.size !== 0) {
214
- const scriptAst = parser.parse(script.content, {
215
- sourceType: 'module',
216
- // 在此添加需要解析的插件
217
- })
218
-
219
360
  traverse(scriptAst, {
220
361
  Program(path) {
221
362
  componentSet.forEach((componentName) => {
@@ -260,14 +401,21 @@ module.exports = function (source) {
260
401
  }
261
402
  }
262
403
  })
263
-
264
404
  // 清除componentSet中的缓存,准备进入下次循环
265
405
  componentSet.clear()
406
+ }
266
407
 
267
- const { code } = generator(scriptAst)
268
- moduleResoveRet.script.content = code
269
- source = generateCode(moduleResoveRet)
408
+ if (vBindMap.size !== 0) {
409
+ addComputedPropertyToScript(scriptAst, vBindMap);
410
+ vBindMap.clear()
270
411
  }
412
+
413
+ const {
414
+ code
415
+ } = generator(scriptAst)
416
+ moduleResoveRet.script.content = code
417
+ source = generateCode(moduleResoveRet)
271
418
  }
272
419
  return source
420
+
273
421
  }
package/src/.DS_Store DELETED
Binary file
Binary file