@fantasticfour/dotenvx-next 0.1.3 → 0.2.1

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 (52) hide show
  1. package/README.md +157 -0
  2. package/dist/chunk-CKQMccvm.cjs +28 -0
  3. package/dist/index.cjs +6 -0
  4. package/dist/index.d.cts +2 -1
  5. package/dist/index.d.mts +2 -1
  6. package/dist/index.mjs +2 -1
  7. package/dist/next-env-compat.cjs +29 -11858
  8. package/dist/next-env-compat.cjs.map +1 -1
  9. package/dist/next-env-compat.d.cts +9 -1
  10. package/dist/next-env-compat.d.cts.map +1 -0
  11. package/dist/next-env-compat.d.mts +9 -0
  12. package/dist/next-env-compat.d.mts.map +1 -0
  13. package/dist/next-env-compat.mjs +31 -0
  14. package/dist/next-env-compat.mjs.map +1 -0
  15. package/dist/plugin.cjs +29 -53
  16. package/dist/plugin.cjs.map +1 -1
  17. package/dist/plugin.d.cts +2 -7
  18. package/dist/plugin.d.cts.map +1 -1
  19. package/dist/plugin.d.mts +2 -7
  20. package/dist/plugin.d.mts.map +1 -1
  21. package/dist/plugin.mjs +25 -52
  22. package/dist/plugin.mjs.map +1 -1
  23. package/dist/turbopack-inject.cjs +95 -4
  24. package/dist/turbopack-inject.cjs.map +1 -0
  25. package/dist/turbopack-inject.d.cts +4 -4
  26. package/dist/turbopack-inject.d.cts.map +1 -1
  27. package/dist/turbopack-inject.d.mts +4 -4
  28. package/dist/turbopack-inject.d.mts.map +1 -1
  29. package/dist/turbopack-inject.mjs +12 -14
  30. package/dist/turbopack-inject.mjs.map +1 -1
  31. package/dist/webpack-plugin-BmC1uBZp.mjs +47 -0
  32. package/dist/webpack-plugin-BmC1uBZp.mjs.map +1 -0
  33. package/dist/webpack-plugin.cjs +3 -11
  34. package/dist/webpack-plugin.cjs.map +1 -1
  35. package/dist/webpack-plugin.d.cts +7 -10
  36. package/dist/webpack-plugin.d.cts.map +1 -1
  37. package/dist/webpack-plugin.d.mts +7 -10
  38. package/dist/webpack-plugin.d.mts.map +1 -1
  39. package/dist/webpack-plugin.mjs +1 -51
  40. package/package.json +18 -19
  41. package/dist/chunk-y_GBKt04.mjs +0 -5
  42. package/dist/init.cjs +0 -16
  43. package/dist/init.cjs.map +0 -1
  44. package/dist/init.d.cts +0 -5
  45. package/dist/init.d.cts.map +0 -1
  46. package/dist/init.d.mts +0 -5
  47. package/dist/init.d.mts.map +0 -1
  48. package/dist/init.mjs +0 -18
  49. package/dist/init.mjs.map +0 -1
  50. package/dist/turbopack-inject-BgCxanwa.cjs +0 -139
  51. package/dist/turbopack-inject-BgCxanwa.cjs.map +0 -1
  52. package/dist/webpack-plugin.mjs.map +0 -1
