@analogjs/vite-plugin-nitro 2.4.0-beta.9 → 3.0.0-alpha.10

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 (50) hide show
  1. package/package.json +6 -8
  2. package/src/index.d.ts +9 -9
  3. package/src/index.js +6 -2
  4. package/src/index.js.map +1 -1
  5. package/src/lib/build-server.d.ts +3 -2
  6. package/src/lib/build-server.js +107 -53
  7. package/src/lib/build-server.js.map +1 -1
  8. package/src/lib/build-sitemap.d.ts +6 -6
  9. package/src/lib/build-sitemap.js +48 -60
  10. package/src/lib/build-sitemap.js.map +1 -1
  11. package/src/lib/build-ssr.d.ts +2 -2
  12. package/src/lib/build-ssr.js +16 -18
  13. package/src/lib/build-ssr.js.map +1 -1
  14. package/src/lib/hooks/post-rendering-hook.d.ts +1 -1
  15. package/src/lib/hooks/post-rendering-hook.js +10 -6
  16. package/src/lib/hooks/post-rendering-hook.js.map +1 -1
  17. package/src/lib/options.d.ts +114 -106
  18. package/src/lib/plugins/dev-server-plugin.d.ts +3 -3
  19. package/src/lib/plugins/dev-server-plugin.js +91 -99
  20. package/src/lib/plugins/dev-server-plugin.js.map +1 -1
  21. package/src/lib/plugins/page-endpoints.d.ts +5 -5
  22. package/src/lib/plugins/page-endpoints.js +39 -51
  23. package/src/lib/plugins/page-endpoints.js.map +1 -1
  24. package/src/lib/utils/get-content-files.d.ts +54 -54
  25. package/src/lib/utils/get-content-files.js +88 -100
  26. package/src/lib/utils/get-content-files.js.map +1 -1
  27. package/src/lib/utils/get-page-handlers.d.ts +58 -58
  28. package/src/lib/utils/get-page-handlers.js +70 -82
  29. package/src/lib/utils/get-page-handlers.js.map +1 -1
  30. package/src/lib/utils/load-esm.d.ts +18 -18
  31. package/src/lib/utils/node-web-bridge.d.ts +3 -0
  32. package/src/lib/utils/node-web-bridge.js +55 -0
  33. package/src/lib/utils/node-web-bridge.js.map +1 -0
  34. package/src/lib/utils/register-dev-middleware.d.ts +12 -40
  35. package/src/lib/utils/register-dev-middleware.js +41 -65
  36. package/src/lib/utils/register-dev-middleware.js.map +1 -1
  37. package/src/lib/utils/renderers.d.ts +53 -3
  38. package/src/lib/utils/renderers.js +112 -27
  39. package/src/lib/utils/renderers.js.map +1 -1
  40. package/src/lib/utils/rolldown.d.ts +2 -0
  41. package/src/lib/utils/rolldown.js +12 -0
  42. package/src/lib/utils/rolldown.js.map +1 -0
  43. package/src/lib/vite-plugin-nitro.d.ts +3 -3
  44. package/src/lib/vite-plugin-nitro.js +505 -450
  45. package/src/lib/vite-plugin-nitro.js.map +1 -1
  46. package/README.md +0 -125
  47. package/src/lib/options.js +0 -2
  48. package/src/lib/options.js.map +0 -1
  49. package/src/lib/utils/load-esm.js +0 -23
  50. package/src/lib/utils/load-esm.js.map +0 -1
