zhangdaren
zhangdaren
  • 发布:2019-06-20 15:23
  • 更新:2024-03-29 21:03
  • 阅读:72893

miniprogram-to-uniapp使用指南(各种小程序项目转换为uni-app项目)

分类:uni-app

工具现在支持npm全局库、HBuilderX插件两种方式使用,任君选择,HBuilderX插件地址:https://ext.dcloud.net.cn/plugin?id=2656

一、它是谁?

【miniprogram-to-uniapp】转换微信小程序”项目为uni-app项目(新版本工具已经支持各种小程序转换)。

二、它的原理是什么?

最初是学了半节课堆和栈,觉得词法分析挺有意思的,再加上转换小程序插件时,发现这些繁琐的操作完全可以使用程序来完成。
核心是使用Babel获取AST(词法分析),然后或使用Babel自带函数增删,或正则分析替换等等操作。
可能有的朋友觉得,这种为啥不是纯正则分析? 纯正则可以解决一部分问题,对于标签与标签内容,其实正则是很难进行区分的哈。

三、它能做哪些事情?

  • 支持微信、QQ、头条/抖音、支付宝/钉钉和百度等小程序转换到 uni-app 项目
  • 支持有/无云开发的小程序项目转换为 uni-app 项目(cloudfunctions 目录将被忽略,uni-app 结合小程序云开发见:使用 uni-app 进行微信小程序云开发经验分享)
  • 支持解析 TypeScript 小程序项目
  • 支持解析使用 npm 模块的小程序项目
  • 支持解析 include 标签
  • 支持解析 template 标签
  • 支持解析 Behavior 文件为 mixins 文件
  • 支持.js', .wxml 和*.wxss 文件进行相应转换,并做了大量的优化
  • 支持识别 App、Page、Component、VantComponent、Behavior 和纯 Javascript 文件的转换
  • 修复变量名与函数重名的情况
  • 合并使用 require 导入的 wxs 文件
  • setData() polyfill
  • 搜索未在 data 声明,而直接在 setData()里使用的变量,并修复
  • 使用jyf-parser替换 wxParse(感谢网友 “爱瑞巴勒康忙北鼻” 的建议)
  • 因 uni-app 会将所有非 static 目录的资源文件删除,因此将所有资源文件移入 static 目录,并修复所有能修复到的路径(目前 uni 编译时会将非 static 目录的文件复制一份到 static 目录,但并不完全,因此本功能仍保留)

