@canonical/summon-application 0.36.0 → 0.38.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 +57 -7
- package/dist/esm/application/react/index.js +97 -35
- package/dist/esm/application/react/index.js.map +1 -1
- package/dist/esm/application/react/templates/.storybook/decorators/withRouter.tsx +3 -2
- package/dist/esm/application/react/templates/.storybook/preview.ts.ejs +4 -2
- package/dist/esm/application/react/templates/README.md.ejs +88 -5
- package/dist/esm/application/react/templates/index.html.ejs +25 -3
- package/dist/esm/application/react/templates/package.json.ejs +22 -1
- package/dist/esm/application/react/templates/src/client/entry.tsx.ejs +76 -17
- package/dist/esm/application/react/templates/src/domains/account/AccountPage.tsx.ejs +4 -5
- package/dist/esm/application/react/templates/src/domains/account/LoginPage.tsx.ejs +4 -5
- package/dist/esm/application/react/templates/src/domains/catalog/CatalogPage.tsx.ejs +16 -5
- package/dist/esm/application/react/templates/src/domains/catalog/ProductList.tsx.ejs +2 -2
- package/dist/esm/application/react/templates/src/domains/contact/ContactPage.tsx.ejs +4 -5
- package/dist/esm/application/react/templates/src/domains/marketing/GuidePage.tsx.ejs +4 -10
- package/dist/esm/application/react/templates/src/domains/marketing/HomePage.tsx.ejs +13 -5
- package/dist/esm/application/react/templates/src/formatDocumentTitle.ts.ejs +21 -0
- package/dist/esm/application/react/templates/src/i18n/ar.ts +4 -5
- package/dist/esm/application/react/templates/src/i18n/en.ts +4 -5
- package/dist/esm/application/react/templates/src/i18n/fr.ts +4 -5
- package/dist/esm/application/react/templates/src/lib/ExampleComponent/styles.css +13 -3
- package/dist/esm/application/react/templates/src/lib/index.ts.ejs +1 -1
- package/dist/esm/application/react/templates/src/routes.tsx.ejs +25 -3
- package/dist/esm/application/react/templates/src/server/entry.tsx.ejs +50 -16
- package/dist/esm/application/react/templates/src/styles/app.css +23 -13
- package/dist/esm/application/react/templates/src/styles/index.css.ejs +27 -0
- package/dist/esm/application/react/templates/test/e2e/servers.e2e.ts.ejs +58 -2
- package/dist/esm/application/react/templates/tsconfig.json.ejs +3 -0
- package/dist/esm/application/react/templates/vite-plugin-ds-icons.ts +69 -0
- package/dist/esm/application/react/templates/vite.config.ts.ejs +57 -3
- package/dist/esm/domain/index.js +3 -4
- package/dist/esm/domain/index.js.map +1 -1
- package/dist/esm/route/index.js +2 -3
- package/dist/esm/route/index.js.map +1 -1
- package/dist/types/application/react/index.d.ts +1 -0
- package/dist/types/application/react/index.d.ts.map +1 -1
- package/dist/types/domain/index.d.ts.map +1 -1
- package/dist/types/route/index.d.ts.map +1 -1
- package/package.json +8 -8
|
@@ -19,18 +19,17 @@ const fr: Record<MessageKey, MessageValue> = {
|
|
|
19
19
|
"locale.label": "Langue",
|
|
20
20
|
"notFound.heading": "Page introuvable",
|
|
21
21
|
"notFound.body": "La page que vous recherchez n'existe pas.",
|
|
22
|
-
"home.title": "Accueil
|
|
22
|
+
"home.title": "Accueil",
|
|
23
23
|
"home.heading": "Accueil",
|
|
24
24
|
"home.tagline": "Bienvenue dans le boilerplate du routeur pragma.",
|
|
25
25
|
"home.exampleHeading": "Composant d'exemple",
|
|
26
26
|
"home.streamingHeading": "Streaming avec Suspense",
|
|
27
27
|
"home.streamFallback": "Chargement du contenu diffusé…",
|
|
28
|
-
"guide.title": "{slug} — Guides",
|
|
29
28
|
"guide.body": "Contenu du guide pour {slug}.",
|
|
30
|
-
"account.title": "Compte
|
|
29
|
+
"account.title": "Compte",
|
|
31
30
|
"account.heading": "Compte",
|
|
32
31
|
"account.body": "Page de compte protégée. Vous êtes connecté.",
|
|
33
|
-
"login.title": "Connexion
|
|
32
|
+
"login.title": "Connexion",
|
|
34
33
|
"login.heading": "Connexion",
|
|
35
34
|
"login.hintBefore": "Connexion de démonstration. Ajoutez",
|
|
36
35
|
"login.hintAfter":
|
|
@@ -48,7 +47,7 @@ const fr: Record<MessageKey, MessageValue> = {
|
|
|
48
47
|
"contact.message": "Message",
|
|
49
48
|
"contact.messageHint": "500 caractères maximum",
|
|
50
49
|
"contact.send": "Envoyer le message",
|
|
51
|
-
"catalog.title": "Catalogue
|
|
50
|
+
"catalog.title": "Catalogue",
|
|
52
51
|
"catalog.heading": "Catalogue",
|
|
53
52
|
"catalog.loading": "Chargement du catalogue…",
|
|
54
53
|
"catalog.error":
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
/* @canonical/generator-ds 0.9.0-experimental.9 */
|
|
2
2
|
|
|
3
|
-
/*
|
|
4
|
-
|
|
3
|
+
/* This application's own CSS, in `app`. Left unlayered it would beat every
|
|
4
|
+
* rule the design system ships, because unlayered CSS outranks any layer —
|
|
5
|
+
* by accident rather than by intent. `app` sorts above the design system's
|
|
6
|
+
* own layers only while the entry imports `#styles/index.css`, and with it
|
|
7
|
+
* `@canonical/styles`' order statement, before anything that pulls this file
|
|
8
|
+
* in; that is what canonical/pragma#1122 puts in the generated entry, and
|
|
9
|
+
* this file lands after it. Keep that import first. */
|
|
5
10
|
|
|
11
|
+
@layer app {
|
|
12
|
+
/*
|
|
13
|
+
.ds.example-component {
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
*/
|
|
6
17
|
}
|
|
7
|
-
*/
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<% if (intl) { -%>
|
|
2
2
|
import { useTranslation } from "@canonical/i18n-react";
|
|
3
3
|
<% } -%>
|
|
4
|
+
import { Head } from "@canonical/react-head";
|
|
4
5
|
import {
|
|
5
6
|
type AnyRoute,
|
|
6
7
|
group,
|
|
@@ -13,9 +14,13 @@ import {
|
|
|
13
14
|
} from "@canonical/router-core";
|
|
14
15
|
import type { ReactElement, ReactNode } from "react";
|
|
15
16
|
import accountRoutes from "#domains/account/routes.js";
|
|
16
|
-
|
|
17
|
+
<%# getRequest and the ProductList pair exist only for serverQueries and -%>
|
|
18
|
+
<%# resolveRelayPayloads, which the SPA arm omits; catalogRoutes is always used. -%>
|
|
19
|
+
<% if (relay && !spa) { -%>
|
|
17
20
|
import { getRequest } from "relay-runtime";
|
|
18
21
|
import { PAGE_SIZE, productListQuery } from "#domains/catalog/ProductList.js";
|
|
22
|
+
<% } -%>
|
|
23
|
+
<% if (relay) { -%>
|
|
19
24
|
import catalogRoutes from "#domains/catalog/routes.js";
|
|
20
25
|
<% } -%>
|
|
21
26
|
<% if (forms) { -%>
|
|
@@ -23,7 +28,7 @@ import contactRoutes from "#domains/contact/routes.js";
|
|
|
23
28
|
<% } -%>
|
|
24
29
|
import marketingRoutes from "#domains/marketing/routes.js";
|
|
25
30
|
import Navigation from "#lib/Navigation/index.js";
|
|
26
|
-
<% if (relay) { -%>
|
|
31
|
+
<% if (relay && !spa) { -%>
|
|
27
32
|
import type { RelaySeedPayload } from "#relay/environment.js";
|
|
28
33
|
<% } -%>
|
|
29
34
|
|
|
@@ -35,6 +40,9 @@ function hasDemoAuth(search: unknown): boolean {
|
|
|
35
40
|
return authValue === "1";
|
|
36
41
|
}
|
|
37
42
|
|
|
43
|
+
<%# Server-only: its single consumer is src/server/entry.tsx, which the SPA arm does -%>
|
|
44
|
+
<%# not emit. The client path guards the same routes through withAuth below. -%>
|
|
45
|
+
<% if (!spa) { -%>
|
|
38
46
|
/**
|
|
39
47
|
* Auth decision for an already-matched route, from the router's own data —
|
|
40
48
|
* the matched pattern and the schema-validated search. Deriving it from the
|
|
@@ -53,6 +61,7 @@ export function getAuthRedirectForMatch(match: {
|
|
|
53
61
|
return `/login?from=${encodeURIComponent(match.pathname)}`;
|
|
54
62
|
}
|
|
55
63
|
|
|
64
|
+
<% } -%>
|
|
56
65
|
export function withAuth(loginPath: string): RouteMiddleware {
|
|
57
66
|
return <TRoute extends AnyRoute>(currentRoute: TRoute): TRoute => {
|
|
58
67
|
if (!protectedPaths.has(currentRoute.url)) {
|
|
@@ -103,6 +112,7 @@ function NotFoundPage(): ReactElement {
|
|
|
103
112
|
|
|
104
113
|
return (
|
|
105
114
|
<section>
|
|
115
|
+
<Head title={t("notFound.heading")} />
|
|
106
116
|
<h1>{t("notFound.heading")}</h1>
|
|
107
117
|
<p>{t("notFound.body")}</p>
|
|
108
118
|
</section>
|
|
@@ -117,6 +127,7 @@ const notFoundRoute = route({
|
|
|
117
127
|
<% } else { -%>
|
|
118
128
|
content: () => (
|
|
119
129
|
<section>
|
|
130
|
+
<Head title="Page not found" />
|
|
120
131
|
<h1>Page not found</h1>
|
|
121
132
|
<p>The page you are looking for does not exist.</p>
|
|
122
133
|
</section>
|
|
@@ -142,9 +153,16 @@ const [contact] = group(publicLayout, [contactRoutes.contact] as const);
|
|
|
142
153
|
const [catalog] = group(publicLayout, [catalogRoutes.catalog] as const);
|
|
143
154
|
|
|
144
155
|
<% } -%>
|
|
156
|
+
<% if (spa) { -%>
|
|
157
|
+
// Static redirect route: matched entirely from the URL, no content — the
|
|
158
|
+
// router resolves it client-side. There is no server to answer a cold request
|
|
159
|
+
// with a real 301. Static redirects accept 301 or 308 only; runtime redirect()
|
|
160
|
+
// covers the 302 family.
|
|
161
|
+
<% } else { -%>
|
|
145
162
|
// Static redirect route: matched entirely from the URL, no content — the
|
|
146
163
|
// router (and the SSR disposition helper) answers with a real 301. Static
|
|
147
164
|
// redirects accept 301 or 308 only; runtime redirect() covers the 302 family.
|
|
165
|
+
<% } -%>
|
|
148
166
|
const legacyHome = route({
|
|
149
167
|
url: "/home",
|
|
150
168
|
redirect: "/",
|
|
@@ -173,6 +191,9 @@ declare module "@canonical/router-react" {
|
|
|
173
191
|
}
|
|
174
192
|
}
|
|
175
193
|
|
|
194
|
+
<%# Declared for every flag combination EXCEPT the SPA arm, which has no server -%>
|
|
195
|
+
<%# layer whose signatures need to stay uniform. -%>
|
|
196
|
+
<% if (!spa) { -%>
|
|
176
197
|
/**
|
|
177
198
|
* A GraphQL operation captured on the server and replayed on the client:
|
|
178
199
|
* the operation name identifies the query, `variables` rebuilds the operation
|
|
@@ -187,7 +208,8 @@ export interface SerializedRelayPayload {
|
|
|
187
208
|
readonly data: Record<string, unknown>;
|
|
188
209
|
}
|
|
189
210
|
|
|
190
|
-
<%
|
|
211
|
+
<% } -%>
|
|
212
|
+
<% if (relay && !spa) { -%>
|
|
191
213
|
/**
|
|
192
214
|
* Per-route server queries: what the SSR layer fetches (and serializes) for a
|
|
193
215
|
* matched route before rendering it. Keyed by the route ids that
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
// Styles first, for the same reason as in the client entry: the order the
|
|
2
|
+
// bundler evaluates style imports in is the order the rules are emitted in.
|
|
3
|
+
// The server build only needs the application's own sheet — the design
|
|
4
|
+
// system's layers reach the browser through the client bundle's stylesheet,
|
|
5
|
+
// which the renderer links from the built HTML.
|
|
6
|
+
import "#styles/app.css";
|
|
1
7
|
<% if (intl) { -%>
|
|
2
8
|
import { documentAttrs, isSupportedLocale } from "@canonical/i18n-core";
|
|
3
9
|
import { I18nProvider } from "@canonical/i18n-react";
|
|
@@ -19,6 +25,7 @@ import { catalogs, i18nConfig } from "#i18n/index.js";
|
|
|
19
25
|
<% if (relay) { -%>
|
|
20
26
|
import { createEnvironment } from "#relay/environment.js";
|
|
21
27
|
<% } -%>
|
|
28
|
+
import formatDocumentTitle from "../formatDocumentTitle.js";
|
|
22
29
|
import {
|
|
23
30
|
appRoutes,
|
|
24
31
|
getAuthRedirectForMatch,
|
|
@@ -32,7 +39,6 @@ import {
|
|
|
32
39
|
serverQueries,
|
|
33
40
|
<% } -%>
|
|
34
41
|
} from "../routes.js";
|
|
35
|
-
import "#styles/app.css";
|
|
36
42
|
|
|
37
43
|
interface InitialData extends Record<string, unknown> {
|
|
38
44
|
readonly url?: string;
|
|
@@ -282,27 +288,45 @@ export default function EntryServer(props: ServerEntrypointProps<InitialData>) {
|
|
|
282
288
|
const { lang, dir } = documentAttrs(i18nConfig, locale);
|
|
283
289
|
|
|
284
290
|
<% } -%>
|
|
285
|
-
//
|
|
286
|
-
//
|
|
287
|
-
//
|
|
291
|
+
// The root declaration plus the theme, on one element. `app` is the context
|
|
292
|
+
// and `comfortable` the density; together they choose the --density-* channel
|
|
293
|
+
// every control reads. The cookie-resolved theme joins them so the first
|
|
294
|
+
// paint is flash-free — it is the same element `usePreferredTheme` toggles on
|
|
295
|
+
// the client, and React does not hydrate it (only `#root` is), so there is no
|
|
296
|
+
// mismatch to reconcile. index.html carries the same classes for the
|
|
297
|
+
// client-only build.
|
|
298
|
+
//
|
|
299
|
+
// Context, density and the theme are the whole of the root contract. Nothing
|
|
300
|
+
// on this element declares the design system itself: its stylesheet applies
|
|
301
|
+
// to the page as it stands.
|
|
288
302
|
<% if (relay) { -%>
|
|
289
303
|
return (
|
|
290
304
|
<% if (intl) { -%>
|
|
291
|
-
<html
|
|
305
|
+
<html
|
|
306
|
+
lang={lang}
|
|
307
|
+
dir={dir}
|
|
308
|
+
className={["app comfortable", initialData.theme]
|
|
309
|
+
.filter(Boolean)
|
|
310
|
+
.join(" ")}
|
|
311
|
+
>
|
|
292
312
|
<% } else { -%>
|
|
293
|
-
<html
|
|
313
|
+
<html
|
|
314
|
+
lang={props.lang}
|
|
315
|
+
className={["app comfortable", initialData.theme]
|
|
316
|
+
.filter(Boolean)
|
|
317
|
+
.join(" ")}
|
|
318
|
+
>
|
|
294
319
|
<% } -%>
|
|
295
320
|
<head>
|
|
296
321
|
{props.otherHeadElements}
|
|
297
322
|
{props.scriptElements}
|
|
298
323
|
{props.linkElements}
|
|
299
324
|
</head>
|
|
300
|
-
|
|
301
|
-
<body className="app comfortable">
|
|
325
|
+
<body>
|
|
302
326
|
<div id="root">
|
|
303
327
|
<% if (intl) { -%>
|
|
304
328
|
<I18nProvider config={i18nConfig} catalogs={catalogs} locale={locale}>
|
|
305
|
-
<HeadProvider>
|
|
329
|
+
<HeadProvider titleTemplate={formatDocumentTitle}>
|
|
306
330
|
<RelayEnvironmentProvider environment={relayEnvironment}>
|
|
307
331
|
<RouterProvider router={router}>
|
|
308
332
|
<Outlet fallback={<p>Loading…</p>} />
|
|
@@ -311,7 +335,7 @@ export default function EntryServer(props: ServerEntrypointProps<InitialData>) {
|
|
|
311
335
|
</HeadProvider>
|
|
312
336
|
</I18nProvider>
|
|
313
337
|
<% } else { -%>
|
|
314
|
-
<HeadProvider>
|
|
338
|
+
<HeadProvider titleTemplate={formatDocumentTitle}>
|
|
315
339
|
<RelayEnvironmentProvider environment={relayEnvironment}>
|
|
316
340
|
<RouterProvider router={router}>
|
|
317
341
|
<Outlet fallback={<p>Loading…</p>} />
|
|
@@ -326,28 +350,38 @@ export default function EntryServer(props: ServerEntrypointProps<InitialData>) {
|
|
|
326
350
|
<% } else { -%>
|
|
327
351
|
return (
|
|
328
352
|
<% if (intl) { -%>
|
|
329
|
-
<html
|
|
353
|
+
<html
|
|
354
|
+
lang={lang}
|
|
355
|
+
dir={dir}
|
|
356
|
+
className={["app comfortable", initialData.theme]
|
|
357
|
+
.filter(Boolean)
|
|
358
|
+
.join(" ")}
|
|
359
|
+
>
|
|
330
360
|
<% } else { -%>
|
|
331
|
-
<html
|
|
361
|
+
<html
|
|
362
|
+
lang={props.lang}
|
|
363
|
+
className={["app comfortable", initialData.theme]
|
|
364
|
+
.filter(Boolean)
|
|
365
|
+
.join(" ")}
|
|
366
|
+
>
|
|
332
367
|
<% } -%>
|
|
333
368
|
<head>
|
|
334
369
|
{props.otherHeadElements}
|
|
335
370
|
{props.scriptElements}
|
|
336
371
|
{props.linkElements}
|
|
337
372
|
</head>
|
|
338
|
-
|
|
339
|
-
<body className="app comfortable">
|
|
373
|
+
<body>
|
|
340
374
|
<div id="root">
|
|
341
375
|
<% if (intl) { -%>
|
|
342
376
|
<I18nProvider config={i18nConfig} catalogs={catalogs} locale={locale}>
|
|
343
|
-
<HeadProvider>
|
|
377
|
+
<HeadProvider titleTemplate={formatDocumentTitle}>
|
|
344
378
|
<RouterProvider router={router}>
|
|
345
379
|
<Outlet fallback={<p>Loading…</p>} />
|
|
346
380
|
</RouterProvider>
|
|
347
381
|
</HeadProvider>
|
|
348
382
|
</I18nProvider>
|
|
349
383
|
<% } else { -%>
|
|
350
|
-
<HeadProvider>
|
|
384
|
+
<HeadProvider titleTemplate={formatDocumentTitle}>
|
|
351
385
|
<RouterProvider router={router}>
|
|
352
386
|
<Outlet fallback={<p>Loading…</p>} />
|
|
353
387
|
</RouterProvider>
|
|
@@ -1,16 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
/* This application's own CSS, in `app`. Left unlayered it would beat every
|
|
2
|
+
* rule the design system ships, because unlayered CSS outranks any layer —
|
|
3
|
+
* by accident rather than by intent. `app` sorts above the design system's
|
|
4
|
+
* own layers only while the entry imports `#styles/index.css`, and with it
|
|
5
|
+
* `@canonical/styles`' order statement, before anything that pulls this file
|
|
6
|
+
* in; that is what canonical/pragma#1122 puts in the generated entry, and
|
|
7
|
+
* this file lands after it. Keep that import first. */
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
@layer app {
|
|
10
|
+
.app-shell {
|
|
11
|
+
max-width: 1280px;
|
|
12
|
+
margin: 0 auto;
|
|
13
|
+
padding-inline: var(--grid-gap, 1.5rem);
|
|
14
|
+
padding-block: var(--container-gap-loose, 1.5rem);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.shell-header {
|
|
18
|
+
padding-block-end: var(--container-gap-default, 1rem);
|
|
19
|
+
}
|
|
11
20
|
|
|
12
|
-
.shell-header nav {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
21
|
+
.shell-header nav {
|
|
22
|
+
grid-column: 1 / -1;
|
|
23
|
+
display: flex;
|
|
24
|
+
gap: var(--container-gap-default, 1rem);
|
|
25
|
+
}
|
|
16
26
|
}
|
|
@@ -1,4 +1,31 @@
|
|
|
1
|
+
/* The application's one stylesheet, and the order it is built in.
|
|
2
|
+
*
|
|
3
|
+
* Order is the whole point of this file. The cascade sorts by layer, and the
|
|
4
|
+
* layer order is fixed by the first `@layer` statement a browser sees, so
|
|
5
|
+
* whatever arrives first decides how everything after it is arbitrated. That
|
|
6
|
+
* is why this file — not a component, not a route — is the first thing the
|
|
7
|
+
* client entry imports.
|
|
8
|
+
*
|
|
9
|
+
* 1. fonts `@font-face` only. The cascade does not sort font faces, so
|
|
10
|
+
* they can sit ahead of the layer statement; they are a
|
|
11
|
+
* separate import because loading a typeface is a choice an
|
|
12
|
+
* application makes, not something a stylesheet imposes.
|
|
13
|
+
* 2. styles the layer statement, the tokens, the reset, the modifiers.
|
|
14
|
+
* 3. components every component stylesheet the design system ships, in one
|
|
15
|
+
* file, so that every component rule is in this stylesheet
|
|
16
|
+
* from the first paint and no lazily-loaded route can render
|
|
17
|
+
* before its own CSS arrives. It does not decide which of a
|
|
18
|
+
* component's rules and this application's wins a tie: both
|
|
19
|
+
* copies of a component sheet reach the build and the
|
|
20
|
+
* minifier keeps the last, which is the module's — measured,
|
|
21
|
+
* it lands after this file's app.css. The layer wrapper now
|
|
22
|
+
* landing on the component packages is what settles that.
|
|
23
|
+
* 4. app this application's own CSS, last.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
@import url("@canonical/styles/fonts");
|
|
1
27
|
@import url("@canonical/styles");
|
|
28
|
+
@import url("@canonical/react-ds-global/index.css");
|
|
2
29
|
<% if (forms) { -%>
|
|
3
30
|
@import url("@canonical/react-ds-global-form/dist/esm/index.css");
|
|
4
31
|
<% } -%>
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
// @vitest-environment node
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
+
<% if (spa) { -%>
|
|
5
|
+
* End-to-end test of the build: boot each of the two server scripts (`dev` and
|
|
6
|
+
* `preview`) and assert it serves correctly over HTTP. The defects this guards
|
|
7
|
+
* against live at the HTTP/content-type layer (assets served as text/html; a
|
|
8
|
+
* 500 page), so fetch-level assertions catch them — no browser needed.
|
|
9
|
+
*
|
|
10
|
+
* Each cell runs its real package.json script — including the build that
|
|
11
|
+
* `preview` performs — so the test mirrors the actual user experience. `dev`
|
|
12
|
+
* boots in a second or two (tight readiness budget); `preview` builds the
|
|
13
|
+
* client first, so it gets a larger budget. Kept out of the default `test` run
|
|
14
|
+
* via the `*.e2e.ts` name; invoke with `bun run test:e2e`.
|
|
15
|
+
<% } else { -%>
|
|
4
16
|
* End-to-end test of the build: boot each of the 2×3 server scripts and assert
|
|
5
17
|
* it serves correctly over HTTP. The defects this guards against live at the
|
|
6
18
|
* HTTP/content-type layer (assets served as text/html; a 500 page), so
|
|
@@ -12,14 +24,20 @@
|
|
|
12
24
|
* `preview*` cells build the client + compile the renderer first, so they get a
|
|
13
25
|
* larger budget. Kept out of the default `test` run via the `*.e2e.ts` name;
|
|
14
26
|
* invoke with `bun run test:e2e`.
|
|
27
|
+
<% } -%>
|
|
15
28
|
*/
|
|
16
29
|
import { describe, expect, it } from "vitest";
|
|
17
30
|
import { startServer } from "./serverHarness.js";
|
|
18
31
|
|
|
19
32
|
const CWD = process.cwd();
|
|
20
33
|
|
|
34
|
+
<% if (spa) { -%>
|
|
35
|
+
// Readiness budgets: dev boots straight away; preview builds the client before
|
|
36
|
+
// serving, so it needs a larger budget.
|
|
37
|
+
<% } else { -%>
|
|
21
38
|
// Readiness budgets: dev* boots straight away; preview* builds the client +
|
|
22
39
|
// renderer before serving, so it needs a larger budget.
|
|
40
|
+
<% } -%>
|
|
23
41
|
const DEV_READY_MS = 20_000;
|
|
24
42
|
const PREVIEW_READY_MS = 90_000;
|
|
25
43
|
const TEST_TIMEOUT_MS = PREVIEW_READY_MS + 30_000;
|
|
@@ -27,6 +45,16 @@ const TEST_TIMEOUT_MS = PREVIEW_READY_MS + 30_000;
|
|
|
27
45
|
interface Cell {
|
|
28
46
|
/** package.json script to run. */
|
|
29
47
|
script: string;
|
|
48
|
+
<% if (spa) { -%>
|
|
49
|
+
/** Readiness budget for this cell's boot (and build, for preview). */
|
|
50
|
+
timeoutMs: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const MATRIX: Cell[] = [
|
|
54
|
+
{ script: "dev", timeoutMs: DEV_READY_MS },
|
|
55
|
+
{ script: "preview", timeoutMs: PREVIEW_READY_MS },
|
|
56
|
+
];
|
|
57
|
+
<% } else { -%>
|
|
30
58
|
/** Readiness budget for this cell's boot (and build, for preview*). */
|
|
31
59
|
timeoutMs: number;
|
|
32
60
|
/**
|
|
@@ -45,11 +73,16 @@ const MATRIX: Cell[] = [
|
|
|
45
73
|
{ script: "preview:bun", timeoutMs: PREVIEW_READY_MS, ssr: true },
|
|
46
74
|
{ script: "preview:express", timeoutMs: PREVIEW_READY_MS, ssr: true },
|
|
47
75
|
];
|
|
76
|
+
<% } -%>
|
|
48
77
|
|
|
49
78
|
/** A JS/TS module or CSS-as-JS asset must never come back as the HTML page. */
|
|
50
79
|
const JS_CONTENT_TYPE = /javascript/;
|
|
51
80
|
|
|
81
|
+
<% if (spa) { -%>
|
|
82
|
+
describe("server matrix (dev + preview) serves correctly", () => {
|
|
83
|
+
<% } else { -%>
|
|
52
84
|
describe("server matrix (2×3) serves correctly", () => {
|
|
85
|
+
<% } -%>
|
|
53
86
|
for (const cell of MATRIX) {
|
|
54
87
|
it(
|
|
55
88
|
`${cell.script} serves an HTML page and JS assets (not HTML)`,
|
|
@@ -77,8 +110,24 @@ describe("server matrix (2×3) serves correctly", () => {
|
|
|
77
110
|
const asset = await fetch(new URL(scriptSrc as string, server.base));
|
|
78
111
|
expect(asset.status).toBe(200);
|
|
79
112
|
expect(asset.headers.get("content-type")).toMatch(JS_CONTENT_TYPE);
|
|
113
|
+
<% if (!spa) { -%>
|
|
114
|
+
|
|
115
|
+
// 4. SSR cells carry the page's own title, inside <head>, in the
|
|
116
|
+
// served HTML — the point of @canonical/react-head's <Head>, and
|
|
117
|
+
// the thing that was silently missing before it: head tags were
|
|
118
|
+
// applied in an effect, which never runs on the server. Scoped to
|
|
119
|
+
// <head> deliberately: a title streamed in after a suspended
|
|
120
|
+
// boundary lands in <body>, which is a regression a whole-document
|
|
121
|
+
// match would not see. Exactly one, because the shell carries none
|
|
122
|
+
// for the page's to beat.
|
|
123
|
+
if (cell.ssr) {
|
|
124
|
+
const head = html.slice(0, html.indexOf("</head>"));
|
|
125
|
+
|
|
126
|
+
expect(head.match(/<title>/g)?.length ?? 0).toBe(1);
|
|
127
|
+
expect(head).toContain("<title>Home — <%= name %></title>");
|
|
128
|
+
}
|
|
80
129
|
|
|
81
|
-
//
|
|
130
|
+
// 5. SSR cells render /sitemap.xml as XML from the sitemap renderer —
|
|
82
131
|
// the second renderer, picked by path, never the HTML app. (The SPA
|
|
83
132
|
// dev/preview cells have no SSR route, so they are exempt.)
|
|
84
133
|
if (cell.ssr) {
|
|
@@ -90,8 +139,10 @@ describe("server matrix (2×3) serves correctly", () => {
|
|
|
90
139
|
expect(xml).toContain("<loc>");
|
|
91
140
|
}
|
|
92
141
|
|
|
142
|
+
<% } -%>
|
|
93
143
|
<% if (intl) { -%>
|
|
94
|
-
|
|
144
|
+
<% if (!spa) { -%>
|
|
145
|
+
// 6. SSR cells negotiate the locale per request (i18n-core:
|
|
95
146
|
// cookie > Accept-Language > default) and render the translated
|
|
96
147
|
// document server-side: `<html lang dir>` plus the localized
|
|
97
148
|
// navigation chrome, before any client JavaScript runs.
|
|
@@ -114,6 +165,8 @@ describe("server matrix (2×3) serves correctly", () => {
|
|
|
114
165
|
}
|
|
115
166
|
|
|
116
167
|
<% } -%>
|
|
168
|
+
<% } -%>
|
|
169
|
+
<% if (!spa) { -%>
|
|
117
170
|
// SSR cells answer with the router's disposition: a matched
|
|
118
171
|
// not-found page is a real 404, static redirect routes and the
|
|
119
172
|
// auth guard are real HTTP redirects, and an authorized request
|
|
@@ -170,6 +223,8 @@ describe("server matrix (2×3) serves correctly", () => {
|
|
|
170
223
|
expect(authorized.status).toBe(200);
|
|
171
224
|
}
|
|
172
225
|
|
|
226
|
+
<% } -%>
|
|
227
|
+
<% if (!spa) { -%>
|
|
173
228
|
// SSR cells serialize Relay data across the boundary (when the app
|
|
174
229
|
// was scaffolded with --relay): the catalog page arrives with
|
|
175
230
|
// server-rendered product markup and carries the captured operation
|
|
@@ -187,6 +242,7 @@ describe("server matrix (2×3) serves correctly", () => {
|
|
|
187
242
|
const home = await fetch(`${server.base}/`);
|
|
188
243
|
expect(await home.text()).not.toContain("relayPayloads");
|
|
189
244
|
}
|
|
245
|
+
<% } -%>
|
|
190
246
|
} finally {
|
|
191
247
|
await server.stop();
|
|
192
248
|
}
|
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
"baseUrl": "src",
|
|
5
5
|
"skipLibCheck": true,
|
|
6
6
|
"types": [
|
|
7
|
+
<% if (!spa) { -%>
|
|
7
8
|
"bun-types",
|
|
9
|
+
<% } -%>
|
|
8
10
|
"node",
|
|
9
11
|
"react",
|
|
10
12
|
"react-dom",
|
|
@@ -30,6 +32,7 @@
|
|
|
30
32
|
".storybook/*.ts",
|
|
31
33
|
".storybook/*.tsx",
|
|
32
34
|
"vite.config.ts",
|
|
35
|
+
"vite-plugin-ds-icons.ts",
|
|
33
36
|
"vitest.setup.ts",
|
|
34
37
|
"vitest.config.ts",
|
|
35
38
|
"vitest.e2e.config.ts",
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { createReadStream, existsSync } from "node:fs";
|
|
2
|
+
import { cp, mkdir } from "node:fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import type { Plugin, ResolvedConfig } from "vite";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The design system's icons at `/icons`, in development and in the build.
|
|
9
|
+
*
|
|
10
|
+
* `Icon`, `Spinner`, and every component that renders one resolve
|
|
11
|
+
* `/icons/<name>.svg#<name>` at runtime, from the site root. An unserved
|
|
12
|
+
* icon renders as nothing at all — no error, no broken-image glyph — so the
|
|
13
|
+
* files must be reachable in development and in the assembled app alike.
|
|
14
|
+
* They live in `@canonical/ds-assets`; this plugin serves them from there
|
|
15
|
+
* rather than copying them into the repository, so they can never drift
|
|
16
|
+
* from the installed package version.
|
|
17
|
+
*/
|
|
18
|
+
const iconsDir = () =>
|
|
19
|
+
join(
|
|
20
|
+
dirname(
|
|
21
|
+
fileURLToPath(import.meta.resolve("@canonical/ds-assets/package.json")),
|
|
22
|
+
),
|
|
23
|
+
"icons",
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/** One directory of SVGs, by exact name: no traversal, no directory listing. */
|
|
27
|
+
const ICON_NAME = /^[a-z0-9-]+\.svg$/;
|
|
28
|
+
|
|
29
|
+
export function dsIcons(): Plugin {
|
|
30
|
+
let config: ResolvedConfig;
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
name: "design-system-icons-at-root",
|
|
34
|
+
configResolved(resolved) {
|
|
35
|
+
config = resolved;
|
|
36
|
+
},
|
|
37
|
+
configureServer(server) {
|
|
38
|
+
server.middlewares.use("/icons", (request, response, next) => {
|
|
39
|
+
const name = (request.url ?? "").split("?")[0].replace(/^\//, "");
|
|
40
|
+
if (!ICON_NAME.test(name)) {
|
|
41
|
+
next();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const file = join(iconsDir(), name);
|
|
45
|
+
if (!existsSync(file)) {
|
|
46
|
+
// A real 404, not the SPA fallback: a missing icon should be
|
|
47
|
+
// visible in the network tab, not silently resolve to HTML.
|
|
48
|
+
response.statusCode = 404;
|
|
49
|
+
response.end();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
response.setHeader("Content-Type", "image/svg+xml");
|
|
53
|
+
createReadStream(file).pipe(response);
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
async writeBundle() {
|
|
57
|
+
// The client build only: `build:server` (the SSR renderers) has no
|
|
58
|
+
// static output to serve from. The dev/preview/production servers all
|
|
59
|
+
// serve the client output directory, so the copy lands where every
|
|
60
|
+
// one of them looks.
|
|
61
|
+
if (config.command !== "build" || config.build.ssr) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const target = join(config.build.outDir, "icons");
|
|
65
|
+
await mkdir(target, { recursive: true });
|
|
66
|
+
await cp(iconsDir(), target, { recursive: true });
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|