@@ -1,121 +1,129 @@
1
- import { PrerenderRoute } from 'nitropack';
1
+ import type { PrerenderRoute } from "nitro/types";
2
+ import type { UserConfig } from "vite";
2
3
  export interface Options {
3
- ssr?: boolean;
4
- ssrBuildDir?: string;
5
- /**
6
- * Prerender the static pages without producing the server output.
7
- */
8
- static?: boolean;
9
- prerender?: PrerenderOptions;
10
- entryServer?: string;
11
- index?: string;
12
- /**
13
- * Relative path to source files. Default is 'src'.
14
- */
15
- sourceRoot?: string;
16
- /**
17
- * Absolute path to workspace root. Default is 'process.cwd()'
18
- */
19
- workspaceRoot?: string;
20
- /**
21
- * Additional page paths to include
22
- */
23
- additionalPagesDirs?: string[];
24
- /**
25
- * Additional API paths to include
26
- */
27
- additionalAPIDirs?: string[];
28
- apiPrefix?: string;
29
- /**
30
- * Toggles internal API middleware.
31
- * If disabled, a proxy request is used to route /api
32
- * requests to / in the production server build.
33
- *
34
- * @deprecated
35
- * Use the src/server/routes/api folder
36
- * for API routes.
37
- */
38
- useAPIMiddleware?: boolean;
4
+ ssr?: boolean;
5
+ ssrBuildDir?: string;
6
+ /**
7
+ * Prerender the static pages without producing the server output.
8
+ */
9
+ static?: boolean;
10
+ prerender?: PrerenderOptions;
11
+ entryServer?: string;
12
+ index?: string;
13
+ /**
14
+ * Relative path to source files. Default is 'src'.
15
+ */
16
+ sourceRoot?: string;
17
+ /**
18
+ * Absolute path to workspace root. Default is 'process.cwd()'
19
+ */
20
+ workspaceRoot?: string;
21
+ /**
22
+ * Additional page paths to include
23
+ */
24
+ additionalPagesDirs?: string[];
25
+ /**
26
+ * Additional API paths to include
27
+ */
28
+ additionalAPIDirs?: string[];
29
+ apiPrefix?: string;
30
+ /**
31
+ * Toggles internal API middleware.
32
+ * If disabled, a proxy request is used to route /api
33
+ * requests to / in the production server build.
34
+ *
35
+ * @deprecated
36
+ * Use the src/server/routes/api folder
37
+ * for API routes.
38
+ */
39
+ useAPIMiddleware?: boolean;
40
+ /**
41
+ * Vite-native build passthrough. Rolldown-only options such as
42
+ * `build.rolldownOptions.output.codeSplitting` are forwarded when present.
43
+ */
44
+ vite?: {
45
+ build?: UserConfig["build"];
46
+ };
39
47
  }
40
48
  export interface PrerenderOptions {
41
- /**
42
- * Add additional routes to prerender through crawling page links.
43
- */
44
- discover?: boolean;
45
- /**
46
- * List of routes to prerender resolved statically or dynamically.
47
- */
48
- routes?: (string | PrerenderContentDir | PrerenderRouteConfig)[] | (() => Promise<(string | PrerenderContentDir | PrerenderRouteConfig | undefined)[]>);
49
- sitemap?: SitemapConfig;
50
- /** List of functions that run for each route after pre-rendering is complete. */
51
- postRenderingHooks?: ((routes: PrerenderRoute) => Promise<void>)[];
49
+ /**
50
+ * Add additional routes to prerender through crawling page links.
51
+ */
52
+ discover?: boolean;
53
+ /**
54
+ * List of routes to prerender resolved statically or dynamically.
55
+ */
56
+ routes?: (string | PrerenderContentDir | PrerenderRouteConfig)[] | (() => Promise<(string | PrerenderContentDir | PrerenderRouteConfig | undefined)[]>);
57
+ sitemap?: SitemapConfig;
58
+ /** List of functions that run for each route after pre-rendering is complete. */
59
+ postRenderingHooks?: ((routes: PrerenderRoute) => Promise<void>)[];
52
60
  }
53
61
  export interface SitemapConfig {
54
- host: string;
62
+ host: string;
55
63
  }
56
64
  export interface PrerenderContentDir {
57
- /**
58
- * The directory where files should be grabbed from.
59
- * @example `/src/contents/blog`
60
- */
61
- contentDir: string;
62
- /**
63
- * Transform the matching content files path into a route.
64
- * The function is called for each matching content file within the specified contentDir.
65
- * @param file information of the matching file (`path`, `name`, `extension`, `attributes`, `content`)
66
- * @returns a string with the route should be returned (e. g. `/blog/<slug>`) or the value `false`, when the route should not be prerendered.
67
- */
68
- transform: (file: PrerenderContentFile) => string | false;
69
- /**
70
- * Customize the sitemap definition for the prerendered route
71
- *
72
- * https://www.sitemaps.org/protocol.html#xmlTagDefinitions
73
- */
74
- sitemap?: PrerenderSitemapConfig | ((file: PrerenderContentFile) => PrerenderSitemapConfig);
75
- /**
76
- * Output the source markdown content alongside the prerendered route.
77
- * The source file will be accessible at the route path with a .md extension.
78
- * @param file information of the matching file including its content
79
- * @returns the markdown content string to output, or `false` to skip outputting for this file
80
- */
81
- outputSourceFile?: (file: PrerenderContentFile) => string | false;
65
+ /**
66
+ * The directory where files should be grabbed from.
67
+ * @example `/src/contents/blog`
68
+ */
69
+ contentDir: string;
70
+ /**
71
+ * Transform the matching content files path into a route.
72
+ * The function is called for each matching content file within the specified contentDir.
73
+ * @param file information of the matching file (`path`, `name`, `extension`, `attributes`, `content`)
74
+ * @returns a string with the route should be returned (e. g. `/blog/<slug>`) or the value `false`, when the route should not be prerendered.
75
+ */
76
+ transform: (file: PrerenderContentFile) => string | false;
77
+ /**
78
+ * Customize the sitemap definition for the prerendered route
79
+ *
80
+ * https://www.sitemaps.org/protocol.html#xmlTagDefinitions
81
+ */
82
+ sitemap?: PrerenderSitemapConfig | ((file: PrerenderContentFile) => PrerenderSitemapConfig);
83
+ /**
84
+ * Output the source markdown content alongside the prerendered route.
85
+ * The source file will be accessible at the route path with a .md extension.
86
+ * @param file information of the matching file including its content
87
+ * @returns the markdown content string to output, or `false` to skip outputting for this file
88
+ */
89
+ outputSourceFile?: (file: PrerenderContentFile) => string | false;
82
90
  }
