Bladeren bron

fix: 修复 H5 构建脚本环境变量优先级问题

- 修改环境变量读取逻辑,优先使用 GitHub Actions 传入的环境变量
- 支持部署后可配置 TARO_API_URL 和 TARO_MQTT_URL
- 修复原先硬编码值覆盖环境变量的问题
- 保留本地开发时的默认值作为回退

改动文件:
- .build/h5_for_production.js
dengdx 1 dag geleden
bovenliggende
commit
cb27ccb1f2
1 gewijzigde bestanden met toevoegingen van 5 en 3 verwijderingen
  1. 5 3
      .build/h5_for_production.js

+ 5 - 3
.build/h5_for_production.js

@@ -4,8 +4,9 @@ const { execSync } = require('child_process');
 const path = require('path');
 const fs = require('fs');
 
-const TARO_API_URL = 'http://localhost:6001'; // 远程地址,这里写死,要做成部署后可配置
-const TARO_MQTT_URL ='ws://localhost:8083/mqtt';
+// 优先使用环境变量,如果不存在则使用默认值(支持部署后可配置)
+const TARO_API_URL = process.env.TARO_API_URL || 'http://localhost:6001';
+const TARO_MQTT_URL = process.env.TARO_MQTT_URL || 'ws://localhost:8083/mqtt';
 const rootDir = path.join(__dirname, '..');          // 项目根目录
 
 try {
@@ -23,7 +24,8 @@ try {
         console.log(`复制arm平台必须构建文件taro.linux-arm64-gnu.node到目标位置===完成`);
     }
     
-    execSync(`npm run build:h5`, { cwd: rootDir, stdio: 'inherit', env: { ...process.env, TARO_API_URL ,TARO_MQTT_URL} }, (error, stdout, stderr) => {
+    // 将读取的环境变量传递给子进程
+    execSync(`npm run build:h5`, { cwd: rootDir, stdio: 'inherit', env: { ...process.env, TARO_API_URL, TARO_MQTT_URL } }, (error, stdout, stderr) => {
     if (error) {
         console.error(`Error executing command: ${error.message}`);
         return;