h5_for_production.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // 执行脚本 taro build --type h5 ,并且把环境变量 TARO_API_URL 传递过去
  2. const { execSync } = require('child_process');
  3. const path = require('path');
  4. const fs = require('fs');
  5. const TARO_API_URL = 'http://localhost:6001'; // 远程地址,这里写死,要做成部署后可配置
  6. const TARO_MQTT_URL ='ws://localhost:8083/mqtt';
  7. const rootDir = path.join(__dirname, '..'); // 项目根目录
  8. try {
  9. // 打印当前平台信息和CPU架构信息
  10. console.log(`当前操作系统平台: ${process.platform}`);
  11. console.log(`当前CPU架构: ${process.arch}`);
  12. // 检测平台和CPU架构,只有当操作系统是linux且CPU架构是arm64时才执行复制行为
  13. if (process.platform === 'linux' && process.arch === 'arm64') {
  14. console.log(`复制arm平台必须构建文件taro.linux-arm64-gnu.node到目标位置`);
  15. fs.copyFileSync(
  16. path.join(rootDir, '.build', 'taro.linux-arm64-gnu.node'),
  17. path.join(rootDir, 'node_modules', '@tarojs', 'binding', 'taro.linux-arm64-gnu.node')
  18. );
  19. console.log(`复制arm平台必须构建文件taro.linux-arm64-gnu.node到目标位置===完成`);
  20. }
  21. execSync(`npm run build:h5`, { cwd: rootDir, stdio: 'inherit', env: { ...process.env, TARO_API_URL ,TARO_MQTT_URL} }, (error, stdout, stderr) => {
  22. if (error) {
  23. console.error(`Error executing command: ${error.message}`);
  24. return;
  25. }
  26. if (stderr) {
  27. console.error(`Command stderr: ${stderr}`);
  28. return;
  29. }
  30. console.log(`Command stdout: ${stdout}`);
  31. });
  32. console.log('\n✅ h5 for production 全部构建完成');
  33. } catch (err) {
  34. console.error('Failed to execute :', err.message);
  35. process.exit(1);
  36. }