dinou 1.0.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/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Roger Gomez Castells (@roggc)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # **dinou**: Minimal React 19 Framework
2
+
3
+ dinou is a React 19 framework. dinou means 19 in catalan.
4
+
5
+ - You can create an npm project by yourself (`npm init -y`),
6
+
7
+ - Install dependencies (`npm i react react-dom dinou`)
8
+
9
+ - Create scripts in `package.json`:
10
+
11
+ - "dev": "dinou dev"
12
+
13
+ - "build": "dinou build"
14
+
15
+ - "start": "dinou start"
16
+
17
+ - "eject": "dinou eject"
18
+
19
+ - Create an `src` folder with a `page.jsx` (or `.tsx`, `.js`)
20
+
21
+ ```typescript
22
+ "use client";
23
+
24
+ export default function Page() {
25
+ return <>hi world!</>;
26
+ }
27
+ ```
28
+
29
+ - Run `npm run dev` to see the page in action in your browser.
30
+
31
+ - If you run `npm run eject`, dinou will be ejected and copied to your root project folder, so you can customize it.
32
+
33
+ - Or you can just run **`npx create-dinou@latest my-app`** to have an app ready to be developed with dinou.
package/cli.js ADDED
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { program } = require("commander");
4
+ const { execSync } = require("child_process");
5
+ const path = require("path");
6
+
7
+ const dinouPath = path.resolve(__dirname, "dinou");
8
+ const projectRoot = process.cwd();
9
+
10
+ const runCommand = (command, options = {}) => {
11
+ try {
12
+ execSync(command, { stdio: "inherit", cwd: projectRoot, ...options });
13
+ } catch (error) {
14
+ console.error(`Error executing "${command}":`, error.message);
15
+ process.exit(1);
16
+ }
17
+ };
18
+
19
+ program
20
+ .command("dev")
21
+ .description("Starts the server")
22
+ .action(() => {
23
+ console.log("Starting the server...");
24
+ runCommand(`node ${path.join(dinouPath, "ssg.js")}`);
25
+ runCommand(
26
+ `node --conditions react-server ${path.join(dinouPath, "server.js")}`
27
+ );
28
+ });
29
+
30
+ program
31
+ .command("build")
32
+ .description("Builds the app for production")
33
+ .action(() => {
34
+ console.log("Building the app...");
35
+ runCommand(`node ${path.join(dinouPath, "ssg.js")}`);
36
+ const configPath = path.join(__dirname, "webpack.config.js");
37
+ runCommand(
38
+ `cross-env NODE_ENV=production npx webpack --config ${configPath}`
39
+ );
40
+ });
41
+
42
+ program
43
+ .command("start")
44
+ .description("Start the app in production mode")
45
+ .action(() => {
46
+ console.log("Starting the app...");
47
+ runCommand(
48
+ `cross-env NODE_ENV=production node --conditions react-server ${path.join(
49
+ dinouPath,
50
+ "server.js"
51
+ )}`
52
+ );
53
+ });
54
+
55
+ program
56
+ .command("eject")
57
+ .description("Copy the framework config to the root of the project")
58
+ .action(() => {
59
+ console.log("Executing eject...");
60
+ require("./eject");
61
+ });
62
+
63
+ program.parse(process.argv);
@@ -0,0 +1,459 @@
1
+ const path = require("path");
2
+ const {
3
+ existsSync,
4
+ readdirSync,
5
+ mkdirSync,
6
+ writeFileSync,
7
+ rmSync,
8
+ } = require("fs");
9
+ const React = require("react");
10
+ const { asyncRenderJSXToClientJSX } = require("./render-jsx-to-client-jsx");
11
+ const {
12
+ getFilePathAndDynamicParams,
13
+ } = require("./get-file-path-and-dynamic-params");
14
+
15
+ async function buildStaticPages() {
16
+ const srcFolder = path.resolve(process.cwd(), "src");
17
+ const distFolder = path.resolve(process.cwd(), "dist");
18
+
19
+ if (existsSync(distFolder)) {
20
+ rmSync(distFolder, { recursive: true, force: true });
21
+ console.log("Deleted existing dist folder");
22
+ }
23
+
24
+ if (!existsSync(distFolder)) {
25
+ mkdirSync(distFolder, { recursive: true });
26
+ }
27
+
28
+ function collectPages(currentPath, segments = []) {
29
+ const entries = readdirSync(currentPath, { withFileTypes: true });
30
+ const pages = [];
31
+
32
+ for (const entry of entries) {
33
+ if (entry.isDirectory()) {
34
+ if (entry.name.startsWith("(") && entry.name.endsWith(")")) {
35
+ pages.push(
36
+ ...collectPages(path.join(currentPath, entry.name), segments)
37
+ );
38
+ } else if (
39
+ entry.name.startsWith("[[...") &&
40
+ entry.name.endsWith("]]")
41
+ ) {
42
+ // Optional catch-all
43
+ const paramName = entry.name.slice(5, -2);
44
+ const dynamicPath = path.join(currentPath, entry.name);
45
+ const [pagePath] = getFilePathAndDynamicParams(
46
+ segments,
47
+ {},
48
+ dynamicPath,
49
+ "page",
50
+ true,
51
+ true,
52
+ undefined,
53
+ segments.length
54
+ );
55
+ const [pageFunctionsPath] = getFilePathAndDynamicParams(
56
+ segments,
57
+ {},
58
+ dynamicPath,
59
+ "page_functions",
60
+ true,
61
+ true,
62
+ undefined,
63
+ segments.length
64
+ );
65
+ let dynamic;
66
+ let getStaticPaths;
67
+ if (pageFunctionsPath) {
68
+ const module = require(pageFunctionsPath);
69
+ getStaticPaths = module.getStaticPaths;
70
+ dynamic = module.dynamic;
71
+ }
72
+ if (pagePath && !dynamic?.()) {
73
+ console.log(
74
+ `Found optional catch-all route: ${
75
+ segments.join("/") ?? ""
76
+ }/[${paramName}]`
77
+ );
78
+ try {
79
+ if (getStaticPaths) {
80
+ const paths = getStaticPaths();
81
+ for (const path of paths) {
82
+ pages.push({
83
+ path: dynamicPath,
84
+ segments: [...segments, ...path],
85
+ params: { [paramName]: path },
86
+ });
87
+ }
88
+ }
89
+ } catch (err) {
90
+ console.error(`Error loading ${pagePath}:`, err);
91
+ }
92
+ }
93
+ } else if (entry.name.startsWith("[...") && entry.name.endsWith("]")) {
94
+ const paramName = entry.name.slice(4, -1);
95
+ const dynamicPath = path.join(currentPath, entry.name);
96
+ const [pagePath] = getFilePathAndDynamicParams(
97
+ segments,
98
+ {},
99
+ dynamicPath,
100
+ "page",
101
+ true,
102
+ true,
103
+ undefined,
104
+ segments.length
105
+ );
106
+ const [pageFunctionsPath] = getFilePathAndDynamicParams(
107
+ segments,
108
+ {},
109
+ dynamicPath,
110
+ "page_functions",
111
+ true,
112
+ true,
113
+ undefined,
114
+ segments.length
115
+ );
116
+ let dynamic;
117
+ let getStaticPaths;
118
+ if (pageFunctionsPath) {
119
+ const module = require(pageFunctionsPath);
120
+ getStaticPaths = module.getStaticPaths;
121
+ dynamic = module.dynamic;
122
+ }
123
+ if (pagePath && !dynamic?.()) {
124
+ console.log(
125
+ `Found catch-all route: ${
126
+ segments.join("/") ?? ""
127
+ }/[${paramName}]`
128
+ );
129
+ try {
130
+ if (getStaticPaths) {
131
+ const paths = getStaticPaths();
132
+ for (const path of paths) {
133
+ pages.push({
134
+ path: dynamicPath,
135
+ segments: [...segments, ...path],
136
+ params: { [paramName]: path },
137
+ });
138
+ }
139
+ }
140
+ } catch (err) {
141
+ console.error(`Error loading ${pagePath}:`, err);
142
+ }
143
+ }
144
+ } else if (entry.name.startsWith("[[") && entry.name.endsWith("]]")) {
145
+ // Optional dynamic param
146
+ const paramName = entry.name.slice(2, -2);
147
+ const dynamicPath = path.join(currentPath, entry.name);
148
+ const [pagePath] = getFilePathAndDynamicParams(
149
+ segments,
150
+ {},
151
+ dynamicPath,
152
+ "page",
153
+ true,
154
+ true,
155
+ undefined,
156
+ segments.length
157
+ );
158
+ const [pageFunctionsPath] = getFilePathAndDynamicParams(
159
+ segments,
160
+ {},
161
+ dynamicPath,
162
+ "page_functions",
163
+ true,
164
+ true,
165
+ undefined,
166
+ segments.length
167
+ );
168
+ let dynamic;
169
+ let getStaticPaths;
170
+ if (pageFunctionsPath) {
171
+ const module = require(pageFunctionsPath);
172
+ getStaticPaths = module.getStaticPaths;
173
+ dynamic = module.dynamic;
174
+ }
175
+ if (pagePath && !dynamic?.()) {
176
+ console.log(
177
+ `Found optional dynamic route: ${
178
+ segments.join("/") ?? ""
179
+ }/[${paramName}]`
180
+ );
181
+ try {
182
+ if (getStaticPaths) {
183
+ const paths = getStaticPaths();
184
+ for (const path of paths) {
185
+ pages.push({
186
+ path: dynamicPath,
187
+ segments: [...segments, path],
188
+ params: { [paramName]: path },
189
+ });
190
+ }
191
+ }
192
+ } catch (err) {
193
+ console.error(`Error loading ${pagePath}:`, err);
194
+ }
195
+ }
196
+ } else if (entry.name.startsWith("[") && entry.name.endsWith("]")) {
197
+ const paramName = entry.name.slice(1, -1);
198
+ const dynamicPath = path.join(currentPath, entry.name);
199
+ const [pagePath] = getFilePathAndDynamicParams(
200
+ segments,
201
+ {},
202
+ dynamicPath,
203
+ "page",
204
+ true,
205
+ true,
206
+ undefined,
207
+ segments.length
208
+ );
209
+ const [pageFunctionsPath] = getFilePathAndDynamicParams(
210
+ segments,
211
+ {},
212
+ dynamicPath,
213
+ "page_functions",
214
+ true,
215
+ true,
216
+ undefined,
217
+ segments.length
218
+ );
219
+ let dynamic;
220
+ let getStaticPaths;
221
+ if (pageFunctionsPath) {
222
+ const module = require(pageFunctionsPath);
223
+ getStaticPaths = module.getStaticPaths;
224
+ dynamic = module.dynamic;
225
+ }
226
+ if (pagePath && !dynamic?.()) {
227
+ console.log(
228
+ `Found dynamic route: ${segments.join("/") ?? ""}/[${paramName}]`
229
+ );
230
+ try {
231
+ if (getStaticPaths) {
232
+ const paths = getStaticPaths();
233
+ for (const path of paths) {
234
+ pages.push({
235
+ path: dynamicPath,
236
+ segments: [...segments, path],
237
+ params: { [paramName]: path },
238
+ });
239
+ }
240
+ }
241
+ } catch (err) {
242
+ console.error(`Error loading ${pagePath}:`, err);
243
+ }
244
+ }
245
+ } else if (!entry.name.startsWith("@")) {
246
+ pages.push(
247
+ ...collectPages(path.join(currentPath, entry.name), [
248
+ ...segments,
249
+ entry.name,
250
+ ])
251
+ );
252
+ }
253
+ }
254
+ }
255
+
256
+ const [pagePath, dParams] = getFilePathAndDynamicParams(
257
+ segments,
258
+ {},
259
+ currentPath,
260
+ "page",
261
+ true,
262
+ true,
263
+ undefined,
264
+ segments.length
265
+ );
266
+ const [pageFunctionsPath] = getFilePathAndDynamicParams(
267
+ segments,
268
+ {},
269
+ currentPath,
270
+ "page_functions",
271
+ true,
272
+ true,
273
+ undefined,
274
+ segments.length
275
+ );
276
+ let dynamic;
277
+ if (pageFunctionsPath) {
278
+ const module = require(pageFunctionsPath);
279
+ dynamic = module.dynamic;
280
+ }
281
+ if (pagePath && !dynamic?.()) {
282
+ pages.push({ path: currentPath, segments, params: dParams });
283
+ console.log(`Found static route: ${segments.join("/") || "/"}`);
284
+ }
285
+
286
+ return pages;
287
+ }
288
+
289
+ const pages = collectPages(srcFolder);
290
+
291
+ for await (const { path: folderPath, segments, params } of pages) {
292
+ try {
293
+ const [pagePath] = getFilePathAndDynamicParams(
294
+ segments,
295
+ {},
296
+ folderPath,
297
+ "page",
298
+ true,
299
+ true,
300
+ undefined,
301
+ segments.length
302
+ );
303
+ const pageModule = require(pagePath);
304
+ const Page = pageModule.default ?? pageModule;
305
+ // Set displayName for better serialization
306
+ // if (!Page.displayName) Page.displayName = "Page";
307
+
308
+ let props = { params, query: {} };
309
+
310
+ const [pageFunctionsPath] = getFilePathAndDynamicParams(
311
+ segments,
312
+ {},
313
+ folderPath,
314
+ "page_functions",
315
+ true,
316
+ true,
317
+ undefined,
318
+ segments.length
319
+ );
320
+
321
+ let pageFunctionsProps;
322
+
323
+ if (pageFunctionsPath) {
324
+ const pageFunctionsModule = require(pageFunctionsPath);
325
+ const getProps = pageFunctionsModule.getProps;
326
+ pageFunctionsProps = await getProps?.(params);
327
+ props = { ...props, ...(pageFunctionsProps?.page ?? {}) };
328
+ }
329
+
330
+ let jsx = React.createElement(Page, props);
331
+ jsx = { ...jsx, __modulePath: pagePath };
332
+
333
+ const noLayoutPath = path.join(folderPath, "no_layout");
334
+ if (!existsSync(noLayoutPath)) {
335
+ const layouts = getFilePathAndDynamicParams(
336
+ segments,
337
+ {},
338
+ srcFolder,
339
+ "layout",
340
+ true,
341
+ false,
342
+ undefined,
343
+ 0,
344
+ {},
345
+ true
346
+ );
347
+
348
+ if (layouts && Array.isArray(layouts)) {
349
+ let index = 0;
350
+ for (const [layoutPath, dParams, slots] of layouts.reverse()) {
351
+ const layoutModule = require(layoutPath);
352
+ const Layout = layoutModule.default ?? layoutModule;
353
+ // if (!Layout.displayName) Layout.displayName = "Layout";
354
+ const updatedSlots = {};
355
+ for (const [slotName, slotElement] of Object.entries(slots)) {
356
+ const slotFolder = path.join(
357
+ layoutPath.split("\\").slice(0, -1).join("\\"),
358
+ `@${slotName}`
359
+ );
360
+ const [slotPath] = getFilePathAndDynamicParams(
361
+ segments,
362
+ {},
363
+ slotFolder,
364
+ "page",
365
+ true,
366
+ true,
367
+ undefined,
368
+ segments.length
369
+ );
370
+ const updatedSlotElement = {
371
+ ...slotElement,
372
+ __modulePath: slotPath ?? null,
373
+ };
374
+ updatedSlots[slotName] = updatedSlotElement;
375
+ }
376
+ let props = { params: dParams, query: {}, ...updatedSlots };
377
+ if (index === layouts.length - 1) {
378
+ props = { ...props, ...(pageFunctionsProps?.layout ?? {}) };
379
+ }
380
+ jsx = React.createElement(Layout, props, jsx);
381
+ jsx = { ...jsx, __modulePath: layoutPath };
382
+ index++;
383
+ }
384
+ }
385
+ }
386
+
387
+ jsx = await asyncRenderJSXToClientJSX(jsx);
388
+
389
+ const outputPath = path.join(distFolder, segments.join("/"));
390
+ mkdirSync(outputPath, { recursive: true });
391
+
392
+ const jsonPath = path.join(outputPath, "index.json");
393
+
394
+ writeFileSync(jsonPath, JSON.stringify(serializeReactElement(jsx)));
395
+
396
+ console.log(
397
+ `Generated static page at ${path.relative(process.cwd(), jsonPath)}`
398
+ );
399
+ } catch (err) {
400
+ console.error(`Error building page ${segments.join("/")}:`, err);
401
+ continue;
402
+ }
403
+ }
404
+
405
+ console.log(`Static site generated with ${pages.length} pages`);
406
+ }
407
+
408
+ function filterProps(props_) {
409
+ if (React.isValidElement(props_)) {
410
+ return serializeReactElement(props_);
411
+ }
412
+ if (Array.isArray(props_)) {
413
+ return props_.map((item) => filterProps(item));
414
+ }
415
+ if (props_ && typeof props_ === "object") {
416
+ const props = {};
417
+ for (const [key, value] of Object.entries(props_)) {
418
+ if (!key.startsWith("_")) {
419
+ props[key] = filterProps(value);
420
+ }
421
+ }
422
+ return props;
423
+ }
424
+ return props_;
425
+ }
426
+
427
+ function serializeReactElement(element) {
428
+ if (React.isValidElement(element)) {
429
+ let type;
430
+ if (typeof element.type === "string") {
431
+ type = element.type; // HTML elements (e.g., "html", "div")
432
+ } else if (element.type === Symbol.for("react.fragment")) {
433
+ type = "Fragment";
434
+ } else if (typeof element.type === "function") {
435
+ type = "__clientComponent__";
436
+ } else {
437
+ type = element.type.displayName ?? element.type.name ?? "Unknown";
438
+ }
439
+ const modulePath = element.__modulePath;
440
+ return {
441
+ type,
442
+ modulePath: modulePath ? path.relative(process.cwd(), modulePath) : null,
443
+ props: {
444
+ ...filterProps(element.props),
445
+ children: Array.isArray(element.props.children)
446
+ ? element.props.children.map((child) => serializeReactElement(child))
447
+ : element.props.children
448
+ ? serializeReactElement(element.props.children)
449
+ : undefined,
450
+ },
451
+ };
452
+ }
453
+ // Handle non-React elements (strings, numbers, null)
454
+ return element;
455
+ }
456
+
457
+ module.exports = {
458
+ buildStaticPages,
459
+ };
@@ -0,0 +1,23 @@
1
+ import { use } from "react";
2
+ import { createFromFetch } from "react-server-dom-webpack/client";
3
+ import { hydrateRoot } from "react-dom/client";
4
+
5
+ const cache = new Map();
6
+ const route = window.location.href.replace(window.location.origin, "");
7
+
8
+ function Root() {
9
+ let content = cache.get(route);
10
+ if (!content) {
11
+ content = createFromFetch(fetch("/____rsc_payload____" + route));
12
+ cache.set(route, content);
13
+ }
14
+
15
+ return use(content);
16
+ }
17
+
18
+ hydrateRoot(document, <Root />);
19
+
20
+ // HMR
21
+ if (import.meta.hot) {
22
+ import.meta.hot.accept();
23
+ }