@gracile/engine 0.0.4-next.3 → 0.0.4-next.35

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 (70) hide show
  1. package/ambient.d.ts +6 -0
  2. package/dist/assertions.d.ts +1 -1
  3. package/dist/assertions.d.ts.map +1 -1
  4. package/dist/build/static.d.ts +11 -2
  5. package/dist/build/static.d.ts.map +1 -1
  6. package/dist/build/static.js +29 -16
  7. package/dist/dev/dev.d.ts +5 -2
  8. package/dist/dev/dev.d.ts.map +1 -1
  9. package/dist/dev/dev.js +14 -7
  10. package/dist/dev/server.d.ts +22 -2
  11. package/dist/dev/server.d.ts.map +1 -1
  12. package/dist/dev/server.js +51 -23
  13. package/dist/preview.d.ts +1 -1
  14. package/dist/preview.d.ts.map +1 -1
  15. package/dist/preview.js +3 -0
  16. package/dist/render/route-template.d.ts +15 -4
  17. package/dist/render/route-template.d.ts.map +1 -1
  18. package/dist/render/route-template.js +45 -15
  19. package/dist/render/utils.d.ts.map +1 -0
  20. package/dist/routes/collect.d.ts +2 -3
  21. package/dist/routes/collect.d.ts.map +1 -1
  22. package/dist/routes/collect.js +4 -1
  23. package/dist/routes/load-module.d.ts +5 -1
  24. package/dist/routes/load-module.d.ts.map +1 -1
  25. package/dist/routes/load-module.js +13 -4
  26. package/dist/routes/match.d.ts +4 -2
  27. package/dist/routes/match.d.ts.map +1 -1
  28. package/dist/routes/match.js +9 -6
  29. package/dist/routes/route.d.ts +7 -1
  30. package/dist/routes/route.d.ts.map +1 -1
  31. package/dist/routes/route.js +6 -1
  32. package/dist/server/env.d.ts +5 -0
  33. package/dist/server/env.d.ts.map +1 -0
  34. package/dist/server/env.js +6 -0
  35. package/dist/server/request.d.ts +21 -0
  36. package/dist/server/request.d.ts.map +1 -0
  37. package/dist/{dev → server}/request.js +59 -26
  38. package/dist/server/server.d.ts +4 -0
  39. package/dist/server/server.d.ts.map +1 -0
  40. package/dist/server/server.js +25 -0
  41. package/dist/server/utils.d.ts +4 -0
  42. package/dist/server/utils.d.ts.map +1 -0
  43. package/dist/server/utils.js +21 -0
  44. package/dist/tsconfig.tsbuildinfo +1 -1
  45. package/dist/user-config.d.ts +11 -0
  46. package/dist/user-config.d.ts.map +1 -1
  47. package/dist/user-config.js +9 -0
  48. package/dist/vite/build.d.ts.map +1 -1
  49. package/dist/vite/build.js +82 -7
  50. package/dist/vite/config.d.ts +2 -0
  51. package/dist/vite/config.d.ts.map +1 -1
  52. package/dist/vite/config.js +11 -1
  53. package/dist/vite/plugins/build-routes.d.ts +28 -0
  54. package/dist/vite/plugins/build-routes.d.ts.map +1 -0
  55. package/dist/vite/plugins/build-routes.js +98 -0
  56. package/dist/vite/plugins/virtual-routes.d.ts +11 -0
  57. package/dist/vite/plugins/virtual-routes.d.ts.map +1 -0
  58. package/dist/vite/plugins/virtual-routes.js +42 -0
  59. package/dist/vite/server.d.ts +4 -1
  60. package/dist/vite/server.d.ts.map +1 -1
  61. package/dist/vite/server.js +5 -3
  62. package/package.json +11 -12
  63. package/dist/dev/request.d.ts +0 -4
  64. package/dist/dev/request.d.ts.map +0 -1
  65. package/dist/vite/plugins/html-static-pages.d.ts +0 -13
  66. package/dist/vite/plugins/html-static-pages.d.ts.map +0 -1
  67. package/dist/vite/plugins/html-static-pages.js +0 -58
  68. package/dist/vite/utils.d.ts.map +0 -1
  69. /package/dist/{vite → render}/utils.d.ts +0 -0
  70. /package/dist/{vite → render}/utils.js +0 -0
@@ -1,7 +1,6 @@
1
- import { routes } from './collect.js';
2
1
  import { loadForeignRouteObject } from './load-module.js';
