@nocobase/portal-template-default 1.0.14 → 1.0.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/portal-template-default",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "packageManager": "pnpm@10.28.0",
5
5
  "type": "module",
6
6
  "devDependencies": {
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
- import { loadEnv } from "vite";
4
+ import * as util from "node:util";
5
5
 
6
6
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
7
  const rootDir = path.resolve(__dirname, "..");
@@ -12,9 +12,66 @@ const indexPath = path.join(distDir, "index.html");
12
12
  const startMarker = "<!-- nocobase-runtime-config:start -->";
13
13
  const endMarker = "<!-- nocobase-runtime-config:end -->";
14
14
 
15
+ const parseEnv = (content) => {
16
+ if (typeof util.parseEnv === "function") return util.parseEnv(content);
17
+
18
+ const parsed = {};
19
+ const linePattern =
20
+ /^\s*(?:export\s+)?([\w.-]+)\s*=\s*('(?:\\'|[^'])*'|"(?:\\"|[^"])*"|[^#\r\n]*)?\s*(?:#.*)?$/;
21
+
22
+ for (const line of content.split(/\r?\n/)) {
23
+ const match = line.match(linePattern);
24
+ if (!match) continue;
25
+
26
+ const [, key, rawValue = ""] = match;
27
+ const quote = rawValue[0];
28
+ let value = rawValue.trim();
29
+
30
+ if (
31
+ (quote === '"' || quote === "'") &&
32
+ value.endsWith(quote) &&
33
+ value.length >= 2
34
+ ) {
35
+ value = value.slice(1, -1);
36
+ }
37
+
38
+ parsed[key] = value.replace(/\\n/g, "\n").replace(/\\r/g, "\r");
39
+ }
40
+
41
+ return parsed;
42
+ };
43
+
44
+ const expandEnvValue = (value, env) =>
45
+ value.replace(/\\?\${?([A-Za-z_][A-Za-z0-9_]*)}?/g, (match, key) => {
46
+ if (match.startsWith("\\")) return match.slice(1);
47
+ return env[key] ?? "";
48
+ });
49
+
50
+ const getEnvFilesForMode = (mode) => {
51
+ if (mode === "local") {
52
+ throw new Error(
53
+ '"local" cannot be used as a mode name because it conflicts with .env.local.'
54
+ );
55
+ }
56
+
57
+ return [".env", ".env.local", `.env.${mode}`, `.env.${mode}.local`].map(
58
+ (file) => path.join(rootDir, file)
59
+ );
60
+ };
61
+
15
62
  const loadBuildHtmlEnv = () => {
16
63
  const mode = process.env.MODE || "production";
17
- const env = loadEnv(mode, rootDir, "");
64
+ const env = {};
65
+
66
+ for (const envFile of getEnvFilesForMode(mode)) {
67
+ if (!fs.existsSync(envFile)) continue;
68
+ Object.assign(env, parseEnv(fs.readFileSync(envFile, "utf8")));
69
+ }
70
+
71
+ const expansionEnv = { ...env, ...process.env };
72
+ for (const [key, value] of Object.entries(env)) {
73
+ env[key] = expandEnvValue(value, expansionEnv);
74
+ }
18
75
 
19
76
  for (const [key, value] of Object.entries(env)) {
20
77
  if (process.env[key] === undefined) {