dinou 2.3.0 → 2.3.2

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,18 @@ 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.3.2]
9
+
10
+ ### Fixed
11
+
12
+ - Do not use `for await` when it is not necessary in buildStaticPages.
13
+
14
+ ## [2.3.1]
15
+
16
+ ### Fixed
17
+
18
+ - Build for ES modules (ESM).
19
+
8
20
  ## [2.3.0]
9
21
 
10
22
  ### Added
@@ -12,6 +12,7 @@ const { asyncRenderJSXToClientJSX } = require("./render-jsx-to-client-jsx");
12
12
  const {
13
13
  getFilePathAndDynamicParams,
14
14
  } = require("./get-file-path-and-dynamic-params");
15
+ const importModule = require("./import-module");
15
16
 
16
17
  async function buildStaticPages() {
17
18
  const srcFolder = path.resolve(process.cwd(), "src");
@@ -26,7 +27,7 @@ async function buildStaticPages() {
26
27
  mkdirSync(distFolder, { recursive: true });
27
28
  }
28
29
 
29
- function collectPages(currentPath, segments = [], params = {}) {
30
+ async function collectPages(currentPath, segments = [], params = {}) {
30
31
  const entries = readdirSync(currentPath, { withFileTypes: true });
31
32
  const pages = [];
32
33
 
@@ -34,7 +35,10 @@ async function buildStaticPages() {
34
35
  if (entry.isDirectory()) {
35
36
  if (entry.name.startsWith("(") && entry.name.endsWith(")")) {
36
37
  pages.push(
37
- ...collectPages(path.join(currentPath, entry.name), segments)
38
+ ...(await collectPages(
39
+ path.join(currentPath, entry.name),
40
+ segments
41
+ ))
38
42
  );
39
43
  } else if (
40
44
  entry.name.startsWith("[[...") &&
@@ -66,7 +70,7 @@ async function buildStaticPages() {
66
70
  let dynamic;
67
71
  let getStaticPaths;
68
72
  if (pageFunctionsPath) {
69
- const module = require(pageFunctionsPath);
73
+ const module = await importModule(pageFunctionsPath);
70
74
  getStaticPaths = module.getStaticPaths;
71
75
  dynamic = module.dynamic;
72
76
  }
@@ -81,10 +85,14 @@ async function buildStaticPages() {
81
85
  const paths = getStaticPaths();
82
86
  for (const path of paths) {
83
87
  pages.push(
84
- ...collectPages(dynamicPath, [...segments, ...path], {
85
- ...params,
86
- [paramName]: path,
87
- })
88
+ ...(await collectPages(
89
+ dynamicPath,
90
+ [...segments, ...path],
91
+ {
92
+ ...params,
93
+ [paramName]: path,
94
+ }
95
+ ))
88
96
  );
89
97
  }
90
98
  }
@@ -118,7 +126,7 @@ async function buildStaticPages() {
118
126
  let dynamic;
119
127
  let getStaticPaths;
120
128
  if (pageFunctionsPath) {
121
- const module = require(pageFunctionsPath);
129
+ const module = await importModule(pageFunctionsPath);
122
130
  getStaticPaths = module.getStaticPaths;
123
131
  dynamic = module.dynamic;
124
132
  }
@@ -133,10 +141,14 @@ async function buildStaticPages() {
133
141
  const paths = getStaticPaths();
134
142
  for (const path of paths) {
135
143
  pages.push(
136
- ...collectPages(dynamicPath, [...segments, ...path], {
137
- ...params,
138
- [paramName]: path,
139
- })
144
+ ...(await collectPages(
145
+ dynamicPath,
146
+ [...segments, ...path],
147
+ {
148
+ ...params,
149
+ [paramName]: path,
150
+ }
151
+ ))
140
152
  );
141
153
  }
142
154
  }
@@ -171,7 +183,7 @@ async function buildStaticPages() {
171
183
  let dynamic;
172
184
  let getStaticPaths;
173
185
  if (pageFunctionsPath) {
174
- const module = require(pageFunctionsPath);
186
+ const module = await importModule(pageFunctionsPath);
175
187
  getStaticPaths = module.getStaticPaths;
176
188
  dynamic = module.dynamic;
177
189
  }
@@ -186,10 +198,10 @@ async function buildStaticPages() {
186
198
  const paths = getStaticPaths();
187
199
  for (const path of paths) {
188
200
  pages.push(
189
- ...collectPages(dynamicPath, [...segments, path], {
201
+ ...(await collectPages(dynamicPath, [...segments, path], {
190
202
  ...params,
191
203
  [paramName]: path,
192
- })
204
+ }))
193
205
  );
194
206
  }
195
207
  }
@@ -223,7 +235,7 @@ async function buildStaticPages() {
223
235
  let dynamic;
224
236
  let getStaticPaths;
225
237
  if (pageFunctionsPath) {
226
- const module = require(pageFunctionsPath);
238
+ const module = await importModule(pageFunctionsPath);
227
239
  getStaticPaths = module.getStaticPaths;
228
240
  dynamic = module.dynamic;
229
241
  }
@@ -236,10 +248,10 @@ async function buildStaticPages() {
236
248
  const paths = getStaticPaths();
237
249
  for (const path of paths) {
238
250
  pages.push(
239
- ...collectPages(dynamicPath, [...segments, path], {
251
+ ...(await collectPages(dynamicPath, [...segments, path], {
240
252
  ...params,
241
253
  [paramName]: path,
242
- })
254
+ }))
243
255
  );
244
256
  }
245
257
  }
@@ -249,11 +261,11 @@ async function buildStaticPages() {
249
261
  }
250
262
  } else if (!entry.name.startsWith("@")) {
251
263
  pages.push(
252
- ...collectPages(
264
+ ...(await collectPages(
253
265
  path.join(currentPath, entry.name),
254
266
  [...segments, entry.name],
255
267
  params
256
- )
268
+ ))
257
269
  );
258
270
  }
259
271
  }
@@ -282,7 +294,7 @@ async function buildStaticPages() {
282
294
  );
283
295
  let dynamic;
284
296
  if (pageFunctionsPath) {
285
- const module = require(pageFunctionsPath);
297
+ const module = await importModule(pageFunctionsPath);
286
298
  dynamic = module.dynamic;
287
299
  }
288
300
  if (pagePath && !dynamic?.()) {
@@ -293,9 +305,9 @@ async function buildStaticPages() {
293
305
  return pages;
294
306
  }
295
307
 
296
- const pages = collectPages(srcFolder);
308
+ const pages = await collectPages(srcFolder);
297
309
 
298
- for await (const { path: folderPath, segments, params } of pages) {
310
+ for (const { path: folderPath, segments, params } of pages) {
299
311
  try {
300
312
  const [pagePath] = getFilePathAndDynamicParams(
301
313
  segments,
@@ -307,7 +319,7 @@ async function buildStaticPages() {
307
319
  undefined,
308
320
  segments.length
309
321
  );
310
- const pageModule = require(pagePath);
322
+ const pageModule = await importModule(pagePath);
311
323
  const Page = pageModule.default ?? pageModule;
312
324
  // Set displayName for better serialization
313
325
  // if (!Page.displayName) Page.displayName = "Page";
@@ -329,7 +341,7 @@ async function buildStaticPages() {
329
341
  let revalidate;
330
342
 
331
343
  if (pageFunctionsPath) {
332
- const pageFunctionsModule = require(pageFunctionsPath);
344
+ const pageFunctionsModule = await importModule(pageFunctionsPath);
333
345
  const getProps = pageFunctionsModule.getProps;
334
346
  revalidate = pageFunctionsModule.revalidate;
335
347
  pageFunctionsProps = await getProps?.(params, {}, {});
@@ -357,7 +369,7 @@ async function buildStaticPages() {
357
369
  if (layouts && Array.isArray(layouts)) {
358
370
  let index = 0;
359
371
  for (const [layoutPath, dParams, slots] of layouts.reverse()) {
360
- const layoutModule = require(layoutPath);
372
+ const layoutModule = await importModule(layoutPath);
361
373
  const Layout = layoutModule.default ?? layoutModule;
362
374
  // if (!Layout.displayName) Layout.displayName = "Layout";
363
375
  const updatedSlots = {};
@@ -489,7 +501,7 @@ async function buildStaticPage(reqPath) {
489
501
  );
490
502
  if (!pagePath) throw new Error(`No page found for ${reqPath}`);
491
503
 
492
- const pageModule = require(pagePath);
504
+ const pageModule = await importModule(pagePath);
493
505
  const Page = pageModule.default ?? pageModule;
494
506
 
495
507
  let props = { params: dParams, query: {} };
@@ -507,7 +519,7 @@ async function buildStaticPage(reqPath) {
507
519
  let pageFunctionsProps;
508
520
  let revalidate;
509
521
  if (pageFunctionsPath) {
510
- const pageFunctionsModule = require(pageFunctionsPath);
522
+ const pageFunctionsModule = await importModule(pageFunctionsPath);
511
523
  const getProps = pageFunctionsModule.getProps;
512
524
  revalidate = pageFunctionsModule.revalidate;
513
525
  pageFunctionsProps = await getProps?.(dParams, {}, {});
@@ -534,7 +546,7 @@ async function buildStaticPage(reqPath) {
534
546
  if (layouts && Array.isArray(layouts)) {
535
547
  let index = 0;
536
548
  for (const [layoutPath, dParams, slots] of layouts.reverse()) {
537
- const layoutModule = require(layoutPath);
549
+ const layoutModule = await importModule(layoutPath);
538
550
  const Layout = layoutModule.default ?? layoutModule;
539
551
  const updatedSlots = {};
540
552
  for (const [slotName, slotElement] of Object.entries(slots)) {
@@ -10,7 +10,7 @@ async function getSSGJSXOrJSX(
10
10
  const result =
11
11
  Object.keys(query).length || isDevelopment || Object.keys(cookies).length
12
12
  ? await getJSX(reqPath, query, cookies)
13
- : getSSGJSX(reqPath) ?? (await getJSX(reqPath, query, cookies));
13
+ : (await getSSGJSX(reqPath)) ?? (await getJSX(reqPath, query, cookies));
14
14
  return result;
15
15
  }
16
16
 
@@ -1,8 +1,9 @@
1
1
  const path = require("path");
2
2
  const { existsSync, readFileSync } = require("fs");
3
3
  const React = require("react");
4
+ const importModule = require("./import-module");
4
5
 
5
- function deserializeReactElement(
6
+ async function deserializeReactElement(
6
7
  serialized,
7
8
  returnUndefined = { value: false }
8
9
  ) {
@@ -17,7 +18,9 @@ function deserializeReactElement(
17
18
  let Component;
18
19
  if (modulePath) {
19
20
  try {
20
- const module = require(path.resolve(process.cwd(), modulePath));
21
+ const module = await importModule(
22
+ path.resolve(process.cwd(), modulePath)
23
+ );
21
24
  Component = module.default ?? module;
22
25
  } catch (err) {
23
26
  console.error(`Error loading module ${modulePath}:`, err);
@@ -67,12 +70,12 @@ function deserializeReactElement(
67
70
  return returnUndefined.value ? undefined : serialized;
68
71
  }
69
72
 
70
- function getSSGJSX(reqPath) {
73
+ async function getSSGJSX(reqPath) {
71
74
  const distFolder = path.resolve(process.cwd(), "dist");
72
75
  const jsonPath = path.join(distFolder, reqPath, "index.json");
73
76
  if (existsSync(jsonPath)) {
74
77
  const { jsx } = JSON.parse(readFileSync(jsonPath, "utf8"));
75
- const deserializedJSX = deserializeReactElement(jsx);
78
+ const deserializedJSX = await deserializeReactElement(jsx);
76
79
  return deserializedJSX;
77
80
  }
78
81
  }
@@ -126,7 +126,7 @@ async function renderToStream(reqPath, query, cookies = {}) {
126
126
  const jsx =
127
127
  Object.keys(query).length || isDevelopment || Object.keys(cookies).length
128
128
  ? renderJSXToClientJSX(await getJSX(reqPath, query, cookies))
129
- : getSSGJSX(reqPath) ??
129
+ : (await getSSGJSX(reqPath)) ??
130
130
  renderJSXToClientJSX(await getJSX(reqPath, query, cookies));
131
131
 
132
132
  const stream = renderToPipeableStream(jsx, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dinou",
3
- "version": "2.3.0",
3
+ "version": "2.3.2",
4
4
  "description": "Minimal React 19 Framework",
5
5
  "main": "index.js",
6
6
  "bin": {