with-dotenv example (#2399)
This commit is contained in:
parent
247dd98cf3
commit
c9bc471d3a
6 changed files with 66 additions and 0 deletions
13
examples/with-dotenv/.babelrc
Normal file
13
examples/with-dotenv/.babelrc
Normal file
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"presets": [
|
||||
"next/babel",
|
||||
],
|
||||
"env": {
|
||||
"development": {
|
||||
"plugins": ["inline-dotenv"]
|
||||
},
|
||||
"production": {
|
||||
"plugins": ["transform-inline-environment-variables"]
|
||||
}
|
||||
}
|
||||
}
|
1
examples/with-dotenv/.env
Normal file
1
examples/with-dotenv/.env
Normal file
|
@ -0,0 +1 @@
|
|||
TEST=it works!
|
1
examples/with-dotenv/.env.production
Normal file
1
examples/with-dotenv/.env.production
Normal file
|
@ -0,0 +1 @@
|
|||
TEST=it works!
|
31
examples/with-dotenv/README.md
Normal file
31
examples/with-dotenv/README.md
Normal file
|
@ -0,0 +1,31 @@
|
|||
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-dotenv)
|
||||
|
||||
# With Dotenv example
|
||||
|
||||
## How to use
|
||||
|
||||
Download the example [or clone the repo](https://github.com/zeit/next.js):
|
||||
|
||||
```bash
|
||||
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-dotenv
|
||||
cd with-dotenv
|
||||
```
|
||||
|
||||
Install it and run:
|
||||
|
||||
```bash
|
||||
npm install
|
||||
npm run dev
|
||||
```
|
||||
|
||||
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
|
||||
|
||||
```bash
|
||||
now
|
||||
```
|
||||
|
||||
## The idea behind the example
|
||||
|
||||
This example shows the most basic idea of babel replacement from multiple environment. We have 1 env variable: `TEST` which will be replaced in development env and in production env with different babel plugin. In local development, babel reads .env file and replace process.env.* in your nextjs files. In production env (such as heroku), babel reads the ENV and replace process.env.* in your nextjs files. Thus no more needed to commit your secrets anymore.
|
||||
|
||||
Of course, please put .env* in your .gitignore when using this example locally.
|
17
examples/with-dotenv/package.json
Normal file
17
examples/with-dotenv/package.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"name": "with-dotenv",
|
||||
"version": "1.0.0",
|
||||
"scripts": {
|
||||
"dev": "next",
|
||||
"build": "next build",
|
||||
"start": "next start"
|
||||
},
|
||||
"dependencies": {
|
||||
"next": "latest",
|
||||
"react": "^15.4.2",
|
||||
"react-dom": "^15.4.2",
|
||||
"babel-plugin-inline-dotenv": "^1.1.1",
|
||||
"babel-plugin-transform-inline-environment-variables": "^0.1.1"
|
||||
},
|
||||
"license": "ISC"
|
||||
}
|
3
examples/with-dotenv/pages/index.js
Normal file
3
examples/with-dotenv/pages/index.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
export default () => (
|
||||
<div>{ process.env.TEST }</div>
|
||||
)
|
Loading…
Reference in a new issue