83
91
  /**
84
- * @param path the path to the content file
85
- * @param name the basename of the matching content file without the file extension
86
- * @param extension the file extension
87
- * @param attributes the frontmatter attributes extracted from the frontmatter section of the file
88
- * @param content the raw file content including frontmatter
89
- * @returns a string with the route should be returned (e. g. `/blog/<slug>`) or the value `false`, when the route should not be prerendered.
90
- */
92
+ * @param path the path to the content file
93
+ * @param name the basename of the matching content file without the file extension
94
+ * @param extension the file extension
95
+ * @param attributes the frontmatter attributes extracted from the frontmatter section of the file
96
+ * @param content the raw file content including frontmatter
97
+ * @returns a string with the route should be returned (e. g. `/blog/<slug>`) or the value `false`, when the route should not be prerendered.
98
+ */
91
99
  export interface PrerenderContentFile {
92
- path: string;
93
- attributes: Record<string, any>;
94
- name: string;
95
- extension: string;
96
- content: string;
100
+ path: string;
101
+ attributes: Record<string, any>;
102
+ name: string;
103
+ extension: string;
104
+ content: string;
97
105
  }
98
106
  export interface PrerenderSitemapConfig {
99
- lastmod?: string;
100
- changefreq?: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never';
101
- priority?: string;
107
+ lastmod?: string;
108
+ changefreq?: "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never";
109
+ priority?: string;
102
110
  }
103
111
  export interface PrerenderRouteConfig {
104
- route: string;
105
- /**
106
- * Customize the sitemap definition for the prerendered route
107
- *
108
- * https://www.sitemaps.org/protocol.html#xmlTagDefinitions
109
- */
110
- sitemap?: PrerenderSitemapConfig | (() => PrerenderSitemapConfig);
111
- /**
112
- * Prerender static data for the prerendered route
113
- */
114
- staticData?: boolean;
115
- /**
116
- * Path to the source markdown file to output alongside the prerendered route.
117
- * The source file will be accessible at the route path with a .md extension.
118
- * @example 'src/content/overview.md'
119
- */
120
- outputSourceFile?: string;
112
+ route: string;
113
+ /**
114
+ * Customize the sitemap definition for the prerendered route
115
+ *
116
+ * https://www.sitemaps.org/protocol.html#xmlTagDefinitions
117
+ */
118
+ sitemap?: PrerenderSitemapConfig | (() => PrerenderSitemapConfig);
119
+ /**
120
+ * Prerender static data for the prerendered route
121
+ */
122
+ staticData?: boolean;
123
+ /**
124
+ * Path to the source markdown file to output alongside the prerendered route.
125
+ * The source file will be accessible at the route path with a .md extension.
126
+ * @example 'src/content/overview.md'
127
+ */
128
+ outputSourceFile?: string;
121
129
  }
@@ -1,7 +1,7 @@
1
- import { Plugin } from 'vite';
2
- import { Options } from '../options.js';
1
+ import { Plugin } from "vite";
2
+ import { Options } from "../options.js";
3
3
  type ServerOptions = Options & {
4
- routeRules?: Record<string, any> | undefined;
4
+ routeRules?: Record<string, any> | undefined;
5
5
  };
6
6
  export declare function devServerPlugin(options: ServerOptions): Plugin;
7
7
  export {};
