@dolphinweex/weex-harmony 0.1.0
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/README.md +13 -0
- package/index.js +2 -0
- package/package.json +37 -0
- package/src/components/baseSameLayer.vue +101 -0
- package/src/components/drag-vertical-list-view.vue +86 -0
- package/src/components/hl-button-base.vue +66 -0
- package/src/components/hl-button.vue +115 -0
- package/src/components/hl-image-base.vue +53 -0
- package/src/components/hl-image.vue +151 -0
- package/src/components/hl-lottie-base.vue +151 -0
- package/src/components/hl-lottie.vue +214 -0
- package/src/components/hl-richtext-base.vue +40 -0
- package/src/components/hl-richtext.vue +118 -0
- package/src/components/hl-slider-base.vue +75 -0
- package/src/components/hl-slider.vue +126 -0
- package/src/components/hl-textarea-base.vue +40 -0
- package/src/components/hl-textarea.vue +118 -0
- package/src/components/hl-video-base.vue +65 -0
- package/src/components/hl-video.vue +148 -0
- package/src/components/hl-web-base.vue +85 -0
- package/src/components/mdiea-arc-slider.vue +75 -0
- package/src/components/midea-apng-view-base.vue +111 -0
- package/src/components/midea-barchart-view.vue +56 -0
- package/src/components/midea-blur-view.vue +57 -0
- package/src/components/midea-calendar-pave.vue +64 -0
- package/src/components/midea-camera-view.vue +91 -0
- package/src/components/midea-circle-progress.vue +56 -0
- package/src/components/midea-combinechart-view.vue +56 -0
- package/src/components/midea-common-weex-view.vue +55 -0
- package/src/components/midea-drag-list-view.vue +89 -0
- package/src/components/midea-drag-slider-base.vue +66 -0
- package/src/components/midea-dragging-linechart-view.vue +56 -0
- package/src/components/midea-gesture-password-base.vue +63 -0
- package/src/components/midea-ijkplayer-view.vue +93 -0
- package/src/components/midea-linechart-view.vue +56 -0
- package/src/components/midea-lottie-view.vue +69 -0
- package/src/components/midea-map-view.vue +74 -0
- package/src/components/midea-pdf-view-base.vue +75 -0
- package/src/components/midea-picker.vue +58 -0
- package/src/components/midea-piechart-view.vue +56 -0
- package/src/components/midea-progresscycle-view.vue +56 -0
- package/src/components/midea-seek-bar-base.vue +136 -0
- package/src/components/midea-time-pave.vue +58 -0
- package/src/components/midea-video.vue +91 -0
- package/src/components/midea-vslider-bar-base.vue +89 -0
- package/src/components/pick-pallet.vue +66 -0
- package/src/index.js +180 -0
- package/src/transform-loader.js +249 -0
@@ -0,0 +1,249 @@
|
|
1
|
+
const compiler = require('vue-template-compiler')
|
2
|
+
const parser = require('@babel/parser')
|
3
|
+
const traverse = require('@babel/traverse').default
|
4
|
+
const generator = require('@babel/generator').default
|
5
|
+
const t = require('@babel/types')
|
6
|
+
const { parse } = require('himalaya')
|
7
|
+
const path = require('path')
|
8
|
+
|
9
|
+
let currentModulePath = ''
|
10
|
+
|
11
|
+
// 组件引入基路径,baseUrl的值为引入组件路径的公共部分
|
12
|
+
const baseUrl = '../src/components/'
|
13
|
+
|
14
|
+
/**
|
15
|
+
* componentName:该属性的值是导入和注册组件时的组件名称,使用时请遵循Vue组件的命名和使用规范
|
16
|
+
*
|
17
|
+
* componentAddress: 组件所在地址
|
18
|
+
*
|
19
|
+
* isInPlugin: 是否从三方库引入
|
20
|
+
*/
|
21
|
+
const componentMap = require("./index")
|
22
|
+
|
23
|
+
const componentSet = new Set()
|
24
|
+
|
25
|
+
/**
|
26
|
+
* 判断是否为windows环境
|
27
|
+
* @returns
|
28
|
+
*/
|
29
|
+
function isWin32(){
|
30
|
+
if(process.platform === 'win32') {
|
31
|
+
return true
|
32
|
+
} else {
|
33
|
+
return false
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
/**
|
38
|
+
* 处理解析标签名,将<hl-video>这种转为HlVideo
|
39
|
+
* @param {*} tagName
|
40
|
+
*/
|
41
|
+
function transformTagName(tagName) {
|
42
|
+
if(tagName.includes('-')) {
|
43
|
+
const nameSplit = tagName.split('-')
|
44
|
+
return nameSplit.join('')
|
45
|
+
}
|
46
|
+
return tagName
|
47
|
+
}
|
48
|
+
|
49
|
+
function hasTargetElement(tagName) {
|
50
|
+
let hasElement = false
|
51
|
+
let componentName = ''
|
52
|
+
componentMap.forEach((item) => {
|
53
|
+
if(item.componentName.toLowerCase() === transformTagName(tagName)) {
|
54
|
+
hasElement = true
|
55
|
+
componentName = item.componentName
|
56
|
+
}
|
57
|
+
})
|
58
|
+
|
59
|
+
return {
|
60
|
+
hasElement,
|
61
|
+
componentName
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
/**
|
66
|
+
* 遍历html节点,将需要引入的组件添加到componentSet中
|
67
|
+
* @param {*} node html节点
|
68
|
+
*/
|
69
|
+
function traverseNode(node) {
|
70
|
+
if (node.type === 'element') {
|
71
|
+
const {hasElement, componentName} = hasTargetElement(node.tagName)
|
72
|
+
if (hasElement) {
|
73
|
+
componentSet.add(componentName)
|
74
|
+
}
|
75
|
+
if (node.children) {
|
76
|
+
node.children.forEach((child) => {
|
77
|
+
traverseNode(child)
|
78
|
+
})
|
79
|
+
}
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
/**
|
84
|
+
* 创建import组件导入节点
|
85
|
+
* @param {*} componentName 组件名称
|
86
|
+
* @returns 导入的ast节点
|
87
|
+
*/
|
88
|
+
function buildImportDeclaration(componentName) {
|
89
|
+
let isInPlugin = getIsInPlugin(componentName)
|
90
|
+
return t.importDeclaration(
|
91
|
+
isInPlugin ? [t.importSpecifier(t.identifier(componentName), t.identifier(componentName), false, true)] : [t.importDefaultSpecifier(t.identifier(componentName))],
|
92
|
+
t.stringLiteral(getComponentLoc(componentName))
|
93
|
+
)
|
94
|
+
}
|
95
|
+
|
96
|
+
/**
|
97
|
+
* 判断当前组件的引入是否是三方库引入
|
98
|
+
* @param {*} componentName
|
99
|
+
*/
|
100
|
+
function getIsInPlugin(name) {
|
101
|
+
let isInPlugin = false
|
102
|
+
componentMap.forEach((item) => {
|
103
|
+
if(item.componentName === name) {
|
104
|
+
isInPlugin = item.isInPlugin
|
105
|
+
}
|
106
|
+
})
|
107
|
+
|
108
|
+
return isInPlugin
|
109
|
+
}
|
110
|
+
|
111
|
+
/**
|
112
|
+
* 通过组件名称获取组件所在位置
|
113
|
+
* @param {*} name 组件名称
|
114
|
+
*/
|
115
|
+
function getComponentLoc(name) {
|
116
|
+
let address = ''
|
117
|
+
let isInPlugin = false
|
118
|
+
componentMap.forEach((item) => {
|
119
|
+
if(item.componentName === name) {
|
120
|
+
address = baseUrl + item.componentAddress
|
121
|
+
isInPlugin = item.isInPlugin
|
122
|
+
}
|
123
|
+
})
|
124
|
+
|
125
|
+
if (isInPlugin) {
|
126
|
+
return address
|
127
|
+
} else {
|
128
|
+
return getAbsPath(address)
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
/**
|
133
|
+
* 根据引入的路径获取组件导入的路径
|
134
|
+
* @param {*} address
|
135
|
+
* @returns
|
136
|
+
*/
|
137
|
+
function getAbsPath(address){
|
138
|
+
const componentAbsolute = path.resolve(__dirname, address)
|
139
|
+
if(isWin32()) {
|
140
|
+
return path.relative(currentModulePath, componentAbsolute).replace('..\\','').replace(/\\/g,'/')
|
141
|
+
} else {
|
142
|
+
return path.relative(currentModulePath, componentAbsolute).replace('../','')
|
143
|
+
}
|
144
|
+
}
|
145
|
+
|
146
|
+
/**
|
147
|
+
* 生产SFC代码
|
148
|
+
* @param {*} moduleResoveRet
|
149
|
+
*/
|
150
|
+
function generateCode(moduleResoveRet) {
|
151
|
+
let styleTemplate = ''
|
152
|
+
let styleAttrs = ''
|
153
|
+
let scriptAttrs = ''
|
154
|
+
|
155
|
+
moduleResoveRet.styles.forEach((item) => {
|
156
|
+
styleTemplate += item.content
|
157
|
+
})
|
158
|
+
|
159
|
+
Object.keys(moduleResoveRet.script.attrs).forEach((item) => {
|
160
|
+
if (typeof moduleResoveRet.script.attrs[item] === 'boolean') {
|
161
|
+
scriptAttrs += ` ${item}`
|
162
|
+
} else {
|
163
|
+
scriptAttrs += ` ${item}="${moduleResoveRet.script.attrs[item]}"`
|
164
|
+
}
|
165
|
+
})
|
166
|
+
|
167
|
+
if(moduleResoveRet.styles.length){
|
168
|
+
Object.keys(moduleResoveRet.styles[0].attrs).forEach((item) => {
|
169
|
+
if (typeof moduleResoveRet.styles[0].attrs[item] === 'boolean') {
|
170
|
+
styleAttrs += ` ${item}`
|
171
|
+
} else {
|
172
|
+
styleAttrs += ` ${item}="${moduleResoveRet.styles[0].attrs[item]}"`
|
173
|
+
}
|
174
|
+
})
|
175
|
+
}
|
176
|
+
|
177
|
+
const template = `
|
178
|
+
<template>${moduleResoveRet.template.content}</template>
|
179
|
+
<script${scriptAttrs}>${moduleResoveRet.script.content}</script>
|
180
|
+
<style${styleAttrs}>${styleTemplate}</style>
|
181
|
+
`
|
182
|
+
return template
|
183
|
+
}
|
184
|
+
|
185
|
+
/**
|
186
|
+
* 添加components对象
|
187
|
+
* @param {*} data
|
188
|
+
*/
|
189
|
+
function addComponentsProperty(data) {
|
190
|
+
if (componentSet.size !== 0) {
|
191
|
+
componentSet.forEach((value) => {
|
192
|
+
if (!data.value.properties.some(item => item.value.name == value)) {
|
193
|
+
const newProperty = t.objectProperty(t.identifier(value), t.identifier(value),false, true)
|
194
|
+
data.value.properties.push(newProperty)
|
195
|
+
}
|
196
|
+
})
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
module.exports = function (source) {
|
201
|
+
if (process.env.ISHARMONY === 'true') {
|
202
|
+
currentModulePath = this.resourcePath
|
203
|
+
const moduleResoveRet = compiler.parseComponent(source)
|
204
|
+
const { template, script, styles } = moduleResoveRet
|
205
|
+
const tempAst = parse(template.content)
|
206
|
+
tempAst.forEach((node) => {
|
207
|
+
traverseNode(node)
|
208
|
+
})
|
209
|
+
|
210
|
+
// 单模块中引入了自定义组件
|
211
|
+
if (componentSet.size !== 0) {
|
212
|
+
const scriptAst = parser.parse(script.content, {
|
213
|
+
sourceType: 'module',
|
214
|
+
// 在此添加需要解析的插件
|
215
|
+
})
|
216
|
+
|
217
|
+
traverse(scriptAst, {
|
218
|
+
Program(path) {
|
219
|
+
componentSet.forEach((componentName) => {
|
220
|
+
path.unshiftContainer('body', buildImportDeclaration(componentName))
|
221
|
+
})
|
222
|
+
},
|
223
|
+
ExportDefaultDeclaration(path) {
|
224
|
+
if (componentSet.size !== 0) {
|
225
|
+
if (t.isObjectExpression(path.node.declaration)) {
|
226
|
+
const properties = path.node.declaration.properties
|
227
|
+
const result = properties.find(item => item.key.name == 'components')
|
228
|
+
if (result) {
|
229
|
+
addComponentsProperty(result)
|
230
|
+
} else {
|
231
|
+
const componentsAst = t.objectProperty(t.identifier('components'), t.objectExpression([]))
|
232
|
+
properties.push(componentsAst)
|
233
|
+
addComponentsProperty(componentsAst)
|
234
|
+
}
|
235
|
+
}
|
236
|
+
}
|
237
|
+
}
|
238
|
+
})
|
239
|
+
|
240
|
+
// 清除componentSet中的缓存,准备进入下次循环
|
241
|
+
componentSet.clear()
|
242
|
+
|
243
|
+
const { code } = generator(scriptAst)
|
244
|
+
moduleResoveRet.script.content = code
|
245
|
+
source = generateCode(moduleResoveRet)
|
246
|
+
}
|
247
|
+
}
|
248
|
+
return source
|
249
|
+
}
|