@dcloudio/uni-template-compiler 2.0.0 → 2.0.1-32920211122002

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.
@@ -72,6 +72,9 @@ function parseKey (el, isScopedSlot) {
72
72
  const keyIndex = forEl.children.indexOf(el)
73
73
  el.key = genVar(V_FOR, `{forIndex:${it},keyIndex:${keyIndex},key:${el.key}}`)
74
74
  }
75
+ if (el.tag === 'slot') {
76
+ el.attrs.push({ name: 'key', value: el.key })
77
+ }
75
78
  }
76
79
 
77
80
  function parseComponentAttrs (el, genVar) {
package/lib/constants.js CHANGED
@@ -106,6 +106,9 @@ module.exports = {
106
106
  METHOD_TO_STRING,
107
107
  METHOD_RENDER_LIST,
108
108
  METHOD_RESOLVE_FILTER,
109
+ METHOD_RENDER_SLOT,
110
+ METHOD_CREATE_EMPTY_VNODE,
111
+ METHOD_RESOLVE_SCOPED_SLOTS,
109
112
  PREFIX_GLOBAL: 'g',
110
113
  PREFIX_ATTR: 'a',
111
114
  PREFIX_METHOD: 'm',
package/lib/index.js CHANGED
@@ -94,7 +94,7 @@ module.exports = {
94
94
 
95
95
  if (options.mp.platform === 'mp-alipay') {
96
96
  options.modules.push(compilerAlipayModule)
97
- } else if (options.mp.platform === 'mp-toutiao') {
97
+ } else if (options.mp.platform === 'mp-toutiao' || options.mp.platform === 'mp-lark') {
98
98
  options.modules.push(compilerToutiaoModule)
99
99
  }
100
100
 
@@ -150,7 +150,7 @@ module.exports = {
150
150
  delete state.files
151
151
 
152
152
  // resolve scoped slots
153
- res.generic = state.generic || []
153
+ res.generic = state.generic || []
154
154
  delete state.generic
155
155
 
156
156
  // define scoped slots
@@ -290,4 +290,4 @@ at ${resourcePath}.vue:1`)
290
290
  ssrCompile,
291
291
  ssrCompileToFunctions,
292
292
  generateCodeFrame
293
- }
293
+ }
package/lib/mp.js CHANGED
@@ -1,54 +1,73 @@
1
+ const uniI18n = require('@dcloudio/uni-cli-i18n')
2
+
1
3
  const EVENTS = {
2
4
  click: 'tap'
3
5
  }
4
- const tags = [
5
- 'slot',
6
- 'block',
7
- 'component',
8
- 'template',
6
+ const tags = {
7
+ // 小程序平台通用组件
8
+ base: [
9
+ 'slot',
10
+ 'block',
11
+ 'component',
12
+ 'template',
9
13
 
10
- 'ad',
11
- 'audio',
12
- 'button',
13
- 'camera',
14
- 'canvas',
15
- 'checkbox',
16
- 'checkbox-group',
17
- 'cover-image',
18
- 'cover-view',
19
- 'form',
20
- 'functional-page-navigator',
21
- 'icon',
22
- 'image',
23
- 'input',
24
- 'label',
25
- 'live-player',
26
- 'live-pusher',
27
- 'map',
28
- 'movable-area',
29
- 'movable-view',
30
- 'navigator',
31
- 'official-account',
32
- 'open-data',
33
- 'picker',
34
- 'picker-view',
35
- 'picker-view-column',
36
- 'progress',
37
- 'radio',
38
- 'radio-group',
39
- 'rich-text',
40
- 'scroll-view',
41
- 'slider',
42
- 'swiper',
43
- 'swiper-item',
44
- 'switch',
45
- 'text',
46
- 'textarea',
47
- 'video',
48
- 'view',
49
- 'web-view',
50
- 'editor'
51
- ]
14
+ 'ad',
15
+ 'audio',
16
+ 'button',
17
+ 'camera',
18
+ 'canvas',
19
+ 'checkbox',
20
+ 'checkbox-group',
21
+ 'cover-image',
22
+ 'cover-view',
23
+ 'form',
24
+ 'functional-page-navigator',
25
+ 'icon',
26
+ 'image',
27
+ 'input',
28
+ 'label',
29
+ 'live-player',
30
+ 'live-pusher',
31
+ 'map',
32
+ 'movable-area',
33
+ 'movable-view',
34
+ 'navigator',
35
+ 'official-account',
36
+ 'open-data',
37
+ 'picker',
38
+ 'picker-view',
39
+ 'picker-view-column',
40
+ 'progress',
41
+ 'radio',
42
+ 'radio-group',
43
+ 'rich-text',
44
+ 'scroll-view',
45
+ 'slider',
46
+ 'swiper',
47
+ 'swiper-item',
48
+ 'switch',
49
+ 'text',
50
+ 'textarea',
51
+ 'video',
52
+ 'view',
53
+ 'web-view',
54
+ 'editor'
55
+ ],
56
+ // 支付宝小程序平台独有组件
57
+ 'mp-alipay': [
58
+ 'lifestyle',
59
+ 'life-follow',
60
+ 'contact-button',
61
+ 'spread',
62
+ 'error-view',
63
+ 'poster',
64
+ 'cashier',
65
+ 'ix-grid',
66
+ 'ix-native-grid',
67
+ 'ix-native-list',
68
+ 'mkt'
69
+ ]
70
+ }
52
71
 
53
72
  const baseCompiler = {
54
73
  ref: 'data-ref',
@@ -60,7 +79,7 @@ const baseCompiler = {
60
79
  * 目前 template 在前,script 在后,要做的话,就需要把 wxml 的生成机制放到 plugin 中才可以拿到真实的组件列表
61
80
  */
62
81
  isComponent (tagName) {
63
- return !tags.includes(tagName)
82
+ return !tags.base.concat(tags[this.name] || []).includes(tagName)
64
83
  },
65
84
  createFilterTag (filterTag, {
66
85
  content,
@@ -92,7 +111,7 @@ ${content}
92
111
  return `${eventType}${eventName}` // 原生组件不支持 bind:input 等写法,统一使用 bindinput
93
112
  },
94
113
  createScopedSlots (slotName, props, state) {
95
- state.errors.add('暂不支持 scoped slot [' + slotName + ']')
114
+ state.errors.add(uniI18n.__('templateCompiler.notCurrentlySupportScopedSlot', { 0: `[${slotName}]` }))
96
115
  return {
97
116
  type: 'slot',
98
117
  attr: {
@@ -105,7 +124,7 @@ ${content}
105
124
  traverseExpr,
106
125
  normalizeChildren
107
126
  }, state) {
108
- state.errors.add('暂不支持 scoped slot [' + slotName + ']')
127
+ state.errors.add(uniI18n.__('templateCompiler.notCurrentlySupportScopedSlot', { 0: `[${slotName}]` }))
109
128
  return {
110
129
  type: 'view',
111
130
  attr: {
@@ -1,4 +1,5 @@
1
1
  const t = require('@babel/types')
2
+ const uniI18n = require('@dcloudio/uni-cli-i18n')
2
3
 
3
4
  const {
4
5
  getCode
@@ -8,9 +9,9 @@ function processClassArrayExpressionElements (classArrayExpression) {
8
9
  let binaryExpression
9
10
 
10
11
  classArrayExpression.elements.forEach(expr => {
11
- if (t.isArrayExpression(expr)) {
12
- expr = processClassArrayExpressionElements(expr)
13
- }
12
+ if (t.isArrayExpression(expr)) {
13
+ expr = processClassArrayExpressionElements(expr)
14
+ }
14
15
  if (!binaryExpression) {
15
16
  binaryExpression = t.parenthesizedExpression(expr)
16
17
  } else {
@@ -31,15 +32,15 @@ function processClassArrayExpressionElements (classArrayExpression) {
31
32
 
32
33
  function processStaticClass (classArrayExpression, staticClassPath, state) {
33
34
  if (staticClassPath) {
34
- classArrayExpression.elements.unshift(
35
- t.stringLiteral(staticClassPath.node.value.value)
36
- )
35
+ const staticClassPathArr = staticClassPath.node.value.value.split(' ')
36
+ for (let len = staticClassPathArr.length, index = len - 1; index >= 0; index--) {
37
+ classArrayExpression.elements.unshift(t.stringLiteral(staticClassPathArr[index]))
38
+ }
37
39
  staticClassPath.remove()
38
40
  }
39
- if (
40
- state.options.platform.name === 'mp-toutiao' ||
41
- state.options.platform.name === 'mp-alipay'
42
- ) {
41
+
42
+ const transPlatform = ['mp-toutiao', 'mp-alipay', 'mp-lark']
43
+ if (transPlatform.includes(state.options.platform.name)) {
43
44
  // classArrayExpression => binaryExpression
44
45
  return processClassArrayExpressionElements(classArrayExpression)
45
46
  }
@@ -131,8 +132,8 @@ module.exports = function processClass (paths, path, state) {
131
132
  // )
132
133
  // )
133
134
  } else {
134
- state.errors.add(`:class 不支持 ${getCode(classValuePath.node)} 语法`)
135
+ state.errors.add(':class' + uniI18n.__('templateCompiler.noSupportSyntax', { 0: getCode(classValuePath.node) }))
135
136
  }
136
137
  }
137
138
  return []
138
- }
139
+ }
@@ -206,6 +206,22 @@ function isValuePath (path) {
206
206
  return path.key !== 'key' && path.key !== 'id' && (path.key !== 'property' || path.parent.computed) && !(path.key === 'value' && path.parentPath.parentPath.isObjectPattern()) && !(path.key === 'left' && path.parentPath.parentPath.parentPath.isObjectPattern())
207
207
  }
208
208
 
209
+ /**
210
+ * 判断 v-for 中是否包含复杂表达式:数组、对象、方法
211
+ */
212
+ const isSafeScoped = (state) => {
213
+ const scopedArray = state.scoped
214
+ for (let index = 0; index < scopedArray.length; index++) {
215
+ const scoped = scopedArray[index]
216
+ const arrayExtra = scoped.forExtra[0].elements[0].value
217
+ // 简易判断
218
+ if (typeof arrayExtra === 'string' && (arrayExtra.startsWith('[') || arrayExtra.startsWith('{') || /\(.*\)/.test(arrayExtra))) {
219
+ return false
220
+ }
221
+ }
222
+ return true
223
+ }
224
+
209
225
  function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, tagName, ret) {
210
226
  const key = keyPath.node
211
227
  let type = key.value || key.name || ''
@@ -304,18 +320,9 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
304
320
  }
305
321
  }
306
322
 
307
- // 判断是否是复杂表达式 数组 或 对象
308
- const isNotDynamicExpression = (state) => {
309
- if (!(state.scoped[0])) {
310
- return true
311
- }
312
- const value = state.scoped[0].forExtra[0].elements[0].value
313
- return !(typeof value === 'string' && (value.startsWith('[') || value.startsWith('}')))
314
- }
315
-
316
- // 如果v-for遍历的值为 数组、对象 则进入复杂表达式
317
- if (isNotDynamicExpression(state)) {
318
- anonymous && funcPath.traverse({
323
+ // 如果 v-for 遍历的值为 数组、对象、方法 则进入底部匿名表达式处理
324
+ if (anonymous && isSafeScoped(state)) {
325
+ funcPath.traverse({
319
326
  noScope: true,
320
327
  MemberExpression (path) {
321
328
  if (path.node.object.name === '$event' && path.node.property.name ===
@@ -513,8 +520,7 @@ module.exports = function processEvent (paths, path, state, isComponent, tagName
513
520
  ret.push(
514
521
  t.objectProperty(
515
522
  t.stringLiteral(ATTR_DATA_EVENT_PARAMS),
516
- // 直接使用对象格式微信小程序编译会报错
517
- t.stringLiteral(`{{({${params.join(',')}})}}`)
523
+ t.objectExpression(params.map(param => t.objectProperty(t.identifier(param), t.identifier(param), false, true)))
518
524
  )
519
525
  )
520
526
  }
@@ -62,7 +62,7 @@ module.exports = function processRef (paths, path, state) {
62
62
 
63
63
  return [ // attrs:{value:value}
64
64
  t.objectProperty(
65
- t.stringLiteral('value'),
65
+ t.stringLiteral(process.env.UNI_USING_VUE3 ? 'modelValue' : 'value'),
66
66
  t.identifier(prop)
67
67
  )
68
68
  ]
@@ -24,7 +24,8 @@ const {
24
24
 
25
25
  const {
26
26
  getInItIfStatement,
27
- getDataExpressionStatement
27
+ getDataExpressionStatement,
28
+ getRenderSlotStatement
28
29
  } = require('./statements')
29
30
 
30
31
  const visitor = require('./visitor')
@@ -90,6 +91,7 @@ module.exports = function traverse (ast, state) {
90
91
  const blockStatementBody = []
91
92
  const objectPropertyArray = []
92
93
  const initExpressionStatementArray = []
94
+ const renderSlotStatementArray = []
93
95
  // TODO 待重构,至少 filter,method 等实现方式要调整
94
96
  babelTraverse(ast, visitor, undefined, {
95
97
  scoped: [],
@@ -100,7 +102,8 @@ module.exports = function traverse (ast, state) {
100
102
  identifierArray: identifierArray,
101
103
  propertyArray: objectPropertyArray,
102
104
  declarationArray: blockStatementBody,
103
- initExpressionStatementArray: initExpressionStatementArray
105
+ initExpressionStatementArray: initExpressionStatementArray,
106
+ renderSlotStatementArray
104
107
  })
105
108
 
106
109
  if (initExpressionStatementArray.length) {
@@ -111,10 +114,14 @@ module.exports = function traverse (ast, state) {
111
114
  blockStatementBody.push(getDataExpressionStatement(objectPropertyArray))
112
115
  }
113
116
 
117
+ if (renderSlotStatementArray.length) {
118
+ blockStatementBody.push(getRenderSlotStatement(state, renderSlotStatementArray))
119
+ }
120
+
114
121
  reIdentifier(identifierArray)
115
122
 
116
123
  return t.withStatement(
117
124
  t.thisExpression(),
118
125
  t.blockStatement(blockStatementBody)
119
126
  )
120
- }
127
+ }
@@ -135,7 +135,8 @@ module.exports = function traverseRenderList (path, state) {
135
135
  forIndex,
136
136
  forExtra: getForExtra(forItem, forIndex, path, state),
137
137
  propertyArray: [],
138
- declarationArray: []
138
+ declarationArray: [],
139
+ renderSlotStatementArray: []
139
140
  }
140
141
 
141
142
  const forState = {
@@ -149,13 +150,14 @@ module.exports = function traverseRenderList (path, state) {
149
150
  propertyArray: [],
150
151
  declarationArray: [],
151
152
  computedProperty: {},
152
- initExpressionStatementArray: state.initExpressionStatementArray
153
+ initExpressionStatementArray: state.initExpressionStatementArray,
154
+ renderSlotStatementArray: state.renderSlotStatementArray
153
155
  }
154
156
 
155
157
  functionExpression.traverse(require('./visitor'), forState)
156
158
 
157
159
  const forPath = path.get('arguments.0')
158
- if (forStateScoped.propertyArray.length) {
160
+ if (forStateScoped.propertyArray.length || forStateScoped.renderSlotStatementArray.length) {
159
161
  // for => map
160
162
  forPath.replaceWith(
161
163
  getMemberExpr(
@@ -165,9 +167,11 @@ module.exports = function traverseRenderList (path, state) {
165
167
  forPath.node,
166
168
  forStateScoped.propertyArray,
167
169
  forStateScoped.declarationArray,
170
+ forStateScoped.renderSlotStatementArray,
168
171
  [], // eventPropertyArray
169
172
  forItem,
170
- forIndex
173
+ forIndex,
174
+ state
171
175
  ),
172
176
  forState
173
177
  )
@@ -0,0 +1,38 @@
1
+ const t = require('@babel/types')
2
+
3
+ module.exports = function getRenderSlot (path, state) {
4
+ const name = path.get('arguments.0')
5
+ const arg2 = path.get('arguments.2')
6
+ const arg3 = path.get('arguments.3')
7
+ let valueNode
8
+ if (arg3) {
9
+ // v-bind:object
10
+ valueNode = arg3.node
11
+ } else if (arg2 && !arg2.isNullLiteral()) {
12
+ if (arg2.isObjectExpression()) {
13
+ const propertiesPath = arg2.get('properties')
14
+ const oldProperties = []
15
+ const newProperties = []
16
+ propertiesPath.forEach(path => {
17
+ const properties = path.get('key').isStringLiteral({ value: 'SLOT_DEFAULT' }) ? oldProperties : newProperties
18
+ properties.push(state.options.scopedSlotsCompiler === 'auto' ? path.node : t.cloneNode(path.node, true))
19
+ })
20
+ if (!newProperties.length) {
21
+ return
22
+ }
23
+ valueNode = t.objectExpression(newProperties)
24
+ if (state.options.scopedSlotsCompiler !== 'auto') {
25
+ arg2.replaceWith(t.objectExpression(oldProperties))
26
+ }
27
+ } else {
28
+ valueNode = arg2.node
29
+ }
30
+ }
31
+ if (valueNode) {
32
+ const scoped = state.scoped
33
+ // TODO 判断是否包含作用域内变量
34
+ const renderSlotStatementArray = scoped && scoped.length ? scoped[scoped.length - 1].renderSlotStatementArray : state.renderSlotStatementArray
35
+ renderSlotStatementArray.push(t.expressionStatement(t.callExpression(t.identifier('$setScopedSlotsParams'), [t.stringLiteral(name.node.value), valueNode])))
36
+ }
37
+ // TODO 组件嵌套
38
+ }
@@ -0,0 +1,89 @@
1
+ const t = require('@babel/types')
2
+
3
+ const {
4
+ METHOD_BUILT_IN,
5
+ METHOD_CREATE_EMPTY_VNODE,
6
+ METHOD_CREATE_ELEMENT
7
+ } = require('../../constants')
8
+
9
+ function needSlotMode (path, ids) {
10
+ let need
11
+ path.traverse({
12
+ noScope: false,
13
+ Property (path) {
14
+ // 跳过事件
15
+ if (path.node.key.name === 'on') {
16
+ const parentPath = path.parentPath.parentPath
17
+ if (t.isCallExpression(parentPath) && parentPath.node.callee.name === METHOD_CREATE_ELEMENT) {
18
+ path.skip()
19
+ }
20
+ }
21
+ },
22
+ Identifier (path) {
23
+ const name = path.node.name
24
+ if (path.key !== 'key' && (path.key !== 'property' || path.parent.computed)) {
25
+ // 使用作用域内方法或作用域外数据
26
+ if (name in ids) {
27
+ need = path.key === 'callee' ? true : need
28
+ } else if (!path.scope.hasBinding(name) && !METHOD_BUILT_IN.includes(name)) {
29
+ need = true
30
+ }
31
+ }
32
+ }
33
+ })
34
+ return need
35
+ }
36
+
37
+ function replaceId (path, ids) {
38
+ let replaced
39
+ const fnPath = path.parentPath
40
+ path.traverse({
41
+ noScope: false,
42
+ Identifier (path) {
43
+ const name = path.node.name
44
+ if (name in ids && path.key !== 'key' && (path.key !== 'property' || path.parent.computed) && path.scope.path === fnPath) {
45
+ path.replaceWith(ids[name])
46
+ replaced = true
47
+ }
48
+ }
49
+ })
50
+ return replaced
51
+ }
52
+
53
+ module.exports = function getResolveScopedSlots (parent, state) {
54
+ const properties = parent.get('arguments.0.elements.0.properties')
55
+ const fn = properties.find(path => path.get('key').isIdentifier({ name: 'fn' }))
56
+ const params = fn.get('value.params.0')
57
+ if (!params) {
58
+ return
59
+ }
60
+ const vueId = parent.parentPath.parentPath.get('properties').find(path => path.get('key').isIdentifier({ name: 'attrs' })).get('value').get('properties').find(path => path.get('key').isStringLiteral({ value: 'vue-id' })).get('value').node
61
+ const slot = properties.find(path => path.get('key').isIdentifier({ name: 'key' })).get('value').node.value
62
+ const ids = {}
63
+ function updateIds (vueId, slot, value, key) {
64
+ const array = [vueId, t.stringLiteral(slot)]
65
+ if (key) {
66
+ array.push(t.stringLiteral(key))
67
+ }
68
+ ids[value] = t.callExpression(t.identifier('$getScopedSlotsParams'), array)
69
+ }
70
+ if (params.isObjectPattern()) {
71
+ params.get('properties').forEach(prop => {
72
+ updateIds(vueId, slot, prop.get('value').node.name, prop.get('key').node.name)
73
+ })
74
+ } else if (params.isIdentifier()) {
75
+ updateIds(vueId, slot, params.node.name)
76
+ }
77
+ const fnBody = fn.get('value.body')
78
+ if (state.options.scopedSlotsCompiler === 'augmented' || needSlotMode(fnBody, ids)) {
79
+ if (replaceId(fnBody, ids)) {
80
+ const orgin = fnBody.get('body.0.argument')
81
+ const elements = orgin.get('elements')
82
+ const node = (elements.length === 1 ? elements[0] : orgin).node
83
+ const test = t.callExpression(t.identifier('$hasScopedSlotsParams'), [vueId])
84
+ orgin.replaceWith(t.arrayExpression([t.conditionalExpression(test, node, t.callExpression(t.identifier(METHOD_CREATE_EMPTY_VNODE), []))]))
85
+ // scopedSlotsCompiler auto
86
+ parent.get('arguments.0.elements.0').node.scopedSlotsCompiler = 'augmented'
87
+ }
88
+ }
89
+ }
@@ -4,7 +4,9 @@ const {
4
4
  VAR_MP,
5
5
  VAR_ROOT,
6
6
  VAR_ORIGINAL,
7
- INTERNAL_GET_ORIG
7
+ INTERNAL_GET_ORIG,
8
+ IDENTIFIER_METHOD,
9
+ IDENTIFIER_FILTER
8
10
  } = require('../../constants')
9
11
  /**
10
12
  * e0=e=>count++
@@ -31,6 +33,53 @@ function getInItIfStatement (expressionStatementArray) {
31
33
  )
32
34
  }
33
35
 
36
+ function getRenderSlotStatement (state, renderSlotStatementArray, forItem) {
37
+ function cloneNode (node) {
38
+ if (Array.isArray(node)) {
39
+ return node.map(function (item) {
40
+ return cloneNode(item)
41
+ })
42
+ } else if (typeof node === 'object') {
43
+ if (!node) {
44
+ return node
45
+ }
46
+ if (t.isMemberExpression(node)) { // 纠正被处理过的对象
47
+ const name = node.object.name
48
+ // identifier 使用原值以被后续修改
49
+ if ((name === VAR_ROOT || name === forItem) && t.isIdentifier(node.property) && [IDENTIFIER_METHOD, IDENTIFIER_FILTER].includes(node.property.name)) {
50
+ return node.property
51
+ }
52
+ } else if (t.isIdentifier(node, { name: forItem })) { // 预处理 forItem
53
+ return t.identifier(VAR_ORIGINAL)
54
+ }
55
+ const target = Object.create(node)
56
+ Object.keys(node).forEach(function (key) {
57
+ target[key] = cloneNode(node[key])
58
+ })
59
+ return target
60
+ } else {
61
+ return node
62
+ }
63
+ }
64
+ renderSlotStatementArray.forEach(renderSlotStatement => {
65
+ const argument = renderSlotStatement.expression.arguments[1]
66
+ if (t.isObjectExpression(argument)) {
67
+ // 克隆以避免影响模板
68
+ argument.properties = cloneNode(argument.properties)
69
+ }
70
+ })
71
+ const blockStatement = t.blockStatement(renderSlotStatementArray)
72
+ if (state.options.scopedSlotsCompiler === 'auto') {
73
+ return t.ifStatement(
74
+ t.binaryExpression('===',
75
+ t.memberExpression(t.memberExpression(t.identifier('$scope'), t.identifier(state.options.platform.name === 'mp-alipay' ? 'props' : 'data')), t.identifier('scopedSlotsCompiler')), t.stringLiteral('augmented')
76
+ ),
77
+ blockStatement
78
+ )
79
+ }
80
+ return blockStatement
81
+ }
82
+
34
83
  /**
35
84
  * items.map(function(item,index){return {}})
36
85
  */
@@ -38,9 +87,11 @@ function getMapCallExpression (
38
87
  object,
39
88
  objectPropertyArray,
40
89
  declarationArray,
90
+ renderSlotStatementArray,
41
91
  eventPropertyArray,
42
92
  forItem,
43
- forIndex
93
+ forIndex,
94
+ state
44
95
  ) {
45
96
  const blockStatement = []
46
97
  // var $orgi = __get_orig(forItem)
@@ -49,24 +100,28 @@ function getMapCallExpression (
49
100
  t.identifier(forItem)
50
101
  ]))
51
102
  ]))
52
-
53
103
  if (declarationArray.length) {
54
104
  declarationArray.forEach(declaration => {
55
105
  blockStatement.push(declaration)
56
106
  })
57
- blockStatement.push(t.returnStatement(
58
- // return {$orgi:$orgi}
59
- t.objectExpression(
60
- [
61
- t.objectProperty(
62
- t.identifier(VAR_ORIGINAL),
63
- t.identifier(VAR_ORIGINAL)
64
- )
65
- ].concat(objectPropertyArray)
66
- )
67
- ))
68
107
  }
69
108
 
109
+ if (renderSlotStatementArray.length) {
110
+ blockStatement.push(getRenderSlotStatement(state, renderSlotStatementArray, forItem))
111
+ }
112
+
113
+ blockStatement.push(t.returnStatement(
114
+ // return {$orgi:$orgi}
115
+ t.objectExpression(
116
+ [
117
+ t.objectProperty(
118
+ t.identifier(VAR_ORIGINAL),
119
+ t.identifier(VAR_ORIGINAL)
120
+ )
121
+ ].concat(objectPropertyArray)
122
+ )
123
+ ))
124
+
70
125
  const params = [t.identifier(forItem)]
71
126
  if (forIndex) {
72
127
  params.push(t.identifier(forIndex))
@@ -119,5 +174,6 @@ module.exports = {
119
174
  getInItIfStatement,
120
175
  getMapCallExpression,
121
176
  getDataExpressionStatement,
122
- getEventExpressionStatement
177
+ getEventExpressionStatement,
178
+ getRenderSlotStatement
123
179
  }
@@ -1,4 +1,5 @@
1
1
  const t = require('@babel/types')
2
+ const uniI18n = require('@dcloudio/uni-cli-i18n')
2
3
 
3
4
  const {
4
5
  METHOD_CREATE_ELEMENT,
@@ -6,6 +7,8 @@ const {
6
7
  METHOD_RENDER_LIST,
7
8
  METHOD_BUILT_IN,
8
9
  METHOD_RESOLVE_FILTER,
10
+ METHOD_RENDER_SLOT,
11
+ METHOD_RESOLVE_SCOPED_SLOTS,
9
12
  IDENTIFIER_FILTER,
10
13
  IDENTIFIER_METHOD,
11
14
  IDENTIFIER_GLOBAL
@@ -26,6 +29,8 @@ const traverseData = require('./data')
26
29
  const traverseRenderList = require('./render-list')
27
30
 
28
31
  const getMemberExpr = require('./member-expr')
32
+ const getRenderSlot = require('./render-slot')
33
+ const getResolveScopedSlots = require('./resolve-scoped-slots')
29
34
 
30
35
  function addStaticClass (path, staticClass) {
31
36
  const dataPath = path.get('arguments.1')
@@ -73,7 +78,7 @@ function addVueId (path, state) {
73
78
  const scoped = scopeds[i]
74
79
  forIndexSet.add(scoped.forIndex)
75
80
  if (forIndexSet.size !== i + 1) {
76
- state.errors.add(`v-for 嵌套时,索引名称 ${scoped.forIndex} 不允许重复`)
81
+ state.errors.add(uniI18n.__('templateCompiler.forNestedIndexNameNoArrowRepeat', { 0: 'v-for', 1: scoped.forIndex }))
77
82
  break
78
83
  }
79
84
  }
@@ -217,6 +222,12 @@ module.exports = {
217
222
  this
218
223
  )
219
224
  )
225
+ } else if (this.options.scopedSlotsCompiler === 'auto' || this.options.scopedSlotsCompiler === 'augmented') {
226
+ if (methodName === METHOD_RESOLVE_SCOPED_SLOTS) {
227
+ getResolveScopedSlots(path, this)
228
+ } else if (methodName === METHOD_RENDER_SLOT) {
229
+ getRenderSlot(path, this)
230
+ }
220
231
  }
221
232
  break
222
233
  }
@@ -6,6 +6,7 @@ const {
6
6
  SELF_CLOSING_TAGS,
7
7
  INTERNAL_EVENT_LINK
8
8
  } = require('../constants')
9
+ const uniI18n = require('@dcloudio/uni-cli-i18n')
9
10
 
10
11
  function processElement (ast, state, isRoot) {
11
12
  const platformName = state.options.platform.name
@@ -48,12 +49,17 @@ function processElement (ast, state, isRoot) {
48
49
  const slots = []
49
50
  for (let i = children.length - 1; i >= 0; i--) {
50
51
  const childElement = children[i]
51
- // <block name="left"></block> => <view name="left"></view>
52
+ /**
53
+ * 仅百度、字节支持使用 block 作为命名插槽根节点
54
+ * 此处为了统一仅忽略默认插槽
55
+ * <block slot="left"></block> => <view slot="left"></view>
56
+ */
52
57
  if (typeof childElement !== 'string' && childElement.attr.slot) {
53
- if (childElement.type === 'block') {
58
+ const slot = childElement.attr.slot
59
+ if (slot && slot !== 'default' && childElement.type === 'block') {
54
60
  childElement.type = 'view'
55
61
  }
56
- slots.push(childElement.attr.slot)
62
+ slots.push(slot)
57
63
  } else {
58
64
  defaultSlot = true
59
65
  }
@@ -65,8 +71,8 @@ function processElement (ast, state, isRoot) {
65
71
  Object.keys(ast.attr.generic).forEach(scopedSlotName => {
66
72
  slots.push(scopedSlotName)
67
73
  })
68
- if (platformName === 'mp-toutiao') {
69
- // 用于字节跳动小程序模拟抽象节点
74
+ if (platformName === 'mp-toutiao' || platformName === 'mp-lark') {
75
+ // 用于字节跳动|飞书小程序模拟抽象节点
70
76
  ast.attr.generic = `{{${JSON.stringify(ast.attr.generic)}}}`.replace(/"/g, '\'')
71
77
  } else {
72
78
  delete ast.attr.generic
@@ -76,9 +82,9 @@ function processElement (ast, state, isRoot) {
76
82
  ast.attr['vue-slots'] = '{{[' + slots.reverse().map(slotName => `'${slotName}'`).join(',') + ']}}'
77
83
  }
78
84
  if (ast.attr.id && ast.attr.id.indexOf('{{') === 0) {
79
- state.tips.add(`id 作为属性保留名,不允许在自定义组件 ${ast.type} 中定义为 props`)
85
+ state.tips.add(uniI18n.__('templateCompiler.idAttribNotAllowInCustomComponentProps', { 0: ast.type }))
80
86
  }
81
- if (hasOwn(ast.attr, 'data') && platformName !== 'mp-toutiao') { // 百度中会出现异常情况
87
+ if (hasOwn(ast.attr, 'data') && platformName !== 'mp-toutiao') { // 百度中会出现异常情况
82
88
  // TODO 暂不输出
83
89
  // state.tips.add(`data 作为属性保留名,不允许在自定义组件 ${ast.type} 中定义为 props`)
84
90
  }
@@ -4,6 +4,7 @@ const t = require('@babel/types')
4
4
  const babelTraverse = require('@babel/traverse').default
5
5
 
6
6
  const generate = require('./generate')
7
+ const uniI18n = require('@dcloudio/uni-cli-i18n')
7
8
 
8
9
  const {
9
10
  genCode,
@@ -230,10 +231,10 @@ function traverseDataNode (dataNode, state, node) {
230
231
  let key
231
232
  // 自定义组件不支持 hidden 属性
232
233
  const platform = state.options.platform.name
233
- const platforms = ['mp-weixin', 'mp-qq', 'mp-toutiao']
234
+ const platforms = ['mp-weixin', 'mp-qq', 'mp-toutiao', 'mp-lark']
234
235
  if (isComponent(node.type) && platforms.includes(platform)) {
235
- // 字节跳动小程序自定义属性不会反应在DOM上,只能使用事件格式
236
- key = `${platform === 'mp-toutiao' ? 'bind:-' : ''}${ATTE_DATA_CUSTOM_HIDDEN}`
236
+ // 字节跳动|飞书小程序自定义属性不会反应在DOM上,只能使用事件格式
237
+ key = `${platform === 'mp-toutiao' || platform === 'mp-lark' ? 'bind:-' : ''}${ATTE_DATA_CUSTOM_HIDDEN}`
237
238
  } else {
238
239
  key = 'hidden'
239
240
  }
@@ -273,6 +274,10 @@ function genSlotNode (slotName, slotNode, fallbackNodes, state) {
273
274
  if (!fallbackNodes || t.isNullLiteral(fallbackNodes)) {
274
275
  return slotNode
275
276
  }
277
+ // 支付宝小程序默认插槽为 $default
278
+ if (state.options.platform.name === 'mp-alipay') {
279
+ slotName = slotName === 'default' ? '$default' : slotName
280
+ }
276
281
  const prefix = state.options.platform.directive
277
282
  return [{
278
283
  type: 'block',
@@ -293,14 +298,14 @@ function genSlotNode (slotName, slotNode, fallbackNodes, state) {
293
298
 
294
299
  function traverseRenderSlot (callExprNode, state) {
295
300
  if (!t.isStringLiteral(callExprNode.arguments[0])) {
296
- state.errors.add('v-slot 不支持动态插槽名')
301
+ state.errors.add(uniI18n.__('templateCompiler.notSupportDynamicSlotName', { 0: 'v-slot' }))
297
302
  return
298
303
  }
299
304
 
300
305
  const slotName = callExprNode.arguments[0].value
301
306
 
302
307
  let deleteSlotName = false // 标记是否组件 slot 手动指定了 name="default"
303
- if (callExprNode.arguments.length > 2) { // 作用域插槽
308
+ if (state.options.scopedSlotsCompiler !== 'augmented' && callExprNode.arguments.length > 2) { // 作用域插槽
304
309
  const props = {}
305
310
  callExprNode.arguments[2].properties.forEach(property => {
306
311
  props[property.key.value] = genCode(property.value)
@@ -333,6 +338,20 @@ function traverseRenderSlot (callExprNode, state) {
333
338
  }
334
339
 
335
340
  function traverseResolveScopedSlots (callExprNode, state) {
341
+ function single (children, slotName, ignore) {
342
+ if (Array.isArray(children) && children.length === 1) {
343
+ const child = children[0]
344
+ if (!child.type) {
345
+ return
346
+ }
347
+ if (ignore.includes(child.type)) {
348
+ return single(child.children, slotName, ignore)
349
+ }
350
+ child.attr = child.attr || {}
351
+ child.attr.slot = slotName
352
+ return true
353
+ }
354
+ }
336
355
  return callExprNode.arguments[0].elements.map(slotNode => {
337
356
  let keyProperty = false
338
357
  let fnProperty = false
@@ -351,11 +370,11 @@ function traverseResolveScopedSlots (callExprNode, state) {
351
370
  })
352
371
  const slotName = keyProperty.value.value
353
372
  const returnExprNodes = fnProperty.value.body.body[0].argument
354
- if (!proxyProperty) {
373
+ const parentNode = callExprNode.$node
374
+ if (slotNode.scopedSlotsCompiler !== 'augmented' && !proxyProperty) {
355
375
  const resourcePath = state.options.resourcePath
356
376
  const ownerName = path.basename(resourcePath, path.extname(resourcePath))
357
377
 
358
- const parentNode = callExprNode.$node
359
378
  const parentName = parentNode.type
360
379
 
361
380
  const paramExprNode = fnProperty.value.params[0]
@@ -379,14 +398,21 @@ function traverseResolveScopedSlots (callExprNode, state) {
379
398
  state
380
399
  )
381
400
  }
382
- const node = {
383
- type: 'view',
401
+ if (state.options.scopedSlotsCompiler === 'auto' && slotNode.scopedSlotsCompiler === 'augmented') {
402
+ parentNode.attr['scoped-slots-compiler'] = 'augmented'
403
+ }
404
+ const children = normalizeChildren(traverseExpr(returnExprNodes, state))
405
+ // 除百度、字节外其他小程序仅默认插槽可以支持多个节点
406
+ if (single(children, slotName, ['template', 'block'])) {
407
+ return children[0]
408
+ }
409
+ return {
410
+ type: 'block',
384
411
  attr: {
385
412
  slot: slotName
386
413
  },
387
- children: normalizeChildren(traverseExpr(returnExprNodes, state))
414
+ children
388
415
  }
389
- return node
390
416
  })
391
417
  }
392
418
 
@@ -402,8 +428,15 @@ function traverseRenderList (callExprNode, state) {
402
428
 
403
429
  const prefix = state.options.platform.directive
404
430
 
431
+ const isBaidu = state.options.platform.name === 'mp-baidu'
432
+ let forValue = genCode(callExprNode.arguments[0], isBaidu)
433
+
434
+ if (isBaidu && forKey) {
435
+ forValue += ` trackBy ${getForKey(forKey, forIndex, state)}`
436
+ }
437
+
405
438
  const attr = {
406
- [prefix + 'for']: genCode(callExprNode.arguments[0]),
439
+ [prefix + 'for']: forValue,
407
440
  [prefix + 'for-item']: forItem
408
441
  }
409
442
 
@@ -411,7 +444,7 @@ function traverseRenderList (callExprNode, state) {
411
444
  attr[prefix + 'for-index'] = forIndex
412
445
  }
413
446
 
414
- if (forKey) {
447
+ if (forKey && !isBaidu) {
415
448
  const key = getForKey(forKey, forIndex, state)
416
449
  if (key) {
417
450
  attr[prefix + 'key'] = key
@@ -488,4 +521,4 @@ function traverseCreateTextVNode (callExprNode, state) {
488
521
 
489
522
  function traverseCreateEmptyVNode (callExprNode, state) {
490
523
  return ''
491
- }
524
+ }
package/lib/util.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const t = require('@babel/types')
2
2
  const babelTraverse = require('@babel/traverse').default
3
3
  const babelGenerate = require('@babel/generator').default
4
+ const uniI18n = require('@dcloudio/uni-cli-i18n')
4
5
 
5
6
  const {
6
7
  METHOD_RENDER_LIST
@@ -106,6 +107,7 @@ function getForKey (forKey, forIndex, state) {
106
107
  if (forKey) {
107
108
  if (t.isIdentifier(forKey)) {
108
109
  if (forIndex !== forKey.name) { // 非 forIndex
110
+ if (state.options.platform.name === 'mp-baidu') return getCode(forKey)
109
111
  return '*this'
110
112
  } else {
111
113
  // TODO
@@ -113,9 +115,10 @@ function getForKey (forKey, forIndex, state) {
113
115
  return forKey.name
114
116
  }
115
117
  } else if (t.isMemberExpression(forKey)) {
118
+ if (state.options.platform.name === 'mp-baidu') return getCode(forKey)
116
119
  return forKey.property.name || forKey.property.value
117
120
  } else {
118
- state.tips.add(`非 h5 平台 :key 不支持表达式 ${getCode(forKey)},详情参考:https://uniapp.dcloud.io/use?id=key`)
121
+ state.tips.add(uniI18n.__('templateCompiler.noH5KeyNoSupportExpression', { 0: getCode(forKey), 1: 'https://uniapp.dcloud.io/use?id=key' }))
119
122
  }
120
123
  }
121
124
  return ''
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dcloudio/uni-template-compiler",
3
- "version": "2.0.0",
3
+ "version": "2.0.1-32920211122002",
4
4
  "description": "uni-template-compiler",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
@@ -22,5 +22,5 @@
22
22
  "@babel/types": "^7.3.3",
23
23
  "vue-template-compiler": "^2.6.10"
24
24
  },
25
- "gitHead": "b1bdfb4de3f518c1c0fbab242403ea05feced4e3"
25
+ "gitHead": "dc33ced4b2fff3063385d9eda8dd1f3539133a67"
26
26
  }