dinou 9.1.0 → 9.3.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.
@@ -3,6 +3,7 @@
3
3
 
4
4
  import { pathToFileURL } from "node:url";
5
5
  import { createRequire } from "node:module";
6
+ import { ensureServerHooksPatched } from "./render-jsx-to-client-jsx.js";
6
7
 
7
8
  const require = createRequire(import.meta.url);
8
9
 
@@ -55,6 +56,8 @@ function getEdgeRenderer() {
55
56
  * @returns {ReadableStream | { pipe: Function, abort: Function }}
56
57
  */
57
58
  function renderRSCStream(model, manifest, options = {}) {
59
+ ensureServerHooksPatched();
60
+
58
61
  if (isEdgeRuntime(options)) {
59
62
  const edge = getEdgeRenderer();
60
63
  const clientManifest =
@@ -313,7 +313,7 @@ if (
313
313
  entry.pendingPromise = promise;
314
314
  entry.pendingReqId = reqId;
315
315
  const baseRef = entry.baseChildren;
316
- promise.then(
316
+ const settledPromise = promise.then(
317
317
  (resolved) => {
318
318
  if (entry.latestReqId !== reqId) {
319
319
  return;
@@ -335,6 +335,9 @@ if (
335
335
  }
336
336
  }
337
337
  );
338
+ if (Array.isArray(e?.detail?.promises)) {
339
+ e.detail.promises.push(settledPromise);
340
+ }
338
341
  }
339
342
  }
340
343
  }
@@ -459,7 +462,7 @@ export function DinouCacheSlotBoundary({ id, tags, children }) {
459
462
  curEntry.pendingPromise = promise;
460
463
  curEntry.pendingReqId = reqId;
461
464
  }
462
- promise.then(
465
+ const settledPromise = promise.then(
463
466
  (resolved) => {
464
467
  if (curEntry && curEntry.latestReqId !== reqId) {
465
468
  return;
@@ -476,6 +479,9 @@ export function DinouCacheSlotBoundary({ id, tags, children }) {
476
479
  console.error(`[Dinou] Failed to refresh slot "${id}":`, err);
477
480
  }
478
481
  );
482
+ if (Array.isArray(e?.detail?.promises)) {
483
+ e.detail.promises.push(settledPromise);
484
+ }
479
485
  } else if (promise) {
480
486
  if (curEntry) {
481
487
  curEntry.pendingPromise = null;
@@ -1,242 +1,242 @@
1
- import fs from "node:fs/promises";
2
- import fsSync from "node:fs";
3
- import path from "node:path";
4
- import glob from "fast-glob";
5
- import { transform } from "@swc/core";
6
- import { useClientRegex, useServerRegex } from "../../constants.js";
7
- import { hasServerFunctions, extractServerFunctions } from "../../core/transform-server-functions.js";
8
- import { extensions as assetExtensions } from "../../core/asset-extensions.js";
9
-
10
- function isServerFunctionsModule(raw, filename) {
11
- const trimmed = raw.trim();
12
- if (useClientRegex.test(trimmed)) return false;
13
- if (useServerRegex.test(trimmed)) return true;
14
- if (hasServerFunctions(raw)) {
15
- const actions = extractServerFunctions(raw, filename);
16
- return Boolean(actions && actions.length > 0);
17
- }
18
- return false;
19
- }
20
-
21
- const styleExtensions = ["css", "scss", "less", "sass"];
22
- const fontExtensions = ["woff", "woff2", "ttf", "eot", "otf"];
23
- const redirectExtensions = [...styleExtensions, ...assetExtensions, ...fontExtensions];
24
- const redirectFilter = new RegExp(`\\.(${redirectExtensions.join("|")})$`, "i");
25
-
26
- const normPath = (p) => {
27
- if (!p) return "";
28
- let s = path.resolve(p).replace(/\\/g, "/");
29
- if (process.platform === "win32") {
30
- s = s.replace(/^([a-zA-Z]):/, (_, d) => d.toLowerCase() + ":");
31
- }
32
- return s;
33
- };
34
-
35
- export function getMirrorPath(srcPath, projectRoot = process.cwd()) {
36
- if (!srcPath) return "";
37
- const norm = normPath(srcPath);
38
- const rootNorm = normPath(projectRoot);
39
- if (!norm.startsWith(rootNorm)) return srcPath;
40
- const rel = norm.slice(rootNorm.length + 1);
41
- const targetRel = rel.replace(/\.[jt]sx?$/, ".js");
42
- return path.resolve(projectRoot, ".dinou/swc", targetRel);
43
- }
44
-
45
- export function getOriginalPath(mirrorPath, projectRoot = process.cwd()) {
46
- if (!mirrorPath) return "";
47
- const norm = normPath(mirrorPath);
48
- const rootNorm = normPath(projectRoot);
49
- const swcPrefix = `${rootNorm}/.dinou/swc/`;
50
- if (!norm.startsWith(swcPrefix)) return mirrorPath;
51
- const rel = norm.slice(swcPrefix.length);
52
- const base = path.resolve(projectRoot, rel.replace(/\.js$/, ""));
53
- for (const ext of [".tsx", ".ts", ".jsx", ".js"]) {
54
- const candidate = base + ext;
55
- if (fsSync.existsSync(candidate)) return candidate;
56
- }
57
- return base + ".tsx";
58
- }
59
-
60
- export async function transformToDisk(srcPath, projectRoot = process.cwd()) {
61
- const normSrc = path.resolve(srcPath);
62
- if (!fsSync.existsSync(normSrc)) return null;
63
-
64
- const raw = await fs.readFile(normSrc, "utf8");
65
- const targetPath = getMirrorPath(normSrc, projectRoot);
66
- // Skip server functions - handled by serverFunctionsPlugin proxy
67
- if (isServerFunctionsModule(raw, normSrc)) {
68
- try {
69
- if (fsSync.existsSync(targetPath)) await fs.unlink(targetPath);
70
- } catch (e) {}
71
- return null;
72
- }
73
-
74
- try {
75
- const { code } = await transform(raw, {
76
- filename: normSrc,
77
- sourceMaps: "inline",
78
- jsc: {
79
- parser: { syntax: "typescript", tsx: true, dynamicImport: true },
80
- target: "es2022",
81
- transform: {
82
- react: {
83
- refresh: true,
84
- development: true,
85
- runtime: "automatic",
86
- },
87
- },
88
- },
89
- });
90
- await fs.mkdir(path.dirname(targetPath), { recursive: true });
91
- await fs.writeFile(targetPath, code, "utf8");
92
- return targetPath;
93
- } catch (err) {
94
- await fs.mkdir(path.dirname(targetPath), { recursive: true });
95
- await fs.writeFile(targetPath, raw, "utf8");
96
- return targetPath;
97
- }
98
- }
99
-
100
- export async function syncAllSwcFiles(srcDir = path.resolve("src"), projectRoot = process.cwd()) {
101
- const files = await glob(["**/*.{js,jsx,ts,tsx}"], { cwd: srcDir, absolute: true });
102
- const tasks = [];
103
-
104
- for (const f of files) {
105
- let raw = "";
106
- try {
107
- raw = fsSync.readFileSync(f, "utf8");
108
- } catch (e) {
109
- continue;
110
- }
111
- const targetPath = getMirrorPath(f, projectRoot);
112
- // Skip server functions
113
- if (isServerFunctionsModule(raw, f)) {
114
- try {
115
- if (fsSync.existsSync(targetPath)) fsSync.unlinkSync(targetPath);
116
- } catch (e) {}
117
- continue;
118
- }
119
-
120
- let needCompile = true;
121
- try {
122
- if (fsSync.existsSync(targetPath)) {
123
- const srcStat = fsSync.statSync(f);
124
- const tgtStat = fsSync.statSync(targetPath);
125
- if (tgtStat.mtimeMs >= srcStat.mtimeMs) {
126
- needCompile = false;
127
- }
128
- }
129
- } catch (e) {}
130
-
131
- if (needCompile) {
132
- tasks.push(transformToDisk(f, projectRoot));
133
- }
134
- }
135
-
136
- if (tasks.length > 0) {
137
- await Promise.all(tasks);
138
- }
139
- }
140
-
141
- export function swcRedirectPlugin({ projectRoot = process.cwd() } = {}) {
142
- const swcDir = path.resolve(projectRoot, ".dinou/swc");
143
- const resolveCache = new Map();
144
-
145
- let redirTime = 0;
146
- let redirCalls = 0;
147
- let redirHits = 0;
148
-
149
- const candidateExtensions = [
150
- "",
151
- ".js",
152
- ".jsx",
153
- ".ts",
154
- ".tsx",
155
- "/index.js",
156
- "/index.jsx",
157
- "/index.ts",
158
- "/index.tsx",
159
- ];
160
-
161
- return {
162
- name: "swc-disk-cache-redirect",
163
- setup(build) {
164
- build.onStart(() => {
165
- resolveCache.clear();
166
- redirTime = 0;
167
- redirCalls = 0;
168
- redirHits = 0;
169
- });
170
-
171
- // Redirect CSS, assets, and server functions / source files imported from inside .dinou/swc back to src/
172
- build.onResolve(
173
- { filter: /.*/ },
174
- (args) => {
175
- const importer = args.importer || "";
176
- if (!importer.includes(".dinou/swc") && !importer.includes(".dinou\\swc")) {
177
- return null;
178
- }
179
-
180
- // Only process relative imports or assets
181
- const isRelative = args.path.startsWith(".");
182
- const isAsset = redirectFilter.test(args.path);
183
- if (!isRelative && !isAsset) {
184
- return null;
185
- }
186
-
187
- const tStart = Date.now();
188
- redirCalls++;
189
-
190
- const cacheKey = importer + "::" + args.path;
191
- if (resolveCache.has(cacheKey)) {
192
- redirHits++;
193
- redirTime += Date.now() - tStart;
194
- return resolveCache.get(cacheKey);
195
- }
196
-
197
- // 1. Check if it exists directly in .dinou/swc
198
- const candidateBase = path.resolve(path.dirname(importer), args.path);
199
- if (fsSync.existsSync(candidateBase) && !fsSync.statSync(candidateBase).isDirectory()) {
200
- resolveCache.set(cacheKey, null);
201
- redirTime += Date.now() - tStart;
202
- return null;
203
- }
204
-
205
- for (const ext of [".js", ".jsx", ".ts", ".tsx", "/index.js", "/index.jsx", "/index.ts", "/index.tsx"]) {
206
- const candidate = candidateBase + ext;
207
- if (fsSync.existsSync(candidate) && !fsSync.statSync(candidate).isDirectory()) {
208
- resolveCache.set(cacheKey, null);
209
- redirTime += Date.now() - tStart;
210
- return null;
211
- }
212
- }
213
-
214
- // 2. Otherwise redirect to original in src/
215
- const relFromSwc = path.relative(swcDir, path.dirname(importer));
216
- const originalDir = path.join(projectRoot, relFromSwc);
217
- const targetBase = path.resolve(originalDir, args.path);
218
-
219
- for (const ext of candidateExtensions) {
220
- const target = targetBase + ext;
221
- if (fsSync.existsSync(target) && !fsSync.statSync(target).isDirectory()) {
222
- const res = { path: target };
223
- resolveCache.set(cacheKey, res);
224
- redirTime += Date.now() - tStart;
225
- return res;
226
- }
227
- }
228
-
229
- resolveCache.set(cacheKey, null);
230
- redirTime += Date.now() - tStart;
231
- return null;
232
- }
233
- );
234
-
235
- build.onEnd(() => {
236
- globalThis.__DINOU_REDIRECT_TIME__ = redirTime;
237
- globalThis.__DINOU_REDIRECT_CALLS__ = redirCalls;
238
- globalThis.__DINOU_REDIRECT_HITS__ = redirHits;
239
- });
240
- },
241
- };
242
- }
1
+ import fs from "node:fs/promises";
2
+ import fsSync from "node:fs";
3
+ import path from "node:path";
4
+ import glob from "fast-glob";
5
+ import { transform } from "@swc/core";
6
+ import { useClientRegex, useServerRegex } from "../../constants.js";
7
+ import { hasServerFunctions, extractServerFunctions } from "../../core/transform-server-functions.js";
8
+ import { extensions as assetExtensions } from "../../core/asset-extensions.js";
9
+
10
+ function isServerFunctionsModule(raw, filename) {
11
+ const trimmed = raw.trim();
12
+ if (useClientRegex.test(trimmed)) return false;
13
+ if (useServerRegex.test(trimmed)) return true;
14
+ if (hasServerFunctions(raw)) {
15
+ const actions = extractServerFunctions(raw, filename);
16
+ return Boolean(actions && actions.length > 0);
17
+ }
18
+ return false;
19
+ }
20
+
21
+ const styleExtensions = ["css", "scss", "less", "sass"];
22
+ const fontExtensions = ["woff", "woff2", "ttf", "eot", "otf"];
23
+ const redirectExtensions = [...styleExtensions, ...assetExtensions, ...fontExtensions];
24
+ const redirectFilter = new RegExp(`\\.(${redirectExtensions.join("|")})$`, "i");
25
+
26
+ const normPath = (p) => {
27
+ if (!p) return "";
28
+ let s = path.resolve(p).replace(/\\/g, "/");
29
+ if (process.platform === "win32") {
30
+ s = s.replace(/^([a-zA-Z]):/, (_, d) => d.toLowerCase() + ":");
31
+ }
32
+ return s;
33
+ };
34
+
35
+ export function getMirrorPath(srcPath, projectRoot = process.cwd()) {
36
+ if (!srcPath) return "";
37
+ const norm = normPath(srcPath);
38
+ const rootNorm = normPath(projectRoot);
39
+ if (!norm.startsWith(rootNorm)) return srcPath;
40
+ const rel = norm.slice(rootNorm.length + 1);
41
+ const targetRel = rel.replace(/\.[jt]sx?$/, ".js");
42
+ return path.resolve(projectRoot, ".dinou/swc", targetRel);
43
+ }
44
+
45
+ export function getOriginalPath(mirrorPath, projectRoot = process.cwd()) {
46
+ if (!mirrorPath) return "";
47
+ const norm = normPath(mirrorPath);
48
+ const rootNorm = normPath(projectRoot);
49
+ const swcPrefix = `${rootNorm}/.dinou/swc/`;
50
+ if (!norm.startsWith(swcPrefix)) return mirrorPath;
51
+ const rel = norm.slice(swcPrefix.length);
52
+ const base = path.resolve(projectRoot, rel.replace(/\.js$/, ""));
53
+ for (const ext of [".tsx", ".ts", ".jsx", ".js"]) {
54
+ const candidate = base + ext;
55
+ if (fsSync.existsSync(candidate)) return candidate;
56
+ }
57
+ return base + ".tsx";
58
+ }
59
+
60
+ export async function transformToDisk(srcPath, projectRoot = process.cwd()) {
61
+ const normSrc = path.resolve(srcPath);
62
+ if (!fsSync.existsSync(normSrc)) return null;
63
+
64
+ const raw = await fs.readFile(normSrc, "utf8");
65
+ const targetPath = getMirrorPath(normSrc, projectRoot);
66
+ // Skip server functions - handled by serverFunctionsPlugin proxy
67
+ if (isServerFunctionsModule(raw, normSrc)) {
68
+ try {
69
+ if (fsSync.existsSync(targetPath)) await fs.unlink(targetPath);
70
+ } catch (e) {}
71
+ return null;
72
+ }
73
+
74
+ try {
75
+ const { code } = await transform(raw, {
76
+ filename: normSrc,
77
+ sourceMaps: "inline",
78
+ jsc: {
79
+ parser: { syntax: "typescript", tsx: true, dynamicImport: true },
80
+ target: "es2022",
81
+ transform: {
82
+ react: {
83
+ refresh: true,
84
+ development: true,
85
+ runtime: "automatic",
86
+ },
87
+ },
88
+ },
89
+ });
90
+ await fs.mkdir(path.dirname(targetPath), { recursive: true });
91
+ await fs.writeFile(targetPath, code, "utf8");
92
+ return targetPath;
93
+ } catch (err) {
94
+ await fs.mkdir(path.dirname(targetPath), { recursive: true });
95
+ await fs.writeFile(targetPath, raw, "utf8");
96
+ return targetPath;
97
+ }
98
+ }
99
+
100
+ export async function syncAllSwcFiles(srcDir = path.resolve("src"), projectRoot = process.cwd()) {
101
+ const files = await glob(["**/*.{js,jsx,ts,tsx}"], { cwd: srcDir, absolute: true });
102
+ const tasks = [];
103
+
104
+ for (const f of files) {
105
+ let raw = "";
106
+ try {
107
+ raw = fsSync.readFileSync(f, "utf8");
108
+ } catch (e) {
109
+ continue;
110
+ }
111
+ const targetPath = getMirrorPath(f, projectRoot);
112
+ // Skip server functions
113
+ if (isServerFunctionsModule(raw, f)) {
114
+ try {
115
+ if (fsSync.existsSync(targetPath)) fsSync.unlinkSync(targetPath);
116
+ } catch (e) {}
117
+ continue;
118
+ }
119
+
120
+ let needCompile = true;
121
+ try {
122
+ if (fsSync.existsSync(targetPath)) {
123
+ const srcStat = fsSync.statSync(f);
124
+ const tgtStat = fsSync.statSync(targetPath);
125
+ if (tgtStat.mtimeMs >= srcStat.mtimeMs) {
126
+ needCompile = false;
127
+ }
128
+ }
129
+ } catch (e) {}
130
+
131
+ if (needCompile) {
132
+ tasks.push(transformToDisk(f, projectRoot));
133
+ }
134
+ }
135
+
136
+ if (tasks.length > 0) {
137
+ await Promise.all(tasks);
138
+ }
139
+ }
140
+
141
+ export function swcRedirectPlugin({ projectRoot = process.cwd() } = {}) {
142
+ const swcDir = path.resolve(projectRoot, ".dinou/swc");
143
+ const resolveCache = new Map();
144
+
145
+ let redirTime = 0;
146
+ let redirCalls = 0;
147
+ let redirHits = 0;
148
+
149
+ const candidateExtensions = [
150
+ "",
151
+ ".js",
152
+ ".jsx",
153
+ ".ts",
154
+ ".tsx",
155
+ "/index.js",
156
+ "/index.jsx",
157
+ "/index.ts",
158
+ "/index.tsx",
159
+ ];
160
+
161
+ return {
162
+ name: "swc-disk-cache-redirect",
163
+ setup(build) {
164
+ build.onStart(() => {
165
+ resolveCache.clear();
166
+ redirTime = 0;
167
+ redirCalls = 0;
168
+ redirHits = 0;
169
+ });
170
+
171
+ // Redirect CSS, assets, and server functions / source files imported from inside .dinou/swc back to src/
172
+ build.onResolve(
173
+ { filter: /.*/ },
174
+ (args) => {
175
+ const importer = args.importer || "";
176
+ if (!importer.includes(".dinou/swc") && !importer.includes(".dinou\\swc")) {
177
+ return null;
178
+ }
179
+
180
+ // Only process relative imports or assets
181
+ const isRelative = args.path.startsWith(".");
182
+ const isAsset = redirectFilter.test(args.path);
183
+ if (!isRelative && !isAsset) {
184
+ return null;
185
+ }
186
+
187
+ const tStart = Date.now();
188
+ redirCalls++;
189
+
190
+ const cacheKey = importer + "::" + args.path;
191
+ if (resolveCache.has(cacheKey)) {
192
+ redirHits++;
193
+ redirTime += Date.now() - tStart;
194
+ return resolveCache.get(cacheKey);
195
+ }
196
+
197
+ // 1. Check if it exists directly in .dinou/swc
198
+ const candidateBase = path.resolve(path.dirname(importer), args.path);
199
+ if (fsSync.existsSync(candidateBase) && !fsSync.statSync(candidateBase).isDirectory()) {
200
+ resolveCache.set(cacheKey, null);
201
+ redirTime += Date.now() - tStart;
202
+ return null;
203
+ }
204
+
205
+ for (const ext of [".js", ".jsx", ".ts", ".tsx", "/index.js", "/index.jsx", "/index.ts", "/index.tsx"]) {
206
+ const candidate = candidateBase + ext;
207
+ if (fsSync.existsSync(candidate) && !fsSync.statSync(candidate).isDirectory()) {
208
+ resolveCache.set(cacheKey, null);
209
+ redirTime += Date.now() - tStart;
210
+ return null;
211
+ }
212
+ }
213
+
214
+ // 2. Otherwise redirect to original in src/
215
+ const relFromSwc = path.relative(swcDir, path.dirname(importer));
216
+ const originalDir = path.join(projectRoot, relFromSwc);
217
+ const targetBase = path.resolve(originalDir, args.path);
218
+
219
+ for (const ext of candidateExtensions) {
220
+ const target = targetBase + ext;
221
+ if (fsSync.existsSync(target) && !fsSync.statSync(target).isDirectory()) {
222
+ const res = { path: target };
223
+ resolveCache.set(cacheKey, res);
224
+ redirTime += Date.now() - tStart;
225
+ return res;
226
+ }
227
+ }
228
+
229
+ resolveCache.set(cacheKey, null);
230
+ redirTime += Date.now() - tStart;
231
+ return null;
232
+ }
233
+ );
234
+
235
+ build.onEnd(() => {
236
+ globalThis.__DINOU_REDIRECT_TIME__ = redirTime;
237
+ globalThis.__DINOU_REDIRECT_CALLS__ = redirCalls;
238
+ globalThis.__DINOU_REDIRECT_HITS__ = redirHits;
239
+ });
240
+ },
241
+ };
242
+ }