create-rasti 0.0.1 → 0.1.0-alpha.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.
Files changed (56) hide show
  1. package/README.md +21 -2
  2. package/extras/cn/src/index.js +14 -1
  3. package/extras/micro-router/README.md +35 -3
  4. package/extras/micro-router/package.json +1 -1
  5. package/extras/micro-router/src/index.js +124 -44
  6. package/extras/rasti-icons/README.md +15 -5
  7. package/extras/rasti-icons/src/generate.js +95 -49
  8. package/package.json +9 -6
  9. package/src/apply/agents.js +68 -0
  10. package/src/apply/base.js +1 -1
  11. package/src/apply/description.js +2 -0
  12. package/src/apply/featuresInclude.js +3 -2
  13. package/src/apply/index.js +13 -37
  14. package/src/apply/router.js +2 -2
  15. package/src/apply/ssr.js +1 -1
  16. package/src/apply/typescript.js +38 -0
  17. package/src/args.js +3 -0
  18. package/src/cli.js +2 -0
  19. package/src/plan.js +2 -1
  20. package/src/prompts.js +17 -0
  21. package/src/utils/template.js +32 -15
  22. package/src/versions.js +16 -12
  23. package/templates/AGENTS.md +31 -8
  24. package/templates/_base/App-cssfun.js +40 -23
  25. package/templates/_base/App-cssfun.ts +107 -0
  26. package/templates/_base/App-tailwind.js +39 -22
  27. package/templates/_base/App-tailwind.ts +77 -0
  28. package/templates/_base/App.js +39 -22
  29. package/templates/_base/App.ts +77 -0
  30. package/templates/_base/components/Button-cssfun.ts +54 -0
  31. package/templates/_base/components/Button-tailwind.ts +55 -0
  32. package/templates/_base/components/Button.ts +25 -0
  33. package/templates/_base/components/Header-cssfun.ts +71 -0
  34. package/templates/_base/components/Header-tailwind.ts +19 -0
  35. package/templates/_base/components/Header.ts +19 -0
  36. package/templates/_base/components/Home-cssfun.ts +101 -0
  37. package/templates/_base/components/Home-tailwind.ts +38 -0
  38. package/templates/_base/components/Home.ts +38 -0
  39. package/templates/_extras/router/components/About-cssfun.ts +45 -0
  40. package/templates/_extras/router/components/About-tailwind.ts +16 -0
  41. package/templates/_extras/router/components/About.ts +18 -0
  42. package/templates/_extras/router/router-setup.js +21 -21
  43. package/templates/_extras/router/router-setup.ts +52 -0
  44. package/templates/_features/cssfun/index.html +1 -1
  45. package/templates/_features/typescript/tsconfig.json +30 -0
  46. package/templates/spa/index.html +1 -1
  47. package/templates/spa/src/main.js +1 -2
  48. package/templates/spa/src/main.ts +14 -0
  49. package/templates/ssr/app.js +10 -10
  50. package/templates/ssr/index.html +1 -1
  51. package/templates/ssr/package.json +1 -1
  52. package/templates/ssr/src/entry-client.js +2 -1
  53. package/templates/ssr/src/entry-client.ts +28 -0
  54. package/templates/ssr/src/entry-server.js +31 -25
  55. package/templates/ssr/src/entry-server.ts +62 -0
  56. package/templates/static/scripts/build-static.js +11 -1
