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
|
@@ -2,7 +2,7 @@ import parser from "#babel/parser";
|
|
|
2
2
|
import traverseModule from "#babel/traverse";
|
|
3
3
|
import generateModule from "#babel/generator";
|
|
4
4
|
import * as tModule from "#babel/types";
|
|
5
|
-
import { useServerRegex } from "../constants.js";
|
|
5
|
+
import { useClientRegex, useServerRegex } from "../constants.js";
|
|
6
6
|
import parseExports from "./parse-exports.js";
|
|
7
7
|
|
|
8
8
|
const traverse = traverseModule?.default || traverseModule;
|
|
@@ -64,28 +64,142 @@ function isInsideScope(childScope, parentScope) {
|
|
|
64
64
|
return false;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
const BABEL_PARSER_PLUGINS = [
|
|
68
|
+
"jsx",
|
|
69
|
+
"typescript",
|
|
70
|
+
"asyncGenerators",
|
|
71
|
+
"classProperties",
|
|
72
|
+
"classPrivateProperties",
|
|
73
|
+
"classPrivateMethods",
|
|
74
|
+
"dynamicImport",
|
|
75
|
+
"exportDefaultFrom",
|
|
76
|
+
"exportNamespaceFrom",
|
|
77
|
+
"nullishCoalescingOperator",
|
|
78
|
+
"numericSeparator",
|
|
79
|
+
"objectRestSpread",
|
|
80
|
+
"optionalCatchBinding",
|
|
81
|
+
"optionalChaining",
|
|
82
|
+
"topLevelAwait",
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
function getExportedSpecifierName(exported) {
|
|
86
|
+
if (!exported) return null;
|
|
87
|
+
if (exported.type === "Identifier") return exported.name;
|
|
88
|
+
if (exported.type === "StringLiteral") return exported.value;
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Resolves (and if anonymous/expression, binds) a local module-scope identifier
|
|
94
|
+
* for a module's `default` export so `registerServerReference` can reference it.
|
|
95
|
+
*/
|
|
96
|
+
function ensureModuleDefaultIdentifier(code, filename) {
|
|
97
|
+
let ast;
|
|
98
|
+
try {
|
|
99
|
+
ast = parser.parse(code, {
|
|
100
|
+
sourceType: "module",
|
|
101
|
+
plugins: BABEL_PARSER_PLUGINS,
|
|
102
|
+
});
|
|
103
|
+
} catch (err) {
|
|
104
|
+
console.warn(`[transformServerFunctions] Babel parse error in ${filename}:`, err.message);
|
|
105
|
+
return { didMutate: false, code, defaultExportIdentifier: null };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
let defaultExportIdentifier = null;
|
|
109
|
+
let didMutate = false;
|
|
110
|
+
const body = ast.program.body;
|
|
111
|
+
|
|
112
|
+
for (let i = 0; i < body.length; i++) {
|
|
113
|
+
const stmt = body[i];
|
|
114
|
+
if (stmt.type === "ExportDefaultDeclaration") {
|
|
115
|
+
const decl = stmt.declaration;
|
|
116
|
+
if (decl.type === "FunctionDeclaration" || decl.type === "ClassDeclaration") {
|
|
117
|
+
if (decl.id && decl.id.name) {
|
|
118
|
+
defaultExportIdentifier = decl.id.name;
|
|
119
|
+
} else {
|
|
120
|
+
decl.id = t.identifier("$$dinou_default_action");
|
|
121
|
+
defaultExportIdentifier = "$$dinou_default_action";
|
|
122
|
+
didMutate = true;
|
|
123
|
+
}
|
|
124
|
+
} else if (decl.type === "Identifier") {
|
|
125
|
+
defaultExportIdentifier = decl.name;
|
|
126
|
+
} else {
|
|
127
|
+
const defaultId = "$$dinou_default_action";
|
|
128
|
+
defaultExportIdentifier = defaultId;
|
|
129
|
+
body.splice(
|
|
130
|
+
i,
|
|
131
|
+
1,
|
|
132
|
+
t.variableDeclaration("const", [
|
|
133
|
+
t.variableDeclarator(t.identifier(defaultId), decl),
|
|
134
|
+
]),
|
|
135
|
+
t.exportDefaultDeclaration(t.identifier(defaultId))
|
|
136
|
+
);
|
|
137
|
+
didMutate = true;
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
} else if (stmt.type === "ExportNamedDeclaration" && Array.isArray(stmt.specifiers)) {
|
|
141
|
+
const defaultSpec = stmt.specifiers.find(
|
|
142
|
+
(s) => s.type === "ExportSpecifier" && getExportedSpecifierName(s.exported) === "default"
|
|
143
|
+
);
|
|
144
|
+
if (defaultSpec) {
|
|
145
|
+
if (!stmt.source && defaultSpec.local && defaultSpec.local.type === "Identifier") {
|
|
146
|
+
defaultExportIdentifier = defaultSpec.local.name;
|
|
147
|
+
} else if (stmt.source && defaultSpec.local) {
|
|
148
|
+
const defaultId = "$$dinou_default_action";
|
|
149
|
+
defaultExportIdentifier = defaultId;
|
|
150
|
+
body.unshift(
|
|
151
|
+
t.importDeclaration(
|
|
152
|
+
[t.importSpecifier(t.identifier(defaultId), t.cloneNode(defaultSpec.local))],
|
|
153
|
+
t.cloneNode(stmt.source)
|
|
154
|
+
)
|
|
155
|
+
);
|
|
156
|
+
didMutate = true;
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (didMutate) {
|
|
164
|
+
const output = generate(ast, { retainLines: false });
|
|
165
|
+
return { didMutate: true, code: output.code, defaultExportIdentifier };
|
|
166
|
+
}
|
|
167
|
+
return { didMutate: false, code, defaultExportIdentifier };
|
|
168
|
+
}
|
|
169
|
+
|
|
67
170
|
/**
|
|
68
171
|
* Transforms function-level "use server" directives:
|
|
69
172
|
* 1. Hoists inline/nested server functions to module top level and binds captured closure variables.
|
|
70
173
|
* 2. Ensures top-level functions with "use server" in their body are exported so they can be referenced and invoked.
|
|
71
|
-
* 3. Returns the transformed code
|
|
174
|
+
* 3. Returns the transformed code, the list of exported server function names, and defaultExportIdentifier when "default" is an action.
|
|
72
175
|
*
|
|
73
176
|
* @param {string} code Source code
|
|
74
177
|
* @param {string} [filename="file.tsx"]
|
|
75
|
-
* @returns {{ transformed: boolean, isModuleLevel: boolean, code: string, actions: string[] }}
|
|
178
|
+
* @returns {{ transformed: boolean, isModuleLevel: boolean, code: string, actions: string[], defaultExportIdentifier: string | null }}
|
|
76
179
|
*/
|
|
77
180
|
export function transformServerFunctions(code, filename = "file.tsx") {
|
|
78
181
|
if (!hasServerFunctions(code)) {
|
|
79
|
-
return { transformed: false, isModuleLevel: false, code, actions: [] };
|
|
182
|
+
return { transformed: false, isModuleLevel: false, code, actions: [], defaultExportIdentifier: null };
|
|
80
183
|
}
|
|
81
184
|
|
|
82
185
|
if (isModuleLevelUseServer(code)) {
|
|
83
186
|
const exports = parseExports(code);
|
|
187
|
+
if (exports.includes("default")) {
|
|
188
|
+
const resolved = ensureModuleDefaultIdentifier(code, filename);
|
|
189
|
+
return {
|
|
190
|
+
transformed: resolved.didMutate,
|
|
191
|
+
isModuleLevel: true,
|
|
192
|
+
code: resolved.code,
|
|
193
|
+
actions: exports,
|
|
194
|
+
defaultExportIdentifier: resolved.defaultExportIdentifier,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
84
197
|
return {
|
|
85
198
|
transformed: false,
|
|
86
199
|
isModuleLevel: true,
|
|
87
200
|
code,
|
|
88
201
|
actions: exports,
|
|
202
|
+
defaultExportIdentifier: null,
|
|
89
203
|
};
|
|
90
204
|
}
|
|
91
205
|
|
|
@@ -93,31 +207,16 @@ export function transformServerFunctions(code, filename = "file.tsx") {
|
|
|
93
207
|
try {
|
|
94
208
|
ast = parser.parse(code, {
|
|
95
209
|
sourceType: "module",
|
|
96
|
-
plugins:
|
|
97
|
-
"jsx",
|
|
98
|
-
"typescript",
|
|
99
|
-
"asyncGenerators",
|
|
100
|
-
"classProperties",
|
|
101
|
-
"classPrivateProperties",
|
|
102
|
-
"classPrivateMethods",
|
|
103
|
-
"dynamicImport",
|
|
104
|
-
"exportDefaultFrom",
|
|
105
|
-
"exportNamespaceFrom",
|
|
106
|
-
"nullishCoalescingOperator",
|
|
107
|
-
"numericSeparator",
|
|
108
|
-
"objectRestSpread",
|
|
109
|
-
"optionalCatchBinding",
|
|
110
|
-
"optionalChaining",
|
|
111
|
-
"topLevelAwait",
|
|
112
|
-
],
|
|
210
|
+
plugins: BABEL_PARSER_PLUGINS,
|
|
113
211
|
});
|
|
114
212
|
} catch (err) {
|
|
115
213
|
console.warn(`[transformServerFunctions] Babel parse error in ${filename}:`, err.message);
|
|
116
|
-
return { transformed: false, isModuleLevel: false, code, actions: [] };
|
|
214
|
+
return { transformed: false, isModuleLevel: false, code, actions: [], defaultExportIdentifier: null };
|
|
117
215
|
}
|
|
118
216
|
|
|
119
217
|
const actions = [];
|
|
120
218
|
const hoistedNodes = [];
|
|
219
|
+
let defaultExportIdentifier = null;
|
|
121
220
|
let actionCounter = 0;
|
|
122
221
|
|
|
123
222
|
traverse(ast, {
|
|
@@ -155,7 +254,33 @@ export function transformServerFunctions(code, filename = "file.tsx") {
|
|
|
155
254
|
|
|
156
255
|
if (isTopLevel) {
|
|
157
256
|
cleanDirectivesAndStatements();
|
|
158
|
-
|
|
257
|
+
|
|
258
|
+
if (path.parentPath.isExportDefaultDeclaration()) {
|
|
259
|
+
if (!actions.includes("default")) {
|
|
260
|
+
actions.push("default");
|
|
261
|
+
}
|
|
262
|
+
if (path.isFunctionDeclaration()) {
|
|
263
|
+
if (!path.node.id) {
|
|
264
|
+
path.node.id = t.identifier("$$dinou_default_action");
|
|
265
|
+
}
|
|
266
|
+
defaultExportIdentifier = path.node.id.name;
|
|
267
|
+
} else {
|
|
268
|
+
const defaultId = "$$dinou_default_action";
|
|
269
|
+
defaultExportIdentifier = defaultId;
|
|
270
|
+
path.parentPath.replaceWithMultiple([
|
|
271
|
+
t.variableDeclaration("const", [
|
|
272
|
+
t.variableDeclarator(t.identifier(defaultId), path.node),
|
|
273
|
+
]),
|
|
274
|
+
t.exportDefaultDeclaration(t.identifier(defaultId)),
|
|
275
|
+
]);
|
|
276
|
+
}
|
|
277
|
+
path.skip();
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (!actions.includes(fnName)) {
|
|
282
|
+
actions.push(fnName);
|
|
283
|
+
}
|
|
159
284
|
|
|
160
285
|
if (path.isFunctionDeclaration()) {
|
|
161
286
|
if (!path.parentPath.isExportNamedDeclaration()) {
|
|
@@ -244,11 +369,41 @@ export function transformServerFunctions(code, filename = "file.tsx") {
|
|
|
244
369
|
});
|
|
245
370
|
|
|
246
371
|
if (actions.length === 0) {
|
|
247
|
-
return { transformed: false, isModuleLevel: false, code, actions: [] };
|
|
372
|
+
return { transformed: false, isModuleLevel: false, code, actions: [], defaultExportIdentifier: null };
|
|
248
373
|
}
|
|
249
374
|
|
|
250
375
|
// Insert hoisted function declarations right after the last import statement in the program
|
|
251
376
|
const program = ast.program;
|
|
377
|
+
if (!defaultExportIdentifier) {
|
|
378
|
+
for (const stmt of program.body) {
|
|
379
|
+
if (stmt.type === "ExportDefaultDeclaration" && stmt.declaration.type === "Identifier") {
|
|
380
|
+
if (actions.includes(stmt.declaration.name)) {
|
|
381
|
+
defaultExportIdentifier = stmt.declaration.name;
|
|
382
|
+
if (!actions.includes("default")) actions.push("default");
|
|
383
|
+
}
|
|
384
|
+
break;
|
|
385
|
+
} else if (
|
|
386
|
+
stmt.type === "ExportNamedDeclaration" &&
|
|
387
|
+
!stmt.source &&
|
|
388
|
+
Array.isArray(stmt.specifiers)
|
|
389
|
+
) {
|
|
390
|
+
const defaultSpec = stmt.specifiers.find(
|
|
391
|
+
(s) => s.type === "ExportSpecifier" && getExportedSpecifierName(s.exported) === "default"
|
|
392
|
+
);
|
|
393
|
+
if (
|
|
394
|
+
defaultSpec &&
|
|
395
|
+
defaultSpec.local &&
|
|
396
|
+
defaultSpec.local.type === "Identifier" &&
|
|
397
|
+
actions.includes(defaultSpec.local.name)
|
|
398
|
+
) {
|
|
399
|
+
defaultExportIdentifier = defaultSpec.local.name;
|
|
400
|
+
if (!actions.includes("default")) actions.push("default");
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
252
407
|
let lastImportIdx = -1;
|
|
253
408
|
for (let i = 0; i < program.body.length; i++) {
|
|
254
409
|
if (program.body[i].type === "ImportDeclaration") {
|
|
@@ -266,6 +421,7 @@ export function transformServerFunctions(code, filename = "file.tsx") {
|
|
|
266
421
|
isModuleLevel: false,
|
|
267
422
|
code: output.code,
|
|
268
423
|
actions,
|
|
424
|
+
defaultExportIdentifier,
|
|
269
425
|
};
|
|
270
426
|
}
|
|
271
427
|
|
|
@@ -284,9 +440,101 @@ export function extractServerFunctions(code, filename = "file.tsx") {
|
|
|
284
440
|
return result.actions || [];
|
|
285
441
|
}
|
|
286
442
|
|
|
443
|
+
const SSR_SERVER_STUB_PREAMBLE = `import { registerServerReference as __dinouRegisterServerReference } from "react-server-dom-webpack/client.edge";
|
|
444
|
+
function __dinouCreateSsrServerStub(id) {
|
|
445
|
+
const stub = () => {
|
|
446
|
+
const err = new Error(
|
|
447
|
+
\`[Dinou] Server Function "\${id}" deferred from SSR to client <Suspense> mount.\`
|
|
448
|
+
);
|
|
449
|
+
Object.defineProperty(err, Symbol.for("react.recoverable"), { value: true });
|
|
450
|
+
const rejected = Promise.reject(err);
|
|
451
|
+
rejected.catch(() => {});
|
|
452
|
+
return rejected;
|
|
453
|
+
};
|
|
454
|
+
__dinouRegisterServerReference(stub, id);
|
|
455
|
+
return stub;
|
|
456
|
+
}
|
|
457
|
+
`;
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Generates a pure SSR Server Reference stub module for Pass B (`ssr-engine`),
|
|
461
|
+
* stripping all server-side implementation bodies and server-only imports while
|
|
462
|
+
* preserving exact ESM export signatures (`export const ...` and `export default ...`).
|
|
463
|
+
*
|
|
464
|
+
* @param {string} code
|
|
465
|
+
* @param {string} [filename="file.tsx"]
|
|
466
|
+
* @param {string} [relativeFileUrl="file:///src/action.ts"]
|
|
467
|
+
* @returns {string | null}
|
|
468
|
+
*/
|
|
469
|
+
export function buildSsrServerReferenceModule(
|
|
470
|
+
code,
|
|
471
|
+
filename = "file.tsx",
|
|
472
|
+
relativeFileUrl = "file:///src/action.ts"
|
|
473
|
+
) {
|
|
474
|
+
if (typeof code !== "string" || useClientRegex.test(code.trim())) {
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
477
|
+
if (!hasServerFunctions(code)) {
|
|
478
|
+
return null;
|
|
479
|
+
}
|
|
480
|
+
const actions = extractServerFunctions(code, filename);
|
|
481
|
+
if (!actions || actions.length === 0) {
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const uniqueActions = Array.from(new Set(actions));
|
|
486
|
+
let out = SSR_SERVER_STUB_PREAMBLE;
|
|
487
|
+
for (const name of uniqueActions) {
|
|
488
|
+
const fullId = `${relativeFileUrl}#${name}`;
|
|
489
|
+
if (name === "default") {
|
|
490
|
+
out += `const __dinou_default_stub = __dinouCreateSsrServerStub(${JSON.stringify(fullId)});\n`;
|
|
491
|
+
out += `export default __dinou_default_stub;\n`;
|
|
492
|
+
} else {
|
|
493
|
+
out += `export const ${name} = __dinouCreateSsrServerStub(${JSON.stringify(fullId)});\n`;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return out;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Synthesizes `sf_0`, `sf_1`, ... module namespace stub objects directly inside
|
|
501
|
+
* `ssr-client-manifest` from `server-functions-manifest.json` without importing
|
|
502
|
+
* server source files into the browser-conditioned SSR engine (Pass B).
|
|
503
|
+
*
|
|
504
|
+
* @param {Record<string, string[] | Set<string>>} parsedServerFunctionsManifest
|
|
505
|
+
* @returns {string}
|
|
506
|
+
*/
|
|
507
|
+
export function generateSsrServerManifestStubs(parsedServerFunctionsManifest) {
|
|
508
|
+
const serverFunctionFiles = Object.keys(parsedServerFunctionsManifest || {});
|
|
509
|
+
if (serverFunctionFiles.length === 0) {
|
|
510
|
+
return "";
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
let code = `\n${SSR_SERVER_STUB_PREAMBLE}`;
|
|
514
|
+
serverFunctionFiles.forEach((relPath, index) => {
|
|
515
|
+
const normRel = relPath.replace(/\\/g, "/");
|
|
516
|
+
const relFileUrl = "file:///" + normRel;
|
|
517
|
+
const rawFns = parsedServerFunctionsManifest[relPath] || [];
|
|
518
|
+
const fns = Array.from(
|
|
519
|
+
new Set(Array.isArray(rawFns) ? rawFns : Array.from(rawFns))
|
|
520
|
+
);
|
|
521
|
+
const entries = fns
|
|
522
|
+
.map(
|
|
523
|
+
(fn) =>
|
|
524
|
+
` ${JSON.stringify(fn)}: __dinouCreateSsrServerStub(${JSON.stringify(`${relFileUrl}#${fn}`)})`
|
|
525
|
+
)
|
|
526
|
+
.join(",\n");
|
|
527
|
+
const objLiteral = entries ? `{\n${entries}\n }` : `{}`;
|
|
528
|
+
code += `const sf_${index} = Object.defineProperty(${objLiteral}, "__esModule", { value: true });\n`;
|
|
529
|
+
});
|
|
530
|
+
return code;
|
|
531
|
+
}
|
|
532
|
+
|
|
287
533
|
export default {
|
|
288
534
|
hasServerFunctions,
|
|
289
535
|
isModuleLevelUseServer,
|
|
290
536
|
transformServerFunctions,
|
|
291
537
|
extractServerFunctions,
|
|
538
|
+
buildSsrServerReferenceModule,
|
|
539
|
+
generateSsrServerManifestStubs,
|
|
292
540
|
};
|