四、它还有哪些不支持转换?

  • 不支持转换反编译后的小程序项目
  • 不支持转换使用 uni-app 编译的小程序项目
  • 不支持转换使用 redux 开发的小程序(代表为:网易云信小程序 DEMO)
  • 不支持转换使用 wxpage 开发的小程序(https://github.com/tvfe/wxpage)
  • 不支持转换使用腾讯 omi 开发的小程序(https://github.com/Tencent/omi)
  • 不支持转换小程序抽象节点 componentGenerics
  • 不支持 component 里的 pageLifetimes 生命周期,请手动绕过
  • 不支持使用 js 系统关键字作为函数或变量名(如 default、import、return、switch 等)
  • 不支持以\$开头的变量名称,如 Page({data:{$data:{name:"hello"}}}) ,刚好\$data 是 vue 内置变量,so 不支持,需手动修复
  • 不支持以动态绑定的函数<input @input="test{{index+1}}">,需手动修复
  • 更多,请参照miniprogram to uniapp 工具答疑

五、怎么使用?

第一步

在命令行里,运行【 npm install miniprogram-to-uniapp -g 】进行安装,因为这个包是工具,要求全局都能使用,所以需要-g进行全局安装。
如果运行npm报错,请先安装Node.js,下载地址:https://nodejs.org/zh-cn/

第二步

继续在命令行里,运行【 wtu -V 】,执行结果如下:

显示版本号,说明已经安装成功了。(wtu -> 取自wx to uni之意,后面都用这个全局命令)

第三步

在命令行里,输入【wtu -i "你的小程序项目路径"】
注意 -i 前面和后面都有空格!!!
注意 -i 前面和后面都有空格!!!
注意 -i 前面和后面都有空格!!!
如:【wtu -i "E:\zpWork\Project_self\miniprogram-to-uniapp\test\test-wx-to-uni"】 ,回车后即可以在源项目同及目录得到一个后缀为_uni的目录,即转换成功。

转换前:

转换后:

转换后的项目文件对比(左边是小程序项目,右边是Uni-app项目目录):

第四步

将转换后的xxx_uni项目导入到hbuilder X,
点击菜单 运行--> 运行到小程序模拟器-->微信开发者工具!(如果是使用了vant的项目,请运行到H5,vant项目转换后仅支持H5和app)
点击菜单 运行--> 运行到小程序模拟器-->微信开发者工具!(如果是使用了vant的项目,请运行到H5,vant项目转换后仅支持H5和app)
点击菜单 运行--> 运行到小程序模拟器-->微信开发者工具!(如果是使用了vant的项目,请运行到H5,vant项目转换后仅支持H5和app)
重要的话说三遍!
然后查看转换后的项目运行到小程序,是否可以正常运行无报错!
(因为这种转换非100%,所以至少需要保证 小程序-->uniapp-->小程序仍然能正常运行,再考虑运行到其他小程序或app),
如有报错,请根据miniprogram to uniapp 工具答疑 进行修改,保证无报错,然后再运行到其他平台。

工具升级

因为工具更新比较频繁,安装后,可以使用如下命令进行升级:
npm update miniprogram-to-uniapp -g

注意:

  1. 遇到问题请参考文档:miniprogram to uniapp 工具答疑
  2. github上面的只是源代码。 日常使用,"仅"需要运行【 npm install miniprogram-to-uniapp -g 】进行安装才行,无需下载工具源码。
  3. 每次转换时都会将输出目录清空,请注意代码保存。
  4. 后续会有版本更新,请及时关注并更新~~,可以关注QQ群:780359397 获取最新信息

对于使用有疑问或建议,可以加入QQ群:780359397 进行讨论。

<a target="_blank" href="http://shang.qq.com/wpa/qunwpa?idkey=6cccd111e447ed70ee0c17672a452bf71e7e62cfa6b427bbd746df2d32297b64"><img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="小程序转uni-app讨论群" title="小程序转uni-app讨论群"></a>

因为有朋友使用本工具导致自己文件被删,在此表示抱歉!

this.setData()代码出处:https://ask.dcloud.net.cn/article/35020,在些表示感谢~

转换工具源码:https://github.com/zhangdaren/miniprogram-to-uniapp ,欢迎star和建议~

如有遇到bug或问题,欢迎评论、给出改进建议,感谢~~

21 关注 分享
z***@qq.com m***@gmail.com 7***@qq.com orageBeardApe 邓琪昌 bzliukai 我要 撇横横 8***@qq.com g***@126.com DigitalYu jinjidecj ai666 9***@qq.com heck b***@126.com 2***@qq.com Dlog_帅 灯下等雪 aliang888 876

要回复文章请先登录注册

zhangdaren

zhangdaren (作者)

回复 8***@qq.com :
已经内置了一个mixins,里面有setData的实现,所以能直接用。
2024-03-29 21:03
8***@qq.com

8***@qq.com

转换完居然还有this.setdata这种方法,真是离谱
2024-01-03 16:28
zhangdaren

zhangdaren (作者)

回复 2***@qq.com :
重新安装一下HBuilderX??
2022-02-17 16:40
2***@qq.com

2***@qq.com

报错:”下载编译工具失败,请重新运行”,是什么情况
2022-02-11 09:57
zhangdaren

zhangdaren (作者)

回复 s***@163.com :
请加群,然后,下载群里的文件替换一下就行了,,目前 在弄v2.0,大幅修改了原有逻辑,预计十一前可以出来。
2021-09-11 14:05
s***@163.com

s***@163.com

这种错误怎么解决?

[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
[Tip] 检测setData里面的变量 [] 是数组形式,uniapp不支持,已尝试进行修复。 file--> pages\index\index.js
(node:26388) UnhandledPromiseRejectionWarning: TypeError: Property value of ObjectProperty expected node to be of a type ["Expression","PatternLike"] but instead got undefined
at Object.validate (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\node_modules\@babel\types\lib\definitions\utils.js:130:11)
at validateField (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\node_modules\@babel\types\lib\validators\validate.js:24:9)
at validate (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\node_modules\@babel\types\lib\validators\validate.js:17:3)
at builder (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\node_modules\@babel\types\lib\builders\builder.js:38:27)
at Object.objectProperty (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\node_modules\@babel\types\lib\builders\generated\index.js:400:31)
at addWatchHandlerItem (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\wx2uni\js\componentConverter.js:437:27)
at C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\wx2uni\js\componentConverter.js:422:17 at Array.forEach (<anonymous>)
at observersHandle (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\wx2uni\js\componentConverter.js:378:20)
at lifeCycleHandle (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\wx2uni\js\componentConverter.js:213:17)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:26388) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:26388) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:26388) UnhandledPromiseRejectionWarning: TypeError: Property value of ObjectProperty expected node to be of a type ["Expression","PatternLike"] but instead got undefined
at Object.validate (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\node_modules\@babel\types\lib\definitions\utils.js:130:11)
at validateField (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\node_modules\@babel\types\lib\validators\validate.js:24:9)
at validate (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\node_modules\@babel\types\lib\validators\validate.js:17:3)
at builder (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\node_modules\@babel\types\lib\builders\builder.js:38:27)
at Object.objectProperty (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\node_modules\@babel\types\lib\builders\generated\index.js:400:31)
at addWatchHandlerItem (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\wx2uni\js\componentConverter.js:437:27)
at C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\wx2uni\js\componentConverter.js:422:17 at Array.forEach (<anonymous>)
at observersHandle (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\wx2uni\js\componentConverter.js:378:20)
at lifeCycleHandle (C:\Users\Yappam\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\wx2uni\js\componentConverter.js:213:17)
(node:26388) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)
2021-08-25 10:36
zhangdaren