@@ -0,0 +1,19 @@
1
+ import { Component } from 'rasti';
2
+
3
+ /** App header: navigation (when router is enabled). */
4
+ class Header extends Component {
5
+ template() {
6
+ return this.html`
7
+ <header class="header">
8
+ {{#if ROUTER}}
9
+ <nav class="nav">
10
+ <a data-router href="/">Home</a>
11
+ <a data-router href="{{ROUTE_ABOUT}}">About</a>
12
+ </nav>
13
+ {{#endif}}
14
+ </header>
15
+ `;
16
+ }
17
+ }
18
+
19
+ export default Header;
@@ -0,0 +1,101 @@
1
+ import { Component } from 'rasti';
2
+ import { css } from 'cssfun';
3
+ import Button from './Button.ts';
4
+
5
+ const { classes } = css({
6
+ root : {
7
+ width : '100%',
8
+ display : 'flex',
9
+ flexDirection : 'column',
10
+ alignItems : 'center',
11
+ gap : 'clamp(24px, 6vw, 40px)'
12
+ },
13
+ title : { display : 'flex', alignItems : 'center', justifyContent : 'center', gap : '16px', fontSize : '36px', fontWeight : '700', margin : 0 },
14
+ logoLink : { display : 'inline-block' },
15
+ logoLight : { height : 'clamp(56px, 15vw, 96px)', display : 'var(--fun-showWhenLight)' },
16
+ logoDark : { height : 'clamp(56px, 15vw, 96px)', display : 'var(--fun-showWhenDark)' },
17
+ counter : {
18
+ display : 'flex',
19
+ alignItems : 'center',
20
+ justifyContent : 'center'
21
+ },
22
+ info : {
23
+ width : 'min(100%, 720px)',
24
+ padding : 'clamp(20px, 4vw, 30px)',
25
+ background : 'var(--fun-cardBg)',
26
+ borderRadius : '24px',
27
+ border : '1px solid var(--fun-cardBorder)',
28
+ boxShadow : 'var(--fun-cardInset), var(--fun-cardShadow)',
29
+ backdropFilter : 'blur(18px)',
30
+ '& code' : {
31
+ backgroundColor : 'var(--fun-codeBg)',
32
+ padding : '3px 10px',
33
+ borderRadius : '999px',
34
+ fontSize : '13px',
35
+ fontFamily : 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
36
+ fontWeight : '600',
37
+ border : '1px solid var(--fun-codeBorder)'
38
+ }
39
+ },
40
+ infoStack : {
41
+ display : 'grid',
42
+ gap : '16px'
43
+ },
44
+ infoDescription : {
45
+ margin : 0,
46
+ lineHeight : '1.7',
47
+ fontSize : '15px'
48
+ },
49
+ infoIncludes : {
50
+ padding : '16px 20px',
51
+ borderRadius : '16px',
52
+ background : 'var(--fun-metaBg)',
53
+ border : '1px solid var(--fun-metaBorder)',
54
+ '& p' : {
55
+ margin : 0,
56
+ lineHeight : '1.7'
57
+ },
58
+ '& p + p' : {
59
+ marginTop : '8px'
60
+ }
61
+ },
62
+ infoHint : {
63
+ margin : 0,
64
+ lineHeight : '1.7',
65
+ color : 'var(--fun-mutedText)'
66
+ }
67
+ });
68
+
69
+ export interface HomeProps {
70
+ /** Current counter value. */
71
+ count: number;
72
+ /** Increments the counter. */
73
+ handleIncrement: () => void;
74
+ }
75
+
76
+ /** Home view: logo, counter and project info. */
77
+ class Home extends Component<HomeProps> {
78
+ template() {
79
+ return this.html`
80
+ <div class="${classes.root}">
81
+ <h1 class="${classes.title}">
82
+ <a class="${classes.logoLink}" href="https://rasti.js.org" target="_blank"><img class="${classes.logoLight}" src="{{LOGO_LIGHT_SRC}}" alt="{{NAME}}" /><img class="${classes.logoDark}" src="{{LOGO_SRC}}" alt="{{NAME}}" /></a>
83
+ </h1>
84
+ <div class="${classes.counter}">
85
+ <${Button} handleClick=${this.props.handleIncrement}>
86
+ count is ${this.props.count}
87
+ </${Button}>
88
+ </div>
89
+ <div class="${classes.info}">
90
+ <div class="${classes.infoStack}">
91
+ <p class="${classes.infoDescription}">{{DESCRIPTION}}</p>
92
+ <div class="${classes.infoIncludes}">{{FEATURES_INCLUDE}}</div>
93
+ <p class="${classes.infoHint}">Edit <code>src/App.ts</code> and save to test HMR.</p>
94
+ </div>
95
+ </div>
96
+ </div>
97
+ `;
98
+ }
99
+ }
100
+
101
+ export default Home;
@@ -0,0 +1,38 @@
1
+ import { Component } from 'rasti';
2
+ import Button from './Button.ts';
3
+
4
+ export interface HomeProps {
5
+ /** Current counter value. */
6
+ count: number;
7
+ /** Increments the counter. */
8
+ handleIncrement: () => void;
9
+ }
10
+
11
+ /** Home view: logo, counter and project info. */
12
+ class Home extends Component<HomeProps> {
13
+ template() {
14
+ return this.html`
15
+ <div class="flex w-full flex-col items-center gap-6 md:gap-10">
16
+ <h1 class="flex items-center justify-center gap-4 text-4xl font-bold m-0">
17
+ <a class="inline-block" href="https://rasti.js.org" target="_blank">
18
+ <img class="h-14 md:h-24 block" src="{{LOGO_SRC}}" alt="{{NAME}}" />
19
+ </a>
20
+ </h1>
21
+ <div class="flex items-center">
22
+ <${Button} handleClick=${this.props.handleIncrement}>
23
+ count is ${this.props.count}
24
+ </${Button}>
25
+ </div>
26
+ <div class="w-full max-w-[720px] rounded-3xl border border-white/12 bg-linear-to-b from-[rgba(20,24,28,0.92)] to-[rgba(10,10,10,0.82)] p-5 md:p-[30px] shadow-[inset_0_1px_0_rgba(255,255,255,0.06),0_12px_40px_rgba(0,0,0,0.2)] backdrop-blur-[18px]">
27
+ <div class="grid gap-4">
28
+ <p class="m-0 text-[15px] leading-[1.7] text-white/88">{{DESCRIPTION}}</p>
29
+ <div class="rounded-2xl border border-white/6 bg-white/4 px-5 py-4 leading-[1.7] text-white/88 [&_p+p]:mt-2 [&_p]:m-0">{{FEATURES_INCLUDE}}</div>
30
+ <p class="m-0 leading-[1.7] text-white/58">Edit <code class="rounded-full border border-white/10 bg-white/10 px-2.5 py-0.5 text-[13px] font-semibold font-mono">src/App.ts</code> and save to test HMR.</p>
31
+ </div>
32
+ </div>
33
+ </div>
34
+ `;
35
+ }
36
+ }
37
+
38
+ export default Home;
@@ -0,0 +1,38 @@
1
+ import { Component } from 'rasti';
2
+ import Button from './Button.ts';
3
+
4
+ export interface HomeProps {
5
+ /** Current counter value. */
6
+ count: number;
7
+ /** Increments the counter. */
8
+ handleIncrement: () => void;
9
+ }
10
+
11
+ /** Home view: logo, counter and project info. */
12
+ class Home extends Component<HomeProps> {
13
+ template() {
14
+ return this.html`
15
+ <div class="home">
16
+ <h1 class="title">
17
+ <a class="logo-link" href="https://rasti.js.org" target="_blank">
18
+ <img class="logo" src="{{LOGO_SRC}}" alt="{{NAME}}" />
19
+ </a>
20
+ </h1>
21
+ <div class="counter">
22
+ <${Button} handleClick=${this.props.handleIncrement}>
23
+ count is ${this.props.count}
24
+ </${Button}>
25
+ </div>
26
+ <div class="info">
27
+ <div class="info-stack">
28
+ <p class="info-description">{{DESCRIPTION}}</p>
29
+ <div class="info-includes">{{FEATURES_INCLUDE}}</div>
30
+ <p class="info-hint">Edit <code>src/App.ts</code> and save to test HMR.</p>
31
+ </div>
32
+ </div>
33
+ </div>
34
+ `;
35
+ }
36
+ }
37
+
38
+ export default Home;
@@ -0,0 +1,45 @@
1
+ import { Component } from 'rasti';
2
+ import { css } from 'cssfun';
3
+
4
+ const { classes } = css({
5
+ root : {
6
+ width : '100%',
7
+ display : 'flex',
8
+ justifyContent : 'center'
9
+ },
10
+ info : {
11
+ width : 'min(100%, 720px)',
12
+ padding : 'clamp(20px, 4vw, 30px)',
13
+ background : 'var(--fun-cardBg)',
14
+ borderRadius : '24px',
15
+ border : '1px solid var(--fun-cardBorder)',
16
+ boxShadow : 'var(--fun-cardInset), var(--fun-cardShadow)',
17
+ backdropFilter : 'blur(18px)'
18
+ },
19
+ infoStack : {
20
+ display : 'grid',
21
+ gap : '16px'
22
+ },
23
+ infoDescription : {
24
+ margin : 0,
25
+ lineHeight : '1.7',
26
+ fontSize : '15px'
27
+ }
28
+ });
29
+
30
+ /** About page (cssfun). */
31
+ class About extends Component {
32
+ template() {
33
+ return this.html`
34
+ <div class="${classes.root}">
35
+ <div class="${classes.info}">
36
+ <div class="${classes.infoStack}">
37
+ <p class="${classes.infoDescription}">This is the About page. Add your content here.</p>
38
+ </div>
39
+ </div>
40
+ </div>
41
+ `;
42
+ }
43
+ }
44
+
45
+ export default About;
@@ -0,0 +1,16 @@
1
+ import { Component } from 'rasti';
2
+
3
+ /** About page. */
4
+ class About extends Component {
5
+ template() {
6
+ return this.html`
7
+ <div class="flex w-full justify-center">
8
+ <div class="w-full max-w-[720px] rounded-3xl border border-white/12 bg-linear-to-b from-[rgba(20,24,28,0.92)] to-[rgba(10,10,10,0.82)] p-5 md:p-[30px] shadow-[inset_0_1px_0_rgba(255,255,255,0.06),0_12px_40px_rgba(0,0,0,0.2)] backdrop-blur-[18px]">
9
+ <p class="m-0 text-[15px] leading-[1.7] text-white/88">This is the About page. Add your content here.</p>
10
+ </div>
11
+ </div>
12
+ `;
13
+ }
14
+ }
15
+
16
+ export default About;
@@ -0,0 +1,18 @@
1
+ import { Component } from 'rasti';
2
+
3
+ /** About page. */
4
+ class About extends Component {
5
+ template() {
6
+ return this.html`
7
+ <div class="page">
8
+ <div class="info">
9
+ <div class="info-stack">
10
+ <p class="info-description">This is the About page. Add your content here.</p>
11
+ </div>
12
+ </div>
13
+ </div>
14
+ `;
15
+ }
16
+ }
17
+
18
+ export default About;
@@ -2,13 +2,20 @@ import createRouter from './lib/router.js';
2
2
 
