@fantasticfour/dotenvx-next 0.1.3 → 0.2.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.
- package/README.md +85 -0
- package/dist/next-env-compat.cjs +2 -2
- package/dist/next-env-compat.cjs.map +1 -1
- package/dist/plugin.cjs +21 -48
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.cts +2 -7
- package/dist/plugin.d.cts.map +1 -1
- package/dist/plugin.d.mts +2 -7
- package/dist/plugin.d.mts.map +1 -1
- package/dist/plugin.mjs +20 -49
- package/dist/plugin.mjs.map +1 -1
- package/dist/{turbopack-inject-BgCxanwa.cjs → turbopack-inject-BElH61NN.cjs} +13 -15
- package/dist/turbopack-inject-BElH61NN.cjs.map +1 -0
- package/dist/turbopack-inject.cjs +1 -1
- package/dist/turbopack-inject.d.cts +4 -4
- package/dist/turbopack-inject.d.cts.map +1 -1
- package/dist/turbopack-inject.d.mts +4 -4
- package/dist/turbopack-inject.d.mts.map +1 -1
- package/dist/turbopack-inject.mjs +12 -14
- package/dist/turbopack-inject.mjs.map +1 -1
- package/dist/webpack-plugin-BmC1uBZp.mjs +47 -0
- package/dist/webpack-plugin-BmC1uBZp.mjs.map +1 -0
- package/dist/webpack-plugin.cjs +3 -11
- package/dist/webpack-plugin.cjs.map +1 -1
- package/dist/webpack-plugin.d.cts +7 -10
- package/dist/webpack-plugin.d.cts.map +1 -1
- package/dist/webpack-plugin.d.mts +7 -10
- package/dist/webpack-plugin.d.mts.map +1 -1
- package/dist/webpack-plugin.mjs +1 -51
- package/package.json +17 -19
- package/dist/chunk-y_GBKt04.mjs +0 -5
- package/dist/init.cjs +0 -16
- package/dist/init.cjs.map +0 -1
- package/dist/init.d.cts +0 -5
- package/dist/init.d.cts.map +0 -1
- package/dist/init.d.mts +0 -5
- package/dist/init.d.mts.map +0 -1
- package/dist/init.mjs +0 -18
- package/dist/init.mjs.map +0 -1
- package/dist/turbopack-inject-BgCxanwa.cjs.map +0 -1
- package/dist/webpack-plugin.mjs.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# @fantasticfour/dotenvx-next
|
|
2
|
+
|
|
3
|
+
A Next.js plugin that loads [dotenvx](https://dotenvx.com) secrets before any user code evaluates at runtime. It solves the timing problem where encrypted env vars need to be decrypted before modules like Prisma initialize their connection pools.
|
|
4
|
+
|
|
5
|
+
## The problem
|
|
6
|
+
|
|
7
|
+
dotenvx decrypts secrets at startup, but Next.js evaluates server modules before that decryption runs. Two common patterns break silently:
|
|
8
|
+
|
|
9
|
+
**`instrumentation.ts`** — Next.js's official hook for startup side-effects runs _after_ modules have already been imported. By the time `dotenvx.config()` executes there, any module-scope code has already read `process.env`:
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// instrumentation.ts — too late
|
|
13
|
+
export async function register() {
|
|
14
|
+
const { config } = await import('@dotenvx/dotenvx');
|
|
15
|
+
config(); // DATABASE_URL was already read as undefined
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Module-scope `process.env` access** — Anything that reads an env var at the top level of a module (outside a function) runs at import time, before any instrumentation or config call:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
// db.ts — evaluated at import time
|
|
23
|
+
const pool = new Pool({ connectionString: process.env.DATABASE_URL }); // undefined ✗
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This plugin injects the dotenvx init bundle directly into webpack and turbopack runtime files at build time, so decryption happens before any user module is imported.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install @fantasticfour/dotenvx-next
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Requires `next >= 14`.
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
Wrap your Next.js config with `withDotenvx`:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
// next.config.ts
|
|
42
|
+
import { withDotenvx } from '@fantasticfour/dotenvx-next';
|
|
43
|
+
|
|
44
|
+
export default withDotenvx({
|
|
45
|
+
// your existing Next.js config
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
By default it loads `.env` from your project root.
|
|
50
|
+
|
|
51
|
+
### Options
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
withDotenvx(nextConfig, {
|
|
55
|
+
// Env files to load. Defaults to ['.env']
|
|
56
|
+
files: ['.env', '.env.production'],
|
|
57
|
+
|
|
58
|
+
// Directory where env files live. Defaults to process.cwd()
|
|
59
|
+
envDir: '/path/to/env/files',
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### With an async config function
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
export default withDotenvx(async (phase) => {
|
|
67
|
+
return {
|
|
68
|
+
// your config
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## How it works
|
|
74
|
+
|
|
75
|
+
At build time, `dotenvx.config()` decrypts the specified env files and the resolved key-value pairs are serialized into a compact `Object.assign(process.env, {...})` snippet.
|
|
76
|
+
|
|
77
|
+
**Webpack builds:** `DotenvxWebpackPlugin` prepends this snippet to `webpack-runtime.js` via the `processAssets` hook at `PROCESS_ASSETS_STAGE_ADDITIONS`, so every value is available before any module code runs.
|
|
78
|
+
|
|
79
|
+
**Turbopack builds:** Since turbopack bypasses webpack's plugin API, the plugin patches `fs.writeFile` to detect when compilation completes (signaled by `export-detail.json` being written), then prepends the same snippet into `[turbopack]_runtime.js`.
|
|
80
|
+
|
|
81
|
+
Because env values are inlined at build time, no encrypted `.env` files need to be present in the deployed serverless bundle.
|
|
82
|
+
|
|
83
|
+
## Debugging
|
|
84
|
+
|
|
85
|
+
Set `DEBUG_DOTENVX_NEXT=1` to see injection logs during your build.
|
package/dist/next-env-compat.cjs
CHANGED
|
@@ -11839,8 +11839,8 @@ function resolveEnvFiles(envDir) {
|
|
|
11839
11839
|
const vercelEnv = process.env.VERCEL_ENV;
|
|
11840
11840
|
const nodeEnv = process.env.NODE_ENV;
|
|
11841
11841
|
let candidates = [];
|
|
11842
|
-
if (vercelEnv === "production" || nodeEnv === "production") candidates = [".env
|
|
11843
|
-
else if (vercelEnv === "preview") candidates = [".env
|
|
11842
|
+
if (vercelEnv === "production" || nodeEnv === "production") candidates = [".env", ".env.production"];
|
|
11843
|
+
else if (vercelEnv === "preview") candidates = [".env", ".env.preview"];
|
|
11844
11844
|
else return [];
|
|
11845
11845
|
return candidates.filter((f) => fs.existsSync(path.resolve(envDir, f)));
|
|
11846
11846
|
}
|