Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过静态分析减小wxml大小 #77

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

canfoo
Copy link
Contributor

@canfoo canfoo commented Jun 28, 2020

alita 在JSX碰到 {} 这种语法块,会生成一块动态化模板片段,即:

<template name="c">
        <block wx:if="{{_t.l(d)}}">{{d}}</block>
        <_g wx:elif="{{d._generic}}" diuu="{{d._generic}}" style="{{_t.s(d._genericstyle)}}"/>
        <template wx:elif="{{d.tempName}}" is="{{d.tempName}}" data="{{...d}}"/>
        <block wx:else>
            <block wx:for="{{d}}" wx:key="key">
                <block wx:if="{{_t.l(item)}}">{{item}}</block>
                <_g wx:elif="{{item._generic}}" diuu="{{item._generic}}" style="{{_t.s(item._genericstyle)}}"/>
                <template wx:else is="{{item.tempName}}" data="{{...item}}"/>
            </block>
        </block>
  </template>

大多数情况下,上面动态化模板会命中 <template wx:elif="{{d.tempName}}" is="{{d.tempName}}" data="{{...d}}"/>
所以可以针对这种情况,做下静态解析优化,如果在静态解析就知道 {} 会转换为 <template wx:elif="{{d.tempName}}" is="{{d.tempName}}" data="{{...d}}"/>,则直接生成,就不需要生成上面的动态化模板,从而减少包大小。

// 3. 如果JSXExpressionContainer为{a},且a类型为JSXElement,则直接生成对应
// 的template
else if (ele.expression && ele.expression.type === 'Identifier') {
const varnName = ele.expression.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a 有没有可能在下次渲染的时候变化呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有可能的,不过checkVarnIsJsx已经过滤掉这种情况了,因为checkVarnIsJsx判断逻辑比较严格,如果发现 a 变量所有赋值操作有一个不是 jsx 类型,就判断不可以删掉动态化模板的。只有以下情况才会删掉动态化模板: 当前文件只有一个声明 a 变量,且a变量赋值操作都是jsx类型,才能删除。

@ykforerlang
Copy link
Member

@canfoo 非常赞!, 如果命中的比较多, 性能可能就真的和原生小程序差不多了

@canfoo
Copy link
Contributor Author

canfoo commented Jul 9, 2020

@canfoo 非常赞!, 如果命中的比较多, 性能可能就真的和原生小程序差不多了

哈哈,是啊,主要还是可以减少很多代码,我一个组件优化完后,减少了8KB了。另外应该还可以对 children,数组等这些情况进行命中处理,后续有时间我再补上。

@ykforerlang ykforerlang self-assigned this Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants