Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wzj-java
uni-app
提交
f0cfbbc4
U
uni-app
项目概览
wzj-java
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f0cfbbc4
编写于
10月 22, 2021
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
wip(mp): add extend helper
上级
a29f277d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
140 addition
and
142 deletion
+140
-142
packages/uni-mp-compiler/src/compile.ts
packages/uni-mp-compiler/src/compile.ts
+5
-141
packages/uni-mp-compiler/src/transform.ts
packages/uni-mp-compiler/src/transform.ts
+135
-1
未找到文件。
packages/uni-mp-compiler/src/compile.ts
浏览文件 @
f0cfbbc4
...
...
@@ -3,13 +3,8 @@ import { baseParse } from '@vue/compiler-core'
import
{
isString
,
extend
}
from
'
@vue/shared
'
import
{
hash
,
parseFilterNames
}
from
'
@dcloudio/uni-cli-shared
'
import
{
generate
}
from
'
./codegen
'
import
{
CompilerOptions
}
from
'
./options
'
import
{
DirectiveTransform
,
NodeTransform
,
transform
,
TransformContext
,
}
from
'
./transform
'
import
{
CodegenRootNode
,
CompilerOptions
}
from
'
./options
'
import
{
DirectiveTransform
,
NodeTransform
,
transform
}
from
'
./transform
'
import
{
transformExpression
}
from
'
./transforms/transformExpression
'
import
{
transformIdentifier
}
from
'
./transforms/transformIdentifier
'
import
{
transformIf
}
from
'
./transforms/vIf
'
...
...
@@ -19,27 +14,6 @@ import { transformOn } from './transforms/vOn'
import
{
transformElement
}
from
'
./transforms/transformElement
'
import
{
transformBind
}
from
'
./transforms/vBind
'
import
{
transformComponent
}
from
'
./transforms/transformComponent
'
import
{
ArrowFunctionExpression
,
BlockStatement
,
CallExpression
,
callExpression
,
ConditionalExpression
,
identifier
,
isCallExpression
,
isConditionalExpression
,
isIdentifier
,
isObjectExpression
,
isObjectProperty
,
isSpreadElement
,
objectExpression
,
ObjectExpression
,
ObjectProperty
,
ReturnStatement
,
SpreadElement
,
}
from
'
@babel/types
'
import
{
createObjectExpression
}
from
'
./ast
'
import
{
EXTEND
}
from
'
./runtimeHelpers
'
export
type
TransformPreset
=
[
NodeTransform
[],
...
...
@@ -68,7 +42,9 @@ export function getBaseTransformPreset({
export
function
baseCompile
(
template
:
string
,
options
:
CompilerOptions
=
{})
{
const
prefixIdentifiers
=
options
.
prefixIdentifiers
===
true
||
options
.
mode
===
'
module
'
const
ast
=
isString
(
template
)
?
baseParse
(
template
,
options
)
:
template
const
ast
=
(
isString
(
template
)
?
baseParse
(
template
,
options
)
:
template
)
as
CodegenRootNode
const
[
nodeTransforms
,
directiveTransforms
]
=
getBaseTransformPreset
({
prefixIdentifiers
,
skipTransformIdentifier
:
options
.
skipTransformIdentifier
===
true
,
...
...
@@ -100,7 +76,6 @@ export function baseCompile(template: string, options: CompilerOptions = {}) {
const
result
=
extend
(
generate
(
extend
(
ast
,
{
renderData
:
createRenderDataExpr
(
context
.
scope
.
properties
,
context
),
bindingComponents
:
context
.
bindingComponents
,
}),
options
...
...
@@ -140,114 +115,3 @@ function parseFilters(lang: string, filename: string) {
}
return
[]
}
function
createRenderDataExpr
(
properties
:
(
ObjectProperty
|
SpreadElement
)[],
context
:
TransformContext
)
{
const
objExpr
=
createObjectExpression
(
properties
)
if
(
context
.
renderDataSpread
||
!
hasSpreadElement
(
objExpr
))
{
return
objExpr
}
return
transformObjectSpreadExpr
(
objExpr
,
context
)
}
function
hasSpreadElement
(
expr
:
ObjectExpression
):
boolean
{
return
expr
.
properties
.
some
((
prop
)
=>
{
if
(
isSpreadElement
(
prop
))
{
return
true
}
else
{
const
objExpr
=
parseReturnObjExpr
(
prop
as
ObjectProperty
)
if
(
objExpr
)
{
return
hasSpreadElement
(
objExpr
)
}
}
})
}
function
parseReturnObjExpr
(
prop
:
ObjectProperty
)
{
if
(
isObjectProperty
(
prop
)
&&
isCallExpression
(
prop
.
value
)
&&
isIdentifier
(
prop
.
value
.
callee
)
&&
prop
.
value
.
callee
.
name
===
'
_vFor
'
)
{
// 目前硬编码
return
(
(
(
prop
.
value
.
arguments
[
1
]
as
ArrowFunctionExpression
)
.
body
as
BlockStatement
).
body
[
0
]
as
ReturnStatement
).
argument
as
ObjectExpression
}
}
function
transformObjectPropertyExpr
(
prop
:
ObjectProperty
,
context
:
TransformContext
)
{
// vFor
const
objExpr
=
parseReturnObjExpr
(
prop
)
if
(
objExpr
)
{
if
(
hasSpreadElement
(
objExpr
))
{
;(
(
(
(
prop
.
value
as
CallExpression
)
.
arguments
[
1
]
as
ArrowFunctionExpression
).
body
as
BlockStatement
).
body
[
0
]
as
ReturnStatement
).
argument
=
transformObjectSpreadExpr
(
objExpr
,
context
)
}
}
return
prop
}
function
transformObjectSpreadExpr
(
objExpr
:
ObjectExpression
,
context
:
TransformContext
)
{
const
properties
=
objExpr
.
properties
as
(
ObjectProperty
|
SpreadElement
)[]
const
args
:
(
ObjectExpression
|
ConditionalExpression
)[]
=
[]
let
objExprProperties
:
ObjectProperty
[]
=
[]
properties
.
forEach
((
prop
)
=>
{
if
(
isObjectProperty
(
prop
))
{
objExprProperties
.
push
(
transformObjectPropertyExpr
(
prop
,
context
))
}
else
{
if
(
objExprProperties
.
length
)
{
args
.
push
(
objectExpression
(
objExprProperties
))
}
args
.
push
(
transformConditionalExpression
(
prop
.
argument
as
ConditionalExpression
,
context
)
)
objExprProperties
=
[]
}
})
if
(
objExprProperties
.
length
)
{
args
.
push
(
objectExpression
(
objExprProperties
))
}
if
(
args
.
length
===
1
)
{
return
args
[
0
]
as
ObjectExpression
}
return
callExpression
(
identifier
(
context
.
helperString
(
EXTEND
)),
args
)
}
function
transformConditionalExpression
(
expr
:
ConditionalExpression
,
context
:
TransformContext
)
{
const
{
consequent
,
alternate
}
=
expr
if
(
isObjectExpression
(
consequent
)
&&
hasSpreadElement
(
consequent
))
{
expr
.
consequent
=
transformObjectSpreadExpr
(
consequent
,
context
)
}
if
(
isObjectExpression
(
alternate
))
{
if
(
hasSpreadElement
(
alternate
))
{
expr
.
alternate
=
transformObjectSpreadExpr
(
alternate
,
context
)
}
}
else
if
(
isConditionalExpression
(
alternate
))
{
transformConditionalExpression
(
alternate
,
context
)
}
return
expr
}
packages/uni-mp-compiler/src/transform.ts
浏览文件 @
f0cfbbc4
...
...
@@ -8,6 +8,25 @@ import {
camelize
,
}
from
'
@vue/shared
'
import
{
ConditionalExpression
,
isObjectExpression
,
isConditionalExpression
,
identifier
,
callExpression
,
ObjectExpression
,
objectExpression
,
ObjectProperty
,
SpreadElement
,
isObjectProperty
,
BlockStatement
,
ArrowFunctionExpression
,
ReturnStatement
,
isCallExpression
,
isIdentifier
,
isSpreadElement
,
CallExpression
,
}
from
'
@babel/types
'
import
{
DirectiveNode
,
ElementNode
,
...
...
@@ -28,6 +47,7 @@ import {
}
from
'
@vue/compiler-core
'
import
IdentifierGenerator
from
'
./identifier
'
import
{
CodegenRootNode
,
CodegenRootScope
,
CodegenScope
,
CodegenVForScope
,
...
...
@@ -36,6 +56,8 @@ import {
CodegenVIfScopeInit
,
TransformOptions
,
}
from
'
./options
'
import
{
EXTEND
}
from
'
./runtimeHelpers
'
import
{
createObjectExpression
}
from
'
./ast
'
export
interface
ImportItem
{
exp
:
string
|
ExpressionNode
...
...
@@ -119,9 +141,10 @@ export function isVForScope(scope: CodegenScope): scope is CodegenVForScope {
return
!!
(
scope
as
CodegenVForScope
).
source
}
export
function
transform
(
root
:
RootNode
,
options
:
TransformOptions
)
{
export
function
transform
(
root
:
Codegen
RootNode
,
options
:
TransformOptions
)
{
const
context
=
createTransformContext
(
root
,
options
)
traverseNode
(
root
,
context
)
root
.
renderData
=
createRenderDataExpr
(
context
.
scope
.
properties
,
context
)
// finalize meta information
root
.
helpers
=
[...
context
.
helpers
.
keys
()]
root
.
components
=
[...
context
.
components
]
...
...
@@ -475,3 +498,114 @@ export function createStructuralDirectiveTransform(
}
}
}
function
createRenderDataExpr
(
properties
:
(
ObjectProperty
|
SpreadElement
)[],
context
:
TransformContext
)
{
const
objExpr
=
createObjectExpression
(
properties
)
if
(
context
.
renderDataSpread
||
!
hasSpreadElement
(
objExpr
))
{
return
objExpr
}
return
transformObjectSpreadExpr
(
objExpr
,
context
)
}
function
hasSpreadElement
(
expr
:
ObjectExpression
):
boolean
{
return
expr
.
properties
.
some
((
prop
)
=>
{
if
(
isSpreadElement
(
prop
))
{
return
true
}
else
{
const
objExpr
=
parseReturnObjExpr
(
prop
as
ObjectProperty
)
if
(
objExpr
)
{
return
hasSpreadElement
(
objExpr
)
}
}
})
}
function
parseReturnObjExpr
(
prop
:
ObjectProperty
)
{
if
(
isObjectProperty
(
prop
)
&&
isCallExpression
(
prop
.
value
)
&&
isIdentifier
(
prop
.
value
.
callee
)
&&
prop
.
value
.
callee
.
name
===
'
_vFor
'
)
{
// 目前硬编码
return
(
(
(
prop
.
value
.
arguments
[
1
]
as
ArrowFunctionExpression
)
.
body
as
BlockStatement
).
body
[
0
]
as
ReturnStatement
).
argument
as
ObjectExpression
}
}
function
transformObjectPropertyExpr
(
prop
:
ObjectProperty
,
context
:
TransformContext
)
{
// vFor
const
objExpr
=
parseReturnObjExpr
(
prop
)
if
(
objExpr
)
{
if
(
hasSpreadElement
(
objExpr
))
{
;(
(
(
(
prop
.
value
as
CallExpression
)
.
arguments
[
1
]
as
ArrowFunctionExpression
).
body
as
BlockStatement
).
body
[
0
]
as
ReturnStatement
).
argument
=
transformObjectSpreadExpr
(
objExpr
,
context
)
}
}
return
prop
}
function
transformObjectSpreadExpr
(
objExpr
:
ObjectExpression
,
context
:
TransformContext
)
{
const
properties
=
objExpr
.
properties
as
(
ObjectProperty
|
SpreadElement
)[]
const
args
:
(
ObjectExpression
|
ConditionalExpression
)[]
=
[]
let
objExprProperties
:
ObjectProperty
[]
=
[]
properties
.
forEach
((
prop
)
=>
{
if
(
isObjectProperty
(
prop
))
{
objExprProperties
.
push
(
transformObjectPropertyExpr
(
prop
,
context
))
}
else
{
if
(
objExprProperties
.
length
)
{
args
.
push
(
objectExpression
(
objExprProperties
))
}
args
.
push
(
transformConditionalExpression
(
prop
.
argument
as
ConditionalExpression
,
context
)
)
objExprProperties
=
[]
}
})
if
(
objExprProperties
.
length
)
{
args
.
push
(
objectExpression
(
objExprProperties
))
}
if
(
args
.
length
===
1
)
{
return
args
[
0
]
as
ObjectExpression
}
return
callExpression
(
identifier
(
context
.
helperString
(
EXTEND
)),
args
)
}
function
transformConditionalExpression
(
expr
:
ConditionalExpression
,
context
:
TransformContext
)
{
const
{
consequent
,
alternate
}
=
expr
if
(
isObjectExpression
(
consequent
)
&&
hasSpreadElement
(
consequent
))
{
expr
.
consequent
=
transformObjectSpreadExpr
(
consequent
,
context
)
}
if
(
isObjectExpression
(
alternate
))
{
if
(
hasSpreadElement
(
alternate
))
{
expr
.
alternate
=
transformObjectSpreadExpr
(
alternate
,
context
)
}
}
else
if
(
isConditionalExpression
(
alternate
))
{
transformConditionalExpression
(
alternate
,
context
)
}
return
expr
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录