create-foldkit-app 0.31.1 → 0.32.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 (77) hide show
  1. package/dist/templates/base/FOLDKIT.md +1 -1
  2. package/dist/templates/examples/api-cache/package.json +7 -7
  3. package/dist/templates/examples/auth/package.json +7 -7
  4. package/dist/templates/examples/auth/src/page/loggedOut/page/login.ts +28 -35
  5. package/dist/templates/examples/auth/src/update.ts +10 -13
  6. package/dist/templates/examples/canvas-art/package.json +7 -7
  7. package/dist/templates/examples/charting/package.json +7 -7
  8. package/dist/templates/examples/charting/src/view/chart.ts +43 -45
  9. package/dist/templates/examples/counter/package.json +7 -7
  10. package/dist/templates/examples/counters/package.json +7 -7
  11. package/dist/templates/examples/crash-view/package.json +7 -7
  12. package/dist/templates/examples/embedding/package.json +7 -7
  13. package/dist/templates/examples/form/package.json +7 -7
  14. package/dist/templates/examples/form/src/main.ts +59 -74
  15. package/dist/templates/examples/generative-art/package.json +7 -7
  16. package/dist/templates/examples/interrupting-commands/package.json +7 -7
  17. package/dist/templates/examples/interrupting-commands/src/main.ts +6 -9
  18. package/dist/templates/examples/job-application/package.json +7 -7
  19. package/dist/templates/examples/job-application/src/view/education.ts +31 -32
  20. package/dist/templates/examples/job-application/src/view/field.ts +7 -8
  21. package/dist/templates/examples/job-application/src/view/workHistory.ts +40 -41
  22. package/dist/templates/examples/kanban/package.json +8 -8
  23. package/dist/templates/examples/managed-resource-layer/package.json +7 -7
  24. package/dist/templates/examples/managed-resource-layer/src/main.ts +10 -11
  25. package/dist/templates/examples/map/package.json +8 -8
  26. package/dist/templates/examples/map/src/main.ts +59 -63
  27. package/dist/templates/examples/map/src/vite-env.d.ts +1 -0
  28. package/dist/templates/examples/pixel-art/package.json +7 -7
  29. package/dist/templates/examples/pixel-art/src/scene.test.ts +3 -3
  30. package/dist/templates/examples/pixel-art/src/story.test.ts +3 -3
  31. package/dist/templates/examples/query-sync/package.json +7 -7
  32. package/dist/templates/examples/query-sync/src/main.ts +25 -37
  33. package/dist/templates/examples/route-transitions/package.json +7 -7
  34. package/dist/templates/examples/route-transitions/src/main.ts +24 -32
  35. package/dist/templates/examples/routing/package.json +7 -7
  36. package/dist/templates/examples/routing/src/main.ts +24 -30
  37. package/dist/templates/examples/routing/src/page/people.ts +80 -86
  38. package/dist/templates/examples/shopping-cart/package.json +7 -7
  39. package/dist/templates/examples/shopping-cart/src/main.ts +16 -22
  40. package/dist/templates/examples/slow-warnings/package.json +7 -7
  41. package/dist/templates/examples/snake/package.json +7 -7
  42. package/dist/templates/examples/ssg/package.json +6 -8
  43. package/dist/templates/examples/ssg/src/main.ts +65 -75
  44. package/dist/templates/examples/ssr/package.json +8 -9
  45. package/dist/templates/examples/state-machine/package.json +7 -7
  46. package/dist/templates/examples/state-machine/src/view.ts +50 -59
  47. package/dist/templates/examples/stopwatch/package.json +7 -7
  48. package/dist/templates/examples/todo/package.json +7 -7
  49. package/dist/templates/examples/todo/src/main.ts +29 -37
  50. package/dist/templates/examples/ui-showcase/package.json +7 -7
  51. package/dist/templates/examples/ui-showcase/src/main.ts +49 -42
  52. package/dist/templates/examples/ui-showcase/src/scene.test.ts +11 -0
  53. package/dist/templates/examples/ui-showcase/src/ui/init.ts +2 -0
  54. package/dist/templates/examples/ui-showcase/src/ui/message.ts +2 -0
  55. package/dist/templates/examples/ui-showcase/src/ui/model.ts +2 -0
  56. package/dist/templates/examples/ui-showcase/src/ui/update.ts +21 -0
  57. package/dist/templates/examples/ui-showcase/src/ui/view/hoverIntent.ts +89 -0
  58. package/dist/templates/examples/ui-showcase/src/ui/view/index.ts +1 -0
  59. package/dist/templates/examples/view-transitions/package.json +7 -7
  60. package/dist/templates/examples/view-transitions/src/main.ts +29 -40
  61. package/dist/templates/examples/weather/package.json +7 -7
  62. package/dist/templates/examples/web-components/package.json +7 -7
  63. package/dist/templates/examples/websocket-chat/package.json +7 -7
  64. package/dist/templates/examples/websocket-chat/src/main.ts +22 -32
  65. package/dist/templates/release.json +18 -18
  66. package/dist/templates/rendering/ssg/README.md +8 -7
  67. package/dist/templates/rendering/ssg/package.json +2 -2
  68. package/dist/templates/rendering/ssg/src/main.ts +61 -70
  69. package/dist/templates/rendering/ssg/tsconfig.json +1 -1
  70. package/dist/templates/rendering/ssg/vite.config.ts +22 -1
  71. package/dist/templates/rendering/ssr/README.md +10 -7
  72. package/dist/templates/rendering/ssr/package.json +2 -2
  73. package/dist/templates/rendering/ssr/vite.config.ts +22 -1
  74. package/package.json +5 -5
  75. package/dist/templates/rendering/ssg/scripts/build.mjs +0 -27
  76. package/dist/templates/rendering/ssg/scripts/prerender.ts +0 -120
  77. package/dist/templates/rendering/ssr/scripts/build.mjs +0 -23
