使用命令行打包uniapp项目中web端

157 阅读1分钟

1、生成package.json文件

npm init -y

2、安装npm-run-all

npm i npm-run-all -D

3、测试Mac端打包命令

MacOSX命令行工具所在位置

  • 正式版
/Applications/HBuilderX.app/Contents/MacOS/cli
  • Alpha版
/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli
#打包
/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli publish --platform h5 --project 项目名

项目打包后会在根目录中生成 unpackage/dist/build/h5

3.1、实现打包后的项目重命名为指定名字

  • 根目录中新建reaname.js
//reaname.js
const path = require('path')
const fs = require('fs');
const version = require('./package.json').version
const pathName = path.join(__dirname, './unpackage/dist/build');
const files = fs.readdirSync(pathName)
if(pathName && files.indexOf('h5')){
	files.forEach((file,index) => {
		if(file.includes('h5')){
			fs.rename(`${pathName}/h5`,  `${pathName}/${version}`, (err) => {
				if(!err) {
					console.log('已重命名!')
				}
			})
		}
	})
	return
}
console.log('重命名失败,查找h5打包文件失败')
  • 修改package.json的版本号 image.png
  • 配置打包命令 在package.json中的scripts添加打包命令
{
    "scripts":{
         "build-mac-defaultName:h5": "/Applications/HBuilderX-Alpha.app/Contents/MacOS/cli publish --platform h5 --project shenqi-association-miniapp",
         "build-mac:h5":"npm-run-all -p build-mac-defaultName:h5 -s rename",
    }
}

4、测试Window端打包命令