修改项目 build 结构
This commit is contained in:
2
.env.dev_local
Normal file
2
.env.dev_local
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
APP_ENV=dev_local
|
||||||
|
TARO_APP_ID=wx815b533167eb7b53
|
||||||
79
config/env.config.ts
Normal file
79
config/env.config.ts
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
/**
|
||||||
|
* 统一环境配置(dev/sit/pr)
|
||||||
|
* 构建时通过 APP_ENV 选择,defineConstants 注入业务代码
|
||||||
|
* project.config.json 的 appid 由 scripts/sync-project-config.js 同步
|
||||||
|
*/
|
||||||
|
|
||||||
|
export type EnvType = "dev" | "dev_local" | "sit" | "pr";
|
||||||
|
|
||||||
|
export interface EnvConfig {
|
||||||
|
name: string;
|
||||||
|
apiBaseURL: string;
|
||||||
|
ossBaseURL: string;
|
||||||
|
appid: string;
|
||||||
|
timeout: number;
|
||||||
|
enableLog: boolean;
|
||||||
|
enableMock: boolean;
|
||||||
|
customerService: {
|
||||||
|
corpId: string;
|
||||||
|
serviceUrl: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseConfig = {
|
||||||
|
apiBaseURL: "https://tennis.bimwe.com",
|
||||||
|
ossBaseURL: "https://bimwe-oss.oss-cn-shanghai.aliyuncs.com",
|
||||||
|
appid: "wx815b533167eb7b53", // 测试号
|
||||||
|
timeout: 15000,
|
||||||
|
enableLog: true,
|
||||||
|
enableMock: false,
|
||||||
|
customerService: {
|
||||||
|
corpId: "ww51fc969e8b76af82",
|
||||||
|
serviceUrl: "https://work.weixin.qq.com/kfid/kfc64085b93243c5c91",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const envConfigs: Record<EnvType, EnvConfig> = {
|
||||||
|
// 本地开发:API 指向本地或测试服
|
||||||
|
dev: {
|
||||||
|
name: "DEV",
|
||||||
|
// apiBaseURL: "http://localhost:9098",
|
||||||
|
...baseConfig
|
||||||
|
},
|
||||||
|
// 本地联调:API 指向本机
|
||||||
|
dev_local: {
|
||||||
|
name: "DEV_LOCAL",
|
||||||
|
|
||||||
|
...Object.assign(baseConfig, {
|
||||||
|
apiBaseURL: "http://localhost:9098",
|
||||||
|
})
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
// SIT 测试环境
|
||||||
|
sit: {
|
||||||
|
name: "SIT",
|
||||||
|
...Object.assign(baseConfig, {
|
||||||
|
apiBaseURL: "https://tennis.bimwe.com",
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
// PR 生产环境
|
||||||
|
pr: {
|
||||||
|
name: "PR",
|
||||||
|
apiBaseURL: "https://youchang.qiongjingtiyu.com",
|
||||||
|
ossBaseURL: "https://youchang2026.oss-cn-shanghai.aliyuncs.com",
|
||||||
|
appid: "wx915ecf6c01bea4ec", // 生产小程序 appid,按实际填写
|
||||||
|
timeout: 10000,
|
||||||
|
enableLog: false,
|
||||||
|
enableMock: false,
|
||||||
|
customerService: {
|
||||||
|
corpId: "ww9a2d9a5d9410c664",
|
||||||
|
serviceUrl: "https://work.weixin.qq.com/kfid/kfcd355e162e0390684",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export function getEnvConfig(env: EnvType): EnvConfig {
|
||||||
|
return envConfigs[env];
|
||||||
|
}
|
||||||
@@ -2,11 +2,21 @@ import { defineConfig, type UserConfigExport } from '@tarojs/cli'
|
|||||||
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'
|
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'
|
||||||
import devConfig from './dev'
|
import devConfig from './dev'
|
||||||
import prodConfig from './prod'
|
import prodConfig from './prod'
|
||||||
// import vitePluginImp from 'vite-plugin-imp'
|
import { getEnvConfig, type EnvType } from './env.config'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
|
||||||
|
// 环境:dev(本地) | dev_local(联调) | sit(测试) | pr(生产)
|
||||||
|
const ENV_LIST: EnvType[] = ['dev', 'dev_local', 'sit', 'pr']
|
||||||
|
|
||||||
// https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
|
// https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
|
||||||
export default defineConfig<'webpack5'>(async (merge, { command, mode }) => {
|
export default defineConfig<'webpack5'>(async (merge, { command, mode }) => {
|
||||||
|
const appEnv = (
|
||||||
|
(ENV_LIST.includes(mode as EnvType) ? mode : process.env.APP_ENV) ||
|
||||||
|
(process.env.NODE_ENV === 'production' ? 'pr' : 'dev')
|
||||||
|
) as EnvType
|
||||||
|
|
||||||
|
const envConfig = getEnvConfig(appEnv)
|
||||||
|
|
||||||
const baseConfig: UserConfigExport<'webpack5'> = {
|
const baseConfig: UserConfigExport<'webpack5'> = {
|
||||||
projectName: 'playBallTogether',
|
projectName: 'playBallTogether',
|
||||||
date: '2025-8-9',
|
date: '2025-8-9',
|
||||||
@@ -22,6 +32,13 @@ export default defineConfig<'webpack5'>(async (merge, { command, mode }) => {
|
|||||||
outputRoot: 'dist',
|
outputRoot: 'dist',
|
||||||
plugins: ['@tarojs/plugin-html'],
|
plugins: ['@tarojs/plugin-html'],
|
||||||
defineConstants: {
|
defineConstants: {
|
||||||
|
'process.env.APP_ENV': JSON.stringify(appEnv),
|
||||||
|
'process.env.API_BASE_URL': JSON.stringify(envConfig.apiBaseURL),
|
||||||
|
'process.env.OSS_BASE_URL': JSON.stringify(envConfig.ossBaseURL),
|
||||||
|
'process.env.ENABLE_LOG': JSON.stringify(envConfig.enableLog),
|
||||||
|
'process.env.TIMEOUT': JSON.stringify(envConfig.timeout),
|
||||||
|
'process.env.CUSTOMER_CORP_ID': JSON.stringify(envConfig.customerService.corpId),
|
||||||
|
'process.env.CUSTOMER_SERVICE_URL': JSON.stringify(envConfig.customerService.serviceUrl),
|
||||||
},
|
},
|
||||||
alias: {
|
alias: {
|
||||||
'@': path.resolve(__dirname, '..', 'src'),
|
'@': path.resolve(__dirname, '..', 'src'),
|
||||||
@@ -76,6 +93,9 @@ export default defineConfig<'webpack5'>(async (merge, { command, mode }) => {
|
|||||||
},
|
},
|
||||||
// @ts-expect-error: Taro 类型定义缺少 mini.hot
|
// @ts-expect-error: Taro 类型定义缺少 mini.hot
|
||||||
hot: true,
|
hot: true,
|
||||||
|
projectConfig: {
|
||||||
|
appid: envConfig.appid,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
h5: {
|
h5: {
|
||||||
publicPath: '/',
|
publicPath: '/',
|
||||||
|
|||||||
35
package.json
35
package.json
@@ -10,32 +10,17 @@
|
|||||||
"framework": "React"
|
"framework": "React"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npm run build:weapp ",
|
"dev": "npm run dev:weapp",
|
||||||
"dev": "npm run dev:weapp ",
|
"dev:local": "npm run dev:weapp:dev_local",
|
||||||
"build:weapp": "taro build --type weapp --mode production",
|
"dev:weapp": "node scripts/sync-project-config.js dev && taro build --type weapp --mode dev --watch",
|
||||||
"build:swan": "taro build --type swan",
|
"dev:weapp:dev_local": "node scripts/sync-project-config.js dev_local && taro build --type weapp --mode dev_local --watch",
|
||||||
"build:alipay": "taro build --type alipay",
|
"build": "npm run build:weapp",
|
||||||
"build:tt": "taro build --type tt",
|
"build:weapp": "node scripts/sync-project-config.js pr && taro build --type weapp --mode pr",
|
||||||
"build:h5": "taro build --type h5",
|
"build:weapp:sit": "node scripts/sync-project-config.js sit && taro build --type weapp --mode sit",
|
||||||
"build:rn": "taro build --type rn",
|
"build:weapp:pr": "node scripts/sync-project-config.js pr && taro build --type weapp --mode pr",
|
||||||
"build:qq": "taro build --type qq",
|
"dev:h5": "npm run build:h5 -- --watch"
|
||||||
"build:jd": "taro build --type jd",
|
|
||||||
"build:quickapp": "taro build --type quickapp",
|
|
||||||
"dev:weapp": "npm run build:weapp -- --watch",
|
|
||||||
"dev:swan": "npm run build:swan -- --watch",
|
|
||||||
"dev:alipay": "npm run build:alipay -- --watch",
|
|
||||||
"dev:tt": "npm run build:tt -- --watch",
|
|
||||||
"dev:h5": "npm run build:h5 -- --watch",
|
|
||||||
"dev:rn": "npm run build:rn -- --watch",
|
|
||||||
"dev:qq": "npm run build:qq -- --watch",
|
|
||||||
"dev:jd": "npm run build:jd -- --watch",
|
|
||||||
"dev:quickapp": "npm run build:quickapp -- --watch"
|
|
||||||
},
|
},
|
||||||
"browserslist": [
|
"browserslist": ["last 3 versions", "Android >= 4.1", "ios >= 8"],
|
||||||
"last 3 versions",
|
|
||||||
"Android >= 4.1",
|
|
||||||
"ios >= 8"
|
|
||||||
],
|
|
||||||
"author": "",
|
"author": "",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/plugin-transform-runtime": "^7.28.3",
|
"@babel/plugin-transform-runtime": "^7.28.3",
|
||||||
|
|||||||
25
scripts/sync-project-config.js
Normal file
25
scripts/sync-project-config.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
require('ts-node/register/transpile-only');
|
||||||
|
|
||||||
|
const envArg = process.argv[2];
|
||||||
|
const appEnv = envArg || process.env.APP_ENV || (process.env.NODE_ENV === 'production' ? 'pr' : 'dev');
|
||||||
|
|
||||||
|
const envConfigPath = path.resolve(__dirname, '../config/env.config.ts');
|
||||||
|
const { envConfigs } = require(envConfigPath);
|
||||||
|
|
||||||
|
const config = envConfigs[appEnv];
|
||||||
|
if (!config) {
|
||||||
|
console.error(`[sync-project-config] Unknown APP_ENV: ${appEnv}`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const projectConfigPath = path.resolve(__dirname, '../project.config.json');
|
||||||
|
const projectConfigRaw = fs.readFileSync(projectConfigPath, 'utf-8');
|
||||||
|
const projectConfig = JSON.parse(projectConfigRaw);
|
||||||
|
|
||||||
|
projectConfig.appid = config.appid;
|
||||||
|
|
||||||
|
fs.writeFileSync(projectConfigPath, JSON.stringify(projectConfig, null, 2) + '\n', 'utf-8');
|
||||||
|
console.log(`[sync-project-config] project.config.json appid -> ${config.appid} (${appEnv})`);
|
||||||
@@ -1,135 +1,61 @@
|
|||||||
import Taro from '@tarojs/taro'
|
import Taro from "@tarojs/taro";
|
||||||
|
|
||||||
// 环境类型
|
/**
|
||||||
export type EnvType = 'development' | 'production'
|
* 环境配置:从 config/env.config.ts 经 defineConstants 注入
|
||||||
|
* 构建时由 config/index.ts 根据 APP_ENV 选择并注入
|
||||||
|
*/
|
||||||
|
export type EnvType = "dev" | "dev_local" | "sit" | "pr";
|
||||||
|
|
||||||
// 环境配置接口
|
|
||||||
export interface EnvConfig {
|
export interface EnvConfig {
|
||||||
name: string
|
name: string;
|
||||||
apiBaseURL: string
|
apiBaseURL: string;
|
||||||
ossBaseURL: string
|
ossBaseURL: string;
|
||||||
timeout: number
|
timeout: number;
|
||||||
enableLog: boolean
|
enableLog: boolean;
|
||||||
enableMock: boolean
|
enableMock: boolean;
|
||||||
// 客服配置
|
|
||||||
customerService: {
|
customerService: {
|
||||||
corpId: string
|
corpId: string;
|
||||||
serviceUrl: string
|
serviceUrl: string;
|
||||||
phoneNumber?: string
|
};
|
||||||
email?: string
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 各环境配置
|
// 从 defineConstants 注入的编译时常量读取
|
||||||
const envConfigs: Record<EnvType, EnvConfig> = {
|
const getInjectedConfig = (): EnvConfig => ({
|
||||||
|
name: process.env.APP_ENV || "dev",
|
||||||
|
apiBaseURL: process.env.API_BASE_URL || "",
|
||||||
// 开发环境
|
ossBaseURL: process.env.OSS_BASE_URL || "",
|
||||||
development: {
|
timeout: Number(process.env.TIMEOUT) || 10000,
|
||||||
name: '开发环境',
|
enableLog: process.env.ENABLE_LOG === "true",
|
||||||
apiBaseURL: 'https://tennis.bimwe.com',
|
enableMock: false,
|
||||||
ossBaseURL: 'https://bimwe.oss-cn-shanghai.aliyuncs.com',
|
customerService: {
|
||||||
//apiBaseURL: 'http://localhost:9098',
|
corpId: process.env.CUSTOMER_CORP_ID || "",
|
||||||
timeout: 15000,
|
serviceUrl: process.env.CUSTOMER_SERVICE_URL || "",
|
||||||
enableLog: true,
|
|
||||||
enableMock: false,
|
|
||||||
// 客服配置
|
|
||||||
customerService: {
|
|
||||||
corpId: 'ww51fc969e8b76af82', // 企业ID
|
|
||||||
serviceUrl: 'https://work.weixin.qq.com/kfid/kfc64085b93243c5c91',
|
|
||||||
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const getCurrentEnv = (): EnvType =>
|
||||||
|
(process.env.APP_ENV as EnvType) || "dev";
|
||||||
|
|
||||||
|
export const getCurrentConfig = (): EnvConfig => getInjectedConfig();
|
||||||
|
|
||||||
|
export const isDevelopment = (): boolean =>
|
||||||
|
getCurrentEnv() === "dev" || getCurrentEnv() === "dev_local" || getCurrentEnv() === "sit";
|
||||||
|
|
||||||
|
export const isProduction = (): boolean => getCurrentEnv() === "pr";
|
||||||
|
|
||||||
|
|
||||||
// 生产环境1
|
|
||||||
production: {
|
|
||||||
name: '生产环境1',
|
|
||||||
apiBaseURL: 'https://tennis.bimwe.com',
|
|
||||||
ossBaseURL: 'https://bimwe.oss-cn-shanghai.aliyuncs.com',
|
|
||||||
timeout: 10000,
|
|
||||||
enableLog: false,
|
|
||||||
enableMock: false,
|
|
||||||
// 客服配置
|
|
||||||
customerService: {
|
|
||||||
corpId: 'ww51fc969e8b76af82', // 企业ID
|
|
||||||
serviceUrl: 'https://work.weixin.qq.com/kfid/kfc64085b93243c5c91',
|
|
||||||
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
//生产环境2
|
|
||||||
// production: {
|
|
||||||
// name: '生产环境2',
|
|
||||||
// apiBaseURL: 'https://youchang.qiongjingtiyu.com',
|
|
||||||
// ossBaseURL: 'https://youchang2026.oss-cn-shanghai.aliyuncs.com',
|
|
||||||
// timeout: 10000,
|
|
||||||
// enableLog: false,
|
|
||||||
// enableMock: false,
|
|
||||||
// // 客服配置
|
|
||||||
// customerService: {
|
|
||||||
// corpId: 'ww9a2d9a5d9410c664', // 企业ID
|
|
||||||
// serviceUrl: 'https://work.weixin.qq.com/kfid/kfcd355e162e0390684',
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取当前环境
|
|
||||||
export const getCurrentEnv = (): EnvType => {
|
|
||||||
// 在小程序环境中,使用默认逻辑判断环境
|
|
||||||
// 可以根据实际需要配置不同的判断逻辑
|
|
||||||
|
|
||||||
// 可以根据实际部署情况添加更多判断逻辑
|
|
||||||
// 比如通过 Taro.getEnv() 获取当前平台环境
|
|
||||||
|
|
||||||
const isProd = process.env.NODE_ENV === 'production'
|
|
||||||
if (isProd) {
|
|
||||||
return 'production'
|
|
||||||
} else {
|
|
||||||
return 'development'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取当前环境配置
|
|
||||||
export const getCurrentConfig = (): EnvConfig => {
|
|
||||||
const env = getCurrentEnv()
|
|
||||||
return envConfigs[env]
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取指定环境配置
|
|
||||||
export const getEnvConfig = (env: EnvType): EnvConfig => {
|
|
||||||
return envConfigs[env]
|
|
||||||
}
|
|
||||||
|
|
||||||
// 是否为开发环境
|
|
||||||
export const isDevelopment = (): boolean => {
|
|
||||||
return getCurrentEnv() === 'development'
|
|
||||||
}
|
|
||||||
|
|
||||||
// 是否为生产环境
|
|
||||||
export const isProduction = (): boolean => {
|
|
||||||
return getCurrentEnv() === 'production'
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 环境配置调试信息
|
|
||||||
export const getEnvInfo = () => {
|
export const getEnvInfo = () => {
|
||||||
const config = getCurrentConfig()
|
const config = getCurrentConfig();
|
||||||
return {
|
return {
|
||||||
env: getCurrentEnv(),
|
env: getCurrentEnv(),
|
||||||
config,
|
config,
|
||||||
taroEnv: Taro.getEnv(),
|
taroEnv: (Taro as any).getEnv?.(),
|
||||||
platform: Taro.getEnv() === Taro.ENV_TYPE.WEAPP ? '微信小程序' :
|
platform:
|
||||||
Taro.getEnv() === Taro.ENV_TYPE.WEB ? 'Web' :
|
(Taro as any).getEnv?.() === (Taro as any).ENV_TYPE?.WEAPP
|
||||||
Taro.getEnv() === Taro.ENV_TYPE.RN ? 'React Native' : '未知'
|
? "微信小程序"
|
||||||
}
|
: (Taro as any).getEnv?.() === (Taro as any).ENV_TYPE?.WEB
|
||||||
}
|
? "Web"
|
||||||
|
: "未知",
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// 导出当前环境配置(方便直接使用)
|
export default getCurrentConfig();
|
||||||
export default getCurrentConfig()
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
.enable_notification_page {
|
.enable_notification_page {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
// min-height: 100vh;
|
height: 100%;
|
||||||
// background: radial-gradient(circle at 50% 0%, rgba(191, 255, 239, 1) 0%, rgba(255, 255, 255, 1) 37%);
|
background: radial-gradient(circle at 50% 0%, rgba(191, 255, 239, 1) 0%, rgba(255, 255, 255, 1) 37%);
|
||||||
|
box-sizing: border-box;
|
||||||
|
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
|
||||||
|
|||||||
2
types/global.d.ts
vendored
2
types/global.d.ts
vendored
@@ -17,6 +17,8 @@ declare namespace NodeJS {
|
|||||||
NODE_ENV: 'development' | 'production',
|
NODE_ENV: 'development' | 'production',
|
||||||
/** 当前构建的平台 */
|
/** 当前构建的平台 */
|
||||||
TARO_ENV: 'weapp' | 'swan' | 'alipay' | 'h5' | 'rn' | 'tt' | 'quickapp' | 'qq' | 'jd'
|
TARO_ENV: 'weapp' | 'swan' | 'alipay' | 'h5' | 'rn' | 'tt' | 'quickapp' | 'qq' | 'jd'
|
||||||
|
/** 应用环境标识 */
|
||||||
|
APP_ENV: 'dev' | 'dev_local' | 'sit' | 'pr'
|
||||||
/**
|
/**
|
||||||
* 当前构建的小程序 appid
|
* 当前构建的小程序 appid
|
||||||
* @description 若不同环境有不同的小程序,可通过在 env 文件中配置环境变量`TARO_APP_ID`来方便快速切换 appid, 而不必手动去修改 dist/project.config.json 文件
|
* @description 若不同环境有不同的小程序,可通过在 env 文件中配置环境变量`TARO_APP_ID`来方便快速切换 appid, 而不必手动去修改 dist/project.config.json 文件
|
||||||
|
|||||||
Reference in New Issue
Block a user