@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/dist/plugin.cjs
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_turbopack_inject = require("./turbopack-inject-
|
|
2
|
+
const require_turbopack_inject = require("./turbopack-inject-BElH61NN.cjs");
|
|
3
3
|
const require_webpack_plugin = require("./webpack-plugin.cjs");
|
|
4
4
|
let node_fs = require("node:fs");
|
|
5
5
|
node_fs = require_turbopack_inject.__toESM(node_fs, 1);
|
|
6
6
|
let node_path = require("node:path");
|
|
7
7
|
node_path = require_turbopack_inject.__toESM(node_path, 1);
|
|
8
|
+
let _dotenvx_dotenvx = require("@dotenvx/dotenvx");
|
|
9
|
+
_dotenvx_dotenvx = require_turbopack_inject.__toESM(_dotenvx_dotenvx, 1);
|
|
8
10
|
//#region src/plugin.ts
|
|
9
11
|
/**
|
|
10
|
-
* withDotenvx() — Next.js config wrapper that
|
|
11
|
-
*
|
|
12
|
+
* withDotenvx() — Next.js config wrapper that loads dotenvx secrets at build time
|
|
13
|
+
* and inlines them into the webpack/turbopack runtime so they are available before
|
|
14
|
+
* any user code evaluates (Prisma Pool, etc.) at module-load time.
|
|
12
15
|
*
|
|
13
16
|
* Strategy:
|
|
14
|
-
* -
|
|
15
|
-
*
|
|
16
|
-
* -
|
|
17
|
-
*
|
|
17
|
+
* - At build time, call dotenvx.config() to decrypt env files and capture the
|
|
18
|
+
* resolved key-value pairs.
|
|
19
|
+
* - Webpack builds: prepend Object.assign(process.env, {...}) into webpack-runtime.js
|
|
20
|
+
* via DotenvxWebpackPlugin (processAssets hook at PROCESS_ASSETS_STAGE_ADDITIONS).
|
|
21
|
+
* - Turbopack builds: patch fs.writeFile to detect compilation completion and inject
|
|
22
|
+
* the same snippet into [turbopack]_runtime.js files.
|
|
18
23
|
* - Also aliases @next/env → @fantasticfour/dotenvx-next/next-env so dotenvx
|
|
19
24
|
* secrets are available during Next.js config evaluation itself.
|
|
20
25
|
*/
|
|
21
|
-
const DEFAULT_ENV_FILES = [
|
|
22
|
-
".env.base",
|
|
23
|
-
".env.production",
|
|
24
|
-
".env.preview"
|
|
25
|
-
];
|
|
26
|
+
const DEFAULT_ENV_FILES = [".env"];
|
|
26
27
|
const PLUGIN_DEBUG = !!process.env.DEBUG_DOTENVX_NEXT;
|
|
27
28
|
function debugLog(...args) {
|
|
28
29
|
if (!PLUGIN_DEBUG) return;
|
|
@@ -31,40 +32,20 @@ function debugLog(...args) {
|
|
|
31
32
|
function resolveExistingFiles(files, envDir) {
|
|
32
33
|
return files.map((f) => node_path.default.resolve(envDir, f)).filter((absPath) => node_fs.default.existsSync(absPath));
|
|
33
34
|
}
|
|
34
|
-
/**
|
|
35
|
-
* Load the compiled init bundle source. This is the CJS bundle from dist/init.cjs
|
|
36
|
-
* that will be prepended into webpack/turbopack runtime files.
|
|
37
|
-
*/
|
|
38
|
-
function loadInitSource() {
|
|
39
|
-
try {
|
|
40
|
-
return require("node:fs").readFileSync(require.resolve("./init.cjs"), "utf8");
|
|
41
|
-
} catch {
|
|
42
|
-
const pkgRoot = node_path.default.resolve(new URL(require("url").pathToFileURL(__filename).href).pathname, "../../");
|
|
43
|
-
return node_fs.default.readFileSync(node_path.default.join(pkgRoot, "dist", "init.cjs"), "utf8");
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
35
|
async function dotenvxNextConfigFn(nextConfig, options, phase, defaults) {
|
|
47
36
|
let resolvedNextConfig;
|
|
48
37
|
if (typeof nextConfig === "function") resolvedNextConfig = { ...await nextConfig(phase, defaults) };
|
|
49
38
|
else resolvedNextConfig = { ...nextConfig };
|
|
50
39
|
const envDir = options.envDir ?? process.cwd();
|
|
51
40
|
const resolvedFiles = resolveExistingFiles(options.files ?? DEFAULT_ENV_FILES, envDir);
|
|
52
|
-
const shouldTrace = options.traceEnvFiles !== false;
|
|
53
41
|
const isTurbopack = !!(process.env.TURBOPACK || process.env.TURBOPACK_DEV || process.env.TURBOPACK_BUILD || process.env.npm_config_turbopack);
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
else tracing[catchAll] = resolvedFiles;
|
|
62
|
-
}
|
|
63
|
-
if (isTurbopack) try {
|
|
64
|
-
require_turbopack_inject.activateTurbopackInjection(loadInitSource());
|
|
65
|
-
} catch (err) {
|
|
66
|
-
console.warn("[dotenvx-next] Could not load init bundle for turbopack injection:", err);
|
|
67
|
-
}
|
|
42
|
+
const { parsed: env = {} } = resolvedFiles.length ? _dotenvx_dotenvx.default.config({
|
|
43
|
+
path: resolvedFiles,
|
|
44
|
+
overload: true,
|
|
45
|
+
quiet: true
|
|
46
|
+
}) : { parsed: {} };
|
|
47
|
+
debugLog(`phase=${phase}, isTurbopack=${isTurbopack}, resolvedFiles=${JSON.stringify(resolvedFiles)}, envKeys=${Object.keys(env).join(",")}`);
|
|
48
|
+
if (isTurbopack) require_turbopack_inject.activateTurbopackInjection(env);
|
|
68
49
|
const prevWebpack = resolvedNextConfig.webpack;
|
|
69
50
|
resolvedNextConfig.webpack = (webpackConfig, webpackOptions) => {
|
|
70
51
|
const config = prevWebpack ? prevWebpack(webpackConfig, webpackOptions) : webpackConfig;
|
|
@@ -76,15 +57,7 @@ async function dotenvxNextConfigFn(nextConfig, options, phase, defaults) {
|
|
|
76
57
|
} catch {
|
|
77
58
|
debugLog("Could not resolve @fantasticfour/dotenvx-next/next-env for alias — skipping");
|
|
78
59
|
}
|
|
79
|
-
if (!isTurbopack)
|
|
80
|
-
const plugin = new require_webpack_plugin.DotenvxWebpackPlugin({
|
|
81
|
-
files: resolvedFiles,
|
|
82
|
-
initSource: loadInitSource()
|
|
83
|
-
});
|
|
84
|
-
config.plugins.push(plugin);
|
|
85
|
-
} catch (err) {
|
|
86
|
-
console.warn("[dotenvx-next] Could not load init bundle for webpack injection:", err);
|
|
87
|
-
}
|
|
60
|
+
if (!isTurbopack) config.plugins.push(new require_webpack_plugin.DotenvxWebpackPlugin({ env }));
|
|
88
61
|
return config;
|
|
89
62
|
};
|
|
90
63
|
return resolvedNextConfig;
|
package/dist/plugin.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.cjs","names":["path","fs","DotenvxWebpackPlugin"],"sources":["../src/plugin.ts"],"sourcesContent":["/**\n * withDotenvx() — Next.js config wrapper that
|
|
1
|
+
{"version":3,"file":"plugin.cjs","names":["path","fs","dotenvx","DotenvxWebpackPlugin"],"sources":["../src/plugin.ts"],"sourcesContent":["/**\n * withDotenvx() — Next.js config wrapper that loads dotenvx secrets at build time\n * and inlines them into the webpack/turbopack runtime so they are available before\n * any user code evaluates (Prisma Pool, etc.) at module-load time.\n *\n * Strategy:\n * - At build time, call dotenvx.config() to decrypt env files and capture the\n * resolved key-value pairs.\n * - Webpack builds: prepend Object.assign(process.env, {...}) into webpack-runtime.js\n * via DotenvxWebpackPlugin (processAssets hook at PROCESS_ASSETS_STAGE_ADDITIONS).\n * - Turbopack builds: patch fs.writeFile to detect compilation completion and inject\n * the same snippet into [turbopack]_runtime.js files.\n * - Also aliases @next/env → @fantasticfour/dotenvx-next/next-env so dotenvx\n * secrets are available during Next.js config evaluation itself.\n */\n\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport type { NextConfig } from 'next';\nimport dotenvx from '@dotenvx/dotenvx';\nimport { DotenvxWebpackPlugin } from './webpack-plugin.js';\nimport { activateTurbopackInjection } from './turbopack-inject.js';\n\nexport interface DotenvxNextOptions {\n /**\n * Env files to load. Defaults to ['.env'], filtered to files that actually\n * exist in envDir.\n */\n files?: string[];\n\n /**\n * Directory where env files live. Defaults to process.cwd().\n */\n envDir?: string;\n}\n\nconst DEFAULT_ENV_FILES = ['.env'];\n\nconst PLUGIN_DEBUG = !!process.env.DEBUG_DOTENVX_NEXT;\n\nfunction debugLog(...args: unknown[]): void {\n if (!PLUGIN_DEBUG) return;\n console.log('[dotenvx-next]', ...args);\n}\n\nfunction resolveExistingFiles(files: string[], envDir: string): string[] {\n return files\n .map((f) => path.resolve(envDir, f))\n .filter((absPath) => fs.existsSync(absPath));\n}\n\ntype NextConfigFn = (\n phase: string,\n defaults: { defaultConfig: NextConfig }\n) => NextConfig | Promise<NextConfig>;\n\nasync function dotenvxNextConfigFn(\n nextConfig: NextConfig | NextConfigFn,\n options: DotenvxNextOptions,\n phase: string,\n defaults: { defaultConfig: NextConfig }\n): Promise<NextConfig> {\n let resolvedNextConfig: NextConfig;\n\n if (typeof nextConfig === 'function') {\n resolvedNextConfig = {\n ...(await (nextConfig as NextConfigFn)(phase, defaults)),\n };\n } else {\n // Shallow-copy so we never mutate the frozen original config object\n resolvedNextConfig = { ...nextConfig };\n }\n\n const envDir = options.envDir ?? process.cwd();\n const fileNames = options.files ?? DEFAULT_ENV_FILES;\n const resolvedFiles = resolveExistingFiles(fileNames, envDir);\n\n // Detect turbopack (Next 16+ uses it by default in dev)\n const isTurbopack = !!(\n process.env.TURBOPACK ||\n process.env.TURBOPACK_DEV ||\n process.env.TURBOPACK_BUILD ||\n process.env.npm_config_turbopack\n );\n\n // Decrypt and resolve env values at build time.\n // parsed contains only the keys from the specified files (not all of process.env).\n const { parsed: env = {} } = resolvedFiles.length\n ? dotenvx.config({ path: resolvedFiles, overload: true, quiet: true })\n : { parsed: {} };\n\n debugLog(\n `phase=${phase}, isTurbopack=${isTurbopack}, resolvedFiles=${JSON.stringify(resolvedFiles)}, envKeys=${Object.keys(env).join(',')}`\n );\n\n if (isTurbopack) {\n activateTurbopackInjection(env);\n }\n\n // Extend webpack config (used for non-turbopack builds and edge runtime)\n const prevWebpack = resolvedNextConfig.webpack;\n\n resolvedNextConfig.webpack = (webpackConfig, webpackOptions) => {\n // Apply user's existing webpack customizations first\n // eslint-disable-next-line @typescript-eslint/no-unsafe-call\n const config = prevWebpack\n ? prevWebpack(webpackConfig, webpackOptions)\n : webpackConfig;\n\n // Alias @next/env to our drop-in replacement so dotenvx.config() is called\n // during Next's own env loading phase\n config.resolve ??= {};\n (config.resolve as Record<string, unknown>).alias ??= {};\n const alias = (config.resolve as Record<string, unknown>).alias as Record<\n string,\n string\n >;\n\n // Use require.resolve so this works from the dist directory\n try {\n alias['@next/env'] = require.resolve(\n '@fantasticfour/dotenvx-next/next-env'\n );\n } catch {\n // If the package isn't installed under that name (e.g. local dev), skip alias\n debugLog(\n 'Could not resolve @fantasticfour/dotenvx-next/next-env for alias — skipping'\n );\n }\n\n // Register the webpack plugin to inject the env snippet into runtime files\n if (!isTurbopack) {\n (config.plugins as unknown[]).push(new DotenvxWebpackPlugin({ env }));\n }\n\n return config;\n };\n\n return resolvedNextConfig;\n}\n\nexport function withDotenvx(\n nextConfig: NextConfig | NextConfigFn,\n options: DotenvxNextOptions = {}\n): NextConfigFn {\n return (phase: string, defaults: { defaultConfig: NextConfig }) =>\n dotenvxNextConfigFn(nextConfig, options, phase, defaults);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,MAAM,oBAAoB,CAAC,OAAO;AAElC,MAAM,eAAe,CAAC,CAAC,QAAQ,IAAI;AAEnC,SAAS,SAAS,GAAG,MAAuB;AAC1C,KAAI,CAAC,aAAc;AACnB,SAAQ,IAAI,kBAAkB,GAAG,KAAK;;AAGxC,SAAS,qBAAqB,OAAiB,QAA0B;AACvE,QAAO,MACJ,KAAK,MAAMA,UAAAA,QAAK,QAAQ,QAAQ,EAAE,CAAC,CACnC,QAAQ,YAAYC,QAAAA,QAAG,WAAW,QAAQ,CAAC;;AAQhD,eAAe,oBACb,YACA,SACA,OACA,UACqB;CACrB,IAAI;AAEJ,KAAI,OAAO,eAAe,WACxB,sBAAqB,EACnB,GAAI,MAAO,WAA4B,OAAO,SAAS,EACxD;KAGD,sBAAqB,EAAE,GAAG,YAAY;CAGxC,MAAM,SAAS,QAAQ,UAAU,QAAQ,KAAK;CAE9C,MAAM,gBAAgB,qBADJ,QAAQ,SAAS,mBACmB,OAAO;CAG7D,MAAM,cAAc,CAAC,EACnB,QAAQ,IAAI,aACZ,QAAQ,IAAI,iBACZ,QAAQ,IAAI,mBACZ,QAAQ,IAAI;CAKd,MAAM,EAAE,QAAQ,MAAM,EAAE,KAAK,cAAc,SACvCC,iBAAAA,QAAQ,OAAO;EAAE,MAAM;EAAe,UAAU;EAAM,OAAO;EAAM,CAAC,GACpE,EAAE,QAAQ,EAAE,EAAE;AAElB,UACE,SAAS,MAAM,gBAAgB,YAAY,kBAAkB,KAAK,UAAU,cAAc,CAAC,YAAY,OAAO,KAAK,IAAI,CAAC,KAAK,IAAI,GAClI;AAED,KAAI,YACF,0BAAA,2BAA2B,IAAI;CAIjC,MAAM,cAAc,mBAAmB;AAEvC,oBAAmB,WAAW,eAAe,mBAAmB;EAG9D,MAAM,SAAS,cACX,YAAY,eAAe,eAAe,GAC1C;AAIJ,SAAO,YAAY,EAAE;AACpB,SAAO,QAAoC,UAAU,EAAE;EACxD,MAAM,QAAS,OAAO,QAAoC;AAM1D,MAAI;AACF,SAAM,eAAe,QAAQ,QAC3B,uCACD;UACK;AAEN,YACE,8EACD;;AAIH,MAAI,CAAC,YACF,QAAO,QAAsB,KAAK,IAAIC,uBAAAA,qBAAqB,EAAE,KAAK,CAAC,CAAC;AAGvE,SAAO;;AAGT,QAAO;;AAGT,SAAgB,YACd,YACA,UAA8B,EAAE,EAClB;AACd,SAAQ,OAAe,aACrB,oBAAoB,YAAY,SAAS,OAAO,SAAS"}
|
package/dist/plugin.d.cts
CHANGED
|
@@ -3,19 +3,14 @@ import { NextConfig } from "next";
|
|
|
3
3
|
//#region src/plugin.d.ts
|
|
4
4
|
interface DotenvxNextOptions {
|
|
5
5
|
/**
|
|
6
|
-
* Env files to load. Defaults to ['.env
|
|
7
|
-
*
|
|
6
|
+
* Env files to load. Defaults to ['.env'], filtered to files that actually
|
|
7
|
+
* exist in envDir.
|
|
8
8
|
*/
|
|
9
9
|
files?: string[];
|
|
10
10
|
/**
|
|
11
11
|
* Directory where env files live. Defaults to process.cwd().
|
|
12
12
|
*/
|
|
13
13
|
envDir?: string;
|
|
14
|
-
/**
|
|
15
|
-
* Add resolved env files to outputFileTracingIncludes so Vercel traces them
|
|
16
|
-
* into the serverless function bundle. Default: true.
|
|
17
|
-
*/
|
|
18
|
-
traceEnvFiles?: boolean;
|
|
19
14
|
}
|
|
20
15
|
type NextConfigFn = (phase: string, defaults: {
|
|
21
16
|
defaultConfig: NextConfig;
|
package/dist/plugin.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.cts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"plugin.d.cts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;UAuBiB,kBAAA;EA6Bf;;;;EAxBA,KAAA;EA0BgB;;;EArBhB,MAAA;AAAA;AAAA,KAkBG,YAAA,IACH,KAAA,UACA,QAAA;EAAY,aAAA,EAAe,UAAA;AAAA,MACxB,UAAA,GAAa,OAAA,CAAQ,UAAA;AAAA,iBAuFV,WAAA,CACd,UAAA,EAAY,UAAA,GAAa,YAAA,EACzB,OAAA,GAAS,kBAAA,GACR,YAAA"}
|
package/dist/plugin.d.mts
CHANGED
|
@@ -3,19 +3,14 @@ import { NextConfig } from "next";
|
|
|
3
3
|
//#region src/plugin.d.ts
|
|
4
4
|
interface DotenvxNextOptions {
|
|
5
5
|
/**
|
|
6
|
-
* Env files to load. Defaults to ['.env
|
|
7
|
-
*
|
|
6
|
+
* Env files to load. Defaults to ['.env'], filtered to files that actually
|
|
7
|
+
* exist in envDir.
|
|
8
8
|
*/
|
|
9
9
|
files?: string[];
|
|
10
10
|
/**
|
|
11
11
|
* Directory where env files live. Defaults to process.cwd().
|
|
12
12
|
*/
|
|
13
13
|
envDir?: string;
|
|
14
|
-
/**
|
|
15
|
-
* Add resolved env files to outputFileTracingIncludes so Vercel traces them
|
|
16
|
-
* into the serverless function bundle. Default: true.
|
|
17
|
-
*/
|
|
18
|
-
traceEnvFiles?: boolean;
|
|
19
14
|
}
|
|
20
15
|
type NextConfigFn = (phase: string, defaults: {
|
|
21
16
|
defaultConfig: NextConfig;
|
package/dist/plugin.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.mts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"plugin.d.mts","names":[],"sources":["../src/plugin.ts"],"mappings":";;;UAuBiB,kBAAA;EA6Bf;;;;EAxBA,KAAA;EA0BgB;;;EArBhB,MAAA;AAAA;AAAA,KAkBG,YAAA,IACH,KAAA,UACA,QAAA;EAAY,aAAA,EAAe,UAAA;AAAA,MACxB,UAAA,GAAa,OAAA,CAAQ,UAAA;AAAA,iBAuFV,WAAA,CACd,UAAA,EAAY,UAAA,GAAa,YAAA,EACzB,OAAA,GAAS,kBAAA,GACR,YAAA"}
|
package/dist/plugin.mjs
CHANGED
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DotenvxWebpackPlugin } from "./webpack-plugin.mjs";
|
|
1
|
+
import { n as __require, t as DotenvxWebpackPlugin } from "./webpack-plugin-BmC1uBZp.mjs";
|
|
3
2
|
import { activateTurbopackInjection } from "./turbopack-inject.mjs";
|
|
4
3
|
import fs from "node:fs";
|
|
5
4
|
import path from "node:path";
|
|
5
|
+
import dotenvx from "@dotenvx/dotenvx";
|
|
6
6
|
//#region src/plugin.ts
|
|
7
7
|
/**
|
|
8
|
-
* withDotenvx() — Next.js config wrapper that
|
|
9
|
-
*
|
|
8
|
+
* withDotenvx() — Next.js config wrapper that loads dotenvx secrets at build time
|
|
9
|
+
* and inlines them into the webpack/turbopack runtime so they are available before
|
|
10
|
+
* any user code evaluates (Prisma Pool, etc.) at module-load time.
|
|
10
11
|
*
|
|
11
12
|
* Strategy:
|
|
12
|
-
* -
|
|
13
|
-
*
|
|
14
|
-
* -
|
|
15
|
-
*
|
|
13
|
+
* - At build time, call dotenvx.config() to decrypt env files and capture the
|
|
14
|
+
* resolved key-value pairs.
|
|
15
|
+
* - Webpack builds: prepend Object.assign(process.env, {...}) into webpack-runtime.js
|
|
16
|
+
* via DotenvxWebpackPlugin (processAssets hook at PROCESS_ASSETS_STAGE_ADDITIONS).
|
|
17
|
+
* - Turbopack builds: patch fs.writeFile to detect compilation completion and inject
|
|
18
|
+
* the same snippet into [turbopack]_runtime.js files.
|
|
16
19
|
* - Also aliases @next/env → @fantasticfour/dotenvx-next/next-env so dotenvx
|
|
17
20
|
* secrets are available during Next.js config evaluation itself.
|
|
18
21
|
*/
|
|
19
|
-
const DEFAULT_ENV_FILES = [
|
|
20
|
-
".env.base",
|
|
21
|
-
".env.production",
|
|
22
|
-
".env.preview"
|
|
23
|
-
];
|
|
22
|
+
const DEFAULT_ENV_FILES = [".env"];
|
|
24
23
|
const PLUGIN_DEBUG = !!process.env.DEBUG_DOTENVX_NEXT;
|
|
25
24
|
function debugLog(...args) {
|
|
26
25
|
if (!PLUGIN_DEBUG) return;
|
|
@@ -29,40 +28,20 @@ function debugLog(...args) {
|
|
|
29
28
|
function resolveExistingFiles(files, envDir) {
|
|
30
29
|
return files.map((f) => path.resolve(envDir, f)).filter((absPath) => fs.existsSync(absPath));
|
|
31
30
|
}
|
|
32
|
-
/**
|
|
33
|
-
* Load the compiled init bundle source. This is the CJS bundle from dist/init.cjs
|
|
34
|
-
* that will be prepended into webpack/turbopack runtime files.
|
|
35
|
-
*/
|
|
36
|
-
function loadInitSource() {
|
|
37
|
-
try {
|
|
38
|
-
return __require("node:fs").readFileSync(__require.resolve("./init.cjs"), "utf8");
|
|
39
|
-
} catch {
|
|
40
|
-
const pkgRoot = path.resolve(new URL(import.meta.url).pathname, "../../");
|
|
41
|
-
return fs.readFileSync(path.join(pkgRoot, "dist", "init.cjs"), "utf8");
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
31
|
async function dotenvxNextConfigFn(nextConfig, options, phase, defaults) {
|
|
45
32
|
let resolvedNextConfig;
|
|
46
33
|
if (typeof nextConfig === "function") resolvedNextConfig = { ...await nextConfig(phase, defaults) };
|
|
47
34
|
else resolvedNextConfig = { ...nextConfig };
|
|
48
35
|
const envDir = options.envDir ?? process.cwd();
|
|
49
36
|
const resolvedFiles = resolveExistingFiles(options.files ?? DEFAULT_ENV_FILES, envDir);
|
|
50
|
-
const shouldTrace = options.traceEnvFiles !== false;
|
|
51
37
|
const isTurbopack = !!(process.env.TURBOPACK || process.env.TURBOPACK_DEV || process.env.TURBOPACK_BUILD || process.env.npm_config_turbopack);
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
else tracing[catchAll] = resolvedFiles;
|
|
60
|
-
}
|
|
61
|
-
if (isTurbopack) try {
|
|
62
|
-
activateTurbopackInjection(loadInitSource());
|
|
63
|
-
} catch (err) {
|
|
64
|
-
console.warn("[dotenvx-next] Could not load init bundle for turbopack injection:", err);
|
|
65
|
-
}
|
|
38
|
+
const { parsed: env = {} } = resolvedFiles.length ? dotenvx.config({
|
|
39
|
+
path: resolvedFiles,
|
|
40
|
+
overload: true,
|
|
41
|
+
quiet: true
|
|
42
|
+
}) : { parsed: {} };
|
|
43
|
+
debugLog(`phase=${phase}, isTurbopack=${isTurbopack}, resolvedFiles=${JSON.stringify(resolvedFiles)}, envKeys=${Object.keys(env).join(",")}`);
|
|
44
|
+
if (isTurbopack) activateTurbopackInjection(env);
|
|
66
45
|
const prevWebpack = resolvedNextConfig.webpack;
|
|
67
46
|
resolvedNextConfig.webpack = (webpackConfig, webpackOptions) => {
|
|
68
47
|
const config = prevWebpack ? prevWebpack(webpackConfig, webpackOptions) : webpackConfig;
|
|
@@ -74,15 +53,7 @@ async function dotenvxNextConfigFn(nextConfig, options, phase, defaults) {
|
|
|
74
53
|
} catch {
|
|
75
54
|
debugLog("Could not resolve @fantasticfour/dotenvx-next/next-env for alias — skipping");
|
|
76
55
|
}
|
|
77
|
-
if (!isTurbopack)
|
|
78
|
-
const plugin = new DotenvxWebpackPlugin({
|
|
79
|
-
files: resolvedFiles,
|
|
80
|
-
initSource: loadInitSource()
|
|
81
|
-
});
|
|
82
|
-
config.plugins.push(plugin);
|
|
83
|
-
} catch (err) {
|
|
84
|
-
console.warn("[dotenvx-next] Could not load init bundle for webpack injection:", err);
|
|
85
|
-
}
|
|
56
|
+
if (!isTurbopack) config.plugins.push(new DotenvxWebpackPlugin({ env }));
|
|
86
57
|
return config;
|
|
87
58
|
};
|
|
88
59
|
return resolvedNextConfig;
|
package/dist/plugin.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.mjs","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["/**\n * withDotenvx() — Next.js config wrapper that
|
|
1
|
+
{"version":3,"file":"plugin.mjs","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["/**\n * withDotenvx() — Next.js config wrapper that loads dotenvx secrets at build time\n * and inlines them into the webpack/turbopack runtime so they are available before\n * any user code evaluates (Prisma Pool, etc.) at module-load time.\n *\n * Strategy:\n * - At build time, call dotenvx.config() to decrypt env files and capture the\n * resolved key-value pairs.\n * - Webpack builds: prepend Object.assign(process.env, {...}) into webpack-runtime.js\n * via DotenvxWebpackPlugin (processAssets hook at PROCESS_ASSETS_STAGE_ADDITIONS).\n * - Turbopack builds: patch fs.writeFile to detect compilation completion and inject\n * the same snippet into [turbopack]_runtime.js files.\n * - Also aliases @next/env → @fantasticfour/dotenvx-next/next-env so dotenvx\n * secrets are available during Next.js config evaluation itself.\n */\n\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport type { NextConfig } from 'next';\nimport dotenvx from '@dotenvx/dotenvx';\nimport { DotenvxWebpackPlugin } from './webpack-plugin.js';\nimport { activateTurbopackInjection } from './turbopack-inject.js';\n\nexport interface DotenvxNextOptions {\n /**\n * Env files to load. Defaults to ['.env'], filtered to files that actually\n * exist in envDir.\n */\n files?: string[];\n\n /**\n * Directory where env files live. Defaults to process.cwd().\n */\n envDir?: string;\n}\n\nconst DEFAULT_ENV_FILES = ['.env'];\n\nconst PLUGIN_DEBUG = !!process.env.DEBUG_DOTENVX_NEXT;\n\nfunction debugLog(...args: unknown[]): void {\n if (!PLUGIN_DEBUG) return;\n console.log('[dotenvx-next]', ...args);\n}\n\nfunction resolveExistingFiles(files: string[], envDir: string): string[] {\n return files\n .map((f) => path.resolve(envDir, f))\n .filter((absPath) => fs.existsSync(absPath));\n}\n\ntype NextConfigFn = (\n phase: string,\n defaults: { defaultConfig: NextConfig }\n) => NextConfig | Promise<NextConfig>;\n\nasync function dotenvxNextConfigFn(\n nextConfig: NextConfig | NextConfigFn,\n options: DotenvxNextOptions,\n phase: string,\n defaults: { defaultConfig: NextConfig }\n): Promise<NextConfig> {\n let resolvedNextConfig: NextConfig;\n\n if (typeof nextConfig === 'function') {\n resolvedNextConfig = {\n ...(await (nextConfig as NextConfigFn)(phase, defaults)),\n };\n } else {\n // Shallow-copy so we never mutate the frozen original config object\n resolvedNextConfig = { ...nextConfig };\n }\n\n const envDir = options.envDir ?? process.cwd();\n const fileNames = options.files ?? DEFAULT_ENV_FILES;\n const resolvedFiles = resolveExistingFiles(fileNames, envDir);\n\n // Detect turbopack (Next 16+ uses it by default in dev)\n const isTurbopack = !!(\n process.env.TURBOPACK ||\n process.env.TURBOPACK_DEV ||\n process.env.TURBOPACK_BUILD ||\n process.env.npm_config_turbopack\n );\n\n // Decrypt and resolve env values at build time.\n // parsed contains only the keys from the specified files (not all of process.env).\n const { parsed: env = {} } = resolvedFiles.length\n ? dotenvx.config({ path: resolvedFiles, overload: true, quiet: true })\n : { parsed: {} };\n\n debugLog(\n `phase=${phase}, isTurbopack=${isTurbopack}, resolvedFiles=${JSON.stringify(resolvedFiles)}, envKeys=${Object.keys(env).join(',')}`\n );\n\n if (isTurbopack) {\n activateTurbopackInjection(env);\n }\n\n // Extend webpack config (used for non-turbopack builds and edge runtime)\n const prevWebpack = resolvedNextConfig.webpack;\n\n resolvedNextConfig.webpack = (webpackConfig, webpackOptions) => {\n // Apply user's existing webpack customizations first\n // eslint-disable-next-line @typescript-eslint/no-unsafe-call\n const config = prevWebpack\n ? prevWebpack(webpackConfig, webpackOptions)\n : webpackConfig;\n\n // Alias @next/env to our drop-in replacement so dotenvx.config() is called\n // during Next's own env loading phase\n config.resolve ??= {};\n (config.resolve as Record<string, unknown>).alias ??= {};\n const alias = (config.resolve as Record<string, unknown>).alias as Record<\n string,\n string\n >;\n\n // Use require.resolve so this works from the dist directory\n try {\n alias['@next/env'] = require.resolve(\n '@fantasticfour/dotenvx-next/next-env'\n );\n } catch {\n // If the package isn't installed under that name (e.g. local dev), skip alias\n debugLog(\n 'Could not resolve @fantasticfour/dotenvx-next/next-env for alias — skipping'\n );\n }\n\n // Register the webpack plugin to inject the env snippet into runtime files\n if (!isTurbopack) {\n (config.plugins as unknown[]).push(new DotenvxWebpackPlugin({ env }));\n }\n\n return config;\n };\n\n return resolvedNextConfig;\n}\n\nexport function withDotenvx(\n nextConfig: NextConfig | NextConfigFn,\n options: DotenvxNextOptions = {}\n): NextConfigFn {\n return (phase: string, defaults: { defaultConfig: NextConfig }) =>\n dotenvxNextConfigFn(nextConfig, options, phase, defaults);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoCA,MAAM,oBAAoB,CAAC,OAAO;AAElC,MAAM,eAAe,CAAC,CAAC,QAAQ,IAAI;AAEnC,SAAS,SAAS,GAAG,MAAuB;AAC1C,KAAI,CAAC,aAAc;AACnB,SAAQ,IAAI,kBAAkB,GAAG,KAAK;;AAGxC,SAAS,qBAAqB,OAAiB,QAA0B;AACvE,QAAO,MACJ,KAAK,MAAM,KAAK,QAAQ,QAAQ,EAAE,CAAC,CACnC,QAAQ,YAAY,GAAG,WAAW,QAAQ,CAAC;;AAQhD,eAAe,oBACb,YACA,SACA,OACA,UACqB;CACrB,IAAI;AAEJ,KAAI,OAAO,eAAe,WACxB,sBAAqB,EACnB,GAAI,MAAO,WAA4B,OAAO,SAAS,EACxD;KAGD,sBAAqB,EAAE,GAAG,YAAY;CAGxC,MAAM,SAAS,QAAQ,UAAU,QAAQ,KAAK;CAE9C,MAAM,gBAAgB,qBADJ,QAAQ,SAAS,mBACmB,OAAO;CAG7D,MAAM,cAAc,CAAC,EACnB,QAAQ,IAAI,aACZ,QAAQ,IAAI,iBACZ,QAAQ,IAAI,mBACZ,QAAQ,IAAI;CAKd,MAAM,EAAE,QAAQ,MAAM,EAAE,KAAK,cAAc,SACvC,QAAQ,OAAO;EAAE,MAAM;EAAe,UAAU;EAAM,OAAO;EAAM,CAAC,GACpE,EAAE,QAAQ,EAAE,EAAE;AAElB,UACE,SAAS,MAAM,gBAAgB,YAAY,kBAAkB,KAAK,UAAU,cAAc,CAAC,YAAY,OAAO,KAAK,IAAI,CAAC,KAAK,IAAI,GAClI;AAED,KAAI,YACF,4BAA2B,IAAI;CAIjC,MAAM,cAAc,mBAAmB;AAEvC,oBAAmB,WAAW,eAAe,mBAAmB;EAG9D,MAAM,SAAS,cACX,YAAY,eAAe,eAAe,GAC1C;AAIJ,SAAO,YAAY,EAAE;AACpB,SAAO,QAAoC,UAAU,EAAE;EACxD,MAAM,QAAS,OAAO,QAAoC;AAM1D,MAAI;AACF,SAAM,eAAA,UAAuB,QAC3B,uCACD;UACK;AAEN,YACE,8EACD;;AAIH,MAAI,CAAC,YACF,QAAO,QAAsB,KAAK,IAAI,qBAAqB,EAAE,KAAK,CAAC,CAAC;AAGvE,SAAO;;AAGT,QAAO;;AAGT,SAAgB,YACd,YACA,UAA8B,EAAE,EAClB;AACd,SAAQ,OAAe,aACrB,oBAAoB,YAAY,SAAS,OAAO,SAAS"}
|
|
@@ -27,7 +27,7 @@ node_path = __toESM(node_path, 1);
|
|
|
27
27
|
//#region src/turbopack-inject.ts
|
|
28
28
|
/**
|
|
29
29
|
* Turbopack runtime injection — patches fs.writeFile/fs.writeFileSync to intercept
|
|
30
|
-
* turbopack's build output and prepend the
|
|
30
|
+
* turbopack's build output and prepend the env inline snippet into runtime files.
|
|
31
31
|
*
|
|
32
32
|
* Turbopack writes `[turbopack]_runtime.js` directly (bypassing webpack), so we
|
|
33
33
|
* can't use the processAssets hook. Instead we intercept fs writes and inject when
|
|
@@ -42,10 +42,10 @@ function isInjectedTurbopackRuntime() {
|
|
|
42
42
|
return injectedTurbopackRuntime;
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
|
-
* Inject the
|
|
45
|
+
* Inject the env inline snippet into turbopack runtime files.
|
|
46
46
|
* Called after export-detail.json is written (turbopack compilation complete).
|
|
47
47
|
*/
|
|
48
|
-
function injectDotenvxInitIntoTurbopackRuntime(nextDirPath,
|
|
48
|
+
function injectDotenvxInitIntoTurbopackRuntime(nextDirPath, env) {
|
|
49
49
|
if (injectedTurbopackRuntime) return;
|
|
50
50
|
const serverRuntimeFiles = [];
|
|
51
51
|
const edgeWrapperFiles = [];
|
|
@@ -59,11 +59,9 @@ function injectDotenvxInitIntoTurbopackRuntime(nextDirPath, initSource) {
|
|
|
59
59
|
debugLog(`turbopack runtime injection: found ${serverRuntimeFiles.length} server runtime files,`, `${edgeWrapperFiles.length} edge wrapper files`);
|
|
60
60
|
if (!serverRuntimeFiles.length) return;
|
|
61
61
|
injectedTurbopackRuntime = true;
|
|
62
|
-
const
|
|
63
|
-
const iifeWrap = (src) => `(function(exports,module){${src}\n})({},{exports:{}});`;
|
|
64
|
-
const wrappedInit = iifeWrap(strippedSource);
|
|
62
|
+
const inlineSnippet = `(function(){if(typeof process!=='undefined'){Object.assign(process.env,${JSON.stringify(env)});}})();`;
|
|
65
63
|
/**
|
|
66
|
-
* Insert
|
|
64
|
+
* Insert inlineSnippet into source respecting ESM constraints.
|
|
67
65
|
* ESM files require all top-level `import` declarations to appear before any
|
|
68
66
|
* other statements, so we must insert after the last import line rather than
|
|
69
67
|
* prepending to the file.
|
|
@@ -73,40 +71,40 @@ function injectDotenvxInitIntoTurbopackRuntime(nextDirPath, initSource) {
|
|
|
73
71
|
let lastImportIdx = -1;
|
|
74
72
|
for (let i = 0; i < lines.length; i++) if (/^import\s/.test(lines[i])) lastImportIdx = i;
|
|
75
73
|
if (lastImportIdx >= 0) {
|
|
76
|
-
lines.splice(lastImportIdx + 1, 0,
|
|
74
|
+
lines.splice(lastImportIdx + 1, 0, inlineSnippet);
|
|
77
75
|
return lines.join("\n");
|
|
78
76
|
}
|
|
79
|
-
return [
|
|
77
|
+
return [inlineSnippet, origSource].join("\n");
|
|
80
78
|
};
|
|
81
79
|
for (const runtimeFile of serverRuntimeFiles) {
|
|
82
80
|
const origSource = node_fs.default.readFileSync(runtimeFile, "utf8");
|
|
83
81
|
node_fs.default.writeFileSync(runtimeFile, insertIntoSource(origSource));
|
|
84
|
-
debugLog(`injected
|
|
82
|
+
debugLog(`injected env into turbopack server runtime: ${runtimeFile}`);
|
|
85
83
|
}
|
|
86
84
|
for (const wrapperFile of edgeWrapperFiles) {
|
|
87
85
|
const origSource = node_fs.default.readFileSync(wrapperFile, "utf8");
|
|
88
86
|
node_fs.default.writeFileSync(wrapperFile, insertIntoSource(origSource));
|
|
89
|
-
debugLog(`injected
|
|
87
|
+
debugLog(`injected env into turbopack edge wrapper: ${wrapperFile}`);
|
|
90
88
|
}
|
|
91
89
|
}
|
|
92
90
|
/**
|
|
93
91
|
* Patch global fs methods to detect when turbopack compilation completes,
|
|
94
92
|
* then trigger runtime injection.
|
|
95
93
|
*/
|
|
96
|
-
function activateTurbopackInjection(
|
|
94
|
+
function activateTurbopackInjection(env) {
|
|
97
95
|
debugLog("activating turbopack fs intercept");
|
|
98
96
|
const origWriteFileFn = node_fs.default.promises.writeFile;
|
|
99
97
|
node_fs.default.promises.writeFile = async function dotenvxPatchedWriteFile(...args) {
|
|
100
98
|
const filePath = args[0].toString();
|
|
101
99
|
debugLog("fs.promises.writeFile:", filePath);
|
|
102
|
-
if (!injectedTurbopackRuntime && filePath.endsWith("/.next/export-detail.json")) injectDotenvxInitIntoTurbopackRuntime(filePath.substring(0, filePath.lastIndexOf("/")),
|
|
100
|
+
if (!injectedTurbopackRuntime && filePath.endsWith("/.next/export-detail.json")) injectDotenvxInitIntoTurbopackRuntime(filePath.substring(0, filePath.lastIndexOf("/")), env);
|
|
103
101
|
return origWriteFileFn.apply(node_fs.default.promises, args);
|
|
104
102
|
};
|
|
105
103
|
const origWriteFileSyncFn = node_fs.default.writeFileSync;
|
|
106
104
|
node_fs.default.writeFileSync = function dotenvxPatchedWriteFileSync(...args) {
|
|
107
105
|
const filePath = args[0].toString();
|
|
108
106
|
debugLog("fs.writeFileSync:", filePath);
|
|
109
|
-
if (!injectedTurbopackRuntime && filePath.endsWith("/.next/export-detail.json")) injectDotenvxInitIntoTurbopackRuntime(filePath.substring(0, filePath.lastIndexOf("/")),
|
|
107
|
+
if (!injectedTurbopackRuntime && filePath.endsWith("/.next/export-detail.json")) injectDotenvxInitIntoTurbopackRuntime(filePath.substring(0, filePath.lastIndexOf("/")), env);
|
|
110
108
|
origWriteFileSyncFn.apply(node_fs.default, args);
|
|
111
109
|
};
|
|
112
110
|
}
|
|
@@ -136,4 +134,4 @@ Object.defineProperty(exports, "isInjectedTurbopackRuntime", {
|
|
|
136
134
|
}
|
|
137
135
|
});
|
|
138
136
|
|
|
139
|
-
//# sourceMappingURL=turbopack-inject-
|
|
137
|
+
//# sourceMappingURL=turbopack-inject-BElH61NN.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"turbopack-inject-BElH61NN.cjs","names":["fs","path"],"sources":["../src/turbopack-inject.ts"],"sourcesContent":["/**\n * Turbopack runtime injection — patches fs.writeFile/fs.writeFileSync to intercept\n * turbopack's build output and prepend the env inline snippet into runtime files.\n *\n * Turbopack writes `[turbopack]_runtime.js` directly (bypassing webpack), so we\n * can't use the processAssets hook. Instead we intercept fs writes and inject when\n * export-detail.json is written (signals compilation is complete).\n */\n\nimport fs from 'node:fs';\nimport path from 'node:path';\n\nfunction debugLog(...args: unknown[]): void {\n if (!process.env.DEBUG_DOTENVX_NEXT) return;\n console.log('[dotenvx-next]', ...args);\n}\n\nlet injectedTurbopackRuntime = false;\n\nexport function isInjectedTurbopackRuntime(): boolean {\n return injectedTurbopackRuntime;\n}\n\n/**\n * Inject the env inline snippet into turbopack runtime files.\n * Called after export-detail.json is written (turbopack compilation complete).\n */\nexport function injectDotenvxInitIntoTurbopackRuntime(\n nextDirPath: string,\n env: Record<string, string>\n): void {\n if (injectedTurbopackRuntime) return;\n\n const serverRuntimeFiles: string[] = [];\n const edgeWrapperFiles: string[] = [];\n\n const walkDir = (dir: string): void => {\n if (!fs.existsSync(dir)) return;\n for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {\n if (entry.isDirectory()) {\n walkDir(path.join(dir, entry.name));\n } else if (entry.name === '[turbopack]_runtime.js') {\n serverRuntimeFiles.push(path.join(dir, entry.name));\n } else if (\n entry.name.includes('edge-wrapper') &&\n entry.name.endsWith('.js')\n ) {\n edgeWrapperFiles.push(path.join(dir, entry.name));\n }\n }\n };\n\n walkDir(nextDirPath);\n\n debugLog(\n `turbopack runtime injection: found ${serverRuntimeFiles.length} server runtime files,`,\n `${edgeWrapperFiles.length} edge wrapper files`\n );\n\n if (!serverRuntimeFiles.length) {\n // Runtime files may not exist yet — turbopack (Rust) writes them directly.\n // We'll retry on subsequent fs writes until they appear.\n return;\n }\n\n // Mark as done so we don't retry\n injectedTurbopackRuntime = true;\n\n // Inline snippet: assign build-time-resolved values into process.env at runtime.\n const inlineSnippet = `(function(){if(typeof process!=='undefined'){Object.assign(process.env,${JSON.stringify(env)});}})();`;\n\n /**\n * Insert inlineSnippet into source respecting ESM constraints.\n * ESM files require all top-level `import` declarations to appear before any\n * other statements, so we must insert after the last import line rather than\n * prepending to the file.\n */\n const insertIntoSource = (origSource: string): string => {\n const lines = origSource.split('\\n');\n // Find the last line that starts with 'import ' (top-level import declaration)\n let lastImportIdx = -1;\n for (let i = 0; i < lines.length; i++) {\n if (/^import\\s/.test(lines[i])) {\n lastImportIdx = i;\n }\n }\n if (lastImportIdx >= 0) {\n // Insert after the last import line\n lines.splice(lastImportIdx + 1, 0, inlineSnippet);\n return lines.join('\\n');\n }\n // CJS or no imports — prepend\n return [inlineSnippet, origSource].join('\\n');\n };\n\n for (const runtimeFile of serverRuntimeFiles) {\n const origSource = fs.readFileSync(runtimeFile, 'utf8');\n fs.writeFileSync(runtimeFile, insertIntoSource(origSource));\n debugLog(`injected env into turbopack server runtime: ${runtimeFile}`);\n }\n\n for (const wrapperFile of edgeWrapperFiles) {\n const origSource = fs.readFileSync(wrapperFile, 'utf8');\n fs.writeFileSync(wrapperFile, insertIntoSource(origSource));\n debugLog(`injected env into turbopack edge wrapper: ${wrapperFile}`);\n }\n}\n\n/**\n * Patch global fs methods to detect when turbopack compilation completes,\n * then trigger runtime injection.\n */\nexport function activateTurbopackInjection(env: Record<string, string>): void {\n debugLog('activating turbopack fs intercept');\n\n const origWriteFileFn = fs.promises.writeFile;\n fs.promises.writeFile = async function dotenvxPatchedWriteFile(\n ...args: Parameters<typeof fs.promises.writeFile>\n ): ReturnType<typeof fs.promises.writeFile> {\n const filePath = args[0].toString();\n debugLog('fs.promises.writeFile:', filePath);\n\n if (\n !injectedTurbopackRuntime &&\n filePath.endsWith('/.next/export-detail.json')\n ) {\n const nextDirPath = filePath.substring(0, filePath.lastIndexOf('/'));\n injectDotenvxInitIntoTurbopackRuntime(nextDirPath, env);\n }\n\n return origWriteFileFn.apply(fs.promises, args);\n };\n\n const origWriteFileSyncFn = fs.writeFileSync;\n fs.writeFileSync = function dotenvxPatchedWriteFileSync(\n ...args: Parameters<typeof fs.writeFileSync>\n ): void {\n const filePath = args[0].toString();\n debugLog('fs.writeFileSync:', filePath);\n\n if (\n !injectedTurbopackRuntime &&\n filePath.endsWith('/.next/export-detail.json')\n ) {\n const nextDirPath = filePath.substring(0, filePath.lastIndexOf('/'));\n injectDotenvxInitIntoTurbopackRuntime(nextDirPath, env);\n }\n\n origWriteFileSyncFn.apply(fs, args);\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYA,SAAS,SAAS,GAAG,MAAuB;AAC1C,KAAI,CAAC,QAAQ,IAAI,mBAAoB;AACrC,SAAQ,IAAI,kBAAkB,GAAG,KAAK;;AAGxC,IAAI,2BAA2B;AAE/B,SAAgB,6BAAsC;AACpD,QAAO;;;;;;AAOT,SAAgB,sCACd,aACA,KACM;AACN,KAAI,yBAA0B;CAE9B,MAAM,qBAA+B,EAAE;CACvC,MAAM,mBAA6B,EAAE;CAErC,MAAM,WAAW,QAAsB;AACrC,MAAI,CAACA,QAAAA,QAAG,WAAW,IAAI,CAAE;AACzB,OAAK,MAAM,SAASA,QAAAA,QAAG,YAAY,KAAK,EAAE,eAAe,MAAM,CAAC,CAC9D,KAAI,MAAM,aAAa,CACrB,SAAQC,UAAAA,QAAK,KAAK,KAAK,MAAM,KAAK,CAAC;WAC1B,MAAM,SAAS,yBACxB,oBAAmB,KAAKA,UAAAA,QAAK,KAAK,KAAK,MAAM,KAAK,CAAC;WAEnD,MAAM,KAAK,SAAS,eAAe,IACnC,MAAM,KAAK,SAAS,MAAM,CAE1B,kBAAiB,KAAKA,UAAAA,QAAK,KAAK,KAAK,MAAM,KAAK,CAAC;;AAKvD,SAAQ,YAAY;AAEpB,UACE,sCAAsC,mBAAmB,OAAO,yBAChE,GAAG,iBAAiB,OAAO,qBAC5B;AAED,KAAI,CAAC,mBAAmB,OAGtB;AAIF,4BAA2B;CAG3B,MAAM,gBAAgB,0EAA0E,KAAK,UAAU,IAAI,CAAC;;;;;;;CAQpH,MAAM,oBAAoB,eAA+B;EACvD,MAAM,QAAQ,WAAW,MAAM,KAAK;EAEpC,IAAI,gBAAgB;AACpB,OAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,IAChC,KAAI,YAAY,KAAK,MAAM,GAAG,CAC5B,iBAAgB;AAGpB,MAAI,iBAAiB,GAAG;AAEtB,SAAM,OAAO,gBAAgB,GAAG,GAAG,cAAc;AACjD,UAAO,MAAM,KAAK,KAAK;;AAGzB,SAAO,CAAC,eAAe,WAAW,CAAC,KAAK,KAAK;;AAG/C,MAAK,MAAM,eAAe,oBAAoB;EAC5C,MAAM,aAAaD,QAAAA,QAAG,aAAa,aAAa,OAAO;AACvD,UAAA,QAAG,cAAc,aAAa,iBAAiB,WAAW,CAAC;AAC3D,WAAS,+CAA+C,cAAc;;AAGxE,MAAK,MAAM,eAAe,kBAAkB;EAC1C,MAAM,aAAaA,QAAAA,QAAG,aAAa,aAAa,OAAO;AACvD,UAAA,QAAG,cAAc,aAAa,iBAAiB,WAAW,CAAC;AAC3D,WAAS,6CAA6C,cAAc;;;;;;;AAQxE,SAAgB,2BAA2B,KAAmC;AAC5E,UAAS,oCAAoC;CAE7C,MAAM,kBAAkBA,QAAAA,QAAG,SAAS;AACpC,SAAA,QAAG,SAAS,YAAY,eAAe,wBACrC,GAAG,MACuC;EAC1C,MAAM,WAAW,KAAK,GAAG,UAAU;AACnC,WAAS,0BAA0B,SAAS;AAE5C,MACE,CAAC,4BACD,SAAS,SAAS,4BAA4B,CAG9C,uCADoB,SAAS,UAAU,GAAG,SAAS,YAAY,IAAI,CAClB,EAAE,IAAI;AAGzD,SAAO,gBAAgB,MAAMA,QAAAA,QAAG,UAAU,KAAK;;CAGjD,MAAM,sBAAsBA,QAAAA,QAAG;AAC/B,SAAA,QAAA,gBAAmB,SAAS,4BAC1B,GAAG,MACG;EACN,MAAM,WAAW,KAAK,GAAG,UAAU;AACnC,WAAS,qBAAqB,SAAS;AAEvC,MACE,CAAC,4BACD,SAAS,SAAS,4BAA4B,CAG9C,uCADoB,SAAS,UAAU,GAAG,SAAS,YAAY,IAAI,CAClB,EAAE,IAAI;AAGzD,sBAAoB,MAAMA,QAAAA,SAAI,KAAK"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_turbopack_inject = require("./turbopack-inject-
|
|
2
|
+
const require_turbopack_inject = require("./turbopack-inject-BElH61NN.cjs");
|
|
3
3
|
exports.activateTurbopackInjection = require_turbopack_inject.activateTurbopackInjection;
|
|
4
4
|
exports.injectDotenvxInitIntoTurbopackRuntime = require_turbopack_inject.injectDotenvxInitIntoTurbopackRuntime;
|
|
5
5
|
exports.isInjectedTurbopackRuntime = require_turbopack_inject.isInjectedTurbopackRuntime;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//#region src/turbopack-inject.d.ts
|
|
2
2
|
/**
|
|
3
3
|
* Turbopack runtime injection — patches fs.writeFile/fs.writeFileSync to intercept
|
|
4
|
-
* turbopack's build output and prepend the
|
|
4
|
+
* turbopack's build output and prepend the env inline snippet into runtime files.
|
|
5
5
|
*
|
|
6
6
|
* Turbopack writes `[turbopack]_runtime.js` directly (bypassing webpack), so we
|
|
7
7
|
* can't use the processAssets hook. Instead we intercept fs writes and inject when
|
|
@@ -9,15 +9,15 @@
|
|
|
9
9
|
*/
|
|
10
10
|
declare function isInjectedTurbopackRuntime(): boolean;
|
|
11
11
|
/**
|
|
12
|
-
* Inject the
|
|
12
|
+
* Inject the env inline snippet into turbopack runtime files.
|
|
13
13
|
* Called after export-detail.json is written (turbopack compilation complete).
|
|
14
14
|
*/
|
|
15
|
-
declare function injectDotenvxInitIntoTurbopackRuntime(nextDirPath: string,
|
|
15
|
+
declare function injectDotenvxInitIntoTurbopackRuntime(nextDirPath: string, env: Record<string, string>): void;
|
|
16
16
|
/**
|
|
17
17
|
* Patch global fs methods to detect when turbopack compilation completes,
|
|
18
18
|
* then trigger runtime injection.
|
|
19
19
|
*/
|
|
20
|
-
declare function activateTurbopackInjection(
|
|
20
|
+
declare function activateTurbopackInjection(env: Record<string, string>): void;
|
|
21
21
|
//#endregion
|
|
22
22
|
export { activateTurbopackInjection, injectDotenvxInitIntoTurbopackRuntime, isInjectedTurbopackRuntime };
|
|
23
23
|
//# sourceMappingURL=turbopack-inject.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"turbopack-inject.d.cts","names":[],"sources":["../src/turbopack-inject.ts"],"mappings":";;AAmBA;;;;;AAQA;;iBARgB,0BAAA,CAAA
|
|
1
|
+
{"version":3,"file":"turbopack-inject.d.cts","names":[],"sources":["../src/turbopack-inject.ts"],"mappings":";;AAmBA;;;;;AAQA;;iBARgB,0BAAA,CAAA;;;;;iBAQA,qCAAA,CACd,WAAA,UACA,GAAA,EAAK,MAAA;;AAmFP;;;iBAAgB,0BAAA,CAA2B,GAAA,EAAK,MAAA"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//#region src/turbopack-inject.d.ts
|
|
2
2
|
/**
|
|
3
3
|
* Turbopack runtime injection — patches fs.writeFile/fs.writeFileSync to intercept
|
|
4
|
-
* turbopack's build output and prepend the
|
|
4
|
+
* turbopack's build output and prepend the env inline snippet into runtime files.
|
|
5
5
|
*
|
|
6
6
|
* Turbopack writes `[turbopack]_runtime.js` directly (bypassing webpack), so we
|
|
7
7
|
* can't use the processAssets hook. Instead we intercept fs writes and inject when
|
|
@@ -9,15 +9,15 @@
|
|
|
9
9
|
*/
|
|
10
10
|
declare function isInjectedTurbopackRuntime(): boolean;
|
|
11
11
|
/**
|
|
12
|
-
* Inject the
|
|
12
|
+
* Inject the env inline snippet into turbopack runtime files.
|
|
13
13
|
* Called after export-detail.json is written (turbopack compilation complete).
|
|
14
14
|
*/
|
|
15
|
-
declare function injectDotenvxInitIntoTurbopackRuntime(nextDirPath: string,
|
|
15
|
+
declare function injectDotenvxInitIntoTurbopackRuntime(nextDirPath: string, env: Record<string, string>): void;
|
|
16
16
|
/**
|
|
17
17
|
* Patch global fs methods to detect when turbopack compilation completes,
|
|
18
18
|
* then trigger runtime injection.
|
|
19
19
|
*/
|
|
20
|
-
declare function activateTurbopackInjection(
|
|
20
|
+
declare function activateTurbopackInjection(env: Record<string, string>): void;
|
|
21
21
|
//#endregion
|
|
22
22
|
export { activateTurbopackInjection, injectDotenvxInitIntoTurbopackRuntime, isInjectedTurbopackRuntime };
|
|
23
23
|
//# sourceMappingURL=turbopack-inject.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"turbopack-inject.d.mts","names":[],"sources":["../src/turbopack-inject.ts"],"mappings":";;AAmBA;;;;;AAQA;;iBARgB,0BAAA,CAAA
|
|
1
|
+
{"version":3,"file":"turbopack-inject.d.mts","names":[],"sources":["../src/turbopack-inject.ts"],"mappings":";;AAmBA;;;;;AAQA;;iBARgB,0BAAA,CAAA;;;;;iBAQA,qCAAA,CACd,WAAA,UACA,GAAA,EAAK,MAAA;;AAmFP;;;iBAAgB,0BAAA,CAA2B,GAAA,EAAK,MAAA"}
|