Slide 1

Slide 1 text

Vapor Revolution Unleashing Vue evolution and Potential with Vapor Mode JSConf JP 2024 2024/11/23 @kazupon

Slide 2

Slide 2 text

About Me

Slide 3

Slide 3 text

PLAID, inc. Vue.js Core Team Nuxt Ecosystem Team Vue I18n & Intlify author Vue Fes Japan Organizer Vue.js Japan User Group Organizer @kazu_pon kazupon kazupon

Slide 4

Slide 4 text

We are hiring! Engineer Blog X (Twitter) © PLIAD, Inc.

Slide 5

Slide 5 text

Agenda • Vapor Mode • Potential with Vapor Mode

Slide 6

Slide 6 text

Vapor Mode

Slide 7

Slide 7 text

• New alternative compilation strategy of Vue.js • Inspired from SolidJS • Goal: Maximizing performance • Vapor mean has … • Just like steam • lighter, faster, and more flexible • Opt-in What’s Vapor Mode?

Slide 8

Slide 8 text

Again, What’s Vue.js? • Progressive framework for UI Building • SFC: Single File Components • Features: • Composition API • Reactivity • SSR, and … more! • To work on the browser, need compile SFCs

Slide 9

Slide 9 text

Key tech elements of Vue • Compiler • Reactivity System • Virtual DOM

Slide 10

Slide 10 text

Compiler • Compile SFC

Slide 11

Slide 11 text

Compiler • Compile script block and template block to JS Compile to JavaScript

Slide 12

Slide 12 text

Compiler • Compile style block to CSS Compile to CSS

Slide 13

Slide 13 text

Compiler • Optimize for performance Caching

Slide 14

Slide 14 text

• Fine-grained reactivity based on dependency tracking Reactivity System Reactivity System Dependencies full last fi rst

Slide 15

Slide 15 text

