Replies: 4 comments
-
How Vapor Mode Can Improve the Cross-Platform View CompilationWhat is Vapor Mode?Vapor Mode is an optimization mode introduced in Vue 3, primarily aimed at compile-time static analysis. By completely removing runtime template parsing, Vapor Mode compiles templates and directives directly into highly efficient native code. How Does It Improve the Above Approach?We can leverage the advantages of Vapor Mode to optimize the process of compiling the Vue 1. Static Template Analysis
2. Directive Optimization
3. Style Mapping
4. No Runtime Dependencies
SummaryBy leveraging Vue's Vapor Mode, we can significantly improve the process of compiling Vue's view parts (template and style) into SwiftUI and Compose, with key benefits including:
blog:https://www.vuemastery.com/blog/the-future-of-vue-vapor-mode/#supported-features |
Beta Was this translation helpful? Give feedback.
-
Proposed Workflow and Implementation
In this example, the Here is the code for transforming the Vue AST into SwiftUI components: function vueAstToSwiftUI(ast) {
let swiftCode = '';
if (ast.tag === 'div') {
swiftCode += 'VStack {\n';
}
if (ast.children) {
ast.children.forEach(child => {
if (child.type === 1) { // Type 1 represents an element node in Vue AST
if (child.tag === 'h1') {
swiftCode += `Text("${child.children[0].text}")\n`;
}
if (child.tag === 'p' && child.attrsMap['v-if']) {
swiftCode += `if (showText) {\n Text("${child.children[0].text}")\n}\n`;
}
if (child.tag === 'ul') {
swiftCode += 'ForEach(items, id: \\.self) { item in\n Text(item)\n}\n';
}
}
});
}
if (ast.tag === 'div') {
swiftCode += '}\n';
}
return swiftCode;
} For the example template, this would output the following SwiftUI code: VStack {
Text("{{ title }}")
if (showText) {
Text("{{ text }}")
}
ForEach(items, id: \.self) { item in
Text(item)
}
}
import SwiftUI
struct ContentView: View {
@State var title = "Hello, World!"
@State var showText = true
@State var text = "This is a conditional text."
@State var items = ["Item 1", "Item 2", "Item 3"]
var body: some View {
VStack {
Text(title)
if showText {
Text(text)
}
ForEach(items, id: \.self) { item in
Text(item)
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
In the future, Vite will fully transition to Rust, including new tools like Rolldownand oxc. I hope this ecosystem will be helpful to you. |
Beta Was this translation helpful? Give feedback.
-
Alternative Proposal: Direct Usage of Vue Syntax for CompilationProposalInstead of creating a new AST transformation pipeline, we can leverage Vue's existing compiler and syntax directly to generate SwiftUI and Jetpack Compose code. This approach offers the following advantages:
Implementation Overview
Input Template (Vue)<template>
<div>
<h1>{{ title }}</h1>
<p v-if="showText">{{ text }}</p>
<ul>
<li v-for="item in items" :key="item">{{ item }}</li>
</ul>
</div>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const title = ref('Hello, World!');
const showText = ref(true);
const text = ref('This is a conditional text.');
const items = ref(['Item 1', 'Item 2', 'Item 3']);
return {
title,
showText,
text,
items,
};
},
};
</script>
|
Beta Was this translation helpful? Give feedback.
-
Technical Proposal: Cross-Platform View Layer Reuse Based on Template Separation
Background and Requirements
Challenges in View Reuse
Cross-platform development (e.g., iOS and Android) often requires platform-specific view code, which significantly increases development and maintenance costs.
Mature Logic Sharing Solutions
Frameworks like KMM (Kotlin Multiplatform Mobile) have improved efficiency by sharing logic layers, but there is still substantial room for improvement in view layer reuse.
Separation of View and Logic
Inspired by Vue's Single-File Component (SFC) structure, this proposal suggests reusing the
template
andstyle
parts for cross-platform views while retaining thescript
part for native code (e.g., Swift and Kotlin). This approach reduces view code duplication and enhances flexibility by combining shared views with native logic.Technical Path
1. Template-Driven View Layer
Concept
Use a Vue-like
template
to define the view structure. The HTML-style syntax is intuitive and easy to understand.template
into platform-specific view code:View
structures for iOS.Composable
structures for Android.Implementation
A template compiler parses the
template
into an abstract syntax tree (AST) and generates corresponding platform code.2. Style-Driven Visual Consistency
Concept
Use CSS-like
style
syntax to describe view styles..foregroundColor
,.font
).Modifier
).Implementation
A style compiler maps shared style rules to platform-specific implementations.
3. Flexible Native Logic Integration
Concept
script
section, maintaining seamless integration with platform-specific logic.Implementation
The
script
section is directly embedded in the target platform logic without additional compilation.Advantages of .vue Over JSX
Compared to JSX, Vue's SFC model offers several advantages:
Clear Separation of Concerns
template
,style
, andscript
into distinct sections, making it easier to organize and maintain.Better Compilation Suitability
Stronger Extensibility
v-if
,v-for
) that can be optimized during compilation for efficient rendering.Framework Implementation Overview
1. Architecture Design
Each component is written in a
.dioxus
file (or similar extension), mimicking Vue's SFC:2. Compilation Workflow
Template Compilation
Parse the
template
into an AST and map it to platform-specific view code:SwiftUI example:
Jetpack Compose example:
Style Compilation
Convert the
style
section into platform-specific style declarations.Script Integration
Embed the
script
section directly into the target platform logic.Beta Was this translation helpful? Give feedback.
All reactions