package/README.md ADDED
@@ -0,0 +1,157 @@
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 decrypts env files at build time and inlines the resolved values directly into the webpack and turbopack runtime, so they are available 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. Because values are inlined at build time, no encrypted env files need to be present in the deployed serverless bundle, and you do not need `dotenvx run` in your dev or build scripts.
50
+
51
+ ### Full `@next/env` coverage with package overrides
52
+
53
+ The plugin calls `dotenvx.config()` at build time (during `next.config.ts`
54
+ evaluation) and inlines the resolved values into the webpack/turbopack runtime.
55
+ This covers the common case — module-scope `process.env` reads at import time.
56
+
57
+ However, Next.js also calls `loadEnvConfig` from `@next/env` during its own
58
+ **Node.js startup**, before webpack runs. The webpack `resolve.alias` set by
59
+ this plugin only intercepts imports inside webpack-compiled code, so those
60
+ startup calls are not covered by the alias alone.
61
+
62
+ To get full coverage at the Node.js package-resolution level, add an override
63
+ so that `require('@next/env')` resolves to this package instead:
64
+
65
+ **npm** (`package.json`):
66
+
67
+ ```json
68
+ {
69
+ "overrides": {
70
+ "@next/env": "@fantasticfour/dotenvx-next"
71
+ }
72
+ }
73
+ ```
74
+
75
+ **pnpm** (`package.json`):
76
+
77
+ ```json
78
+ {
79
+ "pnpm": {
80
+ "overrides": {
81
+ "@next/env": "@fantasticfour/dotenvx-next"
82
+ }
83
+ }
84
+ }
85
+ ```
86
+
87
+ **Yarn** (`package.json`):
88
+
89
+ ```json
90
+ {
91
+ "resolutions": {
92
+ "@next/env": "@fantasticfour/dotenvx-next"
93
+ }
94
+ }
95
+ ```
96
+
97
+ With this override, `require('@next/env').loadEnvConfig` returns the wrapped
98
+ version that calls `dotenvx.config()` first — at all call sites, including
99
+ Next.js's own startup code.
100
+
101
+ ### Multiple environments
102
+
103
+ The `files` option accepts any list of env files. Because the plugin resolves them at build time, you are responsible for selecting the right files for the current environment. On Vercel, `VERCEL_ENV` is set during the build:
104
+
105
+ ```ts
106
+ // next.config.ts
107
+ const vercelEnv = process.env.VERCEL_ENV;
108
+
109
+ export default withDotenvx(config, {
110
+ files:
111
+ vercelEnv === 'production'
112
+ ? ['.env.base', '.env.production']
113
+ : vercelEnv === 'preview'
114
+ ? ['.env.base', '.env.preview']
115
+ : ['.env'], // local dev
116
+ });
117
+ ```
118
+
119
+ Passing an empty array (`files: []`) skips decryption entirely for that build, which is useful if you want to handle local dev with `dotenvx run` separately rather than baking values into the dev server runtime.
120
+
121
+ ### Options
122
+
123
+ ```ts
124
+ withDotenvx(nextConfig, {
125
+ // Env files to load. Defaults to ['.env']
126
+ files: ['.env', '.env.production'],
127
+
128
+ // Directory where env files live. Defaults to process.cwd()
129
+ envDir: '/path/to/env/files',
130
+ })
131
+ ```
132
+
133
+ ### With an async config function
134
+
135
+ ```ts
136
+ export default withDotenvx(async (phase) => {
137
+ return {
138
+ // your config
139
+ };
140
+ });
141
+ ```
142
+
143
+ ## How it works
144
+
145
+ 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.
146
+
147
+ **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.
148
+
149
+ **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`.
150
+
151
+ **`@next/env` webpack alias:** The plugin sets `webpack resolve.alias['@next/env']` to a compat shim that wraps `loadEnvConfig` to run `dotenvx.config()` first. This covers any import of `@next/env` inside webpack-compiled server bundles (hot-reload handlers, preview mode helpers). It does not cover Next.js's own pre-webpack startup calls — use the [npm overrides pattern](#full-nextenv-coverage-with-package-overrides) for full coverage.
152
+
153
+ **Edge runtime:** The webpack plugin detects edge runtime compilations via `compiler.options.target` and injects into `edge-runtime-webpack.js` rather than `webpack-runtime.js`. The inline snippet uses a `typeof process !== 'undefined'` guard, so it is safe in edge runtimes where `process` may be absent.
154
+
155
+ ## Debugging
156
+
157
+ Set `DEBUG_DOTENVX_NEXT=1` to see injection logs during your build.
@@ -0,0 +1,28 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+ //#endregion
23
+ Object.defineProperty(exports, "__toESM", {
24
+ enumerable: true,
25
+ get: function() {
26
+ return __toESM;
27
+ }
28
+ });
package/dist/index.cjs CHANGED
@@ -1,3 +1,9 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_plugin = require("./plugin.cjs");
3
+ const require_next_env_compat = require("./next-env-compat.cjs");
4
+ exports.initialEnv = require_next_env_compat.initialEnv;
5
+ exports.loadEnvConfig = require_next_env_compat.loadEnvConfig;
6
+ exports.processEnv = require_next_env_compat.processEnv;
7
+ exports.resetEnv = require_next_env_compat.resetEnv;
8
+ exports.updateInitialEnv = require_next_env_compat.updateInitialEnv;
3
9
  exports.withDotenvx = require_plugin.withDotenvx;
package/dist/index.d.cts CHANGED
@@ -1,2 +1,3 @@
1
1
  import { DotenvxNextOptions, withDotenvx } from "./plugin.cjs";
2
- export { type DotenvxNextOptions, withDotenvx };
2
+ import { Env, LoadedEnvFiles, initialEnv, loadEnvConfig, processEnv, resetEnv, updateInitialEnv } from "./next-env-compat.cjs";
3
+ export { type DotenvxNextOptions, type Env, type LoadedEnvFiles, initialEnv, loadEnvConfig, processEnv, resetEnv, updateInitialEnv, withDotenvx };
package/dist/index.d.mts CHANGED
@@ -1,2 +1,3 @@
1
1
  import { DotenvxNextOptions, withDotenvx } from "./plugin.mjs";
2
- export { type DotenvxNextOptions, withDotenvx };
2
+ import { Env, LoadedEnvFiles, initialEnv, loadEnvConfig, processEnv, resetEnv, updateInitialEnv } from "./next-env-compat.mjs";
3
+ export { type DotenvxNextOptions, type Env, type LoadedEnvFiles, initialEnv, loadEnvConfig, processEnv, resetEnv, updateInitialEnv, withDotenvx };
package/dist/index.mjs CHANGED
@@ -1,2 +1,3 @@
1
1
  import { withDotenvx } from "./plugin.mjs";
2
- export { withDotenvx };
2
+ import { initialEnv, loadEnvConfig, processEnv, resetEnv, updateInitialEnv } from "./next-env-compat.mjs";
3
+ export { initialEnv, loadEnvConfig, processEnv, resetEnv, updateInitialEnv, withDotenvx };