@solidjs/start 1.2.0 → 2.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/README.md +88 -8
  2. package/dist/client/StartClient.d.ts +0 -1
  3. package/dist/client/StartClient.jsx +2 -4
  4. package/dist/client/index.d.ts +2 -3
  5. package/dist/client/index.jsx +2 -4
  6. package/dist/client/mount.js +10 -2
  7. package/dist/client/spa/index.d.ts +1 -2
  8. package/dist/client/spa/index.jsx +1 -2
  9. package/dist/config/constants.d.ts +16 -0
  10. package/dist/config/constants.js +16 -0
  11. package/dist/config/dev-server.d.ts +2 -0
  12. package/dist/config/dev-server.js +108 -0
  13. package/dist/config/fs-router.d.ts +37 -0
  14. package/dist/config/fs-router.js +153 -0
  15. package/dist/config/fs-routes/fs-watcher.d.ts +3 -0
  16. package/dist/config/fs-routes/fs-watcher.js +49 -0
  17. package/dist/config/fs-routes/index.d.ts +7 -0
  18. package/dist/config/fs-routes/index.js +109 -0
  19. package/dist/config/fs-routes/router.d.ts +37 -0
  20. package/dist/config/fs-routes/router.js +136 -0
  21. package/dist/config/fs-routes/tree-shake.d.ts +2 -0
  22. package/dist/config/fs-routes/tree-shake.js +357 -0
  23. package/dist/config/index.d.ts +10 -0
  24. package/dist/config/index.js +217 -0
  25. package/dist/config/lazy.d.ts +3 -0
  26. package/dist/config/lazy.js +82 -0
  27. package/dist/config/manifest.d.ts +3 -0
  28. package/dist/config/manifest.js +74 -0
  29. package/dist/config/utils.d.ts +4 -0
  30. package/dist/config/utils.js +6 -0
  31. package/dist/config/vite-utils.d.ts +16 -0
  32. package/dist/config/vite-utils.js +81 -0
  33. package/dist/http/index.d.ts +84 -0
  34. package/dist/http/index.js +131 -0
  35. package/dist/index.d.ts +6 -5
  36. package/dist/index.jsx +5 -5
  37. package/dist/middleware/index.d.ts +6 -6
  38. package/dist/middleware/index.js +39 -0
  39. package/dist/{router/FileRoutes.js → router.jsx} +5 -6
  40. package/dist/server/StartServer.d.ts +3 -3
  41. package/dist/server/StartServer.jsx +9 -63
  42. package/dist/server/assets/index.d.ts +2 -0
  43. package/dist/server/assets/index.js +52 -0
  44. package/dist/server/assets/render.d.ts +19 -0
  45. package/dist/server/{renderAsset.jsx → assets/render.jsx} +1 -1
  46. package/dist/server/collect-styles.d.ts +3 -0
  47. package/dist/server/collect-styles.js +69 -0
  48. package/dist/server/fetchEvent.d.ts +4 -3
  49. package/dist/server/fetchEvent.js +12 -74
  50. package/dist/server/fetchEvent.spec.js +18 -197
  51. package/dist/server/handler.d.ts +6 -12
  52. package/dist/server/handler.js +180 -97
  53. package/dist/server/index.d.ts +8 -6
  54. package/dist/server/index.jsx +14 -6
  55. package/dist/server/manifest/client-manifest.d.ts +9 -0
  56. package/dist/server/manifest/client-manifest.js +6 -0
  57. package/dist/server/manifest/dev-client-manifest.d.ts +4 -0
  58. package/dist/server/manifest/dev-client-manifest.js +24 -0
  59. package/dist/server/manifest/dev-ssr-manifest.d.ts +5 -0
  60. package/dist/server/manifest/dev-ssr-manifest.js +23 -0
  61. package/dist/server/manifest/prod-client-manifest.d.ts +5 -0
  62. package/dist/server/manifest/prod-client-manifest.js +26 -0
  63. package/dist/server/manifest/prod-ssr-manifest.d.ts +6 -0
  64. package/dist/server/manifest/prod-ssr-manifest.js +90 -0
  65. package/dist/server/manifest/ssr-manifest.d.ts +4 -0
  66. package/dist/server/manifest/ssr-manifest.js +6 -0
  67. package/dist/{router → server}/routes.d.ts +5 -10
  68. package/dist/{router → server}/routes.js +35 -36
  69. package/dist/{runtime → server}/server-fns-runtime.d.ts +1 -1
  70. package/dist/{runtime → server}/server-fns-runtime.js +7 -6
  71. package/dist/server/server-functions-handler.d.ts +2 -0
  72. package/dist/{runtime/server-handler.js → server/server-functions-handler.js} +65 -100
  73. package/dist/server/server-runtime.d.ts +3 -0
  74. package/dist/{runtime → server}/server-runtime.js +9 -16
  75. package/dist/server/spa/StartServer.d.ts +1 -1
  76. package/dist/server/spa/StartServer.jsx +7 -9
  77. package/dist/server/spa/handler.d.ts +3 -3
  78. package/dist/server/spa/handler.js +9 -11
  79. package/dist/server/spa/index.d.ts +4 -3
  80. package/dist/server/spa/index.jsx +4 -3
  81. package/dist/server/types.d.ts +7 -9
  82. package/dist/server/util.d.ts +2 -0
  83. package/dist/server/util.js +8 -0
  84. package/dist/shared/ErrorBoundary.jsx +2 -2
  85. package/dist/shared/HttpHeader.jsx +4 -2
  86. package/dist/shared/{HttpStatusCode.jsx → HttpStatusCode.js} +3 -1
  87. package/dist/shared/dev-overlay/DevOverlayDialog.d.ts +2 -2
  88. package/dist/shared/dev-overlay/DevOverlayDialog.jsx +75 -66
  89. package/dist/shared/dev-overlay/createStackFrame.js +3 -3
  90. package/dist/shared/dev-overlay/index.jsx +5 -8
  91. package/dist/shared/lazy.d.ts +3 -0
  92. package/dist/shared/lazy.js +40 -0
  93. package/dist/shared/serverFunction.d.ts +1 -1
  94. package/package.json +45 -67
  95. package/scripts/build.js +15 -0
  96. package/scripts/validate-imports.js +316 -0
  97. package/src/client/StartClient.tsx +34 -0
  98. package/src/client/index.tsx +2 -0
  99. package/src/client/mount.ts +114 -0
  100. package/src/client/spa/index.tsx +9 -0
  101. package/src/config/constants.ts +19 -0
  102. package/src/config/dev-server.ts +149 -0
  103. package/src/config/fs-router.ts +177 -0
  104. package/src/config/fs-routes/fs-watcher.ts +81 -0
  105. package/src/config/fs-routes/index.ts +138 -0
  106. package/src/config/fs-routes/router.ts +169 -0
  107. package/src/config/fs-routes/tree-shake.ts +392 -0
  108. package/src/config/index.ts +236 -0
  109. package/src/config/lazy.ts +100 -0
  110. package/src/config/manifest.ts +84 -0
  111. package/src/config/utils.ts +5 -0
  112. package/src/config/vite-utils.ts +113 -0
  113. package/src/env.d.ts +35 -0
  114. package/src/http/index.ts +253 -0
  115. package/src/index.tsx +19 -0
  116. package/src/internal.d.ts +14 -0
  117. package/src/middleware/index.ts +82 -0
  118. package/src/router.tsx +36 -0
  119. package/src/server/StartServer.tsx +62 -0
  120. package/src/server/assets/index.ts +70 -0
  121. package/src/server/assets/render.tsx +42 -0
  122. package/src/server/collect-styles.ts +94 -0
  123. package/src/server/fetchEvent.spec.ts +89 -0
  124. package/src/server/fetchEvent.ts +36 -0
  125. package/src/server/handler.ts +250 -0
  126. package/src/server/index.tsx +16 -0
  127. package/src/server/manifest/client-manifest.ts +8 -0
  128. package/src/server/manifest/dev-client-manifest.ts +19 -0
  129. package/src/server/manifest/dev-ssr-manifest.ts +21 -0
  130. package/src/server/manifest/prod-client-manifest.ts +18 -0
  131. package/src/server/manifest/prod-ssr-manifest.ts +125 -0
  132. package/src/server/manifest/ssr-manifest.ts +8 -0
  133. package/src/server/routes.ts +138 -0
  134. package/src/server/server-fns-runtime.ts +31 -0
  135. package/src/server/server-functions-handler.ts +364 -0
  136. package/src/server/server-runtime.ts +240 -0
  137. package/src/server/spa/StartServer.tsx +40 -0
  138. package/src/server/spa/handler.ts +29 -0
  139. package/src/server/spa/index.tsx +5 -0
  140. package/src/server/types.ts +82 -0
  141. package/src/server/util.ts +12 -0
  142. package/src/shared/ErrorBoundary.tsx +52 -0
  143. package/src/shared/GET.ts +8 -0
  144. package/src/shared/HttpHeader.tsx +42 -0
  145. package/src/shared/HttpStatusCode.ts +27 -0
  146. package/src/shared/clientOnly.ts +44 -0
  147. package/src/shared/dev-overlay/CodeView.tsx +95 -0
  148. package/src/shared/dev-overlay/DevOverlayDialog.tsx +316 -0
  149. package/src/shared/dev-overlay/createStackFrame.ts +77 -0
  150. package/src/shared/dev-overlay/download.ts +6 -0
  151. package/src/shared/dev-overlay/env.d.ts +4 -0
  152. package/src/shared/dev-overlay/get-source-map.ts +35 -0
  153. package/src/shared/dev-overlay/icons.tsx +288 -0
  154. package/src/shared/dev-overlay/index.tsx +63 -0
  155. package/src/shared/dev-overlay/styles.css +292 -0
  156. package/src/shared/lazy.ts +51 -0
  157. package/src/shared/serverFunction.ts +10 -0
  158. package/src/virtual.d.ts +27 -0
  159. package/tsconfig.json +23 -0
  160. package/vitest.config.ts +9 -0
  161. package/config/fs-router.js +0 -142
  162. package/config/index.d.ts +0 -40
  163. package/config/index.js +0 -304
  164. package/config/server-components.js +0 -106
  165. package/dist/client/islands.d.ts +0 -7
  166. package/dist/client/islands.jsx +0 -10
  167. package/dist/middleware/index.jsx +0 -35
  168. package/dist/router/index.d.ts +0 -1
  169. package/dist/router/index.jsx +0 -2
  170. package/dist/router/lazyRoute.d.ts +0 -6
  171. package/dist/router/lazyRoute.js +0 -53
  172. package/dist/runtime/server-handler.d.ts +0 -2
  173. package/dist/runtime/server-runtime.d.ts +0 -3
  174. package/dist/server/islands/index.d.ts +0 -19
  175. package/dist/server/islands/index.jsx +0 -62
  176. package/dist/server/islands/utils.d.ts +0 -1
  177. package/dist/server/islands/utils.jsx +0 -85
  178. package/dist/server/pageEvent.d.ts +0 -2
  179. package/dist/server/pageEvent.js +0 -56
  180. package/dist/server/renderAsset.d.ts +0 -2
  181. package/env.d.ts +0 -26
  182. /package/dist/{router/FileRoutes.d.ts → router.d.ts} +0 -0
  183. /package/dist/shared/{clientOnly.jsx → clientOnly.js} +0 -0
  184. /package/dist/shared/{serverFunction.jsx → serverFunction.js} +0 -0
@@ -0,0 +1,392 @@
1
+ // All credit for this work goes to the amazing Next.js team.
2
+ // https://github.com/vercel/next.js/blob/canary/packages/next/build/babel/plugins/next-ssg-transform.ts
3
+ // This is adapted to work with routeData functions. It can be run in two modes, one which preserves the routeData and the Component in the same file, and one which creates a
4
+
5
+ import type * as Babel from "@babel/core";
6
+ import type { NodePath, PluginObj, PluginPass } from "@babel/core";
7
+ import type { Binding } from "@babel/traverse";
8
+ import { basename } from "pathe";
9
+ import type { Plugin, ResolvedConfig, ViteDevServer } from "vite";
10
+
11
+ type State = Omit<PluginPass, "opts"> & {
12
+ opts: { pick: string[] };
13
+ refs: Set<any>;
14
+ done: boolean;
15
+ };
16
+
17
+ function treeShakeTransform({ types: t }: typeof Babel): PluginObj<State> {
18
+ function getIdentifier(path: any) {
19
+ const parentPath = path.parentPath;
20
+ if (parentPath.type === "VariableDeclarator") {
21
+ const pp = parentPath;
22
+ const name = pp.get("id");
23
+ return name.node.type === "Identifier" ? name : null;
24
+ }
25
+ if (parentPath.type === "AssignmentExpression") {
26
+ const pp = parentPath;
27
+ const name = pp.get("left");
28
+ return name.node.type === "Identifier" ? name : null;
29
+ }
30
+ if (path.node.type === "ArrowFunctionExpression") {
31
+ return null;
32
+ }
33
+ return path.node.id && path.node.id.type === "Identifier"
34
+ ? path.get("id")
35
+ : null;
36
+ }
37
+
38
+ function isIdentifierReferenced(ident: any) {
39
+ const b: Binding | undefined = ident.scope.getBinding(ident.node.name);
40
+ if (b?.referenced) {
41
+ if (b.path.type === "FunctionDeclaration") {
42
+ return !b.constantViolations
43
+ .concat(b.referencePaths)
44
+ .every((ref) => ref.findParent((p) => p === b.path));
45
+ }
46
+ return true;
47
+ }
48
+ return false;
49
+ }
50
+ function markFunction(path: any, state: any) {
51
+ const ident = getIdentifier(path);
52
+ if (ident && ident.node && isIdentifierReferenced(ident)) {
53
+ state.refs.add(ident);
54
+ }
55
+ }
56
+ function markImport(path: any, state: any) {
57
+ const local = path.get("local");
58
+ if (isIdentifierReferenced(local)) {
59
+ state.refs.add(local);
60
+ }
61
+ }
62
+
63
+ return {
64
+ visitor: {
65
+ Program: {
66
+ enter(path, state) {
67
+ state.refs = new Set();
68
+ state.done = false;
69
+ path.traverse(
70
+ {
71
+ VariableDeclarator(variablePath, variableState: any) {
72
+ if (variablePath.node.id.type === "Identifier") {
73
+ const local = variablePath.get("id");
74
+ if (isIdentifierReferenced(local)) {
75
+ variableState.refs.add(local);
76
+ }
77
+ } else if (variablePath.node.id.type === "ObjectPattern") {
78
+ const pattern = variablePath.get("id");
79
+ const properties = pattern.get(
80
+ "properties",
81
+ ) as Array<NodePath>;
82
+ properties.forEach((p) => {
83
+ const local = p.get(
84
+ p.node.type === "ObjectProperty"
85
+ ? "value"
86
+ : p.node.type === "RestElement"
87
+ ? "argument"
88
+ : (() => {
89
+ throw new Error("invariant");
90
+ })(),
91
+ );
92
+ if (isIdentifierReferenced(local)) {
93
+ variableState.refs.add(local);
94
+ }
95
+ });
96
+ } else if (variablePath.node.id.type === "ArrayPattern") {
97
+ const pattern = variablePath.get("id");
98
+ const elements = pattern.get("elements") as Array<NodePath>;
99
+ elements.forEach((e) => {
100
+ let local: NodePath<any>;
101
+ if (e.node && e.node.type === "Identifier") {
102
+ local = e;
103
+ } else if (e.node && e.node.type === "RestElement") {
104
+ local = e.get("argument");
105
+ } else {
106
+ return;
107
+ }
108
+ if (isIdentifierReferenced(local)) {
109
+ variableState.refs.add(local);
110
+ }
111
+ });
112
+ }
113
+ },
114
+ ExportDefaultDeclaration(exportNamedPath) {
115
+ // if opts.keep is true, we don't remove the routeData export
116
+ if (state.opts.pick && !state.opts.pick.includes("default")) {
117
+ exportNamedPath.remove();
118
+ }
119
+ },
120
+ ExportNamedDeclaration(exportNamedPath) {
121
+ // if opts.keep is false, we don't remove the routeData export
122
+ if (!state.opts.pick) {
123
+ return;
124
+ }
125
+ const specifiers = exportNamedPath.get("specifiers");
126
+ if (specifiers.length) {
127
+ specifiers.forEach((s) => {
128
+ if (
129
+ t.isIdentifier(s.node.exported)
130
+ ? s.node.exported.name
131
+ : state.opts.pick.includes(s.node.exported.value)
132
+ ) {
133
+ s.remove();
134
+ }
135
+ });
136
+ if (exportNamedPath.node.specifiers.length < 1) {
137
+ exportNamedPath.remove();
138
+ }
139
+ return;
140
+ }
141
+ const decl = exportNamedPath.get("declaration");
142
+ if (decl == null || decl.node == null) {
143
+ return;
144
+ }
145
+ switch (decl.node.type) {
146
+ case "FunctionDeclaration": {
147
+ const name = decl.node.id?.name;
148
+ if (
149
+ name &&
150
+ state.opts.pick &&
151
+ !state.opts.pick.includes(name)
152
+ ) {
153
+ exportNamedPath.remove();
154
+ }
155
+ break;
156
+ }
157
+ case "VariableDeclaration": {
158
+ const inner = decl.get("declarations") as Array<
159
+ NodePath<any>
160
+ >;
161
+ inner.forEach((d) => {
162
+ if (d.node.id.type !== "Identifier") {
163
+ return;
164
+ }
165
+ const name = d.node.id.name;
166
+ if (state.opts.pick && !state.opts.pick.includes(name)) {
167
+ d.remove();
168
+ }
169
+ });
170
+ break;
171
+ }
172
+ default: {
173
+ break;
174
+ }
175
+ }
176
+ },
177
+ FunctionDeclaration: markFunction,
178
+ FunctionExpression: markFunction,
179
+ ArrowFunctionExpression: markFunction,
180
+ ImportSpecifier: markImport,
181
+ ImportDefaultSpecifier: markImport,
182
+ ImportNamespaceSpecifier: markImport,
183
+ ImportDeclaration: (path, state) => {
184
+ if (
185
+ path.node.source.value.endsWith(".css") &&
186
+ state.opts.pick &&
187
+ !state.opts.pick.includes("$css")
188
+ ) {
189
+ path.remove();
190
+ }
191
+ },
192
+ },
193
+ state,
194
+ );
195
+
196
+ const refs = state.refs;
197
+ let count = 0;
198
+ const sweepFunction = (sweepPath: any) => {
199
+ const ident = getIdentifier(sweepPath);
200
+ if (
201
+ ident &&
202
+ ident.node &&
203
+ refs.has(ident) &&
204
+ !isIdentifierReferenced(ident)
205
+ ) {
206
+ ++count;
207
+ if (
208
+ t.isAssignmentExpression(sweepPath.parentPath) ||
209
+ t.isVariableDeclarator(sweepPath.parentPath)
210
+ ) {
211
+ sweepPath.parentPath.remove();
212
+ } else {
213
+ sweepPath.remove();
214
+ }
215
+ }
216
+ };
217
+ function sweepImport(sweepPath: any) {
218
+ const local = sweepPath.get("local");
219
+ if (refs.has(local) && !isIdentifierReferenced(local)) {
220
+ ++count;
221
+ sweepPath.remove();
222
+ if (sweepPath.parent.specifiers.length === 0) {
223
+ sweepPath.parentPath.remove();
224
+ }
225
+ }
226
+ }
227
+ do {
228
+ path.scope.crawl();
229
+ count = 0;
230
+ path.traverse({
231
+ VariableDeclarator(variablePath) {
232
+ if (variablePath.node.id.type === "Identifier") {
233
+ const local = variablePath.get("id");
234
+ if (refs.has(local) && !isIdentifierReferenced(local)) {
235
+ ++count;
236
+ variablePath.remove();
237
+ }
238
+ } else if (variablePath.node.id.type === "ObjectPattern") {
239
+ const pattern = variablePath.get("id");
240
+ const beforeCount = count;
241
+ const properties = pattern.get("properties");
242
+ properties.forEach((p) => {
243
+ const local = p.get(
244
+ p.node.type === "ObjectProperty"
245
+ ? "value"
246
+ : p.node.type === "RestElement"
247
+ ? "argument"
248
+ : (() => {
249
+ throw new Error("invariant");
250
+ })(),
251
+ );
252
+ if (refs.has(local) && !isIdentifierReferenced(local)) {
253
+ ++count;
254
+ p.remove();
255
+ }
256
+ });
257
+ if (
258
+ beforeCount !== count &&
259
+ pattern.get("properties").length < 1
260
+ ) {
261
+ variablePath.remove();
262
+ }
263
+ } else if (variablePath.node.id.type === "ArrayPattern") {
264
+ const pattern = variablePath.get("id");
265
+ const beforeCount = count;
266
+ const elements = pattern.get("elements");
267
+ elements.forEach((e) => {
268
+ let local: NodePath<any> | undefined;
269
+ if (e.node && e.node.type === "Identifier") {
270
+ local = e;
271
+ } else if (e.node && e.node.type === "RestElement") {
272
+ local = e.get("argument");
273
+ } else {
274
+ return;
275
+ }
276
+ if (refs.has(local) && !isIdentifierReferenced(local)) {
277
+ ++count;
278
+ e.remove();
279
+ }
280
+ });
281
+ if (
282
+ beforeCount !== count &&
283
+ pattern.get("elements").length < 1
284
+ ) {
285
+ variablePath.remove();
286
+ }
287
+ }
288
+ },
289
+ FunctionDeclaration: sweepFunction,
290
+ FunctionExpression: sweepFunction,
291
+ ArrowFunctionExpression: sweepFunction,
292
+ ImportSpecifier: sweepImport,
293
+ ImportDefaultSpecifier: sweepImport,
294
+ ImportNamespaceSpecifier: sweepImport,
295
+ });
296
+ } while (count);
297
+ },
298
+ },
299
+ },
300
+ };
301
+ }
302
+
303
+ export function treeShake(): Plugin {
304
+ let config: ResolvedConfig;
305
+ const cache: Record<string, any> = {};
306
+ let server: ViteDevServer;
307
+
308
+ async function transform(id: string, code: string) {
309
+ const [path, queryString] = id.split("?");
310
+ const query = new URLSearchParams(queryString);
311
+ if (query.has("pick")) {
312
+ const babel = await import("@babel/core");
313
+ const transformed = await babel.transformAsync(code, {
314
+ plugins: [[treeShakeTransform, { pick: query.getAll("pick") }]],
315
+ parserOpts: {
316
+ plugins: ["jsx", "typescript"],
317
+ },
318
+ filename: basename(id),
319
+ ast: false,
320
+ sourceMaps: true,
321
+ configFile: false,
322
+ babelrc: false,
323
+ sourceFileName: id,
324
+ });
325
+
326
+ return transformed;
327
+ }
328
+ }
329
+ return {
330
+ name: "tree-shake",
331
+ enforce: "pre",
332
+ configResolved(resolvedConfig) {
333
+ config = resolvedConfig;
334
+ },
335
+ configureServer(s) {
336
+ server = s;
337
+ },
338
+ async handleHotUpdate(ctx) {
339
+ if (cache[ctx.file]) {
340
+ const mods = [];
341
+ const newCode = await ctx.read();
342
+ for (const [id, code] of Object.entries(cache[ctx.file])) {
343
+ const transformed = await transform(id, newCode);
344
+ if (!transformed) continue;
345
+
346
+ const { code: transformedCode } = transformed;
347
+
348
+ if (transformedCode !== code) {
349
+ const mod = server.moduleGraph.getModuleById(id);
350
+ if (mod) mods.push(mod);
351
+ }
352
+
353
+ cache[ctx.file] ??= {};
354
+ cache[ctx.file][id] = transformedCode;
355
+ // server.moduleGraph.setModuleSource(id, code);
356
+ }
357
+
358
+ return mods;
359
+ }
360
+ // const mods = [];
361
+ // [...server.moduleGraph.urlToModuleMap.entries()].forEach(([url, m]) => {
362
+ // if (m.file === ctx.file && m.id.includes("pick=")) {
363
+ // if (!m.id.includes("pick=loader")) {
364
+ // mods.push(m);
365
+ // }
366
+ // }
367
+ // });
368
+ // return mods;
369
+ // // this.router.updateRoute(ctx.path);
370
+ // }
371
+ },
372
+ async transform(code, id) {
373
+ const [path, queryString] = id.split("?");
374
+ if (!path) return;
375
+ const query = new URLSearchParams(queryString);
376
+ const ext = path.split(".").pop();
377
+ if (!ext) return;
378
+ if (query.has("pick") && ["js", "jsx", "ts", "tsx"].includes(ext)) {
379
+ const transformed = await transform(id, code);
380
+ if (!transformed?.code) return;
381
+
382
+ cache[path] ??= {};
383
+ cache[path][id] = transformed.code;
384
+
385
+ return {
386
+ code: transformed.code,
387
+ map: transformed.map,
388
+ };
389
+ }
390
+ },
391
+ };
392
+ }
@@ -0,0 +1,236 @@
1
+ import { TanStackServerFnPlugin } from "@tanstack/server-functions-plugin";
2
+ import { defu } from "defu";
3
+ import { globSync } from "node:fs";
4
+ import { extname, isAbsolute, join, normalize } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import { type PluginOption } from "vite";
7
+ import solid, { type Options as SolidOptions } from "vite-plugin-solid";
8
+
9
+ import { DEFAULT_EXTENSIONS, VIRTUAL_MODULES, VITE_ENVIRONMENTS } from "./constants.ts";
10
+ import { devServer } from "./dev-server.ts";
11
+ import { SolidStartClientFileRouter, SolidStartServerFileRouter } from "./fs-router.ts";
12
+ import { fsRoutes } from "./fs-routes/index.ts";
13
+ import type { BaseFileSystemRouter } from "./fs-routes/router.ts";
14
+ import lazy from "./lazy.ts";
15
+ import { manifest } from "./manifest.ts";
16
+ import { parseIdQuery } from "./utils.ts";
17
+
18
+ export interface SolidStartOptions {
19
+ solid?: Partial<SolidOptions>;
20
+ ssr?: boolean;
21
+ routeDir?: string;
22
+ extensions?: string[];
23
+ middleware?: string;
24
+ }
25
+
26
+ const absolute = (path: string, root: string) =>
27
+ path ? (isAbsolute(path) ? path : join(root, path)) : path;
28
+
29
+ export function solidStart(options?: SolidStartOptions): Array<PluginOption> {
30
+ const start = defu(options ?? {}, {
31
+ appRoot: "./src",
32
+ routeDir: "./routes",
33
+ ssr: true,
34
+ devOverlay: true,
35
+ experimental: {
36
+ islands: false
37
+ },
38
+ solid: {},
39
+ extensions: []
40
+ });
41
+ const extensions = [...DEFAULT_EXTENSIONS, ...(start.extensions || [])];
42
+ const routeDir = join(start.appRoot, start.routeDir);
43
+ const root = process.cwd();
44
+ const appEntryPath = globSync(join(root, start.appRoot, "app.{j,t}sx"))[0];
45
+ if (!appEntryPath) {
46
+ throw new Error(`Could not find an app jsx/tsx entry in ${start.appRoot}.`);
47
+ }
48
+ const entryExtension = extname(appEntryPath);
49
+ const handlers = {
50
+ client: `${start.appRoot}/entry-client${entryExtension}`,
51
+ server: `${start.appRoot}/entry-server${entryExtension}`
52
+ };
53
+ return [
54
+ {
55
+ name: "solid-start:config",
56
+ enforce: "pre",
57
+ configEnvironment(name) {
58
+ return {
59
+ define: {
60
+ "import.meta.env.SSR": JSON.stringify(name === VITE_ENVIRONMENTS.server)
61
+ },
62
+ resolve: {
63
+ // remove when https://github.com/solidjs/vite-plugin-solid/pull/228 is released
64
+ externalConditions: ["solid", "node"]
65
+ }
66
+ };
67
+ },
68
+ async config(_, env) {
69
+ const clientInput = [handlers.client];
70
+ if (env.command === "build") {
71
+ const clientRouter: BaseFileSystemRouter = (globalThis as any).ROUTERS.client;
72
+ for (const route of await clientRouter.getRoutes()) {
73
+ for (const [key, value] of Object.entries(route)) {
74
+ if (value && key.startsWith("$") && !key.startsWith("$$")) {
75
+ function toRouteId(route: any) {
76
+ return `${route.src}?${route.pick.map((p: string) => `pick=${p}`).join("&")}`;
77
+ }
78
+ clientInput.push(toRouteId(value));
79
+ }
80
+ }
81
+ }
82
+ }
83
+ return {
84
+ appType: "custom",
85
+ build: { assetsDir: "_build/assets" },
86
+ environments: {
87
+ [VITE_ENVIRONMENTS.client]: {
88
+ consumer: "client",
89
+ build: {
90
+ write: true,
91
+ manifest: true,
92
+ outDir: "dist/client",
93
+ rollupOptions: {
94
+ input: clientInput,
95
+ treeshake: true,
96
+ preserveEntrySignatures: "exports-only"
97
+ }
98
+ }
99
+ },
100
+ [VITE_ENVIRONMENTS.server]: {
101
+ consumer: "server",
102
+ build: {
103
+ ssr: true,
104
+ write: true,
105
+ manifest: true,
106
+ copyPublicDir: false,
107
+ rollupOptions: {
108
+ input: "~/entry-server.tsx"
109
+ },
110
+ outDir: "dist/server",
111
+ commonjsOptions: {
112
+ include: [/node_modules/]
113
+ }
114
+ }
115
+ }
116
+ },
117
+ resolve: {
118
+ alias: {
119
+ "@solidjs/start/server/entry": handlers.server,
120
+ "~": join(process.cwd(), start.appRoot),
121
+ ...(!start.ssr
122
+ ? {
123
+ "@solidjs/start/server": "@solidjs/start/server/spa",
124
+ "@solidjs/start/client": "@solidjs/start/client/spa"
125
+ }
126
+ : {})
127
+ }
128
+ },
129
+ define: {
130
+ "import.meta.env.MANIFEST": `globalThis.MANIFEST`,
131
+ "import.meta.env.START_SSR": JSON.stringify(start.ssr),
132
+ "import.meta.env.START_APP_ENTRY": `"${appEntryPath}"`,
133
+ "import.meta.env.START_CLIENT_ENTRY": `"${handlers.client}"`,
134
+ "import.meta.env.START_DEV_OVERLAY": JSON.stringify(start.devOverlay)
135
+ },
136
+ builder: {
137
+ sharedPlugins: true,
138
+ async buildApp(builder) {
139
+ const client = builder.environments[VITE_ENVIRONMENTS.client];
140
+ const server = builder.environments[VITE_ENVIRONMENTS.server];
141
+
142
+ if (!client) throw new Error("Client environment not found");
143
+ if (!server) throw new Error("SSR environment not found");
144
+
145
+ if (!client.isBuilt) await builder.build(client);
146
+ if (!server.isBuilt) await builder.build(server);
147
+ }
148
+ }
149
+ };
150
+ }
151
+ },
152
+ manifest(start),
153
+ fsRoutes({
154
+ routers: {
155
+ client: new SolidStartClientFileRouter({
156
+ dir: absolute(routeDir, root),
157
+ extensions
158
+ }),
159
+ ssr: new SolidStartServerFileRouter({
160
+ dir: absolute(routeDir, root),
161
+ extensions,
162
+ dataOnly: !start.ssr
163
+ })
164
+ }
165
+ }),
166
+ lazy(),
167
+ // Must be placed after fsRoutes, as treeShake will remove the
168
+ // server fn exports added in by this plugin
169
+ TanStackServerFnPlugin({
170
+ // This is the ID that will be available to look up and import
171
+ // our server function manifest and resolve its module
172
+ manifestVirtualImportId: VIRTUAL_MODULES.serverFnManifest,
173
+ directive: "use server",
174
+ callers: [
175
+ {
176
+ envConsumer: "client",
177
+ envName: VITE_ENVIRONMENTS.client,
178
+ getRuntimeCode: () =>
179
+ `import { createServerReference } from "${normalize(
180
+ fileURLToPath(new URL("../server/server-runtime", import.meta.url))
181
+ )}"`,
182
+ replacer: opts => `createServerReference('${opts.functionId}')`
183
+ },
184
+ {
185
+ envConsumer: "server",
186
+ envName: VITE_ENVIRONMENTS.server,
187
+ getRuntimeCode: () =>
188
+ `import { createServerReference } from '${normalize(
189
+ fileURLToPath(new URL("../server/server-fns-runtime", import.meta.url))
190
+ )}'`,
191
+ replacer: opts => `createServerReference(${opts.fn}, '${opts.functionId}')`
192
+ }
193
+ ],
194
+ provider: {
195
+ envName: VITE_ENVIRONMENTS.server,
196
+ getRuntimeCode: () =>
197
+ `import { createServerReference } from '${normalize(
198
+ fileURLToPath(new URL("../server/server-fns-runtime", import.meta.url))
199
+ )}'`,
200
+ replacer: opts => `createServerReference(${opts.fn}, '${opts.functionId}')`
201
+ }
202
+ }),
203
+ {
204
+ name: "solid-start:virtual-modules",
205
+ async resolveId(id) {
206
+ const { filename, query } = parseIdQuery(id);
207
+
208
+ let base;
209
+ if (filename === VIRTUAL_MODULES.clientEntry) base = handlers.client;
210
+ if (filename === VIRTUAL_MODULES.serverEntry) base = handlers.server;
211
+ if (filename === VIRTUAL_MODULES.app) base = appEntryPath;
212
+
213
+ if (base) {
214
+ let id = (await this.resolve(base))?.id;
215
+ if (!id) return;
216
+
217
+ if (query.size > 0) id += `?${query.toString()}`;
218
+ return id;
219
+ }
220
+ }
221
+ },
222
+ {
223
+ name: "solid-start:capture-client-bundle",
224
+ enforce: "post",
225
+ generateBundle(_options, bundle) {
226
+ globalThis.START_CLIENT_BUNDLE = bundle;
227
+ }
228
+ },
229
+ devServer(),
230
+ solid({
231
+ ...start.solid,
232
+ ssr: true,
233
+ extensions: extensions.map(ext => `.${ext}`)
234
+ })
235
+ ];
236
+ }