Skip to content

Commit

Permalink
feat: patch dotenv file parse
Browse files Browse the repository at this point in the history
  • Loading branch information
PengBoUESTC committed Jul 5, 2023
1 parent 09ac60c commit a470673
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/taro-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default class CLI {
}
const mode = args.mode || process.env.NODE_ENV
// 这里解析 dotenv 以便于 config 解析时能获取 dotenv 配置信息
const expandEnv = dotenvParse(appPath, args.envPrefix, mode)
const expandEnv = dotenvParse(appPath, args as unknown as Parameters<typeof dotenvParse>[1])

const disableGlobalConfig = !!(args['disable-global-config'] || DISABLE_GLOBAL_CONFIG_COMMANDS.includes(command))

Expand Down
24 changes: 22 additions & 2 deletions packages/taro-cli/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,37 @@ export const formatPrefix = (prefixs: string | string[] = ['TARO_APP_']): string
const prefixsArr: string[] = (Array.isArray(prefixs) ? prefixs : prefixs.split(',')).map(prefix => prefix.trim()).filter(prefix => !!prefix)
return prefixsArr
}
export const dotenvParse = (root: string, prefixs: string | string[] = ['TARO_APP_'], mode?: string): Record<string, string> => {
const prefixsArr: string[] = formatPrefix(prefixs)
export const dotenvParse = (root: string, options: {
prefixs: string | string[]
mode?: string
type?: string
}): Record<string, string> => {
const { prefixs = ['TARO_APP_'], mode, type } = options

const prefixsArr: string[] = formatPrefix(prefixs)
// 默认 文件存在即读取 优先级最低
// 不区分打包平台 不区分打包 mode
const envFiles = new Set([
/** default file */ `.env`,
/** local file */ `.env.local`,
])
// 根据打包 平台 读取配置 文件
// 可配置 特定平台 但是 不区分 mode 的配置信息
if(type) {
envFiles.add(/** type file */ `.env.${type}`)
envFiles.add(/** type local file */ `.env.${type}.local`)
}

if(mode) {
// 根据 打包 mode 读取配置文件
// 可配置 特定 mode 但是不区分 平台 的配置信息
envFiles.add(/** mode file */ `.env.${mode}`)
envFiles.add(/** mode local file */ `.env.${mode}.local`)
if(type) {
// 最高优先级 特定平台 特定 mode 的配置信息
envFiles.add(/** mode type file */ `.env.${mode}.${type}`)
envFiles.add(/** mode type local file */ `.env.${mode}.${type}.local`)
}
}

let parseTemp = {}
Expand Down

0 comments on commit a470673

Please sign in to comment.