dinou 9.0.2 → 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.
- package/CHANGELOG.md +54 -0
- package/README.md +503 -9883
- package/dinou/bun/build.js +38 -1
- package/{cli.js → dinou/cli.js} +8 -7
- package/dinou/cloudflare/build.js +37 -0
- package/dinou/core/build-static-pages.js +15 -7
- package/dinou/core/cache-revalidate.js +909 -151
- package/dinou/core/cache-slot.js +728 -97
- package/dinou/core/client-webpack.jsx +60 -9
- package/dinou/core/client.jsx +60 -9
- package/dinou/core/edge-ssr.js +33 -5
- package/dinou/core/generate-static.js +109 -47
- package/dinou/core/get-jsx.js +333 -329
- package/dinou/core/handler.js +2983 -2343
- package/dinou/core/layout-functions.js +102 -1
- package/dinou/core/ppr-context.js +256 -105
- package/dinou/core/ppr-runtime.js +540 -267
- package/dinou/core/render-jsx-to-client-jsx.js +131 -71
- package/dinou/core/slot.js +246 -33
- package/dinou/core/storage-adapter.js +117 -11
- package/dinou/deno/build.js +38 -1
- package/dinou/deno/seed-kv.js +43 -11
- package/dinou/esbuild/helpers-esbuild/swc-disk-cache.js +242 -225
- package/dinou/esbuild/plugins-esbuild/server-functions-plugin.js +253 -226
- package/dinou/esbuild/react-refresh/esm-hmr/client.js +209 -209
- package/dinou/node/build.js +2 -2
- package/dinou/node/bundle-dual-engine.js +2 -0
- package/dinou/node/dev.js +2853 -2704
- package/dinou/package.json +5 -2
- package/dinou/rollup/react-refresh/esm-hmr/client.js +220 -220
- package/dinou/rollup/rollup-plugins/rollup-plugin-server-functions.js +182 -160
- package/dinou/server.d.ts +25 -11
- package/dinou/server.js +5 -0
- package/dinou/webpack/loaders/server-functions-loader.js +101 -94
- package/dinou/webpack/webpack.config.js +9 -4
- package/eject.js +219 -223
- 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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
|
|
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
|
-
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (!
|
|
30
|
-
if (options.serverFiles) {
|
|
31
|
-
options.serverFiles.delete(norm);
|
|
32
|
-
}
|
|
33
|
-
return source;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const exports =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
+
};
|