@@ -1,27 +1,27 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
3
  "channel": "stable",
4
- "sourceCommit": "a7edce98aebd4863db715e549c0b13d5ccc97bb7",
4
+ "sourceCommit": "00c836c7927fd2e89e969c4b25974a371e299451",
5
5
  "packages": {
6
- "create-foldkit-app": "0.31.1",
7
- "@foldkit/devtools": "0.154.0",
8
- "@foldkit/devtools-mcp": "0.19.1",
9
- "foldkit": "0.154.0",
10
- "@foldkit/markdown": "0.8.0",
11
- "@foldkit/oxlint-plugin": "0.10.0",
12
- "@foldkit/ui": "0.154.0",
13
- "@foldkit/vite-plugin": "0.19.0"
6
+ "create-foldkit-app": "0.32.0",
7
+ "@foldkit/devtools": "0.155.0",
8
+ "@foldkit/devtools-mcp": "0.19.2",
9
+ "foldkit": "0.155.0",
10
+ "@foldkit/markdown": "0.8.1",
11
+ "@foldkit/oxlint-plugin": "0.11.0",
12
+ "@foldkit/ui": "0.155.0",
13
+ "@foldkit/vite-plugin": "0.20.0"
14
14
  },
15
15
  "dependencies": {
16
- "@foldkit/devtools": "0.154.0",
17
- "@foldkit/vite-plugin": "0.19.0",
18
- "@foldkit/devtools-mcp": "0.19.1",
19
- "@foldkit/oxlint-plugin": "0.10.0",
16
+ "@foldkit/devtools": "0.155.0",
17
+ "@foldkit/vite-plugin": "0.20.0",
18
+ "@foldkit/devtools-mcp": "0.19.2",
19
+ "@foldkit/oxlint-plugin": "0.11.0",
20
20
  "@trivago/prettier-plugin-sort-imports": "^6.0.2",
21
- "@types/node": "^25.9.3",
22
- "happy-dom": "^20.10.4",
23
- "oxlint": "^1.70.0",
24
- "prettier": "^3.8.4",
25
- "vitest": "^4.1.9"
21
+ "@types/node": "^26.4.0",
22
+ "happy-dom": "^20.11.13",
23
+ "oxlint": "^1.80.0",
24
+ "prettier": "^3.9.6",
25
+ "vitest": "^4.1.11"
26
26
  }
