@analogjs/vite-plugin-nitro 3.0.0-alpha.4 → 3.0.0-alpha.41
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 +26 -11
- package/src/index.d.ts +11 -9
- package/src/index.js +7 -2
- package/src/index.js.map +1 -1
- package/src/lib/build-server.d.ts +2 -2
- package/src/lib/build-server.js +45 -149
- package/src/lib/build-server.js.map +1 -1
- package/src/lib/build-sitemap.d.ts +23 -9
- package/src/lib/build-sitemap.js +132 -63
- package/src/lib/build-sitemap.js.map +1 -1
- package/src/lib/build-ssr.d.ts +3 -2
- package/src/lib/build-ssr.js +26 -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 +143 -106
- package/src/lib/plugins/dev-server-plugin.d.ts +3 -3
- package/src/lib/plugins/dev-server-plugin.js +98 -101
- 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 +26 -57
- package/src/lib/plugins/page-endpoints.js.map +1 -1
- package/src/lib/utils/debug.d.ts +5 -0
- package/src/lib/utils/debug.js +15 -0
- package/src/lib/utils/debug.js.map +1 -0
- package/src/lib/utils/get-content-files.d.ts +54 -54
- package/src/lib/utils/get-content-files.js +88 -97
- 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 -84
- package/src/lib/utils/get-page-handlers.js.map +1 -1
- package/src/lib/utils/i18n-prerender.d.ts +36 -0
- package/src/lib/utils/i18n-prerender.js +23 -0
- package/src/lib/utils/i18n-prerender.js.map +1 -0
- package/src/lib/utils/load-esm.d.ts +18 -18
- package/src/lib/utils/node-web-bridge.d.ts +1 -1
- package/src/lib/utils/node-web-bridge.js +50 -45
- package/src/lib/utils/node-web-bridge.js.map +1 -1
- package/src/lib/utils/register-dev-middleware.d.ts +12 -12
- package/src/lib/utils/register-dev-middleware.js +41 -47
- package/src/lib/utils/register-dev-middleware.js.map +1 -1
- package/src/lib/utils/register-i18n-watcher.d.ts +15 -0
- package/src/lib/utils/renderers.d.ts +47 -47
- package/src/lib/utils/renderers.js +57 -56
- 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 +790 -677
- 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/build-ssr.js
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import { relative, resolve } from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
resolve(workspaceRoot, rootDir, `${sourceRoot}/main.server.ts`),
|
|
14
|
-
},
|
|
15
|
-
outDir: options?.ssrBuildDir || resolve(workspaceRoot, 'dist', rootDir, 'ssr'),
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
await build(ssrBuildConfig);
|
|
1
|
+
import { getBundleOptionsKey } from "./utils/rolldown.js";
|
|
2
|
+
import { build, mergeConfig } from "vite";
|
|
3
|
+
import { relative, resolve } from "node:path";
|
|
4
|
+
//#region packages/vite-plugin-nitro/src/lib/build-ssr.ts
|
|
5
|
+
async function buildClientApp(config, options) {
|
|
6
|
+
const workspaceRoot = options?.workspaceRoot ?? process.cwd();
|
|
7
|
+
const rootDir = relative(workspaceRoot, config.root || ".") || ".";
|
|
8
|
+
await build(mergeConfig(config, { build: {
|
|
9
|
+
ssr: false,
|
|
10
|
+
outDir: config.build?.outDir || resolve(workspaceRoot, "dist", rootDir, "client"),
|
|
11
|
+
emptyOutDir: true
|
|
12
|
+
} }));
|
|
19
13
|
}
|
|
14
|
+
async function buildSSRApp(config, options) {
|
|
15
|
+
const workspaceRoot = options?.workspaceRoot ?? process.cwd();
|
|
16
|
+
const sourceRoot = options?.sourceRoot ?? "src";
|
|
17
|
+
const rootDir = relative(workspaceRoot, config.root || ".") || ".";
|
|
18
|
+
await build(mergeConfig(config, { build: {
|
|
19
|
+
ssr: true,
|
|
20
|
+
[getBundleOptionsKey()]: { input: options?.entryServer || resolve(workspaceRoot, rootDir, `${sourceRoot}/main.server.ts`) },
|
|
21
|
+
outDir: options?.ssrBuildDir || resolve(workspaceRoot, "dist", rootDir, "ssr"),
|
|
22
|
+
emptyOutDir: false
|
|
23
|
+
} }));
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { buildClientApp, buildSSRApp };
|
|
27
|
+
|
|
20
28
|
//# sourceMappingURL=build-ssr.js.map
|
package/src/lib/build-ssr.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-ssr.js","
|
|
1
|
+
{"version":3,"file":"build-ssr.js","names":[],"sources":["../../../src/lib/build-ssr.ts"],"sourcesContent":["import { build, mergeConfig, UserConfig } from 'vite';\nimport { relative, resolve } from 'node:path';\n\nimport { Options } from './options.js';\nimport { getBundleOptionsKey } from './utils/rolldown.js';\n\nexport async function buildClientApp(\n config: UserConfig,\n options?: Options,\n): Promise<void> {\n const workspaceRoot = options?.workspaceRoot ?? process.cwd();\n const rootDir = relative(workspaceRoot, config.root || '.') || '.';\n const clientBuildConfig = mergeConfig(config, <UserConfig>{\n build: {\n ssr: false,\n outDir:\n config.build?.outDir ||\n resolve(workspaceRoot, 'dist', rootDir, 'client'),\n emptyOutDir: true,\n },\n });\n\n await build(clientBuildConfig);\n}\n\nexport async function buildSSRApp(\n config: UserConfig,\n options?: Options,\n): Promise<void> {\n const workspaceRoot = options?.workspaceRoot ?? process.cwd();\n const sourceRoot = options?.sourceRoot ?? 'src';\n const rootDir = relative(workspaceRoot, config.root || '.') || '.';\n const bundleOptionsKey = getBundleOptionsKey();\n\n /**\n * SSR is built as a second pass from the already prepared Analog/Vite config.\n *\n * That means we intentionally start from the same base config used for the\n * client build and then merge only the SSR-specific overrides (entry, outDir,\n * `build.ssr`, etc).\n *\n * A side effect of this design is that the resolved SSR config can expose the\n * same high-level Analog plugin chain more than once when Vite/Nitro replays\n * shared plugins for the server environment. In particular,\n * `@analogjs/vite-plugin-angular` may appear twice in `config.plugins` during\n * SSR resolution:\n * - once from the normal Analog platform plugin expansion\n * - once from the reused/shared plugin graph for the SSR pass\n *\n * That does NOT imply the client build has two competing style registries.\n * The client-side duplicate-registration guard in `vite-plugin-angular`\n * therefore explicitly ignores `build.ssr === true` to avoid treating this\n * valid SSR orchestration detail as a style-map bug.\n */\n const ssrBuildConfig = mergeConfig(config, <UserConfig>{\n build: {\n ssr: true,\n [bundleOptionsKey]: {\n input:\n options?.entryServer ||\n resolve(workspaceRoot, rootDir, `${sourceRoot}/main.server.ts`),\n },\n outDir:\n options?.ssrBuildDir || resolve(workspaceRoot, 'dist', rootDir, 'ssr'),\n // Preserve the client build output. The client pass already handled its\n // own cleanup, and on Windows this nested SSR build can otherwise remove\n // sibling artifacts that Nitro needs to read immediately afterward.\n emptyOutDir: false,\n },\n });\n\n await build(ssrBuildConfig);\n}\n"],"mappings":";;;;AAMA,eAAsB,eACpB,QACA,SACe;CACf,MAAM,gBAAgB,SAAS,iBAAiB,QAAQ,KAAK;CAC7D,MAAM,UAAU,SAAS,eAAe,OAAO,QAAQ,IAAI,IAAI;AAW/D,OAAM,MAVoB,YAAY,QAAoB,EACxD,OAAO;EACL,KAAK;EACL,QACE,OAAO,OAAO,UACd,QAAQ,eAAe,QAAQ,SAAS,SAAS;EACnD,aAAa;EACd,EACF,CAAC,CAE4B;;AAGhC,eAAsB,YACpB,QACA,SACe;CACf,MAAM,gBAAgB,SAAS,iBAAiB,QAAQ,KAAK;CAC7D,MAAM,aAAa,SAAS,cAAc;CAC1C,MAAM,UAAU,SAAS,eAAe,OAAO,QAAQ,IAAI,IAAI;AAwC/D,OAAM,MAjBiB,YAAY,QAAoB,EACrD,OAAO;EACL,KAAK;GAxBgB,qBAAqB,GAyBtB,EAClB,OACE,SAAS,eACT,QAAQ,eAAe,SAAS,GAAG,WAAW,iBAAiB,EAClE;EACD,QACE,SAAS,eAAe,QAAQ,eAAe,QAAQ,SAAS,MAAM;EAIxE,aAAa;EACd,EACF,CAAC,CAEyB"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Nitro, PrerenderRoute } from
|
|
1
|
+
import type { Nitro, PrerenderRoute } from "nitro/types";
|
|
2
2
|
export declare function addPostRenderingHooks(nitro: Nitro, hooks: ((pr: PrerenderRoute) => Promise<void>)[]): void;
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
//#region packages/vite-plugin-nitro/src/lib/hooks/post-rendering-hook.ts
|
|
2
|
+
function addPostRenderingHooks(nitro, hooks) {
|
|
3
|
+
hooks.forEach((hook) => {
|
|
4
|
+
nitro.hooks.hook("prerender:generate", (route) => {
|
|
5
|
+
hook(route);
|
|
6
|
+
});
|
|
7
|
+
});
|
|
7
8
|
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { addPostRenderingHooks };
|
|
11
|
+
|
|
8
12
|
//# sourceMappingURL=post-rendering-hook.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"post-rendering-hook.js","
|
|
1
|
+
{"version":3,"file":"post-rendering-hook.js","names":[],"sources":["../../../../src/lib/hooks/post-rendering-hook.ts"],"sourcesContent":["import type { Nitro, PrerenderRoute } from 'nitro/types';\n\nexport function addPostRenderingHooks(\n nitro: Nitro,\n hooks: ((pr: PrerenderRoute) => Promise<void>)[],\n): void {\n hooks.forEach((hook: (preRoute: PrerenderRoute) => void) => {\n nitro.hooks.hook('prerender:generate', (route: PrerenderRoute) => {\n hook(route);\n });\n });\n}\n"],"mappings":";AAEA,SAAgB,sBACd,OACA,OACM;AACN,OAAM,SAAS,SAA6C;AAC1D,QAAM,MAAM,KAAK,uBAAuB,UAA0B;AAChE,QAAK,MAAM;IACX;GACF"}
|
package/src/lib/options.d.ts
CHANGED
|
@@ -1,121 +1,158 @@
|
|
|
1
|
-
import type { PrerenderRoute } from
|
|
1
|
+
import type { PrerenderRoute } from "nitro/types";
|
|
2
|
+
import type { UserConfig } from "vite";
|
|
3
|
+
export interface I18nPrerenderOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The default/source locale for the application.
|
|
6
|
+
*/
|
|
7
|
+
defaultLocale: string;
|
|
8
|
+
/**
|
|
9
|
+
* List of supported locale identifiers.
|
|
10
|
+
* Each route will be prerendered once per locale with a locale prefix.
|
|
11
|
+
*/
|
|
12
|
+
locales: string[];
|
|
13
|
+
}
|
|
2
14
|
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
|
-
|
|
15
|
+
ssr?: boolean;
|
|
16
|
+
ssrBuildDir?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Prerender the static pages without producing the server output.
|
|
19
|
+
*/
|
|
20
|
+
static?: boolean;
|
|
21
|
+
prerender?: PrerenderOptions;
|
|
22
|
+
entryServer?: string;
|
|
23
|
+
index?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Relative path to source files. Default is 'src'.
|
|
26
|
+
*/
|
|
27
|
+
sourceRoot?: string;
|
|
28
|
+
/**
|
|
29
|
+
* Absolute path to workspace root. Default is 'process.cwd()'
|
|
30
|
+
*/
|
|
31
|
+
workspaceRoot?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Additional page paths to include
|
|
34
|
+
*/
|
|
35
|
+
additionalPagesDirs?: string[];
|
|
36
|
+
/**
|
|
37
|
+
* Additional API paths to include
|
|
38
|
+
*/
|
|
39
|
+
additionalAPIDirs?: string[];
|
|
40
|
+
apiPrefix?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Toggles internal API middleware.
|
|
43
|
+
* If disabled, a proxy request is used to route /api
|
|
44
|
+
* requests to / in the production server build.
|
|
45
|
+
*
|
|
46
|
+
* @deprecated
|
|
47
|
+
* Use the src/server/routes/api folder
|
|
48
|
+
* for API routes.
|
|
49
|
+
*/
|
|
50
|
+
useAPIMiddleware?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Vite-native build passthrough. Rolldown-only options such as
|
|
53
|
+
* `build.rolldownOptions.output.codeSplitting` are forwarded when present.
|
|
54
|
+
*/
|
|
55
|
+
vite?: {
|
|
56
|
+
build?: UserConfig["build"];
|
|
57
|
+
};
|
|
39
58
|
}
|
|
40
59
|
export interface PrerenderOptions {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Add additional routes to prerender through crawling page links.
|
|
62
|
+
*/
|
|
63
|
+
discover?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* List of routes to prerender resolved statically or dynamically.
|
|
66
|
+
*/
|
|
67
|
+
routes?: (string | PrerenderContentDir | PrerenderRouteConfig)[] | (() => Promise<(string | PrerenderContentDir | PrerenderRouteConfig | undefined)[]>);
|
|
68
|
+
sitemap?: SitemapConfig;
|
|
69
|
+
/** List of functions that run for each route after pre-rendering is complete. */
|
|
70
|
+
postRenderingHooks?: ((routes: PrerenderRoute) => Promise<void>)[];
|
|
71
|
+
}
|
|
72
|
+
export type SitemapPriority = number | `${number}`;
|
|
73
|
+
export interface SitemapRouteDefinition {
|
|
74
|
+
route: string;
|
|
75
|
+
lastmod?: string;
|
|
76
|
+
changefreq?: "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never";
|
|
77
|
+
priority?: SitemapPriority;
|
|
78
|
+
}
|
|
79
|
+
export interface SitemapEntry extends SitemapRouteDefinition {
|
|
80
|
+
loc: string;
|
|
52
81
|
}
|
|
82
|
+
export type SitemapRouteInput = string | SitemapRouteDefinition | undefined;
|
|
83
|
+
export type SitemapRouteSource = SitemapRouteInput[] | (() => Promise<SitemapRouteInput[]>);
|
|
84
|
+
export type SitemapExcludeRule = string | RegExp | ((entry: SitemapEntry) => boolean | Promise<boolean>);
|
|
85
|
+
export type SitemapTransform = (entry: SitemapEntry) => SitemapRouteDefinition | false | Promise<SitemapRouteDefinition | false>;
|
|
53
86
|
export interface SitemapConfig {
|
|
54
|
-
|
|
87
|
+
host: string;
|
|
88
|
+
include?: SitemapRouteSource;
|
|
89
|
+
exclude?: SitemapExcludeRule[];
|
|
90
|
+
defaults?: PrerenderSitemapConfig;
|
|
91
|
+
transform?: SitemapTransform;
|
|
55
92
|
}
|
|
56
93
|
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
|
-
|
|
94
|
+
/**
|
|
95
|
+
* The directory where files should be grabbed from.
|
|
96
|
+
* @example `/src/contents/blog`
|
|
97
|
+
*/
|
|
98
|
+
contentDir: string;
|
|
99
|
+
/**
|
|
100
|
+
* Transform the matching content files path into a route.
|
|
101
|
+
* The function is called for each matching content file within the specified contentDir.
|
|
102
|
+
* @param file information of the matching file (`path`, `name`, `extension`, `attributes`, `content`)
|
|
103
|
+
* @returns a string with the route should be returned (e. g. `/blog/<slug>`) or the value `false`, when the route should not be prerendered.
|
|
104
|
+
*/
|
|
105
|
+
transform: (file: PrerenderContentFile) => string | false;
|
|
106
|
+
/**
|
|
107
|
+
* Customize the sitemap definition for the prerendered route
|
|
108
|
+
*
|
|
109
|
+
* https://www.sitemaps.org/protocol.html#xmlTagDefinitions
|
|
110
|
+
*/
|
|
111
|
+
sitemap?: PrerenderSitemapConfig | ((file: PrerenderContentFile) => PrerenderSitemapConfig);
|
|
112
|
+
/**
|
|
113
|
+
* Output the source markdown content alongside the prerendered route.
|
|
114
|
+
* The source file will be accessible at the route path with a .md extension.
|
|
115
|
+
* @param file information of the matching file including its content
|
|
116
|
+
* @returns the markdown content string to output, or `false` to skip outputting for this file
|
|
117
|
+
*/
|
|
118
|
+
outputSourceFile?: (file: PrerenderContentFile) => string | false;
|
|
82
119
|
}
|
|
83
120
|
/**
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
121
|
+
* @param path the path to the content file
|
|
122
|
+
* @param name the basename of the matching content file without the file extension
|
|
123
|
+
* @param extension the file extension
|
|
124
|
+
* @param attributes the frontmatter attributes extracted from the frontmatter section of the file
|
|
125
|
+
* @param content the raw file content including frontmatter
|
|
126
|
+
* @returns a string with the route should be returned (e. g. `/blog/<slug>`) or the value `false`, when the route should not be prerendered.
|
|
127
|
+
*/
|
|
91
128
|
export interface PrerenderContentFile {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
129
|
+
path: string;
|
|
130
|
+
attributes: Record<string, any>;
|
|
131
|
+
name: string;
|
|
132
|
+
extension: string;
|
|
133
|
+
content: string;
|
|
97
134
|
}
|
|
98
135
|
export interface PrerenderSitemapConfig {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
136
|
+
lastmod?: string;
|
|
137
|
+
changefreq?: "always" | "hourly" | "daily" | "weekly" | "monthly" | "yearly" | "never";
|
|
138
|
+
priority?: SitemapPriority;
|
|
102
139
|
}
|
|
103
140
|
export interface PrerenderRouteConfig {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
141
|
+
route: string;
|
|
142
|
+
/**
|
|
143
|
+
* Customize the sitemap definition for the prerendered route
|
|
144
|
+
*
|
|
145
|
+
* https://www.sitemaps.org/protocol.html#xmlTagDefinitions
|
|
146
|
+
*/
|
|
147
|
+
sitemap?: PrerenderSitemapConfig | (() => PrerenderSitemapConfig);
|
|
148
|
+
/**
|
|
149
|
+
* Prerender static data for the prerendered route
|
|
150
|
+
*/
|
|
151
|
+
staticData?: boolean;
|
|
152
|
+
/**
|
|
153
|
+
* Path to the source markdown file to output alongside the prerendered route.
|
|
154
|
+
* The source file will be accessible at the route path with a .md extension.
|
|
155
|
+
* @example 'src/content/overview.md'
|
|
156
|
+
*/
|
|
157
|
+
outputSourceFile?: string;
|
|
121
158
|
}
|
|
@@ -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,70 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
catch (e) {
|
|
65
|
-
viteServer.ssrFixStacktrace(e);
|
|
66
|
-
res.statusCode = 500;
|
|
67
|
-
res.end(`
|
|
1
|
+
import { writeWebResponseToNode } from "../utils/node-web-bridge.js";
|
|
2
|
+
import { registerDevServerMiddleware } from "../utils/register-dev-middleware.js";
|
|
3
|
+
import { detectLocaleFromRoute, setHtmlLang } from "../utils/i18n-prerender.js";
|
|
4
|
+
import { normalizePath } from "vite";
|
|
5
|
+
import { resolve } from "node:path";
|
|
6
|
+
import { readFileSync } from "node:fs";
|
|
7
|
+
import { createRouter, toRouteMatcher } from "radix3";
|
|
8
|
+
import { defu } from "defu";
|
|
9
|
+
//#region packages/vite-plugin-nitro/src/lib/plugins/dev-server-plugin.ts
|
|
10
|
+
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
|
+
appType: "custom",
|
|
25
|
+
resolve: { alias: { "~analog/entry-server": options.entryServer || `${root}/${sourceRoot}/main.server.ts` } }
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
configureServer(viteServer) {
|
|
29
|
+
if (isTest) return;
|
|
30
|
+
return async () => {
|
|
31
|
+
remove_html_middlewares(viteServer.middlewares);
|
|
32
|
+
registerDevServerMiddleware(root, sourceRoot, viteServer);
|
|
33
|
+
if (options.i18n) registerI18nWatcher(viteServer);
|
|
34
|
+
viteServer.middlewares.use(async (req, res) => {
|
|
35
|
+
let template = readFileSync(resolve(viteServer.config.root, index), "utf-8");
|
|
36
|
+
template = await viteServer.transformIndexHtml(req.originalUrl, template);
|
|
37
|
+
const _routeRulesMatcher = toRouteMatcher(createRouter({ routes: options.routeRules }));
|
|
38
|
+
const _getRouteRules = (path) => defu({}, ..._routeRulesMatcher.matchAll(path).reverse());
|
|
39
|
+
try {
|
|
40
|
+
let result;
|
|
41
|
+
if (_getRouteRules(req.originalUrl).ssr === false) result = template;
|
|
42
|
+
else {
|
|
43
|
+
const entryServer = (await viteServer.ssrLoadModule("~analog/entry-server"))["default"];
|
|
44
|
+
result = await entryServer(req.originalUrl, template, {
|
|
45
|
+
req,
|
|
46
|
+
res
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (result instanceof Response) {
|
|
50
|
+
await writeWebResponseToNode(res, result);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
let html = typeof result === "string" ? result : template;
|
|
54
|
+
if (options.i18n) {
|
|
55
|
+
const locale = detectLocaleFromRoute(req.originalUrl, options.i18n);
|
|
56
|
+
html = setHtmlLang(html, locale);
|
|
57
|
+
}
|
|
58
|
+
res.setHeader("Content-Type", "text/html");
|
|
59
|
+
res.end(html);
|
|
60
|
+
} catch (e) {
|
|
61
|
+
viteServer.ssrFixStacktrace(e);
|
|
62
|
+
res.statusCode = 500;
|
|
63
|
+
res.end(`
|
|
68
64
|
<!DOCTYPE html>
|
|
69
65
|
<html lang="en">
|
|
70
66
|
<head>
|
|
@@ -72,50 +68,51 @@ export function devServerPlugin(options) {
|
|
|
72
68
|
<title>Error</title>
|
|
73
69
|
<script type="module">
|
|
74
70
|
import { ErrorOverlay } from '/@vite/client'
|
|
75
|
-
document.body.appendChild(new ErrorOverlay(${JSON.stringify(prepareError(req, e)).replace(/</g,
|
|
76
|
-
|
|
71
|
+
document.body.appendChild(new ErrorOverlay(${JSON.stringify(prepareError(req, e)).replace(/</g, "\\u003c")}))
|
|
72
|
+
<\/script>
|
|
77
73
|
</head>
|
|
78
74
|
<body>
|
|
79
75
|
</body>
|
|
80
76
|
</html>
|
|
81
77
|
`);
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
};
|
|
87
83
|
}
|
|
88
84
|
/**
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
85
|
+
* Removes Vite internal middleware
|
|
86
|
+
*
|
|
87
|
+
* @param server
|
|
88
|
+
*/
|
|
93
89
|
function remove_html_middlewares(server) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
}
|
|
90
|
+
const html_middlewares = [
|
|
91
|
+
"viteIndexHtmlMiddleware",
|
|
92
|
+
"vite404Middleware",
|
|
93
|
+
"viteSpaFallbackMiddleware",
|
|
94
|
+
"viteHtmlFallbackMiddleware"
|
|
95
|
+
];
|
|
96
|
+
for (let i = server.stack.length - 1; i > 0; i--) {
|
|
97
|
+
const handler = server.stack[i]?.handle;
|
|
98
|
+
const handlerName = typeof handler === "function" ? handler.name : void 0;
|
|
99
|
+
if (handlerName && html_middlewares.includes(handlerName)) server.stack.splice(i, 1);
|
|
100
|
+
}
|
|
107
101
|
}
|
|
108
102
|
/**
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
103
|
+
* Formats error for SSR message in error overlay
|
|
104
|
+
* @param req
|
|
105
|
+
* @param error
|
|
106
|
+
* @returns
|
|
107
|
+
*/
|
|
114
108
|
function prepareError(req, error) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
109
|
+
const e = error;
|
|
110
|
+
return {
|
|
111
|
+
message: `An error occured while server rendering ${req.url}:\n\n\t${typeof e === "string" ? e : e.message} `,
|
|
112
|
+
stack: typeof e === "string" ? "" : e.stack
|
|
113
|
+
};
|
|
120
114
|
}
|
|
115
|
+
//#endregion
|
|
116
|
+
export { devServerPlugin };
|
|
117
|
+
|
|
121
118
|
//# sourceMappingURL=dev-server-plugin.js.map
|