Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
cf5f6746
U
uni-app
项目概览
DCloud
/
uni-app
6 个月 前同步成功
通知
751
Star
38709
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
8
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
8
Issue
8
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
cf5f6746
编写于
12月 06, 2023
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(uvue): 支持 v-once
上级
04806327
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
203 addition
and
9 deletion
+203
-9
packages/uni-app-uts/__tests__/android/transforms/__snapshots__/vOnce.spec.ts.snap
...sts__/android/transforms/__snapshots__/vOnce.spec.ts.snap
+43
-0
packages/uni-app-uts/__tests__/android/transforms/vOnce.spec.ts
...es/uni-app-uts/__tests__/android/transforms/vOnce.spec.ts
+142
-0
packages/uni-app-uts/src/plugins/android/uvue/compiler/codegen.ts
.../uni-app-uts/src/plugins/android/uvue/compiler/codegen.ts
+6
-7
packages/uni-app-uts/src/plugins/android/uvue/compiler/index.ts
...es/uni-app-uts/src/plugins/android/uvue/compiler/index.ts
+2
-0
packages/uni-app-uts/src/plugins/android/uvue/compiler/transform.ts
...ni-app-uts/src/plugins/android/uvue/compiler/transform.ts
+10
-2
未找到文件。
packages/uni-app-uts/__tests__/android/transforms/__snapshots__/vOnce.spec.ts.snap
0 → 100644
浏览文件 @
cf5f6746
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`compiler: v-once transform as root node 1`] = `
"_cache[0] ?? run((): VNode | null => {
setBlockTracking(-1)
_cache[0] = createElementVNode("view", utsMapOf({ id: foo }), null, 8 /* PROPS */, ["id"])
setBlockTracking(1)
return _cache[0] as VNode | null
})"
`;
exports[`compiler: v-once transform on component 1`] = `
"createElementVNode("view", null, [
_cache[0] ?? run((): VNode | null => {
setBlockTracking(-1)
_cache[0] = createVNode(_component_Comp, utsMapOf({ id: foo }), null, 8 /* PROPS */, ["id"])
setBlockTracking(1)
return _cache[0] as VNode | null
})
])"
`;
exports[`compiler: v-once transform on nested plain element 1`] = `
"createElementVNode("view", null, [
_cache[0] ?? run((): VNode | null => {
setBlockTracking(-1)
_cache[0] = createElementVNode("view", utsMapOf({ id: foo }), null, 8 /* PROPS */, ["id"])
setBlockTracking(1)
return _cache[0] as VNode | null
})
])"
`;
exports[`compiler: v-once transform on slot outlet 1`] = `
"createElementVNode("view", null, [
_cache[0] ?? run((): VNode | null => {
setBlockTracking(-1)
_cache[0] = renderSlot($slots, "default")
setBlockTracking(1)
return _cache[0] as VNode | null
})
])"
`;
packages/uni-app-uts/__tests__/android/transforms/vOnce.spec.ts
0 → 100644
浏览文件 @
cf5f6746
import
{
NodeTypes
,
SET_BLOCK_TRACKING
,
baseParse
}
from
'
@vue/compiler-core
'
import
{
getBaseTransformPreset
}
from
'
../../../src/plugins/android/uvue/compiler/index
'
import
{
transform
}
from
'
../../../src/plugins/android/uvue/compiler/transform
'
import
{
CompilerOptions
}
from
'
../../../src/plugins/android/uvue/compiler/options
'
import
{
generate
}
from
'
../../../src/plugins/android/uvue/compiler/codegen
'
import
{
RENDER_SLOT
}
from
'
../../../src/plugins/android/uvue/compiler/runtimeHelpers
'
function
transformWithOnce
(
template
:
string
,
options
:
CompilerOptions
=
{})
{
const
ast
=
baseParse
(
template
)
const
[
nodeTransforms
,
directiveTransforms
]
=
getBaseTransformPreset
()
transform
(
ast
,
{
nodeTransforms
,
directiveTransforms
,
...
options
,
})
return
ast
}
describe
(
'
compiler: v-once transform
'
,
()
=>
{
test
(
'
as root node
'
,
()
=>
{
const
root
=
transformWithOnce
(
`<view :id="foo" v-once />`
)
expect
(
root
.
cached
).
toBe
(
1
)
expect
(
root
.
helpers
).
toContain
(
SET_BLOCK_TRACKING
)
expect
(
root
.
codegenNode
).
toMatchObject
({
type
:
NodeTypes
.
JS_CACHE_EXPRESSION
,
index
:
0
,
value
:
{
type
:
NodeTypes
.
VNODE_CALL
,
tag
:
`"view"`
,
},
})
expect
(
generate
(
root
).
code
).
toMatchSnapshot
()
})
test
(
'
on nested plain element
'
,
()
=>
{
const
root
=
transformWithOnce
(
`<view><view :id="foo" v-once /></view>`
)
expect
(
root
.
cached
).
toBe
(
1
)
expect
(
root
.
helpers
).
toContain
(
SET_BLOCK_TRACKING
)
expect
((
root
.
children
[
0
]
as
any
).
children
[
0
].
codegenNode
).
toMatchObject
({
type
:
NodeTypes
.
JS_CACHE_EXPRESSION
,
index
:
0
,
value
:
{
type
:
NodeTypes
.
VNODE_CALL
,
tag
:
`"view"`
,
},
})
expect
(
generate
(
root
).
code
).
toMatchSnapshot
()
})
test
(
'
on component
'
,
()
=>
{
const
root
=
transformWithOnce
(
`<view><Comp :id="foo" v-once /></view>`
)
expect
(
root
.
cached
).
toBe
(
1
)
expect
(
root
.
helpers
).
toContain
(
SET_BLOCK_TRACKING
)
expect
((
root
.
children
[
0
]
as
any
).
children
[
0
].
codegenNode
).
toMatchObject
({
type
:
NodeTypes
.
JS_CACHE_EXPRESSION
,
index
:
0
,
value
:
{
type
:
NodeTypes
.
VNODE_CALL
,
tag
:
`_component_Comp`
,
},
})
expect
(
generate
(
root
).
code
).
toMatchSnapshot
()
})
test
(
'
on slot outlet
'
,
()
=>
{
const
root
=
transformWithOnce
(
`<view><slot v-once /></view>`
)
expect
(
root
.
cached
).
toBe
(
1
)
expect
(
root
.
helpers
).
toContain
(
SET_BLOCK_TRACKING
)
expect
((
root
.
children
[
0
]
as
any
).
children
[
0
].
codegenNode
).
toMatchObject
({
type
:
NodeTypes
.
JS_CACHE_EXPRESSION
,
index
:
0
,
value
:
{
type
:
NodeTypes
.
JS_CALL_EXPRESSION
,
callee
:
RENDER_SLOT
,
},
})
expect
(
generate
(
root
).
code
).
toMatchSnapshot
()
})
// v-once inside v-once should not be cached
test
(
'
inside v-once
'
,
()
=>
{
const
root
=
transformWithOnce
(
`<view v-once><view v-once/></view>`
)
expect
(
root
.
cached
).
not
.
toBe
(
2
)
expect
(
root
.
cached
).
toBe
(
1
)
})
// cached nodes should be ignored by hoistStatic transform
// test('with hoistStatic: true', () => {
// const root = transformWithOnce(`<view><view v-once /></view>`, {
// hoistStatic: true,
// })
// expect(root.cached).toBe(1)
// expect(root.helpers).toContain(SET_BLOCK_TRACKING)
// expect(root.hoists.length).toBe(0)
// expect((root.children[0] as any).children[0].codegenNode).toMatchObject({
// type: NodeTypes.JS_CACHE_EXPRESSION,
// index: 0,
// value: {
// type: NodeTypes.VNODE_CALL,
// tag: `"view"`,
// },
// })
// expect(generate(root).code).toMatchSnapshot()
// })
test
(
'
with v-if/else
'
,
()
=>
{
const
root
=
transformWithOnce
(
`<view v-if="BOOLEAN" v-once /><p v-else/>`
)
expect
(
root
.
cached
).
toBe
(
1
)
expect
(
root
.
helpers
).
toContain
(
SET_BLOCK_TRACKING
)
expect
(
root
.
children
[
0
]).
toMatchObject
({
type
:
NodeTypes
.
IF
,
// should cache the entire v-if/else-if/else expression, not just a single branch
codegenNode
:
{
type
:
NodeTypes
.
JS_CACHE_EXPRESSION
,
value
:
{
type
:
NodeTypes
.
JS_CONDITIONAL_EXPRESSION
,
consequent
:
{
type
:
NodeTypes
.
VNODE_CALL
,
tag
:
`"view"`
,
},
alternate
:
{
type
:
NodeTypes
.
VNODE_CALL
,
tag
:
`"p"`
,
},
},
},
})
})
test
(
'
with v-for
'
,
()
=>
{
const
root
=
transformWithOnce
(
`<view v-for="i in list" v-once />`
)
expect
(
root
.
cached
).
toBe
(
1
)
expect
(
root
.
helpers
).
toContain
(
SET_BLOCK_TRACKING
)
expect
(
root
.
children
[
0
]).
toMatchObject
({
type
:
NodeTypes
.
FOR
,
// should cache the entire v-for expression, not just a single branch
codegenNode
:
{
type
:
NodeTypes
.
JS_CACHE_EXPRESSION
,
},
})
})
})
packages/uni-app-uts/src/plugins/android/uvue/compiler/codegen.ts
浏览文件 @
cf5f6746
...
...
@@ -198,7 +198,7 @@ const UTS_COMPONENT_ELEMENT_IMPORTS = `/*UTS-COMPONENTS-IMPORTS*/`
export
function
generate
(
ast
:
RootNode
,
options
:
CodegenOptions
options
:
CodegenOptions
=
{}
):
CodegenResult
{
const
context
=
createCodegenContext
(
ast
,
options
)
const
{
mode
,
deindent
,
indent
,
push
,
newline
}
=
context
...
...
@@ -822,21 +822,20 @@ function genConditionalExpression(
function
genCacheExpression
(
node
:
CacheExpression
,
context
:
CodegenContext
)
{
const
{
push
,
helper
,
indent
,
deindent
,
newline
}
=
context
push
(
`_cache[
${
node
.
index
}
]
|| (
`
)
push
(
`_cache[
${
node
.
index
}
]
?? run((): VNode | null => {
`
)
if
(
node
.
isVNode
)
{
indent
()
push
(
`
${
helper
(
SET_BLOCK_TRACKING
)}
(-1)
,
`
)
push
(
`
${
helper
(
SET_BLOCK_TRACKING
)}
(-1)`
)
newline
()
}
push
(
`_cache[
${
node
.
index
}
] = `
)
genNode
(
node
.
value
,
context
)
if
(
node
.
isVNode
)
{
push
(
`,`
)
newline
()
push
(
`
${
helper
(
SET_BLOCK_TRACKING
)}
(1)
,
`
)
push
(
`
${
helper
(
SET_BLOCK_TRACKING
)}
(1)`
)
newline
()
push
(
`
_cache[
${
node
.
index
}
]
`
)
push
(
`
return _cache[
${
node
.
index
}
] as VNode | null
`
)
deindent
()
}
push
(
`)`
)
push
(
`
}
)`
)
}
packages/uni-app-uts/src/plugins/android/uvue/compiler/index.ts
浏览文件 @
cf5f6746
...
...
@@ -32,6 +32,7 @@ import { transformElements } from './transforms/transformElements'
import
{
transformStyle
}
from
'
./transforms/transformStyle
'
import
{
transformVHtml
}
from
'
./transforms/vHtml
'
import
{
transformMemo
}
from
'
./transforms/vMemo
'
import
{
transformOnce
}
from
'
./transforms/vOnce
'
export
type
TransformPreset
=
[
NodeTransform
[],
...
...
@@ -43,6 +44,7 @@ export function getBaseTransformPreset(
):
TransformPreset
{
return
[
[
transformOnce
,
transformIf
,
transformMemo
,
transformFor
,
...
...
packages/uni-app-uts/src/plugins/android/uvue/compiler/transform.ts
浏览文件 @
cf5f6746
...
...
@@ -270,10 +270,18 @@ export function transform(root: RootNode, options: TransformOptions) {
const
context
=
createTransformContext
(
root
,
options
)
traverseNode
(
root
,
context
)
createRootCodegen
(
root
,
context
)
// finalize meta information
root
.
helpers
=
new
Set
([...
context
.
helpers
.
keys
()])
root
.
components
=
[...
context
.
components
]
// @ts-ignore
root
.
elements
=
Array
.
from
(
context
.
elements
)
root
.
directives
=
[...
context
.
directives
]
root
.
imports
=
context
.
imports
// root.hoists = context.hoists
root
.
temps
=
context
.
temps
root
.
cached
=
context
.
cached
// @ts-expect-error
root
.
elements
=
Array
.
from
(
context
.
elements
)
}
export
function
isSingleElementRoot
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录