27
27
  }
@@ -16,9 +16,9 @@ A statically generated Foldkit application built with Effect.
16
16
  {{previewCommand}}
17
17
  ```
18
18
 
19
- The build script runs `scripts/build.mjs`, which builds the client bundle, builds
20
- the server bundle, prerenders every path `src/entry.server.ts` lists, and gives
21
- all three steps the same build id.
19
+ The build is one `vite build`. `@foldkit/vite-plugin` builds the client bundle,
20
+ builds the server bundle, and generates a page for every path
21
+ `src/entry.server.ts` lists, all from that one command.
22
22
 
23
23
  ## The build id
24
24
 
@@ -41,9 +41,10 @@ puts in that position, carrying what the visitor typed into it.
41
41
  The comparison happens when a client boots against a page. A tab whose client
42
42
  is already running when a deployment lands is not rechecked.
43
43
 
44
- `scripts/build.mjs` takes care of this: it produces one id per build and passes
45
- it to every step. Supply `FOLDKIT_BUILD_ID` when those steps run in separate
46
- jobs, or when you want the served id to name a deployment you can look up later:
44
+ `vite.config.ts` takes care of this: it produces one id per build, and the one
45
+ `vite build` that reads it hands that id to the client and the server alike.
46
+ Supply `FOLDKIT_BUILD_ID` when you want the served id to name a deployment you
47
+ can look up later:
47
48
 
48
49
  ```bash
49
50
  FOLDKIT_BUILD_ID="$CI_DEPLOYMENT_ID" {{buildCommand}}
@@ -53,7 +54,7 @@ The id is public HTML and must never contain a secret or be derived from one.
53
54
  Every step of one deployment must share an id. By contrast, two deployments
54
55
  must never share one. Reusing an id produces no warning: the ids agree, so
55
56
  hydration proceeds. When in doubt, leave `FOLDKIT_BUILD_ID` unset and let the
56
- build script generate one.
57
+ build generate one.
57
58
 
58
59
  ## Learn More
59
60
 
@@ -4,11 +4,11 @@
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite",
7
- "build": "node scripts/build.mjs",
7
+ "build": "vite build",
8
8
  "preview": "vite preview --outDir dist/client",
9
9
  "typecheck": "tsc --noEmit",
10
10
  "format": "prettier -w .",
11
11
  "test": "vitest run",
12
- "lint": "oxlint src scripts"
12
+ "lint": "oxlint src"
13
13
  }
14
14
  }
@@ -1,4 +1,4 @@
1
- import { Effect, Match as M, Schema as S, pipe } from 'effect'
1
+ import { Effect, Schema as S, pipe } from 'effect'
2
2
  import { Command, Runtime, type Update } from 'foldkit'
3
3
  import { type Document, type Html, type HtmlBuilder } from 'foldkit/html'
4
4
  import { defineMessageUnion } from 'foldkit/message'
