dinou 9.0.1 → 9.0.3

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.
Files changed (37) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/README.md +503 -9883
  3. package/dinou/bun/build.js +38 -1
  4. package/{cli.js → dinou/cli.js} +14 -7
  5. package/dinou/cloudflare/build.js +37 -0
  6. package/dinou/core/build-static-pages.js +15 -7
  7. package/dinou/core/cache-revalidate.js +909 -151
  8. package/dinou/core/cache-slot.js +728 -97
  9. package/dinou/core/client-webpack.jsx +60 -9
  10. package/dinou/core/client.jsx +60 -9
  11. package/dinou/core/edge-ssr.js +33 -5
  12. package/dinou/core/generate-static.js +109 -47
  13. package/dinou/core/get-jsx.js +333 -329
  14. package/dinou/core/handler.js +2983 -2343
  15. package/dinou/core/layout-functions.js +102 -1
  16. package/dinou/core/ppr-context.js +256 -105
  17. package/dinou/core/ppr-runtime.js +540 -267
  18. package/dinou/core/render-jsx-to-client-jsx.js +131 -71
  19. package/dinou/core/slot.js +246 -33
  20. package/dinou/core/storage-adapter.js +117 -11
  21. package/dinou/deno/build.js +38 -1
  22. package/dinou/deno/seed-kv.js +43 -11
  23. package/dinou/esbuild/helpers-esbuild/swc-disk-cache.js +242 -225
  24. package/dinou/esbuild/plugins-esbuild/server-functions-plugin.js +253 -226
  25. package/dinou/esbuild/react-refresh/esm-hmr/client.js +209 -209
  26. package/dinou/node/build.js +2 -2
  27. package/dinou/node/bundle-dual-engine.js +2 -0
  28. package/dinou/node/dev.js +2853 -2704
  29. package/dinou/package.json +5 -2
  30. package/dinou/rollup/react-refresh/esm-hmr/client.js +220 -220
  31. package/dinou/rollup/rollup-plugins/rollup-plugin-server-functions.js +182 -160
  32. package/dinou/server.d.ts +25 -11
  33. package/dinou/server.js +5 -0
  34. package/dinou/webpack/loaders/server-functions-loader.js +101 -94
  35. package/dinou/webpack/webpack.config.js +9 -4
  36. package/eject.js +219 -223
  37. package/package.json +2 -2
