@soroush.design/cli 0.2.0-alpha.3 → 0.2.0-alpha.5
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/dist/bin.mjs +2 -15
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +1 -1
- package/dist/{io-Bybgct-f.mjs → io-CqI2BTaP.mjs} +430 -1032
- package/package.json +14 -5
- package/templates/astro/js/base/README.md +93 -0
- package/templates/astro/js/base/_gitignore +5 -0
- package/templates/astro/js/base/astro.config.mjs +23 -0
- package/templates/astro/js/base/src/components/About.jsx +24 -0
- package/templates/astro/js/base/src/components/Counter.jsx +26 -0
- package/templates/astro/js/base/src/components/Greeting.jsx +60 -0
- package/templates/astro/js/base/src/components/Header.jsx +99 -0
- package/templates/astro/js/base/src/components/Home.jsx +32 -0
- package/templates/astro/js/base/src/components/Home.test.jsx +39 -0
- package/templates/astro/js/base/src/components/NotFound.jsx +17 -0
- package/templates/astro/js/base/src/data/api.js +84 -0
- package/templates/astro/js/base/src/layouts/Layout.astro +52 -0
- package/templates/astro/js/base/src/pages/404.astro +12 -0
- package/templates/astro/js/base/src/pages/about.astro +12 -0
- package/templates/astro/js/base/src/pages/index.astro +23 -0
- package/templates/astro/js/base/src/theme/Island.jsx +89 -0
- package/templates/astro/js/base/src/theme/scheme.js +32 -0
- package/templates/astro/js/base/vitest.config.js +12 -0
- package/templates/astro/js/sample-data/src/components/ItemDetails.jsx +64 -0
- package/templates/astro/js/sample-data/src/components/ItemList.jsx +100 -0
- package/templates/astro/js/sample-data/src/components/StatusChip.jsx +8 -0
- package/templates/astro/js/sample-data/src/data/items.js +65 -0
- package/templates/astro/js/sample-data/src/pages/items/[id].astro +37 -0
- package/templates/astro/js/sample-data/src/pages/items/index.astro +20 -0
- package/templates/astro/js/with/jotai/src/components/Counter.jsx +26 -0
- package/templates/astro/js/with/jotai/src/state/counter.js +4 -0
- package/templates/astro/js/with/msw/_env +3 -0
- package/templates/astro/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/astro/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/astro/js/with/msw/src/mocks/server.js +5 -0
- package/templates/astro/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/astro/js/with/msw/src/mocks/start.js +14 -0
- package/templates/astro/js/with/react-hook-form/src/components/Form.jsx +313 -0
- package/templates/astro/js/with/react-hook-form/src/pages/form.astro +8 -0
- package/templates/astro/js/with/redux-toolkit/src/components/Counter.jsx +27 -0
- package/templates/astro/js/with/redux-toolkit/src/state/store.js +18 -0
- package/templates/astro/js/with/tanstack-query/src/data/queryClient.js +13 -0
- package/templates/astro/js/with/zustand/src/components/Counter.jsx +24 -0
- package/templates/astro/js/with/zustand/src/state/counter.js +14 -0
- package/templates/astro/ts/base/README.md +93 -0
- package/templates/astro/ts/base/_gitignore +5 -0
- package/templates/astro/ts/base/astro.config.mjs +23 -0
- package/templates/astro/ts/base/src/components/About.tsx +24 -0
- package/templates/astro/ts/base/src/components/Counter.tsx +26 -0
- package/templates/astro/ts/base/src/components/Greeting.tsx +61 -0
- package/templates/astro/ts/base/src/components/Header.tsx +99 -0
- package/templates/astro/ts/base/src/components/Home.test.tsx +39 -0
- package/templates/astro/ts/base/src/components/Home.tsx +33 -0
- package/templates/astro/ts/base/src/components/NotFound.tsx +17 -0
- package/templates/astro/ts/base/src/data/api.ts +92 -0
- package/templates/astro/ts/base/src/layouts/Layout.astro +52 -0
- package/templates/astro/ts/base/src/pages/404.astro +12 -0
- package/templates/astro/ts/base/src/pages/about.astro +12 -0
- package/templates/astro/ts/base/src/pages/index.astro +23 -0
- package/templates/astro/ts/base/src/theme/Island.tsx +90 -0
- package/templates/astro/ts/base/src/theme/scheme.ts +34 -0
- package/templates/astro/ts/base/tsconfig.json +9 -0
- package/templates/astro/ts/base/vitest.config.ts +12 -0
- package/templates/astro/ts/sample-data/src/components/ItemDetails.tsx +65 -0
- package/templates/astro/ts/sample-data/src/components/ItemList.tsx +109 -0
- package/templates/astro/ts/sample-data/src/components/StatusChip.tsx +9 -0
- package/templates/astro/ts/sample-data/src/data/items.ts +71 -0
- package/templates/astro/ts/sample-data/src/pages/items/[id].astro +39 -0
- package/templates/astro/ts/sample-data/src/pages/items/index.astro +20 -0
- package/templates/astro/ts/with/jotai/src/components/Counter.tsx +26 -0
- package/templates/astro/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/astro/ts/with/msw/_env +3 -0
- package/templates/astro/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/astro/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/astro/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/astro/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/astro/ts/with/msw/src/mocks/start.ts +14 -0
- package/templates/astro/ts/with/react-hook-form/src/components/Form.tsx +317 -0
- package/templates/astro/ts/with/react-hook-form/src/pages/form.astro +8 -0
- package/templates/astro/ts/with/redux-toolkit/src/components/Counter.tsx +27 -0
- package/templates/astro/ts/with/redux-toolkit/src/state/store.ts +19 -0
- package/templates/astro/ts/with/tanstack-query/src/data/queryClient.ts +13 -0
- package/templates/astro/ts/with/zustand/src/components/Counter.tsx +24 -0
- package/templates/astro/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/react-router/js/base/README.md +67 -0
- package/templates/react-router/js/base/_gitignore +4 -0
- package/templates/react-router/js/base/index.html +12 -0
- package/templates/react-router/js/base/src/components/RouterLink.jsx +12 -0
- package/templates/react-router/js/base/src/data/api.js +76 -0
- package/templates/react-router/js/base/src/main.jsx +38 -0
- package/templates/react-router/js/base/src/pages/ErrorPage.jsx +14 -0
- package/templates/react-router/js/base/src/pages/Layout.jsx +141 -0
- package/templates/react-router/js/base/src/pages/about/Page.jsx +14 -0
- package/templates/react-router/js/base/src/pages/index/Counter.jsx +18 -0
- package/templates/react-router/js/base/src/pages/index/Greeting.jsx +48 -0
- package/templates/react-router/js/base/src/pages/index/Page.jsx +29 -0
- package/templates/react-router/js/base/src/pages/index/Page.test.jsx +61 -0
- package/templates/react-router/js/base/src/routes.jsx +46 -0
- package/templates/react-router/js/base/vite.config.js +13 -0
- package/templates/react-router/js/base/vitest.config.js +13 -0
- package/templates/react-router/js/sample-data/src/data/items.js +55 -0
- package/templates/react-router/js/sample-data/src/pages/item/Page.jsx +49 -0
- package/templates/react-router/js/sample-data/src/pages/item/loader.js +14 -0
- package/templates/react-router/js/sample-data/src/pages/items/Page.jsx +48 -0
- package/templates/react-router/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
- package/templates/react-router/js/sample-data/src/pages/items/loader.js +11 -0
- package/templates/react-router/js/with/jotai/src/pages/index/Counter.jsx +19 -0
- package/templates/react-router/js/with/jotai/src/state/counter.js +4 -0
- package/templates/react-router/js/with/jotai/src/state/scheme.js +4 -0
- package/templates/react-router/js/with/msw/_env +3 -0
- package/templates/react-router/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/react-router/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/react-router/js/with/msw/src/mocks/server.js +5 -0
- package/templates/react-router/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/react-router/js/with/msw/src/mocks/start.js +10 -0
- package/templates/react-router/js/with/react-hook-form/src/pages/form/Page.jsx +294 -0
- package/templates/react-router/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
- package/templates/react-router/js/with/redux-toolkit/src/state/store.js +27 -0
- package/templates/react-router/js/with/zustand/src/pages/index/Counter.jsx +17 -0
- package/templates/react-router/js/with/zustand/src/state/counter.js +14 -0
- package/templates/react-router/js/with/zustand/src/state/scheme.js +14 -0
- package/templates/react-router/ts/base/README.md +67 -0
- package/templates/react-router/ts/base/_gitignore +4 -0
- package/templates/react-router/ts/base/index.html +12 -0
- package/templates/react-router/ts/base/src/components/RouterLink.tsx +12 -0
- package/templates/react-router/ts/base/src/data/api.ts +84 -0
- package/templates/react-router/ts/base/src/main.tsx +38 -0
- package/templates/react-router/ts/base/src/pages/ErrorPage.tsx +14 -0
- package/templates/react-router/ts/base/src/pages/Layout.tsx +141 -0
- package/templates/react-router/ts/base/src/pages/about/Page.tsx +14 -0
- package/templates/react-router/ts/base/src/pages/index/Counter.tsx +18 -0
- package/templates/react-router/ts/base/src/pages/index/Greeting.tsx +49 -0
- package/templates/react-router/ts/base/src/pages/index/Page.test.tsx +61 -0
- package/templates/react-router/ts/base/src/pages/index/Page.tsx +29 -0
- package/templates/react-router/ts/base/src/routes.tsx +47 -0
- package/templates/react-router/ts/base/tsconfig.json +18 -0
- package/templates/react-router/ts/base/vite.config.ts +13 -0
- package/templates/react-router/ts/base/vitest.config.ts +13 -0
- package/templates/react-router/ts/sample-data/src/data/items.ts +61 -0
- package/templates/react-router/ts/sample-data/src/pages/item/Page.tsx +50 -0
- package/templates/react-router/ts/sample-data/src/pages/item/loader.ts +14 -0
- package/templates/react-router/ts/sample-data/src/pages/items/Page.tsx +49 -0
- package/templates/react-router/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
- package/templates/react-router/ts/sample-data/src/pages/items/loader.ts +11 -0
- package/templates/react-router/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
- package/templates/react-router/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/react-router/ts/with/jotai/src/state/scheme.ts +4 -0
- package/templates/react-router/ts/with/msw/_env +3 -0
- package/templates/react-router/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/react-router/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/react-router/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/react-router/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/react-router/ts/with/msw/src/mocks/start.ts +10 -0
- package/templates/react-router/ts/with/react-hook-form/src/pages/form/Page.tsx +298 -0
- package/templates/react-router/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
- package/templates/react-router/ts/with/redux-toolkit/src/state/store.ts +29 -0
- package/templates/react-router/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
- package/templates/react-router/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/react-router/ts/with/zustand/src/state/scheme.ts +19 -0
- package/templates/templates.md +67 -0
- package/templates/vike/js/base/README.md +83 -0
- package/templates/vike/js/base/_gitignore +4 -0
- package/templates/vike/js/base/src/data/api.js +76 -0
- package/templates/vike/js/base/src/pages/+Layout.jsx +183 -0
- package/templates/vike/js/base/src/pages/+config.js +12 -0
- package/templates/vike/js/base/src/pages/+onRenderClient.jsx +39 -0
- package/templates/vike/js/base/src/pages/+onRenderHtml.jsx +27 -0
- package/templates/vike/js/base/src/pages/PageShell.jsx +47 -0
- package/templates/vike/js/base/src/pages/_error/+Page.jsx +14 -0
- package/templates/vike/js/base/src/pages/about/+Page.jsx +14 -0
- package/templates/vike/js/base/src/pages/index/+Page.jsx +33 -0
- package/templates/vike/js/base/src/pages/index/Counter.jsx +20 -0
- package/templates/vike/js/base/src/pages/index/Greeting.jsx +48 -0
- package/templates/vike/js/base/src/pages/index/Page.test.jsx +67 -0
- package/templates/vike/js/base/src/pages/usePageContext.jsx +20 -0
- package/templates/vike/js/base/vite.config.js +14 -0
- package/templates/vike/js/base/vitest.config.js +13 -0
- package/templates/vike/js/sample-data/src/data/items.js +58 -0
- package/templates/vike/js/sample-data/src/pages/items/+Page.jsx +49 -0
- package/templates/vike/js/sample-data/src/pages/items/+data.js +26 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+Page.jsx +40 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+data.js +33 -0
- package/templates/vike/js/sample-data/src/pages/items/@id/+onBeforePrerenderStart.js +8 -0
- package/templates/vike/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
- package/templates/vike/js/ssg/src/pages/+config.js +11 -0
- package/templates/vike/js/ssg/src/pages/about/+Page.jsx +13 -0
- package/templates/vike/js/ssr/src/pages/+config.js +12 -0
- package/templates/vike/js/ssr/src/pages/+onRenderHtml.jsx +27 -0
- package/templates/vike/js/ssr/src/pages/about/+Page.jsx +14 -0
- package/templates/vike/js/with/jotai/src/pages/index/Counter.jsx +19 -0
- package/templates/vike/js/with/jotai/src/state/counter.js +4 -0
- package/templates/vike/js/with/jotai/src/state/scheme.js +4 -0
- package/templates/vike/js/with/msw/_env +3 -0
- package/templates/vike/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/vike/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/vike/js/with/msw/src/mocks/server.js +5 -0
- package/templates/vike/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/vike/js/with/msw/src/mocks/start.js +10 -0
- package/templates/vike/js/with/react-hook-form/src/pages/form/+Page.jsx +294 -0
- package/templates/vike/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
- package/templates/vike/js/with/redux-toolkit/src/state/store.js +27 -0
- package/templates/vike/js/with/zustand/src/pages/index/Counter.jsx +17 -0
- package/templates/vike/js/with/zustand/src/state/counter.js +14 -0
- package/templates/vike/js/with/zustand/src/state/scheme.js +14 -0
- package/templates/vike/ts/base/README.md +83 -0
- package/templates/vike/ts/base/_gitignore +4 -0
- package/templates/vike/ts/base/src/data/api.ts +84 -0
- package/templates/vike/ts/base/src/pages/+Layout.tsx +184 -0
- package/templates/vike/ts/base/src/pages/+config.ts +14 -0
- package/templates/vike/ts/base/src/pages/+onRenderClient.tsx +41 -0
- package/templates/vike/ts/base/src/pages/+onRenderHtml.tsx +28 -0
- package/templates/vike/ts/base/src/pages/PageShell.tsx +48 -0
- package/templates/vike/ts/base/src/pages/_error/+Page.tsx +14 -0
- package/templates/vike/ts/base/src/pages/about/+Page.tsx +14 -0
- package/templates/vike/ts/base/src/pages/index/+Page.tsx +33 -0
- package/templates/vike/ts/base/src/pages/index/Counter.tsx +20 -0
- package/templates/vike/ts/base/src/pages/index/Greeting.tsx +49 -0
- package/templates/vike/ts/base/src/pages/index/Page.test.tsx +67 -0
- package/templates/vike/ts/base/src/pages/usePageContext.tsx +27 -0
- package/templates/vike/ts/base/src/vike.d.ts +20 -0
- package/templates/vike/ts/base/tsconfig.json +18 -0
- package/templates/vike/ts/base/vite.config.ts +14 -0
- package/templates/vike/ts/base/vitest.config.ts +13 -0
- package/templates/vike/ts/sample-data/src/data/items.ts +64 -0
- package/templates/vike/ts/sample-data/src/pages/items/+Page.tsx +50 -0
- package/templates/vike/ts/sample-data/src/pages/items/+data.ts +28 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+Page.tsx +41 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+data.ts +37 -0
- package/templates/vike/ts/sample-data/src/pages/items/@id/+onBeforePrerenderStart.ts +8 -0
- package/templates/vike/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
- package/templates/vike/ts/ssg/src/pages/+config.ts +13 -0
- package/templates/vike/ts/ssg/src/pages/about/+Page.tsx +13 -0
- package/templates/vike/ts/ssr/src/pages/+config.ts +14 -0
- package/templates/vike/ts/ssr/src/pages/+onRenderHtml.tsx +28 -0
- package/templates/vike/ts/ssr/src/pages/about/+Page.tsx +14 -0
- package/templates/vike/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
- package/templates/vike/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/vike/ts/with/jotai/src/state/scheme.ts +4 -0
- package/templates/vike/ts/with/msw/_env +3 -0
- package/templates/vike/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/vike/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/vike/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/vike/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/vike/ts/with/msw/src/mocks/start.ts +10 -0
- package/templates/vike/ts/with/react-hook-form/src/pages/form/+Page.tsx +298 -0
- package/templates/vike/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
- package/templates/vike/ts/with/redux-toolkit/src/state/store.ts +29 -0
- package/templates/vike/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
- package/templates/vike/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/vike/ts/with/zustand/src/state/scheme.ts +19 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// @if ssg
|
|
2
|
+
import { cleanup, render, screen } from '@testing-library/react'
|
|
3
|
+
// @else
|
|
4
|
+
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
|
5
|
+
// @endif
|
|
6
|
+
// @if tanstack-query
|
|
7
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
8
|
+
// @endif
|
|
9
|
+
// @if redux-toolkit
|
|
10
|
+
import { Provider as StoreProvider } from 'react-redux'
|
|
11
|
+
// @endif
|
|
12
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
13
|
+
// @if redux-toolkit
|
|
14
|
+
import { store } from '../../state/store'
|
|
15
|
+
// @endif
|
|
16
|
+
import { Providers } from '../../theme/Providers'
|
|
17
|
+
import Page from './+Page'
|
|
18
|
+
|
|
19
|
+
/** The home page, inside the providers the app gives it. */
|
|
20
|
+
function renderPage() {
|
|
21
|
+
render(
|
|
22
|
+
<Providers>
|
|
23
|
+
{/* @if tanstack-query && redux-toolkit */}
|
|
24
|
+
<QueryClientProvider client={new QueryClient()}>
|
|
25
|
+
<StoreProvider store={store}>
|
|
26
|
+
<Page />
|
|
27
|
+
</StoreProvider>
|
|
28
|
+
</QueryClientProvider>
|
|
29
|
+
{/* @elif tanstack-query */}
|
|
30
|
+
<QueryClientProvider client={new QueryClient()}>
|
|
31
|
+
<Page />
|
|
32
|
+
</QueryClientProvider>
|
|
33
|
+
{/* @elif redux-toolkit */}
|
|
34
|
+
<StoreProvider store={store}>
|
|
35
|
+
<Page />
|
|
36
|
+
</StoreProvider>
|
|
37
|
+
{/* @else */}
|
|
38
|
+
<Page />
|
|
39
|
+
{/* @endif */}
|
|
40
|
+
</Providers>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe('the home page', () => {
|
|
45
|
+
// Vitest has no globals here, so Testing Library cannot clean up on its own.
|
|
46
|
+
afterEach(cleanup)
|
|
47
|
+
|
|
48
|
+
it('shows the app title', () => {
|
|
49
|
+
renderPage()
|
|
50
|
+
expect(screen.getByRole('heading', { name: '__TITLE_STRING__' })).toBeTruthy()
|
|
51
|
+
})
|
|
52
|
+
// @if !ssg
|
|
53
|
+
|
|
54
|
+
it('counts each press of its button', () => {
|
|
55
|
+
renderPage()
|
|
56
|
+
fireEvent.click(screen.getByRole('button', { name: 'Count' }))
|
|
57
|
+
expect(screen.getByText('Count: 1')).toBeTruthy()
|
|
58
|
+
})
|
|
59
|
+
// @endif
|
|
60
|
+
// @if msw
|
|
61
|
+
|
|
62
|
+
it('shows the greeting the mock API answers', async () => {
|
|
63
|
+
renderPage()
|
|
64
|
+
expect(await screen.findByText('Hello from the mock API')).toBeTruthy()
|
|
65
|
+
})
|
|
66
|
+
// @endif
|
|
67
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createContext, useContext, type ReactNode } from 'react'
|
|
2
|
+
import type { PageContext } from 'vike/types'
|
|
3
|
+
|
|
4
|
+
const Context = createContext<PageContext | undefined>(undefined)
|
|
5
|
+
|
|
6
|
+
/** Gives every component of the page its pageContext: https://vike.dev/pageContext */
|
|
7
|
+
export function PageContextProvider({
|
|
8
|
+
pageContext,
|
|
9
|
+
children,
|
|
10
|
+
}: {
|
|
11
|
+
readonly pageContext: PageContext
|
|
12
|
+
readonly children: ReactNode
|
|
13
|
+
}) {
|
|
14
|
+
return <Context.Provider value={pageContext}>{children}</Context.Provider>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The pageContext of the page on screen: its URL, its route's parameters, its data. */
|
|
18
|
+
export function usePageContext() {
|
|
19
|
+
const pageContext = useContext(Context)
|
|
20
|
+
if (!pageContext) throw new Error('usePageContext runs only inside a page.')
|
|
21
|
+
return pageContext
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** What the page's +data returned: https://vike.dev/data */
|
|
25
|
+
export function useData<Data>() {
|
|
26
|
+
return usePageContext().data as Data
|
|
27
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Types for what this app adds to Vike: each page's component, and the +Layout around it.
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
|
|
4
|
+
type Layout = (props: { children: ReactNode }) => ReactNode
|
|
5
|
+
|
|
6
|
+
declare global {
|
|
7
|
+
namespace Vike {
|
|
8
|
+
interface PageContext {
|
|
9
|
+
Page: () => ReactNode
|
|
10
|
+
}
|
|
11
|
+
interface Config {
|
|
12
|
+
Layout?: Layout
|
|
13
|
+
}
|
|
14
|
+
interface ConfigResolved {
|
|
15
|
+
Layout?: Layout
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export {}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"lib": ["ES2023", "DOM", "DOM.Iterable"],
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"verbatimModuleSyntax": true,
|
|
8
|
+
"moduleDetection": "force",
|
|
9
|
+
"noEmit": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"noUnusedLocals": true,
|
|
12
|
+
"noUnusedParameters": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"jsx": "react-jsx",
|
|
15
|
+
"types": ["vite/client"]
|
|
16
|
+
},
|
|
17
|
+
"include": ["src", "vite.config.ts"]
|
|
18
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import react from '@vitejs/plugin-react'
|
|
2
|
+
// @if msw
|
|
3
|
+
import { msw } from 'msw/vite'
|
|
4
|
+
// @endif
|
|
5
|
+
import vike from 'vike/plugin'
|
|
6
|
+
import { defineConfig } from 'vite'
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
// @if msw
|
|
10
|
+
plugins: [react(), vike(), msw({ mode: 'worker-only' })],
|
|
11
|
+
// @else
|
|
12
|
+
plugins: [react(), vike()],
|
|
13
|
+
// @endif
|
|
14
|
+
})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import react from '@vitejs/plugin-react'
|
|
2
|
+
import { defineConfig } from 'vitest/config'
|
|
3
|
+
|
|
4
|
+
// The tests render components in jsdom, with no Vike: a page is a component like any other.
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
plugins: [react()],
|
|
7
|
+
// @if msw
|
|
8
|
+
// The mock API answers the tests' requests, from Node, as it answers the app's in the browser.
|
|
9
|
+
test: { environment: 'jsdom', setupFiles: ['./src/mocks/setupTests.ts'] },
|
|
10
|
+
// @else
|
|
11
|
+
test: { environment: 'jsdom' },
|
|
12
|
+
// @endif
|
|
13
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// @if ts
|
|
2
|
+
/** One sample item. */
|
|
3
|
+
export interface Item {
|
|
4
|
+
id: string
|
|
5
|
+
title: string
|
|
6
|
+
summary: string
|
|
7
|
+
status: 'todo' | 'doing' | 'done'
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// @endif
|
|
11
|
+
/**
|
|
12
|
+
* Sample data, so the items pages have something to show: each page's +data reads it at build
|
|
13
|
+
* time. Make the +data hooks fetch your own data instead, and remove this file.
|
|
14
|
+
*/
|
|
15
|
+
export const items: readonly Item[] = [
|
|
16
|
+
{
|
|
17
|
+
id: 'theme',
|
|
18
|
+
title: 'Make the theme your own',
|
|
19
|
+
summary: 'Change the colors and fonts of both themes in src/theme/themes.ts.',
|
|
20
|
+
status: 'doing',
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: 'fonts',
|
|
24
|
+
title: 'Load the fonts',
|
|
25
|
+
summary: 'The theme names Space Grotesk and JetBrains Mono, and does not load them.',
|
|
26
|
+
status: 'todo',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
id: 'pages',
|
|
30
|
+
title: 'Add a page',
|
|
31
|
+
summary: 'A folder in src/pages with a +Page in it is a new route.',
|
|
32
|
+
status: 'todo',
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: 'data',
|
|
36
|
+
title: 'Read your own data',
|
|
37
|
+
// @if ssr
|
|
38
|
+
summary: 'Each +data here reads this file. Make it fetch from your API, on each request.',
|
|
39
|
+
// @else
|
|
40
|
+
summary: 'Each +data here reads this file. Make it fetch from your API, at build time.',
|
|
41
|
+
// @endif
|
|
42
|
+
status: 'todo',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
id: 'build',
|
|
46
|
+
title: 'Build it',
|
|
47
|
+
// @if ssr
|
|
48
|
+
summary: 'The server renders every page, these items among them, to HTML with its styles.',
|
|
49
|
+
// @else
|
|
50
|
+
summary: 'The build prerenders every page, these items among them, to HTML with its styles.',
|
|
51
|
+
// @endif
|
|
52
|
+
status: 'done',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: 'deploy',
|
|
56
|
+
title: 'Deploy it',
|
|
57
|
+
// @if ssr
|
|
58
|
+
summary: 'npm start runs the server the build wrote, on any Node host.',
|
|
59
|
+
// @else
|
|
60
|
+
summary: 'The build is static files in dist/client, which any static host serves.',
|
|
61
|
+
// @endif
|
|
62
|
+
status: 'todo',
|
|
63
|
+
},
|
|
64
|
+
]
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Link } from '@soroush.tech/design-system/Link'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
// @if tanstack-query
|
|
5
|
+
import { useQuery } from '@tanstack/react-query'
|
|
6
|
+
import { fetchItems } from '../../data/api'
|
|
7
|
+
// @elif swr
|
|
8
|
+
import useSWR from 'swr'
|
|
9
|
+
import { fetchItems } from '../../data/api'
|
|
10
|
+
// @endif
|
|
11
|
+
import { useData } from '../usePageContext'
|
|
12
|
+
import type { Data } from './+data'
|
|
13
|
+
import { StatusChip } from './StatusChip'
|
|
14
|
+
|
|
15
|
+
export default function Page() {
|
|
16
|
+
// @if tanstack-query
|
|
17
|
+
const { data: items } = useQuery({
|
|
18
|
+
queryKey: ['items'],
|
|
19
|
+
queryFn: fetchItems,
|
|
20
|
+
initialData: useData<Data>(),
|
|
21
|
+
})
|
|
22
|
+
// @elif swr
|
|
23
|
+
const initialItems = useData<Data>()
|
|
24
|
+
const { data: items = initialItems } = useSWR('items', fetchItems, {
|
|
25
|
+
fallbackData: initialItems,
|
|
26
|
+
})
|
|
27
|
+
// @else
|
|
28
|
+
const items = useData<Data>()
|
|
29
|
+
// @endif
|
|
30
|
+
return (
|
|
31
|
+
<Flex gap={2}>
|
|
32
|
+
<Typography variant="h2">Items</Typography>
|
|
33
|
+
<Typography color="secondary">
|
|
34
|
+
{/* @if ssr */}
|
|
35
|
+
Sample data from src/data/items.ts, which this page's +data reads on each request.
|
|
36
|
+
{/* @else */}
|
|
37
|
+
Sample data from src/data/items.ts, which this page's +data reads at build time.
|
|
38
|
+
{/* @endif */}
|
|
39
|
+
</Typography>
|
|
40
|
+
<Flex gap={1}>
|
|
41
|
+
{items.map((item) => (
|
|
42
|
+
<Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
|
|
43
|
+
<Link href={`/items/${item.id}`}>{item.title}</Link>
|
|
44
|
+
<StatusChip status={item.status} />
|
|
45
|
+
</Flex>
|
|
46
|
+
))}
|
|
47
|
+
</Flex>
|
|
48
|
+
</Flex>
|
|
49
|
+
)
|
|
50
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// @if ssr
|
|
2
|
+
import { items } from '../../data/items'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The list page's data: what it shows of each item. It runs on the server, on each request. https://vike.dev/data
|
|
6
|
+
*/
|
|
7
|
+
export function data() {
|
|
8
|
+
return items.map(({ id, title, status }) => ({ id, title, status }))
|
|
9
|
+
}
|
|
10
|
+
// @if ts
|
|
11
|
+
|
|
12
|
+
export type Data = ReturnType<typeof data>
|
|
13
|
+
// @endif
|
|
14
|
+
// @else
|
|
15
|
+
import { items } from '../../data/items'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The list page's data: what it shows of each item. It runs at build time, as the page is
|
|
19
|
+
* prerendered. https://vike.dev/data
|
|
20
|
+
*/
|
|
21
|
+
export function data() {
|
|
22
|
+
return items.map(({ id, title, status }) => ({ id, title, status }))
|
|
23
|
+
}
|
|
24
|
+
// @if ts
|
|
25
|
+
|
|
26
|
+
export type Data = ReturnType<typeof data>
|
|
27
|
+
// @endif
|
|
28
|
+
// @endif
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Link } from '@soroush.tech/design-system/Link'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
// @if tanstack-query
|
|
5
|
+
import { useQuery } from '@tanstack/react-query'
|
|
6
|
+
import { fetchItem } from '../../../data/api'
|
|
7
|
+
// @elif swr
|
|
8
|
+
import useSWR from 'swr'
|
|
9
|
+
import { fetchItem } from '../../../data/api'
|
|
10
|
+
// @endif
|
|
11
|
+
import { useData } from '../../usePageContext'
|
|
12
|
+
import { StatusChip } from '../StatusChip'
|
|
13
|
+
import type { Data } from './+data'
|
|
14
|
+
|
|
15
|
+
export default function Page() {
|
|
16
|
+
// @if tanstack-query
|
|
17
|
+
const initialItem = useData<Data>()
|
|
18
|
+
const { data: item } = useQuery({
|
|
19
|
+
queryKey: ['items', initialItem.id],
|
|
20
|
+
queryFn: () => fetchItem(initialItem.id),
|
|
21
|
+
initialData: initialItem,
|
|
22
|
+
})
|
|
23
|
+
// @elif swr
|
|
24
|
+
const initialItem = useData<Data>()
|
|
25
|
+
const { data: item = initialItem } = useSWR(
|
|
26
|
+
['items', initialItem.id],
|
|
27
|
+
() => fetchItem(initialItem.id),
|
|
28
|
+
{ fallbackData: initialItem }
|
|
29
|
+
)
|
|
30
|
+
// @else
|
|
31
|
+
const item = useData<Data>()
|
|
32
|
+
// @endif
|
|
33
|
+
return (
|
|
34
|
+
<Flex gap={2} alignItems="flex-start">
|
|
35
|
+
<Typography variant="h2">{item.title}</Typography>
|
|
36
|
+
<StatusChip status={item.status} />
|
|
37
|
+
<Typography color="secondary">{item.summary}</Typography>
|
|
38
|
+
<Link href="/items">All items</Link>
|
|
39
|
+
</Flex>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// @if ssr
|
|
2
|
+
import { render } from 'vike/abort'
|
|
3
|
+
import type { PageContextServer } from 'vike/types'
|
|
4
|
+
import { items } from '../../../data/items'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One item's data, by the id in the URL, or the 404 page for an id with no item. It runs on the
|
|
8
|
+
* server, on each request. https://vike.dev/data
|
|
9
|
+
*/
|
|
10
|
+
export function data(pageContext: PageContextServer) {
|
|
11
|
+
const item = items.find(({ id }) => id === pageContext.routeParams.id)
|
|
12
|
+
if (!item) throw render(404)
|
|
13
|
+
return item
|
|
14
|
+
}
|
|
15
|
+
// @if ts
|
|
16
|
+
|
|
17
|
+
export type Data = ReturnType<typeof data>
|
|
18
|
+
// @endif
|
|
19
|
+
// @else
|
|
20
|
+
import { render } from 'vike/abort'
|
|
21
|
+
import type { PageContextServer } from 'vike/types'
|
|
22
|
+
import { items } from '../../../data/items'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* One item's data, by the id in the URL, or the 404 page for an id with no item. It runs at build
|
|
26
|
+
* time, for each URL +onBeforePrerenderStart lists. https://vike.dev/data
|
|
27
|
+
*/
|
|
28
|
+
export function data(pageContext: PageContextServer) {
|
|
29
|
+
const item = items.find(({ id }) => id === pageContext.routeParams.id)
|
|
30
|
+
if (!item) throw render(404)
|
|
31
|
+
return item
|
|
32
|
+
}
|
|
33
|
+
// @if ts
|
|
34
|
+
|
|
35
|
+
export type Data = ReturnType<typeof data>
|
|
36
|
+
// @endif
|
|
37
|
+
// @endif
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// @if !ssr
|
|
2
|
+
import { items } from '../../../data/items'
|
|
3
|
+
|
|
4
|
+
/** The URLs of this route the build prerenders: one per item. https://vike.dev/onBeforePrerenderStart */
|
|
5
|
+
export function onBeforePrerenderStart() {
|
|
6
|
+
return items.map(({ id }) => `/items/${id}`)
|
|
7
|
+
}
|
|
8
|
+
// @endif
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Chip } from '@soroush.tech/design-system/Chip'
|
|
2
|
+
import type { Item } from '../../data/items'
|
|
3
|
+
|
|
4
|
+
const COLORS = { todo: 'default', doing: 'primary', done: 'success' } as const
|
|
5
|
+
|
|
6
|
+
/** An item's status, in its palette color. */
|
|
7
|
+
export function StatusChip({ status }: { readonly status: Item['status'] }) {
|
|
8
|
+
return <Chip label={status} size="sm" variant="outlined" color={COLORS[status]} />
|
|
9
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Config } from 'vike/types'
|
|
2
|
+
|
|
3
|
+
// The settings of every page: https://vike.dev/config
|
|
4
|
+
export default {
|
|
5
|
+
// SSG: every page is HTML at build time, with no JavaScript in the browser: a link loads the
|
|
6
|
+
// next page.
|
|
7
|
+
prerender: true,
|
|
8
|
+
// The page and +Layout, a setting of this app's own, load at build time only.
|
|
9
|
+
meta: {
|
|
10
|
+
Page: { env: { server: true, client: false } },
|
|
11
|
+
Layout: { env: { server: true, client: false } },
|
|
12
|
+
},
|
|
13
|
+
} satisfies Config
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
3
|
+
|
|
4
|
+
export default function Page() {
|
|
5
|
+
return (
|
|
6
|
+
<Flex gap={2}>
|
|
7
|
+
<Typography variant="h2">About</Typography>
|
|
8
|
+
<Typography color="secondary">
|
|
9
|
+
A second page: it is HTML with its styles from the build, and a link loads it as a new page.
|
|
10
|
+
</Typography>
|
|
11
|
+
</Flex>
|
|
12
|
+
)
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Config } from 'vike/types'
|
|
2
|
+
|
|
3
|
+
// The settings of every page: https://vike.dev/config
|
|
4
|
+
export default {
|
|
5
|
+
// SSR: every page is rendered on the server, on each request, then the app moves between pages
|
|
6
|
+
// in the browser, with no reload. Vike's own server runs the build: npm start.
|
|
7
|
+
server: true,
|
|
8
|
+
clientRouting: true,
|
|
9
|
+
hydrationCanBeAborted: true,
|
|
10
|
+
// +Layout is a setting of this app's own: the component every page renders inside.
|
|
11
|
+
meta: {
|
|
12
|
+
Layout: { env: { server: true, client: true } },
|
|
13
|
+
},
|
|
14
|
+
} satisfies Config
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import createEmotionServer from '@emotion/server/create-instance'
|
|
2
|
+
import { renderToString } from 'react-dom/server'
|
|
3
|
+
import { styleCache } from '@soroush.tech/design-system/engine'
|
|
4
|
+
import { dangerouslySkipEscape, escapeInject } from 'vike/server'
|
|
5
|
+
import type { OnRenderHtmlSync } from 'vike/types'
|
|
6
|
+
import { PageShell } from './PageShell'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Renders the page to HTML - on the server, on each request - with the styles it uses in the
|
|
10
|
+
* head, so it is drawn styled before any script runs.
|
|
11
|
+
*/
|
|
12
|
+
export const onRenderHtml: OnRenderHtmlSync = (pageContext) => {
|
|
13
|
+
const { extractCriticalToChunks, constructStyleTagsFromChunks } = createEmotionServer(styleCache)
|
|
14
|
+
const html = renderToString(<PageShell pageContext={pageContext} />)
|
|
15
|
+
const styles = constructStyleTagsFromChunks(extractCriticalToChunks(html))
|
|
16
|
+
return escapeInject`<!doctype html>
|
|
17
|
+
<html lang="en">
|
|
18
|
+
<head>
|
|
19
|
+
<meta charset="UTF-8" />
|
|
20
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
21
|
+
<title>__TITLE__</title>
|
|
22
|
+
${dangerouslySkipEscape(styles)}
|
|
23
|
+
</head>
|
|
24
|
+
<body>
|
|
25
|
+
<div id="root">${dangerouslySkipEscape(html)}</div>
|
|
26
|
+
</body>
|
|
27
|
+
</html>`
|
|
28
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
3
|
+
|
|
4
|
+
export default function Page() {
|
|
5
|
+
return (
|
|
6
|
+
<Flex gap={2}>
|
|
7
|
+
<Typography variant="h2">About</Typography>
|
|
8
|
+
<Typography color="secondary">
|
|
9
|
+
A second page: the server renders it, with its styles, and the app moves to it without a
|
|
10
|
+
reload.
|
|
11
|
+
</Typography>
|
|
12
|
+
</Flex>
|
|
13
|
+
)
|
|
14
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
2
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
import { useAtom } from 'jotai'
|
|
5
|
+
import { countAtom } from '../../state/counter'
|
|
6
|
+
|
|
7
|
+
/** A count, held in a Jotai atom, src/state/counter, and the button that adds to it. */
|
|
8
|
+
export function Counter() {
|
|
9
|
+
const [count, setCount] = useAtom(countAtom)
|
|
10
|
+
const increment = () => setCount(count + 1)
|
|
11
|
+
return (
|
|
12
|
+
<Flex flexDirection="row" alignItems="center" gap={2}>
|
|
13
|
+
<Button size="sm" variant="outlined" onClick={increment}>
|
|
14
|
+
Count
|
|
15
|
+
</Button>
|
|
16
|
+
<Typography>Count: {count}</Typography>
|
|
17
|
+
</Flex>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { http, HttpResponse } from 'msw'
|
|
2
|
+
// @if sample-data
|
|
3
|
+
import { items } from '../data/items'
|
|
4
|
+
// @endif
|
|
5
|
+
|
|
6
|
+
/** The mock API: what it answers, by method and path. Add a handler for each call the app makes. */
|
|
7
|
+
export const handlers = [
|
|
8
|
+
http.get('/api/greeting', () => HttpResponse.json({ message: 'Hello from the mock API' })),
|
|
9
|
+
// @if react-hook-form
|
|
10
|
+
http.post<never, { name: string }>('/api/messages', async ({ request }) => {
|
|
11
|
+
const { name } = await request.json()
|
|
12
|
+
return HttpResponse.json({ message: `Thanks, ${name}: the mock API took your message.` })
|
|
13
|
+
}),
|
|
14
|
+
// @endif
|
|
15
|
+
// @if sample-data
|
|
16
|
+
http.get('/api/items', () =>
|
|
17
|
+
HttpResponse.json(items.map(({ id, title, status }) => ({ id, title, status })))
|
|
18
|
+
),
|
|
19
|
+
http.get('/api/items/:id', ({ params }) => {
|
|
20
|
+
const item = items.find(({ id }) => id === params.id)
|
|
21
|
+
return item ? HttpResponse.json(item) : new HttpResponse(null, { status: 404 })
|
|
22
|
+
}),
|
|
23
|
+
// @endif
|
|
24
|
+
]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll } from 'vitest'
|
|
2
|
+
import { server } from './server'
|
|
3
|
+
|
|
4
|
+
// The mock API answers every request a test makes; one it has no handler for fails the test.
|
|
5
|
+
beforeAll(() => server.listen({ onUnhandledFrame: 'error' }))
|
|
6
|
+
afterEach(() => server.resetHandlers())
|
|
7
|
+
afterAll(() => server.close())
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starts the mock API in the browser while VITE_MSW, in .env, is true. Set it to false once the
|
|
3
|
+
* app's own API answers.
|
|
4
|
+
*/
|
|
5
|
+
export async function startMocks() {
|
|
6
|
+
if (import.meta.env.VITE_MSW !== 'true') return
|
|
7
|
+
const { worker } = await import('./browser')
|
|
8
|
+
// A request the mock API has no handler for - a page the router fetches - goes to the network.
|
|
9
|
+
await worker.start({ onUnhandledFrame: 'bypass' })
|
|
10
|
+
}
|