@@ -53,7 +53,6 @@ const LoadExternal = Command.define('LoadExternal', {
53
53
  // UPDATE
54
54
 
55
55
  type UpdateReturn = Update.Return<Model, Message>
56
- const withUpdateReturn = M.withReturnType<UpdateReturn>()
57
56
 
58
57
  export const update = (model: Model, message: Message) =>
59
58
  Message.match<UpdateReturn>(message, {
@@ -61,19 +60,16 @@ export const update = (model: Model, message: Message) =>
61
60
  model: evo(model, { count: count => count + 1 }),
62
61
  }),
63
62
  ClickedLink: ({ request }) =>
64
- M.value(request).pipe(
65
- withUpdateReturn,
66
- M.tagsExhaustive({
67
- Internal: ({ url }) => ({
68
- model,
69
- commands: [NavigateInternal({ url: urlToString(url) })],
70
- }),
71
- External: ({ href }) => ({
72
- model,
73
- commands: [LoadExternal({ href })],
74
- }),
63
+ UrlRequest.match<UpdateReturn>(request, {
64
+ Internal: ({ url }) => ({
65
+ model,
66
+ commands: [NavigateInternal({ url: urlToString(url) })],
75
67
  }),
76
- ),
68
+ External: ({ href }) => ({
69
+ model,
70
+ commands: [LoadExternal({ href })],
71
+ }),
72
+ }),
77
73
  ChangedUrl: ({ url }) => ({
78
74
  model: evo(model, { route: () => urlToAppRoute(url) }),
79
75
  }),
@@ -89,8 +85,7 @@ const appendAppName = (page: string): string => `${page} | ${APP_NAME}`
89
85
 
90
86
  const routeTitle = (route: AppRoute): string =>
91
87
  pipe(
92
- M.value(route),
93
- M.tagsExhaustive({
88
+ AppRoute.match(route, {
94
89
  Home: () => 'Home',
95
90
  About: () => 'About',
96
91
  NotFound: () => 'Not Found',
@@ -108,60 +103,56 @@ const navigationView = (h: HtmlBuilder<Message>): Html =>
108
103
  )
109
104
 
110
105
  const pageView = (model: Model, h: HtmlBuilder<Message>): Html =>
111
- M.value(model.route).pipe(
112
- M.tagsExhaustive({
113
- Home: () =>
114
- h.section(
115
- [h.Class('grid gap-4')],
116
- [
117
- h.h1(
118
- [h.Id('page-title'), h.Class('text-4xl font-bold')],
119
- ['Statically generated home'],
120
- ),
121
- h.p(
122
- [],
123
- [
124
- 'This route was rendered during the build and hydrated in place.',
125
- ],
126
- ),
127
- h.button(
128
- [
129
- h.OnClick(Message.ClickedIncrement()),
130
- h.Class('w-fit bg-black px-4 py-2 text-white'),
131
- ],
132
- [`Count: ${model.count}`],
133
- ),
134
- ],
135
- ),
136
- About: () =>
137
- h.section(
138
- [h.Class('grid gap-4')],
139
- [
140
- h.h1(
141
- [h.Id('page-title'), h.Class('text-4xl font-bold')],
142
- ['Statically generated about page'],
143
- ),
144
- h.p(
145
- [],
146
- [
147
- 'The same renderPage function produced this route in the same build.',
148
- ],
149
- ),
150
- ],
151
- ),
152
- NotFound: ({ path }) =>
153
- h.section(
154
- [h.Class('grid gap-4')],
155
- [
156
- h.h1(
157
- [h.Id('page-title'), h.Class('text-4xl font-bold')],
158
- ['Not found'],
159
- ),
160
- h.p([], [`No statically generated page exists for ${path}.`]),
161
- ],
162
- ),
163
- }),
164
- )
106
+ AppRoute.match(model.route, {
107
+ Home: () =>
108
+ h.section(
109
+ [h.Class('grid gap-4')],
110
+ [
111
+ h.h1(
112
+ [h.Id('page-title'), h.Class('text-4xl font-bold')],
113
+ ['Statically generated home'],
114
+ ),
115
+ h.p(
116
+ [],
117
+ ['This route was rendered during the build and hydrated in place.'],
118
+ ),
119
+ h.button(
120
+ [
121
+ h.OnClick(Message.ClickedIncrement()),
122
+ h.Class('w-fit bg-black px-4 py-2 text-white'),
123
+ ],
124
+ [`Count: ${model.count}`],
125
+ ),
126
+ ],
127
+ ),
128
+ About: () =>
129
+ h.section(
130
+ [h.Class('grid gap-4')],
131
+ [
132
+ h.h1(
133
+ [h.Id('page-title'), h.Class('text-4xl font-bold')],
134
+ ['Statically generated about page'],
135
+ ),
136
+ h.p(
137
+ [],
138
+ [
139
+ 'The same renderPage function produced this route in the same build.',
140
+ ],
141
+ ),
142
+ ],
143
+ ),
144
+ NotFound: ({ path }) =>
145
+ h.section(
146
+ [h.Class('grid gap-4')],
147
+ [
148
+ h.h1(
149
+ [h.Id('page-title'), h.Class('text-4xl font-bold')],
150
+ ['Not found'],
151
+ ),
152
+ h.p([], [`No statically generated page exists for ${path}.`]),
153
+ ],
154
+ ),
155
+ })
165
156
 
166
157
  export const view = (model: Model, h: HtmlBuilder<Message>): Document => ({
167
158
  title: routeTitle(model.route),
@@ -12,5 +12,5 @@
12
12
  "isolatedModules": true,
13
13
  "noEmit": true
14
14
  },
15
- "include": ["src/**/*", "scripts/**/*"]
15
+ "include": ["src/**/*"]
16
16
  }
@@ -1,14 +1,35 @@
1
+ import { randomUUID } from 'node:crypto'
1
2
  import { defineConfig } from 'vite'
2
3
 
3
4
  import { foldkit } from '@foldkit/vite-plugin'
4
5
  import tailwindcss from '@tailwindcss/vite'
5
6
 
7
+ // NOTE: one id has to name this build, and a build that has none refuses to
8
+ // render a hydratable page. A deployment sets FOLDKIT_BUILD_ID to a value it
9
+ // already has, such as a commit or a release tag; a local build takes a fresh
10
+ // one rather than a constant, which would make a stale page look current.
11
+ // NOTE: the fallback is stored back into the environment because Vite reads
12
+ // this file once per environment it builds, and a fresh id per read would give
13
+ // the browser bundle and the server bundle different ids, which is the exact
14
+ // disagreement the id exists to catch: every page of a deployment refused at
15
+ // hydration. Storing it means every later read of this file, in this process,
16
+ // resolves the same id.
17
+ // NOTE: `||=` rather than `??=` because the plugin treats an empty
18
+ // FOLDKIT_BUILD_ID as absent, so an empty value has to take the fallback here
19
+ // too; `??=` would keep it and the build would compile no id at all.
20
+ process.env['FOLDKIT_BUILD_ID'] ||= randomUUID()
21
+ const buildId = process.env['FOLDKIT_BUILD_ID']
22
+
6
23
  export default defineConfig({
7
24
  plugins: [
8
25
  tailwindcss(),
9
26
  foldkit({
27
+ buildId,
10
28
  devToolsMcpPort: 9988,
11
- ssr: { serverEntry: '/src/entry.server.ts' },
29
+ ssr: {
30
+ serverEntry: '/src/entry.server.ts',
31
+ build: { prerender: true },
32
+ },
12
33
  }),
13
34
  ],
14
35
  optimizeDeps: {
@@ -16,8 +16,8 @@ A server-rendered Foldkit application built with Effect.
16
16
  {{startCommand}}
17
17
  ```
18
18
 
19
- The build script runs `scripts/build.mjs`, which builds the client bundle and the
20
- server bundle and gives both the same build id.
19
+ The build is one `vite build`. `@foldkit/vite-plugin` builds the client bundle
20
+ and the server bundle from that one command, and gives both the same build id.
21
21
 
22
22
  `PORT` sets the port the server listens on. `ORIGIN` sets the public origin it
23
23
  serves, which the server entry sees as `Request.url`; set it when deploying
@@ -44,10 +44,13 @@ puts in that position, carrying what the visitor typed into it.
44
44
  The comparison happens when a client boots against a page. A tab whose client
45
45
  is already running when a deployment lands is not rechecked.
46
46
 
47
- `scripts/build.mjs` takes care of this: it produces one id per build and passes
48
- it to both build commands. Supply `FOLDKIT_BUILD_ID` when the two commands run
49
- in separate jobs, or when you want the served id to name a deployment you can
50
- look up later:
47
+ `vite.config.ts` takes care of this: it reads `FOLDKIT_BUILD_ID` and generates
48
+ one when the variable is unset, storing it back so every later read of the
49
+ config resolves the same id. Vite reads the config once per environment it
50
+ builds, so a config that generated a fresh id each time would give the browser
51
+ bundle and the server bundle different ids. Supply `FOLDKIT_BUILD_ID` when the
52
+ build runs in separate jobs, or when you want the served id to name a
53
+ deployment you can look up later:
51
54
 
52
55
  ```bash
53
56
  FOLDKIT_BUILD_ID="$CI_DEPLOYMENT_ID" {{buildCommand}}
@@ -57,7 +60,7 @@ The id is public HTML and must never contain a secret or be derived from one.
57
60
  The client and server halves of one deployment must share an id. By contrast,
58
61
  two deployments must never share one. Reusing an id produces no warning: the
59
62
  ids agree, so hydration proceeds. When in doubt, leave `FOLDKIT_BUILD_ID` unset
60
- and let the build script generate one.
63
+ and let the build generate one.
61
64
 
62
65
  ## Learn More
63
66
 
@@ -4,11 +4,11 @@
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite",
7
- "build": "node scripts/build.mjs",
7
+ "build": "vite build",
8
8
  "start": "node dist/server/main.js",
9
9
  "typecheck": "tsc --noEmit",
10
10
  "format": "prettier -w .",
11
11
  "test": "vitest run",
12
- "lint": "oxlint src server scripts"
12
+ "lint": "oxlint src server"
13
13
  }
14
14
  }
@@ -1,14 +1,35 @@
1
+ import { randomUUID } from 'node:crypto'
1
2
  import { defineConfig } from 'vite'
2
3
 
3
4
  import { foldkit } from '@foldkit/vite-plugin'
4
5
  import tailwindcss from '@tailwindcss/vite'
5
6
 
7
+ // NOTE: one id has to name this build, and a build that has none refuses to
8
+ // render a hydratable page. A deployment sets FOLDKIT_BUILD_ID to a value it
9
+ // already has, such as a commit or a release tag; a local build takes a fresh
10
+ // one rather than a constant, which would make a stale page look current.
11
+ // NOTE: the fallback is stored back into the environment because Vite reads
12
+ // this file once per environment it builds, and a fresh id per read would give
13
+ // the browser bundle and the server bundle different ids, which is the exact
14
+ // disagreement the id exists to catch: every page of a deployment refused at
15
+ // hydration. Storing it means every later read of this file, in this process,
16
+ // resolves the same id.
17
+ // NOTE: `||=` rather than `??=` because the plugin treats an empty
18
+ // FOLDKIT_BUILD_ID as absent, so an empty value has to take the fallback here
19
+ // too; `??=` would keep it and the build would compile no id at all.
20
+ process.env['FOLDKIT_BUILD_ID'] ||= randomUUID()
21
+ const buildId = process.env['FOLDKIT_BUILD_ID']
22
+
6
23
  export default defineConfig({
7
24
  plugins: [
8
25
  tailwindcss(),
9
26
  foldkit({
27
+ buildId,
10
28
  devToolsMcpPort: 9988,
11
- ssr: { serverEntry: '/src/entry.server.ts' },
29
+ ssr: {
30
+ serverEntry: '/src/entry.server.ts',
31
+ build: { entry: '/server/main.ts' },
32
+ },
12
33
  }),
13
34
  ],
14
35
  optimizeDeps: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-foldkit-app",
3
- "version": "0.31.1",
3
+ "version": "0.32.0",
4
4
  "description": "Create Foldkit applications",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -13,15 +13,15 @@
13
13
  "dependencies": {
14
14
  "@effect/platform-node": "4.0.0-rc.112",
15
15
  "@effect/platform-node-shared": "4.0.0-rc.112",
16
- "chalk": "^5.6.2",
16
+ "chalk": "^6.0.0",
17
17
  "effect": "4.0.0-rc.112",
18
18
  "rimraf": "^6.1.3",
19
- "typescript": "^6.0.3"
19
+ "typescript": "^7.0.2"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@effect/vitest": "4.0.0-rc.112",
23
- "@types/node": "^25.9.3",
24
- "vitest": "^4.1.9"
23
+ "@types/node": "^26.4.0",
24
+ "vitest": "^4.1.11"
25
25
  },
26
26
  "keywords": [
27
27
  "create-foldkit-app",
@@ -1,27 +0,0 @@
1
- import { spawnSync } from 'node:child_process'
2
- import { randomUUID } from 'node:crypto'
3
-
4
- // NOTE: one nonempty id must reach every step in this build.
5
- const supplied = process.env.FOLDKIT_BUILD_ID
6
- const buildId =
7
- supplied === undefined || supplied === '' ? randomUUID() : supplied
8
-
9
- const steps = [
10
- ['vite', ['build', '--outDir', 'dist/client']],
11
- [
12
- 'vite',
13
- ['build', '--ssr', 'src/entry.server.ts', '--outDir', 'dist/server'],
14
- ],
15
- ['tsx', ['scripts/prerender.ts']],
16
- ]
17
-
18
- for (const [command, args] of steps) {
19
- const { status } = spawnSync(command, args, {
20
- stdio: 'inherit',
21
- shell: process.platform === 'win32',
22
- env: { ...process.env, FOLDKIT_BUILD_ID: buildId },
23
- })
24
- if (status !== 0) {
25
- process.exit(status ?? 1)
26
- }
27
- }
@@ -1,120 +0,0 @@
1
- import { Console, Effect, FileSystem } from 'effect'
2
- import { Server } from 'foldkit/experimental'
3
- import { dirname, resolve } from 'node:path'
4
- import { fileURLToPath, pathToFileURL } from 'node:url'
5
-
6
- import { NodeRuntime, NodeServices } from '@effect/platform-node'
7
-
8
- import type * as ServerEntry from '../src/entry.server'
9
-
10
- const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url))
11
- const PROJECT_DIR = resolve(SCRIPT_DIR, '..')
12
- const CLIENT_DIR = resolve(PROJECT_DIR, 'dist/client')
13
- const SERVER_ENTRY_PATH = resolve(PROJECT_DIR, 'dist/server/entry.server.js')
14
- const SITE_ORIGIN = 'https://example.com'
15
- const INDEX_PATH = resolve(CLIENT_DIR, 'index.html')
16
- const TEMPLATE_COPY_PATH = resolve(
17
- PROJECT_DIR,
18
- 'node_modules/.cache/foldkit/prerender-template.html',
19
- )
20
- const CONTAINER_ID = 'root'
21
- const CONTAINER_PLACEHOLDER = `<div id="${CONTAINER_ID}"></div>`
22
-
23
- const loadServerEntry: Effect.Effect<typeof ServerEntry> = Effect.promise(
24
- () => import(pathToFileURL(SERVER_ENTRY_PATH).href),
25
- )
26
-
27
- // NOTE: the generated `/` is written over `index.html`, which is also where
28
- // the client build leaves the template, so reading the template from that file
29
- // works once and then reads back a page whose placeholder is already replaced.
30
- // The built file is authoritative while it still holds the placeholder, and
31
- // the cached copy it leaves behind is what lets a re-run against one client
32
- // build generate the same pages. The placeholder is the condition
33
- // `injectIntoTemplate` itself enforces, so a static render, which carries no
34
- // hydration stamp, is covered by the same test.
35
- const readTemplate = Effect.gen(function* () {
36
- const fs = yield* FileSystem.FileSystem
37
-
38
- const isBuilt = yield* fs.exists(INDEX_PATH)
39
- if (!isBuilt) {
40
- return yield* Effect.die(
41
- new Error(
42
- `Cannot prerender without a client build: "${INDEX_PATH}" does not exist.`,
43
- ),
44
- )
45
- }
46
-
47
- const builtIndex = yield* fs.readFileString(INDEX_PATH)
48
- const isTemplate = builtIndex.includes(CONTAINER_PLACEHOLDER)
49
- if (isTemplate) {
50
- yield* fs.makeDirectory(dirname(TEMPLATE_COPY_PATH), { recursive: true })
51
- yield* fs.writeFileString(TEMPLATE_COPY_PATH, builtIndex)
52
- return builtIndex
53
- }
54
-
55
- const hasTemplateCopy = yield* fs.exists(TEMPLATE_COPY_PATH)
56
- if (!hasTemplateCopy) {
57
- return yield* Effect.die(
58
- new Error(
59
- `Cannot prerender: "${INDEX_PATH}" no longer holds the ${CONTAINER_PLACEHOLDER} placeholder, and no copy of the template remains. Run the client build again.`,
60
- ),
61
- )
62
- }
63
-
64
- return yield* fs.readFileString(TEMPLATE_COPY_PATH)
65
- })
66
-
67
- const outputFileFor = (path: string): string => {
68
- const url = new URL(path, SITE_ORIGIN)
69
- if (url.origin !== SITE_ORIGIN || url.pathname !== path) {
70
- throw new Error(
71
- `Cannot generate the non-normalized same-origin path "${path}".`,
72
- )
73
- }
74
- return path === '/'
75
- ? INDEX_PATH
76
- : resolve(CLIENT_DIR, path.slice(1), 'index.html')
77
- }
78
-
79
- const program = Effect.gen(function* () {
80
- const fs = yield* FileSystem.FileSystem
81
- const template = yield* readTemplate
82
- const serverEntry = yield* loadServerEntry
83
-
84
- for (const path of serverEntry.prerenderPaths) {
85
- const result = yield* Effect.promise(() =>
86
- serverEntry.renderPage(new Request(`${SITE_ORIGIN}${path}`)),
87
- )
88
- if (result._tag === 'Responded') {
89
- return yield* Effect.die(
90
- new Error(
91
- `Cannot write the complete Response returned while generating "${path}" to a static HTML file.`,
92
- ),
93
- )
94
- }
95
- if (result.status !== undefined && result.status !== 200) {
96
- return yield* Effect.die(
97
- new Error(
98
- `Cannot preserve status ${result.status} while generating "${path}" as a static HTML file.`,
99
- ),
100
- )
101
- }
102
- if (result.headers !== undefined) {
103
- return yield* Effect.die(
104
- new Error(
105
- `Cannot preserve response headers while generating "${path}" as a static HTML file.`,
106
- ),
107
- )
108
- }
109
- const html = Server.injectIntoTemplate(template, result.application, {
110
- containerId: CONTAINER_ID,
111
- })
112
- const outputFile = outputFileFor(path)
113
-
114
- yield* fs.makeDirectory(dirname(outputFile), { recursive: true })
115
- yield* fs.writeFileString(outputFile, html)
116
- yield* Console.log(`Generated ${path}`)
117
- }
118
- }).pipe(Effect.provide(NodeServices.layer))
119
-
120
- NodeRuntime.runMain(program)
@@ -1,23 +0,0 @@
1
- import { spawnSync } from 'node:child_process'
2
- import { randomUUID } from 'node:crypto'
3
-
4
- // NOTE: one nonempty id must reach every step in this build.
5
- const supplied = process.env.FOLDKIT_BUILD_ID
6
- const buildId =
7
- supplied === undefined || supplied === '' ? randomUUID() : supplied
8
-
9
- const steps = [
10
- ['vite', ['build', '--outDir', 'dist/client']],
11
- ['vite', ['build', '--ssr', 'server/main.ts', '--outDir', 'dist/server']],
12
- ]
13
-
14
- for (const [command, args] of steps) {
15
- const { status } = spawnSync(command, args, {
16
- stdio: 'inherit',
17
- shell: process.platform === 'win32',
18
- env: { ...process.env, FOLDKIT_BUILD_ID: buildId },
19
- })
20
- if (status !== 0) {
21
- process.exit(status ?? 1)
22
- }
23
- }