@@ -1,160 +1,182 @@
1
- // rollup-plugin-server-functions.js
2
- import path from "node:path";
3
- import fs from "node:fs/promises";
4
- import manifestGeneratorPlugin from "./manifest-generator-plugin.js";
5
- import parseExports from "../../core/parse-exports.js";
6
- import { useServerRegex } from "../../constants.js";
7
- import { extractServerFunctions, hasServerFunctions } from "../../core/transform-server-functions.js";
8
-
9
- function serverFunctionsPlugin(manifestData = {}, options = {}) {
10
- const opts =
11
- typeof manifestData === "object" &&
12
- manifestData !== null &&
13
- ("onManifestUpdated" in manifestData || "serverFiles" in manifestData)
14
- ? manifestData
15
- : options;
16
- const serverFiles =
17
- opts.serverFiles || manifestData.serverFiles || options.serverFiles || null;
18
- const root = process.cwd();
19
- const serverFunctions = new Map(); // Collect here: Map<relativePath, Set<exports>>
20
-
21
- function normalizeFsPath(p) {
22
- const resolved = path.resolve(p).replace(/\\/g, "/");
23
- return process.platform === "win32" ? resolved.toLowerCase() : resolved;
24
- }
25
-
26
- return {
27
- name: "server-functions-proxy",
28
- transform(code, id) {
29
- if (id.includes("node_modules") || id.includes("\0")) return null;
30
- if (serverFiles && !serverFiles.has(normalizeFsPath(id))) {
31
- if (!useServerRegex.test(code.trim()) && !hasServerFunctions(code)) return null;
32
- serverFiles.add(normalizeFsPath(id));
33
- } else if (!useServerRegex.test(code.trim()) && !hasServerFunctions(code)) {
34
- return null;
35
- }
36
-
37
- const isModuleLevel = useServerRegex.test(code.trim());
38
- const exports = isModuleLevel ? parseExports(code) : extractServerFunctions(code, id);
39
- if (!exports || exports.length === 0) return null;
40
-
41
- const relativePath = path.relative(root, id).replace(/\\/g, "/");
42
- serverFunctions.set(relativePath, new Set(exports)); // Save exports as a Set for uniqueness
43
-
44
- const fileUrl = `file:///${relativePath}`;
45
-
46
- // Generate a module that exports proxies instead of the real code
47
- let proxyCode = `
48
- import { createServerFunctionProxy } from "/__SERVER_FUNCTION_PROXY__";
49
- `;
50
-
51
- for (const exp of exports) {
52
- const key =
53
- exp === "default" ? `${fileUrl}#default` : `${fileUrl}#${exp}`;
54
- if (exp === "default") {
55
- proxyCode += `export default createServerFunctionProxy(${JSON.stringify(
56
- key
57
- )});\n`;
58
- } else {
59
- proxyCode += `export const ${exp} = createServerFunctionProxy(${JSON.stringify(
60
- key
61
- )});\n`;
62
- }
63
- }
64
-
65
- return {
66
- code: proxyCode,
67
- map: null,
68
- };
69
- },
70
- // 🪄 After manifest exists, replace the placeholder with the final URL
71
- async generateBundle(options, bundle) {
72
- if (serverFiles && serverFiles.size > 0) {
73
- for (const filePath of serverFiles) {
74
- const rel = path.relative(root, filePath).replace(/\\/g, "/");
75
- if (!serverFunctions.has(rel)) {
76
- try {
77
- const fileCode = await fs.readFile(filePath, "utf8");
78
- if (useServerRegex.test(fileCode.trim())) {
79
- const exports = parseExports(fileCode);
80
- if (exports && exports.length > 0) {
81
- serverFunctions.set(rel, new Set(exports));
82
- }
83
- } else if (hasServerFunctions(fileCode)) {
84
- const actions = extractServerFunctions(fileCode, filePath);
85
- if (actions && actions.length > 0) {
86
- serverFunctions.set(rel, new Set(actions));
87
- }
88
- }
89
- } catch (e) {}
90
- }
91
- }
92
- }
93
-
94
- if (serverFunctions.size === 0) {
95
- if (typeof globalThis !== "undefined") {
96
- globalThis.__DINOU_RAW_SERVER_FUNCTIONS_MANIFEST__ = {};
97
- }
98
- return;
99
- }
100
- const isProd =
101
- process.env.NODE_ENV === "production" &&
102
- process.env.DINOU_DEV !== "true";
103
- const manifest = manifestGeneratorPlugin.manifestData;
104
- const proxyName = manifest["serverFunctionProxy.js"];
105
- if (isProd && !proxyName) {
106
- throw new Error(
107
- "[Dinou] rollup-plugin-server-functions: could not find hashed 'serverFunctionProxy.js' in manifest during production build."
108
- );
109
- }
110
- const hashedPath =
111
- "/" + (proxyName || "serverFunctionProxy.js");
112
-
113
- for (const file of Object.keys(bundle)) {
114
- const chunk = bundle[file];
115
- if (chunk.type === "asset" || !chunk.code) continue;
116
- if (chunk.code.includes("/__SERVER_FUNCTION_PROXY__")) {
117
- chunk.code = chunk.code.replace(
118
- /\/__SERVER_FUNCTION_PROXY__/g,
119
- hashedPath
120
- );
121
- }
122
- }
123
-
124
- // Generate manifest: convert Map to a simple object
125
- const manifestObj = {};
126
- for (const [relPath, exportsSet] of serverFunctions.entries()) {
127
- manifestObj[relPath] = Array.from(exportsSet);
128
- }
129
-
130
- if (typeof globalThis !== "undefined") {
131
- globalThis.__DINOU_RAW_SERVER_FUNCTIONS_MANIFEST__ = manifestObj;
132
- }
133
-
134
- const isDev =
135
- process.env.NODE_ENV !== "production" ||
136
- process.env.DINOU_DEV === "true";
137
- const shouldWriteToDisk =
138
- process.env.DINOU_WRITE_TO_DISK === "false"
139
- ? false
140
- : process.env.DINOU_WRITE_TO_DISK === "true" || !isDev;
141
-
142
- if (shouldWriteToDisk) {
143
- // Write the manifest to the specified folder (e.g. same place as other assets)
144
- const manifestPath = path.join(
145
- ".dinou/server_functions_manifest",
146
- "server-functions-manifest.json"
147
- );
148
- try {
149
- await fs.mkdir(path.dirname(manifestPath), { recursive: true });
150
- await fs.writeFile(manifestPath, JSON.stringify(manifestObj, null, 2));
151
- } catch (err) {
152
- console.error(err);
153
- }
154
- }
155
- },
156
- };
157
- }
158
-
159
- export { serverFunctionsPlugin };
160
- export default serverFunctionsPlugin;
1
+ // rollup-plugin-server-functions.js
2
+ import path from "node:path";
3
+ import fs from "node:fs/promises";
4
+ import manifestGeneratorPlugin from "./manifest-generator-plugin.js";
5
+ import parseExports from "../../core/parse-exports.js";
6
+ import { useServerRegex } from "../../constants.js";
7
+ import { extractServerFunctions, hasServerFunctions } from "../../core/transform-server-functions.js";
8
+
9
+ function serverFunctionsPlugin(manifestData = {}, options = {}) {
10
+ const opts =
11
+ typeof manifestData === "object" &&
12
+ manifestData !== null &&
13
+ ("onManifestUpdated" in manifestData || "serverFiles" in manifestData)
14
+ ? manifestData
15
+ : options;
16
+ const serverFiles =
17
+ opts.serverFiles || manifestData.serverFiles || options.serverFiles || null;
18
+ const root = process.cwd();
19
+ const serverFunctions = new Map(); // Collect here: Map<relativePath, Set<exports>>
20
+ const sfMtimeCache = new Map(); // Cache by filePath: { mtimeMs, exportsSet }
21
+
22
+ function normalizeFsPath(p) {
23
+ const resolved = path.resolve(p).replace(/\\/g, "/");
24
+ return process.platform === "win32" ? resolved.toLowerCase() : resolved;
25
+ }
26
+
27
+ return {
28
+ name: "server-functions-proxy",
29
+ transform(code, id) {
30
+ if (id.includes("node_modules") || id.includes("\0")) return null;
31
+ if (serverFiles && !serverFiles.has(normalizeFsPath(id))) {
32
+ if (!useServerRegex.test(code.trim()) && !hasServerFunctions(code)) return null;
33
+ serverFiles.add(normalizeFsPath(id));
34
+ } else if (!useServerRegex.test(code.trim()) && !hasServerFunctions(code)) {
35
+ return null;
36
+ }
37
+
38
+ const isModuleLevel = useServerRegex.test(code.trim());
39
+ const exports = isModuleLevel ? parseExports(code) : extractServerFunctions(code, id);
40
+ if (!exports || exports.length === 0) return null;
41
+
42
+ const relativePath = path.relative(root, id).replace(/\\/g, "/");
43
+ serverFunctions.set(relativePath, new Set(exports)); // Save exports as a Set for uniqueness
44
+
45
+ const fileUrl = `file:///${relativePath}`;
46
+
47
+ // Generate a module that exports proxies instead of the real code
48
+ let proxyCode = `
49
+ import { createServerFunctionProxy } from "/__SERVER_FUNCTION_PROXY__";
50
+ `;
51
+
52
+ for (const exp of exports) {
53
+ const key =
54
+ exp === "default" ? `${fileUrl}#default` : `${fileUrl}#${exp}`;
55
+ if (exp === "default") {
56
+ proxyCode += `export default createServerFunctionProxy(${JSON.stringify(
57
+ key
58
+ )});\n`;
59
+ } else {
60
+ proxyCode += `export const ${exp} = createServerFunctionProxy(${JSON.stringify(
61
+ key
62
+ )});\n`;
63
+ }
64
+ }
65
+
66
+ return {
67
+ code: proxyCode,
68
+ map: null,
69
+ };
70
+ },
71
+ // 🪄 After manifest exists, replace the placeholder with the final URL
72
+ async generateBundle(options, bundle) {
73
+ if (serverFiles && serverFiles.size > 0) {
74
+ for (const filePath of serverFiles) {
75
+ const rel = path.relative(root, filePath).replace(/\\/g, "/");
76
+ try {
77
+ const stat = await fs.stat(filePath);
78
+ const cached = sfMtimeCache.get(filePath);
79
+ if (cached && cached.mtimeMs === stat.mtimeMs) {
80
+ if (cached.exportsSet) {
81
+ serverFunctions.set(rel, cached.exportsSet);
82
+ } else {
83
+ serverFunctions.delete(rel);
84
+ }
85
+ continue;
86
+ }
87
+
88
+ const fileCode = await fs.readFile(filePath, "utf8");
89
+ let exports = null;
90
+ if (useServerRegex.test(fileCode.trim())) {
91
+ exports = parseExports(fileCode);
92
+ } else if (hasServerFunctions(fileCode)) {
93
+ exports = extractServerFunctions(fileCode, filePath);
94
+ }
95
+
96
+ if (exports && exports.length > 0) {
97
+ const exportsSet = new Set(exports);
98
+ serverFunctions.set(rel, exportsSet);
99
+ sfMtimeCache.set(filePath, {
100
+ mtimeMs: stat.mtimeMs,
101
+ exportsSet,
102
+ });
103
+ } else {
104
+ serverFunctions.delete(rel);
105
+ sfMtimeCache.set(filePath, {
106
+ mtimeMs: stat.mtimeMs,
107
+ exportsSet: null,
108
+ });
109
+ }
110
+ } catch (e) {
111
+ serverFunctions.delete(rel);
112
+ }
113
+ }
114
+ }
115
+
116
+ if (serverFunctions.size === 0) {
117
+ if (typeof globalThis !== "undefined") {
118
+ globalThis.__DINOU_RAW_SERVER_FUNCTIONS_MANIFEST__ = {};
119
+ }
120
+ return;
121
+ }
122
+ const isProd =
123
+ process.env.NODE_ENV === "production" &&
124
+ process.env.DINOU_DEV !== "true";
125
+ const manifest = manifestGeneratorPlugin.manifestData;
126
+ const proxyName = manifest["serverFunctionProxy.js"];
127
+ if (isProd && !proxyName) {
128
+ throw new Error(
129
+ "[Dinou] rollup-plugin-server-functions: could not find hashed 'serverFunctionProxy.js' in manifest during production build."
130
+ );
131
+ }
132
+ const hashedPath =
133
+ "/" + (proxyName || "serverFunctionProxy.js");
134
+
135
+ for (const file of Object.keys(bundle)) {
136
+ const chunk = bundle[file];
137
+ if (chunk.type === "asset" || !chunk.code) continue;
138
+ if (chunk.code.includes("/__SERVER_FUNCTION_PROXY__")) {
139
+ chunk.code = chunk.code.replace(
140
+ /\/__SERVER_FUNCTION_PROXY__/g,
141
+ hashedPath
142
+ );
143
+ }
144
+ }
145
+
146
+ // Generate manifest: convert Map to a simple object
147
+ const manifestObj = {};
148
+ for (const [relPath, exportsSet] of serverFunctions.entries()) {
149
+ manifestObj[relPath] = Array.from(exportsSet);
150
+ }
151
+
152
+ if (typeof globalThis !== "undefined") {
153
+ globalThis.__DINOU_RAW_SERVER_FUNCTIONS_MANIFEST__ = manifestObj;
154
+ }
155
+
156
+ const isDev =
157
+ process.env.NODE_ENV !== "production" ||
158
+ process.env.DINOU_DEV === "true";
159
+ const shouldWriteToDisk =
160
+ process.env.DINOU_WRITE_TO_DISK === "false"
161
+ ? false
162
+ : process.env.DINOU_WRITE_TO_DISK === "true" || !isDev;
163
+
164
+ if (shouldWriteToDisk) {
165
+ // Write the manifest to the specified folder (e.g. same place as other assets)
166
+ const manifestPath = path.join(
167
+ ".dinou/server_functions_manifest",
168
+ "server-functions-manifest.json"
169
+ );
170
+ try {
171
+ await fs.mkdir(path.dirname(manifestPath), { recursive: true });
172
+ await fs.writeFile(manifestPath, JSON.stringify(manifestObj, null, 2));
173
+ } catch (err) {
174
+ console.error(err);
175
+ }
176
+ }
177
+ },
178
+ };
179
+ }
180
+
181
+ export { serverFunctionsPlugin };
182
+ export default serverFunctionsPlugin;
package/dinou/server.d.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  /**
2
- * Revalidates the cache for a specific route path.
3
- * * It deletes the old cached HTML and RSC files and rebuilds them synchronously.
4
- * * When `{ cascade: true }` is specified, if a layout is present and revalidated in that path,
5
- * it cascades `revalidatePage` to all child routes to ensure 0 hydration mismatch.
2
+ * Revalidates the cache for a specific route path (both its layout segment, if present, and its page).
3
+ * Child routes sharing the layout have their `index.html` lazily re-composed on demand (preserving their cached `page.rsc` payloads) to ensure 0 hydration mismatch.
6
4
  * @param path The path of the route to revalidate (e.g. "/blog" or "/dashboard").
7
- * @param options Optional configuration: `{ cascade?: boolean }` or legacy `"layout"`.
5
+ */
6
+ export declare function revalidatePath(path: string): Promise<void>;
7
+ /**
8
+ * @deprecated The `options` parameter (`{ cascade }` / `"layout"`) is deprecated and ignored since 9.0.3.
9
+ * Zero hydration mismatch is now guaranteed automatically via lazy `index.html` re-composition,
10
+ * and layout-only revalidation should use `revalidateLayout(path)`.
8
11
  */