@@ -1,69 +1,59 @@
1
- // SSR dev server, middleware and error page source modified from
2
- // https://github.com/solidjs/solid-start/blob/main/packages/start/dev/server.js
3
- import { normalizePath, } from 'vite';
4
- import { resolve } from 'node:path';
5
- import { readFileSync } from 'node:fs';
6
- import { createEvent, sendWebResponse } from 'h3';
7
- import { createRouter as createRadixRouter, toRouteMatcher } from 'radix3';
8
- import { defu } from 'defu';
9
- import { registerDevServerMiddleware } from '../utils/register-dev-middleware.js';
10
- export function devServerPlugin(options) {
11
- const workspaceRoot = options?.workspaceRoot || process.cwd();
12
- const sourceRoot = options?.sourceRoot ?? 'src';
13
- const index = options.index || 'index.html';
14
- let config;
15
- let root;
16
- let isTest = false;
17
- return {
18
- name: 'analogjs-dev-ssr-plugin',
19
- config(userConfig, { mode }) {
20
- config = userConfig;
21
- root = normalizePath(resolve(workspaceRoot, config.root || '.') || '.');
22
- isTest = isTest ? isTest : mode === 'test';
23
- return {
24
- resolve: {
25
- alias: {
26
- '~analog/entry-server': options.entryServer || `${root}/${sourceRoot}/main.server.ts`,
27
- },
28
- },
29
- };
30
- },
31
- configureServer(viteServer) {
32
- if (isTest) {
33
- return;
34
- }
35
- return async () => {
36
- remove_html_middlewares(viteServer.middlewares);
37
- registerDevServerMiddleware(root, sourceRoot, viteServer);
38
- viteServer.middlewares.use(async (req, res) => {
39
- let template = readFileSync(resolve(viteServer.config.root, index), 'utf-8');
40
- template = await viteServer.transformIndexHtml(req.originalUrl, template);
41
- const _routeRulesMatcher = toRouteMatcher(createRadixRouter({ routes: options.routeRules }));
42
- const _getRouteRules = (path) => defu({}, ..._routeRulesMatcher.matchAll(path).reverse());
43
- try {
44
- let result;
45
- // Check for route rules explicitly disabling SSR
46
- if (_getRouteRules(req.originalUrl).ssr === false) {
47
- result = template;
48
- }
49
- else {
50
- const entryServer = (await viteServer.ssrLoadModule('~analog/entry-server'))['default'];
51
- result = await entryServer(req.originalUrl, template, {
52
- req,
53
- res,
54
- });
55
- }
56
- if (result instanceof Response) {
57
- sendWebResponse(createEvent(req, res), result);
58
- return;
59
- }
60
- res.setHeader('Content-Type', 'text/html');
61
- res.end(result);
62
- }
63
- catch (e) {
64
- viteServer.ssrFixStacktrace(e);
65
- res.statusCode = 500;
66
- res.end(`
1
+ import { writeWebResponseToNode } from "../utils/node-web-bridge.js";
2
+ import { registerDevServerMiddleware } from "../utils/register-dev-middleware.js";
3
+ import { normalizePath } from "vite";
4
+ import { resolve } from "node:path";
5
+ import { readFileSync } from "node:fs";
6
+ import { createRouter, toRouteMatcher } from "radix3";
7
+ import { defu } from "defu";
8
+ //#region packages/vite-plugin-nitro/src/lib/plugins/dev-server-plugin.ts
9
+ function devServerPlugin(options) {
10
+ const workspaceRoot = options?.workspaceRoot || process.cwd();
11
+ const sourceRoot = options?.sourceRoot ?? "src";
12
+ const index = options.index || "index.html";
13
+ let config;
14
+ let root;
15
+ let isTest = false;
16
+ return {
17
+ name: "analogjs-dev-ssr-plugin",
18
+ config(userConfig, { mode }) {
19
+ config = userConfig;
20
+ root = normalizePath(resolve(workspaceRoot, config.root || ".") || ".");
21
+ isTest = isTest ? isTest : mode === "test";
22
+ return {
23
+ appType: "custom",
24
+ resolve: { alias: { "~analog/entry-server": options.entryServer || `${root}/${sourceRoot}/main.server.ts` } }
25
+ };
26
+ },
27
+ configureServer(viteServer) {
28
+ if (isTest) return;
29
+ return async () => {
30
+ remove_html_middlewares(viteServer.middlewares);
31
+ registerDevServerMiddleware(root, sourceRoot, viteServer);
32
+ viteServer.middlewares.use(async (req, res) => {
33
+ let template = readFileSync(resolve(viteServer.config.root, index), "utf-8");
34
+ template = await viteServer.transformIndexHtml(req.originalUrl, template);
35
+ const _routeRulesMatcher = toRouteMatcher(createRouter({ routes: options.routeRules }));
36
+ const _getRouteRules = (path) => defu({}, ..._routeRulesMatcher.matchAll(path).reverse());
37
+ try {
38
+ let result;
39
+ if (_getRouteRules(req.originalUrl).ssr === false) result = template;
40
+ else {
41
+ const entryServer = (await viteServer.ssrLoadModule("~analog/entry-server"))["default"];
42
+ result = await entryServer(req.originalUrl, template, {
43
+ req,
44
+ res
45
+ });
46
+ }
47
+ if (result instanceof Response) {
48
+ await writeWebResponseToNode(res, result);
49
+ return;
50
+ }
51
+ res.setHeader("Content-Type", "text/html");
52
+ res.end(result);
53
+ } catch (e) {
54
+ viteServer.ssrFixStacktrace(e);
55
+ res.statusCode = 500;
56
+ res.end(`
67
57
  <!DOCTYPE html>
68
58
  <html lang="en">
69
59
  <head>
@@ -71,49 +61,51 @@ export function devServerPlugin(options) {
71
61
  <title>Error</title>
72
62
  <script type="module">
73
63
  import { ErrorOverlay } from '/@vite/client'
74
- document.body.appendChild(new ErrorOverlay(${JSON.stringify(prepareError(req, e)).replace(/</g, '\\u003c')}))
75
- </script>
64
+ document.body.appendChild(new ErrorOverlay(${JSON.stringify(prepareError(req, e)).replace(/</g, "\\u003c")}))
65
+ <\/script>
76
66
  </head>
77
67
  <body>
78
68
  </body>
79
69
  </html>
80
70
  `);
81
- }
82
- });
83
- };
84
- },
85
- };
71
+ }
72
+ });
73
+ };
74
+ }
75
+ };
86
76
  }
87
77
  /**
88
- * Removes Vite internal middleware
89
- *
90
- * @param server
91
- */
78
+ * Removes Vite internal middleware
79
+ *
80
+ * @param server
81
+ */
92
82
  function remove_html_middlewares(server) {
93
- const html_middlewares = [
94
- 'viteIndexHtmlMiddleware',
95
- 'vite404Middleware',
96
- 'viteSpaFallbackMiddleware',
97
- ];
98
- for (let i = server.stack.length - 1; i > 0; i--) {
99
- const handler = server.stack[i]?.handle;
100
- const handlerName = typeof handler === 'function' ? handler.name : undefined;
101
- if (handlerName && html_middlewares.includes(handlerName)) {
102
- server.stack.splice(i, 1);
103
- }
104
- }
83
+ const html_middlewares = [
84
+ "viteIndexHtmlMiddleware",
85
+ "vite404Middleware",
86
+ "viteSpaFallbackMiddleware",
87
+ "viteHtmlFallbackMiddleware"
88
+ ];
89
+ for (let i = server.stack.length - 1; i > 0; i--) {
90
+ const handler = server.stack[i]?.handle;
91
+ const handlerName = typeof handler === "function" ? handler.name : void 0;
92
+ if (handlerName && html_middlewares.includes(handlerName)) server.stack.splice(i, 1);
93
+ }
105
94
  }
106
95
  /**
107
- * Formats error for SSR message in error overlay
108
- * @param req
109
- * @param error
110
- * @returns
111
- */
96
+ * Formats error for SSR message in error overlay
97
+ * @param req
98
+ * @param error
99
+ * @returns
100
+ */
112
101
  function prepareError(req, error) {
113
- const e = error;
114
- return {
115
- message: `An error occured while server rendering ${req.url}:\n\n\t${typeof e === 'string' ? e : e.message} `,
116
- stack: typeof e === 'string' ? '' : e.stack,
117
- };
102
+ const e = error;
103
+ return {
104
+ message: `An error occured while server rendering ${req.url}:\n\n\t${typeof e === "string" ? e : e.message} `,
105
+ stack: typeof e === "string" ? "" : e.stack
106
+ };
118
107
  }
108
+ //#endregion
109
+ export { devServerPlugin };
110
+
119
111
  //# sourceMappingURL=dev-server-plugin.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dev-server-plugin.js","sourceRoot":"","sources":["../../../../../../packages/vite-plugin-nitro/src/lib/plugins/dev-server-plugin.ts"],"names":[],"mappings":"AAAA,iEAAiE;AACjE,gFAAgF;AAEhF,OAAO,EAKL,aAAa,GACd,MAAM,MAAM,CAAC;AACd,OAA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SAAS,YAAY,CAAC,GAA4B,EAAE,KAAc;IAChE,MAAM,CAAC,GAAG,KAAc,CAAC;IACzB,OAAO;QACL,OAAO,EAAE,2CAA2C,GAAG,CAAC,GAAG,UACzD,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAChC,GAAG;QACH,KAAK,EAAE,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK;KAC5C,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"dev-server-plugin.js","names":[],"sources":["../../../../../../packages/vite-plugin-nitro/src/lib/plugins/dev-server-plugin.ts"],"sourcesContent":["// SSR dev server, middleware and error page source modified from\n// https://github.com/solidjs/solid-start/blob/main/packages/start/dev/server.js\n\nimport {\n Connect,\n Plugin,\n UserConfig,\n ViteDevServer,\n normalizePath,\n} from 'vite';\nimport { resolve } from 'node:path';\nimport { readFileSync } from 'node:fs';\nimport { createRouter as createRadixRouter, toRouteMatcher } from 'radix3';\nimport { defu } from 'defu';\nimport type { NitroRouteRules } from 'nitro/types';\n\nimport { registerDevServerMiddleware } from '../utils/register-dev-middleware.js';\nimport { writeWebResponseToNode } from '../utils/node-web-bridge.js';\nimport { Options } from '../options.js';\n\ntype ServerOptions = Options & { routeRules?: Record<string, any> | undefined };\n\nexport function devServerPlugin(options: ServerOptions): Plugin {\n const workspaceRoot = options?.workspaceRoot || process.cwd();\n const sourceRoot = options?.sourceRoot ?? 'src';\n const index = options.index || 'index.html';\n let config: UserConfig;\n let root: string;\n let isTest = false;\n\n return {\n name: 'analogjs-dev-ssr-plugin',\n config(userConfig, { mode }) {\n config = userConfig;\n root = normalizePath(resolve(workspaceRoot, config.root || '.') || '.');\n isTest = isTest ? isTest : mode === 'test';\n return {\n appType: 'custom',\n resolve: {\n alias: {\n '~analog/entry-server':\n options.entryServer || `${root}/${sourceRoot}/main.server.ts`,\n },\n },\n };\n },\n configureServer(viteServer) {\n if (isTest) {\n return;\n }\n\n return async () => {\n remove_html_middlewares(viteServer.middlewares);\n registerDevServerMiddleware(root, sourceRoot, viteServer);\n\n viteServer.middlewares.use(async (req, res) => {\n let template = readFileSync(\n resolve(viteServer.config.root, index),\n 'utf-8',\n );\n\n template = await viteServer.transformIndexHtml(\n req.originalUrl as string,\n template,\n );\n\n const _routeRulesMatcher = toRouteMatcher(\n createRadixRouter({ routes: options.routeRules }),\n );\n const _getRouteRules = (path: string) =>\n defu(\n {},\n ..._routeRulesMatcher.matchAll(path).reverse(),\n ) as NitroRouteRules;\n\n try {\n let result: string | Response;\n // Check for route rules explicitly disabling SSR\n if (_getRouteRules(req.originalUrl as string).ssr === false) {\n result = template;\n } else {\n const entryServer = (\n await viteServer.ssrLoadModule('~analog/entry-server')\n )['default'];\n result = await entryServer(req.originalUrl, template, {\n req,\n res,\n });\n }\n\n if (result instanceof Response) {\n await writeWebResponseToNode(res, result);\n return;\n }\n res.setHeader('Content-Type', 'text/html');\n res.end(result);\n } catch (e) {\n viteServer.ssrFixStacktrace(e as Error);\n res.statusCode = 500;\n res.end(`\n <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\" />\n <title>Error</title>\n <script type=\"module\">\n import { ErrorOverlay } from '/@vite/client'\n document.body.appendChild(new ErrorOverlay(${JSON.stringify(\n prepareError(req, e),\n ).replace(/</g, '\\\\u003c')}))\n </script>\n </head>\n <body>\n </body>\n </html>\n `);\n }\n });\n };\n },\n };\n}\n\n/**\n * Removes Vite internal middleware\n *\n * @param server\n */\nfunction remove_html_middlewares(server: ViteDevServer['middlewares']) {\n const html_middlewares = [\n 'viteIndexHtmlMiddleware',\n 'vite404Middleware',\n 'viteSpaFallbackMiddleware',\n 'viteHtmlFallbackMiddleware',\n ];\n for (let i = server.stack.length - 1; i > 0; i--) {\n const handler = server.stack[i]?.handle;\n const handlerName =\n typeof handler === 'function' ? handler.name : undefined;\n if (handlerName && html_middlewares.includes(handlerName)) {\n server.stack.splice(i, 1);\n }\n }\n}\n\n/**\n * Formats error for SSR message in error overlay\n * @param req\n * @param error\n * @returns\n */\nfunction prepareError(req: Connect.IncomingMessage, error: unknown) {\n const e = error as Error;\n return {\n message: `An error occured while server rendering ${req.url}:\\n\\n\\t${\n typeof e === 'string' ? e : e.message\n } `,\n stack: typeof e === 'string' ? '' : e.stack,\n };\n}\n"],"mappings":";;;;;;;;AAsBA,SAAgB,gBAAgB,SAAgC;CAC9D,MAAM,gBAAgB,SAAS,iBAAiB,QAAQ,KAAK;CAC7D,MAAM,aAAa,SAAS,cAAc;CAC1C,MAAM,QAAQ,QAAQ,SAAS;CAC/B,IAAI;CACJ,IAAI;CACJ,IAAI,SAAS;AAEb,QAAO;EACL,MAAM;EACN,OAAO,YAAY,EAAE,QAAQ;AAC3B,YAAS;AACT,UAAO,cAAc,QAAQ,eAAe,OAAO,QAAQ,IAAI,IAAI,IAAI;AACvE,YAAS,SAAS,SAAS,SAAS;AACpC,UAAO;IACL,SAAS;IACT,SAAS,EACP,OAAO,EACL,wBACE,QAAQ,eAAe,GAAG,KAAK,GAAG,WAAW,kBAChD,EACF;IACF;;EAEH,gBAAgB,YAAY;AAC1B,OAAI,OACF;AAGF,UAAO,YAAY;AACjB,4BAAwB,WAAW,YAAY;AAC/C,gCAA4B,MAAM,YAAY,WAAW;AAEzD,eAAW,YAAY,IAAI,OAAO,KAAK,QAAQ;KAC7C,IAAI,WAAW,aACb,QAAQ,WAAW,OAAO,MAAM,MAAM,EACtC,QACD;AAED,gBAAW,MAAM,WAAW,mBAC1B,IAAI,aACJ,SACD;KAED,MAAM,qBAAqB,eACzB,aAAkB,EAAE,QAAQ,QAAQ,YAAY,CAAC,CAClD;KACD,MAAM,kBAAkB,SACtB,KACE,EAAE,EACF,GAAG,mBAAmB,SAAS,KAAK,CAAC,SAAS,CAC/C;AAEH,SAAI;MACF,IAAI;AAEJ,UAAI,eAAe,IAAI,YAAsB,CAAC,QAAQ,MACpD,UAAS;WACJ;OACL,MAAM,eACJ,MAAM,WAAW,cAAc,uBAAuB,EACtD;AACF,gBAAS,MAAM,YAAY,IAAI,aAAa,UAAU;QACpD;QACA;QACD,CAAC;;AAGJ,UAAI,kBAAkB,UAAU;AAC9B,aAAM,uBAAuB,KAAK,OAAO;AACzC;;AAEF,UAAI,UAAU,gBAAgB,YAAY;AAC1C,UAAI,IAAI,OAAO;cACR,GAAG;AACV,iBAAW,iBAAiB,EAAW;AACvC,UAAI,aAAa;AACjB,UAAI,IAAI;;;;;;;;iEAQ6C,KAAK,UAChD,aAAa,KAAK,EAAE,CACrB,CAAC,QAAQ,MAAM,UAAU,CAAC;;;;;;cAMjC;;MAEJ;;;EAGP;;;;;;;AAQH,SAAS,wBAAwB,QAAsC;CACrE,MAAM,mBAAmB;EACvB;EACA;EACA;EACA;EACD;AACD,MAAK,IAAI,IAAI,OAAO,MAAM,SAAS,GAAG,IAAI,GAAG,KAAK;EAChD,MAAM,UAAU,OAAO,MAAM,IAAI;EACjC,MAAM,cACJ,OAAO,YAAY,aAAa,QAAQ,OAAO,KAAA;AACjD,MAAI,eAAe,iBAAiB,SAAS,YAAY,CACvD,QAAO,MAAM,OAAO,GAAG,EAAE;;;;;;;;;AAW/B,SAAS,aAAa,KAA8B,OAAgB;CAClE,MAAM,IAAI;AACV,QAAO;EACL,SAAS,2CAA2C,IAAI,IAAI,SAC1D,OAAO,MAAM,WAAW,IAAI,EAAE,QAC/B;EACD,OAAO,OAAO,MAAM,WAAW,KAAK,EAAE;EACvC"}
@@ -1,7 +1,7 @@
1
1
  export declare function pageEndpointsPlugin(): {
2
- name: string;
3
- transform(_code: string, id: string): Promise<{
4
- code: string;
5
- map: null;
6
- } | undefined>;
2
+ name: string;
3
+ transform(_code: string, id: string): Promise<{
4
+ code: string;
5
+ map: null;
6
+ } | undefined>;
7
7
  };
@@ -1,55 +1,43 @@
1
- import { buildSync } from 'esbuild';
2
- import { normalizePath } from 'vite';
3
- export function pageEndpointsPlugin() {
4
- return {
5
- name: 'analogjs-vite-plugin-nitro-rollup-page-endpoint',
6
- async transform(_code, id) {
7
- if (normalizePath(id).includes('/pages/') && id.endsWith('.server.ts')) {
8
- const compiled = buildSync({
9
- stdin: {
10
- contents: _code,
11
- sourcefile: id,
12
- loader: 'ts',
13
- },
14
- write: false,
15
- metafile: true,
16
- platform: 'neutral',
17
- format: 'esm',
18
- logLevel: 'silent',
19
- });
20
- let fileExports = [];
21
- for (const key in compiled.metafile?.outputs) {
22
- if (compiled.metafile?.outputs[key].entryPoint) {
23
- fileExports = compiled.metafile?.outputs[key].exports;
24
- }
25
- }
26
- const code = `
27
- import { defineEventHandler } from 'h3';
1
+ import { SERVER_FETCH_FACTORY_SNIPPET } from "../utils/renderers.js";
2
+ import { normalizePath } from "vite";
3
+ import { parseSync } from "oxc-parser";
4
+ //#region packages/vite-plugin-nitro/src/lib/plugins/page-endpoints.ts
5
+ function pageEndpointsPlugin() {
6
+ return {
7
+ name: "analogjs-vite-plugin-nitro-rollup-page-endpoint",
8
+ async transform(_code, id) {
9
+ if (normalizePath(id).includes("/pages/") && id.endsWith(".server.ts")) {
10
+ const fileExports = parseSync(id, _code, {
11
+ sourceType: "module",
12
+ lang: "ts"
13
+ }).module.staticExports.flatMap((e) => e.entries.filter((entry) => entry.exportName.name !== null).map((entry) => entry.exportName.name));
14
+ return {
15
+ code: `
16
+ import { defineHandler, fetchWithEvent } from 'nitro/h3';
17
+ import { createFetch } from 'ofetch';
28
18
 
29
- ${fileExports.includes('load')
30
- ? _code
31
- : `
19
+ ${fileExports.includes("load") ? _code : `
32
20
  ${_code}
33
21
  export const load = () => {
34
22
  return {};
35
23
  }`}
36
24
 
37
- ${fileExports.includes('action')
38
- ? ''
39
- : `
25
+ ${fileExports.includes("action") ? "" : `
40
26
  export const action = () => {
41
27
  return {};
42
- }
28
+ }
43
29
  `}
44
30
 
45
- export default defineEventHandler(async(event) => {
31
+ export default defineHandler(async(event) => {
32
+ ${SERVER_FETCH_FACTORY_SNIPPET}
33
+
46
34
  if (event.method === 'GET') {
47
35
  try {
48
36
  return await load({
49
37
  params: event.context.params,
50
- req: event.node.req,
51
- res: event.node.res,
52
- fetch: $fetch,
38
+ req: event.node?.req,
39
+ res: event.node?.res,
40
+ fetch: serverFetch,
53
41
  event
54
42
  });
55
43
  } catch(e) {
@@ -60,25 +48,25 @@ export function pageEndpointsPlugin() {
60
48
  try {
61
49
  return await action({
62
50
  params: event.context.params,
63
- req: event.node.req,
64
- res: event.node.res,
65
- fetch: $fetch,
51
+ req: event.node?.req,
52
+ res: event.node?.res,
53
+ fetch: serverFetch,
66
54
  event
67
55
  });
68
56
  } catch(e) {
69
57
  console.error(\` An error occurred: \${e}\`)
70
58
  throw e;
71
- }
59
+ }
72
60
  }
73
61
  });
74
- `;
75
- return {
76
- code,
77
- map: null,
78
- };
79
- }
80
- return;
81
- },
82
- };
62
+ `,
63
+ map: null
64
+ };
65
+ }
66
+ }
67
+ };
83
68
  }
69
+ //#endregion
70
+ export { pageEndpointsPlugin };
71
+
84
72
  //# sourceMappingURL=page-endpoints.js.map