-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
taro rn 支持 config/index.ts 文件 (#14201)
- Loading branch information
1 parent
65f9423
commit 14bf92c
Showing
27 changed files
with
281 additions
and
435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { parse } from 'dotenv' | ||
import { expand } from 'dotenv-expand' | ||
import * as fs from 'fs-extra' | ||
import * as path from 'path' | ||
|
||
import type { IProjectConfig } from '@tarojs/taro/types/compile' | ||
|
||
// 支持 --env-prefix=TARO_APP_,aa 类型参数 | ||
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) | ||
|
||
const envFiles = new Set([ | ||
/** default file */ `.env`, | ||
/** local file */ `.env.local`, | ||
]) | ||
|
||
if(mode) { | ||
envFiles.add(/** mode file */ `.env.${mode}`) | ||
envFiles.add(/** mode local file */ `.env.${mode}.local`) | ||
} | ||
|
||
let parseTemp = {} | ||
const load = envPath => { | ||
// file doesn'et exist | ||
if(!fs.existsSync(envPath)) return | ||
const env = parse(fs.readFileSync(envPath)) | ||
parseTemp = { | ||
...parseTemp, | ||
...env | ||
} | ||
} | ||
|
||
envFiles.forEach(envPath => { | ||
load(path.resolve(root, envPath)) | ||
}) | ||
|
||
const parsed = {} | ||
Object.entries(parseTemp).forEach(([key, value]) => { | ||
if(prefixsArr.some(prefix => key.startsWith(prefix)) || ['TARO_APP_ID'].includes(key)) { | ||
parsed[key] = value | ||
} | ||
}) | ||
expand({ parsed }) | ||
return parsed | ||
} | ||
|
||
// 扩展 env | ||
export const patchEnv = (config: IProjectConfig, expandEnv: Record<string, string>) => { | ||
const expandEnvStringify = {} | ||
for (const key in expandEnv) { | ||
expandEnvStringify[key] = JSON.stringify(expandEnv[key]) | ||
} | ||
return { | ||
...config.env, | ||
...expandEnvStringify | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.