@dotenvx/next-env 0.1.0 → 1.74.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +56 -0
  2. package/index.cjs +191 -0
  3. package/package.json +17 -2
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # @dotenvx/next-env
2
+
3
+ `@dotenvx/next-env` is a drop-in replacement for Next.js' `@next/env` package.
4
+
5
+ Use it when you want Next.js to load encrypted dotenvx files during Next's own environment-loading step. This lets decrypted values exist before Next.js evaluates server modules.
6
+
7
+ ## npm
8
+
9
+ ```json
10
+ {
11
+ "dependencies": {
12
+ "@dotenvx/next-env": "^1.74.0",
13
+ "next": "^16.0.0"
14
+ },
15
+ "overrides": {
16
+ "@next/env": "npm:@dotenvx/next-env@^1.74.0"
17
+ }
18
+ }
19
+ ```
20
+
21
+ ## pnpm
22
+
23
+ ```json
24
+ {
25
+ "dependencies": {
26
+ "@dotenvx/next-env": "^1.74.0",
27
+ "next": "^16.0.0"
28
+ },
29
+ "pnpm": {
30
+ "overrides": {
31
+ "@next/env": "npm:@dotenvx/next-env@^1.74.0"
32
+ }
33
+ }
34
+ }
35
+ ```
36
+
37
+ ## Bun
38
+
39
+ ```json
40
+ {
41
+ "dependencies": {
42
+ "@dotenvx/next-env": "^1.74.0",
43
+ "next": "^16.0.0"
44
+ },
45
+ "overrides": {
46
+ "@next/env": "npm:@dotenvx/next-env@^1.74.0"
47
+ }
48
+ }
49
+ ```
50
+
51
+ After installing, run Next.js normally:
52
+
53
+ ```sh
54
+ npm run dev
55
+ npm run build
56
+ ```
package/index.cjs ADDED
@@ -0,0 +1,191 @@
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+ const dotenvx = require('@dotenvx/dotenvx')
4
+
5
+ let initialEnv
6
+ let combinedEnv
7
+ let parsedEnv
8
+ let cachedLoadedEnvFiles = []
9
+ let previousLoadedEnvFiles = []
10
+
11
+ function updateInitialEnv (newEnv) {
12
+ if (!initialEnv) {
13
+ return
14
+ }
15
+
16
+ for (const [key, value] of Object.entries(newEnv)) {
17
+ if (value === undefined) {
18
+ delete initialEnv[key]
19
+ } else {
20
+ initialEnv[key] = value
21
+ }
22
+ }
23
+ }
24
+
25
+ function replaceProcessEnv (sourceEnv) {
26
+ Object.keys(process.env).forEach((key) => {
27
+ if (!key.startsWith('__NEXT_PRIVATE')) {
28
+ if (sourceEnv[key] === undefined || sourceEnv[key] === '') {
29
+ delete process.env[key]
30
+ }
31
+ }
32
+ })
33
+
34
+ Object.entries(sourceEnv).forEach(([key, value]) => {
35
+ process.env[key] = value
36
+ })
37
+ }
38
+
39
+ function processEnv (
40
+ loadedEnvFiles,
41
+ dir,
42
+ log = console,
43
+ forceReload = false,
44
+ onReload
45
+ ) {
46
+ if (!initialEnv) {
47
+ initialEnv = Object.assign({}, process.env)
48
+ }
49
+
50
+ if (
51
+ !forceReload &&
52
+ (process.env.__NEXT_PROCESSED_ENV || loadedEnvFiles.length === 0)
53
+ ) {
54
+ return [process.env]
55
+ }
56
+
57
+ process.env.__NEXT_PROCESSED_ENV = 'true'
58
+
59
+ const origEnv = Object.assign({}, initialEnv)
60
+ const parsed = {}
61
+
62
+ try {
63
+ const filepaths = loadedEnvFiles.map((envFile) =>
64
+ path.join(dir || '', envFile.path)
65
+ )
66
+
67
+ const result = dotenvx.config({
68
+ path: filepaths,
69
+ processEnv: Object.assign({}, origEnv),
70
+ quiet: false
71
+ })
72
+
73
+ for (const envFile of loadedEnvFiles) {
74
+ if (
75
+ !previousLoadedEnvFiles.some(
76
+ (item) =>
77
+ item.contents === envFile.contents && item.path === envFile.path
78
+ )
79
+ ) {
80
+ if (onReload) onReload(envFile.path)
81
+ }
82
+
83
+ envFile.env = {}
84
+ }
85
+
86
+ for (const key of Object.keys(result.parsed || {})) {
87
+ if (
88
+ typeof parsed[key] === 'undefined' &&
89
+ typeof origEnv[key] === 'undefined'
90
+ ) {
91
+ parsed[key] = result.parsed[key]
92
+ }
93
+ }
94
+ } catch (err) {
95
+ log.error(`Failed to load env from ${dir || ''}`, err)
96
+ }
97
+
98
+ return [Object.assign(process.env, parsed), parsed]
99
+ }
100
+
101
+ function resetEnv () {
102
+ if (initialEnv) {
103
+ replaceProcessEnv(initialEnv)
104
+ }
105
+ }
106
+
107
+ function loadEnvConfig (
108
+ dir,
109
+ dev,
110
+ log = console,
111
+ forceReload = false,
112
+ onReload
113
+ ) {
114
+ if (!initialEnv) {
115
+ initialEnv = Object.assign({}, process.env)
116
+ }
117
+
118
+ if (combinedEnv && !forceReload) {
119
+ return {
120
+ combinedEnv,
121
+ parsedEnv,
122
+ loadedEnvFiles: cachedLoadedEnvFiles
123
+ }
124
+ }
125
+
126
+ replaceProcessEnv(initialEnv)
127
+
128
+ previousLoadedEnvFiles = cachedLoadedEnvFiles
129
+ cachedLoadedEnvFiles = []
130
+
131
+ const isTest = process.env.NODE_ENV === 'test'
132
+ const mode = isTest ? 'test' : dev ? 'development' : 'production'
133
+
134
+ const dotenvFiles = [
135
+ `.env.${mode}.local`,
136
+ mode !== 'test' && '.env.local',
137
+ `.env.${mode}`,
138
+ '.env'
139
+ ].filter(Boolean)
140
+
141
+ for (const envFile of dotenvFiles) {
142
+ const dotEnvPath = path.join(dir, envFile)
143
+
144
+ try {
145
+ const stats = fs.statSync(dotEnvPath)
146
+
147
+ if (!stats.isFile() && !stats.isFIFO()) {
148
+ continue
149
+ }
150
+
151
+ const contents = fs.readFileSync(dotEnvPath, 'utf8')
152
+
153
+ cachedLoadedEnvFiles.push({
154
+ path: envFile,
155
+ contents,
156
+ env: {}
157
+ })
158
+ } catch (err) {
159
+ if (err.code !== 'ENOENT') {
160
+ log.error(`Failed to load env from ${envFile}`, err)
161
+ }
162
+ }
163
+ }
164
+
165
+ const result = processEnv(
166
+ cachedLoadedEnvFiles,
167
+ dir,
168
+ log,
169
+ forceReload,
170
+ onReload
171
+ )
172
+
173
+ combinedEnv = result[0]
174
+ parsedEnv = result[1]
175
+
176
+ return {
177
+ combinedEnv,
178
+ parsedEnv,
179
+ loadedEnvFiles: cachedLoadedEnvFiles
180
+ }
181
+ }
182
+
183
+ module.exports = {
184
+ get initialEnv () {
185
+ return initialEnv
186
+ },
187
+ updateInitialEnv,
188
+ processEnv,
189
+ resetEnv,
190
+ loadEnvConfig
191
+ }
package/package.json CHANGED
@@ -1,7 +1,22 @@
1
1
  {
2
- "version": "0.1.0",
3
2
  "name": "@dotenvx/next-env",
3
+ "version": "1.74.2",
4
+ "description": "dotenvx-powered @next/env compatibility package",
5
+ "license": "BSD-3-Clause",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/dotenvx/dotenvx.git",
9
+ "directory": "packages/next-env"
10
+ },
11
+ "main": "index.cjs",
12
+ "files": [
13
+ "index.cjs"
14
+ ],
15
+ "dependencies": {
16
+ "@dotenvx/dotenvx": "1.74.2"
17
+ },
4
18
  "publishConfig": {
5
- "access": "public"
19
+ "access": "public",
20
+ "provenance": true
6
21
  }
7
22
  }