Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .github/pull_request_template.md

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ jobs:
cache: yarn
- run: yarn --frozen-lockfile
- run: yarn audit --groups dependencies
- run: yarn xo
- run: yarn standard
- run: yarn test --no-cache
30 changes: 2 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
> Load environment variables using `import` statements.

[![npm version](https://badgen.net/npm/v/react-native-dotenv)](https://www.npmjs.com/package/react-native-dotenv)
[![dependencies Status](https://img.shields.io/librariesio/release/npm/react-native-dotenv)](https://img.shields.io/librariesio/release/npm/react-native-dotenv)
[![codecov](https://badgen.net/codecov/c/github/dotenvx/react-native-dotenv)](https://codecov.io/gh/dotenvx/react-native-dotenv)
[![XO code style](https://badgen.net/badge/code%20style/XO/cyan)](https://github.com/xojs/xo)
[![npm downloads](https://img.shields.io/npm/dt/react-native-dotenv.svg?style=flat-square)](https://www.npmjs.com/package/react-native-dotenv)
[![works with dotenv-vault](https://badge.dotenv.org/works-with.svg?r=1)](https://www.dotenv.org/r/github.com/dotenv-org/dotenv-vault?r=1)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![npm downloads](https://img.shields.io/npm/dw/react-native-dotenv.svg?style=flat-square)](https://www.npmjs.com/package/react-native-dotenv)

## Installation

Expand Down Expand Up @@ -115,10 +112,6 @@ fetch(`${API_URL}/users`, {
})
```

## [DEPRECATED] White and black lists

Moving forward to a more inclusive language, terms like `white` and `black` are being moved away. Future versions will just use `allowlist` and `blocklist` while `whitelist`/`blacklist` are still supported.

## Allow and Block lists

It is possible to limit the scope of env variables that will be imported by specifying a `allowlist` and/or a `blocklist` as an array of strings.
Expand Down Expand Up @@ -367,22 +360,3 @@ Or you can override the default `cacheIdentifier` to include some of your enviro
The tests that use `require('@env')` are also not passing.

For nextjs, you _must_ set `moduleName` to `react-native-dotenv`.

## Credits

* Based on [David Chang](https://github.com/zetachang)’s works on [babel-plugin-dotenv](https://github.com/zetachang/react-native-dotenv/tree/master/babel-plugin-dotenv).
* Also based on [Bertrand Marron](https://github.com/tusbar)'s works on [babel-plugin-dotenv-import](https://github.com/tusbar/babel-plugin-dotenv-import).

If you'd like to become an active contributor, please send us a message.

## Miscellaneous

```
╚⊙ ⊙╝
╚═(███)═╝
╚═(███)═╝
╚═(███)═╝
╚═(███)═╝
╚═(███)═╝
╚═(███)═╝
```
46 changes: 23 additions & 23 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {transformFileSync} = require('@babel/core')
const { transformFileSync } = require('@babel/core')

const FIXTURES = '__tests__/__fixtures__/'

Expand All @@ -10,7 +10,7 @@ describe('react-native-dotenv', () => {
const OLD_ENV = process.env
afterEach(() => {
jest.resetModules()
process.env = {...OLD_ENV}
process.env = { ...OLD_ENV }
})

it('should throw if the variable does not exist', () => {
Expand All @@ -26,21 +26,21 @@ describe('react-native-dotenv', () => {
})

it('should load environment variables from .env', () => {
const {code} = transformFileSync(FIXTURES + 'default/source.js')
const { code } = transformFileSync(FIXTURES + 'default/source.js')
expect(code).toBe('console.log("abc123");\nconsole.log("username");')
})

it('should print the environment if setting to verbose', () => {
console.log = jest.fn()
const {code} = transformFileSync(FIXTURES + 'verbose/source.js')
const { code } = transformFileSync(FIXTURES + 'verbose/source.js')
expect(code).toBe('console.log("abc123");\nconsole.log("username");')
expect(console.log.mock.calls[0][1]).toBe('test')
})

it('should allow importing variables already defined in the environment', () => {
process.env.FROM_ENV = 'hello'

const {code} = transformFileSync(FIXTURES + 'from-env/source.js')
const { code } = transformFileSync(FIXTURES + 'from-env/source.js')
expect(code).toBe('console.log("hello");')
})

Expand All @@ -56,61 +56,61 @@ describe('react-native-dotenv', () => {
it('should prioritize environment variables over variables defined in .env even when safe', () => {
process.env.API_KEY = 'i win again'

const {code} = transformFileSync(FIXTURES + 'default-safe/source.js')
const { code } = transformFileSync(FIXTURES + 'default-safe/source.js')
expect(code).toBe('console.log("i win again");\nconsole.log("username");')
})

it('should load custom env file', () => {
const {code} = transformFileSync(FIXTURES + 'filename/source.js')
const { code } = transformFileSync(FIXTURES + 'filename/source.js')
expect(code).toBe('console.log("abc123456");\nconsole.log("username123456");')
})

it('should load multiple env files', () => {
const {code} = transformFileSync(FIXTURES + 'multi-env/source.js')
const { code } = transformFileSync(FIXTURES + 'multi-env/source.js')
expect(code).toBe('console.log("abc123456");\nconsole.log("username123456");')
})

it('should load local env files', () => {
const {code} = transformFileSync(FIXTURES + 'local-env/source.js')
const { code } = transformFileSync(FIXTURES + 'local-env/source.js')
expect(code).toBe('console.log("username123456");\nconsole.log("local-key");')
})

it('should support `as alias` import syntax', () => {
const {code} = transformFileSync(FIXTURES + 'as-alias/source.js')
const { code } = transformFileSync(FIXTURES + 'as-alias/source.js')
expect(code).toBe('const a = "abc123";\nconst b = "username";')
})

it('should allow specifying a custom module name', () => {
const {code} = transformFileSync(FIXTURES + 'custom-module/source.js')
const { code } = transformFileSync(FIXTURES + 'custom-module/source.js')
expect(code).toBe('console.log("abc123");\nconsole.log("username");')
})

it('should allow specifying process.env', () => {
const {code} = transformFileSync(FIXTURES + 'process-env/source.js')
const { code } = transformFileSync(FIXTURES + 'process-env/source.js')
expect(code).toBe('console.log("abc123");\nconsole.log("username");\nconsole.log("test");')
})

it('should not change undefined process.env variables', () => {
const {code} = transformFileSync(FIXTURES + 'process-env-undefined/source.js')
const { code } = transformFileSync(FIXTURES + 'process-env-undefined/source.js')
expect(code).toBe('console.log(process.env.UNDEFINED_VAR);')
})

it('should propagate process.env variables from node process', () => {
const customEnv = 'my-custom-env'
const backupNodeEnv = process.env.NODE_ENV
process.env.NODE_ENV = customEnv
const {code} = transformFileSync(FIXTURES + 'process-env-propagate/source.js')
const { code } = transformFileSync(FIXTURES + 'process-env-propagate/source.js')
expect(code).toBe(`console.log("${customEnv}");`)
process.env.NODE_ENV = backupNodeEnv
})

it('should allow specifying the package module name', () => {
const {code} = transformFileSync(FIXTURES + 'module-name/source.js')
const { code } = transformFileSync(FIXTURES + 'module-name/source.js')
expect(code).toBe('console.log("abc123");\nconsole.log("username");')
})

it('should leave other imports untouched', () => {
const {code} = transformFileSync(FIXTURES + 'unused/source.js')
const { code } = transformFileSync(FIXTURES + 'unused/source.js')
expect(code).toBe('import { join } from \'node:path\';\nconsole.log(join);')
})

Expand All @@ -137,32 +137,32 @@ describe('react-native-dotenv', () => {
})

it('should load environment variables from .env in safe mode', () => {
const {code} = transformFileSync(FIXTURES + 'safe-success/source.js')
const { code } = transformFileSync(FIXTURES + 'safe-success/source.js')
expect(code).toBe('console.log("1");\nconsole.log("test");')
})

it('should import undefined variables', () => {
const {code} = transformFileSync(FIXTURES + 'undefined/source.js')
const { code } = transformFileSync(FIXTURES + 'undefined/source.js')
expect(code).toBe('console.log(undefined);')
})

it('should not throw if .env exists in safe mode', () => {
const {code} = transformFileSync(FIXTURES + 'safe-no-dotenv/source.js')
const { code } = transformFileSync(FIXTURES + 'safe-no-dotenv/source.js')
expect(code).toBe('console.log(undefined);')
})

it('should load APP_ENV specific env file', () => {
process.env.APP_ENV = 'cli'

const {code} = transformFileSync(FIXTURES + 'app-env/source.js')
const { code } = transformFileSync(FIXTURES + 'app-env/source.js')
expect(code).toBe('console.log("abc123456");\nconsole.log("username123456");')
})

it('should fail to load APP_ENV development', () => {
console.error = jest.fn()
process.env.APP_ENV = 'development'

const {code} = transformFileSync(FIXTURES + 'app-env-development/source.js')
const { code } = transformFileSync(FIXTURES + 'app-env-development/source.js')
expect(code).toBe('console.log("never");\nconsole.log("this-should-not-appear");')
expect(console.error.mock.calls[0][0]).toBe('APP_ENV error')
})
Expand All @@ -171,15 +171,15 @@ describe('react-native-dotenv', () => {
console.error = jest.fn()
process.env.APP_ENV = 'production'

const {code} = transformFileSync(FIXTURES + 'app-env-production/source.js')
const { code } = transformFileSync(FIXTURES + 'app-env-production/source.js')
expect(code).toBe('console.log("never");\nconsole.log("this-should-not-appear");')
expect(console.error.mock.calls[0][0]).toBe('APP_ENV error')
})

it('should load MY_ENV specific env file', () => {
process.env.MY_ENV = 'cli'

const {code} = transformFileSync(FIXTURES + 'env-name/source.js')
const { code } = transformFileSync(FIXTURES + 'env-name/source.js')
expect(code).toBe('console.log("abc123456");\nconsole.log("username123456");')
})
})
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs')
const path = require('path')
const dotenv = require('dotenv')

function parseDotenvFile(filepath, verbose = false) {
function parseDotenvFile (filepath, verbose = false) {
let content

try {
Expand All @@ -19,7 +19,7 @@ function parseDotenvFile(filepath, verbose = false) {
return dotenv.parse(content) //
}

function undefObjectAssign(targetObject, sourceObject) {
function undefObjectAssign (targetObject, sourceObject) {
const keys = Object.keys(sourceObject)
for (const key of keys) {
if (sourceObject[key]) {
Expand All @@ -30,7 +30,7 @@ function undefObjectAssign(targetObject, sourceObject) {
return targetObject
}

function safeObjectAssign(targetObject, sourceObject, exceptions = []) {
function safeObjectAssign (targetObject, sourceObject, exceptions = []) {
const keys = Object.keys(targetObject)
for (const key of keys) {
if (targetObject[key] && sourceObject[key]) {
Expand All @@ -47,7 +47,7 @@ function safeObjectAssign(targetObject, sourceObject, exceptions = []) {
return targetObject
}

function mtime(filePath) {
function mtime (filePath) {
try {
return fs.statSync(filePath).mtimeMs
} catch {
Expand All @@ -69,7 +69,7 @@ module.exports = (api, options) => {
safe: false,
allowUndefined: true,
verbose: false,
...options,
...options
}
const babelMode = process.env[options.envName] || (process.env.BABEL_ENV && process.env.BABEL_ENV !== 'undefined' && process.env.BABEL_ENV !== 'development' && process.env.BABEL_ENV) || process.env.NODE_ENV || 'development'
const localFilePath = options.path + '.local'
Expand Down Expand Up @@ -105,7 +105,7 @@ module.exports = (api, options) => {
return ({
name: 'dotenv-import',
visitor: {
ImportDeclaration(filepath) {
ImportDeclaration (filepath) {
if (filepath.node.source.value !== options.moduleName) {
return
}
Expand Down Expand Up @@ -154,7 +154,7 @@ module.exports = (api, options) => {

filepath.remove()
},
MemberExpression(filepath) {
MemberExpression (filepath) {
if (!filepath.get('object').matchesPattern('process.env')) {
return
}
Expand All @@ -167,7 +167,7 @@ module.exports = (api, options) => {
filepath.replaceWith(t.valueToNode(value))
}
}
},
},
}
}
})
}
Loading
Loading