Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(config): Export loadEnv() function #1069

Merged
merged 4 commits into from Nov 15, 2020
Merged

refactor(config): Export loadEnv() function #1069

merged 4 commits into from Nov 15, 2020

Conversation

cawa-93
Copy link
Contributor

@cawa-93 cawa-93 commented Nov 13, 2020

For some projects, such as electron-vue-next, there is a need to load environment variables from non-standard locations and to transfer these environments to other builders (such as rollup, etc.).

This PR will allow simply import loadEnv function from vite and use its algorithm instead of duplicating this algorithm in your own code.

Use cases

Configuring the building of two bundles using a single mechanism for loading environment variables.

import { loadEnv } from 'vite';


// The location of env files can be arbitrary
const dotenvLocation = path.resolve(__dirname, '../../')


// Load all environment variables with the prefix VITE_ to build a web interface
const envForRenderer = loadEnv('production', dotenvLocation)
buildRendererProcessWithEnv(envForRenderer)


// Load all environment variables with the prefix ELECTRON_ to build a backend
const envForMain = loadEnv('production', dotenvLocation, 'ELECTRON_')
buildMainProcessWithEnv(envForMain)

Another use case:
The development environment can be configured to automatically generate *.d.ts files based on .env* and describing environment variables and their types for each mode.

@cawa-93 cawa-93 changed the title Export loadEnv() refactor(config): Export loadEnv() function Nov 15, 2020
Copy link
Member

@aleclarson aleclarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loadEnv function mutates process.env. We should separate the mutation into its own function, so loadEnv can be called without side effects.

@cawa-93
Copy link
Contributor Author

cawa-93 commented Nov 15, 2020

I split loadEnv function into two:

  • loadEnv - reads files and returns an object with environment variables. Without side effects.
  • expandEnv - mutate process.env. For backward compatibility, I call the expandEnv wherever loadEnv is called. But, I assume that expandEnv is not needed everywhere, and its calls can be reduced or completely removed.

cawa-93 and others added 4 commits November 15, 2020 11:06
For some projects, such as electron-vue-next, there is a need to load environment variables from non-standard locations and to transfer these environments to other builders (such as rollup, etc.).
but with side effects disabled

and set `process.env.VITE_ENV` in ./src/node/cli.js
@aleclarson aleclarson merged commit dd03c62 into vitejs:master Nov 15, 2020
@aleclarson
Copy link
Member

I don't have publish rights, so you'll have to wait for @yyx990803. Hopefully we can get auto-release configured once v1.0.0 is released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants