@solidjs/start 2.0.0-beta.1 → 2.0.0-beta.2

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 (231) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/client/StartClient.d.ts +7 -0
  3. package/{src/client/StartClient.tsx → dist/client/StartClient.jsx} +7 -15
  4. package/dist/client/index.jsx +2 -0
  5. package/dist/client/mount.d.ts +1 -0
  6. package/dist/client/mount.js +1 -0
  7. package/dist/client/spa/index.d.ts +4 -0
  8. package/dist/client/spa/index.jsx +5 -0
  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 +128 -0
  13. package/dist/config/env.d.ts +19 -0
  14. package/dist/config/env.js +81 -0
  15. package/dist/config/fs-router.d.ts +37 -0
  16. package/dist/config/fs-router.js +141 -0
  17. package/dist/config/fs-routes/fs-watcher.d.ts +3 -0
  18. package/dist/config/fs-routes/fs-watcher.js +53 -0
  19. package/dist/config/fs-routes/index.d.ts +7 -0
  20. package/dist/config/fs-routes/index.js +107 -0
  21. package/dist/config/fs-routes/router.d.ts +42 -0
  22. package/dist/config/fs-routes/router.js +148 -0
  23. package/dist/config/fs-routes/tree-shake.d.ts +2 -0
  24. package/dist/config/fs-routes/tree-shake.js +305 -0
  25. package/dist/config/index.d.ts +25 -0
  26. package/dist/config/index.js +217 -0
  27. package/dist/config/lazy.d.ts +3 -0
  28. package/dist/config/lazy.js +114 -0
  29. package/dist/config/manifest.d.ts +3 -0
  30. package/dist/config/manifest.js +109 -0
  31. package/dist/config/utils.d.ts +4 -0
  32. package/dist/config/utils.js +6 -0
  33. package/dist/config/vite-utils.d.ts +21 -0
  34. package/dist/config/vite-utils.js +85 -0
  35. package/dist/directives/bubble-function-declaration.d.ts +3 -0
  36. package/dist/directives/bubble-function-declaration.js +23 -0
  37. package/dist/directives/compile.d.ts +9 -0
  38. package/dist/directives/compile.js +40 -0
  39. package/dist/directives/generate-unique-name.d.ts +3 -0
  40. package/dist/directives/generate-unique-name.js +16 -0
  41. package/dist/directives/get-descriptive-name.d.ts +2 -0
  42. package/dist/directives/get-descriptive-name.js +37 -0
  43. package/dist/directives/get-import-identifier.d.ts +4 -0
  44. package/dist/directives/get-import-identifier.js +19 -0
  45. package/dist/directives/get-root-statement-path.d.ts +2 -0
  46. package/dist/directives/get-root-statement-path.js +12 -0
  47. package/dist/directives/index.d.ts +14 -0
  48. package/dist/directives/index.js +170 -0
  49. package/dist/directives/is-statement-top-level.d.ts +3 -0
  50. package/dist/directives/is-statement-top-level.js +9 -0
  51. package/dist/directives/paths.d.ts +9 -0
  52. package/dist/directives/paths.js +36 -0
  53. package/dist/directives/plugin.d.ts +21 -0
  54. package/dist/directives/plugin.js +302 -0
  55. package/dist/directives/remove-unused-variables.d.ts +3 -0
  56. package/dist/directives/remove-unused-variables.js +66 -0
  57. package/{src/directives/types.ts → dist/directives/types.d.ts} +5 -7
  58. package/dist/directives/types.js +1 -0
  59. package/dist/directives/xxhash32.d.ts +6 -0
  60. package/dist/directives/xxhash32.js +193 -0
  61. package/dist/fns/client.d.ts +3 -0
  62. package/dist/fns/client.js +106 -0
  63. package/dist/fns/handler.d.ts +5 -0
  64. package/dist/fns/handler.js +269 -0
  65. package/dist/fns/registration.d.ts +2 -0
  66. package/dist/fns/registration.js +12 -0
  67. package/dist/fns/serialization.d.ts +6 -0
  68. package/dist/fns/serialization.js +208 -0
  69. package/dist/fns/server.d.ts +7 -0
  70. package/dist/fns/server.js +38 -0
  71. package/dist/fns/shared.d.ts +17 -0
  72. package/dist/fns/shared.js +105 -0
  73. package/dist/http/index.d.ts +81 -0
  74. package/dist/http/index.js +129 -0
  75. package/{src/index.tsx → dist/index.d.ts} +0 -2
  76. package/dist/index.jsx +6 -0
  77. package/dist/middleware/index.d.ts +38 -0
  78. package/dist/middleware/index.js +59 -0
  79. package/dist/router.d.ts +6 -0
  80. package/dist/router.jsx +29 -0
  81. package/dist/server/StartServer.d.ts +9 -0
  82. package/dist/server/StartServer.jsx +34 -0
  83. package/dist/server/assets/PatchVirtualDevStyles.d.ts +4 -0
  84. package/{src/server/assets/PatchVirtualDevStyles.tsx → dist/server/assets/PatchVirtualDevStyles.jsx} +7 -10
  85. package/dist/server/assets/index.d.ts +2 -0
  86. package/dist/server/assets/index.js +52 -0
  87. package/dist/server/assets/render.d.ts +19 -0
  88. package/dist/server/assets/render.jsx +14 -0
  89. package/dist/server/collect-styles.d.ts +3 -0
  90. package/dist/server/collect-styles.js +57 -0
  91. package/dist/server/fetchEvent.d.ts +7 -0
  92. package/dist/server/fetchEvent.js +26 -0
  93. package/dist/server/handler.d.ts +64 -0
  94. package/dist/server/handler.js +222 -0
  95. package/dist/server/index.d.ts +9 -0
  96. package/{src/server/index.tsx → dist/server/index.jsx} +4 -19
  97. package/dist/server/manifest/client-manifest.d.ts +9 -0
  98. package/dist/server/manifest/client-manifest.js +6 -0
  99. package/dist/server/manifest/dev-client-manifest.d.ts +4 -0
  100. package/dist/server/manifest/dev-client-manifest.js +24 -0
  101. package/dist/server/manifest/dev-ssr-manifest.d.ts +5 -0
  102. package/dist/server/manifest/dev-ssr-manifest.js +23 -0
  103. package/dist/server/manifest/prod-client-manifest.d.ts +5 -0
  104. package/dist/server/manifest/prod-client-manifest.js +26 -0
  105. package/dist/server/manifest/prod-ssr-manifest.d.ts +6 -0
  106. package/dist/server/manifest/prod-ssr-manifest.js +85 -0
  107. package/dist/server/manifest/ssr-manifest.d.ts +4 -0
  108. package/dist/server/manifest/ssr-manifest.js +6 -0
  109. package/dist/server/routes.d.ts +23 -0
  110. package/dist/server/routes.js +74 -0
  111. package/dist/server/spa/StartServer.d.ts +9 -0
  112. package/dist/server/spa/StartServer.jsx +25 -0
  113. package/dist/server/spa/handler.d.ts +36 -0
  114. package/dist/server/spa/handler.js +20 -0
  115. package/{src/server/spa/index.tsx → dist/server/spa/index.d.ts} +0 -1
  116. package/dist/server/spa/index.jsx +5 -0
  117. package/dist/server/types.d.ts +75 -0
  118. package/dist/server/types.js +1 -0
  119. package/dist/server/util.d.ts +2 -0
  120. package/dist/server/util.js +8 -0
  121. package/dist/shared/ErrorBoundary.d.ts +3 -0
  122. package/dist/shared/ErrorBoundary.jsx +36 -0
  123. package/dist/shared/GET.d.ts +5 -0
  124. package/dist/shared/GET.js +8 -0
  125. package/dist/shared/HttpHeader.d.ts +10 -0
  126. package/dist/shared/HttpHeader.jsx +38 -0
  127. package/dist/shared/HttpStatusCode.d.ts +9 -0
  128. package/dist/shared/HttpStatusCode.js +18 -0
  129. package/dist/shared/clientOnly.d.ts +12 -0
  130. package/dist/shared/clientOnly.js +27 -0
  131. package/dist/shared/dev-overlay/CodeView.d.ts +7 -0
  132. package/dist/shared/dev-overlay/CodeView.jsx +62 -0
  133. package/dist/shared/dev-overlay/DevOverlayDialog.d.ts +9 -0
  134. package/dist/shared/dev-overlay/DevOverlayDialog.jsx +226 -0
  135. package/dist/shared/dev-overlay/createStackFrame.d.ts +9 -0
  136. package/dist/shared/dev-overlay/createStackFrame.js +56 -0
  137. package/dist/shared/dev-overlay/download.d.ts +1 -0
  138. package/dist/shared/dev-overlay/download.js +6 -0
  139. package/dist/shared/dev-overlay/get-source-map.d.ts +2 -0
  140. package/dist/shared/dev-overlay/get-source-map.js +25 -0
  141. package/dist/shared/dev-overlay/icons.d.ts +28 -0
  142. package/dist/shared/dev-overlay/icons.jsx +150 -0
  143. package/dist/shared/dev-overlay/index.d.ts +5 -0
  144. package/dist/shared/dev-overlay/index.jsx +39 -0
  145. package/dist/shared/lazy.d.ts +3 -0
  146. package/dist/shared/lazy.js +42 -0
  147. package/dist/shared/serverFunction.d.ts +6 -0
  148. package/dist/shared/serverFunction.js +8 -0
  149. package/dist/utils/debounce.d.ts +4 -0
  150. package/dist/utils/debounce.js +10 -0
  151. package/package.json +7 -2
  152. package/scripts/build.js +0 -15
  153. package/scripts/validate-imports.js +0 -316
  154. package/src/client/mount.ts +0 -1
  155. package/src/client/spa/index.tsx +0 -9
  156. package/src/config/constants.ts +0 -19
  157. package/src/config/dev-server.ts +0 -161
  158. package/src/config/env.ts +0 -105
  159. package/src/config/fs-router.ts +0 -162
  160. package/src/config/fs-routes/fs-watcher.ts +0 -66
  161. package/src/config/fs-routes/index.ts +0 -136
  162. package/src/config/fs-routes/router.ts +0 -184
  163. package/src/config/fs-routes/tree-shake.ts +0 -324
  164. package/src/config/index.ts +0 -239
  165. package/src/config/lazy.ts +0 -132
  166. package/src/config/manifest.ts +0 -120
  167. package/src/config/utils.ts +0 -5
  168. package/src/config/vite-utils.ts +0 -113
  169. package/src/directives/bubble-function-declaration.ts +0 -33
  170. package/src/directives/compile.ts +0 -54
  171. package/src/directives/generate-unique-name.ts +0 -22
  172. package/src/directives/get-descriptive-name.ts +0 -39
  173. package/src/directives/get-import-identifier.ts +0 -34
  174. package/src/directives/get-root-statement-path.ts +0 -14
  175. package/src/directives/index.ts +0 -233
  176. package/src/directives/is-statement-top-level.ts +0 -13
  177. package/src/directives/paths.ts +0 -63
  178. package/src/directives/plugin.ts +0 -389
  179. package/src/directives/remove-unused-variables.ts +0 -70
  180. package/src/directives/xxhash32.ts +0 -214
  181. package/src/env.d.ts +0 -17
  182. package/src/fns/client.ts +0 -141
  183. package/src/fns/handler.spec.ts +0 -85
  184. package/src/fns/handler.ts +0 -287
  185. package/src/fns/registration.ts +0 -19
  186. package/src/fns/serialization.ts +0 -250
  187. package/src/fns/server.ts +0 -46
  188. package/src/fns/shared.ts +0 -116
  189. package/src/http/index.ts +0 -216
  190. package/src/internal.d.ts +0 -14
  191. package/src/middleware/index.ts +0 -92
  192. package/src/router.tsx +0 -37
  193. package/src/server/StartServer.tsx +0 -58
  194. package/src/server/assets/index.ts +0 -70
  195. package/src/server/assets/render.tsx +0 -46
  196. package/src/server/collect-styles.ts +0 -70
  197. package/src/server/fetchEvent.spec.ts +0 -85
  198. package/src/server/fetchEvent.ts +0 -36
  199. package/src/server/handler.ts +0 -261
  200. package/src/server/manifest/client-manifest.ts +0 -8
  201. package/src/server/manifest/dev-client-manifest.ts +0 -21
  202. package/src/server/manifest/dev-ssr-manifest.ts +0 -23
  203. package/src/server/manifest/prod-client-manifest.ts +0 -21
  204. package/src/server/manifest/prod-ssr-manifest.ts +0 -109
  205. package/src/server/manifest/ssr-manifest.ts +0 -10
  206. package/src/server/routes.ts +0 -126
  207. package/src/server/spa/StartServer.tsx +0 -42
  208. package/src/server/spa/handler.ts +0 -29
  209. package/src/server/types.ts +0 -82
  210. package/src/server/util.ts +0 -12
  211. package/src/shared/ErrorBoundary.tsx +0 -52
  212. package/src/shared/GET.ts +0 -8
  213. package/src/shared/HttpHeader.tsx +0 -42
  214. package/src/shared/HttpStatusCode.ts +0 -27
  215. package/src/shared/clientOnly.ts +0 -44
  216. package/src/shared/dev-overlay/CodeView.tsx +0 -90
  217. package/src/shared/dev-overlay/DevOverlayDialog.tsx +0 -316
  218. package/src/shared/dev-overlay/createStackFrame.ts +0 -74
  219. package/src/shared/dev-overlay/download.ts +0 -6
  220. package/src/shared/dev-overlay/env.d.ts +0 -4
  221. package/src/shared/dev-overlay/get-source-map.ts +0 -33
  222. package/src/shared/dev-overlay/icons.tsx +0 -456
  223. package/src/shared/dev-overlay/index.tsx +0 -63
  224. package/src/shared/lazy.ts +0 -52
  225. package/src/shared/serverFunction.ts +0 -10
  226. package/src/utils/debounce.ts +0 -10
  227. package/src/virtual.d.ts +0 -27
  228. package/tsconfig.json +0 -24
  229. package/vitest.config.ts +0 -9
  230. /package/{src/client/index.tsx → dist/client/index.d.ts} +0 -0
  231. /package/{src → dist}/shared/dev-overlay/styles.css +0 -0
@@ -1,19 +0,0 @@
1
- export const DEFAULT_EXTENSIONS = ["js", "jsx", "ts", "tsx"];
2
-
3
- export const CLIENT_BASE_PATH = "_build";
4
-
5
- export const VIRTUAL_MODULES = {
6
- clientViteManifest: "solid-start:client-vite-manifest",
7
- getClientManifest: "solid-start:get-client-manifest",
8
- getManifest: "solid-start:get-manifest",
9
- middleware: "solid-start:middleware",
10
- serverFnManifest: "solid-start:server-fn-manifest",
11
- clientEntry: "solid-start:client-entry",
12
- serverEntry: "solid-start:server-entry",
13
- app: "solid-start:app",
14
- } as const;
15
-
16
- export const VITE_ENVIRONMENTS = {
17
- client: "client" as const,
18
- server: "ssr" as const,
19
- };
@@ -1,161 +0,0 @@
1
- import { join } from "node:path";
2
- import { pathToFileURL } from "node:url";
3
- import { NodeRequest, sendNodeResponse } from "srvx/node";
4
- import {
5
- type Connect,
6
- isRunnableDevEnvironment,
7
- type PluginOption,
8
- type ViteDevServer,
9
- } from "vite";
10
- import { VITE_ENVIRONMENTS } from "./constants.ts";
11
-
12
- export function devServer(): Array<PluginOption> {
13
- return [
14
- {
15
- name: "solid-start-dev-server",
16
- configurePreviewServer(server) {
17
- return () => {
18
- server.middlewares.use(async (req, res) => {
19
- res.setHeader("content-encoding", "identity");
20
- const webReq = new NodeRequest({ req, res });
21
- const def: {
22
- default: { fetch: (req: Request) => Promise<Response> };
23
- } = await import(
24
- pathToFileURL(join(process.cwd(), "dist/server/entry-server.js")).href
25
- );
26
- sendNodeResponse(res, await def.default.fetch(webReq));
27
- });
28
- };
29
- },
30
- configureServer(viteDevServer) {
31
- (globalThis as any).VITE_DEV_SERVER = viteDevServer;
32
- return async () => {
33
- if (viteDevServer.config.server.middlewareMode) return;
34
-
35
- const serverEnv = viteDevServer.environments[VITE_ENVIRONMENTS.server];
36
-
37
- if (!serverEnv) throw new Error("Server environment not found");
38
- if (
39
- // do not check via `isFetchableDevEnvironment` since nitro does implement the `FetchableDevEnvironment` interface but not via inheritance (which this helper checks)
40
- "dispatchFetch" in serverEnv
41
- )
42
- return;
43
- // another plugin is controlling the dev server
44
- if (!isRunnableDevEnvironment(serverEnv)) {
45
- return;
46
- }
47
-
48
- globalThis.USING_SOLID_START_DEV_SERVER = true;
49
-
50
- removeHtmlMiddlewares(viteDevServer);
51
-
52
- viteDevServer.middlewares.use(async (req, res) => {
53
- if (req.originalUrl) {
54
- req.url = req.originalUrl;
55
- }
56
- const webReq = new NodeRequest({ req, res });
57
-
58
- try {
59
- const serverEntry: {
60
- default: { fetch: (req: Request) => Promise<Response> };
61
- } = await serverEnv.runner.import("./src/entry-server.tsx");
62
-
63
- const webRes = await serverEntry.default.fetch(webReq);
64
-
65
- return sendNodeResponse(res, webRes);
66
- } catch (e: unknown) {
67
- console.error(e);
68
- viteDevServer.ssrFixStacktrace(e as Error);
69
-
70
- if (webReq.headers.get("content-type")?.includes("application/json")) {
71
- return sendNodeResponse(
72
- res,
73
- Response.json(
74
- {
75
- status: 500,
76
- error: "Internal Server Error",
77
- message: "An unexpected error occurred. Please try again later.",
78
- timestamp: new Date().toISOString(),
79
- },
80
- {
81
- status: 500,
82
- headers: {
83
- "Content-Type": "application/json",
84
- },
85
- },
86
- ),
87
- );
88
- }
89
-
90
- return sendNodeResponse(
91
- res,
92
- new Response(
93
- `
94
- <!DOCTYPE html>
95
- <html lang="en">
96
- <head>
97
- <meta charset="UTF-8" />
98
- <title>Error</title>
99
- <script type="module">
100
- import { ErrorOverlay } from '/@vite/client'
101
- document.body.appendChild(new ErrorOverlay(${JSON.stringify(
102
- prepareError(req, e),
103
- ).replace(/</g, "\\u003c")}))
104
- </script>
105
- </head>
106
- <body>
107
- </body>
108
- </html>
109
- `,
110
- {
111
- status: 500,
112
- headers: { "Content-Type": "text/html" },
113
- },
114
- ),
115
- );
116
- }
117
- });
118
- };
119
- },
120
- },
121
- ];
122
- }
123
-
124
- /**
125
- * Removes Vite internal middleware
126
- *
127
- * @param server
128
- */
129
- function removeHtmlMiddlewares(server: ViteDevServer) {
130
- const html_middlewares = [
131
- "viteIndexHtmlMiddleware",
132
- "vite404Middleware",
133
- "viteSpaFallbackMiddleware",
134
- ];
135
- for (let i = server.middlewares.stack.length - 1; i > 0; i--) {
136
- if (
137
- html_middlewares.includes(
138
- // @ts-expect-error
139
- server.middlewares.stack[i].handle.name,
140
- )
141
- ) {
142
- server.middlewares.stack.splice(i, 1);
143
- }
144
- }
145
- }
146
-
147
- /**
148
- * Formats error for SSR message in error overlay
149
- * @param req
150
- * @param error
151
- * @returns
152
- */
153
- function prepareError(req: Connect.IncomingMessage, error: unknown) {
154
- const e = error as Error;
155
- return {
156
- message: `An error occured while server rendering ${req.url}:\n\n\t${
157
- typeof e === "string" ? e : e.message
158
- } `,
159
- stack: typeof e === "string" ? "" : e.stack,
160
- };
161
- }
package/src/config/env.ts DELETED
@@ -1,105 +0,0 @@
1
- import { loadEnv, type Plugin } from "vite";
2
-
3
- const LOADERS = {
4
- node: `export default key => process.env[key];`,
5
- "cloudflare-workers": `import { env } from 'cloudflare:workers';export default key => env[key];`,
6
- "netlify-edge": `export default key => Netlify.env.get(key);`,
7
- };
8
-
9
- export interface EnvPluginOptions {
10
- server?: {
11
- runtime?: keyof typeof LOADERS | (string & {});
12
- load?: (mode: string) => Record<string, string>;
13
- prefix?: string;
14
- };
15
- client?: {
16
- load?: (mode: string) => Record<string, string>;
17
- prefix?: string;
18
- };
19
- }
20
-
21
- const SERVER_ENV = "env:server";
22
- const CLIENT_ENV = "env:client";
23
-
24
- const SERVER_RUNTIME_ENV = `${SERVER_ENV}/runtime`;
25
-
26
- const SERVER_RUNTIME_LOADER = `${SERVER_RUNTIME_ENV}/loader`;
27
-
28
- const DEFAULT_SERVER_PREFIX = "SERVER_";
29
- const DEFAULT_CLIENT_PREFIX = "CLIENT_";
30
-
31
- const SERVER_ONLY_MODULE = `throw new Error('Attempt to load server-only environment variables in client runtime.');`;
32
-
33
- const SERVER_RUNTIME_CODE = `import load from '${SERVER_RUNTIME_LOADER}';
34
-
35
- export default new Proxy({}, {
36
- get(_, key) {
37
- return load(key);
38
- },
39
- })`;
40
-
41
- function convertObjectToModule(object: Record<string, string>): string {
42
- let result = "";
43
- for (const key in object) {
44
- result += `export const ${key} = ${JSON.stringify(object[key])};`;
45
- }
46
- return result;
47
- }
48
-
49
- export function envPlugin(options?: EnvPluginOptions): Plugin {
50
- const currentOptions = options ?? {};
51
- let env: string;
52
- const serverPrefix = currentOptions.server?.prefix ?? DEFAULT_SERVER_PREFIX;
53
- const clientPrefix = currentOptions.client?.prefix ?? DEFAULT_CLIENT_PREFIX;
54
- const runtime = options?.server?.runtime ?? "node";
55
- const runtimeCode = runtime in LOADERS ? LOADERS[runtime as keyof typeof LOADERS] : runtime;
56
-
57
- return {
58
- name: "solid-start:env",
59
- enforce: "pre",
60
- configResolved(config) {
61
- env = config.mode !== "production" ? "development" : "production";
62
- },
63
- resolveId(id) {
64
- if (
65
- id === SERVER_ENV ||
66
- id === CLIENT_ENV ||
67
- id === SERVER_RUNTIME_ENV ||
68
- id === SERVER_RUNTIME_LOADER
69
- ) {
70
- return id;
71
- }
72
- return undefined;
73
- },
74
- load(id, opts) {
75
- if (id === SERVER_ENV) {
76
- if (!opts?.ssr) {
77
- return SERVER_ONLY_MODULE;
78
- }
79
- const vars = currentOptions.server?.load
80
- ? currentOptions.server.load(env)
81
- : loadEnv(env, '.', serverPrefix);
82
- return convertObjectToModule(vars);
83
- }
84
- if (id === CLIENT_ENV) {
85
- const vars = currentOptions.client?.load
86
- ? currentOptions.client.load(env)
87
- : loadEnv(env, '.', clientPrefix);
88
- return convertObjectToModule(vars);
89
- }
90
- if (id === SERVER_RUNTIME_LOADER) {
91
- if (!opts?.ssr) {
92
- return SERVER_ONLY_MODULE;
93
- }
94
- return runtimeCode;
95
- }
96
- if (id === SERVER_RUNTIME_ENV) {
97
- if (!opts?.ssr) {
98
- return SERVER_ONLY_MODULE;
99
- }
100
- return SERVER_RUNTIME_CODE;
101
- }
102
- return undefined;
103
- },
104
- };
105
- }
@@ -1,162 +0,0 @@
1
- import type { ExportSpecifier } from "es-module-lexer";
2
- import {
3
- analyzeModule,
4
- BaseFileSystemRouter,
5
- cleanPath,
6
- type FileSystemRouterConfig,
7
- } from "./fs-routes/router.ts";
8
-
9
- export class SolidStartClientFileRouter extends BaseFileSystemRouter {
10
- toPath(src: string) {
11
- const routePath = cleanPath(src, this.config)
12
- // remove the initial slash
13
- .slice(1)
14
- .replace(/index$/, "")
15
- .replace(/\[([^/]+)\]/g, (_, m) => {
16
- if (m.length > 3 && m.startsWith("...")) {
17
- return `*${m.slice(3)}`;
18
- }
19
- if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) {
20
- return `:${m.slice(1, -1)}?`;
21
- }
22
- return `:${m}`;
23
- });
24
-
25
- return routePath?.length > 0 ? `/${routePath}` : "/";
26
- }
27
-
28
- toRoute(src: string) {
29
- const path = this.toPath(src);
30
-
31
- if (src.endsWith(".md") || src.endsWith(".mdx")) {
32
- return {
33
- page: true,
34
- $component: {
35
- src: src,
36
- pick: ["$css"],
37
- },
38
- $$route: undefined,
39
- path,
40
- // filePath: src
41
- };
42
- }
43
-
44
- const [_, exports] = analyzeModule(src);
45
- const hasDefault = !!exports.find(e => e.n === "default");
46
- const hasRouteConfig = !!exports.find(e => e.n === "route");
47
- if (hasDefault) {
48
- return {
49
- page: true,
50
- $component: {
51
- src: src,
52
- pick: [
53
- ...exports.filter(e => e.n === e.ln && e.n !== "route").map(e => e.n),
54
- "default",
55
- "$css",
56
- ],
57
- },
58
- $$route: hasRouteConfig
59
- ? {
60
- src: src,
61
- pick: ["route"],
62
- }
63
- : undefined,
64
- path,
65
- // filePath: src
66
- };
67
- }
68
- }
69
- }
70
-
71
- const HTTP_METHODS = ["HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"];
72
- function createHTTPHandlers(src: string, exports: readonly ExportSpecifier[]) {
73
- const handlers: Record<string, any> = {};
74
- for (const exp of exports) {
75
- if (HTTP_METHODS.includes(exp.n)) {
76
- handlers[`$${exp.n}`] = {
77
- src: src,
78
- pick: [exp.n],
79
- };
80
- if (exp.n === "GET" && !exports.find(exp => exp.n === "HEAD")) {
81
- handlers.$HEAD = {
82
- src: src,
83
- pick: [exp.n],
84
- };
85
- }
86
- }
87
- }
88
-
89
- return handlers;
90
- }
91
-
92
- export class SolidStartServerFileRouter extends BaseFileSystemRouter {
93
- declare config: FileSystemRouterConfig & { dataOnly?: boolean };
94
-
95
- constructor(config: FileSystemRouterConfig & { dataOnly?: boolean }) {
96
- super(config);
97
- }
98
-
99
- toPath(src: string) {
100
- const routePath = cleanPath(src, this.config)
101
- // remove the initial slash
102
- .slice(1)
103
- .replace(/index$/, "")
104
- .replace(/\[([^/]+)\]/g, (_, m) => {
105
- if (m.length > 3 && m.startsWith("...")) {
106
- return `*${m.slice(3)}`;
107
- }
108
- if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) {
109
- return `:${m.slice(1, -1)}?`;
110
- }
111
- return `:${m}`;
112
- });
113
-
114
- return routePath?.length > 0 ? `/${routePath}` : "/";
115
- }
116
-
117
- toRoute(src: string) {
118
- const path = this.toPath(src);
119
- if (src.endsWith(".md") || src.endsWith(".mdx")) {
120
- return {
121
- page: true,
122
- $component: {
123
- src: src,
124
- pick: ["$css"],
125
- },
126
- $$route: undefined,
127
- path,
128
- };
129
- }
130
-
131
- const [_, exports] = analyzeModule(src);
132
- const hasRouteConfig = exports.find(e => e.n === "route");
133
- const hasDefault = !!exports.find(e => e.n === "default");
134
- const hasAPIRoutes = !!exports.find(exp => HTTP_METHODS.includes(exp.n));
135
- if (hasDefault || hasAPIRoutes) {
136
- return {
137
- page: hasDefault,
138
- $component:
139
- !this.config.dataOnly && hasDefault
140
- ? {
141
- src: src,
142
- pick: [
143
- ...exports
144
- .filter(e => e.n === e.ln && e.n !== "route" && !HTTP_METHODS.includes(e.n))
145
- .map(e => e.n),
146
- "default",
147
- "$css",
148
- ],
149
- }
150
- : undefined,
151
- $$route: hasRouteConfig
152
- ? {
153
- src: src,
154
- pick: ["route"],
155
- }
156
- : undefined,
157
- ...createHTTPHandlers(src, exports),
158
- path,
159
- };
160
- }
161
- }
162
- }
@@ -1,66 +0,0 @@
1
- import type { EnvironmentModuleNode, FSWatcher, PluginOption, ViteDevServer } from "vite";
2
- import { debounce } from "../../utils/debounce.ts";
3
- import { VITE_ENVIRONMENTS } from "../constants.ts";
4
- import { moduleId } from "./index.ts";
5
- import type { BaseFileSystemRouter } from "./router.ts";
6
-
7
- function setupWatcher(watcher: FSWatcher, routes: BaseFileSystemRouter): void {
8
- watcher.on("unlink", path => routes.removeRoute(path));
9
- watcher.on("add", path => routes.addRoute(path));
10
- watcher.on("change", path => routes.updateRoute(path));
11
- }
12
-
13
- function createRoutesReloader(
14
- server: ViteDevServer,
15
- routes: BaseFileSystemRouter,
16
- environment: "client" | "ssr",
17
- ) {
18
- const devEnv = server.environments[environment]!;
19
- if (!devEnv?.moduleGraph) return;
20
-
21
- /**
22
- * Debounce catches multiple route changes in a row
23
- * Short timeout for inexpensive invalidations
24
- */
25
- const invalidateModule = debounce((mod: EnvironmentModuleNode) => {
26
- devEnv.moduleGraph.invalidateModule(mod);
27
- }, 0);
28
-
29
- /**
30
- * Long debounce timeout for expensive reloads
31
- */
32
- const reloadModule = debounce((mod: EnvironmentModuleNode) => {
33
- devEnv.reloadModule(mod);
34
- }, 200);
35
-
36
- return routes.on("reload", function handleRoutesReload(evt): void {
37
- const mod = devEnv.moduleGraph.getModuleById(moduleId)!;
38
- if (!mod) {
39
- devEnv.hot.send({ type: "full-reload" });
40
- return;
41
- }
42
-
43
- if (environment === VITE_ENVIRONMENTS.client && evt.detail.type !== "update") {
44
- // Client has to be reloaded when routes are added or removed
45
- reloadModule(mod);
46
- } else {
47
- invalidateModule(mod);
48
- }
49
- });
50
- }
51
-
52
- export const fileSystemWatcher = (
53
- routers: Record<"client" | "ssr", BaseFileSystemRouter>,
54
- ): PluginOption => {
55
- const plugin: PluginOption = {
56
- name: "fs-watcher",
57
- async configureServer(server: ViteDevServer) {
58
- for (const environment of [VITE_ENVIRONMENTS.server, VITE_ENVIRONMENTS.client]) {
59
- const router = routers[environment];
60
- setupWatcher(server.watcher, router);
61
- createRoutesReloader(server, router, environment);
62
- }
63
- },
64
- };
65
- return plugin;
66
- };
@@ -1,136 +0,0 @@
1
- import { relative } from "node:path";
2
- import { type PluginOption } from "vite";
3
-
4
- import { fileSystemWatcher } from "./fs-watcher.ts";
5
- import type { BaseFileSystemRouter } from "./router.ts";
6
- import { treeShake } from "./tree-shake.ts";
7
-
8
- export const moduleId = "solid-start:routes";
9
-
10
- export interface FsRoutesArgs {
11
- routers: Record<"client" | "ssr", BaseFileSystemRouter>;
12
- }
13
-
14
- export function fsRoutes({ routers }: FsRoutesArgs): Array<PluginOption> {
15
- (globalThis as any).ROUTERS = routers;
16
-
17
- return [
18
- {
19
- name: "solid-start-fs-routes",
20
- enforce: "pre",
21
- resolveId(id) {
22
- if (id === moduleId) return id;
23
- },
24
- async load(id) {
25
- const root = this.environment.config.root;
26
- const isBuild = this.environment.mode === "build";
27
-
28
- if (id !== moduleId) return;
29
- const js = jsCode();
30
-
31
- const router = (globalThis as any).ROUTERS[this.environment.name];
32
-
33
- const routes = await router.getRoutes();
34
-
35
- let routesCode = JSON.stringify(routes ?? [], (k, v) => {
36
- if (v === undefined) return undefined;
37
-
38
- if (k.startsWith("$$")) {
39
- const buildId = `${v.src}?${v.pick.map((p: any) => `pick=${p}`).join("&")}`;
40
-
41
- /**
42
- * @type {{ [key: string]: string }}
43
- */
44
- const refs: Record<string, string> = {};
45
- for (var pick of v.pick) {
46
- refs[pick] = js.addNamedImport(pick, buildId);
47
- }
48
- return {
49
- require: `_$() => ({ ${Object.entries(refs)
50
- .map(([pick, namedImport]) => `'${pick}': ${namedImport}`)
51
- .join(", ")} })$_`,
52
- // src: isBuild ? relative(root, buildId) : buildId
53
- };
54
- } else if (k.startsWith("$")) {
55
- const buildId = `${v.src}?${v.pick.map((p: any) => `pick=${p}`).join("&")}`;
56
- return {
57
- src: relative(root, buildId),
58
- build: isBuild ? `_$() => import('${buildId}')$_` : undefined,
59
- import: `_$() => import('${buildId}')$_`,
60
- };
61
- }
62
- return v;
63
- });
64
-
65
- routesCode = routesCode.replaceAll('"_$(', "(").replaceAll(')$_"', ")");
66
-
67
- const code = `
68
- ${js.getImportStatements()}
69
- export default ${routesCode}`;
70
- return code;
71
- },
72
- },
73
- treeShake(),
74
- fileSystemWatcher(routers),
75
- ];
76
- }
77
-
78
- function jsCode() {
79
- const imports = new Map();
80
- let vars = 0;
81
-
82
- function addImport(p: any) {
83
- let id = imports.get(p);
84
- if (!id) {
85
- id = {};
86
- imports.set(p, id);
87
- }
88
-
89
- const d = "routeData" + vars++;
90
- id["default"] = d;
91
- return d;
92
- }
93
-
94
- function addNamedImport(name: string | number, p: any) {
95
- let id = imports.get(p);
96
- if (!id) {
97
- id = {};
98
- imports.set(p, id);
99
- }
100
-
101
- const d = "routeData" + vars++;
102
- id[name] = d;
103
- return d;
104
- }
105
-
106
- const getNamedExport = (p: any) => {
107
- const id = imports.get(p);
108
-
109
- delete id["default"];
110
-
111
- return Object.keys(id).length > 0
112
- ? `{ ${Object.keys(id)
113
- .map(k => `${k} as ${id[k]}`)
114
- .join(", ")} }`
115
- : "";
116
- };
117
-
118
- const getImportStatements = () => {
119
- return `${[...imports.keys()]
120
- .map(
121
- i =>
122
- `import ${
123
- imports.get(i).default
124
- ? `${imports.get(i).default}${Object.keys(imports.get(i)).length > 1 ? ", " : ""}`
125
- : ""
126
- } ${getNamedExport(i)} from '${i}';`,
127
- )
128
- .join("\n")}`;
129
- };
130
-
131
- return {
132
- addImport,
133
- addNamedImport,
134
- getImportStatements,
135
- };
136
- }