3
- import * as R from './route.js';
4
- function matchRouteFromUrl(url) {
2
+ // FIXME: proper DI for routes
3
+ function matchRouteFromUrl(url, routes) {
5
4
  let match;
6
5
  let foundRoute;
7
6
  const pathname = new URL(url).pathname;
@@ -44,9 +43,13 @@ function extractStaticPaths(options) {
44
43
  return { staticPaths, props };
45
44
  }
46
45
  export async function getRoute(options) {
47
- const { foundRoute, pathname, params } = matchRouteFromUrl(options.url);
48
- const routePath = foundRoute.filePath;
49
- const routeModule = await loadForeignRouteObject(options.vite, routePath);
46
+ const { foundRoute, pathname, params } = matchRouteFromUrl(options.url, options.routes);
47
+ // TODO: Simplify all the routes things
48
+ const routeModule = await loadForeignRouteObject({
49
+ vite: options.vite,
50
+ route: foundRoute,
51
+ routeImports: options.routeImports,
52
+ });
50
53
  const staticPaths = extractStaticPaths({
51
54
  routeModule,
52
55
  foundRoute,
@@ -6,14 +6,16 @@ export type Method = MethodHtml & MethodNonHtml;
6
6
  export type ModuleOptions = {
7
7
  staticPaths?: StaticPathsGeneric;
8
8
  handler?: HandlerGeneric;
9
- template?: (context: RouteContextGeneric) => RouteTemplateResult;
9
+ prerender?: boolean | undefined;
10
10
  document?: DocumentTemplate<RouteContextGeneric>;
11
+ template?: (context: RouteContextGeneric) => RouteTemplateResult;
11
12
  };
12
13
  export declare class RouteModule {
13
14
  #private;
14
15
  get staticPaths(): StaticPathsGeneric | undefined;
15
16
  get handler(): Handler<object | Response | undefined> | Partial<Record<MethodHtml, Handler<object | Response | undefined>> & Record<MethodNonHtml, Handler<Response>>> | undefined;
16
17
  get document(): DocumentTemplate<RouteContextGeneric> | undefined;
18
+ get prerender(): boolean | undefined;
17
19
  get template(): ((context: RouteContextGeneric) => RouteTemplateResult) | undefined;
18
20
  constructor(options: ModuleOptions);
19
21
  }
@@ -59,5 +61,9 @@ export interface Route {
59
61
  pattern: URLPattern;
60
62
  hasParams: boolean;
61
63
  pageAssets: string[];
64
+ prerender: boolean | null;
62
65
  }
66
+ export type RoutesManifest = Map<string, Route>;
67
+ export type RoutesImports = Map<string, () => Record<string, unknown>>;
68
+ export type RoutesAssets = Map<string, string>;
63
69
  //# sourceMappingURL=route.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../src/routes/route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAC5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nB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,SAAS,EAAE,OAAO,GAAG,IAAI,CAAC;CAC1B;AAID,MAAM,MAAM,cAAc,GAAG,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AAChD,MAAM,MAAM,aAAa,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AACvE,MAAM,MAAM,YAAY,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC"}
@@ -12,6 +12,10 @@ export class RouteModule {
12
12
  get document() {
13
13
  return this.#document;
14
14
  }
15
+ #prerender;
16
+ get prerender() {
17
+ return this.#prerender;
18
+ }
15
19
  #template;
16
20
  get template() {
17
21
  return this.#template;
@@ -23,10 +27,11 @@ export class RouteModule {
23
27
  typeof options.handler === 'function') &&
24
28
  options.handler)
25
29
  this.#handler = options.handler;
26
- // if (options.fragment) this.#fragment = options.fragment;
27
30
  if (typeof options.template === 'function')
28
31
  this.#template = options.template;
29
32
  if (typeof options.document === 'function')
30
33
  this.#document = options.document;
34
+ if (typeof options.prerender === 'boolean')
35
+ this.#prerender = options.prerender;
31
36
  }
32
37
  }
@@ -0,0 +1,5 @@
1
+ export declare const IP_LOCALHOST = "127.0.0.1";
2
+ export declare const IP_EXPOSED = "0.0.0.0";
3
+ export declare const RANDOM_PORT = 0;
4
+ export declare const PUBLIC_DIR: "public" | "./dist/client";
5
+ //# sourceMappingURL=env.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../src/server/env.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,cAAc,CAAC;AACxC,eAAO,MAAM,UAAU,YAAY,CAAC;AACpC,eAAO,MAAM,WAAW,IAAI,CAAC;AAE7B,eAAO,MAAM,UAAU,4BAEM,CAAC"}
@@ -0,0 +1,6 @@
1
+ export const IP_LOCALHOST = '127.0.0.1';
2
+ export const IP_EXPOSED = '0.0.0.0';
3
+ export const RANDOM_PORT = 0;
4
+ export const PUBLIC_DIR = import.meta.env?.DEV
5
+ ? 'public'
6
+ : './dist/client';
@@ -0,0 +1,21 @@
1
+ import type { IncomingMessage, ServerResponse } from 'node:http';
2
+ import type { ViteDevServer } from 'vite';
3
+ import type * as R from '../routes/route.js';
4
+ type NextFunction = (error?: unknown) => void | Promise<void>;
5
+ /**
6
+ * This is fully compatible with Express or bare Node HTTP
7
+ * What it adds on top of Connect-style middleware:
8
+ * 1. Async.
9
+ * 2. Can return a `ServerResponse`
10
+ */
11
+ export type ConnectLikeAsyncMiddleware = (req: IncomingMessage, res: ServerResponse, next: NextFunction) => Promise<void | NextFunction | ServerResponse> | (void | NextFunction | ServerResponse);
12
+ export declare function createGracileMiddleware({ vite, routes, routeImports, routeAssets, root, serverMode, }: {
13
+ vite?: ViteDevServer | undefined;
14
+ routes: R.RoutesManifest;
15
+ routeImports?: R.RoutesImports | undefined;
16
+ routeAssets?: R.RoutesAssets;
17
+ root: string;
18
+ serverMode?: boolean | undefined;
19
+ }): ConnectLikeAsyncMiddleware;
20
+ export {};
21
+ //# sourceMappingURL=request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/server/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAMjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAS1C,OAAO,KAAK,KAAK,CAAC,MAAM,oBAAoB,CAAC;AAE7C,KAAK,YAAY,GAAG,CAAC,KAAK,CAAC,EAAE,OAAO,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE9D;;;;;GAKG;AACH,MAAM,MAAM,0BAA0B,GAAG,CACxC,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,cAAc,EACnB,IAAI,EAAE,YAAY,KAEjB,OAAO,CAAC,IAAI,GAAG,YAAY,GAAG,cAAc,CAAC,GAE5C,CAAC,IAAI,GAAG,YAAY,GAAG,cAAc,CAAC,CAAC;AAM1C,wBAAgB,uBAAuB,CAAC,EACvC,IAAI,EACJ,MAAM,EACN,YAAY,EACZ,WAAW,EACX,IAAI,EACJ,UAAU,GACV,EAAE;IACF,IAAI,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC;IACjC,MAAM,EAAE,CAAC,CAAC,cAAc,CAAC;IACzB,YAAY,CAAC,EAAE,CAAC,CAAC,aAAa,GAAG,SAAS,CAAC;IAC3C,WAAW,CAAC,EAAE,CAAC,CAAC,YAAY,CAAC;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CACjC,8BA6NA"}
@@ -1,36 +1,52 @@
1
- import { Writable } from 'node:stream';
1
+ import { Readable, Writable } from 'node:stream';
2
2
  import { logger } from '@gracile/internal-utils/logger';
3
3
  import { createServerAdapter } from '@whatwg-node/server';
4
4
  import c from 'picocolors';
5
5
  import { /* errorInline, */ errorPage } from '../errors/templates.js';
6
6
  import { renderRouteTemplate, } from '../render/route-template.js';
7
+ import { renderSsrTemplate } from '../render/utils.js';
7
8
  import { getRoute } from '../routes/match.js';
8
- import { renderSsrTemplate } from '../vite/utils.js';
9
9
  // NOTE: Find a more canonical way to ponyfill the Node HTTP request to standard Request
10
10
  // @ts-expect-error Abusing this feature!
11
11
  const adapter = createServerAdapter((request) => request);
12
- export function createDevRequestHandler(vite) {
13
- return async (req, res, next) => {
14
- const url = req.originalUrl;
15
- logger.info(`[${c.yellow(req.method)}] ${c.yellow(url)}`, {
12
+ export function createGracileMiddleware({ vite, routes, routeImports, routeAssets, root, serverMode, }) {
13
+ const middleware = async (req, res, next) => {
14
+ // Typing workaround
15
+ if (!req.url)
16
+ throw Error('Incorrect url');
17
+ if (!req.method)
18
+ throw Error('Incorrect method');
19
+ const nodeRequest = { ...req, url: req.url, method: req.method };
20
+ logger.info(`[${c.yellow(req.method)}] ${c.yellow(req.url)}`, {
16
21
  timestamp: true,
17
22
  });
18
23
  // MARK: Skip unwanted requests
19
24
  if (
20
25
  //
21
- url.endsWith('favicon.ico') ||
22
- url.endsWith('favicon.svg'))
26
+ req.url.endsWith('favicon.ico') ||
27
+ req.url.endsWith('favicon.svg'))
23
28
  return next();
24
- const requestPonyfilled = (await Promise.resolve(adapter.handleNodeRequest(req)));
29
+ const requestPonyfilled = (await Promise.resolve(adapter.handleNodeRequest(nodeRequest)));
25
30
  async function renderPageFn(handlerInfos, routeInfos) {
26
- const { output } = await renderRouteTemplate(requestPonyfilled, vite, 'dev', routeInfos, handlerInfos);
27
- return output;
31
+ const { output } = await renderRouteTemplate({
32
+ request: requestPonyfilled,
33
+ vite,
34
+ mode: 'dev',
35
+ routeInfos,
36
+ handlerInfos,
37
+ routeAssets,
38
+ root,
39
+ serverMode,
40
+ });
41
+ return output || undefined;
28
42
  }
29
43
  try {
30
44
  // MARK: Get route infos
31
45
  const moduleInfos = await getRoute({
32
46
  url: requestPonyfilled.url,
33
47
  vite,
48
+ routes,
49
+ routeImports,
34
50
  });
35
51
  let output;
36
52
  // TODO: should move this to `special-file` so we don't recalculate on each request
@@ -85,10 +101,14 @@ export function createDevRequestHandler(vite) {
85
101
  if (output instanceof Response) {
86
102
  if (output.status >= 300 && output.status <= 303) {
87
103
  const location = output.headers.get('location');
88
- if (location)
89
- return res.redirect(location);
104
+ // if (location) return res.redirect(location);
105
+ if (location) {
106
+ res.statusCode = output.status;
107
+ res.setHeader('location', location);
108
+ return res.end(`Found. Redirecting to ${location}`);
109
+ }
90
110
  }
91
- output.headers?.forEach((content, header) => res.set(header, content));
111
+ output.headers?.forEach((content, header) => res.setHeader(header, content));
92
112
  if (output.status)
93
113
  res.statusCode = output.status;
94
114
  if (output.statusText)
@@ -100,17 +120,18 @@ export function createDevRequestHandler(vite) {
100
120
  output.body
101
121
  .pipeTo(Writable.toWeb(res))
102
122
  .catch((e) => logger.error(String(e)));
123
+ // else throw new Error('Missing body.');
103
124
  else
104
- throw new Error('Missing body.');
125
+ return res.end(output);
105
126
  // MARK: Stream page render
106
127
  }
107
128
  else {
108
- new Headers(response.headers)?.forEach((content, header) => res.set(header, content));
129
+ new Headers(response.headers)?.forEach((content, header) => res.setHeader(header, content));
109
130
  if (response.status)
110
131
  res.statusCode = response.status;
111
132
  if (response.statusText)
112
133
  res.statusMessage = response.statusText;
113
- res.set({ 'Content-Type': 'text/html' });
134
+ res.setHeader('Content-Type', 'text/html');
114
135
  // MARK: Page stream error
115
136
  output
116
137
  ?.on('error', (error) => {
@@ -124,11 +145,12 @@ export function createDevRequestHandler(vite) {
124
145
  // Maybe just returning nothing is better to not break the page?
125
146
  // Should send a overlay message anyway via WebSocket
126
147
  // vite.ws.send()
127
- setTimeout(() => {
128
- vite.hot.send('gracile:ssr-error', {
129
- message: errorMessage,
130
- });
131
- }, 500);
148
+ if (vite)
149
+ setTimeout(() => {
150
+ vite.hot.send('gracile:ssr-error', {
151
+ message: errorMessage,
152
+ });
153
+ }, 500);
132
154
  res.end('' /* errorInline(error) */);
133
155
  })
134
156
  .pipe(res);
@@ -137,15 +159,26 @@ export function createDevRequestHandler(vite) {
137
159
  }
138
160
  catch (e) {
139
161
  const error = e;
140
- vite.ssrFixStacktrace(error);
162
+ if (vite)
163
+ vite.ssrFixStacktrace(error);
164
+ // else
165
+ logger.error(error.message);
141
166
  if (error.cause === 404) {
142
- return res.status(404).end('404');
167
+ // TODO: Handle 404 with dedicated page
168
+ if (!vite)
169
+ return next();
170
+ res.statusCode = 404;
171
+ return res.end('404');
143
172
  // TODO: use a nice framework service page
144
173
  // .redirect(new URL('/__404/', requestPonyfilled.url).href)
145
174
  }
146
- const errorTemplate = await renderSsrTemplate(errorPage(error));
147
- res.status(500).end(await vite.transformIndexHtml(url, errorTemplate));
175
+ let errorTemplate = await renderSsrTemplate(errorPage(error));
176
+ if (vite)
177
+ errorTemplate = await vite.transformIndexHtml(req.url, errorTemplate);
178
+ res.statusCode = 500;
179
+ return res.end(errorTemplate);
148
180
  }
149
181
  return next();
150
182
  };
183
+ return middleware;
151
184
  }
@@ -0,0 +1,4 @@
1
+ import type { CreateMiddleware } from '../dev/server.js';
2
+ export declare const createHandlers: CreateMiddleware;
3
+ export { printNodeHttpServerAddressInfos as printAddressInfos } from './utils.js';
4
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server/server.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAUzD,eAAO,MAAM,cAAc,EAAE,gBAiB5B,CAAC;AAEF,OAAO,EAAE,+BAA+B,IAAI,iBAAiB,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { setCurrentWorkingDirectory } from '@gracile/internal-utils/paths';
2
+ import { routeAssets, routeImports, routes } from 'gracile:routes';
3
+ import { createGracileMiddleware } from './request.js';
4
+ routes.forEach((route, pattern) => {
5
+ routes.set(pattern, {
6
+ ...route,
7
+ pattern: new URLPattern(pattern, 'http://gracile'),
8
+ });
9
+ });
10
+ export const createHandlers = async ({ root = process.cwd(),
11
+ // hmrPort,
12
+ // NOTE: We need type parity with the dev. version of this function
13
+ // eslint-disable-next-line @typescript-eslint/require-await
14
+ } = {}) => {
15
+ setCurrentWorkingDirectory(root);
16
+ const gracileHandler = createGracileMiddleware({
17
+ root,
18
+ routes,
19
+ routeImports,
20
+ routeAssets,
21
+ serverMode: true,
22
+ });
23
+ return { handlers: [gracileHandler /* as RequestHandler */], vite: null };
24
+ };
25
+ export { printNodeHttpServerAddressInfos as printAddressInfos } from './utils.js';
@@ -0,0 +1,4 @@
1
+ import type { Server } from 'http';
2
+ import type { AddressInfo } from 'net';
3
+ export declare function printNodeHttpServerAddressInfos(instance: Server): AddressInfo;
4
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/server/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAKvC,wBAAgB,+BAA+B,CAAC,QAAQ,EAAE,MAAM,GAAG,WAAW,CAsB7E"}
@@ -0,0 +1,21 @@
1
+ // NOTE: Util. to pretty print for user provided server.
2
+ import { logger } from '@gracile/internal-utils/logger';
3
+ import { DEV } from 'esm-env';
4
+ import c from 'picocolors';
5
+ import { IP_EXPOSED } from './env.js';
6
+ export function printNodeHttpServerAddressInfos(instance) {
7
+ const infos = instance.address();
8
+ logger.info(c.green(`${DEV ? 'development' : 'production'} server started`), {
9
+ timestamp: true,
10
+ });
11
+ if (typeof infos === 'object' && infos && infos.port && infos.address) {
12
+ logger.info(`
13
+ ${c.dim('┃')} Local ${c.cyan(`http://localhost:${infos.port}/`)}` +
14
+ `${infos.address === IP_EXPOSED
15
+ ? `${c.dim('┃')} Network ${c.cyan(`http://${infos.address}:${infos.port}/`)}`
16
+ : ''}
17
+ `);
18
+ return infos;
19
+ }
20
+ throw Error('Invalid address/port.');
21
+ }