uniapp 微信小程序代码自动上传到微信后台

1.微信后台获取上传密钥和白名单配置

  • 生成小程序代码上传密钥
  • IP 白名单配置(公网白名单)
    img

生成完成之后下载到根目录或项目其他地方(一会用得到)

上传失败后,编译器会提示对应 ip

1
2
上传项目到微信平台失败
Error: {"errCode":-10008,"errMsg":"invalid ip: 220.165.xxx.xx, reference: https://developers.weixin.qq.com/miniprogram/dev/devtools/ci.html"}

2.脚本安装(npm、cnpm、yarn)

1
npm i miniprogram-ci -D

3.创建配置文件

在项目根目录创建 autoUpload.js和添上面生成的密钥

img

autoUpload.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const path = require("path");
import config from "./utils/config"; //配置文件,根据自己项目情况引入

const ci = require("miniprogram-ci");
(async () => {
const project = new ci.Project({
appid: config.appid, //appid
type: "miniProgram",
projectPath: path.resolve(__dirname, "./unpackage/dist/build/mp-weixin"), //项目路径,
privateKeyPath: path.resolve(__dirname, "./private.wx143e******c97bf0.key"), //密钥位置
ignores: ["node_modules/**/*"],
});
const uploadResult = await ci
.upload({
project,
version: "1.0.2",
desc: "gk",
setting: {
es6: true,
minifyJS: true, // 压缩 JS 代码
minifyWXML: true, // 压缩 WXML 代码
minifyWXSS: true, //压缩 WXSS 代码
minify: true, //压缩所有代码,对应小程序开发者工具的 "压缩代码"
// codeProtect:false 对应小程序开发者工具的 "代码保护"
autoPrefixWXSS: true, //对应小程序开发者工具的 "样式自动补全"
},
usingComponents: true,
lazyCodeLoading: "requiredComponents",
onProgressUpdate: console.log,
})
.then((res) => {
console.log("成功", res);
// console.log(`版本${mainfest.versionName}`)
})
.catch((error) => {
console.log("失败", error);
throw error;
});
// console.log(uploadResult)
})();

4.运行结果

上传代码
img

上传成功
img

官网地址