zhangdaren (作者)

回复 峰哥哥哥哥哥 :
需定位报错信息,修改好才行,不同的小程序不同的情况,工具没法覆盖所有情况。
2021-06-18 14:45
zhangdaren

zhangdaren (作者)

回复 峰哥哥哥哥哥 :
需调试看看是什么问题,先把所有报错解决。。也可加入上面提示的qq群反馈~
2021-05-31 14:04
峰哥哥哥哥哥

峰哥哥哥哥哥

转是可以转,小程序转uni-app打开页面都加载不出来,只显示底部tabbar
2021-05-27 16:34
bug开发工程师

bug开发工程师

(node:1680) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'keyword' of undefined
at jsHandle (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\wx2uni\jsHandle.js:963:47)
at C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:715:83
at C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:808:19
at new Promise (<anonymous>)
at filesHandle (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:327:22)
at C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:1246:9
at checkEnd (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:54:40)
at checkEnd (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:54:40)
at checkEnd (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:54:40)
at checkEnd (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:54:40)
(node:1680) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 55)
(node:1680) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'keyword' of undefined
at jsHandle (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\wx2uni\jsHandle.js:963:47)
at C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:715:83
at C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:808:19
at new Promise (<anonymous>)
at filesHandle (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:327:22)
at C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:1246:9
at checkEnd (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:54:40)
at checkEnd (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:54:40)
at checkEnd (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:54:40)
at checkEnd (C:\Users\slb0223\AppData\Roaming\npm\node_modules\miniprogram-to-uniapp\src\index.js:54:40)
(node:1680) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 56)


一堆rejection,nuxt项目,把node_modules与git文件夹过滤掉,然后直接运行 wtu -i 项目路径
2021-03-12 17:51