9
12
  export declare function revalidatePath(
10
13
  path: string,
@@ -13,23 +16,34 @@ export declare function revalidatePath(
13
16
 
14
17
  /**
15
18
  * Revalidates only the page component (`page.rsc`) and its HTML (`index.html`) for a specific route path,
16
- * without touching any layout.
19
+ * composing with the cached layout without re-executing `layout.tsx`.
17
20
  * @param path The path of the page to revalidate (e.g. "/dashboard/analytics").
18
21
  */
19
22
  export declare function revalidatePage(path: string): Promise<void>;
20
23
 
21
24
  /**
22
- * Revalidates all routes that are associated with the specified cache tag.
23
- * * By default, layout tags only revalidate the layout shell (`layout.rsc`) for maximum efficiency.
24
- * * When `{ cascade: true }` is specified, layout tags invoke `revalidatePath(path, { cascade: true })`
25
- * to cascade `revalidatePage` to all child pages, ensuring 0 hydration mismatch.
25
+ * Revalidates only the layout shell (`layout.rsc` and `layout.metadata.json`) for a specific route path,
26
+ * without re-rendering child pages. Affected `index.html` files are lazily re-composed on demand.
27
+ * @param path The path of the layout segment to revalidate (e.g. "/dashboard").
28
+ */
29
+ export declare function revalidateLayout(path: string): Promise<boolean>;
30
+
31
+ /**
32
+ * Revalidates all routes, layouts, or cache slots that are associated with the specified cache tag.
33
+ * Layout tags surgically revalidate only the layout shell (`layout.rsc`), while affected `index.html` files
34
+ * are lazily re-composed on demand to ensure 0 hydration mismatch.
26
35
  * @param tag The tag string to revalidate (e.g. "blog-posts").
27
- * @param options Optional configuration: `{ cascade?: boolean }` or legacy `"layout"`.
36
+ */
37
+ export declare function revalidateTag(tag: string): Promise<void>;
38
+ /**
39
+ * @deprecated The `options` parameter (`{ cascade }` / `"layout"`) is deprecated and ignored since 9.0.3.
40
+ * Zero hydration mismatch is now guaranteed automatically via lazy `index.html` re-composition.
28
41
  */
29
42
  export declare function revalidateTag(
30
43
  tag: string,
31
44
  options?: { cascade?: boolean } | "layout"
32
45
  ): Promise<void>;
46
+
33
47
  export type {
34
48
  PPRConfig,
35
49
  RouteSegmentConfig,
package/dinou/server.js CHANGED
@@ -8,6 +8,11 @@ export async function revalidatePage(path) {
8
8
  return fn(path);
9
9
  }
10
10
 
11
+ export async function revalidateLayout(path) {
12
+ const { revalidateLayout: fn } = await import("./core/cache-revalidate.js");
13
+ return fn(path);
14
+ }
15
+
11
16
  export async function revalidateTag(tag, options) {
12
17
  const { revalidateTag: fn } = await import("./core/cache-revalidate.js");
13
18
  return fn(tag, options);
@@ -1,94 +1,101 @@
1
- import path from "node:path";
2
- import { normalizePathCase } from "../../core/path-utils.js";
3
- import parseExports from "../../core/parse-exports.js";
4
- import { useServerRegex } from "../../constants.js";
5
-
6
- export default function (source) {
7
- const options = this.getOptions ? this.getOptions() : {};
8
- const norm = path.resolve(this.resourcePath).replace(/\\/g, "/").toLowerCase();
9
-
10
- if (options.serverFiles) {
11
- if (!options.serverFiles.has(norm)) {
12
- if (!source.includes("use server")) {
13
- return source;
14
- }
15
- if (useServerRegex.test(source)) {
16
- options.serverFiles.add(norm);
17
- } else {
18
- return source;
19
- }
20
- }
21
- }
22
-
23
- let hasUseServer = false;
24
-
25
- if (useServerRegex.test(source)) {
26
- hasUseServer = true;
27
- }
28
-
29
- if (!hasUseServer) {
30
- if (options.serverFiles) {
31
- options.serverFiles.delete(norm);
32
- }
33
- return source;
34
- }
35
-
36
- const exports = parseExports(source);
37
- if (exports.length === 0) return source;
38
-
39
- // Build IDs
40
- const moduleId = this.resourcePath;
41
- const relativePath = path.relative(
42
- normalizePathCase(process.cwd()),
43
- normalizePathCase(moduleId)
44
- );
45
- const normalizedPath = relativePath.replace(/\\/g, "/");
46
-
47
- const fileUrl = `file:///${normalizedPath}`;
48
-
49
- //
50
- // IMPORTANT: dynamic import instead of static import
51
- //
52
- // Webpack will NOT try to resolve "__SERVER_FUNCTION_PROXY__"
53
- // as a module → it will remain a string → replaced later → browser loads it.
54
-
55
- let proxyCode = `
56
- const loadProxy = new Function('return import("/"+"__SERVER_FUNCTION_PROXY__")');
57
- `;
58
-
59
- for (const exp of exports) {
60
- const key = exp === "default" ? `${fileUrl}#default` : `${fileUrl}#${exp}`;
61
-
62
- if (exp === "default") {
63
- proxyCode += `
64
- export default (...args) =>
65
- loadProxy().then(mod =>
66
- (mod.default ?? mod ?? window.__SERVER_FUNCTION_PROXY_LIB__).createServerFunctionProxy(${JSON.stringify(
67
- key,
68
- )})(...args)
69
- );
70
- `;
71
- } else {
72
- proxyCode += `
73
- export const ${exp} = (...args) =>
74
- loadProxy().then(mod => (mod.default ?? mod ?? window.__SERVER_FUNCTION_PROXY_LIB__).createServerFunctionProxy(${JSON.stringify(
75
- key,
76
- )})(...args)
77
- );
78
- `;
79
- }
80
- }
81
-
82
- // Emit manifest entry
83
- const manifestEntry = {
84
- path: normalizedPath,
85
- exports: exports,
86
- };
87
-
88
- this.emitFile(
89
- `server-functions/${normalizedPath}.json`,
90
- JSON.stringify(manifestEntry, null, 2),
91
- );
92
-
93
- return proxyCode;
94
- };
1
+ import path from "node:path";
2
+ import { normalizePathCase } from "../../core/path-utils.js";
3
+ import parseExports from "../../core/parse-exports.js";
4
+ import { useClientRegex, useServerRegex } from "../../constants.js";
5
+ import { extractServerFunctions, hasServerFunctions } from "../../core/transform-server-functions.js";
6
+
7
+ export default function (source) {
8
+ const options = this.getOptions ? this.getOptions() : {};
9
+ const trimmed = source.trim();
10
+
11
+ if (useClientRegex.test(trimmed)) {
12
+ return source;
13
+ }
14
+
15
+ const norm = path.resolve(this.resourcePath).replace(/\\/g, "/").toLowerCase();
16
+
17
+ const isModuleLevel = useServerRegex.test(trimmed);
18
+ const hasBodyUseServer = !isModuleLevel && hasServerFunctions(source);
19
+
20
+ if (options.serverFiles) {
21
+ if (!options.serverFiles.has(norm)) {
22
+ if (!isModuleLevel && !hasBodyUseServer) {
23
+ return source;
24
+ }
25
+ options.serverFiles.add(norm);
26
+ }
27
+ }
28
+
29
+ if (!isModuleLevel && !hasBodyUseServer) {
30
+ if (options.serverFiles) {
31
+ options.serverFiles.delete(norm);
32
+ }
33
+ return source;
34
+ }
35
+
36
+ const exports = isModuleLevel
37
+ ? parseExports(source)
38
+ : extractServerFunctions(source, this.resourcePath);
39
+ if (!exports || exports.length === 0) {
40
+ if (options.serverFiles) {
41
+ options.serverFiles.delete(norm);
42
+ }
43
+ return source;
44
+ }
45
+
46
+ // Build IDs
47
+ const moduleId = this.resourcePath;
48
+ const relativePath = path.relative(
49
+ normalizePathCase(process.cwd()),
50
+ normalizePathCase(moduleId)
51
+ );
52
+ const normalizedPath = relativePath.replace(/\\/g, "/");
53
+
54
+ const fileUrl = `file:///${normalizedPath}`;
55
+
56
+ //
57
+ // IMPORTANT: dynamic import instead of static import
58
+ //
59
+ // Webpack will NOT try to resolve "__SERVER_FUNCTION_PROXY__"
60
+ // as a module → it will remain a string → replaced later → browser loads it.
61
+
62
+ let proxyCode = `
63
+ const loadProxy = new Function('return import("/"+"__SERVER_FUNCTION_PROXY__")');
64
+ `;
65
+
66
+ for (const exp of exports) {
67
+ const key = exp === "default" ? `${fileUrl}#default` : `${fileUrl}#${exp}`;
68
+
69
+ if (exp === "default") {
70
+ proxyCode += `
71
+ export default (...args) =>
72
+ loadProxy().then(mod =>
73
+ (mod.default ?? mod ?? window.__SERVER_FUNCTION_PROXY_LIB__).createServerFunctionProxy(${JSON.stringify(
74
+ key,
75
+ )})(...args)
76
+ );
77
+ `;
78
+ } else {
79
+ proxyCode += `
80
+ export const ${exp} = (...args) =>
81
+ loadProxy().then(mod => (mod.default ?? mod ?? window.__SERVER_FUNCTION_PROXY_LIB__).createServerFunctionProxy(${JSON.stringify(
82
+ key,
83
+ )})(...args)
84
+ );
85
+ `;
86
+ }
87
+ }
88
+
89
+ // Emit manifest entry
90
+ const manifestEntry = {
91
+ path: normalizedPath,
92
+ exports: exports,
93
+ };
94
+
95
+ this.emitFile(
96
+ `server-functions/${normalizedPath}.json`,
97
+ JSON.stringify(manifestEntry, null, 2),
98
+ );
99
+
100
+ return proxyCode;
101
+ };