create-rasti 0.1.0-alpha.2 → 0.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/README.md +4 -2
- package/extras/micro-router/README.md +27 -5
- package/extras/micro-router/src/index.js +103 -28
- package/extras/rasti-icons/README.md +4 -4
- package/extras/rasti-icons/bin/rasti-icons.js +1 -1
- package/extras/rasti-icons/src/generate.js +44 -16
- package/package.json +7 -7
- package/src/apply/router.js +2 -1
- package/src/cli.js +16 -15
- package/src/utils/template.js +8 -1
- package/src/versions.js +5 -5
- package/templates/AGENTS.md +6 -1
- package/templates/_base/App-cssfun.js +18 -22
- package/templates/_base/App-cssfun.ts +22 -26
- package/templates/_base/App-tailwind.js +18 -22
- package/templates/_base/App-tailwind.ts +22 -26
- package/templates/_base/App.js +18 -22
- package/templates/_base/App.ts +22 -26
- package/templates/_extras/router/router-setup.js +77 -9
- package/templates/_extras/router/router-setup.ts +74 -9
- package/templates/spa/_gitignore +21 -0
- package/templates/spa/src/main.js +8 -1
- package/templates/spa/src/main.ts +8 -1
- package/templates/ssr/_gitignore +21 -0
- package/templates/ssr/app.js +4 -3
- package/templates/ssr/src/entry-client.js +9 -2
- package/templates/ssr/src/entry-client.ts +8 -4
- package/templates/ssr/src/entry-server.js +21 -8
- package/templates/ssr/src/entry-server.ts +21 -9
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { Model } from 'rasti';
|
|
1
2
|
import createRouter from './lib/router.js';
|
|
2
|
-
import type { Location, Router } from './lib/router.js';
|
|
3
|
+
import type { ActionContext, Location, Router } from './lib/router.js';
|
|
3
4
|
|
|
4
5
|
const DEFAULT_TITLE = '{{NAME}}';
|
|
5
6
|
|
|
@@ -33,20 +34,84 @@ export function getTitleForUrl(url: string): string {
|
|
|
33
34
|
return getTitleForPath(pathname);
|
|
34
35
|
}
|
|
35
36
|
|
|
37
|
+
/** A page kept in a chunk of its own. `Component` is `null` until `load` imports it. */
|
|
38
|
+
interface LazyPage<T> {
|
|
39
|
+
Component: T | null;
|
|
40
|
+
load(): Promise<T>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
{{#if SSR}}
|
|
44
|
+
/** Every page `lazy` created, for `loadPages`. */
|
|
45
|
+
const lazyPages: LazyPage<unknown>[] = [];
|
|
46
|
+
|
|
47
|
+
{{#endif}}
|
|
48
|
+
/**
|
|
49
|
+
* Wraps the import of a page module.
|
|
50
|
+
* @param importModule - Imports the page module, e.g. `() => import('./components/About.ts')`.
|
|
51
|
+
*/
|
|
52
|
+
const lazy = <T>(importModule: () => Promise<{ default: T }>): LazyPage<T> => {
|
|
53
|
+
const page: LazyPage<T> = {
|
|
54
|
+
Component : null,
|
|
55
|
+
async load() {
|
|
56
|
+
page.Component ??= (await importModule()).default;
|
|
57
|
+
return page.Component;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
{{#if SSR}}
|
|
61
|
+
lazyPages.push(page);
|
|
62
|
+
{{#endif}}
|
|
63
|
+
return page;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** About page, loaded by its route on the first visit. */
|
|
67
|
+
export const About = lazy(() => import('./components/About.ts'));
|
|
68
|
+
{{#if SSR}}
|
|
69
|
+
|
|
36
70
|
/**
|
|
37
|
-
*
|
|
71
|
+
* Load every page kept in a chunk of its own. The server awaits it before its first render, so
|
|
72
|
+
* no request waits for a chunk{{#if CSSFUN}}, and every page embeds the style sheets of all of them,
|
|
73
|
+
* whatever pages earlier requests loaded: the browser finds them in the page and attaches none{{#endif}}.
|
|
74
|
+
*/
|
|
75
|
+
export async function loadPages(): Promise<void> {
|
|
76
|
+
await Promise.all(lazyPages.map((page) => page.load()));
|
|
77
|
+
}
|
|
78
|
+
{{#endif}}
|
|
79
|
+
|
|
80
|
+
interface RouteAttributes {
|
|
81
|
+
/** Location of the page shown, `null` for a URL no route matches. */
|
|
82
|
+
location: Location | null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Model the router updates and `App` renders. */
|
|
86
|
+
export class RouteModel extends Model<RouteAttributes> {}
|
|
87
|
+
export interface RouteModel extends RouteAttributes {} // exposes the attributes on the model
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Route `url` before mounting `App`: create the model it renders and the router that
|
|
91
|
+
* updates it, and navigate to `url`, which loads the chunk of its page. Rendering is
|
|
92
|
+
* synchronous, so await it before rendering on the server or hydrating in the browser.
|
|
38
93
|
* Updates document.title on client when route changes.
|
|
39
|
-
* @param
|
|
40
|
-
* @returns
|
|
94
|
+
* @param url - Path and query.
|
|
95
|
+
* @returns The `model` and `router` options for `App`.
|
|
41
96
|
*/
|
|
42
|
-
export function
|
|
43
|
-
const
|
|
97
|
+
export async function route(url: string): Promise<{ model: RouteModel, router: Router }> {
|
|
98
|
+
const model = new RouteModel({ location : null });
|
|
99
|
+
|
|
100
|
+
/** Action that shows a page, once its chunk (if any) is loaded. */
|
|
101
|
+
const show = (page?: LazyPage<unknown>) => async (location: Location, { signal }: ActionContext) => {
|
|
102
|
+
await page?.load();
|
|
103
|
+
// A newer navigation took over while the chunk loaded.
|
|
104
|
+
if (signal.aborted) return;
|
|
44
105
|
model.location = location;
|
|
45
106
|
if (typeof document !== 'undefined') document.title = getTitleForPath(location.path);
|
|
46
107
|
};
|
|
47
108
|
|
|
48
|
-
|
|
49
|
-
{ path : '/', action },
|
|
50
|
-
{ path : '{{ROUTE_ABOUT}}', action }
|
|
109
|
+
const router = createRouter([
|
|
110
|
+
{ path : '/', action : show() },
|
|
111
|
+
{ path : '{{ROUTE_ABOUT}}', action : show(About) }
|
|
51
112
|
]);
|
|
113
|
+
|
|
114
|
+
await router.navigate(url, { addToHistory : false });
|
|
115
|
+
|
|
116
|
+
return { model, router };
|
|
52
117
|
}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import App from './App.js';
|
|
2
|
+
{{#if ROUTER}}
|
|
3
|
+
import { route } from './router-setup.js';
|
|
4
|
+
{{#endif}}
|
|
2
5
|
{{#if CSSFUN}}
|
|
3
6
|
import { theme } from './theme.js';
|
|
4
7
|
|
|
@@ -8,7 +11,11 @@ import './style.css';
|
|
|
8
11
|
{{#endif}}
|
|
9
12
|
|
|
10
13
|
{{#if ROUTER}}
|
|
11
|
-
|
|
14
|
+
// Routing loads the chunk of the page, which `App` renders on mount.
|
|
15
|
+
route(window.location.pathname + window.location.search).then(
|
|
16
|
+
({ model, router }) => App.mount({ model, router }, document.querySelector('#app')),
|
|
17
|
+
(error) => console.error('Could not load the page', error)
|
|
18
|
+
);
|
|
12
19
|
{{#else}}
|
|
13
20
|
App.mount({}, document.querySelector('#app'));
|
|
14
21
|
{{#endif}}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import App from './App.ts';
|
|
2
|
+
{{#if ROUTER}}
|
|
3
|
+
import { route } from './router-setup.ts';
|
|
4
|
+
{{#endif}}
|
|
2
5
|
{{#if CSSFUN}}
|
|
3
6
|
import { theme } from './theme.js';
|
|
4
7
|
|
|
@@ -8,7 +11,11 @@ import './style.css';
|
|
|
8
11
|
{{#endif}}
|
|
9
12
|
|
|
10
13
|
{{#if ROUTER}}
|
|
11
|
-
|
|
14
|
+
// Routing loads the chunk of the page, which `App` renders on mount.
|
|
15
|
+
route(window.location.pathname + window.location.search).then(
|
|
16
|
+
({ model, router }) => App.mount({ model, router }, document.querySelector('#app')!),
|
|
17
|
+
(error) => console.error('Could not load the page', error)
|
|
18
|
+
);
|
|
12
19
|
{{#else}}
|
|
13
20
|
App.mount({}, document.querySelector('#app')!);
|
|
14
21
|
{{#endif}}
|
package/templates/ssr/app.js
CHANGED
|
@@ -41,8 +41,9 @@ app.use('*all', async (req, res) => {
|
|
|
41
41
|
render = (await vite.ssrLoadModule('/src/entry-server.{{SRC_EXT}}')).render;
|
|
42
42
|
} else {
|
|
43
43
|
template = templateHtml;
|
|
44
|
-
//
|
|
45
|
-
|
|
44
|
+
// Built by `vite build --ssr`. A path in a variable keeps `tsc` from type-checking the bundle.
|
|
45
|
+
const serverEntry = './dist/server/entry-server.js';
|
|
46
|
+
render = (await import(serverEntry)).render;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
const rendered = await render(url);
|
|
@@ -58,7 +59,7 @@ app.use('*all', async (req, res) => {
|
|
|
58
59
|
html = html.replace(/<title>[\s\S]*?<\/title>/, '<title>' + rendered.title + '</title>');
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
res.status(200).set({ 'Content-Type' : 'text/html' }).send(html);
|
|
62
|
+
res.status(rendered.status ?? 200).set({ 'Content-Type' : 'text/html' }).send(html);
|
|
62
63
|
} catch (error) {
|
|
63
64
|
const e = /** @type {Error} */ (error);
|
|
64
65
|
vite?.ssrFixStacktrace(e);
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import App from './App.js';
|
|
2
|
+
{{#if ROUTER}}
|
|
3
|
+
import { route } from './router-setup.js';
|
|
4
|
+
{{#endif}}
|
|
2
5
|
{{#if CSSFUN}}
|
|
3
6
|
import { theme } from './theme.js';
|
|
4
7
|
|
|
@@ -9,8 +12,12 @@ import './style.css';
|
|
|
9
12
|
|
|
10
13
|
// The third argument `true` hydrates the server-rendered HTML instead of rendering it again.
|
|
11
14
|
{{#if ROUTER}}
|
|
12
|
-
// The server writes the options it rendered with, so the client
|
|
13
|
-
|
|
15
|
+
// The server writes the options it rendered with, so the client routes the same URL. Routing
|
|
16
|
+
// loads the chunk of the page, which hydration needs to render the markup the server sent.
|
|
17
|
+
route(window.__APP_OPTIONS__.url).then(
|
|
18
|
+
({ model, router }) => App.mount({ model, router }, document.querySelector('#app'), true),
|
|
19
|
+
(error) => console.error('Could not load the page to hydrate it', error)
|
|
20
|
+
);
|
|
14
21
|
{{#else}}
|
|
15
22
|
App.mount({}, document.querySelector('#app'), true);
|
|
16
23
|
{{#endif}}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import App from './App.ts';
|
|
2
2
|
{{#if ROUTER}}
|
|
3
|
-
import
|
|
3
|
+
import { route } from './router-setup.ts';
|
|
4
4
|
{{#endif}}
|
|
5
5
|
{{#if CSSFUN}}
|
|
6
6
|
import { theme } from './theme.js';
|
|
@@ -14,15 +14,19 @@ import './style.css';
|
|
|
14
14
|
declare global {
|
|
15
15
|
interface Window {
|
|
16
16
|
/** The options the server rendered `App` with, written into the page by `entry-server`. */
|
|
17
|
-
__APP_OPTIONS__:
|
|
17
|
+
__APP_OPTIONS__: { url: string };
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
{{#endif}}
|
|
21
21
|
|
|
22
22
|
// The third argument `true` hydrates the server-rendered HTML instead of rendering it again.
|
|
23
23
|
{{#if ROUTER}}
|
|
24
|
-
// The server writes the options it rendered with, so the client
|
|
25
|
-
|
|
24
|
+
// The server writes the options it rendered with, so the client routes the same URL. Routing
|
|
25
|
+
// loads the chunk of the page, which hydration needs to render the markup the server sent.
|
|
26
|
+
route(window.__APP_OPTIONS__.url).then(
|
|
27
|
+
({ model, router }) => App.mount({ model, router }, document.querySelector('#app')!, true),
|
|
28
|
+
(error) => console.error('Could not load the page to hydrate it', error)
|
|
29
|
+
);
|
|
26
30
|
{{#else}}
|
|
27
31
|
App.mount({}, document.querySelector('#app')!, true);
|
|
28
32
|
{{#endif}}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Component } from 'rasti';
|
|
2
2
|
import App from './App.js';
|
|
3
3
|
{{#if ROUTER}}
|
|
4
|
-
import { getTitleForUrl } from './router-setup.js';
|
|
4
|
+
import { route, getTitleForUrl, loadPages } from './router-setup.js';
|
|
5
5
|
{{#endif}}
|
|
6
6
|
{{#if CSSFUN}}
|
|
7
7
|
import { theme } from './theme.js';
|
|
@@ -9,10 +9,13 @@ import { StyleSheet } from 'cssfun';
|
|
|
9
9
|
{{#endif}}
|
|
10
10
|
|
|
11
11
|
{{#if ROUTER}}
|
|
12
|
+
// Every page is in memory before the first render.
|
|
13
|
+
await loadPages();
|
|
14
|
+
|
|
12
15
|
/**
|
|
13
16
|
* Serializes the render options for an inline `<script>`, as unicode escapes for the
|
|
14
17
|
* characters that could close the tag (`</script>`) or break older JavaScript parsers.
|
|
15
|
-
* @param {
|
|
18
|
+
* @param {{ url: string }} options - Per-request options.
|
|
16
19
|
* @returns {string} JSON safe to embed into an inline script.
|
|
17
20
|
*/
|
|
18
21
|
function serializeOptions(options) {
|
|
@@ -27,21 +30,31 @@ function serializeOptions(options) {
|
|
|
27
30
|
* Server-side render function.
|
|
28
31
|
* Returns the pieces `app.js` injects into the `index.html` template.
|
|
29
32
|
* @param {string} {{#if ROUTER}}url{{#else}}_url{{#endif}} - Request URL
|
|
30
|
-
|
|
33
|
+
{{#if ROUTER}}
|
|
34
|
+
* @returns {Promise<{ html: string, head?: string, title?: string, bodyClassName?: string, status?: number }>}
|
|
35
|
+
{{#else}}
|
|
36
|
+
* @returns {{ html: string, head?: string, title?: string, bodyClassName?: string, status?: number }}
|
|
37
|
+
{{#endif}}
|
|
31
38
|
*/
|
|
32
|
-
export function render({{#if ROUTER}}url{{#else}}_url{{#endif}}) {
|
|
39
|
+
export {{#if ROUTER}}async {{#endif}}function render({{#if ROUTER}}url{{#else}}_url{{#endif}}) {
|
|
40
|
+
{{#if ROUTER}}
|
|
41
|
+
// Rendering is synchronous, so it waits for routing to set the page.
|
|
42
|
+
const { model, router } = await route(url);
|
|
43
|
+
{{#endif}}
|
|
33
44
|
// Hydration matches elements by uid, so every request numbers them from the start.
|
|
34
45
|
Component.resetUid();
|
|
35
46
|
{{#if ROUTER}}
|
|
47
|
+
const html = new App({ model, router }).toString();
|
|
48
|
+
// `location` is null for a URL no route matches.
|
|
49
|
+
const status = model.location ? 200 : 404;
|
|
36
50
|
const options = { url };
|
|
37
|
-
const html = new App(options).toString();
|
|
38
51
|
// The client hydrates with the same options.
|
|
39
52
|
const script = `<script>window.__APP_OPTIONS__=${serializeOptions(options)}</script>`;
|
|
40
53
|
{{#if CSSFUN}}
|
|
41
|
-
// <style> tags for every style sheet the app modules registered on import, theme included.
|
|
42
|
-
return { html, head : StyleSheet.toString() + script, title : getTitleForUrl(url), bodyClassName : theme.classes.root };
|
|
54
|
+
// <style> tags for every style sheet the app modules registered on import, theme and every page included.
|
|
55
|
+
return { html, head : StyleSheet.toString() + script, title : getTitleForUrl(url), bodyClassName : theme.classes.root, status };
|
|
43
56
|
{{#else}}
|
|
44
|
-
return { html, head : script, title : getTitleForUrl(url) };
|
|
57
|
+
return { html, head : script, title : getTitleForUrl(url), status };
|
|
45
58
|
{{#endif}}
|
|
46
59
|
{{#else}}
|
|
47
60
|
const html = new App().toString();
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { Component } from 'rasti';
|
|
2
2
|
import App from './App.ts';
|
|
3
3
|
{{#if ROUTER}}
|
|
4
|
-
import
|
|
5
|
-
import { getTitleForUrl } from './router-setup.ts';
|
|
4
|
+
import { route, getTitleForUrl, loadPages } from './router-setup.ts';
|
|
6
5
|
{{#endif}}
|
|
7
6
|
{{#if CSSFUN}}
|
|
8
7
|
import { theme } from './theme.js';
|
|
9
8
|
import { StyleSheet } from 'cssfun';
|
|
10
9
|
{{#endif}}
|
|
10
|
+
{{#if ROUTER}}
|
|
11
|
+
|
|
12
|
+
// Every page is in memory before the first render.
|
|
13
|
+
await loadPages();
|
|
14
|
+
{{#endif}}
|
|
11
15
|
|
|
12
16
|
/** The pieces `app.js` injects into the `index.html` template. */
|
|
13
17
|
export interface RenderResult {
|
|
@@ -15,6 +19,8 @@ export interface RenderResult {
|
|
|
15
19
|
head?: string;
|
|
16
20
|
title?: string;
|
|
17
21
|
bodyClassName?: string;
|
|
22
|
+
/** HTTP status of the response, 200 when left out. */
|
|
23
|
+
status?: number;
|
|
18
24
|
}
|
|
19
25
|
|
|
20
26
|
{{#if ROUTER}}
|
|
@@ -24,7 +30,7 @@ export interface RenderResult {
|
|
|
24
30
|
* @param options - Per-request options.
|
|
25
31
|
* @returns JSON safe to embed into an inline script.
|
|
26
32
|
*/
|
|
27
|
-
function serializeOptions(options:
|
|
33
|
+
function serializeOptions(options: { url: string }): string {
|
|
28
34
|
return JSON.stringify(options).replace(
|
|
29
35
|
/[<>&\u2028\u2029]/g,
|
|
30
36
|
char => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`
|
|
@@ -36,19 +42,25 @@ function serializeOptions(options: AppProps): string {
|
|
|
36
42
|
* Server-side render function.
|
|
37
43
|
* @param {{#if ROUTER}}url{{#else}}_url{{#endif}} - Request URL
|
|
38
44
|
*/
|
|
39
|
-
export function render({{#if ROUTER}}url{{#else}}_url{{#endif}}: string): RenderResult {
|
|
45
|
+
export {{#if ROUTER}}async {{#endif}}function render({{#if ROUTER}}url{{#else}}_url{{#endif}}: string): {{#if ROUTER}}Promise<RenderResult>{{#else}}RenderResult{{#endif}} {
|
|
46
|
+
{{#if ROUTER}}
|
|
47
|
+
// Rendering is synchronous, so it waits for routing to set the page.
|
|
48
|
+
const { model, router } = await route(url);
|
|
49
|
+
{{#endif}}
|
|
40
50
|
// Hydration matches elements by uid, so every request numbers them from the start.
|
|
41
51
|
Component.resetUid();
|
|
42
52
|
{{#if ROUTER}}
|
|
43
|
-
const
|
|
44
|
-
|
|
53
|
+
const html = new App({ model, router }).toString();
|
|
54
|
+
// `location` is null for a URL no route matches.
|
|
55
|
+
const status = model.location ? 200 : 404;
|
|
56
|
+
const options = { url };
|
|
45
57
|
// The client hydrates with the same options.
|
|
46
58
|
const script = `<script>window.__APP_OPTIONS__=${serializeOptions(options)}</script>`;
|
|
47
59
|
{{#if CSSFUN}}
|
|
48
|
-
// <style> tags for every style sheet the app modules registered on import, theme included.
|
|
49
|
-
return { html, head : StyleSheet.toString() + script, title : getTitleForUrl(url), bodyClassName : theme.classes.root };
|
|
60
|
+
// <style> tags for every style sheet the app modules registered on import, theme and every page included.
|
|
61
|
+
return { html, head : StyleSheet.toString() + script, title : getTitleForUrl(url), bodyClassName : theme.classes.root, status };
|
|
50
62
|
{{#else}}
|
|
51
|
-
return { html, head : script, title : getTitleForUrl(url) };
|
|
63
|
+
return { html, head : script, title : getTitleForUrl(url), status };
|
|
52
64
|
{{#endif}}
|
|
53
65
|
{{#else}}
|
|
54
66
|
const html = new App().toString();
|