bini-env 1.0.6 → 1.0.7
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 +152 -126
- package/dist/index.d.mts +37 -0
- package/dist/index.d.ts +10 -52
- package/dist/index.js +166 -100
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +235 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +31 -19
- package/dist/index.cjs +0 -191
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -79
package/dist/index.js
CHANGED
|
@@ -1,82 +1,125 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var path = require('path');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
9
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
3
10
|
|
|
4
11
|
// src/index.ts
|
|
5
12
|
var BINI_LOGO = "\xDF";
|
|
6
|
-
var DEFAULT_ENV_PREFIX = "BINI_";
|
|
7
|
-
var DEFAULT_OPTIONS = {
|
|
13
|
+
var DEFAULT_ENV_PREFIX = ["BINI_", "VITE_"];
|
|
14
|
+
var DEFAULT_OPTIONS = Object.freeze({
|
|
8
15
|
enabled: true,
|
|
9
16
|
clearViteHeader: true,
|
|
10
17
|
logo: BINI_LOGO,
|
|
11
18
|
envPrefix: DEFAULT_ENV_PREFIX
|
|
12
|
-
};
|
|
13
|
-
var
|
|
14
|
-
CYAN: "\x1B[36m",
|
|
15
|
-
RESET: "\x1B[0m",
|
|
16
|
-
GREEN: "\x1B[32m"
|
|
17
|
-
};
|
|
19
|
+
});
|
|
20
|
+
var envCache = /* @__PURE__ */ new Map();
|
|
18
21
|
function getEnv(key) {
|
|
22
|
+
if (envCache.has(key)) {
|
|
23
|
+
return envCache.get(key);
|
|
24
|
+
}
|
|
25
|
+
let value;
|
|
19
26
|
const deno = globalThis.Deno;
|
|
20
27
|
if (deno) {
|
|
21
28
|
try {
|
|
22
|
-
|
|
23
|
-
if (
|
|
29
|
+
value = deno.env.get(key);
|
|
30
|
+
if (value !== void 0) {
|
|
31
|
+
envCache.set(key, value);
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
24
34
|
} catch {
|
|
25
35
|
}
|
|
26
36
|
}
|
|
27
37
|
if (typeof process !== "undefined" && process.env) {
|
|
28
|
-
|
|
38
|
+
value = process.env[key];
|
|
39
|
+
if (value !== void 0) {
|
|
40
|
+
envCache.set(key, value);
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
29
43
|
}
|
|
30
44
|
try {
|
|
31
|
-
const metaEnv =
|
|
45
|
+
const metaEnv = undefined;
|
|
32
46
|
if (metaEnv) {
|
|
33
|
-
if (metaEnv[key] !== void 0)
|
|
34
|
-
|
|
47
|
+
if (metaEnv[key] !== void 0) {
|
|
48
|
+
envCache.set(key, metaEnv[key]);
|
|
49
|
+
return metaEnv[key];
|
|
50
|
+
}
|
|
51
|
+
const biniKey = `BINI_${key}`;
|
|
52
|
+
if (metaEnv[biniKey] !== void 0) {
|
|
53
|
+
envCache.set(key, metaEnv[biniKey]);
|
|
54
|
+
return metaEnv[biniKey];
|
|
55
|
+
}
|
|
56
|
+
const viteKey = `VITE_${key}`;
|
|
57
|
+
if (metaEnv[viteKey] !== void 0) {
|
|
58
|
+
envCache.set(key, metaEnv[viteKey]);
|
|
59
|
+
return metaEnv[viteKey];
|
|
60
|
+
}
|
|
35
61
|
}
|
|
36
62
|
} catch {
|
|
37
63
|
}
|
|
64
|
+
envCache.set(key, void 0);
|
|
38
65
|
return void 0;
|
|
39
66
|
}
|
|
40
67
|
function requireEnv(key) {
|
|
41
68
|
const val = getEnv(key);
|
|
42
69
|
if (!val) {
|
|
43
|
-
|
|
44
|
-
`[bini-env] Missing required environment variable: "${key}".
|
|
70
|
+
const errorMsg = `[bini-env] Missing required environment variable: "${key}".
|
|
45
71
|
\u2192 In development: add it to your .env file.
|
|
46
|
-
\u2192 In production: add it to your hosting dashboard environment variables
|
|
47
|
-
);
|
|
72
|
+
\u2192 In production: add it to your hosting dashboard environment variables.`;
|
|
73
|
+
throw new Error(errorMsg);
|
|
48
74
|
}
|
|
49
75
|
return val;
|
|
50
76
|
}
|
|
51
77
|
var _envLoaded = false;
|
|
78
|
+
var _loadPromise = null;
|
|
52
79
|
async function loadEnv(projectRoot) {
|
|
53
80
|
if (_envLoaded) return;
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
return;
|
|
81
|
+
if (_loadPromise) {
|
|
82
|
+
return _loadPromise;
|
|
57
83
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
84
|
+
_loadPromise = (async () => {
|
|
85
|
+
try {
|
|
86
|
+
if (globalThis.Deno) {
|
|
87
|
+
_envLoaded = true;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (typeof process === "undefined" || !process.env) {
|
|
91
|
+
_envLoaded = true;
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (process.env.NODE_ENV === "production") {
|
|
95
|
+
_envLoaded = true;
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const root = projectRoot ?? process.cwd();
|
|
99
|
+
const envPath = path__default.default.join(root, ".env");
|
|
100
|
+
try {
|
|
101
|
+
await fs__default.default.promises.access(envPath, fs__default.default.constants.R_OK);
|
|
102
|
+
} catch {
|
|
103
|
+
_envLoaded = true;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const dotenv = await import('dotenv');
|
|
107
|
+
dotenv.config({ path: envPath });
|
|
108
|
+
envCache.clear();
|
|
109
|
+
} catch {
|
|
110
|
+
} finally {
|
|
70
111
|
_envLoaded = true;
|
|
71
|
-
|
|
112
|
+
_loadPromise = null;
|
|
72
113
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
} catch {
|
|
76
|
-
}
|
|
77
|
-
_envLoaded = true;
|
|
114
|
+
})();
|
|
115
|
+
return _loadPromise;
|
|
78
116
|
}
|
|
117
|
+
var _envFilesCache = null;
|
|
118
|
+
var _cachedRoot = "";
|
|
79
119
|
function detectEnvFiles(projectRoot = process.cwd()) {
|
|
120
|
+
if (_envFilesCache && _cachedRoot === projectRoot) {
|
|
121
|
+
return _envFilesCache;
|
|
122
|
+
}
|
|
80
123
|
const nodeEnv = process.env.NODE_ENV ?? "development";
|
|
81
124
|
const candidates = [
|
|
82
125
|
".env.local",
|
|
@@ -86,95 +129,118 @@ function detectEnvFiles(projectRoot = process.cwd()) {
|
|
|
86
129
|
];
|
|
87
130
|
const found = [];
|
|
88
131
|
for (const file of candidates) {
|
|
89
|
-
const filePath =
|
|
90
|
-
|
|
91
|
-
|
|
132
|
+
const filePath = path__default.default.join(projectRoot, file);
|
|
133
|
+
try {
|
|
134
|
+
if (fs__default.default.existsSync(filePath)) {
|
|
135
|
+
found.push({ name: file, path: filePath });
|
|
136
|
+
}
|
|
137
|
+
} catch {
|
|
92
138
|
}
|
|
93
139
|
}
|
|
140
|
+
_envFilesCache = found;
|
|
141
|
+
_cachedRoot = projectRoot;
|
|
94
142
|
return found;
|
|
95
143
|
}
|
|
96
144
|
function clearLine() {
|
|
97
|
-
process.stdout.
|
|
145
|
+
if (process.stdout.isTTY) {
|
|
146
|
+
process.stdout.write("\x1B[2K\r");
|
|
147
|
+
}
|
|
98
148
|
}
|
|
99
149
|
function printResolvedUrls(urls) {
|
|
100
150
|
if (!urls) return;
|
|
101
|
-
if (urls.local.length > 0)
|
|
102
|
-
console.log(` ${
|
|
103
|
-
|
|
104
|
-
|
|
151
|
+
if (urls.local.length > 0) {
|
|
152
|
+
console.log(` ${"\x1B[32m" /* GREEN */}\u279C${"\x1B[0m" /* RESET */} Local: ${"\x1B[36m" /* CYAN */}${urls.local[0]}${"\x1B[0m" /* RESET */}`);
|
|
153
|
+
}
|
|
154
|
+
if (urls.network.length > 0) {
|
|
155
|
+
console.log(` ${"\x1B[32m" /* GREEN */}\u279C${"\x1B[0m" /* RESET */} Network: ${"\x1B[36m" /* CYAN */}${urls.network[0]}${"\x1B[0m" /* RESET */}`);
|
|
156
|
+
}
|
|
105
157
|
}
|
|
106
158
|
function biniEnv(options = {}) {
|
|
107
|
-
|
|
108
|
-
|
|
159
|
+
if (options.enabled === false) {
|
|
160
|
+
return {
|
|
161
|
+
name: "vite-plugin-bini-env"
|
|
162
|
+
// Minimal plugin that does nothing
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
const resolvedOptions = { ...DEFAULT_OPTIONS, ...options };
|
|
166
|
+
const { clearViteHeader, logo, envPrefix } = resolvedOptions;
|
|
109
167
|
let resolvedConfig;
|
|
110
|
-
let serverStarted = false;
|
|
111
|
-
let previewStarted = false;
|
|
112
168
|
return {
|
|
113
169
|
name: "vite-plugin-bini-env",
|
|
114
|
-
|
|
115
|
-
// Set BINI_ as the env prefix so import.meta.env.BINI_* works in client code
|
|
170
|
+
// Optimized config merge
|
|
116
171
|
config() {
|
|
117
172
|
return { envPrefix };
|
|
118
173
|
},
|
|
119
174
|
configResolved(cfg) {
|
|
120
175
|
resolvedConfig = cfg;
|
|
121
176
|
},
|
|
177
|
+
// Optimized server configuration with lazy loading
|
|
122
178
|
configureServer(server) {
|
|
123
|
-
|
|
179
|
+
const loadEnvTask = () => {
|
|
180
|
+
void loadEnv(resolvedConfig?.root ?? process.cwd());
|
|
181
|
+
};
|
|
124
182
|
if (server.httpServer) {
|
|
125
|
-
server.httpServer.once("listening",
|
|
126
|
-
void loadEnv(resolvedConfig?.root ?? process.cwd());
|
|
127
|
-
});
|
|
183
|
+
server.httpServer.once("listening", loadEnvTask);
|
|
128
184
|
} else {
|
|
129
|
-
|
|
185
|
+
setImmediate(loadEnvTask);
|
|
130
186
|
}
|
|
187
|
+
let serverStarted = false;
|
|
131
188
|
const originalPrintUrls = server.printUrls.bind(server);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
189
|
+
if (clearViteHeader || logo !== BINI_LOGO) {
|
|
190
|
+
server.printUrls = () => {
|
|
191
|
+
if (serverStarted) return;
|
|
192
|
+
serverStarted = true;
|
|
193
|
+
if (clearViteHeader) clearLine();
|
|
194
|
+
console.log(`
|
|
195
|
+
${"\x1B[36m" /* CYAN */}${logo} Bini.js${"\x1B[0m" /* RESET */} (dev)`);
|
|
196
|
+
if (process.env.NODE_ENV !== "production") {
|
|
197
|
+
const found = detectEnvFiles(resolvedConfig?.root);
|
|
198
|
+
if (found.length > 0) {
|
|
199
|
+
const envString = found.map((f) => f.name).join(", ");
|
|
200
|
+
console.log(` ${"\x1B[32m" /* GREEN */}\u279C${"\x1B[0m" /* RESET */} Environments: ${envString}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (!clearViteHeader) {
|
|
204
|
+
originalPrintUrls();
|
|
205
|
+
} else {
|
|
206
|
+
printResolvedUrls(server.resolvedUrls);
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
149
210
|
},
|
|
211
|
+
// Preview server with same optimizations
|
|
150
212
|
configurePreviewServer(server) {
|
|
151
|
-
|
|
213
|
+
let previewStarted = false;
|
|
152
214
|
const originalPrintUrls = server.printUrls.bind(server);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
215
|
+
if (clearViteHeader || logo !== BINI_LOGO) {
|
|
216
|
+
server.printUrls = () => {
|
|
217
|
+
if (previewStarted) return;
|
|
218
|
+
previewStarted = true;
|
|
219
|
+
if (clearViteHeader) clearLine();
|
|
220
|
+
console.log(`
|
|
221
|
+
${"\x1B[36m" /* CYAN */}${logo} Bini.js${"\x1B[0m" /* RESET */} (preview)`);
|
|
222
|
+
if (process.env.NODE_ENV !== "production") {
|
|
223
|
+
const found = detectEnvFiles(resolvedConfig?.root);
|
|
224
|
+
if (found.length > 0) {
|
|
225
|
+
const envString = found.map((f) => f.name).join(", ");
|
|
226
|
+
console.log(` ${"\x1B[32m" /* GREEN */}\u279C${"\x1B[0m" /* RESET */} Environments: ${envString}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (!clearViteHeader) {
|
|
230
|
+
originalPrintUrls();
|
|
231
|
+
} else {
|
|
232
|
+
printResolvedUrls(server.resolvedUrls);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
}
|
|
174
236
|
}
|
|
175
237
|
};
|
|
176
238
|
}
|
|
177
239
|
|
|
178
|
-
|
|
240
|
+
exports.biniEnv = biniEnv;
|
|
241
|
+
exports.detectEnvFiles = detectEnvFiles;
|
|
242
|
+
exports.getEnv = getEnv;
|
|
243
|
+
exports.loadEnv = loadEnv;
|
|
244
|
+
exports.requireEnv = requireEnv;
|
|
179
245
|
//# sourceMappingURL=index.js.map
|
|
180
246
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAgEA,IAAM,SAAA,GAAY,MAAA;AAClB,IAAM,kBAAA,GAAqB,OAAA;AAE3B,IAAM,eAAA,GAAkB;AAAA,EACtB,OAAA,E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,CAAC,MAAM,CAAA,CAAE,IAAI,CAAA,CAAE,IAAA,CAAK,IAAI,CAAA;AACpD,UAAA,OAAA,CAAQ,GAAA,CAAI,KAAK,MAAA,CAAO,KAAK,SAAI,MAAA,CAAO,KAAK,CAAA,gBAAA,EAAmB,SAAS,CAAA,CAAE,CAAA;AAAA,QAC7E;AAEA,QAAA,IAAI,CAAC,eAAA,EAAiB;AACpB,UAAA,iBAAA,EAAkB;AAAA,QACpB,CAAA,MAAO;AACL,UAAA,iBAAA,CAAkB,OAAO,YAAY,CAAA;AAAA,QACvC;AAAA,MACF,CAAA;AAAA,IACF,CAAA;AAAA,IAEA,QAAA,GAAW;AACT,MAAA,aAAA,GAAgB,KAAA;AAChB,MAAA,cAAA,GAAiB,KAAA;AAAA,IACnB;AAAA,GACF;AACF","file":"index.js","sourcesContent":["// src/index.ts\nimport fs from 'fs';\nimport path from 'path';\nimport type { Plugin, ResolvedConfig, UserConfig } from 'vite';\n\n/* ==========================================================================\n Global type declarations for cross-runtime compatibility\n ========================================================================== */\n\n// Deno runtime type — declared as an interface so we can safely cast\n// without relying on a global that TypeScript doesn't know about\ninterface DenoEnv {\n get(key: string): string | undefined;\n toObject(): Record<string, string>;\n}\ninterface DenoNamespace {\n env: DenoEnv;\n}\n\n// Vite augments ImportMeta with .env\n// Using triple-slash reference so it works both inside and outside Vite projects\n/// <reference types=\"vite/client\" />\n\n/* ==========================================================================\n Types\n ========================================================================== */\n\nexport interface BiniEnvPluginOptions {\n /**\n * Enable/disable the plugin\n * @default true\n */\n readonly enabled?: boolean;\n\n /**\n * Remove Vite's default header\n * @default true\n */\n readonly clearViteHeader?: boolean;\n\n /**\n * Custom logo/text to display\n * @default 'ß'\n */\n readonly logo?: string;\n\n /**\n * Override the env var prefix exposed to client code.\n * Defaults to 'BINI_' — so BINI_MY_VAR is accessible via import.meta.env.BINI_MY_VAR\n * Set to 'VITE_' to keep Vite's default behaviour.\n * @default 'BINI_'\n */\n readonly envPrefix?: string;\n}\n\nexport interface DetectedEnvFile {\n readonly name: string;\n readonly path: string;\n}\n\n/* ==========================================================================\n Constants\n ========================================================================== */\n\nconst BINI_LOGO = 'ß' as const;\nconst DEFAULT_ENV_PREFIX = 'BINI_' as const;\n\nconst DEFAULT_OPTIONS = {\n enabled: true,\n clearViteHeader: true,\n logo: BINI_LOGO,\n envPrefix: DEFAULT_ENV_PREFIX,\n} as const;\n\nconst COLORS = {\n CYAN: '\\x1b[36m',\n RESET: '\\x1b[0m',\n GREEN: '\\x1b[32m',\n} as const;\n\n/* ==========================================================================\n getEnv — Universal env var accessor\n Works in: Node.js, Deno (edge functions), Bun, Vite client\n ========================================================================== */\n\n/**\n * Read an environment variable in any runtime.\n *\n * Priority order:\n * 1. Deno.env — Netlify edge functions / Deno runtime\n * 2. process.env — Node.js / Bun / bini-server\n * 3. import.meta.env — Vite client (BINI_-prefixed vars)\n *\n * Usage in API routes:\n * import { getEnv } from 'bini-env';\n * const smtpUser = getEnv('SMTP_USER');\n */\nexport function getEnv(key: string): string | undefined {\n // 1. Deno runtime (Netlify edge functions)\n const deno = (globalThis as unknown as { Deno?: DenoNamespace }).Deno;\n if (deno) {\n try {\n const val = deno.env.get(key);\n if (val !== undefined) return val;\n } catch {\n // Deno env access denied — fall through\n }\n }\n\n // 2. Node.js / Bun / bini-server\n if (typeof process !== 'undefined' && process.env) {\n if (process.env[key] !== undefined) return process.env[key];\n }\n\n // 3. Vite injected (import.meta.env) — BINI_-prefixed client vars\n // Wrapped in try/catch because import.meta.env doesn't exist in Node/Deno\n try {\n const metaEnv = import.meta.env as Record<string, string | undefined> | undefined;\n if (metaEnv) {\n if (metaEnv[key] !== undefined) return metaEnv[key];\n // Try with BINI_ prefix automatically\n if (metaEnv[`BINI_${key}`] !== undefined) return metaEnv[`BINI_${key}`];\n }\n } catch {\n // import.meta.env not available in this runtime — ignore\n }\n\n return undefined;\n}\n\n/**\n * Read an environment variable and throw if it's missing or empty.\n * Use this for required secrets so you get a clear error at startup\n * instead of a cryptic crash later.\n *\n * Usage:\n * const smtpPass = requireEnv('SMTP_PASS');\n */\nexport function requireEnv(key: string): string {\n const val = getEnv(key);\n if (!val) {\n throw new Error(\n `[bini-env] Missing required environment variable: \"${key}\".\\n` +\n ` → In development: add it to your .env file.\\n` +\n ` → In production: add it to your hosting dashboard environment variables.`\n );\n }\n return val;\n}\n\n/* ==========================================================================\n loadEnv — Auto dotenv loader for bini-router API routes (Node.js only)\n Called internally by the Vite plugin. Safe to call multiple times.\n ========================================================================== */\n\nlet _envLoaded = false;\n\n/**\n * Auto-load .env into process.env for Node.js API route contexts.\n *\n * Behaviour by environment:\n * - Deno (edge functions) → no-op, Deno/Netlify injects env natively\n * - NODE_ENV=production → no-op, host injects env vars automatically\n * - Node.js dev → loads .env via dotenv (dynamic import, safe for bundlers)\n *\n * Called automatically by bini-env Vite plugin. You should not need to call this manually.\n */\nexport async function loadEnv(projectRoot?: string): Promise<void> {\n // Already loaded — skip\n if (_envLoaded) return;\n\n // Deno runtime — env is handled natively, skip\n if ((globalThis as unknown as { Deno?: DenoNamespace }).Deno) {\n _envLoaded = true;\n return;\n }\n\n // Not in a Node-like runtime — skip\n if (typeof process === 'undefined' || !process.env) {\n _envLoaded = true;\n return;\n }\n\n // Production — host injects env vars, dotenv not needed\n if (process.env.NODE_ENV === 'production') {\n _envLoaded = true;\n return;\n }\n\n try {\n const root = projectRoot ?? process.cwd();\n const envPath = path.join(root, '.env');\n\n if (!fs.existsSync(envPath)) {\n _envLoaded = true;\n return;\n }\n\n // Dynamic import so Deno/edge bundlers never try to statically resolve dotenv\n const dotenv = await import('dotenv');\n dotenv.config({ path: envPath });\n } catch {\n // dotenv not installed or read failed — silently skip.\n // requireEnv() will surface clear errors if vars are missing when used.\n }\n\n _envLoaded = true;\n}\n\n/* ==========================================================================\n detectEnvFiles — used by the Vite plugin display\n ========================================================================== */\n\n/**\n * Detect which .env files exist in the project root, in Vite's priority order.\n */\nexport function detectEnvFiles(projectRoot: string = process.cwd()): DetectedEnvFile[] {\n const nodeEnv = process.env.NODE_ENV ?? 'development';\n\n const candidates = [\n '.env.local',\n `.env.${nodeEnv}.local`,\n `.env.${nodeEnv}`,\n '.env',\n ] as const;\n\n const found: DetectedEnvFile[] = [];\n\n for (const file of candidates) {\n const filePath = path.join(projectRoot, file);\n if (fs.existsSync(filePath)) {\n found.push({ name: file, path: filePath });\n }\n }\n\n return found;\n}\n\n/* ==========================================================================\n Vite plugin\n ========================================================================== */\n\nfunction clearLine(): void {\n process.stdout.write('\\x1b[2K\\r');\n}\n\nfunction printResolvedUrls(urls: { local: string[]; network: string[] } | null | undefined): void {\n if (!urls) return;\n if (urls.local.length > 0)\n console.log(` ${COLORS.GREEN}➜${COLORS.RESET} Local: ${COLORS.CYAN}${urls.local[0]}${COLORS.RESET}`);\n if (urls.network.length > 0)\n console.log(` ${COLORS.GREEN}➜${COLORS.RESET} Network: ${COLORS.CYAN}${urls.network[0]}${COLORS.RESET}`);\n}\n\n/**\n * Bini.js Vite plugin.\n *\n * What it does:\n * - Sets envPrefix to 'BINI_' so client code uses import.meta.env.BINI_MY_VAR\n * - Auto-loads .env for API routes in dev via loadEnv()\n * - Replaces Vite's startup output with the Bini.js branded header\n */\nexport function biniEnv(options: Readonly<BiniEnvPluginOptions> = {}): Plugin {\n const resolvedOptions = Object.freeze({ ...DEFAULT_OPTIONS, ...options });\n const { enabled, clearViteHeader, logo, envPrefix } = resolvedOptions;\n\n let resolvedConfig: ResolvedConfig;\n let serverStarted = false;\n let previewStarted = false;\n\n return {\n name: 'vite-plugin-bini-env',\n\n apply: 'serve',\n\n // Set BINI_ as the env prefix so import.meta.env.BINI_* works in client code\n config(): UserConfig {\n return { envPrefix };\n },\n\n configResolved(cfg: ResolvedConfig) {\n resolvedConfig = cfg;\n },\n\n configureServer(server) {\n if (!enabled) return;\n\n // Auto-load .env after server starts — guarantees resolvedConfig is available\n // Falls back to immediate call in middleware mode where httpServer is null\n if (server.httpServer) {\n server.httpServer.once('listening', () => {\n void loadEnv(resolvedConfig?.root ?? process.cwd());\n });\n } else {\n // Middleware mode — httpServer is null, call immediately\n void loadEnv(resolvedConfig?.root ?? process.cwd());\n }\n\n const originalPrintUrls = server.printUrls.bind(server);\n\n server.printUrls = () => {\n if (serverStarted) return;\n serverStarted = true;\n\n if (clearViteHeader) clearLine();\n console.log(`\\n ${COLORS.CYAN}${logo} Bini.js${COLORS.RESET} (dev)`);\n\n const found = detectEnvFiles(resolvedConfig?.root);\n if (found.length > 0) {\n const envString = found.map((f) => f.name).join(', ');\n console.log(` ${COLORS.GREEN}➜${COLORS.RESET} Environments: ${envString}`);\n }\n\n if (!clearViteHeader) {\n originalPrintUrls();\n } else {\n printResolvedUrls(server.resolvedUrls);\n }\n };\n },\n\n configurePreviewServer(server) {\n if (!enabled) return;\n\n const originalPrintUrls = server.printUrls.bind(server);\n\n server.printUrls = () => {\n if (previewStarted) return;\n previewStarted = true;\n\n if (clearViteHeader) clearLine();\n console.log(`\\n ${COLORS.CYAN}${logo} Bini.js${COLORS.RESET} (preview)`);\n\n const found = detectEnvFiles(resolvedConfig?.root);\n if (found.length > 0) {\n const envString = found.map((f) => f.name).join(', ');\n console.log(` ${COLORS.GREEN}➜${COLORS.RESET} Environments: ${envString}`);\n }\n\n if (!clearViteHeader) {\n originalPrintUrls();\n } else {\n printResolvedUrls(server.resolvedUrls);\n }\n };\n },\n\n buildEnd() {\n serverStarted = false;\n previewStarted = false;\n },\n };\n}\n\n// Named export: import { biniEnv } from 'bini-env'\nexport type { Plugin } from 'vite';"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["path","fs"],"mappings":";;;;;;;;;;;AAuCA,IAAM,SAAA,GAAY,MAAA;AAClB,IAAM,kBAAA,GAA+B,CAAC,OAAA,EAAS,OAAO,CAAA;AAEtD,IAAM,eAAA,GAAkD,OAAO,MAAA,CAAO;AAAA,EACpE,OAAA,EAAS,IAAA;AAAA,EACT,eAAA,EAAiB,IAAA;AAAA,EACjB,IAAA,EAAM,S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W,EAAG,IAAI,CAAA,QAAA,EAAW,sBAAY,UAAA,CAAY,CAAA;AAExE,UAAA,IAAI,OAAA,CAAQ,GAAA,CAAI,QAAA,KAAa,YAAA,EAAc;AACzC,YAAA,MAAM,KAAA,GAAQ,cAAA,CAAe,cAAA,EAAgB,IAAI,CAAA;AACjD,YAAA,IAAI,KAAA,CAAM,SAAS,CAAA,EAAG;AACpB,cAAA,MAAM,SAAA,GAAY,MAAM,GAAA,CAAI,CAAC,MAAM,CAAA,CAAE,IAAI,CAAA,CAAE,IAAA,CAAK,IAAI,CAAA;AACpD,cAAA,OAAA,CAAQ,IAAI,CAAA,EAAA,EAAK,UAAA,qBAAgB,SAAA,aAAY,gBAAA,EAAmB,SAAS,CAAA,CAAE,CAAA;AAAA,YAC7E;AAAA,UACF;AAEA,UAAA,IAAI,CAAC,eAAA,EAAiB;AACpB,YAAA,iBAAA,EAAkB;AAAA,UACpB,CAAA,MAAO;AACL,YAAA,iBAAA,CAAkB,OAAO,YAAY,CAAA;AAAA,UACvC;AAAA,QACF,CAAA;AAAA,MACF;AAAA,IACF;AAAA,GACF;AACF","file":"index.js","sourcesContent":["// src/index.ts\nimport fs from 'fs';\nimport path from 'path';\nimport type { Plugin, ResolvedConfig, UserConfig } from 'vite';\n\n/* ==========================================================================\n Global type declarations for cross-runtime compatibility\n ========================================================================== */\n\ninterface DenoEnv {\n get(key: string): string | undefined;\n toObject(): Record<string, string>;\n}\ninterface DenoNamespace {\n env: DenoEnv;\n}\n\n/// <reference types=\"vite/client\" />\n\n/* ==========================================================================\n Types\n ========================================================================== */\n\nexport interface BiniEnvPluginOptions {\n readonly enabled?: boolean;\n readonly clearViteHeader?: boolean;\n readonly logo?: string;\n readonly envPrefix?: string | string[];\n}\n\nexport interface DetectedEnvFile {\n readonly name: string;\n readonly path: string;\n}\n\n/* ==========================================================================\n Constants - Optimized with Object.freeze for immutability\n ========================================================================== */\n\nconst BINI_LOGO = 'ß';\nconst DEFAULT_ENV_PREFIX: string[] = ['BINI_', 'VITE_'];\n\nconst DEFAULT_OPTIONS: Required<BiniEnvPluginOptions> = Object.freeze({\n enabled: true,\n clearViteHeader: true,\n logo: BINI_LOGO,\n envPrefix: DEFAULT_ENV_PREFIX,\n});\n\n// ANSI color codes - using const enum for better performance\nconst enum COLORS {\n CYAN = '\\x1b[36m',\n RESET = '\\x1b[0m',\n GREEN = '\\x1b[32m',\n}\n\n// Cache for environment variables to avoid repeated lookups\nconst envCache = new Map<string, string | undefined>();\n\n/* ==========================================================================\n Optimized getEnv with caching\n ========================================================================== */\n\n/**\n * Read an environment variable with caching for performance.\n * Cache is cleared on module reload in development only.\n */\nexport function getEnv(key: string): string | undefined {\n // Check cache first for frequent access patterns\n if (envCache.has(key)) {\n return envCache.get(key);\n }\n\n let value: string | undefined;\n\n // 1. Deno runtime (edge functions)\n const deno = (globalThis as unknown as { Deno?: DenoNamespace }).Deno;\n if (deno) {\n try {\n value = deno.env.get(key);\n if (value !== undefined) {\n envCache.set(key, value);\n return value;\n }\n } catch {\n // Silently fall through\n }\n }\n\n // 2. Node.js / Bun runtime\n if (typeof process !== 'undefined' && process.env) {\n value = process.env[key];\n if (value !== undefined) {\n envCache.set(key, value);\n return value;\n }\n }\n\n // 3. Vite client environment (with prefix fallback)\n try {\n const metaEnv = import.meta.env as Record<string, string | undefined> | undefined;\n if (metaEnv) {\n // Direct match\n if (metaEnv[key] !== undefined) {\n envCache.set(key, metaEnv[key]);\n return metaEnv[key];\n }\n \n // Try BINI_ prefix\n const biniKey = `BINI_${key}`;\n if (metaEnv[biniKey] !== undefined) {\n envCache.set(key, metaEnv[biniKey]);\n return metaEnv[biniKey];\n }\n \n // Try VITE_ prefix\n const viteKey = `VITE_${key}`;\n if (metaEnv[viteKey] !== undefined) {\n envCache.set(key, metaEnv[viteKey]);\n return metaEnv[viteKey];\n }\n }\n } catch {\n // import.meta.env not available\n }\n\n // Cache undefined to prevent repeated lookups\n envCache.set(key, undefined);\n return undefined;\n}\n\n/**\n * Optimized requireEnv with cached error messages\n */\nexport function requireEnv(key: string): string {\n const val = getEnv(key);\n if (!val) {\n // Create error message once\n const errorMsg = `[bini-env] Missing required environment variable: \"${key}\".\\n` +\n ` → In development: add it to your .env file.\\n` +\n ` → In production: add it to your hosting dashboard environment variables.`;\n throw new Error(errorMsg);\n }\n return val;\n}\n\n/* ==========================================================================\n Optimized loadEnv with production checks\n ========================================================================== */\n\nlet _envLoaded = false;\nlet _loadPromise: Promise<void> | null = null;\n\n/**\n * Load .env file with memoization and race condition prevention\n */\nexport async function loadEnv(projectRoot?: string): Promise<void> {\n // Fast path: already loaded\n if (_envLoaded) return;\n \n // Prevent concurrent loads\n if (_loadPromise) {\n return _loadPromise;\n }\n\n _loadPromise = (async () => {\n try {\n // Deno runtime check - fast path\n if ((globalThis as unknown as { Deno?: DenoNamespace }).Deno) {\n _envLoaded = true;\n return;\n }\n\n // Node.js runtime check\n if (typeof process === 'undefined' || !process.env) {\n _envLoaded = true;\n return;\n }\n\n // Production optimization - skip file system operations entirely\n if (process.env.NODE_ENV === 'production') {\n _envLoaded = true;\n return;\n }\n\n const root = projectRoot ?? process.cwd();\n const envPath = path.join(root, '.env');\n\n // Check file existence with fs.promises for better performance\n try {\n await fs.promises.access(envPath, fs.constants.R_OK);\n } catch {\n _envLoaded = true;\n return;\n }\n\n // Dynamic import with caching\n const dotenv = await import('dotenv');\n dotenv.config({ path: envPath });\n \n // Clear cache after loading new env vars\n envCache.clear();\n } catch {\n // Silent fail - requireEnv will handle missing vars\n } finally {\n _envLoaded = true;\n _loadPromise = null;\n }\n })();\n\n return _loadPromise;\n}\n\n/* ==========================================================================\n Optimized detectEnvFiles with caching\n ========================================================================== */\n\nlet _envFilesCache: DetectedEnvFile[] | null = null;\nlet _cachedRoot: string = '';\n\nexport function detectEnvFiles(projectRoot: string = process.cwd()): DetectedEnvFile[] {\n // Cache results for the same root directory\n if (_envFilesCache && _cachedRoot === projectRoot) {\n return _envFilesCache;\n }\n\n const nodeEnv = process.env.NODE_ENV ?? 'development';\n const candidates = [\n '.env.local',\n `.env.${nodeEnv}.local`,\n `.env.${nodeEnv}`,\n '.env',\n ];\n\n const found: DetectedEnvFile[] = [];\n\n // Use for...of with early break if needed\n for (const file of candidates) {\n const filePath = path.join(projectRoot, file);\n try {\n if (fs.existsSync(filePath)) {\n found.push({ name: file, path: filePath });\n }\n } catch {\n // Skip files that can't be accessed\n }\n }\n\n // Cache the result\n _envFilesCache = found;\n _cachedRoot = projectRoot;\n \n return found;\n}\n\n/* ==========================================================================\n Optimized Vite plugin with minimal overhead\n ========================================================================== */\n\nfunction clearLine(): void {\n // Only write if stdout is a TTY (performance optimization)\n if (process.stdout.isTTY) {\n process.stdout.write('\\x1b[2K\\r');\n }\n}\n\nfunction printResolvedUrls(urls: { local: string[]; network: string[] } | null | undefined): void {\n if (!urls) return;\n if (urls.local.length > 0) {\n console.log(` ${COLORS.GREEN}➜${COLORS.RESET} Local: ${COLORS.CYAN}${urls.local[0]}${COLORS.RESET}`);\n }\n if (urls.network.length > 0) {\n console.log(` ${COLORS.GREEN}➜${COLORS.RESET} Network: ${COLORS.CYAN}${urls.network[0]}${COLORS.RESET}`);\n }\n}\n\n/**\n * Production-ready Vite plugin with performance optimizations:\n * - Lazy loading\n * - Caching\n * - Minimal bundle impact\n * - No runtime overhead in production\n */\nexport function biniEnv(options: Readonly<BiniEnvPluginOptions> = {}): Plugin {\n // Fast path: disabled plugin returns no-op\n if (options.enabled === false) {\n return {\n name: 'vite-plugin-bini-env',\n // Minimal plugin that does nothing\n };\n }\n\n const resolvedOptions = { ...DEFAULT_OPTIONS, ...options };\n const { clearViteHeader, logo, envPrefix } = resolvedOptions;\n\n let resolvedConfig: ResolvedConfig;\n\n return {\n name: 'vite-plugin-bini-env',\n \n // Optimized config merge\n config(): UserConfig {\n return { envPrefix };\n },\n\n configResolved(cfg: ResolvedConfig) {\n resolvedConfig = cfg;\n },\n\n // Optimized server configuration with lazy loading\n configureServer(server) {\n // Defer env loading to avoid blocking server start\n const loadEnvTask = () => {\n void loadEnv(resolvedConfig?.root ?? process.cwd());\n };\n\n if (server.httpServer) {\n server.httpServer.once('listening', loadEnvTask);\n } else {\n // Use setImmediate for non-blocking execution\n setImmediate(loadEnvTask);\n }\n\n let serverStarted = false;\n const originalPrintUrls = server.printUrls.bind(server);\n\n // Only override printUrls if needed\n if (clearViteHeader || logo !== BINI_LOGO) {\n server.printUrls = () => {\n if (serverStarted) return;\n serverStarted = true;\n\n if (clearViteHeader) clearLine();\n console.log(`\\n ${COLORS.CYAN}${logo} Bini.js${COLORS.RESET} (dev)`);\n\n // Only detect env files in development\n if (process.env.NODE_ENV !== 'production') {\n const found = detectEnvFiles(resolvedConfig?.root);\n if (found.length > 0) {\n const envString = found.map((f) => f.name).join(', ');\n console.log(` ${COLORS.GREEN}➜${COLORS.RESET} Environments: ${envString}`);\n }\n }\n\n if (!clearViteHeader) {\n originalPrintUrls();\n } else {\n printResolvedUrls(server.resolvedUrls);\n }\n };\n }\n },\n\n // Preview server with same optimizations\n configurePreviewServer(server) {\n let previewStarted = false;\n const originalPrintUrls = server.printUrls.bind(server);\n\n if (clearViteHeader || logo !== BINI_LOGO) {\n server.printUrls = () => {\n if (previewStarted) return;\n previewStarted = true;\n\n if (clearViteHeader) clearLine();\n console.log(`\\n ${COLORS.CYAN}${logo} Bini.js${COLORS.RESET} (preview)`);\n\n if (process.env.NODE_ENV !== 'production') {\n const found = detectEnvFiles(resolvedConfig?.root);\n if (found.length > 0) {\n const envString = found.map((f) => f.name).join(', ');\n console.log(` ${COLORS.GREEN}➜${COLORS.RESET} Environments: ${envString}`);\n }\n }\n\n if (!clearViteHeader) {\n originalPrintUrls();\n } else {\n printResolvedUrls(server.resolvedUrls);\n }\n };\n }\n },\n };\n}\n\nexport type { Plugin } from 'vite';"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
var BINI_LOGO = "\xDF";
|
|
6
|
+
var DEFAULT_ENV_PREFIX = ["BINI_", "VITE_"];
|
|
7
|
+
var DEFAULT_OPTIONS = Object.freeze({
|
|
8
|
+
enabled: true,
|
|
9
|
+
clearViteHeader: true,
|
|
10
|
+
logo: BINI_LOGO,
|
|
11
|
+
envPrefix: DEFAULT_ENV_PREFIX
|
|
12
|
+
});
|
|
13
|
+
var envCache = /* @__PURE__ */ new Map();
|
|
14
|
+
function getEnv(key) {
|
|
15
|
+
if (envCache.has(key)) {
|
|
16
|
+
return envCache.get(key);
|
|
17
|
+
}
|
|
18
|
+
let value;
|
|
19
|
+
const deno = globalThis.Deno;
|
|
20
|
+
if (deno) {
|
|
21
|
+
try {
|
|
22
|
+
value = deno.env.get(key);
|
|
23
|
+
if (value !== void 0) {
|
|
24
|
+
envCache.set(key, value);
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
} catch {
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (typeof process !== "undefined" && process.env) {
|
|
31
|
+
value = process.env[key];
|
|
32
|
+
if (value !== void 0) {
|
|
33
|
+
envCache.set(key, value);
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const metaEnv = import.meta.env;
|
|
39
|
+
if (metaEnv) {
|
|
40
|
+
if (metaEnv[key] !== void 0) {
|
|
41
|
+
envCache.set(key, metaEnv[key]);
|
|
42
|
+
return metaEnv[key];
|
|
43
|
+
}
|
|
44
|
+
const biniKey = `BINI_${key}`;
|
|
45
|
+
if (metaEnv[biniKey] !== void 0) {
|
|
46
|
+
envCache.set(key, metaEnv[biniKey]);
|
|
47
|
+
return metaEnv[biniKey];
|
|
48
|
+
}
|
|
49
|
+
const viteKey = `VITE_${key}`;
|
|
50
|
+
if (metaEnv[viteKey] !== void 0) {
|
|
51
|
+
envCache.set(key, metaEnv[viteKey]);
|
|
52
|
+
return metaEnv[viteKey];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
}
|
|
57
|
+
envCache.set(key, void 0);
|
|
58
|
+
return void 0;
|
|
59
|
+
}
|
|
60
|
+
function requireEnv(key) {
|
|
61
|
+
const val = getEnv(key);
|
|
62
|
+
if (!val) {
|
|
63
|
+
const errorMsg = `[bini-env] Missing required environment variable: "${key}".
|
|
64
|
+
\u2192 In development: add it to your .env file.
|
|
65
|
+
\u2192 In production: add it to your hosting dashboard environment variables.`;
|
|
66
|
+
throw new Error(errorMsg);
|
|
67
|
+
}
|
|
68
|
+
return val;
|
|
69
|
+
}
|
|
70
|
+
var _envLoaded = false;
|
|
71
|
+
var _loadPromise = null;
|
|
72
|
+
async function loadEnv(projectRoot) {
|
|
73
|
+
if (_envLoaded) return;
|
|
74
|
+
if (_loadPromise) {
|
|
75
|
+
return _loadPromise;
|
|
76
|
+
}
|
|
77
|
+
_loadPromise = (async () => {
|
|
78
|
+
try {
|
|
79
|
+
if (globalThis.Deno) {
|
|
80
|
+
_envLoaded = true;
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (typeof process === "undefined" || !process.env) {
|
|
84
|
+
_envLoaded = true;
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (process.env.NODE_ENV === "production") {
|
|
88
|
+
_envLoaded = true;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const root = projectRoot ?? process.cwd();
|
|
92
|
+
const envPath = path.join(root, ".env");
|
|
93
|
+
try {
|
|
94
|
+
await fs.promises.access(envPath, fs.constants.R_OK);
|
|
95
|
+
} catch {
|
|
96
|
+
_envLoaded = true;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const dotenv = await import('dotenv');
|
|
100
|
+
dotenv.config({ path: envPath });
|
|
101
|
+
envCache.clear();
|
|
102
|
+
} catch {
|
|
103
|
+
} finally {
|
|
104
|
+
_envLoaded = true;
|
|
105
|
+
_loadPromise = null;
|
|
106
|
+
}
|
|
107
|
+
})();
|
|
108
|
+
return _loadPromise;
|
|
109
|
+
}
|
|
110
|
+
var _envFilesCache = null;
|
|
111
|
+
var _cachedRoot = "";
|
|
112
|
+
function detectEnvFiles(projectRoot = process.cwd()) {
|
|
113
|
+
if (_envFilesCache && _cachedRoot === projectRoot) {
|
|
114
|
+
return _envFilesCache;
|
|
115
|
+
}
|
|
116
|
+
const nodeEnv = process.env.NODE_ENV ?? "development";
|
|
117
|
+
const candidates = [
|
|
118
|
+
".env.local",
|
|
119
|
+
`.env.${nodeEnv}.local`,
|
|
120
|
+
`.env.${nodeEnv}`,
|
|
121
|
+
".env"
|
|
122
|
+
];
|
|
123
|
+
const found = [];
|
|
124
|
+
for (const file of candidates) {
|
|
125
|
+
const filePath = path.join(projectRoot, file);
|
|
126
|
+
try {
|
|
127
|
+
if (fs.existsSync(filePath)) {
|
|
128
|
+
found.push({ name: file, path: filePath });
|
|
129
|
+
}
|
|
130
|
+
} catch {
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
_envFilesCache = found;
|
|
134
|
+
_cachedRoot = projectRoot;
|
|
135
|
+
return found;
|
|
136
|
+
}
|
|
137
|
+
function clearLine() {
|
|
138
|
+
if (process.stdout.isTTY) {
|
|
139
|
+
process.stdout.write("\x1B[2K\r");
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function printResolvedUrls(urls) {
|
|
143
|
+
if (!urls) return;
|
|
144
|
+
if (urls.local.length > 0) {
|
|
145
|
+
console.log(` ${"\x1B[32m" /* GREEN */}\u279C${"\x1B[0m" /* RESET */} Local: ${"\x1B[36m" /* CYAN */}${urls.local[0]}${"\x1B[0m" /* RESET */}`);
|
|
146
|
+
}
|
|
147
|
+
if (urls.network.length > 0) {
|
|
148
|
+
console.log(` ${"\x1B[32m" /* GREEN */}\u279C${"\x1B[0m" /* RESET */} Network: ${"\x1B[36m" /* CYAN */}${urls.network[0]}${"\x1B[0m" /* RESET */}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function biniEnv(options = {}) {
|
|
152
|
+
if (options.enabled === false) {
|
|
153
|
+
return {
|
|
154
|
+
name: "vite-plugin-bini-env"
|
|
155
|
+
// Minimal plugin that does nothing
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
const resolvedOptions = { ...DEFAULT_OPTIONS, ...options };
|
|
159
|
+
const { clearViteHeader, logo, envPrefix } = resolvedOptions;
|
|
160
|
+
let resolvedConfig;
|
|
161
|
+
return {
|
|
162
|
+
name: "vite-plugin-bini-env",
|
|
163
|
+
// Optimized config merge
|
|
164
|
+
config() {
|
|
165
|
+
return { envPrefix };
|
|
166
|
+
},
|
|
167
|
+
configResolved(cfg) {
|
|
168
|
+
resolvedConfig = cfg;
|
|
169
|
+
},
|
|
170
|
+
// Optimized server configuration with lazy loading
|
|
171
|
+
configureServer(server) {
|
|
172
|
+
const loadEnvTask = () => {
|
|
173
|
+
void loadEnv(resolvedConfig?.root ?? process.cwd());
|
|
174
|
+
};
|
|
175
|
+
if (server.httpServer) {
|
|
176
|
+
server.httpServer.once("listening", loadEnvTask);
|
|
177
|
+
} else {
|
|
178
|
+
setImmediate(loadEnvTask);
|
|
179
|
+
}
|
|
180
|
+
let serverStarted = false;
|
|
181
|
+
const originalPrintUrls = server.printUrls.bind(server);
|
|
182
|
+
if (clearViteHeader || logo !== BINI_LOGO) {
|
|
183
|
+
server.printUrls = () => {
|
|
184
|
+
if (serverStarted) return;
|
|
185
|
+
serverStarted = true;
|
|
186
|
+
if (clearViteHeader) clearLine();
|
|
187
|
+
console.log(`
|
|
188
|
+
${"\x1B[36m" /* CYAN */}${logo} Bini.js${"\x1B[0m" /* RESET */} (dev)`);
|
|
189
|
+
if (process.env.NODE_ENV !== "production") {
|
|
190
|
+
const found = detectEnvFiles(resolvedConfig?.root);
|
|
191
|
+
if (found.length > 0) {
|
|
192
|
+
const envString = found.map((f) => f.name).join(", ");
|
|
193
|
+
console.log(` ${"\x1B[32m" /* GREEN */}\u279C${"\x1B[0m" /* RESET */} Environments: ${envString}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (!clearViteHeader) {
|
|
197
|
+
originalPrintUrls();
|
|
198
|
+
} else {
|
|
199
|
+
printResolvedUrls(server.resolvedUrls);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
// Preview server with same optimizations
|
|
205
|
+
configurePreviewServer(server) {
|
|
206
|
+
let previewStarted = false;
|
|
207
|
+
const originalPrintUrls = server.printUrls.bind(server);
|
|
208
|
+
if (clearViteHeader || logo !== BINI_LOGO) {
|
|
209
|
+
server.printUrls = () => {
|
|
210
|
+
if (previewStarted) return;
|
|
211
|
+
previewStarted = true;
|
|
212
|
+
if (clearViteHeader) clearLine();
|
|
213
|
+
console.log(`
|
|
214
|
+
${"\x1B[36m" /* CYAN */}${logo} Bini.js${"\x1B[0m" /* RESET */} (preview)`);
|
|
215
|
+
if (process.env.NODE_ENV !== "production") {
|
|
216
|
+
const found = detectEnvFiles(resolvedConfig?.root);
|
|
217
|
+
if (found.length > 0) {
|
|
218
|
+
const envString = found.map((f) => f.name).join(", ");
|
|
219
|
+
console.log(` ${"\x1B[32m" /* GREEN */}\u279C${"\x1B[0m" /* RESET */} Environments: ${envString}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (!clearViteHeader) {
|
|
223
|
+
originalPrintUrls();
|
|
224
|
+
} else {
|
|
225
|
+
printResolvedUrls(server.resolvedUrls);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export { biniEnv, detectEnvFiles, getEnv, loadEnv, requireEnv };
|
|
234
|
+
//# sourceMappingURL=index.mjs.map
|
|
235
|
+
//# sourceMappingURL=index.mjs.map
|