1***@qq.com
1***@qq.com
  • 发布:2023-08-30 14:09
  • 更新:2023-08-30 15:46
  • 阅读:314

uniapp 有编译前运行脚本入口吗

分类:uni-app

uniapp 有编译前运行脚本入口吗?
想自定义发行时打包到不同的目录,但是uniapp 没这个功能,cli 命令行 又不支持自定义发行

2023-08-30 14:09 负责人:无 分享
已邀请:

最佳回复

BoredApe

BoredApe - 有问题就会有答案。

package.json:

  "scripts": {  
    "h5": "node build.js"  
  },  
// .........  

 "uni-app": {  
    "scripts": {  
      "H5-ABC": {  
        "title": "渠道1",  
        "browser": "chrome",  
        "env": {  
          "UNI_PLATFORM": "h5",  
          "TENANT": ABC"  
        },  
        "define": {  
          "H5-ABC": true  
        }  
      }  
}

build.js


const shell = require('shelljs');  
const os = require('os');  

// Check OS type  
const isWindows = os.platform() === 'win32';  
const isMac = os.platform() === 'darwin';  

// [channel] is the first argument  
const [channel] = process.argv.slice(2);  

// Base path  
let basePath;  
if (isWindows) {  
    basePath = 'C:\\soft\\HBuilderX\\plugins';  
} else if (isMac) {  
    basePath = '/Applications/HBuilderX-Alpha.app/Contents/HBuilderX/plugins';  
} else {  
    console.error('Unsupported OS');  
    process.exit(1);  
}  

// 设置 NODE_SKIP_PLATFORM_CHECK 环境变量,否则低版本的window server无法运行HBuilder X的高版本node  
shell.env['NODE_SKIP_PLATFORM_CHECK'] = '1';  

let command;  
if (isWindows) {  
    command = `cd ${basePath}\\uniapp-cli\\ && cross-env UNI_INPUT_DIR=%INIT_CWD%\\ UNI_OUTPUT_DIR=%INIT_CWD%\\unpackage\\dist\\build\\${channel} UNI_PLATFORM=h5 UNI_SCRIPT=${channel} NODE_ENV=production ${basePath}\\node\\node bin\\uniapp-cli.js`;  
} else if (isMac) {  
    command = `cd ${basePath}/uniapp-cli/ &&  UNI_INPUT_DIR=$INIT_CWD/ UNI_OUTPUT_DIR=$INIT_CWD/unpackage/dist/build/h5_${channel} UNI_PLATFORM=h5 UNI_SCRIPT=${channel} NODE_ENV=production ${basePath}/node/node bin/uniapp-cli.js`;  
}  

const { code, stdout, stderr } = shell.exec(command, { silent: false });  
console.log('{ 【stdout】 }:>>>>>>>>>>>> build.js:30', stdout);  
console.log('{ 【stderr】 }:>>>>>>>>>>>> build.js:31', stderr);  

if (code !== 0) {  
    console.error('Error executing the command');  
} else {  
    console.log('Command executed successfully');  
}

terminal:

npm run h5 -- H5-ABC
  • 1***@qq.com (作者)

    学到了,谢谢

    2023-08-30 18:08

要回复问题请先登录注册