-
Notifications
You must be signed in to change notification settings - Fork 33
/
tag.js
99 lines (88 loc) · 2.63 KB
/
tag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @file build
* @author zhangjingyuan02
*/
/* eslint-disable fecs-no-require */
// import component from './component';
const tagMap = require('./vueComponentTagMap');
const customTemplate = 'template-mars';
const nativeEvent = {
'@touchstart': '@touchstart',
'@touchmove': '@touchmove',
'@touchend': '@touchend',
'@touchcancel': '@touchcancel',
'@tap': '@click',
'v-on:touchstart': 'v-on:touchstart',
'v-on:touchmove': 'v-on:touchmove',
'v-on:touchend': 'v-on:touchend',
'v-on:touchcancel': 'v-on:touchcancel',
'v-on:tap': 'v-on:click'
};
const directiveMap = {
':animation': 'v-animation',
'v-bind:animation': 'v-animation'
};
// page-home-index -> PageHomeIndex
function toCamel(name) {
let camelName = name.replace(/^\-/, '').replace(/\-(\w)(\w+)/g, function (a, b, c) {
return b.toUpperCase() + c.toLowerCase();
});
return camelName.substring(0, 1).toUpperCase() + camelName.substring(1);
}
// 判断租先节点是否含有 template-mars target=swan|wx,如果含有,则不收集该ast tag
function checkScopedTemplateIsH5(ast) {
let parent = ast.parent;
if (parent) {
if (parent.tag === 'template-mars' && parent.attrsMap && parent.attrsMap.target !== 'h5') {
return false;
}
else {
return checkScopedTemplateIsH5(parent);
}
}
return true;
}
module.exports = function (ast, options) {
const {
basicCompMap,
componentsInUsed
} = options;
let tag = ast.tag;
const isH5Component = checkScopedTemplateIsH5(ast);
if (componentsInUsed[tag] && isH5Component) {
componentsInUsed[tag].using = true;
}
if (!tag) {
return ast;
}
if (
tag === customTemplate
&& ast.attrsMap.target === (process.env.MARS_ENV_TARGET || 'h5')
) {
tag = 'template';
delete ast.attrsMap.target;
}
Object.keys(ast.attrsMap).forEach(key => {
let keyArr = key.split('.');
if (nativeEvent[keyArr[0]]) {
keyArr[0] = `${nativeEvent[keyArr[0]]}.native`;
const newKey = keyArr.join('.');
ast.attrsMap[newKey] = ast.attrsMap[`${key}`];
delete ast.attrsMap[`${key}`];
}
if (directiveMap[key]) {
ast.attrsMap[directiveMap[key]] = ast.attrsMap[key];
delete ast.attrsMap[key];
}
});
ast.tag = tagMap[tag] || tag;
if (
tagMap[tag]
&& !basicCompMap[tagMap[tag]]
&& tagMap[tag] !== 'template'
&& isH5Component
) {
basicCompMap[`${tagMap[tag]}`] = toCamel(tagMap[tag]);
}
return ast;
};