@dotenvx/next-env 1.74.2 → 2.0.0

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 (5) hide show
  1. package/LICENSE +28 -0
  2. package/README.md +109 -33
  3. package/dist/index.cjs +6295 -0
  4. package/package.json +16 -9
  5. package/index.cjs +0 -191
package/package.json CHANGED
@@ -1,22 +1,29 @@
1
1
  {
2
2
  "name": "@dotenvx/next-env",
3
- "version": "1.74.2",
4
- "description": "dotenvx-powered @next/env compatibility package",
3
+ "version": "2.0.0",
4
+ "description": "dotenvx drop-in replacement for @next/env",
5
5
  "license": "BSD-3-Clause",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/dotenvx/dotenvx.git",
9
9
  "directory": "packages/next-env"
10
10
  },
11
- "main": "index.cjs",
11
+ "main": "dist/index.cjs",
12
12
  "files": [
13
- "index.cjs"
13
+ "dist/**/*",
14
+ "LICENSE",
15
+ "README.md"
14
16
  ],
15
- "dependencies": {
16
- "@dotenvx/dotenvx": "1.74.2"
17
- },
18
17
  "publishConfig": {
19
- "access": "public",
20
- "provenance": true
18
+ "access": "public"
19
+ },
20
+ "scripts": {
21
+ "build": "esbuild index.cjs --bundle --platform=node --format=cjs --target=node16 --outfile=dist/index.cjs --legal-comments=none",
22
+ "prepack": "npm run build",
23
+ "prepublishOnly": "npm pack --dry-run"
24
+ },
25
+ "devDependencies": {
26
+ "@dotenvx/primitives": "0.3.0",
27
+ "esbuild": "^0.28.1"
21
28
  }
22
29
  }
package/index.cjs DELETED
@@ -1,191 +0,0 @@
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
- }