@analogjs/vite-plugin-nitro 3.0.0-alpha.3 → 3.0.0-alpha.31

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 (57) hide show
  1. package/package.json +27 -11
  2. package/src/index.d.ts +11 -9
  3. package/src/index.js +7 -2
  4. package/src/index.js.map +1 -1
  5. package/src/lib/build-server.d.ts +2 -2
  6. package/src/lib/build-server.js +101 -119
  7. package/src/lib/build-server.js.map +1 -1
  8. package/src/lib/build-sitemap.d.ts +23 -9
  9. package/src/lib/build-sitemap.js +132 -63
  10. package/src/lib/build-sitemap.js.map +1 -1
  11. package/src/lib/build-ssr.d.ts +3 -2
  12. package/src/lib/build-ssr.js +26 -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 +143 -106
  18. package/src/lib/plugins/dev-server-plugin.d.ts +3 -3
  19. package/src/lib/plugins/dev-server-plugin.js +98 -101
  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 +26 -57
  23. package/src/lib/plugins/page-endpoints.js.map +1 -1
  24. package/src/lib/utils/debug.d.ts +5 -0
  25. package/src/lib/utils/debug.js +15 -0
  26. package/src/lib/utils/debug.js.map +1 -0
  27. package/src/lib/utils/get-content-files.d.ts +54 -54
  28. package/src/lib/utils/get-content-files.js +88 -97
  29. package/src/lib/utils/get-content-files.js.map +1 -1
  30. package/src/lib/utils/get-page-handlers.d.ts +58 -58
  31. package/src/lib/utils/get-page-handlers.js +70 -84
  32. package/src/lib/utils/get-page-handlers.js.map +1 -1
  33. package/src/lib/utils/i18n-prerender.d.ts +36 -0
  34. package/src/lib/utils/i18n-prerender.js +23 -0
  35. package/src/lib/utils/i18n-prerender.js.map +1 -0
  36. package/src/lib/utils/load-esm.d.ts +18 -18
  37. package/src/lib/utils/node-web-bridge.d.ts +1 -1
  38. package/src/lib/utils/node-web-bridge.js +50 -45
  39. package/src/lib/utils/node-web-bridge.js.map +1 -1
  40. package/src/lib/utils/register-dev-middleware.d.ts +12 -12
  41. package/src/lib/utils/register-dev-middleware.js +41 -47
  42. package/src/lib/utils/register-dev-middleware.js.map +1 -1
  43. package/src/lib/utils/register-i18n-watcher.d.ts +15 -0
  44. package/src/lib/utils/renderers.d.ts +47 -47
  45. package/src/lib/utils/renderers.js +57 -56
  46. package/src/lib/utils/renderers.js.map +1 -1
  47. package/src/lib/utils/rolldown.d.ts +2 -0
  48. package/src/lib/utils/rolldown.js +12 -0
  49. package/src/lib/utils/rolldown.js.map +1 -0
  50. package/src/lib/vite-plugin-nitro.d.ts +3 -3
  51. package/src/lib/vite-plugin-nitro.js +735 -677
  52. package/src/lib/vite-plugin-nitro.js.map +1 -1
  53. package/README.md +0 -125
  54. package/src/lib/options.js +0 -2
  55. package/src/lib/options.js.map +0 -1
  56. package/src/lib/utils/load-esm.js +0 -23
  57. package/src/lib/utils/load-esm.js.map +0 -1
