Browse Source

feat: 拆分H5构建命令为浏览器和Electron两种环境

- 将 h5_for_production 命令拆分为 h5:browser 和 h5:electron 两个命令
- 添加 cross-env 依赖以支持跨平台环境变量传递
- 浏览器环境使用空字符串API URL和相对路径MQTT URL(适配Nginx代理)
- Electron环境使用localhost本地服务地址
- 更新 README.md 添加详细的使用说明和环境区分文档
- 与CI工作流逻辑保持一致

改动文件:
- package.json
- package-lock.json
- README.md
dengdx 2 days ago
parent
commit
bfde6825cb
3 changed files with 59 additions and 6 deletions
  1. 28 3
      README.md
  2. 28 2
      package-lock.json
  3. 3 1
      package.json

+ 28 - 3
README.md

@@ -24,10 +24,35 @@
 
 - npm run pack:linux
 
-## 构建H5 production
+## 构建 H5 Production
 
-- npm i
-- npm run h5_for_production
+根据部署环境选择对应的构建命令:
+
+### 浏览器环境(Web 服务器部署)
+
+适用于 Nginx 反向代理部署场景:
+
+```bash
+npm i
+npm run h5:browser
+```
+
+- API URL: 空字符串(由 Nginx 代理处理)
+- MQTT URL: `/mqtt`(相对路径)
+
+### Electron 环境(桌面应用)
+
+适用于 Electron 桌面应用场景:
+
+```bash
+npm i
+npm run h5:electron
+```
+
+- API URL: `http://localhost:6001`
+- MQTT URL: `ws://localhost:8083/mqtt`
+
+> **提示**:两个命令都支持通过环境变量 `TARO_API_URL` 和 `TARO_MQTT_URL` 覆盖默认配置
 
 ## 构建android平台的应用包[Hybrid]
 

+ 28 - 2
package-lock.json

@@ -1,12 +1,12 @@
 {
   "name": "zsis",
-  "version": "1.2.3",
+  "version": "1.2.4",
   "lockfileVersion": 3,
   "requires": true,
   "packages": {
     "": {
       "name": "zsis",
-      "version": "1.2.3",
+      "version": "1.2.4",
       "dependencies": {
         "@babel/runtime": "^7.24.4",
         "@cornerstonejs/core": "^3.28.0",
@@ -70,6 +70,7 @@
         "babel-preset-taro": "4.1.1",
         "browserify-fs": "^1.0.0",
         "cordova": "^12.0.0",
+        "cross-env": "^10.1.0",
         "cypress": "^14.5.4",
         "electron-builder": "^26.0.12",
         "eslint": "^9.28.0",
@@ -2851,6 +2852,13 @@
       "resolved": "https://registry.npmjs.org/@emotion/unitless/-/unitless-0.7.5.tgz",
       "integrity": "sha512-OWORNpfjMsSSUBVrRBVGECkhWcULOAJz9ZW8uK9qgxD+87M7jHRcvh/A96XXNhXTLmKcoYSQtBEX7lHMO7YRwg=="
     },
+    "node_modules/@epic-web/invariant": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/@epic-web/invariant/-/invariant-1.0.0.tgz",
+      "integrity": "sha512-lrTPqgvfFQtR/eY/qkIzp98OGdNJu0m5ji3q/nJI8v3SXkRKEnWiOxMmbvcSoAIzv/cGiuvRy57k4suKQSAdwA==",
+      "dev": true,
+      "license": "MIT"
+    },
     "node_modules/@esbuild/aix-ppc64": {
       "version": "0.21.5",
       "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.21.5.tgz",
@@ -11569,6 +11577,24 @@
       "optional": true,
       "peer": true
     },
+    "node_modules/cross-env": {
+      "version": "10.1.0",
+      "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-10.1.0.tgz",
+      "integrity": "sha512-GsYosgnACZTADcmEyJctkJIoqAhHjttw7RsFrVoJNXbsWWqaq6Ym+7kZjq6mS45O0jij6vtiReppKQEtqWy6Dw==",
+      "dev": true,
+      "license": "MIT",
+      "dependencies": {
+        "@epic-web/invariant": "^1.0.0",
+        "cross-spawn": "^7.0.6"
+      },
+      "bin": {
+        "cross-env": "dist/bin/cross-env.js",
+        "cross-env-shell": "dist/bin/cross-env-shell.js"
+      },
+      "engines": {
+        "node": ">=20"
+      }
+    },
     "node_modules/cross-spawn": {
       "version": "7.0.6",
       "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",

+ 3 - 1
package.json

@@ -42,7 +42,8 @@
     "pack:linux": "node ./.build/h5_for_electron.build.linux.arm.js",
     "pack:win": "node ./.build/h5_for_electron.build.win.x64.js",
     "pkg": "node ./.build/h5_for_webserver.build.js",
-    "h5_for_production": "node ./.build/h5_for_production.js",
+    "h5:browser": "cross-env TARO_API_URL= TARO_MQTT_URL=/mqtt node ./.build/h5_for_production.js",
+    "h5:electron": "cross-env TARO_API_URL=http://localhost:6001 TARO_MQTT_URL=ws://localhost:8083/mqtt node ./.build/h5_for_production.js",
     "clean:cache": "node ./.build/clean-cache.js"
   },
   "browserslist": [
@@ -113,6 +114,7 @@
     "babel-preset-taro": "4.1.1",
     "browserify-fs": "^1.0.0",
     "cordova": "^12.0.0",
+    "cross-env": "^10.1.0",
     "cypress": "^14.5.4",
     "electron-builder": "^26.0.12",
     "eslint": "^9.28.0",