• Rendering with Diff & Patch & Reactive lifecycle Virtual DOM https://vuejs.org/guide/extras/rendering-mechanism.html#render-pipeline Tamplate compiled into Render Function Component Reactivity State Virtual DOM Tree Actual DOM returns diff / patch trigger re-render track dependencies
...
function render() { return h(‘div', { id: "hello" }, [/* more childs */] ) } const vnode = { type: 'div', props: { id: 'hello' }, children: [ /* more vnodes */ ] }
...

Slide 16

Slide 16 text

Virtual DOM Tamplate compiled into Render Function Component Reactivity State Virtual DOM Tree Actual DOM returns diff / patch track dependencies
...
function render() { return h(‘div', { id: "hello" }, [/* more childs */] ) } const vnode = { type: 'div', props: { id: 'hello' }, children: [ /* more vnodes */ ] }
...
• First rendering

Slide 17

Slide 17 text

Virtual DOM Tamplate compiled into Render Function Component Reactivity State Virtual DOM Tree Actual DOM returns diff / patch trigger re-render
...
function render() { return h(‘div', { id: "hello" }, [/* more childs */] ) } const vnode = { type: 'div', props: { id: 'hello' }, children: [ /* more vnodes */ ] }
...
• Re-rending

Slide 18

Slide 18 text

Changes with Vapor Mode

Slide 19

Slide 19 text

Changes with Vapor Mode • Rendering mechanism • Compiler • Runtime

Slide 20

Slide 20 text

Rendering mechanism • Drop Virtual DOM, use Native DOM API Tamplate compiled into Render Function Component Reactivity State Framgments (DOM Nodes) Actual DOM returns mount update track dependencies {{ count }}
0
const t0 = template("") delegateEvents("click") function render(ctx) { const n0 = t0() delegate(n0, “click", () => $event => (ctx.count++)) renderEffect( () => setText(n0, ctx.count)) return n0 } 0 Reactive Effect trigger effect register
1

Slide 21

Slide 21 text

Rendering mechanism Tamplate compiled into Render Function Component Reactivity State Framgments (DOM Nodes) Actual DOM returns mount update track dependencies {{ count }}
0
const t0 = template("") delegateEvents("click") function render(ctx) { const n0 = t0() delegate(n0, “click", () => $event => (ctx.count++)) renderEffect( () => setText(n0, ctx.count)) return n0 } 0 Reactive Effect trigger effect register
1
• First rendering

Slide 22

Slide 22 text

Rendering mechanism Tamplate compiled into Render Function Component Reactivity State Framgments (DOM Nodes) Actual DOM returns mount update track dependencies {{ count }}
0
const t0 = template("") delegateEvents("click") function render(ctx) { const n0 = t0() delegate(n0, “click", () => $event => (ctx.count++)) renderEffect( () => setText(n0, ctx.count)) return n0 } 0 Reactive Effect trigger effect register
1
• Re-rendering

Slide 23

Slide 23 text

• IR (Intermediate Representation) Architecture Compiler Optimized Vue AST generate transform JS Code Vue AST Tamplate parse Virutal DOM Mode (Currently)

Slide 24

Slide 24 text

• IR (Intermediate Representation) Architecture Compiler Vapor IR generate transform JS Code Vue AST Tamplate parse Optimized Vue AST generate transform JS Code Vue AST Tamplate parse vDOM Mode (Currently) Vapor Mode

Slide 25

Slide 25 text

Runtime • Virtual DOM is no longer necessary ! • LESS codes ! SMALL size! runtime-vapor is over 50% smaller than runtime-dom!

Slide 26

Slide 26 text

Bene fi ts with Vapor Mode

Slide 27

Slide 27 text

Bene fi ts with Vapor Mode • Performance • Compatibility • Interoperability

Slide 28

Slide 28 text

Performance https://talks.sxzz.moe/2024-10-vue-fes-japan/presenter/7?clicks=5

Slide 29

Slide 29 text

Compatibility • Vapor Mode is a subset of vDOM Mode • vDOM Mode • Composition API • Options API, and etc … • Vapor Mode • Composition API • Vapor Mode vDOM Mode

Slide 30

Slide 30 text

Interoperability • Mixin Vapor Mode and vDOM Mode components in App App.vue vDOM vDOM vDOM Vapor Vapor Vapor

Slide 31

Slide 31 text

Potential of Vapor Mode

Slide 32

Slide 32 text

@vue/compiler-vapor [compile] Compilation Architecture [parse] [transform] [generate] @vue/compiler-sfc [compileTemplate] @vitejs/plugin-vue [transform] Figure inspired from Kevin Deng & Rizume Ayaka, Thanks! SFC

Slide 33

Slide 33 text

@vue/compiler-vapor [compile] Compilation Architecture [parse] Vue Template → Vue AST [transform] Vue AST → Vapor IR [generate] Vapor IR → JS Code @vue/compiler-sfc [compileTemplate] @vitejs/plugin-vue [transform] SFC Figure inspired from Kevin Deng & Rizume Ayaka, Thanks!

Slide 34

Slide 34 text

What’s IR? • IR (Intermediate Representation) • Data strcture or code internally used by compiler or virtual machine to represent source code • Usages • Cross Platform • Optimization • Translation Compile / Transform IR Source Code Generate Target Executable Code / Binary

Slide 35

Slide 35 text

• Representation of template structure and template operations, Tree nodes like AST Vapor IR Block Root If SetText CreateText Node InsertNode Create Component Block Block CreateText Node Prepend Node export interface IfIRNode extends BaseIRNode { type: IRNodeTypes.IF id: number condition: SimpleExpressionNode positive: BlockIRNode negative?: BlockIRNode | IfIRNode once?: boolean }

Slide 36

Slide 36 text

Vapor IR Node Types • 20 Vapor IR node types • Block node has operations • Block node is able to register 18 Vapor IR types as operations • Operations of render function • e.g. DOM manupluration, Set Props / Attrs, Event bindings, and … • If & For node has block node export enum IRNodeTypes { ROOT, BLOCK, SET_PROP, SET_DYNAMIC_PROPS, SET_TEXT, SET_EVENT, SET_DYNAMIC_EVENTS, SET_HTML, SET_TEMPLATE_REF, SET_MODEL_VALUE, SET_INHERIT_ATTRS, INSERT_NODE, PREPEND_NODE, CREATE_TEXT_NODE, CREATE_COMPONENT_NODE, SLOT_OUTLET_NODE, WITH_DIRECTIVE, DECLARE_OLD_REF, IF, FOR, }

Slide 37

Slide 37 text

e.g. Vapor IR Tree • Counter component, which has condition Block Root If SetEvent Block InsertNode
count: {{ count }}

Count over 10

Slide 38

Slide 38 text

Now, As you may have noticed …

Slide 39

Slide 39 text

Is it possible to transform it to Vapor IR using non-Vue template?

Slide 40

Slide 40 text

YES!

Slide 41

Slide 41 text

Support Vue JSX! @vue/compiler-vapor [compile] [parse] Vue Template → Vue AST [transform] Vue AST → Vapor IR [generate] Vapor IR → JS Code @vue/compiler-sfc [compileTemplate] @vitejs/plugin-vue [transform] SFC unplugin-vue-jsx-vapor [transform] compiler [compile] [parse] Vue JSX → JSX AST [transform] JSX AST → Vapor IR Vue JSX

Slide 42

Slide 42 text

Svelte DEMO

Slide 43

Slide 43 text

DEMO Contents • Svelte Template Explorer • Playground Counter Component

Slide 44

Slide 44 text

inclusion-vapor • Open-source project • Component interoperability based on Vapor • Repository: https://github.com/kazupon/inclusion-vapor

Slide 45

Slide 45 text

Fusion of components coming? • Just `import` only! Vue Vapor Vue Vapor React Vapor React Vapor Svelte Vapor Svelte Vapor Vue Vapor

Slide 46

Slide 46 text

Vapor future plan

Slide 47

Slide 47 text

Vapor future plan • Support platform-agnostic • currently, browser only • native mobile app • More improvements and generalization? • Release with Vue 3.6 as experimental

Slide 48

Slide 48 text

Conclusion

Slide 49

Slide 49 text

Recap • Vapor Mode • Vapor is an alternative compilation strategy & new begenning for Vue • Potential with Vapor Mode • Vapor is support IR architecture • Vapor will have better Vue JSX support • If we follow the Vapor conventions & I/F, we can also support non-Vue templates

Slide 50

Slide 50 text

Vapor Team, Thanks! Evan You Kevin Deng Rizumu Ayaka Ubugeeei Doctor Wu zhiyuanzmj

Slide 51

Slide 51 text

Join us! • Vapor Project : https://github.com/vuejs/vue-vapor • Lean about Vapor : https://github.com/ubugeeei/reading-vuejs-core-vapor • Component interoperability OSS Project : inclusion-vapor 🫶 https://github.com/kazupon/inclusion-vapor

Slide 52

Slide 52 text

Thank You! ❤