dinou 7.1.0 → 7.2.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 +11 -0
- package/README.md +1 -1
- package/cli.js +85 -0
- package/dinou/adapters/netlify.js +3 -15
- package/dinou/cloudflare/build.mjs +1 -0
- package/dinou/core/load-dinou-config.js +35 -3
- package/dinou/core/server-function-proxy-webpack.js +20 -1
- package/dinou/core/server-function-proxy.js +20 -1
- package/dinou/index.d.ts +21 -0
- package/dinou/node/build.mjs +45 -2
- package/dinou/node/dev.mjs +48 -1
- package/dinou/package.json +1 -1
- package/eject.js +45 -1
- package/package.json +1 -1
- package/dinou/core/babel-esm-loader.js +0 -267
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,17 @@ 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
|
+
## [7.2.0]
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- **Plugin Hook `configureServer`**: Attach WebSockets (Socket.IO, WS) and native Node.js HTTP middlewares before server startup without ejecting.
|
|
12
|
+
- **Node Standalone Binary Compilation**: Added `dinou build:node:compile` and cross-compilation matrix (`:linux`, `:linux_arm`, `:mac`, `:win`).
|
|
13
|
+
- **Client-Side Server Function Auth Hooks**: Added `window.__DINOU_SERVER_FUNCTION_HEADERS__` and `window.__DINOU_GET_AUTH_TOKEN__` to inject dynamic JWT/Bearer authorization headers into `"use server"` calls.
|
|
14
|
+
- **Esbuild-Powered Config Loader**: `dinou.config.*` is automatically pre-bundled with Esbuild, enabling native `tsconfig.json` path aliases (`@/*`) and TypeScript configuration files out of the box.
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
- Removed legacy `babel-esm-loader.js` and cleaned up residual loader registrations.
|
|
18
|
+
|
|
8
19
|
## [7.1.0] - 2026-10-16
|
|
9
20
|
|
|
10
21
|
### Added
|
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
|
|
package/cli.js
CHANGED
|
@@ -121,6 +121,91 @@ program
|
|
|
121
121
|
runCommand(`cross-env NODE_ENV=production DINOU_BUILD_TOOL=webpack node ${p("adapters/node.mjs")}`);
|
|
122
122
|
});
|
|
123
123
|
|
|
124
|
+
// --- NODE (COMPILE VIA BUN) ---
|
|
125
|
+
|
|
126
|
+
const nodeTargets = [
|
|
127
|
+
{ name: "", flags: "", outfile: "dist/server" },
|
|
128
|
+
{ name: ":linux", flags: "--target=bun-linux-x64 ", outfile: "dist/server-linux" },
|
|
129
|
+
{ name: ":linux_arm", flags: "--target=bun-linux-arm64 ", outfile: "dist/server-linux-arm" },
|
|
130
|
+
{ name: ":mac", flags: "--target=bun-darwin-arm64 ", outfile: "dist/server-mac" },
|
|
131
|
+
{ name: ":win", flags: "--target=bun-windows-x64 ", outfile: "dist/server-win" },
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
const runNodeBuild = (bundler) => {
|
|
135
|
+
const bundlerCmd = bundler === "rollup"
|
|
136
|
+
? `cross-env NODE_ENV=production npx rollup -c ${p("rollup/rollup.config.js")}`
|
|
137
|
+
: bundler === "webpack"
|
|
138
|
+
? `cross-env NODE_ENV=production npx webpack --config ${p("webpack/webpack.config.js")}`
|
|
139
|
+
: `cross-env NODE_ENV=production node ${p("esbuild/build.mjs")}`;
|
|
140
|
+
|
|
141
|
+
const envPrefix = bundler === "webpack" ? "cross-env DINOU_BUILD_TOOL=webpack " : "";
|
|
142
|
+
return `${bundlerCmd} && ${envPrefix}node ${p("node/build.mjs")}`;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
for (const t of nodeTargets) {
|
|
146
|
+
for (const b of ["esbuild", "rollup", "webpack"]) {
|
|
147
|
+
const cmdNames = b === "esbuild"
|
|
148
|
+
? [`build:node:compile${t.name}`, `build:node:compile${t.name}:esbuild`]
|
|
149
|
+
: [`build:node:compile${t.name}:${b}`];
|
|
150
|
+
for (const cmdName of cmdNames) {
|
|
151
|
+
program
|
|
152
|
+
.command(cmdName)
|
|
153
|
+
.description(`Compile standalone Node.js server executable with Bun (${t.name || "host"}, ${b})`)
|
|
154
|
+
.action(() => {
|
|
155
|
+
runCommand(`${runNodeBuild(b)} && bun build --compile ${t.flags}.dinou/node/server.mjs --outfile ${t.outfile}`);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// --- NODE (PURE BUN RUNNER COMPILE) ---
|
|
162
|
+
|
|
163
|
+
const runNodePureBuild = (bundler) => {
|
|
164
|
+
if (bundler === "rollup") {
|
|
165
|
+
return `cross-env NODE_ENV=production npx rollup -c ${p("rollup/rollup.config.js")} && bun ${p("node/build.mjs")}`;
|
|
166
|
+
}
|
|
167
|
+
if (bundler === "webpack") {
|
|
168
|
+
return `cross-env NODE_ENV=production npx webpack --config ${p("webpack/webpack.config.js")} && cross-env DINOU_BUILD_TOOL=webpack bun ${p("node/build.mjs")}`;
|
|
169
|
+
}
|
|
170
|
+
return `cross-env NODE_ENV=production bun ${p("esbuild/build.mjs")} && bun ${p("node/build.mjs")}`;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
program
|
|
174
|
+
.command("build:node:pure")
|
|
175
|
+
.description("Build the app for Node.js with 100% Bun (using esbuild)")
|
|
176
|
+
.action(() => { runCommand(runNodePureBuild("esbuild")); });
|
|
177
|
+
|
|
178
|
+
program
|
|
179
|
+
.command("build:node:pure:esbuild")
|
|
180
|
+
.description("Build the app for Node.js with 100% Bun (Esbuild)")
|
|
181
|
+
.action(() => { runCommand(runNodePureBuild("esbuild")); });
|
|
182
|
+
|
|
183
|
+
program
|
|
184
|
+
.command("build:node:pure:rollup")
|
|
185
|
+
.description("Build the app for Node.js with 100% Bun (Rollup)")
|
|
186
|
+
.action(() => { runCommand(runNodePureBuild("rollup")); });
|
|
187
|
+
|
|
188
|
+
program
|
|
189
|
+
.command("build:node:pure:webpack")
|
|
190
|
+
.description("Build the app for Node.js with 100% Bun (Webpack)")
|
|
191
|
+
.action(() => { runCommand(runNodePureBuild("webpack")); });
|
|
192
|
+
|
|
193
|
+
for (const t of nodeTargets) {
|
|
194
|
+
for (const b of ["esbuild", "rollup", "webpack"]) {
|
|
195
|
+
const cmdNames = b === "esbuild"
|
|
196
|
+
? [`build:node:pure:compile${t.name}`, `build:node:pure:compile${t.name}:esbuild`]
|
|
197
|
+
: [`build:node:pure:compile${t.name}:${b}`];
|
|
198
|
+
for (const cmdName of cmdNames) {
|
|
199
|
+
program
|
|
200
|
+
.command(cmdName)
|
|
201
|
+
.description(`Compile standalone Node.js server executable with 100% Bun (${t.name || "host"}, ${b})`)
|
|
202
|
+
.action(() => {
|
|
203
|
+
runCommand(`${runNodePureBuild(b)} && bun build --compile ${t.flags}.dinou/node/server.mjs --outfile ${t.outfile}`);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
124
209
|
// --- CLOUDFLARE WORKERS ---
|
|
125
210
|
|
|
126
211
|
program
|
|
@@ -3,21 +3,9 @@
|
|
|
3
3
|
// Automatically routes all requests to Dinou's universal Web Standards handler.
|
|
4
4
|
Object.assign(process.env, { NODE_ENV: "production" });
|
|
5
5
|
|
|
6
|
-
import { register } from "node:module";
|
|
7
|
-
import { pathToFileURL } from "node:url";
|
|
8
6
|
import path from "node:path";
|
|
9
7
|
import Module from "node:module";
|
|
10
8
|
|
|
11
|
-
import { dinouCoreDir } from "../core/dinou-paths.js";
|
|
12
|
-
|
|
13
|
-
if (typeof register === "function") {
|
|
14
|
-
try {
|
|
15
|
-
const loaderPath = path.join(dinouCoreDir, "babel-esm-loader.js");
|
|
16
|
-
register(pathToFileURL(loaderPath).href, pathToFileURL("./"));
|
|
17
|
-
} catch (e) {
|
|
18
|
-
console.error("[Dinou Adapter] Loader registration failed:", e);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
9
|
|
|
22
10
|
let reactServerPath, reactDomServerPath, reactJsxRuntimePath, reactJsxDevRuntimePath;
|
|
23
11
|
let roggcServerNodePath, webpackServerNodePath;
|
|
@@ -30,17 +18,17 @@ try {
|
|
|
30
18
|
|
|
31
19
|
const reactDomPkgJson = require.resolve("react-dom/package.json");
|
|
32
20
|
reactDomServerPath = path.join(path.dirname(reactDomPkgJson), "react-dom.react-server.js");
|
|
33
|
-
} catch (e) {}
|
|
21
|
+
} catch (e) { }
|
|
34
22
|
|
|
35
23
|
try {
|
|
36
24
|
const roggcPkgJson = require.resolve("@roggc/react-server-dom-esm/package.json");
|
|
37
25
|
roggcServerNodePath = path.join(path.dirname(roggcPkgJson), "server.node.js");
|
|
38
|
-
} catch (e) {}
|
|
26
|
+
} catch (e) { }
|
|
39
27
|
|
|
40
28
|
try {
|
|
41
29
|
const webpackPkgJson = require.resolve("react-server-dom-webpack/package.json");
|
|
42
30
|
webpackServerNodePath = path.join(path.dirname(webpackPkgJson), "server.node.js");
|
|
43
|
-
} catch (e) {}
|
|
31
|
+
} catch (e) { }
|
|
44
32
|
|
|
45
33
|
if (Module && Module._resolveFilename) {
|
|
46
34
|
const originalResolveFilename = Module._resolveFilename;
|
|
@@ -819,6 +819,7 @@ const commonAlias = {
|
|
|
819
819
|
"@": path.resolve(projectRoot, "src"),
|
|
820
820
|
"dinou/config": path.resolve(dinouDir, "core/config.js"),
|
|
821
821
|
dinou: dinouDir,
|
|
822
|
+
esbuild: path.resolve(shimsDir, "empty.js"),
|
|
822
823
|
fs: path.resolve(shimsDir, "fs.js"),
|
|
823
824
|
"node:fs": path.resolve(shimsDir, "fs.js"),
|
|
824
825
|
"fs/promises": path.resolve(shimsDir, "fs.js"),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const path = require("path");
|
|
2
|
-
const { existsSync } = require("fs");
|
|
2
|
+
const { existsSync, mkdirSync } = require("fs");
|
|
3
3
|
const { pathToFileURL } = require("url");
|
|
4
4
|
|
|
5
5
|
let dinouConfig = { plugins: [] };
|
|
@@ -12,13 +12,45 @@ async function getDinouConfig() {
|
|
|
12
12
|
if (dinouConfigPromise) return dinouConfigPromise;
|
|
13
13
|
dinouConfigPromise = (async () => {
|
|
14
14
|
const cwd = typeof process !== "undefined" && typeof process.cwd === "function" ? process.cwd() : ".";
|
|
15
|
-
for (const filename of ["dinou.config.
|
|
15
|
+
for (const filename of ["dinou.config.ts", "dinou.config.mjs", "dinou.config.js", "dinou.config.cjs"]) {
|
|
16
16
|
const p = path.resolve(cwd, filename);
|
|
17
17
|
if (existsSync(p)) {
|
|
18
18
|
try {
|
|
19
19
|
let loaded;
|
|
20
20
|
const isDev = process.env.NODE_ENV !== "production";
|
|
21
|
-
|
|
21
|
+
let esbuild;
|
|
22
|
+
if (
|
|
23
|
+
typeof process !== "undefined" &&
|
|
24
|
+
process.versions &&
|
|
25
|
+
process.versions.node &&
|
|
26
|
+
process.env.DINOU_RUNTIME !== "edge"
|
|
27
|
+
) {
|
|
28
|
+
try {
|
|
29
|
+
const req = typeof require === "function" ? require : null;
|
|
30
|
+
const esbuildPkg = "esbuild";
|
|
31
|
+
if (req) {
|
|
32
|
+
esbuild = req(esbuildPkg);
|
|
33
|
+
}
|
|
34
|
+
} catch (e) {}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (esbuild && typeof esbuild.build === "function") {
|
|
38
|
+
const outDir = path.resolve(cwd, ".dinou");
|
|
39
|
+
if (!existsSync(outDir)) {
|
|
40
|
+
mkdirSync(outDir, { recursive: true });
|
|
41
|
+
}
|
|
42
|
+
const bundledPath = path.resolve(outDir, "dinou.config.bundled.mjs");
|
|
43
|
+
await esbuild.build({
|
|
44
|
+
entryPoints: [p],
|
|
45
|
+
outfile: bundledPath,
|
|
46
|
+
bundle: true,
|
|
47
|
+
format: "esm",
|
|
48
|
+
platform: "node",
|
|
49
|
+
packages: "external",
|
|
50
|
+
sourcemap: "inline",
|
|
51
|
+
});
|
|
52
|
+
loaded = await import(pathToFileURL(bundledPath).href + (isDev ? `?t=${Date.now()}` : ""));
|
|
53
|
+
} else if (p.endsWith(".mjs")) {
|
|
22
54
|
loaded = await import(pathToFileURL(p).href + (isDev ? `?t=${Date.now()}` : ""));
|
|
23
55
|
} else {
|
|
24
56
|
const nodeReq =
|
|
@@ -17,14 +17,33 @@ function executeRedirect(url) {
|
|
|
17
17
|
function createServerFunctionProxy(id) {
|
|
18
18
|
return new Proxy(() => { }, {
|
|
19
19
|
apply: async (_target, _thisArg, args) => {
|
|
20
|
-
let
|
|
20
|
+
let customHeaders = {};
|
|
21
|
+
if (typeof window !== "undefined") {
|
|
22
|
+
if (typeof window.__DINOU_SERVER_FUNCTION_HEADERS__ === "function") {
|
|
23
|
+
try {
|
|
24
|
+
customHeaders = window.__DINOU_SERVER_FUNCTION_HEADERS__() || {};
|
|
25
|
+
} catch (e) {
|
|
26
|
+
console.error("[Dinou] Error getting custom headers for server function:", e);
|
|
27
|
+
}
|
|
28
|
+
} else if (typeof window.__DINOU_GET_AUTH_TOKEN__ === "function") {
|
|
29
|
+
try {
|
|
30
|
+
const token = window.__DINOU_GET_AUTH_TOKEN__();
|
|
31
|
+
if (token) customHeaders = { Authorization: `Bearer ${token}` };
|
|
32
|
+
} catch (e) {
|
|
33
|
+
console.error("[Dinou] Error getting auth token for server function:", e);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
21
38
|
const headers = {
|
|
22
39
|
"x-server-function-call": "1",
|
|
40
|
+
...customHeaders,
|
|
23
41
|
};
|
|
24
42
|
if (typeof window !== "undefined" && window.location) {
|
|
25
43
|
headers["x-dinou-current-path"] = window.location.pathname;
|
|
26
44
|
}
|
|
27
45
|
|
|
46
|
+
let body;
|
|
28
47
|
const formDataIndex = args.findIndex((arg) => arg instanceof FormData);
|
|
29
48
|
|
|
30
49
|
if (formDataIndex !== -1) {
|
|
@@ -17,14 +17,33 @@ function executeRedirect(url) {
|
|
|
17
17
|
export function createServerFunctionProxy(id) {
|
|
18
18
|
return new Proxy(() => { }, {
|
|
19
19
|
apply: async (_target, _thisArg, args) => {
|
|
20
|
-
let
|
|
20
|
+
let customHeaders = {};
|
|
21
|
+
if (typeof window !== "undefined") {
|
|
22
|
+
if (typeof window.__DINOU_SERVER_FUNCTION_HEADERS__ === "function") {
|
|
23
|
+
try {
|
|
24
|
+
customHeaders = window.__DINOU_SERVER_FUNCTION_HEADERS__() || {};
|
|
25
|
+
} catch (e) {
|
|
26
|
+
console.error("[Dinou] Error getting custom headers for server function:", e);
|
|
27
|
+
}
|
|
28
|
+
} else if (typeof window.__DINOU_GET_AUTH_TOKEN__ === "function") {
|
|
29
|
+
try {
|
|
30
|
+
const token = window.__DINOU_GET_AUTH_TOKEN__();
|
|
31
|
+
if (token) customHeaders = { Authorization: `Bearer ${token}` };
|
|
32
|
+
} catch (e) {
|
|
33
|
+
console.error("[Dinou] Error getting auth token for server function:", e);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
21
38
|
const headers = {
|
|
22
39
|
"x-server-function-call": "1",
|
|
40
|
+
...customHeaders,
|
|
23
41
|
};
|
|
24
42
|
if (typeof window !== "undefined" && window.location) {
|
|
25
43
|
headers["x-dinou-current-path"] = window.location.pathname;
|
|
26
44
|
}
|
|
27
45
|
|
|
46
|
+
let body;
|
|
28
47
|
const formDataIndex = args.findIndex((arg) => arg instanceof FormData);
|
|
29
48
|
|
|
30
49
|
if (formDataIndex !== -1) {
|
package/dinou/index.d.ts
CHANGED
|
@@ -120,8 +120,29 @@ export interface RequestContextStore {
|
|
|
120
120
|
// DINOU CONFIGURATION & PLUGIN INTERFACES
|
|
121
121
|
// ====================================================================
|
|
122
122
|
|
|
123
|
+
export type NodeMiddleware = (
|
|
124
|
+
req: import("node:http").IncomingMessage,
|
|
125
|
+
res: import("node:http").ServerResponse,
|
|
126
|
+
next: () => void
|
|
127
|
+
) => void | Promise<void>;
|
|
128
|
+
|
|
129
|
+
export interface ServerHookContext {
|
|
130
|
+
/** The underlying Node.js http.Server instance */
|
|
131
|
+
server: import("node:http").Server;
|
|
132
|
+
/** Whether the server is running in development mode */
|
|
133
|
+
isDev: boolean;
|
|
134
|
+
/** Register a raw Node.js middleware (req, res, next) executed before Dinou routes */
|
|
135
|
+
use(middleware: NodeMiddleware): void;
|
|
136
|
+
}
|
|
137
|
+
|
|
123
138
|
export interface DinouPlugin {
|
|
124
139
|
name?: string;
|
|
140
|
+
/**
|
|
141
|
+
* Hook invoked when the Node.js HTTP server is instantiated,
|
|
142
|
+
* allowing plugins to attach WebSockets (Socket.IO, ws), listeners, or raw Node middlewares.
|
|
143
|
+
*/
|
|
144
|
+
configureServer?(context: ServerHookContext): Promise<void> | void;
|
|
145
|
+
|
|
125
146
|
/**
|
|
126
147
|
* Universal Web Standards middleware and webhook handler.
|
|
127
148
|
* Runs on every incoming HTTP request before React rendering.
|
package/dinou/node/build.mjs
CHANGED
|
@@ -119,12 +119,32 @@ export async function fetch(req) {
|
|
|
119
119
|
|
|
120
120
|
const PORT = Number(process.env.PORT || 3000);
|
|
121
121
|
|
|
122
|
-
|
|
122
|
+
const nodeMiddlewares = [];
|
|
123
|
+
|
|
124
|
+
export async function startServer(port = PORT, host = "0.0.0.0") {
|
|
123
125
|
const server = http.createServer(async (req, res) => {
|
|
124
126
|
try {
|
|
127
|
+
if (res.writableEnded || res.headersSent) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (nodeMiddlewares.length > 0) {
|
|
132
|
+
for (const mw of nodeMiddlewares) {
|
|
133
|
+
let continued = false;
|
|
134
|
+
await mw(req, res, () => {
|
|
135
|
+
continued = true;
|
|
136
|
+
});
|
|
137
|
+
if (!continued || res.writableEnded || res.headersSent) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
125
143
|
const webReq = nodeToWebRequest(req);
|
|
126
144
|
const webRes = await fetch(webReq);
|
|
127
|
-
|
|
145
|
+
if (!res.writableEnded && !res.headersSent) {
|
|
146
|
+
await sendWebResponseToNode(webRes, res);
|
|
147
|
+
}
|
|
128
148
|
} catch (err) {
|
|
129
149
|
console.error("[Dinou Node Server] Request error:", err);
|
|
130
150
|
if (!res.headersSent) {
|
|
@@ -135,6 +155,29 @@ export function startServer(port = PORT, host = "0.0.0.0") {
|
|
|
135
155
|
}
|
|
136
156
|
});
|
|
137
157
|
|
|
158
|
+
try {
|
|
159
|
+
const { getDinouConfig } = await import("${dinouDirSlash}/core/load-dinou-config.js");
|
|
160
|
+
const cfg = await getDinouConfig();
|
|
161
|
+
if (cfg.plugins && Array.isArray(cfg.plugins)) {
|
|
162
|
+
const serverContext = {
|
|
163
|
+
server,
|
|
164
|
+
isDev: false,
|
|
165
|
+
use(middleware) {
|
|
166
|
+
if (typeof middleware === "function") {
|
|
167
|
+
nodeMiddlewares.push(middleware);
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
for (const plugin of cfg.plugins) {
|
|
172
|
+
if (typeof plugin.configureServer === "function") {
|
|
173
|
+
await plugin.configureServer(serverContext);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
} catch (err) {
|
|
178
|
+
console.error("❌ [Dinou Node Server] Error running configureServer plugins:", err);
|
|
179
|
+
}
|
|
180
|
+
|
|
138
181
|
return new Promise((resolve, reject) => {
|
|
139
182
|
server.on("error", reject);
|
|
140
183
|
server.listen(port, () => {
|
package/dinou/node/dev.mjs
CHANGED
|
@@ -2302,9 +2302,52 @@ async function startClientBundler(tool) {
|
|
|
2302
2302
|
throw new Error(`Unsupported DINOU_BUILD_TOOL: ${tool}`);
|
|
2303
2303
|
}
|
|
2304
2304
|
|
|
2305
|
+
// Node.js Middlewares registered by plugins via configureServer
|
|
2306
|
+
const nodeMiddlewares = [];
|
|
2307
|
+
|
|
2308
|
+
async function applyServerPlugins(serverInstance) {
|
|
2309
|
+
try {
|
|
2310
|
+
const { getDinouConfig } = require(path.join(dinouDir, "core/load-dinou-config.js"));
|
|
2311
|
+
const config = await getDinouConfig();
|
|
2312
|
+
if (config.plugins && Array.isArray(config.plugins)) {
|
|
2313
|
+
const serverContext = {
|
|
2314
|
+
server: serverInstance,
|
|
2315
|
+
isDev: true,
|
|
2316
|
+
use(middleware) {
|
|
2317
|
+
if (typeof middleware === "function") {
|
|
2318
|
+
nodeMiddlewares.push(middleware);
|
|
2319
|
+
}
|
|
2320
|
+
},
|
|
2321
|
+
};
|
|
2322
|
+
for (const plugin of config.plugins) {
|
|
2323
|
+
if (typeof plugin.configureServer === "function") {
|
|
2324
|
+
await plugin.configureServer(serverContext);
|
|
2325
|
+
}
|
|
2326
|
+
}
|
|
2327
|
+
}
|
|
2328
|
+
} catch (err) {
|
|
2329
|
+
console.error("❌ [Dinou Dev] Error applying plugin configureServer hooks:", err);
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
|
|
2305
2333
|
// HTTP Server
|
|
2306
2334
|
const server = http.createServer(async (req, res) => {
|
|
2307
2335
|
try {
|
|
2336
|
+
if (res.writableEnded || res.headersSent) {
|
|
2337
|
+
return;
|
|
2338
|
+
}
|
|
2339
|
+
|
|
2340
|
+
if (nodeMiddlewares.length > 0) {
|
|
2341
|
+
for (const mw of nodeMiddlewares) {
|
|
2342
|
+
let continued = false;
|
|
2343
|
+
await mw(req, res, () => {
|
|
2344
|
+
continued = true;
|
|
2345
|
+
});
|
|
2346
|
+
if (!continued || res.writableEnded || res.headersSent) {
|
|
2347
|
+
return;
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2308
2351
|
if (clientBundlerPromise) {
|
|
2309
2352
|
await clientBundlerPromise;
|
|
2310
2353
|
}
|
|
@@ -2493,7 +2536,9 @@ const server = http.createServer(async (req, res) => {
|
|
|
2493
2536
|
console.log(`[DEV 404] ${req.method} ${pathname}`);
|
|
2494
2537
|
}
|
|
2495
2538
|
|
|
2496
|
-
|
|
2539
|
+
if (!res.writableEnded && !res.headersSent) {
|
|
2540
|
+
await sendWebResponseToNode(webRes, res);
|
|
2541
|
+
}
|
|
2497
2542
|
} catch (err) {
|
|
2498
2543
|
console.error("[Dinou Dev Server] Request error:", err);
|
|
2499
2544
|
if (!res.headersSent) {
|
|
@@ -2515,6 +2560,8 @@ server.on("error", (err) => {
|
|
|
2515
2560
|
process.exit(1);
|
|
2516
2561
|
});
|
|
2517
2562
|
|
|
2563
|
+
await applyServerPlugins(server);
|
|
2564
|
+
|
|
2518
2565
|
server.listen(PORT, async () => {
|
|
2519
2566
|
const buildTool = isWebpackBuild ? "webpack" : (process.env.DINOU_BUILD_TOOL || "esbuild");
|
|
2520
2567
|
|
package/dinou/package.json
CHANGED
package/eject.js
CHANGED
|
@@ -28,6 +28,50 @@ const ejectedScripts = {
|
|
|
28
28
|
"build:node:esbuild": "npm run build:node",
|
|
29
29
|
"build:node:rollup": "npm run build:client:rollup && node ./dinou/node/build.mjs",
|
|
30
30
|
"build:node:webpack": "npm run build:client:webpack && cross-env DINOU_BUILD_TOOL=webpack node ./dinou/node/build.mjs",
|
|
31
|
+
"build:node:compile": "npm run build:node:compile:esbuild",
|
|
32
|
+
"build:node:compile:esbuild": "npm run build:node:esbuild && bun build --compile .dinou/node/server.mjs --outfile dist/server",
|
|
33
|
+
"build:node:compile:rollup": "npm run build:node:rollup && bun build --compile .dinou/node/server.mjs --outfile dist/server",
|
|
34
|
+
"build:node:compile:webpack": "npm run build:node:webpack && bun build --compile .dinou/node/server.mjs --outfile dist/server",
|
|
35
|
+
"build:node:compile:linux": "npm run build:node:compile:linux:esbuild",
|
|
36
|
+
"build:node:compile:linux:esbuild": "npm run build:node:esbuild && bun build --compile --target=bun-linux-x64 .dinou/node/server.mjs --outfile dist/server-linux",
|
|
37
|
+
"build:node:compile:linux:rollup": "npm run build:node:rollup && bun build --compile --target=bun-linux-x64 .dinou/node/server.mjs --outfile dist/server-linux",
|
|
38
|
+
"build:node:compile:linux:webpack": "npm run build:node:webpack && bun build --compile --target=bun-linux-x64 .dinou/node/server.mjs --outfile dist/server-linux",
|
|
39
|
+
"build:node:compile:linux_arm": "npm run build:node:compile:linux_arm:esbuild",
|
|
40
|
+
"build:node:compile:linux_arm:esbuild": "npm run build:node:esbuild && bun build --compile --target=bun-linux-arm64 .dinou/node/server.mjs --outfile dist/server-linux-arm",
|
|
41
|
+
"build:node:compile:linux_arm:rollup": "npm run build:node:rollup && bun build --compile --target=bun-linux-arm64 .dinou/node/server.mjs --outfile dist/server-linux-arm",
|
|
42
|
+
"build:node:compile:linux_arm:webpack": "npm run build:node:webpack && bun build --compile --target=bun-linux-arm64 .dinou/node/server.mjs --outfile dist/server-linux-arm",
|
|
43
|
+
"build:node:compile:mac": "npm run build:node:compile:mac:esbuild",
|
|
44
|
+
"build:node:compile:mac:esbuild": "npm run build:node:esbuild && bun build --compile --target=bun-darwin-arm64 .dinou/node/server.mjs --outfile dist/server-mac",
|
|
45
|
+
"build:node:compile:mac:rollup": "npm run build:node:rollup && bun build --compile --target=bun-darwin-arm64 .dinou/node/server.mjs --outfile dist/server-mac",
|
|
46
|
+
"build:node:compile:mac:webpack": "npm run build:node:webpack && bun build --compile --target=bun-darwin-arm64 .dinou/node/server.mjs --outfile dist/server-mac",
|
|
47
|
+
"build:node:compile:win": "npm run build:node:compile:win:esbuild",
|
|
48
|
+
"build:node:compile:win:esbuild": "npm run build:node:esbuild && bun build --compile --target=bun-windows-x64 .dinou/node/server.mjs --outfile dist/server-win",
|
|
49
|
+
"build:node:compile:win:rollup": "npm run build:node:rollup && bun build --compile --target=bun-windows-x64 .dinou/node/server.mjs --outfile dist/server-win",
|
|
50
|
+
"build:node:compile:win:webpack": "npm run build:node:webpack && bun build --compile --target=bun-windows-x64 .dinou/node/server.mjs --outfile dist/server-win",
|
|
51
|
+
"build:node:pure": "npm run build:node:pure:esbuild",
|
|
52
|
+
"build:node:pure:esbuild": "cross-env NODE_ENV=production bun dinou/esbuild/build.mjs && bun ./dinou/node/build.mjs",
|
|
53
|
+
"build:node:pure:rollup": "cross-env NODE_ENV=production rollup -c ./dinou/rollup/rollup.config.js && bun ./dinou/node/build.mjs",
|
|
54
|
+
"build:node:pure:webpack": "cross-env NODE_ENV=production webpack --config dinou/webpack/webpack.config.js && cross-env DINOU_BUILD_TOOL=webpack bun ./dinou/node/build.mjs",
|
|
55
|
+
"build:node:pure:compile": "npm run build:node:pure:compile:esbuild",
|
|
56
|
+
"build:node:pure:compile:esbuild": "cross-env NODE_ENV=production bun dinou/esbuild/build.mjs && bun ./dinou/node/build.mjs && bun build --compile .dinou/node/server.mjs --outfile dist/server",
|
|
57
|
+
"build:node:pure:compile:rollup": "cross-env NODE_ENV=production rollup -c ./dinou/rollup/rollup.config.js && bun ./dinou/node/build.mjs && bun build --compile .dinou/node/server.mjs --outfile dist/server",
|
|
58
|
+
"build:node:pure:compile:webpack": "cross-env NODE_ENV=production webpack --config dinou/webpack/webpack.config.js && cross-env DINOU_BUILD_TOOL=webpack bun ./dinou/node/build.mjs && bun build --compile .dinou/node/server.mjs --outfile dist/server",
|
|
59
|
+
"build:node:pure:compile:linux": "npm run build:node:pure:compile:linux:esbuild",
|
|
60
|
+
"build:node:pure:compile:linux:esbuild": "cross-env NODE_ENV=production bun dinou/esbuild/build.mjs && bun ./dinou/node/build.mjs && bun build --compile --target=bun-linux-x64 .dinou/node/server.mjs --outfile dist/server-linux",
|
|
61
|
+
"build:node:pure:compile:linux:rollup": "cross-env NODE_ENV=production rollup -c ./dinou/rollup/rollup.config.js && bun ./dinou/node/build.mjs && bun build --compile --target=bun-linux-x64 .dinou/node/server.mjs --outfile dist/server-linux",
|
|
62
|
+
"build:node:pure:compile:linux:webpack": "cross-env NODE_ENV=production webpack --config dinou/webpack/webpack.config.js && cross-env DINOU_BUILD_TOOL=webpack bun ./dinou/node/build.mjs && bun build --compile --target=bun-linux-x64 .dinou/node/server.mjs --outfile dist/server-linux",
|
|
63
|
+
"build:node:pure:compile:linux_arm": "npm run build:node:pure:compile:linux_arm:esbuild",
|
|
64
|
+
"build:node:pure:compile:linux_arm:esbuild": "cross-env NODE_ENV=production bun dinou/esbuild/build.mjs && bun ./dinou/node/build.mjs && bun build --compile --target=bun-linux-arm64 .dinou/node/server.mjs --outfile dist/server-linux-arm",
|
|
65
|
+
"build:node:pure:compile:linux_arm:rollup": "cross-env NODE_ENV=production rollup -c ./dinou/rollup/rollup.config.js && bun ./dinou/node/build.mjs && bun build --compile --target=bun-linux-arm64 .dinou/node/server.mjs --outfile dist/server-linux-arm",
|
|
66
|
+
"build:node:pure:compile:linux_arm:webpack": "cross-env NODE_ENV=production webpack --config dinou/webpack/webpack.config.js && cross-env DINOU_BUILD_TOOL=webpack bun ./dinou/node/build.mjs && bun build --compile --target=bun-linux-arm64 .dinou/node/server.mjs --outfile dist/server-linux-arm",
|
|
67
|
+
"build:node:pure:compile:mac": "npm run build:node:pure:compile:mac:esbuild",
|
|
68
|
+
"build:node:pure:compile:mac:esbuild": "cross-env NODE_ENV=production bun dinou/esbuild/build.mjs && bun ./dinou/node/build.mjs && bun build --compile --target=bun-darwin-arm64 .dinou/node/server.mjs --outfile dist/server-mac",
|
|
69
|
+
"build:node:pure:compile:mac:rollup": "cross-env NODE_ENV=production rollup -c ./dinou/rollup/rollup.config.js && bun ./dinou/node/build.mjs && bun build --compile --target=bun-darwin-arm64 .dinou/node/server.mjs --outfile dist/server-mac",
|
|
70
|
+
"build:node:pure:compile:mac:webpack": "cross-env NODE_ENV=production webpack --config dinou/webpack/webpack.config.js && cross-env DINOU_BUILD_TOOL=webpack bun ./dinou/node/build.mjs && bun build --compile --target=bun-darwin-arm64 .dinou/node/server.mjs --outfile dist/server-mac",
|
|
71
|
+
"build:node:pure:compile:win": "npm run build:node:pure:compile:win:esbuild",
|
|
72
|
+
"build:node:pure:compile:win:esbuild": "cross-env NODE_ENV=production bun dinou/esbuild/build.mjs && bun ./dinou/node/build.mjs && bun build --compile --target=bun-windows-x64 .dinou/node/server.mjs --outfile dist/server-win",
|
|
73
|
+
"build:node:pure:compile:win:rollup": "cross-env NODE_ENV=production rollup -c ./dinou/rollup/rollup.config.js && bun ./dinou/node/build.mjs && bun build --compile --target=bun-windows-x64 .dinou/node/server.mjs --outfile dist/server-win",
|
|
74
|
+
"build:node:pure:compile:win:webpack": "cross-env NODE_ENV=production webpack --config dinou/webpack/webpack.config.js && cross-env DINOU_BUILD_TOOL=webpack bun ./dinou/node/build.mjs && bun build --compile --target=bun-windows-x64 .dinou/node/server.mjs --outfile dist/server-win",
|
|
31
75
|
"start:node": "cross-env NODE_ENV=production node dinou/adapters/node.mjs",
|
|
32
76
|
"start:node:esbuild": "npm run start:node",
|
|
33
77
|
"start:node:rollup": "npm run start:node",
|
|
@@ -146,7 +190,7 @@ function detectPackageManager() {
|
|
|
146
190
|
|
|
147
191
|
if (fs.existsSync(path.join(projectRoot, "yarn.lock"))) return "yarn";
|
|
148
192
|
if (fs.existsSync(path.join(projectRoot, "pnpm-lock.yaml"))) return "pnpm";
|
|
149
|
-
if (fs.existsSync(path.join(projectRoot, "bun.lockb"))) return "bun";
|
|
193
|
+
if (fs.existsSync(path.join(projectRoot, "bun.lockb")) || fs.existsSync(path.join(projectRoot, "bun.lock"))) return "bun";
|
|
150
194
|
if (fs.existsSync(path.join(projectRoot, "package-lock.json"))) return "npm";
|
|
151
195
|
|
|
152
196
|
return "npm";
|
package/package.json
CHANGED
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const { transformAsync } = require("@babel/core");
|
|
4
|
-
const { fileURLToPath, pathToFileURL } = require("url");
|
|
5
|
-
const createScopedName = require("./createScopedName");
|
|
6
|
-
const { extensionsWithDot } = require("./asset-extensions.js");
|
|
7
|
-
const { getAbsPathWithExt } = require("./get-abs-path-with-ext.js");
|
|
8
|
-
|
|
9
|
-
const { normalizePathCase } = require("./path-utils.js");
|
|
10
|
-
|
|
11
|
-
const Module = require("module");
|
|
12
|
-
const originalResolveFilename = Module._resolveFilename;
|
|
13
|
-
const isWebpack = process.env.DINOU_BUILD_TOOL === "webpack";
|
|
14
|
-
|
|
15
|
-
let reactServerPath, reactDomServerPath, reactJsxRuntimePath, reactJsxDevRuntimePath;
|
|
16
|
-
|
|
17
|
-
const reactPkgJson = require.resolve("react/package.json");
|
|
18
|
-
reactServerPath = path.join(path.dirname(reactPkgJson), "react.react-server.js");
|
|
19
|
-
reactJsxRuntimePath = path.join(path.dirname(reactPkgJson), "jsx-runtime.react-server.js");
|
|
20
|
-
reactJsxDevRuntimePath = path.join(path.dirname(reactPkgJson), "jsx-dev-runtime.react-server.js");
|
|
21
|
-
|
|
22
|
-
const reactDomPkgJson = require.resolve("react-dom/package.json");
|
|
23
|
-
reactDomServerPath = path.join(path.dirname(reactDomPkgJson), "react-dom.react-server.js");
|
|
24
|
-
|
|
25
|
-
let roggcServerNodePath, webpackServerNodePath;
|
|
26
|
-
try {
|
|
27
|
-
const roggcPkg = require.resolve("@roggc/react-server-dom-esm/package.json");
|
|
28
|
-
roggcServerNodePath = path.join(path.dirname(roggcPkg), "server.node.js");
|
|
29
|
-
} catch (e) {}
|
|
30
|
-
|
|
31
|
-
try {
|
|
32
|
-
const wpPkg = require.resolve("react-server-dom-webpack/package.json");
|
|
33
|
-
webpackServerNodePath = path.join(path.dirname(wpPkg), "server.node.js");
|
|
34
|
-
} catch (e) {}
|
|
35
|
-
|
|
36
|
-
Module._resolveFilename = function (request, parent, isMain, options) {
|
|
37
|
-
if (request === "react") {
|
|
38
|
-
return reactServerPath;
|
|
39
|
-
} else if (request === "react-dom") {
|
|
40
|
-
return reactDomServerPath;
|
|
41
|
-
} else if (request === "react/jsx-runtime") {
|
|
42
|
-
return reactJsxRuntimePath;
|
|
43
|
-
} else if (request === "react/jsx-dev-runtime") {
|
|
44
|
-
return reactJsxDevRuntimePath;
|
|
45
|
-
} else if (request === "@roggc/react-server-dom-esm/server" && roggcServerNodePath) {
|
|
46
|
-
return roggcServerNodePath;
|
|
47
|
-
} else if (request === "react-server-dom-webpack/server" && webpackServerNodePath) {
|
|
48
|
-
return webpackServerNodePath;
|
|
49
|
-
}
|
|
50
|
-
return originalResolveFilename.call(this, request, parent, isMain, options);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
require("./css-require-hook.js")();
|
|
54
|
-
|
|
55
|
-
function getMtimeParam(absPath) {
|
|
56
|
-
try {
|
|
57
|
-
const stats = fs.statSync(absPath);
|
|
58
|
-
return Math.round(stats.mtimeMs);
|
|
59
|
-
} catch (e) {
|
|
60
|
-
return Date.now();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
exports.resolve = async function resolve(specifier, context, defaultResolve) {
|
|
65
|
-
if (process.env.DINOU_PROCESS !== "ssr-html") {
|
|
66
|
-
if (specifier === "react") {
|
|
67
|
-
return { url: pathToFileURL(reactServerPath).href, shortCircuit: true };
|
|
68
|
-
}
|
|
69
|
-
if (specifier === "react/jsx-runtime") {
|
|
70
|
-
return { url: pathToFileURL(reactJsxRuntimePath).href, shortCircuit: true };
|
|
71
|
-
}
|
|
72
|
-
if (specifier === "react/jsx-dev-runtime") {
|
|
73
|
-
return { url: pathToFileURL(reactJsxDevRuntimePath).href, shortCircuit: true };
|
|
74
|
-
}
|
|
75
|
-
if (specifier === "react-dom") {
|
|
76
|
-
return { url: pathToFileURL(reactDomServerPath).href, shortCircuit: true };
|
|
77
|
-
}
|
|
78
|
-
if (specifier === "@roggc/react-server-dom-esm/server" && roggcServerNodePath) {
|
|
79
|
-
return { url: pathToFileURL(roggcServerNodePath).href, shortCircuit: true };
|
|
80
|
-
}
|
|
81
|
-
if (specifier === "react-server-dom-webpack/server" && webpackServerNodePath) {
|
|
82
|
-
return { url: pathToFileURL(webpackServerNodePath).href, shortCircuit: true };
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const absPathWithExt = getAbsPathWithExt(specifier, context);
|
|
87
|
-
if (absPathWithExt) {
|
|
88
|
-
let url = pathToFileURL(absPathWithExt).href;
|
|
89
|
-
if (process.env.NODE_ENV !== "production") {
|
|
90
|
-
url += `?mtime=${getMtimeParam(absPathWithExt)}`;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return {
|
|
94
|
-
url,
|
|
95
|
-
shortCircuit: true,
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Fallback to default resolver
|
|
100
|
-
return defaultResolve(specifier, context, defaultResolve);
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
exports.load = async function load(url, context, defaultLoad) {
|
|
104
|
-
// --- 🟢 Handle non-JS assets (png, jpg, etc.)
|
|
105
|
-
const assetExts = extensionsWithDot;
|
|
106
|
-
const ext = path.extname(url.split("?")[0]); // remove search params if any
|
|
107
|
-
|
|
108
|
-
if (assetExts.includes(ext)) {
|
|
109
|
-
// Return a tiny stub that mimics what asset-require-hook would do
|
|
110
|
-
const filepath = fileURLToPath(url.split("?")[0]);
|
|
111
|
-
const localName = path.basename(filepath, ext);
|
|
112
|
-
const hashedName = createScopedName(localName, filepath);
|
|
113
|
-
const virtualExport = `export default "/assets/${hashedName}${ext}";`;
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
format: "module",
|
|
117
|
-
source: virtualExport,
|
|
118
|
-
shortCircuit: true,
|
|
119
|
-
url,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (ext === ".css") {
|
|
124
|
-
const mod = require(fileURLToPath(url.split("?")[0]));
|
|
125
|
-
const source = `export default ${JSON.stringify(mod)};`;
|
|
126
|
-
return { format: "module", source, shortCircuit: true, url };
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const cleanUrl = url.split("?")[0];
|
|
130
|
-
if (/\.(jsx|tsx|ts|js)$/.test(cleanUrl)) {
|
|
131
|
-
let filename;
|
|
132
|
-
try {
|
|
133
|
-
filename = fileURLToPath(
|
|
134
|
-
cleanUrl.startsWith("file://") ? cleanUrl : pathToFileURL(cleanUrl).href
|
|
135
|
-
);
|
|
136
|
-
} catch (e) {
|
|
137
|
-
throw e;
|
|
138
|
-
}
|
|
139
|
-
const rel = path.relative(
|
|
140
|
-
normalizePathCase(process.cwd()),
|
|
141
|
-
normalizePathCase(filename)
|
|
142
|
-
);
|
|
143
|
-
const source = fs.readFileSync(filename, "utf-8");
|
|
144
|
-
const urlToReturn = pathToFileURL(filename).href;
|
|
145
|
-
|
|
146
|
-
const useClientRegex =
|
|
147
|
-
/^\s*(?:(?:\/\/[^\n]*\n\s*)|(?:\/\*[\s\S]*?\*\/\s*))*['"]use client['"]/;
|
|
148
|
-
const hasUseClient = useClientRegex.test(source);
|
|
149
|
-
|
|
150
|
-
const esmSyntaxRegex = /^(?:import|export)\b/m;
|
|
151
|
-
const hasESMSyntax = esmSyntaxRegex.test(source);
|
|
152
|
-
|
|
153
|
-
if (ext === ".js" && !rel.startsWith("src" + path.sep) && !hasESMSyntax) {
|
|
154
|
-
return defaultLoad(url, context, defaultLoad);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const isReactServer = process.env.DINOU_PROCESS !== "ssr-html";
|
|
158
|
-
if (isReactServer && hasUseClient) {
|
|
159
|
-
const parseExports = require("./parse-exports.js");
|
|
160
|
-
const exports = parseExports(source);
|
|
161
|
-
const clientPkgPath = isWebpack
|
|
162
|
-
? require.resolve("react-server-dom-webpack/package.json")
|
|
163
|
-
: require.resolve("@roggc/react-server-dom-esm/package.json");
|
|
164
|
-
const clientServerNodePath = path.join(path.dirname(clientPkgPath), "server.node.js");
|
|
165
|
-
const clientServerNodeUrl = pathToFileURL(clientServerNodePath).href;
|
|
166
|
-
let newSrc = `import pkg from ${JSON.stringify(clientServerNodeUrl)};\n`;
|
|
167
|
-
newSrc += 'const {registerClientReference} = pkg;\n';
|
|
168
|
-
for (const name of exports) {
|
|
169
|
-
if (name === 'default') {
|
|
170
|
-
newSrc += 'export default ';
|
|
171
|
-
newSrc += 'registerClientReference(function() {';
|
|
172
|
-
newSrc += 'throw new Error(' + JSON.stringify("Attempted to call the default export of " + urlToReturn + " from the server but it's on the client.") + ');';
|
|
173
|
-
newSrc += '},';
|
|
174
|
-
newSrc += JSON.stringify(urlToReturn) + ',';
|
|
175
|
-
newSrc += JSON.stringify(name) + ');\n';
|
|
176
|
-
} else {
|
|
177
|
-
newSrc += 'export const ' + name + ' = ';
|
|
178
|
-
newSrc += 'registerClientReference(function() {';
|
|
179
|
-
newSrc += 'throw new Error(' + JSON.stringify("Attempted to call " + name + "() from the server but " + name + " is on the client.") + ');';
|
|
180
|
-
newSrc += '},';
|
|
181
|
-
newSrc += JSON.stringify(urlToReturn) + ',';
|
|
182
|
-
newSrc += JSON.stringify(name) + ');\n';
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return {
|
|
186
|
-
format: "module",
|
|
187
|
-
source: newSrc,
|
|
188
|
-
shortCircuit: true,
|
|
189
|
-
url,
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const { useServerRegex } = require("../constants.js");
|
|
194
|
-
const hasUseServer = useServerRegex.test(source);
|
|
195
|
-
|
|
196
|
-
if (isReactServer && hasUseServer) {
|
|
197
|
-
const parseExports = require("./parse-exports.js");
|
|
198
|
-
const exports = parseExports(source);
|
|
199
|
-
|
|
200
|
-
const { code } = await transformAsync(source, {
|
|
201
|
-
filename,
|
|
202
|
-
presets: [
|
|
203
|
-
["@babel/preset-react", { runtime: "automatic" }],
|
|
204
|
-
"@babel/preset-typescript",
|
|
205
|
-
],
|
|
206
|
-
sourceMaps: "inline",
|
|
207
|
-
ast: false,
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
let newSrc = code + "\n\n";
|
|
211
|
-
|
|
212
|
-
const serverPkgPath = isWebpack
|
|
213
|
-
? require.resolve("react-server-dom-webpack/package.json")
|
|
214
|
-
: require.resolve("@roggc/react-server-dom-esm/package.json");
|
|
215
|
-
const serverNodePath = path.join(path.dirname(serverPkgPath), "server.node.js");
|
|
216
|
-
const serverNodeUrl = pathToFileURL(serverNodePath).href;
|
|
217
|
-
newSrc += `import pkgServer from ${JSON.stringify(serverNodeUrl)};\n`;
|
|
218
|
-
newSrc += 'const {registerServerReference} = pkgServer;\n';
|
|
219
|
-
|
|
220
|
-
const relativeFileUrl = "file:///" + rel.replace(/\\/g, "/");
|
|
221
|
-
for (const name of exports) {
|
|
222
|
-
if (name !== 'default') {
|
|
223
|
-
newSrc += `registerServerReference(${name}, ${JSON.stringify(relativeFileUrl)}, ${JSON.stringify(name)});\n`;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
return {
|
|
228
|
-
format: "module",
|
|
229
|
-
source: newSrc,
|
|
230
|
-
shortCircuit: true,
|
|
231
|
-
url,
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
if (ext === ".js") {
|
|
236
|
-
return {
|
|
237
|
-
format: "module",
|
|
238
|
-
source,
|
|
239
|
-
shortCircuit: true,
|
|
240
|
-
url,
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const { code } = await transformAsync(source, {
|
|
245
|
-
filename,
|
|
246
|
-
presets: [
|
|
247
|
-
["@babel/preset-react", { runtime: "automatic" }],
|
|
248
|
-
"@babel/preset-typescript",
|
|
249
|
-
],
|
|
250
|
-
sourceMaps: "inline",
|
|
251
|
-
ast: false,
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
// const urlToReturn = pathToFileURL(filename).href;
|
|
255
|
-
|
|
256
|
-
return {
|
|
257
|
-
format: "module",
|
|
258
|
-
source: code,
|
|
259
|
-
shortCircuit: true,
|
|
260
|
-
url,
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (url) {
|
|
265
|
-
return defaultLoad(url, context, defaultLoad);
|
|
266
|
-
}
|
|
267
|
-
};
|