dinou 8.0.2 → 8.1.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.
- package/CHANGELOG.md +25 -0
- package/README.md +15 -3
- package/dinou/bun/build.js +15 -6
- package/dinou/cloudflare/build.js +15 -6
- package/dinou/core/load-dinou-config.js +10 -0
- package/dinou/core/route-generator.js +1 -1
- package/dinou/core/transform-server-functions.js +291 -0
- package/dinou/deno/build.js +15 -6
- package/dinou/esbuild/build.js +7 -2
- package/dinou/esbuild/helpers-esbuild/get-config-esbuild-prod.js +3 -3
- package/dinou/esbuild/helpers-esbuild/get-esbuild-entries.js +17 -6
- package/dinou/esbuild/plugins-esbuild/manifest-generator-plugin.js +4 -2
- package/dinou/esbuild/plugins-esbuild/server-functions-plugin.js +43 -29
- package/dinou/index.d.ts +9 -3
- package/dinou/index.js +1 -4
- package/dinou/node/bundle-dual-engine.js +15 -6
- package/dinou/node/dev.js +16 -7
- package/dinou/package.json +48 -48
- package/dinou/rollup/rollup-plugins/rollup-plugin-react-client-manifest.js +32 -7
- package/dinou/rollup/rollup-plugins/rollup-plugin-server-functions.js +44 -6
- package/dinou/webpack/helpers/get-webpack-entries.js +24 -4
- package/dinou/webpack/helpers/patch-webpack-internals.js +35 -0
- package/dinou/webpack/plugins/server-functions-plugin.js +44 -2
- package/dinou/webpack/webpack.config.js +2 -0
- package/package.json +48 -48
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,31 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/).
|
|
7
7
|
|
|
8
|
+
## [8.1.0] - 2026-10-23
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- **Function-Level Server Functions & Closure Hoisting**: Added full support for `"use server"` directives inside function bodies—both in utility files and inline closures within Server Components. Dinou's compiler statically detects captured lexical variables, hoists nested functions to top-level module exports (`$$dinou_action_<name>_<id>`), and binds closures (`.bind(null, ...args)`) for native React 19 RSC streaming and RPC execution via `POST /____server_function____`.
|
|
12
|
+
- **Server Functions as Props**: Server Components can now pass Server Functions (including inline closures and bound instances) directly as props to Client Components. They can be seamlessly invoked inside client event handlers, transitions (`useTransition`), form actions (`<form action={...}>`), or React 19's `useActionState`.
|
|
13
|
+
- **Auto-Exporting of Top-Level Actions**: Top-level functions declaring `"use server"` without an explicit `export` are automatically exported at compile time, allowing the HTTP RPC dispatcher to dynamically load them via ESM `import()` without extra boilerplate.
|
|
14
|
+
- **Granular Manifest Tree-Shaking**: Updated `server-functions-manifest.json` generation across all bundlers (Rollup, Webpack, esbuild) and runtimes. In files containing function-level actions, only functions with `"use server"` in their body are registered in the manifest, keeping private utilities hidden. Unreferenced ("orphan") action modules are completely excluded.
|
|
15
|
+
- **Discovery of Function-Level Actions in Bundler Crawlers**: Enhanced entry crawlers across esbuild, Webpack, and Rollup to detect pages, layouts, and component or utility files containing body-level `"use server"` (`hasServerFunctions`). These files are now properly registered in `serverFiles` for manifest generation while continuing recursive traversal of their styles and static assets.
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
- **Deterministic Builds via Pinned Dependencies**: Pinned all internal compiler and build toolchain dependencies (`esbuild`, `webpack`, `rollup`, `@swc/core`, `@babel/*`, PostCSS, and loaders) to exact versions (removing `^` carets). This guarantees 100% reproducible, zero-drift builds across all environments and prevents upstream regressions from affecting end-user applications.
|
|
19
|
+
- **Aligned React 19 RSC Protocol & Peer Dependencies**: Synchronized `@roggc/react-server-dom-esm` and `react-server-dom-webpack` to `19.3.1`. Updated Dinou's `peerDependencies` to `react >=19.3.1` and `react-dom >=19.3.1` for complete protocol parity across all bundlers without peer dependency warnings.
|
|
20
|
+
|
|
21
|
+
### Fixed
|
|
22
|
+
- **Fail-Fast Validation for Server Functions Proxy**: Added build-time checks across esbuild, Rollup, and Webpack to immediately fail with a descriptive error if `serverFunctionProxy.js` is missing from the client manifest in production, preventing silent 404 dynamic import failures in the browser.
|
|
23
|
+
- **esbuild Manifest Propagation Fix**: Ensured the client manifest reference is correctly passed by reference to `serverFunctionsPlugin` in production builds, resolving dynamic import 404s when invoking Server Functions from Client Components.
|
|
24
|
+
- **Webpack 5.112+ Internal Path Compatibility Shim**: Added a module resolution shim (`patch-webpack-internals.js`) in `webpack.config.js` to redirect legacy `react-server-dom-webpack` internal paths to Webpack 5.112+'s reorganized directory structure.
|
|
25
|
+
- **DRY Single-Source Definition of `defineConfig`**: Refactored `dinou/index.js` to re-export `defineConfig` directly from `./core/config.js` instead of maintaining duplicate definitions.
|
|
26
|
+
|
|
27
|
+
## [8.0.3] - 2026-10-22
|
|
28
|
+
|
|
29
|
+
### Fixed
|
|
30
|
+
- **Type-Safe Route Pattern Autocompletion in Segment Configs**: Added generic constraints (`TRouteOrParams extends DinouRoutePattern | object = any`) to `PageFunctions` and `LayoutFunctions`, as well as `RouteParams<T extends DinouRoutePattern = DinouRoutePattern>`. This enables immediate IDE / TypeScript autocompletion for known project routes when typing string literals (`PageFunctions<"..."`) and strict compile-time validation against typos in route strings, while preserving full backward compatibility for parameter dictionaries and custom interfaces (`PageFunctions<CustomParams>`).
|
|
31
|
+
- **Direct `defineConfig` Import from `"dinou"`**: Added an esbuild resolution plugin (`dinou-config-alias`) in `load-dinou-config.js` to alias exact `"dinou"` specifier imports to `"dinou/config"` when bundling configuration files (`dinou.config.ts`, `dinou.config.mjs`, etc.). This allows importing `defineConfig` directly from `"dinou"` (`import { defineConfig } from "dinou"`) without triggering Node.js native ESM `ERR_UNKNOWN_FILE_EXTENSION` errors from unbundled React JSX components (`redirect.jsx`, `link.jsx`) in `dinou/index.js`, while keeping all other subpath imports (`dinou/adapters/*`, `dinou/config`) intact.
|
|
32
|
+
|
|
8
33
|
## [8.0.2] - 2026-10-22
|
|
9
34
|
|
|
10
35
|
### Fixed
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# **Dinou**
|
|
2
2
|
|
|
3
|
-
[](https://dinou.dev) [](https://dinou.dev) [](https://www.npmjs.com/package/dinou)
|
|
4
4
|
|
|
5
5
|
### **Full-Stack React 19 Framework**
|
|
6
6
|
|
|
@@ -2222,12 +2222,24 @@ export interface RouteSegmentConfig<TParams = any, TProps = any> {
|
|
|
2222
2222
|
/**
|
|
2223
2223
|
* Concrete semantic alias to import and use in 'page_functions.ts'
|
|
2224
2224
|
*/
|
|
2225
|
-
export type PageFunctions<
|
|
2225
|
+
export type PageFunctions<
|
|
2226
|
+
TRouteOrParams extends DinouRoutePattern | object = any,
|
|
2227
|
+
TProps = any
|
|
2228
|
+
> = RouteSegmentConfig<
|
|
2229
|
+
[TRouteOrParams] extends [string] ? RouteParams<TRouteOrParams> : TRouteOrParams,
|
|
2230
|
+
TProps
|
|
2231
|
+
>;
|
|
2226
2232
|
|
|
2227
2233
|
/**
|
|
2228
2234
|
* Concrete semantic alias to import and use in 'layout_functions.ts'
|
|
2229
2235
|
*/
|
|
2230
|
-
export type LayoutFunctions<
|
|
2236
|
+
export type LayoutFunctions<
|
|
2237
|
+
TRouteOrParams extends DinouRoutePattern | object = any,
|
|
2238
|
+
TProps = any
|
|
2239
|
+
> = RouteSegmentConfig<
|
|
2240
|
+
[TRouteOrParams] extends [string] ? RouteParams<TRouteOrParams> : TRouteOrParams,
|
|
2241
|
+
TProps
|
|
2242
|
+
>;
|
|
2231
2243
|
```
|
|
2232
2244
|
|
|
2233
2245
|
| Where to use | Type to import | Purpose |
|
package/dinou/bun/build.js
CHANGED
|
@@ -12,6 +12,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
12
12
|
import { generateRouteModulesCode, generateRouteTypes } from "../core/route-generator.js";
|
|
13
13
|
import parseExports from "../core/parse-exports.js";
|
|
14
14
|
import { useClientRegex, useServerRegex } from "../constants.js";
|
|
15
|
+
import { transformServerFunctions, hasServerFunctions } from "../core/transform-server-functions.js";
|
|
15
16
|
import {
|
|
16
17
|
isSupportedClientModule,
|
|
17
18
|
scanProjectDependenciesForClientComponents,
|
|
@@ -931,14 +932,18 @@ const serverReferencesPlugin = {
|
|
|
931
932
|
return null;
|
|
932
933
|
}
|
|
933
934
|
|
|
934
|
-
if (!
|
|
935
|
+
if (!hasServerFunctions(code)) return null;
|
|
935
936
|
|
|
936
|
-
const
|
|
937
|
+
const sfResult = transformServerFunctions(code, args.path);
|
|
938
|
+
if (!sfResult.actions || sfResult.actions.length === 0) return null;
|
|
939
|
+
|
|
940
|
+
const transformedCode = sfResult.code;
|
|
941
|
+
const exports = sfResult.actions;
|
|
937
942
|
const absPath = path.resolve(args.path);
|
|
938
943
|
const relPath = path.relative(projectRoot, absPath).replace(/\\/g, "/");
|
|
939
944
|
const relativeFileUrl = "file:///" + relPath;
|
|
940
945
|
|
|
941
|
-
let transformed =
|
|
946
|
+
let transformed = transformedCode + "\n\n";
|
|
942
947
|
transformed += `import { registerServerReference } from "react-server-dom-webpack/server.edge";\n`;
|
|
943
948
|
|
|
944
949
|
for (const name of exports) {
|
|
@@ -972,14 +977,18 @@ const serverReferencesPluginSsr = {
|
|
|
972
977
|
return null;
|
|
973
978
|
}
|
|
974
979
|
|
|
975
|
-
if (!
|
|
980
|
+
if (!hasServerFunctions(code)) return null;
|
|
976
981
|
|
|
977
|
-
const
|
|
982
|
+
const sfResult = transformServerFunctions(code, args.path);
|
|
983
|
+
if (!sfResult.actions || sfResult.actions.length === 0) return null;
|
|
984
|
+
|
|
985
|
+
const transformedCode = sfResult.code;
|
|
986
|
+
const exports = sfResult.actions;
|
|
978
987
|
const absPath = path.resolve(args.path);
|
|
979
988
|
const relPath = path.relative(projectRoot, absPath).replace(/\\/g, "/");
|
|
980
989
|
const relativeFileUrl = "file:///" + relPath;
|
|
981
990
|
|
|
982
|
-
let transformed =
|
|
991
|
+
let transformed = transformedCode + "\n\n";
|
|
983
992
|
transformed += `import { registerServerReference } from "react-server-dom-webpack/client.edge";\n`;
|
|
984
993
|
|
|
985
994
|
for (const name of exports) {
|
|
@@ -12,6 +12,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
12
12
|
import { generateRouteModulesCode, generateRouteTypes } from "../core/route-generator.js";
|
|
13
13
|
import parseExports from "../core/parse-exports.js";
|
|
14
14
|
import { useClientRegex, useServerRegex } from "../constants.js";
|
|
15
|
+
import { transformServerFunctions, hasServerFunctions } from "../core/transform-server-functions.js";
|
|
15
16
|
import {
|
|
16
17
|
isSupportedClientModule,
|
|
17
18
|
scanProjectDependenciesForClientComponents,
|
|
@@ -976,14 +977,18 @@ const serverReferencesPlugin = {
|
|
|
976
977
|
return null;
|
|
977
978
|
}
|
|
978
979
|
|
|
979
|
-
if (!
|
|
980
|
+
if (!hasServerFunctions(code)) return null;
|
|
980
981
|
|
|
981
|
-
const
|
|
982
|
+
const sfResult = transformServerFunctions(code, args.path);
|
|
983
|
+
if (!sfResult.actions || sfResult.actions.length === 0) return null;
|
|
984
|
+
|
|
985
|
+
const transformedCode = sfResult.code;
|
|
986
|
+
const exports = sfResult.actions;
|
|
982
987
|
const absPath = path.resolve(args.path);
|
|
983
988
|
const relPath = path.relative(projectRoot, absPath).replace(/\\/g, "/");
|
|
984
989
|
const relativeFileUrl = "file:///" + relPath;
|
|
985
990
|
|
|
986
|
-
let transformed =
|
|
991
|
+
let transformed = transformedCode + "\n\n";
|
|
987
992
|
transformed += `import { registerServerReference } from "react-server-dom-webpack/server.edge";\n`;
|
|
988
993
|
|
|
989
994
|
for (const name of exports) {
|
|
@@ -1017,14 +1022,18 @@ const serverReferencesPluginSsr = {
|
|
|
1017
1022
|
return null;
|
|
1018
1023
|
}
|
|
1019
1024
|
|
|
1020
|
-
if (!
|
|
1025
|
+
if (!hasServerFunctions(code)) return null;
|
|
1021
1026
|
|
|
1022
|
-
const
|
|
1027
|
+
const sfResult = transformServerFunctions(code, args.path);
|
|
1028
|
+
if (!sfResult.actions || sfResult.actions.length === 0) return null;
|
|
1029
|
+
|
|
1030
|
+
const transformedCode = sfResult.code;
|
|
1031
|
+
const exports = sfResult.actions;
|
|
1023
1032
|
const absPath = path.resolve(args.path);
|
|
1024
1033
|
const relPath = path.relative(projectRoot, absPath).replace(/\\/g, "/");
|
|
1025
1034
|
const relativeFileUrl = "file:///" + relPath;
|
|
1026
1035
|
|
|
1027
|
-
let transformed =
|
|
1036
|
+
let transformed = transformedCode + "\n\n";
|
|
1028
1037
|
transformed += `import { registerServerReference } from "react-server-dom-webpack/client.edge";\n`;
|
|
1029
1038
|
|
|
1030
1039
|
for (const name of exports) {
|
|
@@ -51,6 +51,16 @@ async function getDinouConfig() {
|
|
|
51
51
|
platform: "node",
|
|
52
52
|
packages: "external",
|
|
53
53
|
sourcemap: "inline",
|
|
54
|
+
plugins: [
|
|
55
|
+
{
|
|
56
|
+
name: "dinou-config-alias",
|
|
57
|
+
setup(build) {
|
|
58
|
+
build.onResolve({ filter: /^dinou$/ }, () => {
|
|
59
|
+
return { path: "dinou/config", external: true };
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
],
|
|
54
64
|
});
|
|
55
65
|
loaded = await import(pathToFileURL(bundledPath).href + (isDev ? `?t=${Date.now()}` : ""));
|
|
56
66
|
} else if (p.endsWith(".mjs")) {
|
|
@@ -61,7 +61,7 @@ function generateRouteModulesCode(projectRoot = process.cwd(), relativeImportBas
|
|
|
61
61
|
lines.push(` ${JSON.stringify(file.relPath)}: () => import(${JSON.stringify(importPath)}),`);
|
|
62
62
|
|
|
63
63
|
// Detect PPR from page_functions or layout_functions ONLY
|
|
64
|
-
if (/(?:^|\/)(?:page_functions|layout_functions
|
|
64
|
+
if (/(?:^|\/)(?:page_functions|layout_functions)\.(?:[jt]sx?|mjs)$/.test(file.relPath)) {
|
|
65
65
|
try {
|
|
66
66
|
const content = fs.readFileSync(file.fullPath, "utf8");
|
|
67
67
|
const isPpr = parseExports.parsePprConfig ? parseExports.parsePprConfig(content) : null;
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import parser from "@babel/parser";
|
|
2
|
+
import traverseModule from "@babel/traverse";
|
|
3
|
+
import generateModule from "@babel/generator";
|
|
4
|
+
import * as t from "@babel/types";
|
|
5
|
+
import { useServerRegex } from "../constants.js";
|
|
6
|
+
import parseExports from "./parse-exports.js";
|
|
7
|
+
|
|
8
|
+
const traverse = traverseModule?.default || traverseModule;
|
|
9
|
+
const generate = generateModule?.default || generateModule;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Fast string check to determine if the source code could contain any "use server" directive.
|
|
13
|
+
* @param {string} code
|
|
14
|
+
* @returns {boolean}
|
|
15
|
+
*/
|
|
16
|
+
export function hasServerFunctions(code) {
|
|
17
|
+
if (typeof code !== "string") return false;
|
|
18
|
+
return code.includes("use server");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Check if the file starts with a module-level "use server" directive.
|
|
23
|
+
* @param {string} code
|
|
24
|
+
* @returns {boolean}
|
|
25
|
+
*/
|
|
26
|
+
export function isModuleLevelUseServer(code) {
|
|
27
|
+
if (!hasServerFunctions(code)) return false;
|
|
28
|
+
return useServerRegex.test(code.trim());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Checks if a function's block statement body contains a "use server" directive.
|
|
33
|
+
* @param {object} body
|
|
34
|
+
* @returns {boolean}
|
|
35
|
+
*/
|
|
36
|
+
function hasUseServerDirective(body) {
|
|
37
|
+
if (!body || body.type !== "BlockStatement") return false;
|
|
38
|
+
if (body.directives && body.directives.some((d) => d.value && d.value.value === "use server")) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
if (
|
|
42
|
+
body.body &&
|
|
43
|
+
body.body[0] &&
|
|
44
|
+
body.body[0].type === "ExpressionStatement" &&
|
|
45
|
+
body.body[0].expression &&
|
|
46
|
+
body.body[0].expression.type === "StringLiteral" &&
|
|
47
|
+
body.body[0].expression.value === "use server"
|
|
48
|
+
) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Helper to check if childScope is equal to or nested within parentScope.
|
|
56
|
+
*/
|
|
57
|
+
function isInsideScope(childScope, parentScope) {
|
|
58
|
+
let cur = childScope;
|
|
59
|
+
while (cur) {
|
|
60
|
+
if (cur === parentScope) return true;
|
|
61
|
+
cur = cur.parent;
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Transforms function-level "use server" directives:
|
|
68
|
+
* 1. Hoists inline/nested server functions to module top level and binds captured closure variables.
|
|
69
|
+
* 2. Ensures top-level functions with "use server" in their body are exported so they can be referenced and invoked.
|
|
70
|
+
* 3. Returns the transformed code and the list of exported server function names.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} code Source code
|
|
73
|
+
* @param {string} [filename="file.tsx"]
|
|
74
|
+
* @returns {{ transformed: boolean, isModuleLevel: boolean, code: string, actions: string[] }}
|
|
75
|
+
*/
|
|
76
|
+
export function transformServerFunctions(code, filename = "file.tsx") {
|
|
77
|
+
if (!hasServerFunctions(code)) {
|
|
78
|
+
return { transformed: false, isModuleLevel: false, code, actions: [] };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (isModuleLevelUseServer(code)) {
|
|
82
|
+
const exports = parseExports(code);
|
|
83
|
+
return {
|
|
84
|
+
transformed: false,
|
|
85
|
+
isModuleLevel: true,
|
|
86
|
+
code,
|
|
87
|
+
actions: exports,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let ast;
|
|
92
|
+
try {
|
|
93
|
+
ast = parser.parse(code, {
|
|
94
|
+
sourceType: "module",
|
|
95
|
+
plugins: [
|
|
96
|
+
"jsx",
|
|
97
|
+
"typescript",
|
|
98
|
+
"asyncGenerators",
|
|
99
|
+
"classProperties",
|
|
100
|
+
"classPrivateProperties",
|
|
101
|
+
"classPrivateMethods",
|
|
102
|
+
"dynamicImport",
|
|
103
|
+
"exportDefaultFrom",
|
|
104
|
+
"exportNamespaceFrom",
|
|
105
|
+
"nullishCoalescingOperator",
|
|
106
|
+
"numericSeparator",
|
|
107
|
+
"objectRestSpread",
|
|
108
|
+
"optionalCatchBinding",
|
|
109
|
+
"optionalChaining",
|
|
110
|
+
"topLevelAwait",
|
|
111
|
+
],
|
|
112
|
+
});
|
|
113
|
+
} catch (err) {
|
|
114
|
+
console.warn(`[transformServerFunctions] Babel parse error in ${filename}:`, err.message);
|
|
115
|
+
return { transformed: false, isModuleLevel: false, code, actions: [] };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const actions = [];
|
|
119
|
+
const hoistedNodes = [];
|
|
120
|
+
let actionCounter = 0;
|
|
121
|
+
|
|
122
|
+
traverse(ast, {
|
|
123
|
+
Function(path) {
|
|
124
|
+
const body = path.node.body;
|
|
125
|
+
if (!hasUseServerDirective(body)) return;
|
|
126
|
+
|
|
127
|
+
const isTopLevel = path.getFunctionParent() === null;
|
|
128
|
+
let fnName = path.node.id ? path.node.id.name : null;
|
|
129
|
+
if (!fnName) {
|
|
130
|
+
if (path.parentPath.isVariableDeclarator() && path.parentPath.node.id.type === "Identifier") {
|
|
131
|
+
fnName = path.parentPath.node.id.name;
|
|
132
|
+
} else if (path.parentPath.isJSXAttribute() && path.parentPath.node.name.type === "JSXIdentifier") {
|
|
133
|
+
fnName = path.parentPath.node.name.name;
|
|
134
|
+
} else {
|
|
135
|
+
fnName = "action";
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Helper to strip "use server" from function body
|
|
140
|
+
const cleanDirectivesAndStatements = () => {
|
|
141
|
+
body.body = body.body.filter((stmt) => {
|
|
142
|
+
return !(
|
|
143
|
+
stmt.type === "ExpressionStatement" &&
|
|
144
|
+
stmt.expression.type === "StringLiteral" &&
|
|
145
|
+
stmt.expression.value === "use server"
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
if (body.directives) {
|
|
149
|
+
body.directives = body.directives.filter(
|
|
150
|
+
(d) => !d.value || d.value.value !== "use server"
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
if (isTopLevel) {
|
|
156
|
+
cleanDirectivesAndStatements();
|
|
157
|
+
actions.push(fnName);
|
|
158
|
+
|
|
159
|
+
if (path.isFunctionDeclaration()) {
|
|
160
|
+
if (!path.parentPath.isExportNamedDeclaration()) {
|
|
161
|
+
path.replaceWith(t.exportNamedDeclaration(path.node));
|
|
162
|
+
}
|
|
163
|
+
} else if (path.parentPath.isVariableDeclarator()) {
|
|
164
|
+
const varDecl = path.parentPath.parentPath;
|
|
165
|
+
if (varDecl && varDecl.isVariableDeclaration() && !varDecl.parentPath.isExportNamedDeclaration()) {
|
|
166
|
+
varDecl.replaceWith(t.exportNamedDeclaration(varDecl.node));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
path.skip();
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Case B: Nested / Inline function capturing closure variables
|
|
175
|
+
const hoistedName = `$$dinou_action_${fnName}_${actionCounter++}`;
|
|
176
|
+
|
|
177
|
+
// Identify free / captured variables from outer scopes
|
|
178
|
+
const captured = new Set();
|
|
179
|
+
path.traverse({
|
|
180
|
+
Identifier(identPath) {
|
|
181
|
+
if (!identPath.isReferencedIdentifier()) return;
|
|
182
|
+
const name = identPath.node.name;
|
|
183
|
+
const binding = identPath.scope.getBinding(name);
|
|
184
|
+
if (binding) {
|
|
185
|
+
// Local variables declared inside the function or child blocks are not captured
|
|
186
|
+
if (isInsideScope(binding.scope, path.scope)) return;
|
|
187
|
+
// Variables declared at top-level Program scope (imports, module constants) are already accessible
|
|
188
|
+
if (binding.scope.block.type === "Program") return;
|
|
189
|
+
captured.add(name);
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const capturedVars = Array.from(captured);
|
|
195
|
+
const hoistedParams = [
|
|
196
|
+
...capturedVars.map((v) => t.identifier(v)),
|
|
197
|
+
...path.node.params,
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
// Clean the "use server" directive from the hoisted function body
|
|
201
|
+
const cleanBodyStatements = body.body.filter((stmt) => {
|
|
202
|
+
return !(
|
|
203
|
+
stmt.type === "ExpressionStatement" &&
|
|
204
|
+
stmt.expression.type === "StringLiteral" &&
|
|
205
|
+
stmt.expression.value === "use server"
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
const cleanDirectives = (body.directives || []).filter(
|
|
209
|
+
(d) => !d.value || d.value.value !== "use server"
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
const hoistedFn = t.exportNamedDeclaration(
|
|
213
|
+
t.functionDeclaration(
|
|
214
|
+
t.identifier(hoistedName),
|
|
215
|
+
hoistedParams,
|
|
216
|
+
t.blockStatement(cleanBodyStatements, cleanDirectives),
|
|
217
|
+
path.node.generator,
|
|
218
|
+
true /* async */
|
|
219
|
+
)
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
hoistedNodes.push(hoistedFn);
|
|
223
|
+
actions.push(hoistedName);
|
|
224
|
+
|
|
225
|
+
// Replace the inline function with a .bind() call attaching captured variables
|
|
226
|
+
const bindCall = t.callExpression(
|
|
227
|
+
t.memberExpression(t.identifier(hoistedName), t.identifier("bind")),
|
|
228
|
+
[t.nullLiteral(), ...capturedVars.map((v) => t.identifier(v))]
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
if (path.isFunctionDeclaration()) {
|
|
232
|
+
path.replaceWith(
|
|
233
|
+
t.variableDeclaration("const", [
|
|
234
|
+
t.variableDeclarator(t.identifier(fnName), bindCall),
|
|
235
|
+
])
|
|
236
|
+
);
|
|
237
|
+
} else {
|
|
238
|
+
path.replaceWith(bindCall);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
path.skip();
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
if (actions.length === 0) {
|
|
246
|
+
return { transformed: false, isModuleLevel: false, code, actions: [] };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Insert hoisted function declarations right after the last import statement in the program
|
|
250
|
+
const program = ast.program;
|
|
251
|
+
let lastImportIdx = -1;
|
|
252
|
+
for (let i = 0; i < program.body.length; i++) {
|
|
253
|
+
if (program.body[i].type === "ImportDeclaration") {
|
|
254
|
+
lastImportIdx = i;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (hoistedNodes.length > 0) {
|
|
259
|
+
program.body.splice(lastImportIdx + 1, 0, ...hoistedNodes);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const output = generate(ast, { retainLines: false });
|
|
263
|
+
return {
|
|
264
|
+
transformed: true,
|
|
265
|
+
isModuleLevel: false,
|
|
266
|
+
code: output.code,
|
|
267
|
+
actions,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Extracts action export names from code (handles both module-level and function-level).
|
|
273
|
+
* @param {string} code
|
|
274
|
+
* @param {string} [filename="file.tsx"]
|
|
275
|
+
* @returns {string[]}
|
|
276
|
+
*/
|
|
277
|
+
export function extractServerFunctions(code, filename = "file.tsx") {
|
|
278
|
+
if (!hasServerFunctions(code)) return [];
|
|
279
|
+
if (isModuleLevelUseServer(code)) {
|
|
280
|
+
return parseExports(code);
|
|
281
|
+
}
|
|
282
|
+
const result = transformServerFunctions(code, filename);
|
|
283
|
+
return result.actions || [];
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export default {
|
|
287
|
+
hasServerFunctions,
|
|
288
|
+
isModuleLevelUseServer,
|
|
289
|
+
transformServerFunctions,
|
|
290
|
+
extractServerFunctions,
|
|
291
|
+
};
|
package/dinou/deno/build.js
CHANGED
|
@@ -12,6 +12,7 @@ import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
12
12
|
import { generateRouteModulesCode, generateRouteTypes } from "../core/route-generator.js";
|
|
13
13
|
import parseExports from "../core/parse-exports.js";
|
|
14
14
|
import { useClientRegex, useServerRegex } from "../constants.js";
|
|
15
|
+
import { transformServerFunctions, hasServerFunctions } from "../core/transform-server-functions.js";
|
|
15
16
|
import {
|
|
16
17
|
isSupportedClientModule,
|
|
17
18
|
scanProjectDependenciesForClientComponents,
|
|
@@ -965,14 +966,18 @@ const serverReferencesPlugin = {
|
|
|
965
966
|
return null;
|
|
966
967
|
}
|
|
967
968
|
|
|
968
|
-
if (!
|
|
969
|
+
if (!hasServerFunctions(code)) return null;
|
|
969
970
|
|
|
970
|
-
const
|
|
971
|
+
const sfResult = transformServerFunctions(code, args.path);
|
|
972
|
+
if (!sfResult.actions || sfResult.actions.length === 0) return null;
|
|
973
|
+
|
|
974
|
+
const transformedCode = sfResult.code;
|
|
975
|
+
const exports = sfResult.actions;
|
|
971
976
|
const absPath = path.resolve(args.path);
|
|
972
977
|
const relPath = path.relative(projectRoot, absPath).replace(/\\/g, "/");
|
|
973
978
|
const relativeFileUrl = "file:///" + relPath;
|
|
974
979
|
|
|
975
|
-
let transformed =
|
|
980
|
+
let transformed = transformedCode + "\n\n";
|
|
976
981
|
transformed += `import { registerServerReference } from "react-server-dom-webpack/server.edge";\n`;
|
|
977
982
|
|
|
978
983
|
for (const name of exports) {
|
|
@@ -1006,14 +1011,18 @@ const serverReferencesPluginSsr = {
|
|
|
1006
1011
|
return null;
|
|
1007
1012
|
}
|
|
1008
1013
|
|
|
1009
|
-
if (!
|
|
1014
|
+
if (!hasServerFunctions(code)) return null;
|
|
1010
1015
|
|
|
1011
|
-
const
|
|
1016
|
+
const sfResult = transformServerFunctions(code, args.path);
|
|
1017
|
+
if (!sfResult.actions || sfResult.actions.length === 0) return null;
|
|
1018
|
+
|
|
1019
|
+
const transformedCode = sfResult.code;
|
|
1020
|
+
const exports = sfResult.actions;
|
|
1012
1021
|
const absPath = path.resolve(args.path);
|
|
1013
1022
|
const relPath = path.relative(projectRoot, absPath).replace(/\\/g, "/");
|
|
1014
1023
|
const relativeFileUrl = "file:///" + relPath;
|
|
1015
1024
|
|
|
1016
|
-
let transformed =
|
|
1025
|
+
let transformed = transformedCode + "\n\n";
|
|
1017
1026
|
transformed += `import { registerServerReference } from "react-server-dom-webpack/client.edge";\n`;
|
|
1018
1027
|
|
|
1019
1028
|
for (const name of exports) {
|
package/dinou/esbuild/build.js
CHANGED
|
@@ -46,8 +46,12 @@ const frameworkEntryPoints = {
|
|
|
46
46
|
try {
|
|
47
47
|
const manifest = {};
|
|
48
48
|
|
|
49
|
-
const [
|
|
50
|
-
|
|
49
|
+
const [
|
|
50
|
+
esbuildEntries,
|
|
51
|
+
detectedCSSEntries,
|
|
52
|
+
detectedAssetEntries,
|
|
53
|
+
serverFiles,
|
|
54
|
+
] = await getEsbuildEntries({ manifest });
|
|
51
55
|
|
|
52
56
|
updateManifestForModule(
|
|
53
57
|
absPathToClientRedirect,
|
|
@@ -95,6 +99,7 @@ try {
|
|
|
95
99
|
entryPoints,
|
|
96
100
|
manifest,
|
|
97
101
|
outdir,
|
|
102
|
+
serverFiles,
|
|
98
103
|
})
|
|
99
104
|
);
|
|
100
105
|
|
|
@@ -9,13 +9,13 @@ import writePlugin from "../plugins-esbuild/write-plugin.js";
|
|
|
9
9
|
import babelReactCompilerPlugin from "../plugins-esbuild/babel-react-compiler-plugin.js";
|
|
10
10
|
import { existsSync } from "node:fs";
|
|
11
11
|
|
|
12
|
-
const manifestData = {};
|
|
13
|
-
|
|
14
12
|
export default function getConfigEsbuildProd({
|
|
15
13
|
entryPoints,
|
|
16
14
|
outdir = ".dinou/dist3",
|
|
17
15
|
manifest = {},
|
|
16
|
+
serverFiles,
|
|
18
17
|
}) {
|
|
18
|
+
const manifestData = {};
|
|
19
19
|
let plugins = [
|
|
20
20
|
babelReactCompilerPlugin(),
|
|
21
21
|
TsconfigPathsPlugin({}),
|
|
@@ -26,7 +26,7 @@ export default function getConfigEsbuildProd({
|
|
|
26
26
|
}),
|
|
27
27
|
assetsPlugin(),
|
|
28
28
|
manifestGeneratorPlugin(manifestData),
|
|
29
|
-
serverFunctionsPlugin(manifestData),
|
|
29
|
+
serverFunctionsPlugin({ manifestData, serverFiles }),
|
|
30
30
|
writePlugin(),
|
|
31
31
|
];
|
|
32
32
|
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
import { getAbsPathWithExt } from "../../core/get-abs-path-with-ext.js";
|
|
13
13
|
import normalizePath from "./normalize-path.js";
|
|
14
14
|
import { useClientRegex, useServerRegex } from "../../constants.js";
|
|
15
|
+
import { hasServerFunctions } from "../../core/transform-server-functions.js";
|
|
15
16
|
import { updateManifestForModule } from "./update-manifest-for-module.js";
|
|
16
17
|
|
|
17
18
|
function hashFilePath(absPath) {
|
|
@@ -95,6 +96,8 @@ export default async function getEsbuildEntries({
|
|
|
95
96
|
if (isImportedFileServer) {
|
|
96
97
|
serverFunctions.add(normalizePath(absImportPathWithExt));
|
|
97
98
|
continue;
|
|
99
|
+
} else if (hasServerFunctions(importedCode)) {
|
|
100
|
+
serverFunctions.add(normalizePath(absImportPathWithExt));
|
|
98
101
|
}
|
|
99
102
|
} else {
|
|
100
103
|
const isImportedFileServer = useServerRegex.test(importedCode.trim());
|
|
@@ -102,6 +105,8 @@ export default async function getEsbuildEntries({
|
|
|
102
105
|
if (isImportedFileServer) {
|
|
103
106
|
serverFunctions.add(normalizePath(absImportPathWithExt));
|
|
104
107
|
continue;
|
|
108
|
+
} else if (hasServerFunctions(importedCode)) {
|
|
109
|
+
serverFunctions.add(normalizePath(absImportPathWithExt));
|
|
105
110
|
}
|
|
106
111
|
}
|
|
107
112
|
|
|
@@ -156,7 +161,12 @@ export default async function getEsbuildEntries({
|
|
|
156
161
|
|
|
157
162
|
function isPageOrLayout(absPath) {
|
|
158
163
|
const fileName = path.basename(absPath);
|
|
159
|
-
return
|
|
164
|
+
return (
|
|
165
|
+
fileName.startsWith("page.") ||
|
|
166
|
+
fileName.startsWith("layout.") ||
|
|
167
|
+
fileName.startsWith("page_functions.") ||
|
|
168
|
+
fileName.startsWith("layout_functions.")
|
|
169
|
+
);
|
|
160
170
|
}
|
|
161
171
|
|
|
162
172
|
|
|
@@ -173,13 +183,8 @@ export default async function getEsbuildEntries({
|
|
|
173
183
|
}
|
|
174
184
|
const code = readFileSync(absPath, "utf8");
|
|
175
185
|
const isClientModule = useClientRegex.test(code.trim());
|
|
176
|
-
const isServerFunction = useServerRegex.test(code.trim());
|
|
177
186
|
const normalizedPath = normalizePath(absPath);
|
|
178
187
|
|
|
179
|
-
if (isServerFunction) {
|
|
180
|
-
serverFunctions.add(normalizedPath);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
188
|
if (isClientModule) {
|
|
184
189
|
const name = path.basename(absPath, path.extname(absPath));
|
|
185
190
|
|
|
@@ -188,8 +193,14 @@ export default async function getEsbuildEntries({
|
|
|
188
193
|
absPath: normalizedPath,
|
|
189
194
|
name,
|
|
190
195
|
});
|
|
196
|
+
try {
|
|
197
|
+
await getImportsAndAssetsAndCsss(code, absPath, new Set(), true);
|
|
198
|
+
} catch (err) {}
|
|
191
199
|
} else if (isPageOrLayout(absPath)) {
|
|
192
200
|
serverModules.add(normalizedPath);
|
|
201
|
+
if (hasServerFunctions(code)) {
|
|
202
|
+
serverFunctions.add(normalizedPath);
|
|
203
|
+
}
|
|
193
204
|
try {
|
|
194
205
|
const { imports, assets, csss } = await getImportsAndAssetsAndCsss(
|
|
195
206
|
code,
|