dinou 9.3.0 → 10.0.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.
- package/CHANGELOG.md +196 -0
- package/README.md +157 -132
- package/dinou/adapters/bun.js +34 -5
- package/dinou/adapters/cloudflare.js +110 -17
- package/dinou/adapters/deno.js +46 -8
- package/dinou/adapters/netlify.js +86 -50
- package/dinou/adapters/node.js +8 -0
- package/dinou/assets.d.ts +32 -5
- package/dinou/bun/build.js +190 -106
- package/dinou/cli.js +992 -532
- package/dinou/cloudflare/build.js +187 -106
- package/dinou/compiled/autoprefixer/index.js +509 -517
- package/dinou/compiled/babel-core/index.js +738 -716
- package/dinou/compiled/babel-generator/index.js +341 -299
- package/dinou/compiled/babel-loader/index.js +766 -744
- package/dinou/compiled/babel-parser/index.js +2 -2
- package/dinou/compiled/babel-plugin-react-compiler/index.js +178 -178
- package/dinou/compiled/babel-plugin-syntax-import-meta/index.js +4 -4
- package/dinou/compiled/babel-preset-react/index.js +752 -730
- package/dinou/compiled/babel-preset-typescript/index.js +738 -716
- package/dinou/compiled/babel-traverse/index.js +463 -433
- package/dinou/compiled/babel-types/index.js +176 -176
- package/dinou/compiled/chokidar/index.js +4 -4
- package/dinou/compiled/commander/index.js +15 -15
- package/dinou/compiled/copy-webpack-plugin/index.js +433 -425
- package/dinou/compiled/css-loader/index.js +289 -289
- package/dinou/compiled/css-loader/runtime/api.js +85 -0
- package/dinou/compiled/css-loader/runtime/getUrl.js +26 -0
- package/dinou/compiled/css-loader/runtime/noSourceMaps.js +5 -0
- package/dinou/compiled/css-loader/runtime/package.json +3 -0
- package/dinou/compiled/css-loader/runtime/sourceMaps.js +16 -0
- package/dinou/compiled/fast-glob/index.js +5810 -0
- package/dinou/compiled/generic-names/index.js +16 -16
- package/dinou/compiled/mini-css-extract-plugin/{chunk-YYEJWYXE.js → chunk-R6DP5ZYM.js} +36 -36
- package/dinou/compiled/mini-css-extract-plugin/hmr/hotModuleReplacement.js +253 -0
- package/dinou/compiled/mini-css-extract-plugin/hmr/normalize-url.js +39 -0
- package/dinou/compiled/mini-css-extract-plugin/hmr/package.json +3 -0
- package/dinou/compiled/mini-css-extract-plugin/index.js +1 -1
- package/dinou/compiled/mini-css-extract-plugin/loader.js +5 -5
- package/dinou/compiled/postcss/index.js +83 -83
- package/dinou/compiled/postcss-import/index.js +147 -117
- package/dinou/compiled/postcss-modules/index.js +216 -216
- package/dinou/compiled/resolve/index.js +1224 -0
- package/dinou/compiled/rollup-plugin-babel/index.js +753 -731
- package/dinou/compiled/rollup-plugin-commonjs/index.js +43 -38
- package/dinou/compiled/rollup-plugin-delete/index.js +615 -253
- package/dinou/compiled/rollup-plugin-json/index.js +22 -18
- package/dinou/compiled/rollup-plugin-node-resolve/index.js +130 -100
- package/dinou/compiled/rollup-plugin-postcss/index.js +3299 -3277
- package/dinou/compiled/rollup-plugin-replace/index.js +34 -29
- package/dinou/compiled/tailwindcss-postcss/index.js +214 -211
- package/dinou/compiled/vendors-manifest.json +59 -83
- package/dinou/core/asset-extensions.js +482 -33
- package/dinou/core/bailout-proxy.js +21 -2
- package/dinou/core/build-static-pages.js +523 -371
- package/dinou/core/builtin-asset-extensions.js +63 -0
- package/dinou/core/cache-revalidate.js +449 -256
- package/dinou/core/cache-slot.js +289 -79
- package/dinou/core/client-webpack.jsx +470 -39
- package/dinou/core/client.jsx +402 -39
- package/dinou/core/config.js +3 -0
- package/dinou/core/copy-static-dirs.js +98 -0
- package/dinou/core/createScopedName.js +22 -4
- package/dinou/core/dinou-paths.js +1 -1
- package/dinou/core/discover-layouts.js +200 -0
- package/dinou/core/edge-ssr.js +1 -1
- package/dinou/core/ensure-vendor-mode.js +78 -19
- package/dinou/core/env.js +544 -0
- package/dinou/core/export-static.js +41 -9
- package/dinou/core/generate-route-types.js +989 -95
- package/dinou/core/generate-static-rsc.js +5 -6
- package/dinou/core/generate-static.js +45 -6
- package/dinou/core/get-abs-path-with-ext.js +217 -75
- package/dinou/core/get-asset-from-manifest.js +19 -8
- package/dinou/core/get-error-jsx.js +4 -5
- package/dinou/core/get-file-path-and-dynamic-params.js +561 -338
- package/dinou/core/get-jsx.js +220 -53
- package/dinou/core/handler.js +1647 -466
- package/dinou/core/import-module.js +7 -0
- package/dinou/core/layout-functions.js +72 -13
- package/dinou/core/link.jsx +34 -6
- package/dinou/core/load-dinou-config.js +520 -89
- package/dinou/core/manifest-provider.js +4 -14
- package/dinou/core/navigation-utils.js +141 -3
- package/dinou/core/navigation.js +134 -50
- package/dinou/core/node-middleware-runner.js +165 -0
- package/dinou/core/postcss-pipeline.js +236 -0
- package/dinou/core/ppr-context.js +114 -22
- package/dinou/core/ppr-runtime.js +391 -127
- package/dinou/core/redirect.jsx +1 -1
- package/dinou/core/render-jsx-to-client-jsx.js +216 -48
- package/dinou/core/request-context.js +59 -3
- package/dinou/core/resolve-contextual-dinou.js +523 -0
- package/dinou/core/resolve-setup-files.js +85 -27
- package/dinou/core/revalidating.js +10 -2
- package/dinou/core/route-generator.js +6 -2
- package/dinou/core/rsc-renderer.js +4 -1
- package/dinou/core/run-ssg.js +6 -0
- package/dinou/core/runtime-env.js +265 -0
- package/dinou/core/server-css-modules-plugin.js +155 -0
- package/dinou/core/server-function-proxy-webpack.js +27 -2
- package/dinou/core/server-function-proxy.js +27 -2
- package/dinou/core/slot.js +47 -2
- package/dinou/core/static-assets.js +503 -0
- package/dinou/core/storage-adapter.js +452 -24
- package/dinou/core/transform-server-functions.js +272 -24
- package/dinou/core/tsconfig-aliases.js +964 -0
- package/dinou/core/uneject.js +640 -0
- package/dinou/core/url-resolver.js +1 -1
- package/dinou/core/vfs.js +11 -2
- package/dinou/css.d.ts +4 -1
- package/dinou/deno/build.js +205 -153
- package/dinou/deno/seed-kv.js +4 -0
- package/dinou/env.d.ts +22 -2
- package/dinou/esbuild/build.js +40 -50
- package/dinou/esbuild/dev.js +33 -7
- package/dinou/esbuild/helpers-esbuild/get-config-esbuild-prod.js +41 -16
- package/dinou/esbuild/helpers-esbuild/get-config-esbuild.js +51 -49
- package/dinou/esbuild/helpers-esbuild/get-esbuild-entries.js +73 -60
- package/dinou/esbuild/helpers-esbuild/swc-disk-cache.js +35 -16
- package/dinou/esbuild/helpers-esbuild/write.js +13 -3
- package/dinou/esbuild/plugins-esbuild/assets-plugin.js +100 -238
- package/dinou/esbuild/plugins-esbuild/babel-react-compiler-plugin.js +73 -29
- package/dinou/esbuild/plugins-esbuild/css-processor-plugin.js +283 -91
- package/dinou/esbuild/plugins-esbuild/manifest-generator-plugin.js +3 -5
- package/dinou/esbuild/plugins-esbuild/react-client-manifest-plugin.js +9 -3
- package/dinou/esbuild/plugins-esbuild/server-functions-plugin.js +4 -3
- package/dinou/esbuild/plugins-esbuild/stable-chunk-names-and-maps-plugin.js +10 -1
- package/dinou/esbuild/plugins-postcss/postcss-extract-plugin.js +84 -21
- package/dinou/esbuild/react-refresh/esm-hmr/client.js +31 -10
- package/dinou/esbuild/react-refresh/esm-hmr-plugin.js +48 -18
- package/dinou/esbuild/react-refresh/is-react-refresh-boundary.js +9 -1
- package/dinou/esbuild/react-refresh/react-refresh-runtime.js +13 -7
- package/dinou/index.d.ts +1028 -88
- package/dinou/node/build.js +138 -74
- package/dinou/node/bundle-dual-engine.js +393 -131
- package/dinou/node/dev.js +1005 -275
- package/dinou/package.json +11 -34
- package/dinou/rollup/postcss.config.js +3 -4
- package/dinou/rollup/react-refresh/esm-hmr/client.js +31 -10
- package/dinou/rollup/react-refresh/react-refresh-wrap-modules.js +16 -13
- package/dinou/rollup/react-refresh/rollup-plugin-esm-hmr.js +53 -7
- package/dinou/rollup/rollup-plugins/dinou-asset-plugin.js +58 -20
- package/dinou/rollup/rollup-plugins/manifest-generator-plugin.js +16 -7
- package/dinou/rollup/rollup-plugins/rollup-plugin-react-client-manifest.js +118 -42
- package/dinou/rollup/rollup-plugins/rollup-plugin-server-functions.js +48 -36
- package/dinou/rollup/rollup.config.js +174 -101
- package/dinou/scripts/build-vendors.js +123 -33
- package/dinou/server.d.ts +126 -15
- package/dinou/webpack/helpers/get-webpack-entries.js +87 -41
- package/dinou/webpack/helpers/patch-webpack-internals.js +93 -1
- package/dinou/webpack/loaders/server-functions-loader.js +1 -1
- package/dinou/webpack/plugins/manifest-generator-plugin.js +27 -7
- package/dinou/webpack/plugins/server-functions-plugin.js +39 -12
- package/dinou/webpack/plugins/webpack-memory-plugin.js +25 -12
- package/dinou/webpack/postcss.config.js +3 -4
- package/dinou/webpack/webpack.config.js +408 -132
- package/eject.js +6 -224
- package/package.json +7 -9
- package/dinou/compiled/cross-env/index.js +0 -661
- package/dinou/compiled/dotenv/index.js +0 -375
- package/dinou/compiled/esbuild-copy-static-files/index.js +0 -63
- package/dinou/compiled/esbuild-tsconfig-paths/index.js +0 -1314
- package/dinou/compiled/rollup-plugin-copy/index.js +0 -12066
- package/dinou/compiled/rollup-plugin-tsconfig-paths/index.js +0 -569
- package/dinou/compiled/tsconfig-paths-webpack-plugin/index.js +0 -3956
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
// dinou/core/env.js
|
|
2
|
+
// Server & build-time .env* hierarchy loader, live dev hot-reloader,
|
|
3
|
+
// TypeScript .env key scanner, and DINOU_PUBLIC_* bundler replacement builder.
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { DINOU_PUBLIC_PREFIX, isDotEnvFileName } from "./runtime-env.js";
|
|
8
|
+
|
|
9
|
+
const ENV_LOADER_STATE_KEY = Symbol.for("dinou.env.loaderState");
|
|
10
|
+
|
|
11
|
+
function getLoaderStateMap() {
|
|
12
|
+
if (typeof globalThis === "undefined") return new Map();
|
|
13
|
+
if (!globalThis[ENV_LOADER_STATE_KEY]) {
|
|
14
|
+
globalThis[ENV_LOADER_STATE_KEY] = new Map();
|
|
15
|
+
}
|
|
16
|
+
return globalThis[ENV_LOADER_STATE_KEY];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const LINE_KEY_REGEX = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_.]*)\s*=\s*(.*)$/;
|
|
20
|
+
const VALID_TS_ENV_IDENT_REGEX = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
21
|
+
const RESERVED_PROCESS_ENV_KEYS = new Set(["NODE_ENV", "TZ"]);
|
|
22
|
+
|
|
23
|
+
function unescapeDoubleQuotedValue(raw) {
|
|
24
|
+
return raw.replace(/\\([nrt"'\\])/g, (_match, ch) => {
|
|
25
|
+
switch (ch) {
|
|
26
|
+
case "n":
|
|
27
|
+
return "\n";
|
|
28
|
+
case "r":
|
|
29
|
+
return "\r";
|
|
30
|
+
case "t":
|
|
31
|
+
return "\t";
|
|
32
|
+
case '"':
|
|
33
|
+
return '"';
|
|
34
|
+
case "'":
|
|
35
|
+
return "'";
|
|
36
|
+
case "\\":
|
|
37
|
+
return "\\";
|
|
38
|
+
default:
|
|
39
|
+
return ch;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function findClosingQuoteIndex(str, quoteChar, allowEscapes) {
|
|
45
|
+
for (let i = 0; i < str.length; i++) {
|
|
46
|
+
const ch = str[i];
|
|
47
|
+
if (allowEscapes && ch === "\\") {
|
|
48
|
+
i++;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (ch === quoteChar) {
|
|
52
|
+
return i;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return -1;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function stripUnquotedInlineComment(val) {
|
|
59
|
+
for (let i = 0; i < val.length; i++) {
|
|
60
|
+
if (val[i] === "#" && (i === 0 || /\s/.test(val[i - 1]))) {
|
|
61
|
+
return val.slice(0, i).trim();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return val.trim();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Parses `.env` file contents compatibly with `dotenv@16` (UTF-8 BOM, comments,
|
|
69
|
+
* optional `export `, single/double/backtick quotes, multiline strings, escapes).
|
|
70
|
+
*
|
|
71
|
+
* @param {string} rawContent
|
|
72
|
+
* @returns {Record<string, string>}
|
|
73
|
+
*/
|
|
74
|
+
export function parseDotEnv(rawContent) {
|
|
75
|
+
const result = {};
|
|
76
|
+
if (typeof rawContent !== "string" || rawContent.length === 0) {
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const normalized = rawContent
|
|
81
|
+
.replace(/^\uFEFF/, "")
|
|
82
|
+
.replace(/\r\n?/g, "\n");
|
|
83
|
+
const lines = normalized.split("\n");
|
|
84
|
+
|
|
85
|
+
for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
|
|
86
|
+
const line = lines[lineIdx];
|
|
87
|
+
const trimmed = line.trim();
|
|
88
|
+
if (!trimmed || trimmed.startsWith("#")) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const match = LINE_KEY_REGEX.exec(line);
|
|
93
|
+
if (!match) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const key = match[1];
|
|
98
|
+
const rest = match[2] ?? "";
|
|
99
|
+
|
|
100
|
+
if (rest.length === 0) {
|
|
101
|
+
result[key] = "";
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const firstChar = rest[0];
|
|
106
|
+
if (firstChar === '"' || firstChar === "'" || firstChar === "`") {
|
|
107
|
+
const allowEscapes = firstChar === '"' || firstChar === "`";
|
|
108
|
+
let afterOpen = rest.slice(1);
|
|
109
|
+
let closeIdx = findClosingQuoteIndex(afterOpen, firstChar, allowEscapes);
|
|
110
|
+
|
|
111
|
+
if (closeIdx !== -1) {
|
|
112
|
+
const inner = afterOpen.slice(0, closeIdx);
|
|
113
|
+
result[key] =
|
|
114
|
+
firstChar === '"' ? unescapeDoubleQuotedValue(inner) : inner;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Multiline quoted value
|
|
119
|
+
const chunks = [afterOpen];
|
|
120
|
+
while (lineIdx + 1 < lines.length) {
|
|
121
|
+
lineIdx++;
|
|
122
|
+
const nextLine = lines[lineIdx];
|
|
123
|
+
const nextClose = findClosingQuoteIndex(
|
|
124
|
+
nextLine,
|
|
125
|
+
firstChar,
|
|
126
|
+
allowEscapes
|
|
127
|
+
);
|
|
128
|
+
if (nextClose !== -1) {
|
|
129
|
+
chunks.push(nextLine.slice(0, nextClose));
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
chunks.push(nextLine);
|
|
133
|
+
}
|
|
134
|
+
const joined = chunks.join("\n");
|
|
135
|
+
result[key] =
|
|
136
|
+
firstChar === '"' ? unescapeDoubleQuotedValue(joined) : joined;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
result[key] = stripUnquotedInlineComment(rest);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return result;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Returns the ordered `.env*` file names from highest priority to lowest priority
|
|
148
|
+
* for a given `mode` (`development`, `production`, `test`, etc.).
|
|
149
|
+
*
|
|
150
|
+
* Precedence (after real OS / CLI environment variables):
|
|
151
|
+
* 1. `.env.<mode>.local`
|
|
152
|
+
* 2. `.env.local` (skipped when `mode === "test"`)
|
|
153
|
+
* 3. `.env.<mode>`
|
|
154
|
+
* 4. `.env`
|
|
155
|
+
*/
|
|
156
|
+
export function getEnvFileHierarchy(mode) {
|
|
157
|
+
const resolvedMode =
|
|
158
|
+
String(
|
|
159
|
+
mode ||
|
|
160
|
+
(typeof process !== "undefined" && process.env?.NODE_ENV) ||
|
|
161
|
+
"development"
|
|
162
|
+
).trim() || "development";
|
|
163
|
+
|
|
164
|
+
const candidates = [
|
|
165
|
+
`.env.${resolvedMode}.local`,
|
|
166
|
+
...(resolvedMode !== "test" ? [".env.local"] : []),
|
|
167
|
+
`.env.${resolvedMode}`,
|
|
168
|
+
".env",
|
|
169
|
+
];
|
|
170
|
+
|
|
171
|
+
return Array.from(new Set(candidates));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Reads and merges `.env*` files from `projectRoot` without mutating `process.env`.
|
|
176
|
+
*/
|
|
177
|
+
export function readEnvFilesMap(projectRoot = ".", options = {}) {
|
|
178
|
+
const cwd = path.resolve(
|
|
179
|
+
projectRoot ||
|
|
180
|
+
(typeof process !== "undefined" && typeof process.cwd === "function"
|
|
181
|
+
? process.cwd()
|
|
182
|
+
: ".")
|
|
183
|
+
);
|
|
184
|
+
const mode =
|
|
185
|
+
String(
|
|
186
|
+
options.mode ||
|
|
187
|
+
(typeof process !== "undefined" && process.env?.NODE_ENV) ||
|
|
188
|
+
"development"
|
|
189
|
+
).trim() || "development";
|
|
190
|
+
|
|
191
|
+
const hierarchy = getEnvFileHierarchy(mode);
|
|
192
|
+
const mergedValues = {};
|
|
193
|
+
const loadedFiles = [];
|
|
194
|
+
|
|
195
|
+
for (const fileName of hierarchy) {
|
|
196
|
+
const filePath = path.join(cwd, fileName);
|
|
197
|
+
try {
|
|
198
|
+
if (!fs.existsSync(filePath)) continue;
|
|
199
|
+
const stat = fs.statSync(filePath);
|
|
200
|
+
if (!stat.isFile()) continue;
|
|
201
|
+
const raw = fs.readFileSync(filePath, "utf8");
|
|
202
|
+
const parsed = parseDotEnv(raw);
|
|
203
|
+
loadedFiles.push(fileName);
|
|
204
|
+
for (const [k, v] of Object.entries(parsed)) {
|
|
205
|
+
if (!(k in mergedValues)) {
|
|
206
|
+
mergedValues[k] = v;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
} catch {}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
cwd,
|
|
214
|
+
mode,
|
|
215
|
+
loadedFiles,
|
|
216
|
+
values: mergedValues,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Collects currently active `DINOU_PUBLIC_*` environment variables from `process.env`
|
|
222
|
+
* and `Deno.env` (if running under Deno).
|
|
223
|
+
*/
|
|
224
|
+
export function getPublicEnvVars() {
|
|
225
|
+
const pub = {};
|
|
226
|
+
|
|
227
|
+
if (
|
|
228
|
+
typeof Deno !== "undefined" &&
|
|
229
|
+
Deno &&
|
|
230
|
+
Deno.env &&
|
|
231
|
+
typeof Deno.env.toObject === "function"
|
|
232
|
+
) {
|
|
233
|
+
try {
|
|
234
|
+
const denoVars = Deno.env.toObject();
|
|
235
|
+
for (const key of Object.keys(denoVars).sort()) {
|
|
236
|
+
if (key.startsWith(DINOU_PUBLIC_PREFIX) && denoVars[key] !== undefined) {
|
|
237
|
+
pub[key] = String(denoVars[key]);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
} catch {}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (
|
|
244
|
+
typeof process !== "undefined" &&
|
|
245
|
+
process &&
|
|
246
|
+
process.env &&
|
|
247
|
+
typeof process.env === "object"
|
|
248
|
+
) {
|
|
249
|
+
for (const key of Object.keys(process.env).sort()) {
|
|
250
|
+
if (
|
|
251
|
+
key.startsWith(DINOU_PUBLIC_PREFIX) &&
|
|
252
|
+
process.env[key] !== undefined
|
|
253
|
+
) {
|
|
254
|
+
pub[key] = String(process.env[key]);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return pub;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Loads the `.env*` hierarchy from `projectRoot` into `process.env` (and `Deno.env`
|
|
264
|
+
* when available), never overriding real OS/CLI environment variables already set
|
|
265
|
+
* before Dinou started. Supports `{ reload: true }` for hot-reloading `.env*` edits
|
|
266
|
+
* during `dinou dev`.
|
|
267
|
+
*/
|
|
268
|
+
export function loadEnvFiles(projectRoot, options = {}) {
|
|
269
|
+
const resolvedRoot = path.resolve(
|
|
270
|
+
projectRoot ||
|
|
271
|
+
(typeof process !== "undefined" && typeof process.cwd === "function"
|
|
272
|
+
? process.cwd()
|
|
273
|
+
: ".")
|
|
274
|
+
);
|
|
275
|
+
const mode =
|
|
276
|
+
String(
|
|
277
|
+
options.mode ||
|
|
278
|
+
(typeof process !== "undefined" && process.env?.NODE_ENV) ||
|
|
279
|
+
"development"
|
|
280
|
+
).trim() || "development";
|
|
281
|
+
|
|
282
|
+
const stateMap = getLoaderStateMap();
|
|
283
|
+
const prevState = stateMap.get(resolvedRoot);
|
|
284
|
+
|
|
285
|
+
if (prevState && prevState.mode === mode && !options.reload) {
|
|
286
|
+
return {
|
|
287
|
+
cwd: resolvedRoot,
|
|
288
|
+
mode,
|
|
289
|
+
loadedFiles: prevState.loadedFiles,
|
|
290
|
+
values: prevState.values,
|
|
291
|
+
publicEnv: getPublicEnvVars(),
|
|
292
|
+
changed: false,
|
|
293
|
+
publicEnvChanged: false,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// If reloading or switching mode in the same process, remove keys previously injected from .env*
|
|
298
|
+
if (prevState) {
|
|
299
|
+
if (
|
|
300
|
+
typeof process !== "undefined" &&
|
|
301
|
+
process &&
|
|
302
|
+
process.env &&
|
|
303
|
+
prevState.injectedKeys
|
|
304
|
+
) {
|
|
305
|
+
for (const key of prevState.injectedKeys) {
|
|
306
|
+
try {
|
|
307
|
+
delete process.env[key];
|
|
308
|
+
} catch {}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (
|
|
312
|
+
typeof Deno !== "undefined" &&
|
|
313
|
+
Deno &&
|
|
314
|
+
Deno.env &&
|
|
315
|
+
typeof Deno.env.delete === "function" &&
|
|
316
|
+
prevState.injectedDenoKeys
|
|
317
|
+
) {
|
|
318
|
+
for (const key of prevState.injectedDenoKeys) {
|
|
319
|
+
try {
|
|
320
|
+
Deno.env.delete(key);
|
|
321
|
+
} catch {}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const { loadedFiles, values } = readEnvFilesMap(resolvedRoot, { mode });
|
|
327
|
+
const injectedKeys = new Set();
|
|
328
|
+
const injectedDenoKeys = new Set();
|
|
329
|
+
|
|
330
|
+
for (const [key, val] of Object.entries(values)) {
|
|
331
|
+
if (
|
|
332
|
+
typeof process !== "undefined" &&
|
|
333
|
+
process &&
|
|
334
|
+
process.env &&
|
|
335
|
+
process.env[key] === undefined
|
|
336
|
+
) {
|
|
337
|
+
try {
|
|
338
|
+
process.env[key] = val;
|
|
339
|
+
injectedKeys.add(key);
|
|
340
|
+
} catch {}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (
|
|
344
|
+
typeof Deno !== "undefined" &&
|
|
345
|
+
Deno &&
|
|
346
|
+
Deno.env &&
|
|
347
|
+
typeof Deno.env.get === "function" &&
|
|
348
|
+
typeof Deno.env.set === "function"
|
|
349
|
+
) {
|
|
350
|
+
try {
|
|
351
|
+
if (Deno.env.get(key) === undefined) {
|
|
352
|
+
Deno.env.set(key, val);
|
|
353
|
+
injectedDenoKeys.add(key);
|
|
354
|
+
}
|
|
355
|
+
} catch {}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const publicEnv = getPublicEnvVars();
|
|
360
|
+
const publicSnapshot = JSON.stringify(publicEnv);
|
|
361
|
+
const allSnapshot = JSON.stringify(values);
|
|
362
|
+
|
|
363
|
+
const changed = prevState ? prevState.allSnapshot !== allSnapshot : true;
|
|
364
|
+
const publicEnvChanged = prevState
|
|
365
|
+
? prevState.publicSnapshot !== publicSnapshot
|
|
366
|
+
: Object.keys(publicEnv).length > 0;
|
|
367
|
+
|
|
368
|
+
stateMap.set(resolvedRoot, {
|
|
369
|
+
mode,
|
|
370
|
+
loadedFiles,
|
|
371
|
+
values,
|
|
372
|
+
injectedKeys,
|
|
373
|
+
injectedDenoKeys,
|
|
374
|
+
publicSnapshot,
|
|
375
|
+
allSnapshot,
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
return {
|
|
379
|
+
cwd: resolvedRoot,
|
|
380
|
+
mode,
|
|
381
|
+
loadedFiles,
|
|
382
|
+
values,
|
|
383
|
+
publicEnv,
|
|
384
|
+
changed,
|
|
385
|
+
publicEnvChanged,
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Scans `.env*` and `.env.example` files in `projectRoot` and returns ONLY valid
|
|
391
|
+
* TypeScript identifier key names (never values) for `.dinou/types/routes.d.ts`.
|
|
392
|
+
*/
|
|
393
|
+
export function collectDeclaredEnvKeys(projectRoot = ".") {
|
|
394
|
+
const cwd = path.resolve(
|
|
395
|
+
projectRoot ||
|
|
396
|
+
(typeof process !== "undefined" && typeof process.cwd === "function"
|
|
397
|
+
? process.cwd()
|
|
398
|
+
: ".")
|
|
399
|
+
);
|
|
400
|
+
const keys = new Set();
|
|
401
|
+
|
|
402
|
+
try {
|
|
403
|
+
if (!fs.existsSync(cwd)) return [];
|
|
404
|
+
const entries = fs.readdirSync(cwd, { withFileTypes: true });
|
|
405
|
+
for (const entry of entries) {
|
|
406
|
+
if (!entry.isFile()) continue;
|
|
407
|
+
if (!isDotEnvFileName(entry.name)) continue;
|
|
408
|
+
try {
|
|
409
|
+
const raw = fs.readFileSync(path.join(cwd, entry.name), "utf8");
|
|
410
|
+
const parsed = parseDotEnv(raw);
|
|
411
|
+
for (const key of Object.keys(parsed)) {
|
|
412
|
+
if (
|
|
413
|
+
VALID_TS_ENV_IDENT_REGEX.test(key) &&
|
|
414
|
+
!RESERVED_PROCESS_ENV_KEYS.has(key)
|
|
415
|
+
) {
|
|
416
|
+
keys.add(key);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
} catch {}
|
|
420
|
+
}
|
|
421
|
+
} catch {}
|
|
422
|
+
|
|
423
|
+
return Array.from(keys).sort();
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Builds the sanitized public environment dictionary `{ NODE_ENV, ...DINOU_PUBLIC_*, ...extra }`
|
|
428
|
+
* safe for Client Components and SSR Pass B (`ssr-engine`).
|
|
429
|
+
*/
|
|
430
|
+
export function getPublicEnvObject(options = {}) {
|
|
431
|
+
const mode =
|
|
432
|
+
String(
|
|
433
|
+
options.mode ||
|
|
434
|
+
(typeof process !== "undefined" && process.env?.NODE_ENV) ||
|
|
435
|
+
"development"
|
|
436
|
+
).trim() || "development";
|
|
437
|
+
|
|
438
|
+
const publicVars = getPublicEnvVars();
|
|
439
|
+
const obj = {
|
|
440
|
+
NODE_ENV: mode,
|
|
441
|
+
...publicVars,
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
if (options.extra && typeof options.extra === "object") {
|
|
445
|
+
for (const [k, v] of Object.entries(options.extra)) {
|
|
446
|
+
if (v !== undefined) {
|
|
447
|
+
obj[k] = String(v);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
return obj;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Generates Esbuild `define` entries for Client bundles and Dual-Engine passes.
|
|
457
|
+
* - When `defineProcessEnvObject !== false` (Client bundles & Pass B SSR), also defines
|
|
458
|
+
* `"process.env"` to the sanitized `publicEnvObject` so non-public `process.env.*`
|
|
459
|
+
* evaluates safely to `undefined` without leaking server secrets or throwing `ReferenceError`.
|
|
460
|
+
* - When `defineProcessEnvObject === false` (Pass A RSC server bundles), only defines
|
|
461
|
+
* `NODE_ENV`, `DINOU_PUBLIC_*`, and `extra` keys while keeping `process.env` live for secrets.
|
|
462
|
+
*/
|
|
463
|
+
export function getEsbuildPublicEnvDefines(options = {}) {
|
|
464
|
+
const defineProcessEnvObject = options.defineProcessEnvObject !== false;
|
|
465
|
+
const pubObj = getPublicEnvObject(options);
|
|
466
|
+
const defines = {};
|
|
467
|
+
|
|
468
|
+
for (const key of Object.keys(pubObj).sort()) {
|
|
469
|
+
defines[`process.env.${key}`] = JSON.stringify(pubObj[key]);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (defineProcessEnvObject) {
|
|
473
|
+
defines["process.env"] = JSON.stringify(pubObj);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return defines;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Generates Rollup `@rollup/plugin-replace` maps:
|
|
481
|
+
* - `exactValues`: replaces `process.env.NODE_ENV` and `process.env.DINOU_PUBLIC_*`
|
|
482
|
+
* - `objectValues`: replaces remaining standalone/member `process.env` expressions
|
|
483
|
+
* using `delimiters: ["(?<!\\.)\\b", "\\b"]` so `process.env.SECRET` evaluates to `undefined`.
|
|
484
|
+
*/
|
|
485
|
+
export function getRollupPublicEnvReplacements(options = {}) {
|
|
486
|
+
const pubObj = getPublicEnvObject(options);
|
|
487
|
+
const exactValues = {};
|
|
488
|
+
for (const key of Object.keys(pubObj).sort()) {
|
|
489
|
+
exactValues[`process.env.${key}`] = JSON.stringify(pubObj[key]);
|
|
490
|
+
}
|
|
491
|
+
return {
|
|
492
|
+
exactValues,
|
|
493
|
+
objectValues: {
|
|
494
|
+
"process.env": `(${JSON.stringify(pubObj)})`,
|
|
495
|
+
},
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Generates Webpack `DefinePlugin` definitions for Client bundles (both dev and prod).
|
|
501
|
+
* Wraps `"process.env"` in parentheses `({...})` so statement-position expressions
|
|
502
|
+
* (e.g. `process.env[key] = val` analyzed by Webpack's production `ConcatenatedModule`
|
|
503
|
+
* parser) parse as valid parenthesized object expressions rather than block statements.
|
|
504
|
+
*/
|
|
505
|
+
export function getWebpackPublicEnvDefines(options = {}) {
|
|
506
|
+
const defines = getEsbuildPublicEnvDefines({
|
|
507
|
+
...options,
|
|
508
|
+
defineProcessEnvObject: true,
|
|
509
|
+
});
|
|
510
|
+
if (defines["process.env"]) {
|
|
511
|
+
defines["process.env"] = `(${defines["process.env"]})`;
|
|
512
|
+
}
|
|
513
|
+
return defines;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Returns true when `filePath` is a `.env` or `.env.*` file in the root directory.
|
|
518
|
+
*/
|
|
519
|
+
export function isWatchedDotEnvFile(filePath, projectRoot = ".") {
|
|
520
|
+
if (!filePath || typeof filePath !== "string") return false;
|
|
521
|
+
const absFile = path.resolve(filePath);
|
|
522
|
+
const absRoot = path.resolve(projectRoot);
|
|
523
|
+
if (path.dirname(absFile).toLowerCase() !== absRoot.toLowerCase()) {
|
|
524
|
+
return false;
|
|
525
|
+
}
|
|
526
|
+
return isDotEnvFileName(path.basename(absFile));
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
export default {
|
|
530
|
+
DINOU_PUBLIC_PREFIX,
|
|
531
|
+
isDotEnvFileName,
|
|
532
|
+
parseDotEnv,
|
|
533
|
+
getEnvFileHierarchy,
|
|
534
|
+
readEnvFilesMap,
|
|
535
|
+
getPublicEnvVars,
|
|
536
|
+
loadEnvFiles,
|
|
537
|
+
collectDeclaredEnvKeys,
|
|
538
|
+
getPublicEnvObject,
|
|
539
|
+
getEsbuildPublicEnvDefines,
|
|
540
|
+
getRollupPublicEnvReplacements,
|
|
541
|
+
getWebpackPublicEnvDefines,
|
|
542
|
+
isWatchedDotEnvFile,
|
|
543
|
+
};
|
|
544
|
+
|
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
// dinou/core/export-static.
|
|
2
|
-
// Exports a standalone static website from .dinou/dist3 (assets) and .dinou/dist2 (SSG HTML)
|
|
1
|
+
// dinou/core/export-static.js
|
|
2
|
+
// Exports a standalone static website from .dinou/dist3 (assets) and .dinou/dist2 (SSG HTML + segmented RSC)
|
|
3
3
|
// into an 'out/' directory, ready for Surge.sh, GitHub Pages, Cloudflare Pages (static), etc.
|
|
4
4
|
|
|
5
5
|
import fs from "node:fs";
|
|
6
6
|
import path from "node:path";
|
|
7
|
+
import { loadEnvFiles } from "./env.js";
|
|
8
|
+
import ensureVendorMode from "./ensure-vendor-mode.js";
|
|
9
|
+
import {
|
|
10
|
+
shouldExportDist3Entry,
|
|
11
|
+
shouldExportDist2File,
|
|
12
|
+
injectStaticExportFlag,
|
|
13
|
+
} from "./static-assets.js";
|
|
7
14
|
|
|
8
15
|
const projectRoot = process.cwd();
|
|
16
|
+
loadEnvFiles(projectRoot, { mode: "production" });
|
|
17
|
+
if (!ensureVendorMode()) {
|
|
18
|
+
await new Promise(() => {});
|
|
19
|
+
}
|
|
9
20
|
const outDir = path.resolve(projectRoot, "out");
|
|
10
21
|
const dist3Dir = path.resolve(projectRoot, ".dinou/dist3");
|
|
11
22
|
const dist2Dir = path.resolve(projectRoot, ".dinou/dist2");
|
|
@@ -19,18 +30,29 @@ if (fs.existsSync(outDir)) {
|
|
|
19
30
|
}
|
|
20
31
|
fs.mkdirSync(outDir, { recursive: true });
|
|
21
32
|
|
|
22
|
-
function copyRecursive(src, dest) {
|
|
33
|
+
function copyRecursive(src, dest, filterFile = null, transformFile = null, relPrefix = "") {
|
|
23
34
|
if (!fs.existsSync(src)) return 0;
|
|
24
35
|
let count = 0;
|
|
25
36
|
const entries = fs.readdirSync(src, { withFileTypes: true });
|
|
26
37
|
for (const entry of entries) {
|
|
38
|
+
const relPosix = relPrefix ? `${relPrefix}/${entry.name}` : entry.name;
|
|
27
39
|
const srcPath = path.join(src, entry.name);
|
|
28
40
|
const destPath = path.join(dest, entry.name);
|
|
29
41
|
if (entry.isDirectory()) {
|
|
30
|
-
|
|
31
|
-
count += copyRecursive(srcPath, destPath);
|
|
42
|
+
count += copyRecursive(srcPath, destPath, filterFile, transformFile, relPosix);
|
|
32
43
|
} else {
|
|
44
|
+
if (filterFile && !filterFile(relPosix)) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
33
47
|
fs.mkdirSync(path.dirname(destPath), { recursive: true });
|
|
48
|
+
if (transformFile) {
|
|
49
|
+
const transformed = transformFile(srcPath, relPosix);
|
|
50
|
+
if (typeof transformed === "string") {
|
|
51
|
+
fs.writeFileSync(destPath, transformed, "utf8");
|
|
52
|
+
count++;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
34
56
|
fs.copyFileSync(srcPath, destPath);
|
|
35
57
|
count++;
|
|
36
58
|
}
|
|
@@ -38,19 +60,29 @@ function copyRecursive(src, dest) {
|
|
|
38
60
|
return count;
|
|
39
61
|
}
|
|
40
62
|
|
|
41
|
-
// 2. Copy production assets from .dinou/dist3 (JS bundles, CSS,
|
|
63
|
+
// 2. Copy production assets from .dinou/dist3 (JS bundles, CSS, public/)
|
|
42
64
|
let assetCount = 0;
|
|
43
65
|
if (fs.existsSync(dist3Dir)) {
|
|
44
|
-
assetCount = copyRecursive(dist3Dir, outDir);
|
|
66
|
+
assetCount = copyRecursive(dist3Dir, outDir, shouldExportDist3Entry);
|
|
45
67
|
console.log(` 📦 Copied ${assetCount} production asset(s) from .dinou/dist3`);
|
|
46
68
|
} else {
|
|
47
69
|
console.warn("⚠️ [Dinou Export] Warning: .dinou/dist3 not found. Did you run a production build first?");
|
|
48
70
|
}
|
|
49
71
|
|
|
50
|
-
// 3. Copy pre-rendered SSG HTML pages from .dinou/dist2
|
|
72
|
+
// 3. Copy pre-rendered SSG HTML pages + segmented RSC payloads (index.html, page.rsc, layout.rsc) from .dinou/dist2
|
|
51
73
|
let pageCount = 0;
|
|
52
74
|
if (fs.existsSync(dist2Dir)) {
|
|
53
|
-
pageCount = copyRecursive(
|
|
75
|
+
pageCount = copyRecursive(
|
|
76
|
+
dist2Dir,
|
|
77
|
+
outDir,
|
|
78
|
+
shouldExportDist2File,
|
|
79
|
+
(srcPath, relPosix) => {
|
|
80
|
+
if (relPosix.toLowerCase().endsWith("index.html")) {
|
|
81
|
+
return injectStaticExportFlag(fs.readFileSync(srcPath, "utf8"));
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
);
|
|
54
86
|
console.log(` 📄 Copied ${pageCount} static page/payload file(s) from .dinou/dist2`);
|
|
55
87
|
} else {
|
|
56
88
|
console.warn("⚠️ [Dinou Export] Warning: .dinou/dist2 not found. Ensure your app has static routes.");
|