3
3
  const DEFAULT_TITLE = '{{NAME}}';
4
4
 
5
- /** Maps route paths to document titles. */
5
+ /**
6
+ * Maps route paths to document titles.
7
+ * @type {Object.<string, string>}
8
+ */
6
9
  const PAGE_TITLES = {
7
10
  '/' : 'Home | {{NAME}}',
8
11
  '/about' : 'About | {{NAME}}'
9
12
  };
10
13
 
11
- /** Normalize path for lookup (strip trailing slash; '' -> '/'). */
14
+ /**
15
+ * Normalize path for lookup (strip trailing slash; '' -> '/').
16
+ * @param {string} p
17
+ * @returns {string}
18
+ */
12
19
  function normalizePath(p) {
13
20
  return (p || '/').replace(/\/$/, '') || '/';
14
21
  }
@@ -36,25 +43,18 @@ export function getTitleForUrl(url) {
36
43
  /**
37
44
  * Create router bound to model (sets model.location on navigate).
38
45
  * Updates document.title on client when route changes.
39
- * @param {object} model - Model with location
40
- * @returns {object} Router with navigate, delegateNavigation, bindHistory, createUrl
46
+ * @param {{ location: (import('./lib/router.js').Location|null) }} model - Model with location
47
+ * @returns {import('./lib/router.js').Router} Router with navigate, delegateNavigation, bindHistory, createUrl
41
48
  */
42
49
  export function createAppRouter(model) {
43
- const routes = [
44
- {
45
- path : '/',
46
- action : (location) => {
47
- model.location = location;
48
- if (typeof document !== 'undefined') document.title = getTitleForPath(location.path);
49
- }
50
- },
51
- {
52
- path : '{{ROUTE_ABOUT}}',
53
- action : (location) => {
54
- model.location = location;
55
- if (typeof document !== 'undefined') document.title = getTitleForPath(location.path);
56
- }
57
- }
58
- ];
59
- return createRouter(routes, { baseUrl : '' });
50
+ /** @param {import('./lib/router.js').Location} location */
51
+ const action = (location) => {
52
+ model.location = location;
53
+ if (typeof document !== 'undefined') document.title = getTitleForPath(location.path);
54
+ };
55
+
56
+ return createRouter([
57
+ { path : '/', action },
58
+ { path : '{{ROUTE_ABOUT}}', action }
59
+ ]);
60
60
  }
@@ -0,0 +1,52 @@
1
+ import createRouter from './lib/router.js';
2
+ import type { Location, Router } from './lib/router.js';
3
+
4
+ const DEFAULT_TITLE = '{{NAME}}';
5
+
6
+ /** Maps route paths to document titles. */
7
+ const PAGE_TITLES: Record<string, string> = {
8
+ '/' : 'Home | {{NAME}}',
9
+ '/about' : 'About | {{NAME}}'
10
+ };
11
+
12
+ /** Normalize path for lookup (strip trailing slash; '' -> '/'). */
13
+ function normalizePath(p: string): string {
14
+ return (p || '/').replace(/\/$/, '') || '/';
15
+ }
16
+
17
+ /**
18
+ * Get document title for a route path.
19
+ * @param path - Route path (e.g. '/', '/about', '/about/')
20
+ * @returns Page title
21
+ */
22
+ export function getTitleForPath(path: string): string {
23
+ return PAGE_TITLES[normalizePath(path)] ?? DEFAULT_TITLE;
24
+ }
25
+
26
+ /**
27
+ * Get document title for a request URL (for SSR).
28
+ * @param url - Request URL
29
+ * @returns Page title
30
+ */
31
+ export function getTitleForUrl(url: string): string {
32
+ const pathname = url.replace(/\?.*$/, '').replace(/^https?:\/\/[^/]+/, '') || '/';
33
+ return getTitleForPath(pathname);
34
+ }
35
+
36
+ /**
37
+ * Create router bound to model (sets model.location on navigate).
38
+ * Updates document.title on client when route changes.
39
+ * @param model - Model with location
40
+ * @returns Router with navigate, delegateNavigation, bindHistory, createUrl
41
+ */
42
+ export function createAppRouter(model: { location: Location | null }): Router {
43
+ const action = (location: Location) => {
44
+ model.location = location;
45
+ if (typeof document !== 'undefined') document.title = getTitleForPath(location.path);
46
+ };
47
+
48
+ return createRouter([
49
+ { path : '/', action },
50
+ { path : '{{ROUTE_ABOUT}}', action }
51
+ ]);
52
+ }
@@ -9,6 +9,6 @@
9
9
  </head>
10
10
  <body>
11
11
  <div id="app"><!--app-html--></div>
12
- <script type="module" src="/src/entry-client.js"></script>
12
+ <script type="module" src="/src/entry-client.{{SRC_EXT}}"></script>
13
13
  </body>
14
14
  </html>
@@ -0,0 +1,30 @@
1
+ {
2
+ "compilerOptions" : {
3
+ "target" : "ES2022",
4
+ "module" : "ESNext",
5
+ "moduleResolution" : "bundler",
6
+ "lib" : ["ES2022", "DOM", "DOM.Iterable"],
7
+ {{#if SSR}}
8
+ "types" : ["vite/client", "node"],
9
+ {{#else}}
10
+ "types" : ["vite/client"],
11
+ {{#endif}}
12
+ "allowJs" : true,
13
+ "checkJs" : true,
14
+ "allowImportingTsExtensions" : true,
15
+ "verbatimModuleSyntax" : true,
16
+ "erasableSyntaxOnly" : true,
17
+ "moduleDetection" : "force",
18
+ "useDefineForClassFields" : true,
19
+ "skipLibCheck" : true,
20
+ "noEmit" : true,
21
+ "strict" : true
22
+ },
23
+ {{#if STATIC}}
24
+ "include" : ["src", "app.js", "server.js", "scripts", "static.config.js"]
25
+ {{#elif SSR}}
26
+ "include" : ["src", "app.js", "server.js"]
27
+ {{#else}}
28
+ "include" : ["src"]
29
+ {{#endif}}
30
+ }
@@ -8,7 +8,7 @@
8
8
  </head>
9
9
  <body>
10
10
  <div id="app"></div>
11
- <script type="module" src="/src/main.js"></script>
11
+ <script type="module" src="/src/main.{{SRC_EXT}}"></script>
12
12
  </body>
13
13
  </html>
14
14
 
@@ -8,8 +8,7 @@ import './style.css';
8
8
  {{#endif}}
9
9
 
10
10
  {{#if ROUTER}}
11
- const options = { url : window.location.pathname + window.location.search };
12
- App.mount(options, document.querySelector('#app'));
11
+ App.mount({ url : window.location.pathname + window.location.search }, document.querySelector('#app'));
13
12
  {{#else}}
14
13
  App.mount({}, document.querySelector('#app'));
15
14
  {{#endif}}
@@ -0,0 +1,14 @@
1
+ import App from './App.ts';
2
+ {{#if CSSFUN}}
3
+ import { theme } from './theme.js';
4
+
5
+ document.body.classList.add(theme.classes.root);
6
+ {{#else}}
7
+ import './style.css';
8
+ {{#endif}}
9
+
10
+ {{#if ROUTER}}
11
+ App.mount({ url : window.location.pathname + window.location.search }, document.querySelector('#app')!);
12
+ {{#else}}
13
+ App.mount({}, document.querySelector('#app')!);
14
+ {{#endif}}
@@ -10,7 +10,7 @@ const templateHtml = isProduction
10
10
 
11
11
  const app = express();
12
12
 
13
- /** @type {object|undefined} Vite dev server instance */
13
+ /** @type {import('vite').ViteDevServer|undefined} Vite dev server instance */
14
14
  let vite;
15
15
  if (!isProduction) {
16
16
  const { createServer } = await import('vite');
@@ -33,14 +33,15 @@ app.use('*all', async (req, res) => {
33
33
 
34
34
  /** @type {string} */
35
35
  let template;
36
- /** @type {Function} Server render function */
36
+ /** @type {typeof import('./src/entry-server.js').render} */
37
37
  let render;
38
- if (!isProduction) {
38
+ if (vite) {
39
39
  template = await fs.readFile('./index.html', 'utf-8');
40
40
  template = await vite.transformIndexHtml(url, template);
41
- render = (await vite.ssrLoadModule('/src/entry-server.js')).render;
41
+ render = (await vite.ssrLoadModule('/src/entry-server.{{SRC_EXT}}')).render;
42
42
  } else {
43
43
  template = templateHtml;
44
+ // @ts-ignore Built by `vite build --ssr`, so it only exists after a build.
44
45
  render = (await import('./dist/server/entry-server.js')).render;
45
46
  }
46
47
 
@@ -53,18 +54,17 @@ app.use('*all', async (req, res) => {
53
54
  if (rendered.bodyClassName) {
54
55
  html = html.replace('<body>', '<body class="' + rendered.bodyClassName + '">');
55
56
  }
56
- if (rendered.dataColorScheme) {
57
- html = html.replace('<html', '<html data-color-scheme="' + rendered.dataColorScheme + '"');
58
- }
59
57
  if (rendered.title) {
60
58
  html = html.replace(/<title>[\s\S]*?<\/title>/, '<title>' + rendered.title + '</title>');
61
59
  }
62
60
 
63
61
  res.status(200).set({ 'Content-Type' : 'text/html' }).send(html);
64
- } catch (e) {
62
+ } catch (error) {
63
+ const e = /** @type {Error} */ (error);
65
64
  vite?.ssrFixStacktrace(e);
66
- console.log(e.stack);
67
- res.status(500).end(e.stack);
65
+ console.error(e.stack);
66
+ // The stack helps in development but would expose server internals in production.
67
+ res.status(500).end(isProduction ? 'Internal Server Error' : e.stack);
68
68
  }
69
69
  });
70
70
 
@@ -10,7 +10,7 @@
10
10
  </head>
11
11
  <body>
12
12
  <div id="app"><!--app-html--></div>
13
- <script type="module" src="/src/entry-client.js"></script>
13
+ <script type="module" src="/src/entry-client.{{SRC_EXT}}"></script>
14
14
  </body>
15
15
  </html>
16
16
 
@@ -7,7 +7,7 @@
7
7
  "dev" : "node server",
8
8
  "build" : "npm run build:client && npm run build:server",
9
9
  "build:client" : "vite build --outDir dist/client",
10
- "build:server" : "vite build --ssr src/entry-server.js --outDir dist/server",
10
+ "build:server" : "vite build --ssr src/entry-server.{{SRC_EXT}} --outDir dist/server",
11
11
  "preview" : "cross-env NODE_ENV=production node server"
12
12
  },
13
13
  "dependencies" : {
@@ -7,8 +7,9 @@ document.body.classList.add(theme.classes.root);
7
7
  import './style.css';
8
8
  {{#endif}}
9
9
 
10
- // The third argument `true` enables hydration of server-rendered HTML.
10
+ // The third argument `true` hydrates the server-rendered HTML instead of rendering it again.
11
11
  {{#if ROUTER}}
12
+ // The server writes the options it rendered with, so the client hydrates from the same ones.
12
13
  App.mount(window.__APP_OPTIONS__, document.querySelector('#app'), true);
13
14
  {{#else}}
14
15
  App.mount({}, document.querySelector('#app'), true);
@@ -0,0 +1,28 @@
1
+ import App from './App.ts';
2
+ {{#if ROUTER}}
3
+ import type { AppProps } from './App.ts';
4
+ {{#endif}}
5
+ {{#if CSSFUN}}
6
+ import { theme } from './theme.js';
7
+
8
+ document.body.classList.add(theme.classes.root);
9
+ {{#else}}
10
+ import './style.css';
11
+ {{#endif}}
12
+ {{#if ROUTER}}
13
+
14
+ declare global {
15
+ interface Window {
16
+ /** The options the server rendered `App` with, written into the page by `entry-server`. */
17
+ __APP_OPTIONS__: AppProps;
18
+ }
19
+ }
20
+ {{#endif}}
21
+
22
+ // The third argument `true` hydrates the server-rendered HTML instead of rendering it again.
23
+ {{#if ROUTER}}
24
+ // The server writes the options it rendered with, so the client hydrates from the same ones.
25
+ App.mount(window.__APP_OPTIONS__, document.querySelector('#app')!, true);
26
+ {{#else}}
27
+ App.mount({}, document.querySelector('#app')!, true);
28
+ {{#endif}}
@@ -10,40 +10,46 @@ import { StyleSheet } from 'cssfun';
10
10
 
11
11
  {{#if ROUTER}}
12
12
  /**
13
- * SSR render: pass url so App onCreate runs router.navigate; inject __APP_OPTIONS__ and page title for client hydrate.
14
- * @param {string} url - Request URL
15
- * @returns {{ html: string, head?: string, bodyClassName?: string, title?: string }}
13
+ * Serializes the render options for an inline `<script>`, as unicode escapes for the
14
+ * characters that could close the tag (`</script>`) or break older JavaScript parsers.
15
+ * @param {Object} options - Per-request options.
16
+ * @returns {string} JSON safe to embed into an inline script.
16
17
  */
17
- export function render(url) {
18
+ function serializeOptions(options) {
19
+ return JSON.stringify(options).replace(
20
+ /[<>&\u2028\u2029]/g,
21
+ char => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`
22
+ );
23
+ }
24
+
25
+ {{#endif}}
26
+ /**
27
+ * Server-side render function.
28
+ * Returns the pieces `app.js` injects into the `index.html` template.
29
+ * @param {string} {{#if ROUTER}}url{{#else}}_url{{#endif}} - Request URL
30
+ * @returns {{ html: string, head?: string, title?: string, bodyClassName?: string }}
31
+ */
32
+ export function render({{#if ROUTER}}url{{#else}}_url{{#endif}}) {
33
+ // Hydration matches elements by uid, so every request numbers them from the start.
18
34
  Component.resetUid();
35
+ {{#if ROUTER}}
19
36
  const options = { url };
20
- const app = App.mount(options);
21
- const html = app.toString();
22
- const title = getTitleForUrl(url);
37
+ const html = new App(options).toString();
38
+ // The client hydrates with the same options.
39
+ const script = `<script>window.__APP_OPTIONS__=${serializeOptions(options)}</script>`;
23
40
  {{#if CSSFUN}}
24
- const styles = StyleSheet.toString();
25
- const script = `<script>window.__APP_OPTIONS__=${JSON.stringify(options)}</script>`;
26
- return { html, head : styles + script, title, bodyClassName : theme.classes.root };
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 };
27
43
  {{#else}}
28
- const script = `<script>window.__APP_OPTIONS__=${JSON.stringify(options)}</script>`;
29
- return { html, head : script, title };
44
+ return { html, head : script, title : getTitleForUrl(url) };
30
45
  {{#endif}}
31
- }
32
46
  {{#else}}
33
- /**
34
- * Server-side render function.
35
- * Returns HTML string to be injected into the template.
36
- * @param {string} _url - Request URL
37
- * @returns {object} Rendered content { html: string, head?: string }
38
- */
39
- export function render(_url) {
40
- Component.resetUid();
41
- const html = App.mount({}).toString();
47
+ const html = new App().toString();
42
48
  {{#if CSSFUN}}
43
- const styles = StyleSheet.toString();
44
- return { html, head : styles, bodyClassName : theme.classes.root };
49
+ // <style> tags for every style sheet the app modules registered on import, theme included.
50
+ return { html, head : StyleSheet.toString(), bodyClassName : theme.classes.root };
45
51
  {{#else}}
46
52
  return { html };
47
53
  {{#endif}}
48
- }
49
54
  {{#endif}}
55
+ }