如何将微信小程序从WebView迁移到Skyline

什么是 Skyline

微信小程序新的渲染引擎,使用更加高效的渲染管线,提供更好的性能和全新的交互动画体验。

具体可以查阅官网介绍

将开发者工具切换成 Sykline 模式

  1. 调试基础库切到 2.30.4 或以上版本

  2. 确保右上角 > 详情 > 本地设置里的 开启 Skyline 渲染调试、启用独立域进行调试 选项被勾选上

  3. 确保右上角 > 详情 > 本地设置里的 将 JS 编译成 ES5 选项被勾选上

使用 skylint 工具迁移

复制代码
npx skylint

使用过程中可能会出现文件未找到错误,例如

原因就是使用绝对路径 <import src="/components/chooserList/index.wxml" />导入模块,而 skylint 无法找到该文件,需要修改为相对路径 <import src="../../components/chooserList/index.wxml" />导入模块

有几种提示不是很准确,可以评估下:

  1. @position-fixed 不支持 position: fixed:如果你根据不同 renderer 兼容,则会导致该提示一直存在

  2. @no-pseudo-element 不支持伪元素: 目前对已经支持的 ::before 和 ::after 也会进行提示

手动迁移

在 app.json 配置

json 复制代码
{
"lazyCodeLoading": "requiredComponents", // 开启按需注入
    "rendererOptions": {
        "skyline": {
            "defaultDisplayBlock": true // skyline 下节点默认为 flex 布局,可以在此切换为默认 block 布局
        }
    }
}

在 page.json 配置

json 复制代码
{
    "renderer": "skyline", // 声明为 skyline 渲染,对于已有的项目,建议渐进式迁移,对于新项目,直接全局打开,在 app.json 里进行配置
    "componentFramework": "glass-easel", // 声明使用新版 glass-easel 组件框架
    "disableScroll": true, // skyline 不支持页面全局滚动,为了使之与WebView保持兼容,在此禁止滚动
    "navigationStyle": "custom" // skyline 不支持原生导航栏,为了使之与WebView保持兼容,并且自行实现自定义导航栏
}

skyline 不支持页面全局滚动,如果需要页面滚动,在需要滚动的区域使用 scroll-view 实现

html 复制代码
<scroll-view type="list" scroll-y style="flex: 1; width: 100%; overflow: hidden;"></scroll-view>
css 复制代码
page {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

skyline 渲染模式下 flex-direction 默认值是 column,为了使之与WebView保持兼容,需要在 flex 布局里将 flex-direction 默认值改为 row

在真机上调试 skyline 渲染模式

小程序菜单 > 开发调试 > Switch Render,会出现三个选项,说明如下:

Auto :跟随 AB 实验,即对齐小程序正式用户的表现

WebView :强制切为 WebView 渲染

Skyline :若当前页面已迁移到 Skyline,则强制切为 Skyline 渲染

常见问题

position: fixed 不支持

需要将

html 复制代码
<view class="background"></view>
css 复制代码
.background {

    position: fixed;

}

修改为

html 复制代码
<root-portal>

    <view class="background"></view>

</root-portal>
css 复制代码
.background {

    position: absolute;

}

如果无法做到适配,则可以根据不同 renderer 兼容

html 复制代码
<view class="position {{renderer}}"></view>
css 复制代码
.position {
    position: fixed;
}

.position.skyline {
    position: absolute;
}
js 复制代码
Page({
    data: {
        renderer: 'webview'
    },

    onLoad() {
        this.setData({
            renderer: this.renderer,
        })
    },
})

不支持 Canvas 旧版接口

Skyline 渲染模式下,旧版 Canvas 绘制图案无效(使用 wx.createCanvasContext 创建的上下文)

在真机中图片的 referer 丢失

测试结果如下:

使用 WebView 渲染 Image,请求的 header 是:

json 复制代码
{
    host: 'xxx',
    connection: 'keep-alive',
    accept: 'image/webp,image/avif,video/*;q=0.8,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5',
    'user-agent': 'xxx',
    'accept-language': 'zh-CN,zh-Hans;q=0.9',
    referer: 'https://servicewechat.com/',
    'accept-encoding': 'gzip, deflate'
}

使用 Skyline 渲染 Image,请求的 header 是:

json 复制代码
{

    'user-agent': 'Dart/2.16 (dart:io)',
    'accept-encoding': 'gzip',
    host: 'xxx'
}

官方 Demo

可以参考官方Demo学习使用 Skyline 的增强特性,比如 Worklet 动画、手势系统等,但在首次下载编译时,会遇到【交互动画】页面为空的问题,主要原因是该页面是由 TypeScript 写的,编译成 JavaScript 需要开启工具内置的 TypeScript编译插件,需要在project.config.json project.config.json 配置:

json 复制代码
setting.useCompilerPlugins: ["typescript"]

参考

相关推荐
andrsted12 小时前
作业发布、课堂检测和错题讲评,用一个小程序完成
学习·微信小程序·小程序·学习方法
汤姆yu13 小时前
基于微信小程序的家电维修预约系统
微信小程序·小程序·家电维修
软件聚导航1 天前
「聚小软 AI 助手」技术升级:从数据库检索到 RAG 知识库问答
前端·数据库·mysql·微信小程序·小程序·ai编程·rag
一叶星殇1 天前
微信小程序 Vant Popup 滚动穿透:页面级与组件级解决方案详解
微信小程序·小程序
2601_963869952 天前
【计算机毕业设计】基于微信小程序的点餐传菜系统设计与实现
微信小程序·小程序·课程设计
北漂燕郊杨哥2 天前
VS Code 安装微信小程序 MCP 介绍
vscode·微信小程序·小程序·mcp
维双云2 天前
2026微信小程序开店用哪个平台?长期稳定运营的系统选择方法
微信小程序·小程序
辛迪聊物业数字化2 天前
【智能楼宇系统有什么用?赋能园区高效 节能 安全】
经验分享·安全·微信小程序·php
Coodor3 天前
使用web也可以写NFC微信小程序拉取
前端·微信小程序·小程序·nfc拉起小程序
太阳之神aboluo3 天前
微信小程序 (1) 基础用法
微信小程序·小程序