@@ -1,100 +1,91 @@
1
- import { readFileSync } from 'node:fs';
2
- import { join, relative, resolve } from 'node:path';
3
- import { normalizePath } from 'vite';
4
- import { createRequire } from 'node:module';
5
- import { globSync } from 'tinyglobby';
6
- const require = createRequire(import.meta.url);
1
+ import { normalizePath } from "vite";
2
+ import { join, relative, resolve } from "node:path";
3
+ import { readFileSync } from "node:fs";
4
+ import { globSync } from "tinyglobby";
5
+ import { createRequire } from "node:module";
6
+ //#region packages/vite-plugin-nitro/src/lib/utils/get-content-files.ts
7
+ var require = createRequire(import.meta.url);
7
8
  /**
8
- * Discovers content files with front matter and extracts metadata for prerendering.
9
- *
10
- * This function:
11
- * 1. Discovers all content files matching the specified glob pattern
12
- * 2. Reads each file and parses front matter metadata
13
- * 3. Extracts file name, extension, and path information
14
- * 4. Returns structured data for prerendering content pages
15
- *
16
- * @param workspaceRoot The workspace root directory path
17
- * @param rootDir The project root directory relative to workspace
18
- * @param glob The glob pattern to match content files (e.g., 'content/blog')
19
- * @returns Array of PrerenderContentFile objects with metadata and front matter
20
- *
21
- * Example usage:
22
- * const contentFiles = getMatchingContentFilesWithFrontMatter(
23
- * '/workspace',
24
- * 'apps/my-app',
25
- * 'content/blog'
26
- * );
27
- *
28
- * Sample discovered file paths:
29
- * - /workspace/apps/my-app/content/blog/first-post.md
30
- * - /workspace/apps/my-app/content/blog/2024/01/hello-world.md
31
- * - /workspace/apps/my-app/content/blog/tech/angular-v17.mdx
32
- * - /workspace/apps/my-app/content/blog/about/index.md
33
- *
34
- * Sample output structure:
35
- * {
36
- * name: 'first-post',
37
- * extension: 'md',
38
- * path: 'content/blog',
39
- * attributes: { title: 'My First Post', date: '2024-01-01', tags: ['intro'] }
40
- * }
41
- *
42
- * tinyglobby vs fast-glob comparison:
43
- * - Both support the same glob patterns for file discovery
44
- * - Both are efficient for finding content files
45
- * - tinyglobby is now used instead of fast-glob
46
- * - tinyglobby provides similar functionality with smaller bundle size
47
- * - tinyglobby's globSync returns absolute paths when absolute: true is set
48
- *
49
- * Front matter parsing:
50
- * - Uses front-matter library to parse YAML/TOML front matter
51
- * - Extracts metadata like title, date, tags, author, etc.
52
- * - Supports both YAML (---) and TOML (+++) delimiters
53
- * - Returns structured attributes for prerendering
54
- *
55
- * File path processing:
56
- * - Normalizes paths for cross-platform compatibility
57
- * - Extracts file name without extension
58
- * - Determines file extension for content type handling
59
- * - Maintains relative path structure for routing
60
- */
61
- export function getMatchingContentFilesWithFrontMatter(workspaceRoot, rootDir, glob) {
62
- // Dynamically require front-matter library
63
- // eslint-disable-next-line @typescript-eslint/no-var-requires
64
- const fm = require('front-matter');
65
- // Normalize the project root path for consistent path handling
66
- const root = normalizePath(resolve(workspaceRoot, rootDir));
67
- // Resolve the content directory path relative to the project root
68
- const resolvedDir = normalizePath(relative(root, join(root, glob)));
69
- // Discover all content files in the specified directory
70
- // Pattern: looks for any files in the resolved directory
71
- const contentFiles = globSync([`${root}/${resolvedDir}/*`], {
72
- dot: true,
73
- absolute: true,
74
- });
75
- // Process each discovered content file to extract metadata and front matter
76
- const mappedFilesWithFm = contentFiles.map((f) => {
77
- // Read the file contents as UTF-8 text
78
- const fileContents = readFileSync(f, 'utf8');
79
- // Parse front matter from the file content
80
- const raw = fm(fileContents);
81
- const filepath = normalizePath(f).replace(root, '');
82
- const match = filepath.match(/\/([^/.]+)(\.([^/.]+))?$/);
83
- let name = '';
84
- let extension = '';
85
- if (match) {
86
- name = match[1]; // File name without extension
87
- extension = match[3] || ''; // File extension or empty string if no extension
88
- }
89
- // Return structured content file data for prerendering
90
- return {
91
- name,
92
- extension,
93
- path: resolvedDir,
94
- attributes: raw.attributes,
95
- content: fileContents,
96
- };
97
- });
98
- return mappedFilesWithFm;
9
+ * Discovers content files with front matter and extracts metadata for prerendering.
10
+ *
11
+ * This function:
12
+ * 1. Discovers all content files matching the specified glob pattern
13
+ * 2. Reads each file and parses front matter metadata
14
+ * 3. Extracts file name, extension, and path information
15
+ * 4. Returns structured data for prerendering content pages
16
+ *
17
+ * @param workspaceRoot The workspace root directory path
18
+ * @param rootDir The project root directory relative to workspace
19
+ * @param glob The glob pattern to match content files (e.g., 'content/blog')
20
+ * @returns Array of PrerenderContentFile objects with metadata and front matter
21
+ *
22
+ * Example usage:
23
+ * const contentFiles = getMatchingContentFilesWithFrontMatter(
24
+ * '/workspace',
25
+ * 'apps/my-app',
26
+ * 'content/blog'
27
+ * );
28
+ *
29
+ * Sample discovered file paths:
30
+ * - /workspace/apps/my-app/content/blog/first-post.md
31
+ * - /workspace/apps/my-app/content/blog/2024/01/hello-world.md
32
+ * - /workspace/apps/my-app/content/blog/tech/angular-v17.mdx
33
+ * - /workspace/apps/my-app/content/blog/about/index.md
34
+ *
35
+ * Sample output structure:
36
+ * {
37
+ * name: 'first-post',
38
+ * extension: 'md',
39
+ * path: 'content/blog',
40
+ * attributes: { title: 'My First Post', date: '2024-01-01', tags: ['intro'] }
41
+ * }
42
+ *
43
+ * tinyglobby vs fast-glob comparison:
44
+ * - Both support the same glob patterns for file discovery
45
+ * - Both are efficient for finding content files
46
+ * - tinyglobby is now used instead of fast-glob
47
+ * - tinyglobby provides similar functionality with smaller bundle size
48
+ * - tinyglobby's globSync returns absolute paths when absolute: true is set
49
+ *
50
+ * Front matter parsing:
51
+ * - Uses front-matter library to parse YAML/TOML front matter
52
+ * - Extracts metadata like title, date, tags, author, etc.
53
+ * - Supports both YAML (---) and TOML (+++) delimiters
54
+ * - Returns structured attributes for prerendering
55
+ *
56
+ * File path processing:
57
+ * - Normalizes paths for cross-platform compatibility
58
+ * - Extracts file name without extension
59
+ * - Determines file extension for content type handling
60
+ * - Maintains relative path structure for routing
61
+ */
62
+ function getMatchingContentFilesWithFrontMatter(workspaceRoot, rootDir, glob) {
63
+ const fm = require("front-matter");
64
+ const root = normalizePath(resolve(workspaceRoot, rootDir));
65
+ const resolvedDir = normalizePath(relative(root, join(root, glob)));
66
+ return globSync([`${root}/${resolvedDir}/*`], {
67
+ dot: true,
68
+ absolute: true
69
+ }).map((f) => {
70
+ const fileContents = readFileSync(f, "utf8");
71
+ const raw = fm(fileContents);
72
+ const match = normalizePath(f).replace(root, "").match(/\/([^/.]+)(\.([^/.]+))?$/);
73
+ let name = "";
74
+ let extension = "";
75
+ if (match) {
76
+ name = match[1];
77
+ extension = match[3] || "";
78
+ }
79
+ return {
80
+ name,
81
+ extension,
82
+ path: resolvedDir,
83
+ attributes: raw.attributes,
84
+ content: fileContents
85
+ };
86
+ });
99
87
  }
88
+ //#endregion
89
+ export { getMatchingContentFilesWithFrontMatter };
90
+
100
91
  //# sourceMappingURL=get-content-files.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-content-files.js","sourceRoot":"","sources":["../../../../../../packages/vite-plugin-nitro/src/lib/utils/get-content-files.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAItC,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,MAAM,UAAU,sCAAsC,CACpD,aAAqB,EACrB,OAAe,EACf,IAAY;IAEZ,2CAA2C;IAC3C,8DAA8D;IAC9D,MAAM,EAAE,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IAEnC,+DAA+D;IAC/D,MAAM,IAAI,GAAG,aAAa,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC,CAAC;IAE5D,kEAAkE;IAClE,MAAM,WAAW,GAAG,aAAa,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;IAEpE,wDAAwD;IACxD,yDAAyD;IACzD,MAAM,YAAY,GAAa,QAAQ,CAAC,CAAC,GAAG,IAAI,IAAI,WAAW,IAAI,CAAC,EAAE;QACpE,GAAG,EAAE,IAAI;QACT,QAAQ,EAAE,IAAI;KACf,CAAC,CAAC;IAEH,4EAA4E;IAC5E,MAAM,iBAAiB,GAA2B,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;QACvE,uCAAuC;QACvC,MAAM,YAAY,GAAG,YAAY,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;QAE7C,2CAA2C;QAC3C,MAAM,GAAG,GAAG,EAAE,CAAC,YAAY,CAAC,CAAC;QAE7B,MAAM,QAAQ,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QAEpD,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAC;QACzD,IAAI,IAAI,GAAG,EAAE,CAAC;QACd,IAAI,SAAS,GAAG,EAAE,CAAC;QACnB,IAAI,KAAK,EAAE,CAAC;YACV,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,8BAA8B;YAC/C,SAAS,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,iDAAiD;QAC/E,CAAC;QAED,uDAAuD;QACvD,OAAO;YACL,IAAI;YACJ,SAAS;YACT,IAAI,EAAE,WAAW;YACjB,UAAU,EAAE,GAAG,CAAC,UAAiD;YACjE,OAAO,EAAE,YAAY;SACtB,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,iBAAiB,CAAC;AAC3B,CAAC"}
1
+ {"version":3,"file":"get-content-files.js","names":[],"sources":["../../../../src/lib/utils/get-content-files.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { join, relative, resolve } from 'node:path';\nimport { normalizePath } from 'vite';\nimport { createRequire } from 'node:module';\nimport { globSync } from 'tinyglobby';\n\nimport { PrerenderContentFile } from '../options';\n\nconst require = createRequire(import.meta.url);\n\n/**\n * Discovers content files with front matter and extracts metadata for prerendering.\n *\n * This function:\n * 1. Discovers all content files matching the specified glob pattern\n * 2. Reads each file and parses front matter metadata\n * 3. Extracts file name, extension, and path information\n * 4. Returns structured data for prerendering content pages\n *\n * @param workspaceRoot The workspace root directory path\n * @param rootDir The project root directory relative to workspace\n * @param glob The glob pattern to match content files (e.g., 'content/blog')\n * @returns Array of PrerenderContentFile objects with metadata and front matter\n *\n * Example usage:\n * const contentFiles = getMatchingContentFilesWithFrontMatter(\n * '/workspace',\n * 'apps/my-app',\n * 'content/blog'\n * );\n *\n * Sample discovered file paths:\n * - /workspace/apps/my-app/content/blog/first-post.md\n * - /workspace/apps/my-app/content/blog/2024/01/hello-world.md\n * - /workspace/apps/my-app/content/blog/tech/angular-v17.mdx\n * - /workspace/apps/my-app/content/blog/about/index.md\n *\n * Sample output structure:\n * {\n * name: 'first-post',\n * extension: 'md',\n * path: 'content/blog',\n * attributes: { title: 'My First Post', date: '2024-01-01', tags: ['intro'] }\n * }\n *\n * tinyglobby vs fast-glob comparison:\n * - Both support the same glob patterns for file discovery\n * - Both are efficient for finding content files\n * - tinyglobby is now used instead of fast-glob\n * - tinyglobby provides similar functionality with smaller bundle size\n * - tinyglobby's globSync returns absolute paths when absolute: true is set\n *\n * Front matter parsing:\n * - Uses front-matter library to parse YAML/TOML front matter\n * - Extracts metadata like title, date, tags, author, etc.\n * - Supports both YAML (---) and TOML (+++) delimiters\n * - Returns structured attributes for prerendering\n *\n * File path processing:\n * - Normalizes paths for cross-platform compatibility\n * - Extracts file name without extension\n * - Determines file extension for content type handling\n * - Maintains relative path structure for routing\n */\nexport function getMatchingContentFilesWithFrontMatter(\n workspaceRoot: string,\n rootDir: string,\n glob: string,\n): PrerenderContentFile[] {\n // Dynamically require front-matter library\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const fm = require('front-matter');\n\n // Normalize the project root path for consistent path handling\n const root = normalizePath(resolve(workspaceRoot, rootDir));\n\n // Resolve the content directory path relative to the project root\n const resolvedDir = normalizePath(relative(root, join(root, glob)));\n\n // Discover all content files in the specified directory\n // Pattern: looks for any files in the resolved directory\n const contentFiles: string[] = globSync([`${root}/${resolvedDir}/*`], {\n dot: true,\n absolute: true,\n });\n\n // Process each discovered content file to extract metadata and front matter\n const mappedFilesWithFm: PrerenderContentFile[] = contentFiles.map((f) => {\n // Read the file contents as UTF-8 text\n const fileContents = readFileSync(f, 'utf8');\n\n // Parse front matter from the file content\n const raw = fm(fileContents);\n\n const filepath = normalizePath(f).replace(root, '');\n\n const match = filepath.match(/\\/([^/.]+)(\\.([^/.]+))?$/);\n let name = '';\n let extension = '';\n if (match) {\n name = match[1]; // File name without extension\n extension = match[3] || ''; // File extension or empty string if no extension\n }\n\n // Return structured content file data for prerendering\n return {\n name,\n extension,\n path: resolvedDir,\n attributes: raw.attributes as { attributes: Record<string, any> },\n content: fileContents,\n };\n });\n\n return mappedFilesWithFm;\n}\n"],"mappings":";;;;;;AAQA,IAAM,UAAU,cAAc,OAAO,KAAK,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwD9C,SAAgB,uCACd,eACA,SACA,MACwB;CAGxB,MAAM,KAAK,QAAQ,eAAe;CAGlC,MAAM,OAAO,cAAc,QAAQ,eAAe,QAAQ,CAAC;CAG3D,MAAM,cAAc,cAAc,SAAS,MAAM,KAAK,MAAM,KAAK,CAAC,CAAC;AAqCnE,QAjC+B,SAAS,CAAC,GAAG,KAAK,GAAG,YAAY,IAAI,EAAE;EACpE,KAAK;EACL,UAAU;EACX,CAAC,CAG6D,KAAK,MAAM;EAExE,MAAM,eAAe,aAAa,GAAG,OAAO;EAG5C,MAAM,MAAM,GAAG,aAAa;EAI5B,MAAM,QAFW,cAAc,EAAE,CAAC,QAAQ,MAAM,GAAG,CAE5B,MAAM,2BAA2B;EACxD,IAAI,OAAO;EACX,IAAI,YAAY;AAChB,MAAI,OAAO;AACT,UAAO,MAAM;AACb,eAAY,MAAM,MAAM;;AAI1B,SAAO;GACL;GACA;GACA,MAAM;GACN,YAAY,IAAI;GAChB,SAAS;GACV;GACD"}
@@ -1,62 +1,62 @@
1
- import type { NitroEventHandler } from 'nitro/types';
1
+ import type { NitroEventHandler } from "nitro/types";
2
2
  type GetHandlersArgs = {
3
- workspaceRoot: string;
4
- sourceRoot: string;
5
- rootDir: string;
6
- additionalPagesDirs?: string[];
7
- hasAPIDir?: boolean;
3
+ workspaceRoot: string;
4
+ sourceRoot: string;
5
+ rootDir: string;
6
+ additionalPagesDirs?: string[];
7
+ hasAPIDir?: boolean;
8
8
  };
9
9
  /**
10
- * Discovers and generates Nitro event handlers for server-side page routes.
11
- *
12
- * This function:
13
- * 1. Discovers all .server.ts files in the app/pages directory and additional pages directories
14
- * 2. Converts file paths to route patterns using Angular-style route syntax
15
- * 3. Generates Nitro event handlers with proper route mapping and lazy loading
16
- * 4. Handles dynamic route parameters and catch-all routes
17
- *
18
- * @param workspaceRoot The workspace root directory path
19
- * @param sourceRoot The source directory path (e.g., 'src')
20
- * @param rootDir The project root directory relative to workspace
21
- * @param additionalPagesDirs Optional array of additional pages directories to scan
22
- * @param hasAPIDir Whether the project has an API directory (affects route prefixing)
23
- * @returns Array of NitroEventHandler objects with handler paths and route patterns
24
- *
25
- * Example usage:
26
- * const handlers = getPageHandlers({
27
- * workspaceRoot: '/workspace',
28
- * sourceRoot: 'src',
29
- * rootDir: 'apps/my-app',
30
- * additionalPagesDirs: ['/libs/shared/pages'],
31
- * hasAPIDir: true
32
- * });
33
- *
34
- * Sample discovered file paths:
35
- * - /workspace/apps/my-app/src/app/pages/index.server.ts
36
- * - /workspace/apps/my-app/src/app/pages/users/[id].server.ts
37
- * - /workspace/apps/my-app/src/app/pages/products/[...slug].server.ts
38
- * - /workspace/apps/my-app/src/app/pages/(auth)/login.server.ts
39
- *
40
- * Route transformation examples:
41
- * - index.server.ts → /_analog/pages/index
42
- * - users/[id].server.ts → /_analog/pages/users/:id
43
- * - products/[...slug].server.ts → /_analog/pages/products/**:slug
44
- * - (auth)/login.server.ts → /_analog/pages/-auth-/login
45
- *
46
- * tinyglobby vs fast-glob comparison:
47
- * - Both support the same glob patterns for file discovery
48
- * - Both are efficient for finding server-side page files
49
- * - tinyglobby is now used instead of fast-glob
50
- * - tinyglobby provides similar functionality with smaller bundle size
51
- * - tinyglobby's globSync returns absolute paths when absolute: true is set
52
- *
53
- * Route transformation rules:
54
- * 1. Removes .server.ts extension
55
- * 2. Converts [param] to :param for dynamic routes
56
- * 3. Converts [...param] to **:param for catch-all routes
57
- * 4. Converts (group) to -group- for route groups
58
- * 5. Converts dots to forward slashes
59
- * 6. Prefixes with /_analog/pages and optionally /api
60
- */
61
- export declare function getPageHandlers({ workspaceRoot, sourceRoot, rootDir, additionalPagesDirs, hasAPIDir, }: GetHandlersArgs): NitroEventHandler[];
10
+ * Discovers and generates Nitro event handlers for server-side page routes.
11
+ *
12
+ * This function:
13
+ * 1. Discovers all .server.ts files in the app/pages directory and additional pages directories
14
+ * 2. Converts file paths to route patterns using Angular-style route syntax
15
+ * 3. Generates Nitro event handlers with proper route mapping and lazy loading
16
+ * 4. Handles dynamic route parameters and catch-all routes
17
+ *
18
+ * @param workspaceRoot The workspace root directory path
19
+ * @param sourceRoot The source directory path (e.g., 'src')
20
+ * @param rootDir The project root directory relative to workspace
21
+ * @param additionalPagesDirs Optional array of additional pages directories to scan
22
+ * @param hasAPIDir Whether the project has an API directory (affects route prefixing)
23
+ * @returns Array of NitroEventHandler objects with handler paths and route patterns
24
+ *
25
+ * Example usage:
26
+ * const handlers = getPageHandlers({
27
+ * workspaceRoot: '/workspace',
28
+ * sourceRoot: 'src',
29
+ * rootDir: 'apps/my-app',
30
+ * additionalPagesDirs: ['/libs/shared/pages'],
31
+ * hasAPIDir: true
32
+ * });
33
+ *
34
+ * Sample discovered file paths:
35
+ * - /workspace/apps/my-app/src/app/pages/index.server.ts
36
+ * - /workspace/apps/my-app/src/app/pages/users/[id].server.ts
37
+ * - /workspace/apps/my-app/src/app/pages/products/[...slug].server.ts
38
+ * - /workspace/apps/my-app/src/app/pages/(auth)/login.server.ts
39
+ *
40
+ * Route transformation examples:
41
+ * - index.server.ts → /_analog/pages/index
42
+ * - users/[id].server.ts → /_analog/pages/users/:id
43
+ * - products/[...slug].server.ts → /_analog/pages/products/**:slug
44
+ * - (auth)/login.server.ts → /_analog/pages/-auth-/login
45
+ *
46
+ * tinyglobby vs fast-glob comparison:
47
+ * - Both support the same glob patterns for file discovery
48
+ * - Both are efficient for finding server-side page files
49
+ * - tinyglobby is now used instead of fast-glob
50
+ * - tinyglobby provides similar functionality with smaller bundle size
51
+ * - tinyglobby's globSync returns absolute paths when absolute: true is set
52
+ *
53
+ * Route transformation rules:
54
+ * 1. Removes .server.ts extension
55
+ * 2. Converts [param] to :param for dynamic routes
56
+ * 3. Converts [...param] to **:param for catch-all routes
57
+ * 4. Converts (group) to -group- for route groups
58
+ * 5. Converts dots to forward slashes
59
+ * 6. Prefixes with /_analog/pages and optionally /api
60
+ */
61
+ export declare function getPageHandlers({ workspaceRoot, sourceRoot, rootDir, additionalPagesDirs, hasAPIDir }: GetHandlersArgs): NitroEventHandler[];
62
62
  export {};
@@ -1,87 +1,73 @@
1
- import { resolve } from 'node:path';
2
- import { globSync } from 'tinyglobby';
3
- import { normalizePath } from 'vite';
1
+ import { normalizePath } from "vite";
2
+ import { resolve } from "node:path";
3
+ import { globSync } from "tinyglobby";
4
+ //#region packages/vite-plugin-nitro/src/lib/utils/get-page-handlers.ts
4
5
  /**
5
- * Discovers and generates Nitro event handlers for server-side page routes.
6
- *
7
- * This function:
8
- * 1. Discovers all .server.ts files in the app/pages directory and additional pages directories
9
- * 2. Converts file paths to route patterns using Angular-style route syntax
10
- * 3. Generates Nitro event handlers with proper route mapping and lazy loading
11
- * 4. Handles dynamic route parameters and catch-all routes
12
- *
13
- * @param workspaceRoot The workspace root directory path
14
- * @param sourceRoot The source directory path (e.g., 'src')
15
- * @param rootDir The project root directory relative to workspace
16
- * @param additionalPagesDirs Optional array of additional pages directories to scan
17
- * @param hasAPIDir Whether the project has an API directory (affects route prefixing)
18
- * @returns Array of NitroEventHandler objects with handler paths and route patterns
19
- *
20
- * Example usage:
21
- * const handlers = getPageHandlers({
22
- * workspaceRoot: '/workspace',
23
- * sourceRoot: 'src',
24
- * rootDir: 'apps/my-app',
25
- * additionalPagesDirs: ['/libs/shared/pages'],
26
- * hasAPIDir: true
27
- * });
28
- *
29
- * Sample discovered file paths:
30
- * - /workspace/apps/my-app/src/app/pages/index.server.ts
31
- * - /workspace/apps/my-app/src/app/pages/users/[id].server.ts
32
- * - /workspace/apps/my-app/src/app/pages/products/[...slug].server.ts
33
- * - /workspace/apps/my-app/src/app/pages/(auth)/login.server.ts
34
- *
35
- * Route transformation examples:
36
- * - index.server.ts → /_analog/pages/index
37
- * - users/[id].server.ts → /_analog/pages/users/:id
38
- * - products/[...slug].server.ts → /_analog/pages/products/**:slug
39
- * - (auth)/login.server.ts → /_analog/pages/-auth-/login
40
- *
41
- * tinyglobby vs fast-glob comparison:
42
- * - Both support the same glob patterns for file discovery
43
- * - Both are efficient for finding server-side page files
44
- * - tinyglobby is now used instead of fast-glob
45
- * - tinyglobby provides similar functionality with smaller bundle size
46
- * - tinyglobby's globSync returns absolute paths when absolute: true is set
47
- *
48
- * Route transformation rules:
49
- * 1. Removes .server.ts extension
50
- * 2. Converts [param] to :param for dynamic routes
51
- * 3. Converts [...param] to **:param for catch-all routes
52
- * 4. Converts (group) to -group- for route groups
53
- * 5. Converts dots to forward slashes
54
- * 6. Prefixes with /_analog/pages and optionally /api
55
- */
56
- export function getPageHandlers({ workspaceRoot, sourceRoot, rootDir, additionalPagesDirs, hasAPIDir, }) {
57
- // Normalize the project root path for consistent path handling
58
- const root = normalizePath(resolve(workspaceRoot, rootDir));
59
- // Discover all .server.ts files in the app/pages directory and additional pages directories
60
- // Pattern: looks for any .server.ts files in app/pages/**/*.server.ts and additional directories
61
- const endpointFiles = globSync([
62
- `${root}/${sourceRoot}/app/pages/**/*.server.ts`,
63
- ...(additionalPagesDirs || []).map((dir) => `${workspaceRoot}${dir}/**/*.server.ts`),
64
- ], { dot: true, absolute: true });
65
- // Transform each discovered file into a Nitro event handler
66
- const handlers = endpointFiles.map((endpointFile) => {
67
- // Normalize the endpoint file path for consistent path handling
68
- const normalized = normalizePath(endpointFile);
69
- // Transform the normalized path into a route pattern
70
- const route = normalized
71
- .replace(/^(.*?)\/pages/, '/pages')
72
- .replace(/\.server\.ts$/, '') // Remove .server.ts extension
73
- .replace(/\[\.{3}(.+)\]/g, '**:$1') // Convert [...param] to **:param (catch-all routes)
74
- .replace(/\[\.{3}(\w+)\]/g, '**:$1') // Alternative catch-all pattern
75
- .replace(/\/\((.*?)\)$/, '/-$1-') // Convert (group) to -group- (route groups)
76
- .replace(/\[(\w+)\]/g, ':$1') // Convert [param] to :param (dynamic routes)
77
- .replace(/\./g, '/'); // Convert dots to forward slashes
78
- // Return Nitro event handler with absolute handler path and transformed route
79
- return {
80
- handler: endpointFile,
81
- route: `${hasAPIDir ? '/api' : ''}/_analog${route}`,
82
- lazy: true,
83
- };
84
- });
85
- return handlers;
6
+ * Discovers and generates Nitro event handlers for server-side page routes.
7
+ *
8
+ * This function:
9
+ * 1. Discovers all .server.ts files in the app/pages directory and additional pages directories
10
+ * 2. Converts file paths to route patterns using Angular-style route syntax
11
+ * 3. Generates Nitro event handlers with proper route mapping and lazy loading
12
+ * 4. Handles dynamic route parameters and catch-all routes
13
+ *
14
+ * @param workspaceRoot The workspace root directory path
15
+ * @param sourceRoot The source directory path (e.g., 'src')
16
+ * @param rootDir The project root directory relative to workspace
17
+ * @param additionalPagesDirs Optional array of additional pages directories to scan
18
+ * @param hasAPIDir Whether the project has an API directory (affects route prefixing)
19
+ * @returns Array of NitroEventHandler objects with handler paths and route patterns
20
+ *
21
+ * Example usage:
22
+ * const handlers = getPageHandlers({
23
+ * workspaceRoot: '/workspace',
24
+ * sourceRoot: 'src',
25
+ * rootDir: 'apps/my-app',
26
+ * additionalPagesDirs: ['/libs/shared/pages'],
27
+ * hasAPIDir: true
28
+ * });
29
+ *
30
+ * Sample discovered file paths:
31
+ * - /workspace/apps/my-app/src/app/pages/index.server.ts
32
+ * - /workspace/apps/my-app/src/app/pages/users/[id].server.ts
33
+ * - /workspace/apps/my-app/src/app/pages/products/[...slug].server.ts
34
+ * - /workspace/apps/my-app/src/app/pages/(auth)/login.server.ts
35
+ *
36
+ * Route transformation examples:
37
+ * - index.server.ts → /_analog/pages/index
38
+ * - users/[id].server.ts → /_analog/pages/users/:id
39
+ * - products/[...slug].server.ts → /_analog/pages/products/**:slug
40
+ * - (auth)/login.server.ts → /_analog/pages/-auth-/login
41
+ *
42
+ * tinyglobby vs fast-glob comparison:
43
+ * - Both support the same glob patterns for file discovery
44
+ * - Both are efficient for finding server-side page files
45
+ * - tinyglobby is now used instead of fast-glob
46
+ * - tinyglobby provides similar functionality with smaller bundle size
47
+ * - tinyglobby's globSync returns absolute paths when absolute: true is set
48
+ *
49
+ * Route transformation rules:
50
+ * 1. Removes .server.ts extension
51
+ * 2. Converts [param] to :param for dynamic routes
52
+ * 3. Converts [...param] to **:param for catch-all routes
53
+ * 4. Converts (group) to -group- for route groups
54
+ * 5. Converts dots to forward slashes
55
+ * 6. Prefixes with /_analog/pages and optionally /api
56
+ */
57
+ function getPageHandlers({ workspaceRoot, sourceRoot, rootDir, additionalPagesDirs, hasAPIDir }) {
58
+ return globSync([`${normalizePath(resolve(workspaceRoot, rootDir))}/${sourceRoot}/app/pages/**/*.server.ts`, ...(additionalPagesDirs || []).map((dir) => `${workspaceRoot}${dir}/**/*.server.ts`)], {
59
+ dot: true,
60
+ absolute: true
61
+ }).map((endpointFile) => {
62
+ const route = normalizePath(endpointFile).replace(/^(.*?)\/pages/, "/pages").replace(/\.server\.ts$/, "").replace(/\[\.{3}(.+)\]/g, "**:$1").replace(/\[\.{3}(\w+)\]/g, "**:$1").replace(/\/\((.*?)\)$/, "/-$1-").replace(/\[(\w+)\]/g, ":$1").replace(/\./g, "/");
63
+ return {
64
+ handler: endpointFile,
65
+ route: `${hasAPIDir ? "/api" : ""}/_analog${route}`,
66
+ lazy: true
67
+ };
68
+ });
86
69
  }
70
+ //#endregion
71
+ export { getPageHandlers };
72
+
87
73
  //# sourceMappingURL=get-page-handlers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-page-handlers.js","sourceRoot":"","sources":["../../../../../../packages/vite-plugin-nitro/src/lib/utils/get-page-handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAY,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC,OAAO,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAUrC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,MAAM,UAAU,eAAe,CAAC,EAC9B,aAAa,EACb,UAAU,EACV,OAAO,EACP,mBAAmB,EACnB,SAAS,GACO;IAChB,+DAA+D;IAC/D,MAAM,IAAI,GAAG,aAAa,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC,CAAC;IAE5D,4FAA4F;IAC5F,iGAAiG;IACjG,MAAM,aAAa,GAAa,QAAQ,CACtC;QACE,GAAG,IAAI,IAAI,UAAU,2BAA2B;QAChD,GAAG,CAAC,mBAAmB,IAAI,EAAE,CAAC,CAAC,GAAG,CAChC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,aAAa,GAAG,GAAG,iBAAiB,CACjD;KACF,EACD,EAAE,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAC9B,CAAC;IAEF,4DAA4D;IAC5D,MAAM,QAAQ,GAAwB,aAAa,CAAC,GAAG,CAAC,CAAC,YAAY,EAAE,EAAE;QACvE,gEAAgE;QAChE,MAAM,UAAU,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC;QAC/C,qDAAqD;QACrD,MAAM,KAAK,GAAG,UAAU;aACrB,OAAO,CAAC,eAAe,EAAE,QAAQ,CAAC;aAClC,OAAO,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC,8BAA8B;aAC3D,OAAO,CAAC,gBAAgB,EAAE,OAAO,CAAC,CAAC,oDAAoD;aACvF,OAAO,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAC,gCAAgC;aACpE,OAAO,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC,4CAA4C;aAC7E,OAAO,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC,6CAA6C;aAC1E,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,kCAAkC;QAE1D,8EAA8E;QAC9E,OAAO;YACL,OAAO,EAAE,YAAY;YACrB,KAAK,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,WAAW,KAAK,EAAE;YACnD,IAAI,EAAE,IAAI;SACX,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,QAAQ,CAAC;AAClB,CAAC"}
1
+ {"version":3,"file":"get-page-handlers.js","names":[],"sources":["../../../../src/lib/utils/get-page-handlers.ts"],"sourcesContent":["import { resolve, relative } from 'node:path';\nimport { globSync } from 'tinyglobby';\n\nimport type { NitroEventHandler } from 'nitro/types';\nimport { normalizePath } from 'vite';\n\ntype GetHandlersArgs = {\n workspaceRoot: string;\n sourceRoot: string;\n rootDir: string;\n additionalPagesDirs?: string[];\n hasAPIDir?: boolean;\n};\n\n/**\n * Discovers and generates Nitro event handlers for server-side page routes.\n *\n * This function:\n * 1. Discovers all .server.ts files in the app/pages directory and additional pages directories\n * 2. Converts file paths to route patterns using Angular-style route syntax\n * 3. Generates Nitro event handlers with proper route mapping and lazy loading\n * 4. Handles dynamic route parameters and catch-all routes\n *\n * @param workspaceRoot The workspace root directory path\n * @param sourceRoot The source directory path (e.g., 'src')\n * @param rootDir The project root directory relative to workspace\n * @param additionalPagesDirs Optional array of additional pages directories to scan\n * @param hasAPIDir Whether the project has an API directory (affects route prefixing)\n * @returns Array of NitroEventHandler objects with handler paths and route patterns\n *\n * Example usage:\n * const handlers = getPageHandlers({\n * workspaceRoot: '/workspace',\n * sourceRoot: 'src',\n * rootDir: 'apps/my-app',\n * additionalPagesDirs: ['/libs/shared/pages'],\n * hasAPIDir: true\n * });\n *\n * Sample discovered file paths:\n * - /workspace/apps/my-app/src/app/pages/index.server.ts\n * - /workspace/apps/my-app/src/app/pages/users/[id].server.ts\n * - /workspace/apps/my-app/src/app/pages/products/[...slug].server.ts\n * - /workspace/apps/my-app/src/app/pages/(auth)/login.server.ts\n *\n * Route transformation examples:\n * - index.server.ts → /_analog/pages/index\n * - users/[id].server.ts → /_analog/pages/users/:id\n * - products/[...slug].server.ts → /_analog/pages/products/**:slug\n * - (auth)/login.server.ts → /_analog/pages/-auth-/login\n *\n * tinyglobby vs fast-glob comparison:\n * - Both support the same glob patterns for file discovery\n * - Both are efficient for finding server-side page files\n * - tinyglobby is now used instead of fast-glob\n * - tinyglobby provides similar functionality with smaller bundle size\n * - tinyglobby's globSync returns absolute paths when absolute: true is set\n *\n * Route transformation rules:\n * 1. Removes .server.ts extension\n * 2. Converts [param] to :param for dynamic routes\n * 3. Converts [...param] to **:param for catch-all routes\n * 4. Converts (group) to -group- for route groups\n * 5. Converts dots to forward slashes\n * 6. Prefixes with /_analog/pages and optionally /api\n */\nexport function getPageHandlers({\n workspaceRoot,\n sourceRoot,\n rootDir,\n additionalPagesDirs,\n hasAPIDir,\n}: GetHandlersArgs): NitroEventHandler[] {\n // Normalize the project root path for consistent path handling\n const root = normalizePath(resolve(workspaceRoot, rootDir));\n\n // Discover all .server.ts files in the app/pages directory and additional pages directories\n // Pattern: looks for any .server.ts files in app/pages/**/*.server.ts and additional directories\n const endpointFiles: string[] = globSync(\n [\n `${root}/${sourceRoot}/app/pages/**/*.server.ts`,\n ...(additionalPagesDirs || []).map(\n (dir) => `${workspaceRoot}${dir}/**/*.server.ts`,\n ),\n ],\n { dot: true, absolute: true },\n );\n\n // Transform each discovered file into a Nitro event handler\n const handlers: NitroEventHandler[] = endpointFiles.map((endpointFile) => {\n // Normalize the endpoint file path for consistent path handling\n const normalized = normalizePath(endpointFile);\n // Transform the normalized path into a route pattern\n const route = normalized\n .replace(/^(.*?)\\/pages/, '/pages')\n .replace(/\\.server\\.ts$/, '') // Remove .server.ts extension\n .replace(/\\[\\.{3}(.+)\\]/g, '**:$1') // Convert [...param] to **:param (catch-all routes)\n .replace(/\\[\\.{3}(\\w+)\\]/g, '**:$1') // Alternative catch-all pattern\n .replace(/\\/\\((.*?)\\)$/, '/-$1-') // Convert (group) to -group- (route groups)\n .replace(/\\[(\\w+)\\]/g, ':$1') // Convert [param] to :param (dynamic routes)\n .replace(/\\./g, '/'); // Convert dots to forward slashes\n\n // Return Nitro event handler with absolute handler path and transformed route\n return {\n handler: endpointFile,\n route: `${hasAPIDir ? '/api' : ''}/_analog${route}`,\n lazy: true,\n };\n });\n\n return handlers;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkEA,SAAgB,gBAAgB,EAC9B,eACA,YACA,SACA,qBACA,aACuC;AAsCvC,QAhCgC,SAC9B,CACE,GANS,cAAc,QAAQ,eAAe,QAAQ,CAAC,CAM/C,GAAG,WAAW,4BACtB,IAAI,uBAAuB,EAAE,EAAE,KAC5B,QAAQ,GAAG,gBAAgB,IAAI,iBACjC,CACF,EACD;EAAE,KAAK;EAAM,UAAU;EAAM,CAC9B,CAGmD,KAAK,iBAAiB;EAIxE,MAAM,QAFa,cAAc,aAAa,CAG3C,QAAQ,iBAAiB,SAAS,CAClC,QAAQ,iBAAiB,GAAG,CAC5B,QAAQ,kBAAkB,QAAQ,CAClC,QAAQ,mBAAmB,QAAQ,CACnC,QAAQ,gBAAgB,QAAQ,CAChC,QAAQ,cAAc,MAAM,CAC5B,QAAQ,OAAO,IAAI;AAGtB,SAAO;GACL,SAAS;GACT,OAAO,GAAG,YAAY,SAAS,GAAG,UAAU;GAC5C,MAAM;GACP;GACD"}
@@ -0,0 +1,36 @@
1
+ import { PrerenderRoute } from "nitropack";
2
+ import { I18nPrerenderOptions } from "../options.js";
3
+ /**
4
+ * Expands a list of routes to include locale-prefixed variants.
5
+ *
6
+ * For each route and each locale, generates a prefixed route:
7
+ * '/' + locale + route
8
+ *
9
+ * The default locale's routes are included both with and without the prefix
10
+ * so that `/about` and `/en/about` both render.
11
+ *
12
+ * @param routes - The original routes to expand
13
+ * @param i18n - The i18n prerender configuration
14
+ * @returns Expanded routes with locale prefixes
15
+ */
16
+ export declare function expandRoutesWithLocales(routes: string[], i18n: I18nPrerenderOptions): string[];
17
+ /**
18
+ * Creates a post-rendering hook that injects the `lang` attribute
19
+ * into the `<html>` tag of prerendered pages based on the route's
20
+ * locale prefix.
21
+ *
22
+ * @param i18n - The i18n prerender configuration
23
+ * @returns A post-rendering hook function
24
+ */
25
+ export declare function createI18nPostRenderingHook(i18n: I18nPrerenderOptions): (route: PrerenderRoute) => Promise<void>;
26
+ /**
27
+ * Detects the locale from a prerendered route path by checking
28
+ * the first path segment against the configured locales.
29
+ */
30
+ export declare function detectLocaleFromRoute(route: string, i18n: I18nPrerenderOptions): string;
31
+ /**
32
+ * Sets the `lang` attribute on the `<html>` tag in an HTML string.
33
+ * If a `lang` attribute already exists, it is replaced.
34
+ * If no `lang` attribute exists, it is added.
35
+ */
36
+ export declare function setHtmlLang(html: string, locale: string): string;
@@ -0,0 +1,23 @@
1
+ //#region packages/vite-plugin-nitro/src/lib/utils/i18n-prerender.ts
2
+ /**
3
+ * Detects the locale from a prerendered route path by checking
4
+ * the first path segment against the configured locales.
5
+ */
6
+ function detectLocaleFromRoute(route, i18n) {
7
+ const firstSegment = route.split("/").filter(Boolean)[0];
8
+ if (firstSegment && i18n.locales.includes(firstSegment)) return firstSegment;
9
+ return i18n.defaultLocale;
10
+ }
11
+ /**
12
+ * Sets the `lang` attribute on the `<html>` tag in an HTML string.
13
+ * If a `lang` attribute already exists, it is replaced.
14
+ * If no `lang` attribute exists, it is added.
15
+ */
16
+ function setHtmlLang(html, locale) {
17
+ if (/<html[^>]*\slang\s*=\s*["'][^"']*["']/i.test(html)) return html.replace(/(<html[^>]*\s)lang\s*=\s*["'][^"']*["']/i, `$1lang="${locale}"`);
18
+ return html.replace(/<html/i, `<html lang="${locale}"`);
19
+ }
20
+ //#endregion
21
+ export { detectLocaleFromRoute, setHtmlLang };
22
+
23
+ //# sourceMappingURL=i18n-prerender.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"i18n-prerender.js","names":[],"sources":["../../../../src/lib/utils/i18n-prerender.ts"],"sourcesContent":["import { PrerenderRoute } from 'nitropack';\nimport { I18nPrerenderOptions } from '../options.js';\n\n/**\n * Expands a list of routes to include locale-prefixed variants.\n *\n * For each route and each locale, generates a prefixed route:\n * '/' + locale + route\n *\n * The default locale's routes are included both with and without the prefix\n * so that `/about` and `/en/about` both render.\n *\n * @param routes - The original routes to expand\n * @param i18n - The i18n prerender configuration\n * @returns Expanded routes with locale prefixes\n */\nexport function expandRoutesWithLocales(\n routes: string[],\n i18n: I18nPrerenderOptions,\n): string[] {\n const expanded: string[] = [];\n\n for (const route of routes) {\n // Skip API routes — they don't need locale prefixes\n if (route.includes('/_analog/') || route.startsWith('/api/')) {\n expanded.push(route);\n continue;\n }\n\n for (const locale of i18n.locales) {\n const prefix = `/${locale}`;\n const localizedRoute = route === '/' ? prefix : `${prefix}${route}`;\n expanded.push(localizedRoute);\n }\n\n // Keep the unprefixed route for the default locale\n if (!expanded.includes(route)) {\n expanded.push(route);\n }\n }\n\n return expanded;\n}\n\n/**\n * Creates a post-rendering hook that injects the `lang` attribute\n * into the `<html>` tag of prerendered pages based on the route's\n * locale prefix.\n *\n * @param i18n - The i18n prerender configuration\n * @returns A post-rendering hook function\n */\nexport function createI18nPostRenderingHook(\n i18n: I18nPrerenderOptions,\n): (route: PrerenderRoute) => Promise<void> {\n return async (route: PrerenderRoute) => {\n if (!route.contents || typeof route.contents !== 'string') {\n return;\n }\n\n const locale = detectLocaleFromRoute(route.route, i18n);\n if (!locale) {\n return;\n }\n\n // Inject or replace the lang attribute on <html>\n route.contents = setHtmlLang(route.contents, locale);\n };\n}\n\n/**\n * Detects the locale from a prerendered route path by checking\n * the first path segment against the configured locales.\n */\nexport function detectLocaleFromRoute(\n route: string,\n i18n: I18nPrerenderOptions,\n): string {\n const segments = route.split('/').filter(Boolean);\n const firstSegment = segments[0];\n\n if (firstSegment && i18n.locales.includes(firstSegment)) {\n return firstSegment;\n }\n\n return i18n.defaultLocale;\n}\n\n/**\n * Sets the `lang` attribute on the `<html>` tag in an HTML string.\n * If a `lang` attribute already exists, it is replaced.\n * If no `lang` attribute exists, it is added.\n */\nexport function setHtmlLang(html: string, locale: string): string {\n // Replace existing lang attribute\n if (/<html[^>]*\\slang\\s*=\\s*[\"'][^\"']*[\"']/i.test(html)) {\n return html.replace(\n /(<html[^>]*\\s)lang\\s*=\\s*[\"'][^\"']*[\"']/i,\n `$1lang=\"${locale}\"`,\n );\n }\n\n // Add lang attribute to <html> tag\n return html.replace(/<html/i, `<html lang=\"${locale}\"`);\n}\n"],"mappings":";;;;;AA0EA,SAAgB,sBACd,OACA,MACQ;CAER,MAAM,eADW,MAAM,MAAM,IAAI,CAAC,OAAO,QAAQ,CACnB;AAE9B,KAAI,gBAAgB,KAAK,QAAQ,SAAS,aAAa,CACrD,QAAO;AAGT,QAAO,KAAK;;;;;;;AAQd,SAAgB,YAAY,MAAc,QAAwB;AAEhE,KAAI,yCAAyC,KAAK,KAAK,CACrD,QAAO,KAAK,QACV,4CACA,WAAW,OAAO,GACnB;AAIH,QAAO,KAAK,QAAQ,UAAU,eAAe,OAAO,GAAG"}