首页 > 其他分享 >#yyds干货盘点#Tauri 与 Electron

#yyds干货盘点#Tauri 与 Electron

时间:2023-05-18 21:32:22浏览次数:42  
标签:yyds const app electron yarn js Tauri Electron

Tauri

什么是 Tauri ?

Tauri 是一个为所有主流桌面平台构建小型、快速二进制文件的框架。开发人员可以集成任何编译成 HTML、 JS 和 CSS 的前端框架来构建他们的用户界面。应用程序的后端是一个 Rust 二进制文件,具有前端可以与之交互的 API。

安装方式

Xcode

$ xcode-select --install

Rust

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

安装过程中如果报错 curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused

需要开启代理:

# 7890 和 789 需要换成你自己的代理端口
$ export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:789

要确保 Rust 已成功安装,请运行以下命令

$ rustc --version

$rustc 1.59.0 (9d1b2106e 2022-02-23)

启动一个新的 Tauri

$ yarn create tauri-app

创建项目的时候,如果报错 An unexpected error occurred: "https://registry.yarnpkg.com/create-vite: tunneling socket could not be established

同样的,需要开启代理,使用:

$ yarn config set proxy http://username:password@host:port
$ yarn config set https-proxy http://username:password@host:port

按照说明选择您喜欢的 Web 前端框架,create-tauri-app 根据您的输入创建模板项目,之后你可以直接去检查 tauri info

启动

$ yarn tauri dev

打包

$ yarn tauri build

Electron

什么是 Electron ?

Electron 框架允许您使用 JavaScript、HTML 和 CSS 编写跨平台的桌面应用程序。它基于Node.js和 Chromium,并被Atom 编辑器和许多其他应用程序使用。

安装方式

创建项目

$ mkdir my-electron-app && cd my-electron-app
$ yarn init

修改 package.json,"main" 字段为 main.js,你的 package.json 应该是如下样子:

{
  "name": "my-electron-app",
  "version": "1.0.0",
  "description": "Hello World!",
  "main": "main.js",
  "author": "Jiang Chen",
  "license": "MIT"
}

Electron

$ yarn add --dev electron

执行 Electron。在 package.json 配置字段 scripts,添加 start 如下命令

{
  "scripts": {
    "start": "electron ."
  }
}

根目录新增 main.js

代码如下

// main.js

// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('path')

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

根目录新增 index.html

代码如下

<!--index.html-->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using Node.js <span id="node-version"></span>,
    Chromium <span id="chrome-version"></span>,
    and Electron <span id="electron-version"></span>.

    <!-- You can also require other files to run in this process -->
    <script src="./renderer.js"></script>
  </body>
</html>

根目录新增 renderer.js

代码如下

window.addEventListener('DOMContentLoaded', () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector)
    if (element) element.innerText = text
  }

  for (const type of ['chrome', 'node', 'electron']) {
    replaceText(`${type}-version`, process.versions[type])
  }
})

启动

$ npm run start

打包

添加 Electron Forge 作为应用程序的开发依赖项
$ cnpm install --dev @electron-forge/cli
$ npx electron-forge import
make使用 Forge 的命令创建一个打包
$ yarn run make

两者区别

  1. 大小对比
  • Electron 官方介绍有提到,它基于 Node.js 和 Chromium,很明显的问题,包太大(62.5mb)使用 Chromium 的决定,也是解决 WebView 暂时无法解决的一些兼容性问题
  • Tauri,前端是通过系统的 WebView2,后端使用 Rust,包很小(4.32MB)
  1. 官方文档
  • Electron 官方文档和社区迭代,目前比较稳定,发布了多个版本,可以稳定在生产使用
  • Tauri 作为一款新型桌面端开发框架,1.0.0 版本暂时未出,可以持续关注,尝试做些小工具

总结

Tauri 作为一款新型桌面端开发框架,踩在了 Rust 和 WebView2 的两位巨人的肩膀上,可以说是时代的产物,Rust 近几年非常受欢迎,Deno 采用 Rust,微软拥抱 Rust 等,微软又开始推广 WebView2,天然的优势

