dinou 2.1.0 → 2.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 CHANGED
@@ -5,6 +5,23 @@ 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
+ ## [2.2.0]
9
+
10
+ ### Fixed
11
+
12
+ - buildStaticPages - collectPages.
13
+ - getFilePathAndDynamicParams.
14
+
15
+ ### Added
16
+
17
+ - Cookies support. Now getProps receive params, query, and cookies as parameters (function getProps(params, query, cookies)).
18
+
19
+ ## [2.1.1]
20
+
21
+ ### Fixed
22
+
23
+ - Warning in Rollup about serverFunctionProxy.
24
+
8
25
  ## [2.1.0]
9
26
 
10
27
  ### Added
@@ -26,7 +26,7 @@ async function buildStaticPages() {
26
26
  mkdirSync(distFolder, { recursive: true });
27
27
  }
28
28
 
29
- function collectPages(currentPath, segments = []) {
29
+ function collectPages(currentPath, segments = [], params = {}) {
30
30
  const entries = readdirSync(currentPath, { withFileTypes: true });
31
31
  const pages = [];
32
32
 
@@ -74,17 +74,18 @@ async function buildStaticPages() {
74
74
  console.log(
75
75
  `Found optional catch-all route: ${
76
76
  segments.join("/") ?? ""
77
- }/[${paramName}]`
77
+ }/[[...${paramName}]]`
78
78
  );
79
79
  try {
80
80
  if (getStaticPaths) {
81
81
  const paths = getStaticPaths();
82
82
  for (const path of paths) {
83
- pages.push({
84
- path: dynamicPath,
85
- segments: [...segments, ...path],
86
- params: { [paramName]: path },
87
- });
83
+ pages.push(
84
+ ...collectPages(dynamicPath, [...segments, ...path], {
85
+ ...params,
86
+ [paramName]: path,
87
+ })
88
+ );
88
89
  }
89
90
  }
90
91
  } catch (err) {
@@ -125,17 +126,18 @@ async function buildStaticPages() {
125
126
  console.log(
126
127
  `Found catch-all route: ${
127
128
  segments.join("/") ?? ""
128
- }/[${paramName}]`
129
+ }/[...${paramName}]`
129
130
  );
130
131
  try {
131
132
  if (getStaticPaths) {
132
133
  const paths = getStaticPaths();
133
134
  for (const path of paths) {
134
- pages.push({
135
- path: dynamicPath,
136
- segments: [...segments, ...path],
137
- params: { [paramName]: path },
138
- });
135
+ pages.push(
136
+ ...collectPages(dynamicPath, [...segments, ...path], {
137
+ ...params,
138
+ [paramName]: path,
139
+ })
140
+ );
139
141
  }
140
142
  }
141
143
  } catch (err) {
@@ -177,17 +179,18 @@ async function buildStaticPages() {
177
179
  console.log(
178
180
  `Found optional dynamic route: ${
179
181
  segments.join("/") ?? ""
180
- }/[${paramName}]`
182
+ }/[[${paramName}]]`
181
183
  );
182
184
  try {
183
185
  if (getStaticPaths) {
184
186
  const paths = getStaticPaths();
185
187
  for (const path of paths) {
186
- pages.push({
187
- path: dynamicPath,
188
- segments: [...segments, path],
189
- params: { [paramName]: path },
190
- });
188
+ pages.push(
189
+ ...collectPages(dynamicPath, [...segments, path], {
190
+ ...params,
191
+ [paramName]: path,
192
+ })
193
+ );
191
194
  }
192
195
  }
193
196
  } catch (err) {
@@ -232,11 +235,12 @@ async function buildStaticPages() {
232
235
  if (getStaticPaths) {
233
236
  const paths = getStaticPaths();
234
237
  for (const path of paths) {
235
- pages.push({
236
- path: dynamicPath,
237
- segments: [...segments, path],
238
- params: { [paramName]: path },
239
- });
238
+ pages.push(
239
+ ...collectPages(dynamicPath, [...segments, path], {
240
+ ...params,
241
+ [paramName]: path,
242
+ })
243
+ );
240
244
  }
241
245
  }
242
246
  } catch (err) {
@@ -245,10 +249,11 @@ async function buildStaticPages() {
245
249
  }
246
250
  } else if (!entry.name.startsWith("@")) {
247
251
  pages.push(
248
- ...collectPages(path.join(currentPath, entry.name), [
249
- ...segments,
250
- entry.name,
251
- ])
252
+ ...collectPages(
253
+ path.join(currentPath, entry.name),
254
+ [...segments, entry.name],
255
+ params
256
+ )
252
257
  );
253
258
  }
254
259
  }
@@ -262,7 +267,8 @@ async function buildStaticPages() {
262
267
  true,
263
268
  true,
264
269
  undefined,
265
- segments.length
270
+ segments.length,
271
+ params
266
272
  );
267
273
  const [pageFunctionsPath] = getFilePathAndDynamicParams(
268
274
  segments,
@@ -326,7 +332,7 @@ async function buildStaticPages() {
326
332
  const pageFunctionsModule = require(pageFunctionsPath);
327
333
  const getProps = pageFunctionsModule.getProps;
328
334
  revalidate = pageFunctionsModule.revalidate;
329
- pageFunctionsProps = await getProps?.(params);
335
+ pageFunctionsProps = await getProps?.(params, {}, {});
330
336
  props = { ...props, ...(pageFunctionsProps?.page ?? {}) };
331
337
  }
332
338
 
@@ -504,7 +510,7 @@ async function buildStaticPage(reqPath) {
504
510
  const pageFunctionsModule = require(pageFunctionsPath);
505
511
  const getProps = pageFunctionsModule.getProps;
506
512
  revalidate = pageFunctionsModule.revalidate;
507
- pageFunctionsProps = await getProps?.(dParams);
513
+ pageFunctionsProps = await getProps?.(dParams, {}, {});
508
514
  props = { ...props, ...(pageFunctionsProps?.page ?? {}) };
509
515
  }
510
516
 
@@ -58,13 +58,14 @@ function getFilePathAndDynamicParams(
58
58
  if (!accumulative) return [candidatePath, dParams];
59
59
  const slots = getSlots(currentPath, reqSegments, query);
60
60
  accumulate.push([candidatePath, dParams, slots]);
61
- return accumulate;
62
- }
63
- if (accumulative) {
64
- const slots = getSlots(currentPath, reqSegments, query);
65
- accumulate.push([candidatePath, dParams, slots]);
61
+ if (finalDestination) return accumulate;
66
62
  } else {
67
- foundInCurrentPath = candidatePath;
63
+ if (accumulative) {
64
+ const slots = getSlots(currentPath, reqSegments, query);
65
+ accumulate.push([candidatePath, dParams, slots]);
66
+ } else {
67
+ foundInCurrentPath = candidatePath;
68
+ }
68
69
  }
69
70
  }
70
71
  }
@@ -76,13 +77,14 @@ function getFilePathAndDynamicParams(
76
77
  if (!accumulative) return [candidatePath, dParams];
77
78
  const slots = getSlots(currentPath, reqSegments, query);
78
79
  accumulate.push([candidatePath, dParams, slots]);
79
- return accumulate;
80
- }
81
- if (accumulative) {
82
- const slots = getSlots(currentPath, reqSegments, query);
83
- accumulate.push([candidatePath, dParams, slots]);
80
+ if (finalDestination) return accumulate;
84
81
  } else {
85
- foundInCurrentPath = candidatePath;
82
+ if (accumulative) {
83
+ const slots = getSlots(currentPath, reqSegments, query);
84
+ accumulate.push([candidatePath, dParams, slots]);
85
+ } else {
86
+ foundInCurrentPath = candidatePath;
87
+ }
86
88
  }
87
89
  }
88
90
  }
package/dinou/get-jsx.js CHANGED
@@ -5,7 +5,7 @@ const {
5
5
  getFilePathAndDynamicParams,
6
6
  } = require("./get-file-path-and-dynamic-params");
7
7
 
8
- async function getJSX(reqPath, query) {
8
+ async function getJSX(reqPath, query, cookies) {
9
9
  const srcFolder = path.resolve(process.cwd(), "src");
10
10
  const reqSegments = reqPath.split("/").filter(Boolean);
11
11
  const folderPath = path.join(srcFolder, ...reqSegments);
@@ -55,13 +55,13 @@ async function getJSX(reqPath, query) {
55
55
  params: dParams ?? {},
56
56
  query,
57
57
  });
58
-
58
+
59
59
  const notFoundDir = path.dirname(notFoundPath);
60
60
  const noLayoutNotFoundPath = path.join(
61
61
  notFoundDir,
62
62
  `no_layout_not_found`
63
63
  );
64
- if (existsSync(noLayoutNotFoundPath)) {
64
+ if (existsSync(noLayoutNotFoundPath)) {
65
65
  return jsx;
66
66
  }
67
67
  }
@@ -73,7 +73,7 @@ async function getJSX(reqPath, query) {
73
73
  params: dynamicParams,
74
74
  query,
75
75
  };
76
-
76
+
77
77
  const pageFolder = path.dirname(pagePath);
78
78
  const [pageFunctionsPath] = getFilePathAndDynamicParams(
79
79
  reqSegments,
@@ -85,11 +85,10 @@ async function getJSX(reqPath, query) {
85
85
  undefined,
86
86
  reqSegments.length
87
87
  );
88
-
89
88
  if (pageFunctionsPath) {
90
89
  const pageFunctionsModule = require(pageFunctionsPath);
91
90
  const getProps = pageFunctionsModule.getProps;
92
- pageFunctionsProps = await getProps?.(dynamicParams);
91
+ pageFunctionsProps = await getProps?.(dynamicParams, query, cookies);
93
92
  props = { ...props, ...(pageFunctionsProps?.page ?? {}) };
94
93
  }
95
94
 
@@ -1,11 +1,16 @@
1
1
  const getJSX = require("./get-jsx.js");
2
2
  const getSSGJSX = require("./get-ssg-jsx.js");
3
3
 
4
- async function getSSGJSXOrJSX(reqPath, query, isDevelopment = false) {
4
+ async function getSSGJSXOrJSX(
5
+ reqPath,
6
+ query,
7
+ cookies = {},
8
+ isDevelopment = false
9
+ ) {
5
10
  const result =
6
- Object.keys(query).length || isDevelopment
7
- ? await getJSX(reqPath, query)
8
- : getSSGJSX(reqPath) ?? (await getJSX(reqPath, query));
11
+ Object.keys(query).length || isDevelopment || Object.keys(cookies).length
12
+ ? await getJSX(reqPath, query, cookies)
13
+ : getSSGJSX(reqPath) ?? (await getJSX(reqPath, query, cookies));
9
14
  return result;
10
15
  }
11
16
 
@@ -1,11 +1,16 @@
1
1
  const path = require("path");
2
2
  const { spawn } = require("child_process");
3
3
 
4
- function renderAppToHtml(reqPath, paramsString) {
4
+ function renderAppToHtml(reqPath, paramsString, cookiesString = "{}") {
5
5
  return new Promise((resolve, reject) => {
6
6
  const child = spawn(
7
7
  "node",
8
- [path.resolve(__dirname, "render-html.js"), reqPath, paramsString],
8
+ [
9
+ path.resolve(__dirname, "render-html.js"),
10
+ reqPath,
11
+ paramsString,
12
+ cookiesString,
13
+ ],
9
14
  {
10
15
  env: {
11
16
  ...process.env,
@@ -133,13 +133,13 @@ function formatErrorHtmlProduction(error) {
133
133
  `;
134
134
  }
135
135
 
136
- async function renderToStream(reqPath, query) {
136
+ async function renderToStream(reqPath, query, cookies = {}) {
137
137
  try {
138
138
  const jsx =
139
- Object.keys(query).length || isDevelopment
140
- ? renderJSXToClientJSX(await getJSX(reqPath, query))
139
+ Object.keys(query).length || isDevelopment || Object.keys(cookies).length
140
+ ? renderJSXToClientJSX(await getJSX(reqPath, query, cookies))
141
141
  : getSSGJSX(reqPath) ??
142
- renderJSXToClientJSX(await getJSX(reqPath, query));
142
+ renderJSXToClientJSX(await getJSX(reqPath, query, cookies));
143
143
 
144
144
  const stream = renderToPipeableStream(jsx, {
145
145
  onError(error) {
@@ -174,7 +174,7 @@ async function renderToStream(reqPath, query) {
174
174
  );
175
175
  process.exit(1);
176
176
  },
177
- bootstrapScripts: ["/error.js"],
177
+ bootstrapModules: ["/error.js"],
178
178
  bootstrapScriptContent: `window.__DINOU_ERROR_MESSAGE__=${JSON.stringify(
179
179
  error.message || "Unknown error"
180
180
  )};window.__DINOU_ERROR_STACK__=${JSON.stringify(
@@ -218,6 +218,7 @@ async function renderToStream(reqPath, query) {
218
218
 
219
219
  const reqPath = process.argv[2];
220
220
  const query = JSON.parse(process.argv[3]);
221
+ const cookies = JSON.parse(process.argv[4] || "{}");
221
222
 
222
223
  process.on("uncaughtException", (error) => {
223
224
  process.stdout.write(formatErrorHtml(error));
@@ -242,4 +243,4 @@ process.on("unhandledRejection", (reason) => {
242
243
  process.exit(1);
243
244
  });
244
245
 
245
- renderToStream(reqPath, query);
246
+ renderToStream(reqPath, query, cookies);
package/dinou/server.js CHANGED
@@ -182,9 +182,10 @@ if (isDevelopment) {
182
182
  }
183
183
  });
184
184
  }
185
-
185
+ const cookieParser = require("cookie-parser");
186
+ const appUseCookieParser = cookieParser();
186
187
  const app = express();
187
-
188
+ app.use(appUseCookieParser);
188
189
  app.use(express.json());
189
190
 
190
191
  app.use(express.static(path.resolve(process.cwd(), webpackFolder)));
@@ -217,9 +218,12 @@ app.get(/^\/____rsc_payload____\/.*\/?$/, async (req, res) => {
217
218
  return readStream.pipe(res);
218
219
  }
219
220
  }
220
-
221
- const jsx = await getSSGJSXOrJSX(reqPath, { ...req.query }, isDevelopment);
222
-
221
+ const jsx = await getSSGJSXOrJSX(
222
+ reqPath,
223
+ { ...req.query },
224
+ { ...req.cookies },
225
+ isDevelopment
226
+ );
223
227
  const manifest = JSON.parse(
224
228
  readFileSync(
225
229
  path.resolve(`${webpackFolder}/react-client-manifest.json`),
@@ -273,7 +277,8 @@ app.get(/^\/.*\/?$/, async (req, res) => {
273
277
 
274
278
  const appHtmlStream = await renderAppToHtml(
275
279
  reqPath,
276
- JSON.stringify({ ...req.query })
280
+ JSON.stringify({ ...req.query }),
281
+ JSON.stringify({ ...req.cookies })
277
282
  );
278
283
 
279
284
  res.setHeader("Content-Type", "text/html");
@@ -305,6 +310,10 @@ app.post("/____server_function____", async (req, res) => {
305
310
  return res.status(400).json({ error: "Export is not a function" });
306
311
  }
307
312
 
313
+ const context = { req, res };
314
+ if (fn.length === args.length + 1) {
315
+ args.push(context);
316
+ }
308
317
  const result = await fn(...args);
309
318
 
310
319
  if (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dinou",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Minimal React 19 Framework",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -38,6 +38,7 @@
38
38
  "chokidar": "^4.0.3",
39
39
  "commander": "^14.0.0",
40
40
  "concurrently": "^9.2.0",
41
+ "cookie-parser": "^1.4.7",
41
42
  "cross-env": "^7.0.3",
42
43
  "css-modules-require-hook": "^4.2.3",
43
44
  "dotenv": "^16.5.0",
package/rollup.config.js CHANGED
@@ -51,7 +51,7 @@ module.exports = async function () {
51
51
  entryFileNames: "[name].js",
52
52
  chunkFileNames: "[name].js",
53
53
  },
54
- external: ["/refresh.js", "/__hmr_client__.js"],
54
+ external: ["/refresh.js", "/__hmr_client__.js", "/serverFunctionProxy.js"],
55
55
  plugins: [
56
56
  del({
57
57
  targets: `${outputDirectory}/*`,