Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yue1123/vite-plugin-env-parse
Parse env string values to correct values, not all values are strings. And automatically generate ImportMetaEnv
https://github.com/yue1123/vite-plugin-env-parse
env env-parse environment-variables environments import-meta-env vite vite-plugin
Last synced: about 15 hours ago
JSON representation
Parse env string values to correct values, not all values are strings. And automatically generate ImportMetaEnv
- Host: GitHub
- URL: https://github.com/yue1123/vite-plugin-env-parse
- Owner: yue1123
- License: mit
- Created: 2023-04-30T15:49:46.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-07-13T10:25:13.000Z (5 months ago)
- Last Synced: 2024-12-16T01:05:02.542Z (8 days ago)
- Topics: env, env-parse, environment-variables, environments, import-meta-env, vite, vite-plugin
- Language: TypeScript
- Homepage:
- Size: 1.8 MB
- Stars: 73
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# vite-plugin-env-parse
![npm](https://img.shields.io/npm/v/vite-plugin-env-parse?style=flat-square)
![npm](https://img.shields.io/npm/dm/vite-plugin-env-parse?style=flat-square)
![GitHub](https://img.shields.io/github/license/yue1123/vite-plugin-env-parse?style=flat-square)[įŽäŊä¸æ](./README.zh.md)
Parse env string values to correct values, not all values are strings. And automatically generate ImportMetaEnv
![showcase](./screenshots/showcase.png)
## đĻ Install
```shell
npm i vite-plugin-env-parse -D# yarn
yarn add vite-plugin-env-parse -D# pnpm
pnpm add vite-plugin-env-parse -D
```## đĻ Usage
Add envParse plugin to vite.config.js / vite.config.ts and configure it:
```ts
// vite.config.js / vite.config.ts
import { envParse } from 'vite-plugin-env-parse'export default {
plugins: [envParse()]
}
```Add include configuration to the `tsconfig.json` file.
> Ignore this step if the project `tsconfig.json` `include` configuration already contains `*.d.ts`
>
```json
{
"include": ["env.d.ts"]
}
```[Example](./example)
## API
### Method
The `parseLoadedEnv` utility function can be used in the `vite config` file to parse the env environment variable values.
```ts
// vite.config.ts
import { parseLoadedEnv } from 'vite-plugin-env-parse'
import { defineConfig, loadEnv } from 'vite'export default defineConfig(({ mode }) => {
console.log('vite.config.ts', parseLoadedEnv(loadEnv(mode, './')))return {
plugins: [envParse({})]
}
})
```### EnvParseOptions
| Property | Type | Description |
| -------------- | ------------------- | ---------------------------------------------------------------------------------- |
| `exclude` | `string[]` | A list of environment variable keys to exclude from parsing. Optional. |
| `parseJson` | `boolean` | Whether to parse JSON strings into JSON objects. Defaults to `true`. Optional. |
| `customParser` | `CustomTransformer` | A custom transformer function for parsing environment variables. Optional. |
| `dtsPath` | `string` | The file path for generating the `.d.ts` file. Defaults to `'env.d.ts'`. Optional. |## License
[MIT licenses](https://opensource.org/licenses/MIT)