react-native-dotenv
Advanced tools
Comparing version 3.4.9 to 3.4.10
31
index.js
@@ -41,5 +41,5 @@ const fs = require('fs') | ||
for (let j = 0, length = exceptions.length; j < length; j++) { | ||
if (sourceObject[exceptions[j]]) { | ||
targetObject[exceptions[j]] = sourceObject[exceptions[j]] | ||
for (let index = 0, length = exceptions.length; index < length; index++) { | ||
if (sourceObject[exceptions[index]]) { | ||
targetObject[exceptions[index]] = sourceObject[exceptions[index]] | ||
} | ||
@@ -82,2 +82,5 @@ } | ||
console.log('dotenvMode', babelMode) | ||
if (process.env[options.envName] === 'production' || process.env[options.envName] === 'development') { | ||
console.error('APP_ENV error', 'cannot use APP_ENV=development or APP_ENV=production') | ||
} | ||
} | ||
@@ -108,9 +111,9 @@ | ||
if (path.node.source.value === options.moduleName) { | ||
for (const [idx, specifier] of path.node.specifiers.entries()) { | ||
for (const [index, specifier] of path.node.specifiers.entries()) { | ||
if (specifier.type === 'ImportDefaultSpecifier') { | ||
throw path.get('specifiers')[idx].buildCodeFrameError('Default import is not supported') | ||
throw path.get('specifiers')[index].buildCodeFrameError('Default import is not supported') | ||
} | ||
if (specifier.type === 'ImportNamespaceSpecifier') { | ||
throw path.get('specifiers')[idx].buildCodeFrameError('Wildcard import is not supported') | ||
throw path.get('specifiers')[index].buildCodeFrameError('Wildcard import is not supported') | ||
} | ||
@@ -123,22 +126,22 @@ | ||
if (Array.isArray(options.allowlist) && !options.allowlist.includes(importedId)) { | ||
throw path.get('specifiers')[idx].buildCodeFrameError(`"${importedId}" was not present in allowlist`) | ||
throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not present in allowlist`) | ||
} else if (Array.isArray(options.whitelist) && !options.whitelist.includes(importedId)) { | ||
console.warn('[DEPRECATION WARNING] This option is will be deprecated soon. Use allowlist instead') | ||
throw path.get('specifiers')[idx].buildCodeFrameError(`"${importedId}" was not whitelisted`) | ||
throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not whitelisted`) | ||
} | ||
if (Array.isArray(options.blocklist) && options.blocklist.includes(importedId)) { | ||
throw path.get('specifiers')[idx].buildCodeFrameError(`"${importedId}" was not present in blocklist`) | ||
throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not present in blocklist`) | ||
} else if (Array.isArray(options.blacklist) && options.blacklist.includes(importedId)) { | ||
console.warn('[DEPRECATION WARNING] This option is will be deprecated soon. Use blocklist instead') | ||
throw path.get('specifiers')[idx].buildCodeFrameError(`"${importedId}" was blacklisted`) | ||
throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was blacklisted`) | ||
} | ||
if (!options.allowUndefined && !Object.prototype.hasOwnProperty.call(env, importedId)) { | ||
throw path.get('specifiers')[idx].buildCodeFrameError(`"${importedId}" is not defined in ${options.path}`) | ||
if (!options.allowUndefined && !Object.hasOwn(env, importedId)) { | ||
throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" is not defined in ${options.path}`) | ||
} | ||
const binding = path.scope.getBinding(localId) | ||
for (const refPath of binding.referencePaths) { | ||
refPath.replaceWith(t.valueToNode(env[importedId])) | ||
for (const referencePath of binding.referencePaths) { | ||
referencePath.replaceWith(t.valueToNode(env[importedId])) | ||
} | ||
@@ -145,0 +148,0 @@ } |
{ | ||
"name": "react-native-dotenv", | ||
"version": "3.4.9", | ||
"version": "3.4.10", | ||
"description": "Load environment variables using import statements.", | ||
@@ -26,10 +26,10 @@ "repository": "github:goatandsheep/react-native-dotenv", | ||
"dependencies": { | ||
"dotenv": "^16.3.1" | ||
"dotenv": "^16.4.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.22.5", | ||
"@babel/core": "^7.23.9", | ||
"codecov": "^3.8.3", | ||
"jest": "29.3.1", | ||
"jest-junit": "^15.0.0", | ||
"xo": "^0.54.2" | ||
"jest": "29.7.0", | ||
"jest-junit": "^16.0.0", | ||
"xo": "^0.57.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "author": "Kemal Ahmed", |
@@ -8,5 +8,5 @@ # react-native-dotenv [![CircleCI](https://circleci.com/gh/goatandsheep/react-native-dotenv.svg?style=svg)](https://circleci.com/gh/goatandsheep/react-native-dotenv) | ||
[![codecov](https://badgen.net/codecov/c/github/goatandsheep/react-native-dotenv)](https://codecov.io/gh/goatandsheep/react-native-dotenv) | ||
[![XO code style](https://badgen.net/badge/code%20style/XO/cyan)](https://github.com/xojs/xo) [![Join the chat at https://gitter.im/pass-it-on/react-native-dotenv](https://badges.gitter.im/pass-it-on/react-native-dotenv.svg)](https://gitter.im/pass-it-on/react-native-dotenv?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[![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://camo.githubusercontent.com/f4f6e29efeee2705d4155a0b07373147ac266580fef1172ddd2e72a2d9445c55/68747470733a2f2f62616467652e646f74656e762e6f72672f776f726b732d776974682e7376673f723d33)](https://www.dotenv.org/get-started?r=7) | ||
[![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) | ||
@@ -31,16 +31,19 @@ ## Installation | ||
This babel plugin lets you inject your environment variables into your Javascript environment using dotenv for multiple environments. It is best suited for use with react native and works with all flavors including web. | ||
This babel plugin lets you inject your environment variables into your Javascript environment using dotenv for multiple environments. It is best suited for use with react native and works with all flavors including web. | ||
## Usage | ||
**.babelrc** | ||
> Also preview [the expo test app](https://github.com/goatandsheep/react-native-dotenv-expo-test). | ||
**babel.config.js** | ||
Basic setup: | ||
```json | ||
{ | ||
"plugins": [ | ||
["module:react-native-dotenv"] | ||
```javascript | ||
api.cache(false) | ||
module.exports = { | ||
plugins: [ | ||
['module:react-native-dotenv'] | ||
] | ||
} | ||
}; | ||
``` | ||
@@ -50,23 +53,30 @@ | ||
```json | ||
{ | ||
"plugins": [ | ||
["module:react-native-dotenv", { | ||
"envName": "APP_ENV", | ||
"moduleName": "@env", | ||
"path": ".env", | ||
"blocklist": null, | ||
"allowlist": null, | ||
"blacklist": null, // DEPRECATED | ||
"whitelist": null, // DEPRECATED | ||
"safe": false, | ||
"allowUndefined": true, | ||
"verbose": false | ||
}] | ||
] | ||
} | ||
```javascript | ||
api.cache(false) | ||
module.exports = { | ||
plugins: [ | ||
[ | ||
'module:react-native-dotenv', | ||
{ | ||
envName: 'APP_ENV', | ||
moduleName: '@env', | ||
path: '.env', | ||
blocklist: null, | ||
allowlist: null, | ||
blacklist: null, // DEPRECATED | ||
whitelist: null, // DEPRECATED | ||
safe: false, | ||
allowUndefined: true, | ||
verbose: false, | ||
}, | ||
], | ||
], | ||
}; | ||
``` | ||
Note: for safe mode, it's highly recommended to set `allowUndefined` to `false`. | ||
> Note: for safe mode, it's highly recommended to set `allowUndefined` to `false`. | ||
> Note: Expo now has [built-in environment variable support](https://docs.expo.dev/guides/environment-variables/). Evaluate if you need | ||
**.env** | ||
@@ -79,5 +89,21 @@ | ||
### process.env technique | ||
In **users.js** | ||
```js | ||
fetch(`${process.env.API_URL}/users`, { | ||
headers: { | ||
'Authorization': `Bearer ${process.env.API_TOKEN}` | ||
} | ||
}) | ||
``` | ||
### Import technique | ||
> The import technique, which is the initial functionality of the library, is to have an import statement at the top that turns into an object because of Babel | ||
In **users.js** | ||
```js | ||
import {API_URL, API_TOKEN} from "@env" | ||
@@ -92,4 +118,2 @@ | ||
Also preview [the expo test app](https://github.com/goatandsheep/react-native-dotenv-expo-test). | ||
## [DEPRECATED] White and black lists | ||
@@ -203,3 +227,3 @@ | ||
Note: if you're using `APP_ENV` (or `envName`), you should avoid using `development` nor `production` as values, and you should avoid having a `.env.development` or `.env.production`. This is a Babel and Node thing that I have little control over unfortunately and is consistent with many other platforms that have an override option, like [Gatsby](https://www.gatsbyjs.com/docs/how-to/local-development/environment-variables/#additional-environments-staging-test-etc). If you want to use `development` and `production`, you should not use `APP_ENV` (or `envName`), but rather the built-in `NODE_ENV=development` or `NODE_ENV=production`. | ||
Note: if you're using `APP_ENV` (or `envName`), you cannot use `development` nor `production` as values, and you should avoid having a `.env.development` or `.env.production`. This is a Babel and Node thing that I have little control over unfortunately and is consistent with many other platforms that have an override option, like [Gatsby](https://www.gatsbyjs.com/docs/how-to/local-development/environment-variables/#additional-environments-staging-test-etc). If you want to use `development` and `production`, you should not use `APP_ENV` (or `envName`), but rather the built-in `NODE_ENV=development` or `NODE_ENV=production`. | ||
@@ -238,3 +262,3 @@ | ||
- Create a `types` folder in your project | ||
- Inside that folder, create a `*.d.tsx`file, say, `env.d.tsx` | ||
- Inside that folder, create a `*.d.ts`file, say, `env.d.ts` | ||
- in that file, declare a module as the following format: | ||
@@ -257,4 +281,4 @@ | ||
... | ||
"typeRoots": ["./src/types"], | ||
... | ||
"typeRoots": ["./types"], | ||
... | ||
} | ||
@@ -301,2 +325,6 @@ ... | ||
`npm start -- --reset-cache` | ||
or | ||
`yarn start --reset-cache` | ||
@@ -310,2 +338,6 @@ | ||
`jest --no-cache` | ||
or | ||
`expo r -c` | ||
@@ -312,0 +344,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22020
141
377
12
Updateddotenv@^16.4.1