@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.
- package/package.json +6 -8
- package/src/index.d.ts +9 -9
- package/src/index.js +6 -2
- package/src/index.js.map +1 -1
- package/src/lib/build-server.d.ts +3 -2
- package/src/lib/build-server.js +107 -53
- package/src/lib/build-server.js.map +1 -1
- package/src/lib/build-sitemap.d.ts +6 -6
- package/src/lib/build-sitemap.js +48 -60
- package/src/lib/build-sitemap.js.map +1 -1
- package/src/lib/build-ssr.d.ts +2 -2
- package/src/lib/build-ssr.js +16 -18
- package/src/lib/build-ssr.js.map +1 -1
- package/src/lib/hooks/post-rendering-hook.d.ts +1 -1
- package/src/lib/hooks/post-rendering-hook.js +10 -6
- package/src/lib/hooks/post-rendering-hook.js.map +1 -1
- package/src/lib/options.d.ts +114 -106
- package/src/lib/plugins/dev-server-plugin.d.ts +3 -3
- package/src/lib/plugins/dev-server-plugin.js +91 -99
- package/src/lib/plugins/dev-server-plugin.js.map +1 -1
- package/src/lib/plugins/page-endpoints.d.ts +5 -5
- package/src/lib/plugins/page-endpoints.js +39 -51
- package/src/lib/plugins/page-endpoints.js.map +1 -1
- package/src/lib/utils/get-content-files.d.ts +54 -54
- package/src/lib/utils/get-content-files.js +88 -100
- package/src/lib/utils/get-content-files.js.map +1 -1
- package/src/lib/utils/get-page-handlers.d.ts +58 -58
- package/src/lib/utils/get-page-handlers.js +70 -82
- package/src/lib/utils/get-page-handlers.js.map +1 -1
- package/src/lib/utils/load-esm.d.ts +18 -18
- package/src/lib/utils/node-web-bridge.d.ts +3 -0
- package/src/lib/utils/node-web-bridge.js +55 -0
- package/src/lib/utils/node-web-bridge.js.map +1 -0
- package/src/lib/utils/register-dev-middleware.d.ts +12 -40
- package/src/lib/utils/register-dev-middleware.js +41 -65
- package/src/lib/utils/register-dev-middleware.js.map +1 -1
- package/src/lib/utils/renderers.d.ts +53 -3
- package/src/lib/utils/renderers.js +112 -27
- package/src/lib/utils/renderers.js.map +1 -1
- package/src/lib/utils/rolldown.d.ts +2 -0
- package/src/lib/utils/rolldown.js +12 -0
- package/src/lib/utils/rolldown.js.map +1 -0
- package/src/lib/vite-plugin-nitro.d.ts +3 -3
- package/src/lib/vite-plugin-nitro.js +505 -450
- package/src/lib/vite-plugin-nitro.js.map +1 -1
- package/README.md +0 -125
- package/src/lib/options.js +0 -2
- package/src/lib/options.js.map +0 -1
- package/src/lib/utils/load-esm.js +0 -23
- package/src/lib/utils/load-esm.js.map +0 -1
package/src/lib/options.d.ts
CHANGED
|
@@ -1,121 +1,129 @@
|
|
|
1
|
-
import { PrerenderRoute } from
|
|
1
|
+
import type { PrerenderRoute } from "nitro/types";
|
|
2
|
+
import type { UserConfig } from "vite";
|
|
2
3
|
export interface Options {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
62
|
+
host: string;
|
|
55
63
|
}
|
|
56
64
|
export interface PrerenderContentDir {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
107
|
+
lastmod?: string;
|
|
108
|
+
changefreq?: "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never";
|
|
109
|
+
priority?: string;
|
|
102
110
|
}
|
|
103
111
|
export interface PrerenderRouteConfig {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
|
2
|
-
import { Options } from
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
import { Options } from "../options.js";
|
|
3
3
|
type ServerOptions = Options & {
|
|
4
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
import { normalizePath
|
|
4
|
-
import { resolve } from
|
|
5
|
-
import { readFileSync } from
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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,
|
|
75
|
-
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
78
|
+
* Removes Vite internal middleware
|
|
79
|
+
*
|
|
80
|
+
* @param server
|
|
81
|
+
*/
|
|
92
82
|
function remove_html_middlewares(server) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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","
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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 {
|
|
2
|
-
import { normalizePath } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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(
|
|
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(
|
|
38
|
-
? ''
|
|
39
|
-
: `
|
|
25
|
+
${fileExports.includes("action") ? "" : `
|
|
40
26
|
export const action = () => {
|
|
41
27
|
return {};
|
|
42
|
-
}
|
|
28
|
+
}
|
|
43
29
|
`}
|
|
44
30
|
|
|
45
|
-
export default
|
|
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
|
|
51
|
-
res: event.node
|
|
52
|
-
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
|
|
64
|
-
res: event.node
|
|
65
|
-
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|