Skip to content

Commit e1be447

Browse files
committed
run on process
1 parent d0f36d2 commit e1be447

4 files changed

Lines changed: 34 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project are documented in this file.
44

55
## [Unreleased](https://github.com/dotenvx/react-native-dotenv/compare/v4.1.0...main)
66

7+
### Changed
8+
9+
- Log `◇ injected env` once per process so Metro does not spam it on every file when `api.cache(false)`.
10+
711
## [4.1.0](https://github.com/dotenvx/react-native-dotenv/compare/v4.0.0...v4.1.0) (2026-07-28)
812

913
### Added

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ On transform you'll see a message on stderr (same style as dotenv):
5454
◇ injected env (2) from .env
5555
```
5656

57-
Set `quiet: true` in the plugin options to suppress it.
57+
Metro may prefix worker logs with `|`. Expo also prints its own `env: load .env` line — that is Expo's built-in loader, not this plugin.
58+
59+
Set `quiet: true` in the plugin options to suppress the `◇ injected env` message.
5860

5961
 
6062

index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@ function parseDotenvFile (filepath, verbose = false) {
1616
}
1717
}
1818

19+
// Babel may re-instantiate this plugin per file (e.g. api.cache(false)). Log once
20+
// per process for the same message so Metro workers do not spam stderr.
21+
const loggedInjectMessages = new Set()
22+
1923
function logInjectedEnv (fileEnv, loadedPaths) {
2024
const keysCount = Object.keys(fileEnv).length
21-
if (loadedPaths.length > 0) {
22-
console.error(`◇ injected env (${keysCount}) from ${loadedPaths.join(', ')}`)
23-
} else {
24-
console.error(`◇ injected env (${keysCount})`)
25+
const message = loadedPaths.length > 0
26+
? `◇ injected env (${keysCount}) from ${loadedPaths.join(', ')}`
27+
: `◇ injected env (${keysCount})`
28+
29+
if (loggedInjectMessages.has(message)) {
30+
return
2531
}
32+
33+
loggedInjectMessages.add(message)
34+
console.error(message)
2635
}
2736

2837
function undefObjectAssign (targetObject, sourceObject) {

tests/index.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ describe('react-native-dotenv', () => {
5050
expect(console.error.mock.calls.some(call => /^ injected env \(\d+\) from /.test(String(call[0])))).toBe(true)
5151
})
5252

53+
it('should log injected env only once per process', () => {
54+
jest.resetModules()
55+
const plugin = require('../index.js')
56+
const babelOpts = {
57+
configFile: false,
58+
babelrc: false,
59+
plugins: [[plugin, { path: FIXTURES + 'default/.env' }]]
60+
}
61+
transformSync('import { API_KEY } from "@env"', babelOpts)
62+
transformSync('import { API_KEY } from "@env"', babelOpts)
63+
const injectLogs = console.error.mock.calls.filter(call => String(call[0]).includes('injected env'))
64+
expect(injectLogs).toHaveLength(1)
65+
})
66+
5367
it('should not log injected env when quiet', () => {
5468
jest.resetModules()
5569
transformSync('import { API_KEY } from "@env"', {

0 commit comments

Comments
 (0)