标签:yyds,const,app,electron,yarn,js,Tauri,Electron
From: https://blog.51cto.com/u_11365839/6307251

相关文章

  • #yyds干货盘点# LeetCode程序员面试金典:相交链表
    1.简述:给你两个单链表的头节点 headA和headB,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回null。图示两个链表在节点c1开始相交:题目数据保证整个链式结构中不存在环。注意,函数返回结果后,链表必须保持其原始结构。自定义评测:评测系统的输入......
  • #yyds干货盘点# LeetCode程序员面试金典:从中序与后序遍历序列构造二叉树
    题目:给定两个整数数组inorder和postorder,其中inorder是二叉树的中序遍历,postorder是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。 示例1:输入:inorder=[9,3,15,20,7],postorder=[9,15,7,20,3]输出:[3,9,20,null,null,15,7]示例2:输入:inorder=[-1],postorder......
  • #yyds干货盘点# LeetCode面试题:乘积最大子数组
    1.简述:给你一个整数数组nums ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。测试用例的答案是一个 32-位整数。子数组是数组的连续子序列。 示例1:输入:nums=[2,3,-2,4]输出:6解释: 子数组[2,3]有最大乘积6。示例......
  • #yyds干货盘点# LeetCode程序员面试金典:二叉树的层序遍历
    题目:给你二叉树的根节点root,返回其节点值的层序遍历。(即逐层地,从左到右访问所有节点)。 示例1:输入:root=[3,9,20,null,null,15,7]输出:[[3],[9,20],[15,7]]示例2:输入:root=[1]输出:[[1]]示例3:输入:root=[]输出:[]代码实现:classSolution{publicList<List<Integer>>......
  • #yyds干货盘点# LeetCode程序员面试金典:对称二叉树
    1.简述:给你一个二叉树的根节点root,检查它是否轴对称。 示例1:输入:root=[1,2,2,3,4,4,3]输出:true示例2:输入:root=[1,2,2,null,3,null,3]输出:false2.代码实现:classSolution{publicbooleanisSymmetric(TreeNoderoot){returncheck(root,root);}......
  • Electron设置窗口大小setSize遇到的问题
    应用场景登录页跳转主页-窗口放大主页跳转登录页-窗口缩小方法setSize()遇到的问题在主页退出登录时,窗口缩小时setSize()不生效查看官方文档发现设置了窗口最小范围时,会影响setSize()所以,setSzie()调用要放在setMinimumSize()、setMaximumSize()后面functioninitMain......
  • #yyds干货盘点# LeetCode面试题:不同的二叉搜索树 II
    1.简述:给你一个整数 n ,请你生成并返回所有由 n 个节点组成且节点值从 1 到 n 互不相同的不同 二叉搜索树 。可以按 任意顺序 返回答案。 示例1:输入:n=3输出:[[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]示例2:输入:n=1输出:[[1]]2.代......
  • #yyds干货盘点# LeetCode程序员面试金典:跳跃游戏 II
    题目:给定一个长度为 n 的 0索引整数数组 nums。初始位置为 nums[0]。每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i+j] 处:0<=j<=nums[i] i+j<n返回到达 nums[n-1] 的最小跳跃次数。生成的......
  • #yyds干货盘点#Linux使用者与群组
    Linux使用者身份与群组记录的文件在Linux系统当中,默认的情况下,所有的系统上的帐号与一般身份使用者,还有那个root的相关信息,都是记录在/etc/passwd这个文件内的。至于个人的XX则是记录在/etc/shadow这个文件下。此外,Linux所有的群组名称都纪录在/etc/group内!这三个文件可以说是......
  • #yyds干货盘点#python,Lambda
    lambda 关键字用于创建小巧的匿名函数。lambda a, b: a+b 函数返回两个参数的和。Lambda函数可用于任何需要函数对象的地方。在语法上,匿名函数只能是单个表达式。在语义上,它只是常规函数定义的语法糖。与嵌套函数定义一样,lambda函数可以引用包含作用域中的变量:>>>defmake_......