miniprogram-ci官方文档

miniprogram-ci简介

miniprogram-ci 是从微信开发者工具中抽离的关于小程序/小游戏项目代码的编译模块。

开发者可不打开小程序开发者工具,独立使用 miniprogram-ci 进行小程序代码的上传、预览等操作。

https://www.npmjs.com/package/miniprogram-ci

使用前提

平台设置

使用 miniprogram-ci 前应在微信公众平台登录小程序,访问“开发-开发管理-开发设置”后下载“代码上传密钥”,并配置 IP 白名单。

开发者可选择打开 IP 白名单,打开后只有白名单中的 IP 才能调用相关接口。

代码上传密钥拥有预览、上传代码的权限,密钥不会明文存储在微信公众平台上,一旦遗失必须重置,请开发者妥善保管
小程序代码上传

脚本调用

npm install miniprogram-ci --save

项目对象

const ci = require('miniprogram-ci')
// 注意: new ci.Project 调用时,请确保项目代码已经是完整的,避免编译过程出现找不到文件的报错。
const project = new ci.Project({
  appid: 'wxsomeappid',
  type: 'miniProgram',
  projectPath: 'the/project/path',
  privateKeyPath: 'the/privatekey/path',
  ignores: ['node_modules/**/*'],
})
类型必填说明
appidstring合法的小程序/小游戏 appid
projectPathstring项目路径
privateKeyPathstring私钥的路径
typestring显示指明当前的项目类型, 默认为 miniProgram,有效值 miniProgram/miniProgramPlugin/miniGame/miniGamePlugin
ignoresstring[]指定需要排除的规则

编译参数

类型说明
es6boolean对应小程序开发者工具的 “es6 转 es5”
es7boolean对应小程序开发者工具的 “增强编译”
minifyJSboolean压缩 JS 代码
minifyWXMLboolean压缩 WXML 代码
minifyWXSSboolean压缩 WXSS 代码
minifyboolean压缩所有代码,对应小程序开发者工具的 “压缩代码”
codeProtectboolean对应小程序开发者工具的 “代码保护”
autoPrefixWXSSboolean对应小程序开发者工具的 “样式自动补全”

功能

上传

const ci = require('miniprogram-ci')
;(async () => {
  const project = new ci.Project({
    appid: 'wxsomeappid',
    type: 'miniProgram',
    projectPath: 'the/project/path',
    privateKeyPath: 'the/path/to/privatekey',
    ignores: ['node_modules/**/*'],
  })
  const uploadResult = await ci.upload({
    project,
    version: '1.1.1',
    desc: 'hello',
    setting: {
      es6: true,
    },
    onProgressUpdate: console.log,
  })
  console.log(uploadResult)
})()

参数

类型必填说明
projectIProject#项目对象
versionstring自定义版本号
descstring自定义备注
settingobject#编译设置
onProgressUpdatefunction进度更新监听函数
robotnumber指定使用哪一个 ci 机器人,可选值:1 ~ 30
threadsnumber指定本地编译过程中开启的线程数

返回

类型必填说明
subPackageInfoArray<{name:string, size:number}>小程序包信息, name 为 FULL 时表示整个小程序包, name 为 APP 时表示小程序主包,其他情况都表示分包
pluginInfoArray<{pluginProviderAppid:string, version: string, size:number}>小程序插件信息
devPluginIdstring插件开发模式下,上传版本的插件 id

预览

const ci = require('miniprogram-ci')
;(async () => {
  const project = new ci.Project({
    appid: 'wxsomeappid',
    type: 'miniProgram',
    projectPath: 'the/project/path',
    privateKeyPath: 'the/path/to/privatekey',
    ignores: ['node_modules/**/*'],
  })
  const previewResult = await ci.preview({
    project,
    desc: 'hello', // 此备注将显示在“小程序助手”开发版列表中
    setting: {
      es6: true,
    },
    qrcodeFormat: 'image',
    qrcodeOutputDest: '/path/to/qrcode/file/destination.jpg',
    onProgressUpdate: console.log,
    // pagePath: 'pages/index/index', // 预览页面
    // searchQuery: 'a=1&b=2',  // 预览参数 [注意!]这里的`&`字符在命令行中应写成转义字符`\&`
  })
  console.log(previewResult)
})()

参数

类型必填说明
projectIProject#项目对象
descstring自定义备注,将显示在“小程序助手”开发版列表中
settingobject#编译设置
onProgressUpdatefunction进度更新监听函数
robotnumber指定使用哪一个 ci 机器人,可选值:1 ~ 30
qrcodeFormatstring返回二维码文件的格式 “image” 或 “base64”, 默认值 “terminal” 供调试用
qrcodeOutputDeststring二维码文件保存路径
pagePathstring预览页面路径
searchQuerystring预览页面路径启动参数
scenenumber默认值 1011,具体含义见场景值列表

返回

类型必填说明
subPackageInfoArray<{name:string, size:number}>小程序包信息, name 为 __FULL__ 时表示整个小程序包, name 为 __APP__ 时表示小程序主包,其他情况都表示分包
pluginInfoArray<{pluginProviderAppid:string, version: string,小程序插件信息

获取本地编译后的代码包

当怀疑代码大小发生变化,不符合预期时,可以调用这个方法,检查一下最终上传到微信后台服务器时代码包里的文件内容。

const ci = require('miniprogram-ci')
const path = require('path')
;(async () => {
  const project = new ci.Project({
    appid: 'wxsomeappid',
    type: 'miniProgram',
    projectPath: 'the/project/path',
    privateKeyPath: 'the/path/to/privatekey',
    ignores: ['node_modules/**/*'],
  })

  // zip 文件保存位置
  const saveZipPath = path.join(__dirname, 'compiledResult.zip')

  const compiledResult = await ci.getCompiledResult({
    project,
    desc: 'hello',
    setting: {
      es6: true,
    },
    qrcodeFormat: 'image',
    qrcodeOutputDest: '/path/to/qrcode/file/destination.jpg',
    onProgressUpdate: console.log,
    // pagePath: 'pages/index/index', // 预览页面
    // searchQuery: 'a=1&b=2',  // 预览参数 [注意!]这里的`&`字符在命令行中应写成转义字符`\&`
    // scene: 1011, // 场景值
  }, saveZipPath)

  console.log(compiledResult) // compiledResult 为 Record<string, string | Buffer> 类型
})()
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ironprosper

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值