vite-plugin-environment
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -0,1 +1,10 @@ | ||
## [1.0.1](https://github.com/ElMassimo/vite-plugin-environment/compare/v1.0.0...v1.0.1) (2021-05-04) | ||
### Features | ||
* Add `prefix` and `defineOn` options. Support loading `all` variables. ([8d88a43](https://github.com/ElMassimo/vite-plugin-environment/commit/8d88a433ca695d7cd060d827f49c49c78651813d)) | ||
# 1.0.0 (2021-04-26) | ||
@@ -2,0 +11,0 @@ |
@@ -18,5 +18,17 @@ import { Plugin } from 'vite'; | ||
declare type EnvVarDefaults = Record<string, EnvVarDefault>; | ||
declare type EnvVars = string[] | EnvVarDefaults; | ||
declare type EnvVars = 'all' | string[] | EnvVarDefaults; | ||
interface EnvOptions { | ||
/** | ||
* Only variables that match this prefix will be made available. | ||
* @default '' | ||
* @example EnvironmentPlugin('all', { prefix: 'VUE_APP_' }) | ||
*/ | ||
prefix?: string; | ||
/** | ||
* You may also expose variables on `'import.meta.env'. | ||
* @default 'process.env' | ||
* @example EnvironmentPlugin(['NODE_ENV'], { defineOn: 'import.meta.env' }) | ||
*/ | ||
defineOn?: string; | ||
/** | ||
* Whether to load environment variables defined in `.env` files. | ||
@@ -30,8 +42,10 @@ * @default true | ||
* | ||
* @param {EnvVars} vars A list of variables you wish to expose, or an object | ||
* that maps variable names to defaut values to use when | ||
* a variable is not defined. | ||
* @param {EnvVars} vars Provide a list of variables you wish to expose, | ||
* or an object that maps variable names to defaut values | ||
* to use when a variable is not defined. | ||
* Use 'all' to expose all variables that match the prefix. | ||
* @param {EnvOptions} options | ||
*/ | ||
declare function EnvironmentPlugin(vars: EnvVars, { loadEnvFiles }?: EnvOptions): Plugin; | ||
declare function EnvironmentPlugin(vars: EnvVars, options?: EnvOptions): Plugin; | ||
export default EnvironmentPlugin; |
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/index.ts | ||
var _vite = require('vite'); | ||
function defineProcessEnvVars(env, keys, defaultValues) { | ||
function defineEnvVars(env, defineOn, keys, defaultValues) { | ||
return keys.reduce((vars, key) => { | ||
@@ -8,6 +8,11 @@ const value = env[key] === void 0 ? defaultValues[key] : env[key]; | ||
throwMissingKeyError(key); | ||
vars[`process.env.${key}`] = JSON.stringify(value); | ||
vars[`${defineOn}.${key}`] = JSON.stringify(value); | ||
return vars; | ||
}, {}); | ||
} | ||
function loadProcessEnv(prefix) { | ||
if (prefix === "") | ||
return process.env; | ||
return Object.fromEntries(Object.entries(process.env).filter(([key, _value]) => key.startsWith(prefix))); | ||
} | ||
function throwMissingKeyError(key) { | ||
@@ -19,11 +24,11 @@ throw new Error(`vite-plugin-environment: the \`${key}\` environment variable is undefined. | ||
} | ||
function EnvironmentPlugin(vars, {loadEnvFiles = true} = {}) { | ||
function EnvironmentPlugin(vars, options = {}) { | ||
const {prefix = "", defineOn = "process.env", loadEnvFiles = true} = options; | ||
return { | ||
name: "process-env-variables", | ||
enforce: "post", | ||
config({root = process.cwd()}, {mode}) { | ||
const env = loadEnvFiles ? _vite.loadEnv.call(void 0, mode, root, "") : process.env; | ||
const keys = Array.isArray(vars) ? vars : Object.keys(vars); | ||
const defaultValues = Array.isArray(vars) ? {} : vars; | ||
return {define: defineProcessEnvVars(env, keys, defaultValues)}; | ||
const env = loadEnvFiles ? _vite.loadEnv.call(void 0, mode, root, prefix) : loadProcessEnv(prefix); | ||
const keys = vars === "all" ? Object.keys(env) : Array.isArray(vars) ? vars : Object.keys(vars); | ||
const defaultValues = vars === "all" || Array.isArray(vars) ? {} : vars; | ||
return {define: defineEnvVars(env, defineOn, keys, defaultValues)}; | ||
} | ||
@@ -30,0 +35,0 @@ }; |
{ | ||
"name": "vite-plugin-environment", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Easily expose environment variables in Vite.js", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -61,18 +61,2 @@ <h2 align='center'><samp>vite-plugin-environment</samp></h2> | ||
This is equivalent to [manually configuring][define]: | ||
```js | ||
import { defineConfig } from 'vite' | ||
export default defineConfig({ | ||
define: { | ||
'process.env.API_KEY': JSON.stringify(process.env.API_KEY), | ||
'process.env.DEBUG': JSON.stringify(process.env.DEBUG), | ||
} | ||
}) | ||
``` | ||
except it will also use any variables provided by your `.env` files, and will | ||
__fail__ if any of the specified variables is _not defined_. | ||
### Usage with default values | ||
@@ -102,4 +86,41 @@ | ||
### Using only `process.env` | ||
## Configuration ⚙️ | ||
Have in mind that you can add the plugin several times—passing different options to load different sets of variables. | ||
### Loading prefixed variables | ||
In some cases, it's useful to load all environment variables with a certain prefix. | ||
You can achieve that by passing `'all'` and providing the <kbd>prefix</kbd> option. | ||
```js | ||
EnvironmentPlugin('all', { prefix: 'VUE_APP_' }), | ||
EnvironmentPlugin('all', { prefix: 'REACT_APP_' }), | ||
``` | ||
and then use it as usual: | ||
```js | ||
process.env.VUE_APP_NOT_SECRET_CODE | ||
``` | ||
### Exposing variables differently | ||
When porting apps to Vite or using SSR it can be useful to expose variables in `process.env`, which is the default. | ||
In other cases, you may use the <kbd>defineOn</kbd> option to expose them in a different object, such as `import.meta.env`. | ||
```js | ||
EnvironmentPlugin({ APP_VERSION: 'local' }, { defineOn: 'import.meta.env' }), | ||
``` | ||
and then use it as: | ||
```js | ||
const version = import.meta.env.APP_VERSION | ||
``` | ||
### Ignoring `.env` files | ||
By default the plugin will load `.env` files using the same [strategy][meta env] as Vite.js. | ||
@@ -113,4 +134,33 @@ | ||
## Inside the box 📦 | ||
The first example in this README is equivalent to [manually configuring][define]: | ||
```js | ||
import { defineConfig } from 'vite' | ||
export default defineConfig({ | ||
define: { | ||
'process.env.API_KEY': JSON.stringify(process.env.API_KEY), | ||
'process.env.DEBUG': JSON.stringify(process.env.DEBUG), | ||
} | ||
}) | ||
``` | ||
except it will also use any variables provided by your `.env` files, and will | ||
__fail__ if any of the specified variables is _not defined_. | ||
## Acknowledgements | ||
I created this library only because I wanted something that: | ||
- Reused Vite's `loadEnv` functionality, making the library _very_ light (no dependencies). | ||
- Allowed to provide a subset of variables to expose, and their defaults. | ||
The following libraries might be helpful depending on your use case: | ||
- [vite-plugin-env-compatible](vite-plugin-env-compatible): Convenient if you are porting a Vue CLI or create-react-app. | ||
## License | ||
This library is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
Sorry, the diff of this file is not supported yet
13189
117
164
4