天天看点

miniprogram-ci官方文档miniprogram-ci简介使用前提功能

miniprogram-ci简介

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

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

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

使用前提

平台设置

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

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

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

miniprogram-ci官方文档miniprogram-ci简介使用前提功能

脚本调用

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/**/*'],
})
           
类型 必填 说明
appid string 合法的小程序/小游戏 appid
projectPath string 项目路径
privateKeyPath string 私钥的路径
type string 显示指明当前的项目类型, 默认为 miniProgram,有效值 miniProgram/miniProgramPlugin/miniGame/miniGamePlugin
ignores string[] 指定需要排除的规则

编译参数

类型 说明
es6 boolean 对应小程序开发者工具的 “es6 转 es5”
es7 boolean 对应小程序开发者工具的 “增强编译”
minifyJS boolean 压缩 JS 代码
minifyWXML boolean 压缩 WXML 代码
minifyWXSS boolean 压缩 WXSS 代码
minify boolean 压缩所有代码,对应小程序开发者工具的 “压缩代码”
codeProtect boolean 对应小程序开发者工具的 “代码保护”
autoPrefixWXSS boolean 对应小程序开发者工具的 “样式自动补全”

功能

上传

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)
})()
           

参数

类型 必填 说明
project IProject #项目对象
version string 自定义版本号
desc string 自定义备注
setting object #编译设置
onProgressUpdate function 进度更新监听函数
robot number 指定使用哪一个 ci 机器人,可选值:1 ~ 30
threads number 指定本地编译过程中开启的线程数

返回

类型 必填 说明
subPackageInfo Array<{name:string, size:number}> 小程序包信息, name 为 FULL 时表示整个小程序包, name 为 APP 时表示小程序主包,其他情况都表示分包
pluginInfo Array<{pluginProviderAppid:string, version: string, size:number}> 小程序插件信息
devPluginId string 插件开发模式下,上传版本的插件 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)
})()
           

参数

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

返回

类型 必填 说明
subPackageInfo Array<{name:string, size:number}> 小程序包信息, name 为 __FULL__ 时表示整个小程序包, name 为 __APP__ 时表示小程序主包,其他情况都表示分包
pluginInfo Array<{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> 类型
})()