@qwik.dev/router 2.0.0-beta.11 → 2.0.0-beta.14

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 (132) hide show
  1. package/lib/adapters/azure-swa/vite/index.mjs +28 -207
  2. package/lib/adapters/bun-server/vite/index.mjs +16 -202
  3. package/lib/adapters/cloud-run/vite/index.mjs +15 -201
  4. package/lib/adapters/cloudflare-pages/vite/index.mjs +65 -6
  5. package/lib/adapters/deno-server/vite/index.mjs +18 -204
  6. package/lib/adapters/netlify-edge/vite/index.mjs +58 -246
  7. package/lib/adapters/node-server/vite/index.mjs +16 -202
  8. package/lib/adapters/shared/vite/index.d.ts +4 -4
  9. package/lib/adapters/shared/vite/index.mjs +250 -149
  10. package/lib/adapters/ssg/vite/index.mjs +12 -199
  11. package/lib/adapters/vercel-edge/vite/index.d.ts +1 -1
  12. package/lib/adapters/vercel-edge/vite/index.mjs +50 -235
  13. package/lib/chunks/error-handler.mjs +57 -0
  14. package/lib/chunks/format-error.mjs +137 -0
  15. package/lib/chunks/fs.mjs +254 -0
  16. package/lib/chunks/index.mjs +884 -0
  17. package/lib/chunks/mime-types.mjs +52 -0
  18. package/lib/chunks/routing.qwik.mjs +429 -0
  19. package/lib/chunks/types.qwik.mjs +22 -0
  20. package/lib/index.d.ts +11 -4
  21. package/lib/index.qwik.mjs +533 -1019
  22. package/lib/middleware/aws-lambda/index.mjs +38 -28
  23. package/lib/middleware/azure-swa/index.mjs +65 -48
  24. package/lib/middleware/bun/index.mjs +104 -119
  25. package/lib/middleware/cloudflare-pages/index.mjs +69 -49
  26. package/lib/middleware/deno/index.mjs +94 -114
  27. package/lib/middleware/firebase/index.mjs +26 -18
  28. package/lib/middleware/netlify-edge/index.mjs +53 -38
  29. package/lib/middleware/node/index.mjs +184 -167
  30. package/lib/middleware/request-handler/index.d.ts +22 -4
  31. package/lib/middleware/request-handler/index.mjs +1203 -877
  32. package/lib/middleware/vercel-edge/index.mjs +72 -49
  33. package/lib/service-worker/index.mjs +4 -0
  34. package/lib/ssg/index.mjs +14 -22
  35. package/lib/vite/index.d.ts +13 -7
  36. package/lib/vite/index.mjs +1609 -1217
  37. package/package.json +27 -44
  38. package/lib/adapters/azure-swa/vite/index-BqUeglYs.cjs +0 -1
  39. package/lib/adapters/azure-swa/vite/index-CBIchDYq.js +0 -651
  40. package/lib/adapters/azure-swa/vite/index-ClHGw5z1.js +0 -6
  41. package/lib/adapters/azure-swa/vite/index-CrwlB95_.js +0 -22
  42. package/lib/adapters/azure-swa/vite/index-DTIOTwZo.cjs +0 -11
  43. package/lib/adapters/azure-swa/vite/index-vQuPcef3.cjs +0 -1
  44. package/lib/adapters/azure-swa/vite/index.cjs +0 -5
  45. package/lib/adapters/bun-server/vite/index-BqUeglYs.cjs +0 -1
  46. package/lib/adapters/bun-server/vite/index-CBIchDYq.js +0 -651
  47. package/lib/adapters/bun-server/vite/index-ClHGw5z1.js +0 -6
  48. package/lib/adapters/bun-server/vite/index-CrwlB95_.js +0 -22
  49. package/lib/adapters/bun-server/vite/index-DTIOTwZo.cjs +0 -11
  50. package/lib/adapters/bun-server/vite/index-vQuPcef3.cjs +0 -1
  51. package/lib/adapters/bun-server/vite/index.cjs +0 -5
  52. package/lib/adapters/cloud-run/vite/index-BqUeglYs.cjs +0 -1
  53. package/lib/adapters/cloud-run/vite/index-CBIchDYq.js +0 -651
  54. package/lib/adapters/cloud-run/vite/index-ClHGw5z1.js +0 -6
  55. package/lib/adapters/cloud-run/vite/index-CrwlB95_.js +0 -22
  56. package/lib/adapters/cloud-run/vite/index-DTIOTwZo.cjs +0 -11
  57. package/lib/adapters/cloud-run/vite/index-vQuPcef3.cjs +0 -1
  58. package/lib/adapters/cloud-run/vite/index.cjs +0 -5
  59. package/lib/adapters/cloudflare-pages/vite/index-Bg_9YkM5.js +0 -22
  60. package/lib/adapters/cloudflare-pages/vite/index-C1aDmh1S.cjs +0 -1
  61. package/lib/adapters/cloudflare-pages/vite/index-CHT9Y93A.js +0 -254
  62. package/lib/adapters/cloudflare-pages/vite/index-Ck7KvpK1.cjs +0 -11
  63. package/lib/adapters/cloudflare-pages/vite/index-ClHGw5z1.js +0 -6
  64. package/lib/adapters/cloudflare-pages/vite/index-Cp1cjAds.js +0 -645
  65. package/lib/adapters/cloudflare-pages/vite/index-D9RL9dvJ.cjs +0 -5
  66. package/lib/adapters/cloudflare-pages/vite/index-vQuPcef3.cjs +0 -1
  67. package/lib/adapters/cloudflare-pages/vite/index.cjs +0 -1
  68. package/lib/adapters/deno-server/vite/index-BqUeglYs.cjs +0 -1
  69. package/lib/adapters/deno-server/vite/index-CBIchDYq.js +0 -651
  70. package/lib/adapters/deno-server/vite/index-ClHGw5z1.js +0 -6
  71. package/lib/adapters/deno-server/vite/index-CrwlB95_.js +0 -22
  72. package/lib/adapters/deno-server/vite/index-DTIOTwZo.cjs +0 -11
  73. package/lib/adapters/deno-server/vite/index-vQuPcef3.cjs +0 -1
  74. package/lib/adapters/deno-server/vite/index.cjs +0 -5
  75. package/lib/adapters/netlify-edge/vite/index-BqUeglYs.cjs +0 -1
  76. package/lib/adapters/netlify-edge/vite/index-CBIchDYq.js +0 -651
  77. package/lib/adapters/netlify-edge/vite/index-ClHGw5z1.js +0 -6
  78. package/lib/adapters/netlify-edge/vite/index-CrwlB95_.js +0 -22
  79. package/lib/adapters/netlify-edge/vite/index-DTIOTwZo.cjs +0 -11
  80. package/lib/adapters/netlify-edge/vite/index-vQuPcef3.cjs +0 -1
  81. package/lib/adapters/netlify-edge/vite/index.cjs +0 -6
  82. package/lib/adapters/node-server/vite/index-BqUeglYs.cjs +0 -1
  83. package/lib/adapters/node-server/vite/index-CBIchDYq.js +0 -651
  84. package/lib/adapters/node-server/vite/index-ClHGw5z1.js +0 -6
  85. package/lib/adapters/node-server/vite/index-CrwlB95_.js +0 -22
  86. package/lib/adapters/node-server/vite/index-DTIOTwZo.cjs +0 -11
  87. package/lib/adapters/node-server/vite/index-vQuPcef3.cjs +0 -1
  88. package/lib/adapters/node-server/vite/index.cjs +0 -5
  89. package/lib/adapters/shared/vite/index-BqUeglYs.cjs +0 -1
  90. package/lib/adapters/shared/vite/index-CBIchDYq.js +0 -651
  91. package/lib/adapters/shared/vite/index-ClHGw5z1.js +0 -6
  92. package/lib/adapters/shared/vite/index-CrwlB95_.js +0 -22
  93. package/lib/adapters/shared/vite/index-DTIOTwZo.cjs +0 -11
  94. package/lib/adapters/shared/vite/index-vQuPcef3.cjs +0 -1
  95. package/lib/adapters/shared/vite/index.cjs +0 -5
  96. package/lib/adapters/ssg/vite/index-BqUeglYs.cjs +0 -1
  97. package/lib/adapters/ssg/vite/index-CBIchDYq.js +0 -651
  98. package/lib/adapters/ssg/vite/index-ClHGw5z1.js +0 -6
  99. package/lib/adapters/ssg/vite/index-CrwlB95_.js +0 -22
  100. package/lib/adapters/ssg/vite/index-DTIOTwZo.cjs +0 -11
  101. package/lib/adapters/ssg/vite/index-vQuPcef3.cjs +0 -1
  102. package/lib/adapters/ssg/vite/index.cjs +0 -5
  103. package/lib/adapters/vercel-edge/vite/index-BqUeglYs.cjs +0 -1
  104. package/lib/adapters/vercel-edge/vite/index-CBIchDYq.js +0 -651
  105. package/lib/adapters/vercel-edge/vite/index-ClHGw5z1.js +0 -6
  106. package/lib/adapters/vercel-edge/vite/index-CrwlB95_.js +0 -22
  107. package/lib/adapters/vercel-edge/vite/index-DTIOTwZo.cjs +0 -11
  108. package/lib/adapters/vercel-edge/vite/index-vQuPcef3.cjs +0 -1
  109. package/lib/adapters/vercel-edge/vite/index.cjs +0 -5
  110. package/lib/index.qwik.cjs +0 -2126
  111. package/lib/middleware/aws-lambda/index.cjs +0 -1
  112. package/lib/middleware/azure-swa/index.cjs +0 -1
  113. package/lib/middleware/bun/index.cjs +0 -1
  114. package/lib/middleware/cloudflare-pages/index.cjs +0 -1
  115. package/lib/middleware/deno/index.cjs +0 -1
  116. package/lib/middleware/firebase/index.cjs +0 -1
  117. package/lib/middleware/netlify-edge/index.cjs +0 -1
  118. package/lib/middleware/node/index.cjs +0 -1
  119. package/lib/middleware/request-handler/index.cjs +0 -18
  120. package/lib/middleware/vercel-edge/index.cjs +0 -1
  121. package/lib/service-worker.cjs +0 -1
  122. package/lib/service-worker.mjs +0 -5
  123. package/lib/ssg/deno.cjs +0 -1
  124. package/lib/ssg/deno.mjs +0 -6
  125. package/lib/ssg/index-CBIchDYq.js +0 -651
  126. package/lib/ssg/index-ClHGw5z1.js +0 -6
  127. package/lib/ssg/index-DTIOTwZo.cjs +0 -11
  128. package/lib/ssg/index-vQuPcef3.cjs +0 -1
  129. package/lib/ssg/index.cjs +0 -1
  130. package/lib/ssg/node.cjs +0 -11
  131. package/lib/ssg/node.mjs +0 -651
  132. package/lib/vite/index.cjs +0 -42
@@ -1,561 +1,623 @@
1
- import { mergeConfig as pe, loadEnv as me } from "vite";
2
- import x, { existsSync as ge } from "node:fs";
3
- import Q, { normalize as ye, basename as M, dirname as R, relative as ee, join as j, resolve as T, isAbsolute as q, extname as we } from "node:path";
4
- import { marked as ve } from "marked";
5
- import { SourceMapGenerator as be } from "source-map";
6
- import { visit as E } from "unist-util-visit";
7
- import { parse as Pe } from "yaml";
8
- import xe from "github-slugger";
9
- import { valueToEstree as Se } from "estree-util-value-to-estree";
10
- import { headingRank as te } from "hast-util-heading-rank";
11
- import { toString as B } from "hast-util-to-string";
12
- import { refractor as F } from "refractor";
13
- import ke from "refractor/lang/tsx.js";
14
- import { optimize as Ee } from "svgo";
15
- function yr(e, t) {
16
- return async (r) => {
17
- let n = await e;
18
- typeof n == "function" && (n = await n(r));
19
- let s = await t;
20
- return typeof s == "function" && (s = await s(r)), pe(n, s);
21
- };
22
- }
23
- const Ie = '"serviceWorker"in navigator?(navigator.serviceWorker.register("__url").catch(e=>console.error(e)),"caches"in window&&caches.keys().then(e=>{const r=e.find(e=>e.startsWith("QwikBuild"));r&&caches.delete(r).catch(console.error)}).catch(console.error)):console.log("Service worker not supported in this browser.")';
24
- function Ne(e) {
25
- return e.replace(/\w\S*/g, (t) => t.charAt(0).toUpperCase() + t.slice(1).toLowerCase());
26
- }
27
- function re(e, t) {
28
- e.diagnostics.push({
29
- type: "error",
30
- message: t ? String(t.stack || t) : "Error"
31
- });
32
- }
33
- function Re(e, t) {
34
- e.diagnostics.push({
35
- type: "warn",
36
- message: String(t)
37
- });
38
- }
39
- function ne(e, t) {
40
- if (typeof e == "string" && (e = e.trim(), e !== ""))
41
- try {
42
- if (e = e.replace(/\/+/g, "/"), e.startsWith("/") && (e = e.slice(1)), e = new URL(t + e, "https://qwik.dev").pathname, e !== t) {
43
- if (globalThis.__NO_TRAILING_SLASH__)
44
- e.endsWith("/") && (e = e.slice(0, e.length - 1));
45
- else if (!e.endsWith("/")) {
46
- const r = e.split("/");
47
- r[r.length - 1].includes(".") || (e += "/");
48
- }
49
- }
50
- return e;
51
- } catch (r) {
52
- console.error(r);
1
+ import { mergeConfig, loadEnv } from 'vite';
2
+ import fs, { existsSync } from 'node:fs';
3
+ import path, { join, dirname, basename, resolve, isAbsolute, extname } from 'node:path';
4
+ import { g as getExtension, r as removeExtension, i as isIndexModule, a as isErrorName, b as isLayoutModule, c as isEntryName, d as isMenuFileName, e as isServiceWorkerName, f as isPageModuleExt, h as isModuleExt, j as isMarkdownExt, k as isSameOriginUrl, l as normalizePath, m as getPathnameFromDirPath, o as getMenuPathname, p as createFileId, q as parseRouteIndexName, s as isPluginModule, t as addError, u as addWarning, v as isPageExt } from '../chunks/fs.mjs';
5
+ import { marked } from 'marked';
6
+ import { SourceMapGenerator } from 'source-map';
7
+ import { visit } from 'unist-util-visit';
8
+ import { parse } from 'yaml';
9
+ import Slugger from 'github-slugger';
10
+ import { valueToEstree } from 'estree-util-value-to-estree';
11
+ import { headingRank } from 'hast-util-heading-rank';
12
+ import { toString } from 'hast-util-to-string';
13
+ import { refractor } from 'refractor';
14
+ import tsxLang from 'refractor/lang/tsx.js';
15
+ import { optimize } from 'svgo';
16
+ import { p as parseId, f as formatError } from '../chunks/format-error.mjs';
17
+
18
+ function extendConfig(baseConfigExport, serverConfigExport) {
19
+ return async (env) => {
20
+ let resolvedBase = await baseConfigExport;
21
+ if (typeof resolvedBase === "function") {
22
+ resolvedBase = await resolvedBase(env);
53
23
  }
54
- return null;
55
- }
56
- function se(e) {
57
- if (typeof e == "string" && (e = e.trim(), e !== "")) {
58
- const t = e.charAt(0);
59
- if (t !== "/" && t !== ".") {
60
- if (t === "#")
61
- return !1;
62
- const r = e.indexOf(":");
63
- if (r > -1) {
64
- const n = e.slice(0, r).toLowerCase();
65
- return !$e[n];
66
- }
24
+ let resolvedServer = await serverConfigExport;
25
+ if (typeof resolvedServer === "function") {
26
+ resolvedServer = await resolvedServer(env);
67
27
  }
68
- return !0;
69
- }
70
- return !1;
28
+ return mergeConfig(resolvedBase, resolvedServer);
29
+ };
71
30
  }
72
- const $e = {
73
- https: !0,
74
- http: !0,
75
- about: !0,
76
- javascript: !0,
77
- file: !0
78
- };
79
- function _e(e) {
80
- let t = "";
81
- const r = e.endsWith("!");
82
- r && (e = e.slice(0, e.length - 1));
83
- const n = e.split("@");
84
- return n.length > 1 && (n.shift(), t = n.join("@")), { layoutName: t, layoutStop: r };
85
- }
86
- function L(e, t) {
87
- const r = ee(e.routesDir, t);
88
- let n = v(r);
89
- return n = ne(n, e.basePathname).split("/").filter((s) => !qe(s)).join("/"), n === "" ? "/" : n;
90
- }
91
- function De(e, t) {
92
- let r = v(ee(e.routesDir, t));
93
- r = "/" + v(R(r));
94
- let n = ne(r, e.basePathname);
95
- return n.endsWith("/") || (n += "/"), n;
96
- }
97
- function $(e) {
98
- if (typeof e == "string") {
99
- const t = e.trim().toLowerCase().split(".");
100
- if (t.length > 1) {
101
- const r = t.pop().split("?")[0].split("#")[0];
102
- return r === "ts" && t.pop() === "d" ? ".d.ts" : "." + r;
103
- }
31
+
32
+ const swRegister = 'export default""';
33
+
34
+ function getSourceFile(fileName) {
35
+ const ext = getExtension(fileName);
36
+ const extlessName = removeExtension(fileName);
37
+ const isPageModule = isPageModuleExt(ext);
38
+ const isModule = isModuleExt(ext);
39
+ const isMarkdown = isMarkdownExt(ext);
40
+ let type = null;
41
+ if ((isIndexModule(extlessName) || isErrorName(extlessName)) && (isPageModule || isModule || isMarkdown)) {
42
+ type = "route";
43
+ } else if (isLayoutModule(extlessName) && (isPageModule || isModule)) {
44
+ type = "layout";
45
+ } else if (isEntryName(extlessName) && isModule) {
46
+ type = "entry";
47
+ } else if (isMenuFileName(fileName)) {
48
+ type = "menu";
49
+ } else if (isModule && isServiceWorkerName(extlessName)) {
50
+ type = "service-worker";
104
51
  }
105
- return "";
106
- }
107
- function S(e) {
108
- if (typeof e == "string") {
109
- e = e.trim();
110
- const t = $(e);
111
- return e.slice(0, e.length - t.length);
52
+ if (type !== null) {
53
+ const sourceFileName = {
54
+ type,
55
+ extlessName,
56
+ ext
57
+ };
58
+ return sourceFileName;
112
59
  }
113
- return "";
114
- }
115
- function v(e) {
116
- return Oe(ye(e));
117
- }
118
- function Oe(e) {
119
- const t = /^\\\\\?\\/.test(e), r = /[^\u0000-\u0080]+/.test(e);
120
- return t || r || (e = e.replace(/\\/g, "/"), e.endsWith("/") && (e = e.slice(0, e.length - 1))), e;
60
+ return null;
121
61
  }
122
- function I(e, t, r) {
123
- const n = [];
124
- for (let s = 0; s < 25; s++) {
125
- let i = S(M(t));
126
- if (i = i.replace(/[\W_]+/g, ""), i === "" ? i = "Q" + s : isNaN(i.charAt(0)) || (i = "Q" + i), n.push(Ne(i)), t = v(R(t)), t === e)
127
- break;
62
+
63
+ function getMarkdownRelativeUrl(opts, containingFilePath, url, checkFileExists) {
64
+ if (typeof url !== "string" || !isSameOriginUrl(url)) {
65
+ return url;
128
66
  }
129
- return n.length > 1 && n[0] === "Index" && n.shift(), n.reverse().join("").concat(r || "");
130
- }
131
- const ie = {
132
- ".tsx": !0,
133
- ".jsx": !0
134
- }, Te = {
135
- ".ts": !0,
136
- ".js": !0
137
- }, oe = {
138
- ".md": !0,
139
- ".mdx": !0
140
- };
141
- function We(e) {
142
- return /^index(|!|@.+)$/.test(e);
143
- }
144
- function je(e) {
145
- return /^plugin(|@.+)$/.test(e);
146
- }
147
- function Ae(e) {
148
- return /^layout(|!|-.+)$/.test(e);
149
- }
150
- function ae(e) {
151
- return e in ie;
152
- }
153
- function U(e) {
154
- return e in Te;
155
- }
156
- function J(e) {
157
- return e in oe;
158
- }
159
- function Ce(e) {
160
- return e in ie || e in oe;
161
- }
162
- function ue(e) {
163
- return e === "menu.md";
164
- }
165
- function Me(e) {
166
- return e === "service-worker";
167
- }
168
- function Le(e) {
169
- return e === "entry";
170
- }
171
- function He(e) {
172
- try {
173
- const t = parseInt(e, 10);
174
- return t >= 400 && t <= 599;
175
- } catch {
176
- }
177
- return !1;
178
- }
179
- function qe(e, t = !0) {
180
- return e.startsWith("__") ? (t && console.warn(
181
- `Grouped (pathless) layout "${e}" should use the "(${e.slice(
182
- 2
183
- )})" directory name instead. Prefixing a directory with "__" has been deprecated and will be removed in future versions.`
184
- ), !0) : e.startsWith("(") && e.endsWith(")");
185
- }
186
- function le(e) {
187
- const t = $(e), r = S(e), n = ae(t), s = U(t), i = J(t);
188
- let o = null;
189
- return (We(r) || He(r)) && (n || s || i) ? o = "route" : Ae(r) && (n || s) ? o = "layout" : Le(r) && s ? o = "entry" : ue(e) ? o = "menu" : s && Me(r) && (o = "service-worker"), o !== null ? {
190
- type: o,
191
- extlessName: r,
192
- ext: t
193
- } : null;
194
- }
195
- function C(e, t, r, n) {
196
- if (typeof r != "string" || !se(r))
197
- return r;
198
- const s = r.split("?"), i = r.split("#"), o = r.split("?")[0].split("#")[0], a = $(o);
199
- if (J(a)) {
200
- const u = o.startsWith("/"), c = v(o).split("/").filter((w) => w.length > 0), l = u ? j(e.routesDir, ...c) : j(R(t), ...c);
201
- n && !ge(l) && console.warn(
202
- `
203
- The link "${r}", found within "${t}" does not have a matching source file.
67
+ const querySplit = url.split("?");
68
+ const hashSplit = url.split("#");
69
+ const strippedUrl = url.split("?")[0].split("#")[0];
70
+ const extension = getExtension(strippedUrl);
71
+ if (isMarkdownExt(extension)) {
72
+ const isAbsolute = strippedUrl.startsWith("/");
73
+ const parts = normalizePath(strippedUrl).split("/").filter((p) => p.length > 0);
74
+ const filePath = isAbsolute ? join(opts.routesDir, ...parts) : join(dirname(containingFilePath), ...parts);
75
+ if (checkFileExists && !existsSync(filePath)) {
76
+ console.warn(
77
+ `
78
+ The link "${url}", found within "${containingFilePath}" does not have a matching source file.
204
79
  `
205
- );
206
- const f = M(l);
207
- if (le(f)) {
208
- const w = R(l);
209
- let d = L(e, w);
210
- return s.length > 1 ? d += "?" + s[1] : i.length > 1 && (d += "#" + i[1]), d;
80
+ );
81
+ }
82
+ const fileName = basename(filePath);
83
+ const sourceFileName = getSourceFile(fileName);
84
+ if (sourceFileName) {
85
+ const mdDirPath = dirname(filePath);
86
+ let pathname = getPathnameFromDirPath(opts, mdDirPath);
87
+ if (querySplit.length > 1) {
88
+ pathname += "?" + querySplit[1];
89
+ } else if (hashSplit.length > 1) {
90
+ pathname += "#" + hashSplit[1];
91
+ }
92
+ return pathname;
93
+ }
94
+ } else if (extension === "") {
95
+ if (url.endsWith("/")) {
96
+ if (globalThis.__NO_TRAILING_SLASH__) {
97
+ url = url.slice(0, -1);
98
+ }
99
+ } else if (!globalThis.__NO_TRAILING_SLASH__) {
100
+ url += "/";
211
101
  }
212
- } else a === "" && (r.endsWith("/") ? globalThis.__NO_TRAILING_SLASH__ && (r = r.slice(0, -1)) : globalThis.__NO_TRAILING_SLASH__ || (r += "/"));
213
- return r;
102
+ }
103
+ return url;
214
104
  }
215
- function Fe(e, t) {
216
- return {
217
- pathname: De(e, t),
218
- filePath: t
105
+
106
+ function createMenu(opts, filePath) {
107
+ const menu = {
108
+ pathname: getMenuPathname(opts, filePath),
109
+ filePath
219
110
  };
220
- }
221
- function Be(e, t) {
222
- return Fe(e, t.filePath);
223
- }
224
- async function Ue(e, t, r) {
225
- const n = Je(e, t, r), s = I(e.routesDir, t);
226
- return `${`const ${s} = ${JSON.stringify(n, null, 2)};`} export default ${s}`;
227
- }
228
- function Je(e, t, r, n = !0) {
229
- const s = ve.lexer(r, {});
230
- let i = 0;
231
- const o = [];
232
- for (const a of s)
233
- if (a.type === "heading") {
234
- const u = i - a.depth;
235
- if (u >= 0 && (o.length -= u + 1), u < -1)
111
+ return menu;
112
+ }
113
+ function resolveMenu(opts, menuSourceFile) {
114
+ return createMenu(opts, menuSourceFile.filePath);
115
+ }
116
+ async function transformMenu(opts, filePath, content) {
117
+ const parsedMenu = parseMenu(opts, filePath, content);
118
+ const id = createFileId(opts.routesDir, filePath);
119
+ const code = `const ${id} = ${JSON.stringify(parsedMenu, null, 2)};`;
120
+ return `${code} export default ${id}`;
121
+ }
122
+ function parseMenu(opts, filePath, content, checkFileExists = true) {
123
+ const tokens = marked.lexer(content, {});
124
+ let currentDepth = 0;
125
+ const stack = [];
126
+ for (const t of tokens) {
127
+ if (t.type === "heading") {
128
+ const diff = currentDepth - t.depth;
129
+ if (diff >= 0) {
130
+ stack.length -= diff + 1;
131
+ }
132
+ if (diff < -1) {
236
133
  throw new Error(
237
134
  `Menu hierarchy skipped a level, went from <h${"#".repeat(
238
- i
239
- )}> to <h${"#".repeat(a.depth)}>, in menu: ${t}`
135
+ currentDepth
136
+ )}> to <h${"#".repeat(t.depth)}>, in menu: ${filePath}`
240
137
  );
241
- i = a.depth;
242
- const c = o[o.length - 1];
243
- for (const l of a.tokens || []) {
244
- const f = {
138
+ }
139
+ currentDepth = t.depth;
140
+ const parentNode = stack[stack.length - 1];
141
+ for (const h2Token of t.tokens || []) {
142
+ const lastNode = {
245
143
  text: ""
246
144
  };
247
- if (l.type === "text")
248
- f.text = l.text;
249
- else if (l.type === "link")
250
- f.text = l.text, f.href = C(e, t, l.href, n);
251
- else
145
+ if (h2Token.type === "text") {
146
+ lastNode.text = h2Token.text;
147
+ } else if (h2Token.type === "link") {
148
+ lastNode.text = h2Token.text;
149
+ lastNode.href = getMarkdownRelativeUrl(opts, filePath, h2Token.href, checkFileExists);
150
+ } else {
252
151
  throw new Error(
253
- `Headings can only be a text or link. Received "${l.type}", value "${l.raw}", in menu: ${t}`
152
+ `Headings can only be a text or link. Received "${h2Token.type}", value "${h2Token.raw}", in menu: ${filePath}`
254
153
  );
255
- c && (c.items = c.items || [], c.items.push(f)), o.push(f);
154
+ }
155
+ if (parentNode) {
156
+ parentNode.items = parentNode.items || [];
157
+ parentNode.items.push(lastNode);
158
+ }
159
+ stack.push(lastNode);
256
160
  }
257
- } else if (a.type === "list") {
258
- const u = o[o.length - 1];
259
- u.items = u.items || [];
260
- for (const c of a.items)
261
- if (c.type === "list_item")
262
- for (const l of c.tokens)
263
- if (l.type === "text")
264
- for (const f of l.tokens)
265
- if (f.type === "text")
266
- u.items.push({ text: f.text });
267
- else if (f.type === "link")
268
- u.items.push({
269
- text: f.text,
270
- href: C(e, t, f.href, n)
161
+ } else if (t.type === "list") {
162
+ const parentNode = stack[stack.length - 1];
163
+ parentNode.items = parentNode.items || [];
164
+ for (const li of t.items) {
165
+ if (li.type === "list_item") {
166
+ for (const liToken of li.tokens) {
167
+ if (liToken.type === "text") {
168
+ for (const liItem of liToken.tokens) {
169
+ if (liItem.type === "text") {
170
+ parentNode.items.push({ text: liItem.text });
171
+ } else if (liItem.type === "link") {
172
+ parentNode.items.push({
173
+ text: liItem.text,
174
+ href: getMarkdownRelativeUrl(opts, filePath, liItem.href, checkFileExists)
271
175
  });
272
- else
176
+ } else {
273
177
  throw new Error(
274
- `List items can only be a text or link. Received "${f.type}", value "${f.raw}", in menu: ${t}`
178
+ `List items can only be a text or link. Received "${liItem.type}", value "${liItem.raw}", in menu: ${filePath}`
275
179
  );
276
- else if (l.type === "link")
277
- u.items.push({
278
- text: l.text,
279
- href: C(e, t, l.href, n)
180
+ }
181
+ }
182
+ } else if (liToken.type === "link") {
183
+ parentNode.items.push({
184
+ text: liToken.text,
185
+ href: getMarkdownRelativeUrl(opts, filePath, liToken.href, checkFileExists)
280
186
  });
281
- else
187
+ } else {
282
188
  throw new Error(
283
- `List items can only be a text or link. Received "${l.type}", value "${l.raw}", in menu: ${t}`
189
+ `List items can only be a text or link. Received "${liToken.type}", value "${liToken.raw}", in menu: ${filePath}`
284
190
  );
285
- else
191
+ }
192
+ }
193
+ } else {
286
194
  throw new Error(
287
- `Only list items can be used in lists. Received "${c.type}", value "${c.raw}", in menu: ${t}`
195
+ `Only list items can be used in lists. Received "${li.type}", value "${li.raw}", in menu: ${filePath}`
288
196
  );
197
+ }
198
+ }
199
+ } else if (t.type === "space") {
200
+ continue;
289
201
  } else {
290
- if (a.type === "space")
291
- continue;
292
202
  throw new Error(
293
- `Menu has a "${a.type}" with the value "${a.raw}". However, only headings and lists can be used in the menu: ${t}`
203
+ `Menu has a "${t.type}" with the value "${t.raw}". However, only headings and lists can be used in the menu: ${filePath}`
294
204
  );
295
205
  }
296
- if (o.length === 0)
297
- throw new Error(`Menu must start with an h1 in the index: ${t}`);
298
- return o[0];
206
+ }
207
+ if (stack.length === 0) {
208
+ throw new Error(`Menu must start with an h1 in the index: ${filePath}`);
209
+ }
210
+ return stack[0];
299
211
  }
300
- function G(e, t) {
301
- if (t === e)
212
+
213
+ function parseRoutePathname(basePathname, pathname) {
214
+ if (pathname === basePathname) {
302
215
  return {
303
- pattern: new RegExp("^" + t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "$"),
304
- routeName: t,
216
+ pattern: new RegExp("^" + pathname.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "$"),
217
+ routeName: pathname,
305
218
  paramNames: [],
306
- segments: [[{ content: "", dynamic: !1, rest: !1 }]]
219
+ segments: [[{ content: "", dynamic: false, rest: false }]]
307
220
  };
308
- t = t.slice(1);
309
- const r = t.split("/"), n = [];
310
- return {
311
- pattern: new RegExp(
312
- `^${r.filter((i) => i.length > 0).map((i) => {
313
- const o = decodeURI(i), a = /^\[\.\.\.(\w+)?\]$/.exec(o);
314
- return a ? (n.push(a[1]), "(?:/(.*))?") : "/" + o.split(ze).map((u, c) => {
315
- if (c % 2) {
316
- const l = Ge.exec(u);
317
- if (l) {
318
- const [, f, p] = l;
319
- return n.push(p), f ? "(.*?)" : "([^/]+?)";
320
- }
221
+ }
222
+ pathname = pathname.slice(1);
223
+ const segments = pathname.split("/");
224
+ const paramNames = [];
225
+ const pattern = new RegExp(
226
+ `^${segments.filter((segment) => segment.length > 0).map((s) => {
227
+ const segment = decodeURI(s);
228
+ const catchAll = /^\[\.\.\.(\w+)?\]$/.exec(segment);
229
+ if (catchAll) {
230
+ paramNames.push(catchAll[1]);
231
+ return "(?:/(.*))?";
232
+ }
233
+ return "/" + segment.split(DYNAMIC_SEGMENT).map((content, i) => {
234
+ if (i % 2) {
235
+ const rg = PARAM_PATTERN.exec(content);
236
+ if (rg) {
237
+ const [, rest, name] = rg;
238
+ paramNames.push(name);
239
+ return rest ? "(.*?)" : "([^/]+?)";
321
240
  }
322
- return encodeURI(u).normalize().replace(/%5[Bb]/g, "[").replace(/%5[Dd]/g, "]").replace(/#/g, "%23").replace(/\?/g, "%3F").replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
323
- }).join("");
324
- }).join("")}/?$`
325
- // always match with and without a trailing slash
326
- ),
327
- routeName: t,
328
- paramNames: n,
329
- segments: r.map((i) => {
330
- const o = [];
331
- return i.split(/\[(.+?)\]/).map((a, u) => {
332
- if (a) {
333
- const c = !!(u % 2);
334
- o.push({
335
- content: a,
336
- dynamic: c,
337
- rest: c && a.startsWith("...")
241
+ }
242
+ return encodeURI(content).normalize().replace(/%5[Bb]/g, "[").replace(/%5[Dd]/g, "]").replace(/#/g, "%23").replace(/\?/g, "%3F").replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
243
+ }).join("");
244
+ }).join("")}/?$`
245
+ // always match with and without a trailing slash
246
+ );
247
+ return {
248
+ pattern,
249
+ routeName: pathname,
250
+ paramNames,
251
+ segments: segments.map((segment) => {
252
+ const parts = [];
253
+ segment.split(/\[(.+?)\]/).map((content, i) => {
254
+ if (content) {
255
+ const dynamic = !!(i % 2);
256
+ parts.push({
257
+ content,
258
+ dynamic,
259
+ rest: dynamic && content.startsWith("...")
338
260
  });
339
261
  }
340
- }), o;
262
+ });
263
+ return parts;
341
264
  })
342
265
  };
343
266
  }
344
- const Ge = /^(\.\.\.)?(\w+)?$/, ze = /\[(.+?)\]/;
345
- function Qe(e, t) {
346
- const r = Math.max(e.segments.length, t.segments.length);
347
- for (let n = 0; n < r; n += 1) {
348
- const s = e.segments[n], i = t.segments[n];
349
- if (!s)
350
- return e.pathname.includes("[...") ? 1 : -1;
351
- if (!i)
352
- return t.pathname.includes("[...") ? -1 : 1;
353
- const o = Math.max(s.length, i.length);
354
- for (let a = 0; a < o; a += 1) {
355
- const u = s[a], c = i[a];
356
- if (u === void 0)
357
- return c.dynamic ? -1 : 1;
358
- if (c === void 0 || u.dynamic !== c.dynamic)
359
- return u.dynamic ? 1 : -1;
360
- if (u.dynamic && u.rest !== c.rest)
361
- return u.rest ? 1 : -1;
267
+ const PARAM_PATTERN = /^(\.\.\.)?(\w+)?$/;
268
+ const DYNAMIC_SEGMENT = /\[(.+?)\]/;
269
+
270
+ function routeSortCompare(a, b) {
271
+ const maxSegments = Math.max(a.segments.length, b.segments.length);
272
+ for (let i = 0; i < maxSegments; i += 1) {
273
+ const sa = a.segments[i];
274
+ const sb = b.segments[i];
275
+ if (!sa) {
276
+ return a.pathname.includes("[...") ? 1 : -1;
277
+ }
278
+ if (!sb) {
279
+ return b.pathname.includes("[...") ? -1 : 1;
280
+ }
281
+ const maxParts = Math.max(sa.length, sb.length);
282
+ for (let i2 = 0; i2 < maxParts; i2 += 1) {
283
+ const pa = sa[i2];
284
+ const pb = sb[i2];
285
+ if (pa === void 0) {
286
+ return pb.dynamic ? -1 : 1;
287
+ }
288
+ if (pb === void 0) {
289
+ return pa.dynamic ? 1 : -1;
290
+ }
291
+ if (pa.dynamic !== pb.dynamic) {
292
+ return pa.dynamic ? 1 : -1;
293
+ }
294
+ if (pa.dynamic) {
295
+ if (pa.rest !== pb.rest) {
296
+ return pa.rest ? 1 : -1;
297
+ }
298
+ }
362
299
  }
363
300
  }
364
- return e.pathname === t.pathname ? e.ext > t.ext ? -1 : 1 : e.pathname < t.pathname ? -1 : 1;
365
- }
366
- function Ke(e, t) {
367
- const r = t.filter((l) => l.type === "layout").map((l) => Ve(e, l)).sort((l, f) => l.id < f.id ? -1 : 1), n = t.filter((l) => l.type === "route").map((l) => Ze(e, r, l)).sort(Qe), s = t.filter((l) => l.type === "entry").map((l) => et(e, l)).sort((l, f) => l.chunkFileName < f.chunkFileName ? -1 : 1), i = t.filter((l) => l.type === "service-worker").map((l) => tt(e, l)).sort((l, f) => l.chunkFileName < f.chunkFileName ? -1 : 1), o = t.filter((l) => l.type === "menu").map((l) => Be(e, l)).sort((l, f) => l.pathname < f.pathname ? -1 : 1);
368
- let a = 0;
369
- const u = /* @__PURE__ */ new Set(), c = (l) => {
370
- for (const f of l) {
371
- let p = f.id;
372
- for (; u.has(p); )
373
- p = `${f.id}_${a++}`;
374
- f.id = p, u.add(p);
301
+ if (a.pathname === b.pathname) {
302
+ return a.ext > b.ext ? -1 : 1;
303
+ }
304
+ return a.pathname < b.pathname ? -1 : 1;
305
+ }
306
+
307
+ function resolveSourceFiles(opts, sourceFiles) {
308
+ const layouts = sourceFiles.filter((s) => s.type === "layout").map((s) => resolveLayout(opts, s)).sort((a, b) => {
309
+ return a.id < b.id ? -1 : 1;
310
+ });
311
+ const routes = sourceFiles.filter((s) => s.type === "route").map((s) => resolveRoute(opts, layouts, s)).sort(routeSortCompare);
312
+ const entries = sourceFiles.filter((s) => s.type === "entry").map((s) => resolveEntry(opts, s)).sort((a, b) => {
313
+ return a.chunkFileName < b.chunkFileName ? -1 : 1;
314
+ });
315
+ const serviceWorkers = sourceFiles.filter((s) => s.type === "service-worker").map((p) => resolveServiceWorkerEntry(opts, p)).sort((a, b) => {
316
+ return a.chunkFileName < b.chunkFileName ? -1 : 1;
317
+ });
318
+ const menus = sourceFiles.filter((s) => s.type === "menu").map((p) => resolveMenu(opts, p)).sort((a, b) => {
319
+ return a.pathname < b.pathname ? -1 : 1;
320
+ });
321
+ let inc = 0;
322
+ const ids = /* @__PURE__ */ new Set();
323
+ const uniqueIds = (b) => {
324
+ for (const r of b) {
325
+ let id = r.id;
326
+ while (ids.has(id)) {
327
+ id = `${r.id}_${inc++}`;
328
+ }
329
+ r.id = id;
330
+ ids.add(id);
375
331
  }
376
332
  };
377
- return c(r), c(n), c(s), c(i), { layouts: r, routes: n, entries: s, menus: o, serviceWorkers: i };
378
- }
379
- function Ve(e, t) {
380
- let r = t.extlessName;
381
- const n = t.filePath, s = t.dirPath;
382
- let i, o;
383
- return r.endsWith(Xe) ? (o = "top", r = r.slice(0, r.length - 1)) : o = "nested", r.startsWith(K) ? i = r.slice(K.length) : i = "", {
384
- id: I(e.routesDir, n),
385
- filePath: n,
386
- dirPath: s,
387
- layoutType: o,
388
- layoutName: i
333
+ uniqueIds(layouts);
334
+ uniqueIds(routes);
335
+ uniqueIds(entries);
336
+ uniqueIds(serviceWorkers);
337
+ return { layouts, routes, entries, menus, serviceWorkers };
338
+ }
339
+ function resolveLayout(opts, layoutSourceFile) {
340
+ let extlessName = layoutSourceFile.extlessName;
341
+ const filePath = layoutSourceFile.filePath;
342
+ const dirPath = layoutSourceFile.dirPath;
343
+ let layoutName;
344
+ let layoutType;
345
+ if (extlessName.endsWith(LAYOUT_TOP_SUFFIX)) {
346
+ layoutType = "top";
347
+ extlessName = extlessName.slice(0, extlessName.length - 1);
348
+ } else {
349
+ layoutType = "nested";
350
+ }
351
+ if (extlessName.startsWith(LAYOUT_NAMED_PREFIX)) {
352
+ layoutName = extlessName.slice(LAYOUT_NAMED_PREFIX.length);
353
+ } else {
354
+ layoutName = "";
355
+ }
356
+ const layout = {
357
+ id: createFileId(opts.routesDir, filePath),
358
+ filePath,
359
+ dirPath,
360
+ layoutType,
361
+ layoutName
389
362
  };
390
- }
391
- const Ye = "layout", K = Ye + "-", Xe = "!";
392
- function Ze(e, t, r) {
393
- const n = r.filePath, s = [], i = e.routesDir, { layoutName: o, layoutStop: a } = _e(r.extlessName);
394
- let u = L(e, r.dirPath);
395
- if (r.extlessName === "404" && (u += r.extlessName + ".html"), !a) {
396
- let l = v(R(n)), f = !1;
397
- const p = o !== "";
398
- for (let w = 0; w < 20; w++) {
399
- let d;
400
- if (p && !f ? (d = t.find((h) => h.dirPath === l && h.layoutName === o), d && (f = !0)) : d = t.find((h) => h.dirPath === l && h.layoutName === ""), d && (s.push(d), d.layoutType === "top") || l === i)
363
+ return layout;
364
+ }
365
+ const LAYOUT_ID = "layout";
366
+ const LAYOUT_NAMED_PREFIX = LAYOUT_ID + "-";
367
+ const LAYOUT_TOP_SUFFIX = "!";
368
+ function resolveRoute(opts, appLayouts, sourceFile) {
369
+ const filePath = sourceFile.filePath;
370
+ const layouts = [];
371
+ const routesDir = opts.routesDir;
372
+ const { layoutName, layoutStop } = parseRouteIndexName(sourceFile.extlessName);
373
+ let pathname = getPathnameFromDirPath(opts, sourceFile.dirPath);
374
+ if (sourceFile.extlessName === "404") {
375
+ pathname += sourceFile.extlessName + ".html";
376
+ }
377
+ if (!layoutStop) {
378
+ let currentDir = normalizePath(dirname(filePath));
379
+ let hasFoundNamedLayout = false;
380
+ const hasNamedLayout = layoutName !== "";
381
+ for (let i = 0; i < 20; i++) {
382
+ let layout = void 0;
383
+ if (hasNamedLayout && !hasFoundNamedLayout) {
384
+ layout = appLayouts.find((l) => l.dirPath === currentDir && l.layoutName === layoutName);
385
+ if (layout) {
386
+ hasFoundNamedLayout = true;
387
+ }
388
+ } else {
389
+ layout = appLayouts.find((l) => l.dirPath === currentDir && l.layoutName === "");
390
+ }
391
+ if (layout) {
392
+ layouts.push(layout);
393
+ if (layout.layoutType === "top") {
394
+ break;
395
+ }
396
+ }
397
+ if (currentDir === routesDir) {
401
398
  break;
402
- l = v(R(l));
399
+ }
400
+ currentDir = normalizePath(dirname(currentDir));
403
401
  }
404
402
  }
405
- return {
406
- id: I(e.routesDir, n, "Route"),
407
- filePath: n,
408
- pathname: u,
409
- layouts: s.reverse(),
410
- ext: r.ext,
411
- ...G(e.basePathname, u)
403
+ const buildRoute = {
404
+ id: createFileId(opts.routesDir, filePath, "Route"),
405
+ filePath,
406
+ pathname,
407
+ layouts: layouts.reverse(),
408
+ ext: sourceFile.ext,
409
+ ...parseRoutePathname(opts.basePathname, pathname)
412
410
  };
413
- }
414
- function et(e, t) {
415
- const r = L(e, t.dirPath), n = r.slice(e.basePathname.length);
416
- return {
417
- id: I(e.routesDir, t.filePath, "Route"),
418
- filePath: t.filePath,
419
- chunkFileName: n,
420
- ...G(e.basePathname, r)
411
+ return buildRoute;
412
+ }
413
+ function resolveEntry(opts, sourceFile) {
414
+ const pathname = getPathnameFromDirPath(opts, sourceFile.dirPath);
415
+ const chunkFileName = pathname.slice(opts.basePathname.length);
416
+ const buildEntry = {
417
+ id: createFileId(opts.routesDir, sourceFile.filePath, "Route"),
418
+ filePath: sourceFile.filePath,
419
+ chunkFileName,
420
+ ...parseRoutePathname(opts.basePathname, pathname)
421
421
  };
422
- }
423
- function tt(e, t) {
424
- const n = L(e, t.dirPath) + t.extlessName + ".js", s = n.slice(e.basePathname.length);
425
- return {
426
- id: I(e.routesDir, t.filePath, "ServiceWorker"),
427
- filePath: t.filePath,
428
- chunkFileName: s,
429
- ...G(e.basePathname, n)
422
+ return buildEntry;
423
+ }
424
+ function resolveServiceWorkerEntry(opts, sourceFile) {
425
+ const dirPathname = getPathnameFromDirPath(opts, sourceFile.dirPath);
426
+ const pathname = dirPathname + sourceFile.extlessName + ".js";
427
+ const chunkFileName = pathname.slice(opts.basePathname.length);
428
+ const buildEntry = {
429
+ id: createFileId(opts.routesDir, sourceFile.filePath, "ServiceWorker"),
430
+ filePath: sourceFile.filePath,
431
+ chunkFileName,
432
+ ...parseRoutePathname(opts.basePathname, pathname)
430
433
  };
434
+ return buildEntry;
431
435
  }
432
- async function rt(e) {
433
- const t = [];
434
- return await ce(t, v(e), M(e)), t;
436
+
437
+ async function walkRoutes(routesDir) {
438
+ const sourceFiles = [];
439
+ await walkRouteDir(sourceFiles, normalizePath(routesDir), basename(routesDir));
440
+ return sourceFiles;
435
441
  }
436
- async function ce(e, t, r) {
437
- const n = await x.promises.readdir(t);
442
+ async function walkRouteDir(sourceFiles, dirPath, dirName) {
443
+ const dirItemNames = await fs.promises.readdir(dirPath);
438
444
  await Promise.all(
439
- n.map(async (s) => {
440
- const i = v(j(t, s));
441
- if ((await x.promises.stat(i)).isDirectory())
442
- await ce(e, i, s);
443
- else {
444
- const a = le(s);
445
- a !== null && e.push({
446
- ...a,
447
- fileName: s,
448
- filePath: i,
449
- dirName: r,
450
- dirPath: t
451
- });
445
+ dirItemNames.map(async (itemName) => {
446
+ const itemPath = normalizePath(join(dirPath, itemName));
447
+ const stat = await fs.promises.stat(itemPath);
448
+ if (stat.isDirectory()) {
449
+ await walkRouteDir(sourceFiles, itemPath, itemName);
450
+ } else {
451
+ const sourceFileName = getSourceFile(itemName);
452
+ if (sourceFileName !== null) {
453
+ sourceFiles.push({
454
+ ...sourceFileName,
455
+ fileName: itemName,
456
+ filePath: itemPath,
457
+ dirName,
458
+ dirPath
459
+ });
460
+ }
452
461
  }
453
462
  })
454
463
  );
455
464
  }
456
- async function nt(e) {
457
- const t = e.serverPluginsDir, r = await x.promises.readdir(t), n = [];
458
- return await Promise.all(
459
- r.map(async (s) => {
460
- const i = v(j(t, s)), o = $(s), a = S(s);
461
- (U(o) || ae(o)) && je(a) && n.push({
462
- id: I(e.serverPluginsDir, i, "Plugin"),
463
- filePath: i,
464
- ext: o
465
- });
465
+
466
+ async function walkServerPlugins(opts) {
467
+ const dirPath = opts.serverPluginsDir;
468
+ const dirItemNames = await fs.promises.readdir(dirPath);
469
+ const sourceFiles = [];
470
+ await Promise.all(
471
+ dirItemNames.map(async (itemName) => {
472
+ const itemPath = normalizePath(join(dirPath, itemName));
473
+ const ext = getExtension(itemName);
474
+ const extlessName = removeExtension(itemName);
475
+ if ((isModuleExt(ext) || isPageModuleExt(ext)) && isPluginModule(extlessName)) {
476
+ sourceFiles.push({
477
+ id: createFileId(opts.serverPluginsDir, itemPath, "Plugin"),
478
+ filePath: itemPath,
479
+ ext
480
+ });
481
+ }
466
482
  })
467
- ), n;
483
+ );
484
+ return sourceFiles;
468
485
  }
469
- async function fe(e) {
486
+
487
+ async function parseRoutesDir(ctx) {
470
488
  try {
471
- await st(e), at(e);
472
- } catch (t) {
473
- re(e, t);
474
- }
475
- for (const t of e.diagnostics) {
476
- if (t.type === "error")
477
- throw new Error(t.message);
478
- console.warn(t.message);
479
- }
480
- }
481
- async function st(e) {
482
- return e.activeBuild || (e.activeBuild = new Promise((t, r) => {
483
- nt(e.opts).then((n) => (e.serverPlugins = n, rt(e.opts.routesDir))).then((n) => {
484
- const s = Ke(e.opts, n);
485
- it(e, s), e.layouts = s.layouts, e.routes = s.routes, e.entries = s.entries, e.serviceWorkers = s.serviceWorkers, e.menus = s.menus, t();
486
- }, r).finally(() => {
487
- e.activeBuild = null;
488
- });
489
- })), e.activeBuild;
489
+ await updateRoutingContext(ctx);
490
+ validateBuild(ctx);
491
+ } catch (e) {
492
+ addError(ctx, e);
493
+ }
494
+ for (const d of ctx.diagnostics) {
495
+ if (d.type === "error") {
496
+ throw new Error(d.message);
497
+ } else {
498
+ console.warn(d.message);
499
+ }
500
+ }
490
501
  }
491
- function it(e, t) {
492
- if (!e.opts.rewriteRoutes || !t.routes)
493
- return;
494
- const r = [];
495
- let n = e.opts.rewriteRoutes.flatMap((s) => Object.keys(s.paths || {}));
496
- n = Array.from(new Set(n)), t.routes.forEach((s) => {
497
- r.push(s), (s.pathname.split("/").some(
498
- (a) => n.includes(a)
499
- ) || s.pathname === "/") && e.opts.rewriteRoutes.forEach((a, u) => {
500
- if (s.pathname === "/" && !a.prefix)
501
- return;
502
- const c = ot(s, a, u);
503
- r.some(
504
- (l) => l.pathname === c.pathname && l.routeName === c.routeName
505
- ) || r.push(c);
506
- });
507
- }), t.routes = r;
508
- }
509
- function ot(e, t, r) {
510
- const n = (k) => (t.paths || {})[k] ?? k, s = t.prefix ? "/" + t.prefix : "", i = t.prefix ? t.prefix + "/" : "", o = t.prefix?.toUpperCase().replace(/-/g, ""), a = t.prefix ? [t.prefix] : [], c = e.pathname.split("/").map(n), f = e.routeName.split("/").map(n), p = e.pattern.toString().split("\\/"), [w, ...d] = p.map(n), m = [
511
- w,
512
- ...a,
513
- ...d
514
- ].join("\\/"), g = m.substring(
502
+ function updateRoutingContext(ctx) {
503
+ ctx.activeBuild ||= _updateRoutingContext(ctx).finally(() => {
504
+ ctx.activeBuild = null;
505
+ });
506
+ return ctx.activeBuild;
507
+ }
508
+ async function _updateRoutingContext(ctx) {
509
+ const serverPlugins = await walkServerPlugins(ctx.opts);
510
+ const sourceFiles = await walkRoutes(ctx.opts.routesDir);
511
+ const resolved = resolveSourceFiles(ctx.opts, sourceFiles);
512
+ resolved.routes = rewriteRoutes(ctx, resolved.routes);
513
+ ctx.serverPlugins = serverPlugins;
514
+ ctx.layouts = resolved.layouts;
515
+ ctx.routes = resolved.routes;
516
+ ctx.entries = resolved.entries;
517
+ ctx.serviceWorkers = resolved.serviceWorkers;
518
+ ctx.menus = resolved.menus;
519
+ }
520
+ function rewriteRoutes(ctx, routes) {
521
+ if (!ctx.opts.rewriteRoutes) {
522
+ return routes;
523
+ }
524
+ const translatedRoutes = [];
525
+ let segmentsToTranslate = ctx.opts.rewriteRoutes.flatMap((rewriteConfig) => {
526
+ return Object.keys(rewriteConfig.paths || {});
527
+ });
528
+ segmentsToTranslate = Array.from(new Set(segmentsToTranslate));
529
+ routes.forEach((route) => {
530
+ translatedRoutes.push(route);
531
+ const currentRouteSegments = route.pathname.split("/");
532
+ const foundSegmentToTranslate = currentRouteSegments.some(
533
+ (segment) => segmentsToTranslate.includes(segment)
534
+ );
535
+ if (foundSegmentToTranslate || route.pathname === "/") {
536
+ ctx.opts.rewriteRoutes.forEach((config, configIndex) => {
537
+ if (route.pathname === "/" && !config.prefix) {
538
+ return;
539
+ }
540
+ const routeToPush = translateRoute(route, config, configIndex);
541
+ if (!translatedRoutes.some(
542
+ (item) => item.pathname === routeToPush.pathname && item.routeName === routeToPush.routeName
543
+ )) {
544
+ translatedRoutes.push(routeToPush);
545
+ }
546
+ });
547
+ }
548
+ });
549
+ return translatedRoutes.sort(routeSortCompare);
550
+ }
551
+ function translateRoute(route, config, configIndex) {
552
+ const replacePath = (part) => (config.paths || {})[part] ?? part;
553
+ const pathnamePrefix = config.prefix ? "/" + config.prefix : "";
554
+ const routeNamePrefix = config.prefix ? config.prefix + "/" : "";
555
+ const idSuffix = config.prefix?.toUpperCase().replace(/-/g, "");
556
+ const patternInfix = config.prefix ? [config.prefix] : [];
557
+ const splittedPathName = route.pathname.split("/");
558
+ const translatedPathParts = splittedPathName.map(replacePath);
559
+ const splittedRouteName = route.routeName.split("/");
560
+ const translatedRouteParts = splittedRouteName.map(replacePath);
561
+ const splittedPattern = route.pattern.toString().split("\\/");
562
+ const [translatedPatternFirst, ...translatedPatternOthers] = splittedPattern.map(replacePath);
563
+ const translatedPatternParts = [
564
+ translatedPatternFirst,
565
+ ...patternInfix,
566
+ ...translatedPatternOthers
567
+ ];
568
+ const translatedPatternString = translatedPatternParts.join("\\/");
569
+ const translatedRegExp = translatedPatternString.substring(
515
570
  1,
516
- e.pathname === "/" ? m.length - 1 : m.length - 2
517
- ), b = e.segments.map(
518
- (k) => k.map((A) => ({ ...A, content: n(A.content) }))
571
+ route.pathname === "/" ? translatedPatternString.length - 1 : translatedPatternString.length - 2
519
572
  );
520
- t.prefix && b.splice(0, 0, [
521
- {
522
- content: t.prefix,
523
- dynamic: !1,
524
- rest: !1
525
- }
526
- ]);
527
- const P = c.join("/"), y = f.join("/");
528
- return {
529
- ...e,
530
- id: e.id + (o || r),
531
- pathname: s + P,
532
- routeName: i + (y !== "/" ? y : ""),
533
- pattern: new RegExp(g),
534
- segments: b
573
+ const translatedSegments = route.segments.map(
574
+ (segment) => segment.map((item) => ({ ...item, content: replacePath(item.content) }))
575
+ );
576
+ if (config.prefix) {
577
+ translatedSegments.splice(0, 0, [
578
+ {
579
+ content: config.prefix,
580
+ dynamic: false,
581
+ rest: false
582
+ }
583
+ ]);
584
+ }
585
+ const translatedPath = translatedPathParts.join("/");
586
+ const translatedRoute = translatedRouteParts.join("/");
587
+ const routeToPush = {
588
+ ...route,
589
+ id: route.id + (idSuffix || configIndex),
590
+ pathname: pathnamePrefix + translatedPath,
591
+ routeName: routeNamePrefix + (translatedRoute !== "/" ? translatedRoute : ""),
592
+ pattern: new RegExp(translatedRegExp),
593
+ segments: translatedSegments
535
594
  };
536
- }
537
- function at(e) {
538
- const t = Array.from(new Set(e.routes.map((r) => r.pathname))).sort();
539
- for (const r of t) {
540
- const n = e.routes.filter((s) => s.pathname === r);
541
- n.length > 1 && re(
542
- e,
543
- `More than one route has been found for pathname "${r}". Please narrow it down to only one of these:
544
- ${n.map((s) => ` - ${s.filePath}`).join(`
545
- `)}`
546
- );
595
+ return routeToPush;
596
+ }
597
+ function validateBuild(ctx) {
598
+ const pathnames = Array.from(new Set(ctx.routes.map((r) => r.pathname))).sort();
599
+ for (const pathname of pathnames) {
600
+ const foundRoutes = ctx.routes.filter((r) => r.pathname === pathname);
601
+ if (foundRoutes.length > 1) {
602
+ addError(
603
+ ctx,
604
+ `More than one route has been found for pathname "${pathname}". Please narrow it down to only one of these:
605
+ ${foundRoutes.map((r) => ` - ${r.filePath}`).join("\n")}`
606
+ );
607
+ }
547
608
  }
548
- e.layouts.filter((r) => r.layoutType === "top").forEach((r) => {
549
- Re(
550
- e,
551
- `The "top" layout feature, which is used by "${r.filePath}" has been deprecated and will be removed from future versions. In most cases the "group" layout feature can be used in its place: https://qwik.dev/docs/advanced/routing/`
609
+ ctx.layouts.filter((l) => l.layoutType === "top").forEach((l) => {
610
+ addWarning(
611
+ ctx,
612
+ `The "top" layout feature, which is used by "${l.filePath}" has been deprecated and will be removed from future versions. In most cases the "group" layout feature can be used in its place: https://qwik.dev/docs/advanced/routing/`
552
613
  );
553
614
  });
554
615
  }
555
- function ut(e, t, r, n, s) {
556
- return {
557
- rootDir: v(e),
558
- opts: ct(e, t, r),
616
+
617
+ function createBuildContext(rootDir, viteBasePath, userOpts, target, dynamicImports) {
618
+ const ctx = {
619
+ rootDir: normalizePath(rootDir),
620
+ opts: normalizeOptions(rootDir, viteBasePath, userOpts),
559
621
  routes: [],
560
622
  serverPlugins: [],
561
623
  layouts: [],
@@ -564,57 +626,106 @@ function ut(e, t, r, n, s) {
564
626
  menus: [],
565
627
  diagnostics: [],
566
628
  frontmatter: /* @__PURE__ */ new Map(),
567
- target: n || "ssr",
568
- dynamicImports: n === "client" || !!s,
569
- isDirty: !0,
629
+ target: target || "ssr",
630
+ dynamicImports: target === "client" || !!dynamicImports,
631
+ isDirty: true,
570
632
  activeBuild: null
571
633
  };
634
+ return ctx;
635
+ }
636
+ function resetBuildContext(ctx) {
637
+ if (ctx) {
638
+ ctx.routes.length = 0;
639
+ ctx.layouts.length = 0;
640
+ ctx.entries.length = 0;
641
+ ctx.menus.length = 0;
642
+ ctx.diagnostics.length = 0;
643
+ ctx.frontmatter.clear();
644
+ ctx.isDirty = true;
645
+ }
572
646
  }
573
- function lt(e) {
574
- e && (e.routes.length = 0, e.layouts.length = 0, e.entries.length = 0, e.menus.length = 0, e.diagnostics.length = 0, e.frontmatter.clear(), e.isDirty = !0);
575
- }
576
- function ct(e, t, r) {
577
- t.startsWith("/") && t.endsWith("/") || (console.error(
578
- "warning: vite's config.base must begin and end with /. This will be an error in v2. If you have a valid use case, please open an issue."
579
- ), t.endsWith("/") || (t += "/"));
580
- const n = { ...r };
581
- typeof n.routesDir != "string" ? n.routesDir = T(e, "src", "routes") : q(n.routesDir) || (n.routesDir = T(e, n.routesDir)), n.routesDir = v(n.routesDir), typeof n.serverPluginsDir != "string" ? n.serverPluginsDir = n.routesDir : q(n.serverPluginsDir) || (n.serverPluginsDir = T(e, n.serverPluginsDir)), n.serverPluginsDir = v(n.serverPluginsDir), typeof n.baseUrl == "string" && (n.basePathname = n.baseUrl), typeof n.basePathname != "string" && (n.basePathname = t), n.basePathname.endsWith("/") || (console.error(
582
- "Warning: qwik-router plugin basePathname must end with /. This will be an error in v2"
583
- ), n.basePathname += "/");
584
- const s = new URL(n.basePathname, "https://qwik.dev/");
585
- return n.basePathname = s.pathname, n.mdx = n.mdx || {}, n.platform = n.platform || {}, n;
586
- }
587
- function ft(e) {
588
- return (t, r) => {
589
- const n = {};
590
- E(t, "yaml", (s) => {
591
- const i = dt(s.value);
592
- for (const o in i)
593
- n[o] = i[o];
594
- }), Object.keys(n).length > 0 && e.frontmatter.set(v(r.path), n);
647
+ function normalizeOptions(rootDir, viteBasePath, userOpts) {
648
+ if (!(viteBasePath.startsWith("/") && viteBasePath.endsWith("/"))) {
649
+ console.error(
650
+ `warning: vite's config.base must begin and end with /. This will be an error in v2. If you have a valid use case, please open an issue.`
651
+ );
652
+ if (!viteBasePath.endsWith("/")) {
653
+ viteBasePath += "/";
654
+ }
655
+ }
656
+ const opts = { ...userOpts };
657
+ if (typeof opts.routesDir !== "string") {
658
+ opts.routesDir = resolve(rootDir, "src", "routes");
659
+ } else if (!isAbsolute(opts.routesDir)) {
660
+ opts.routesDir = resolve(rootDir, opts.routesDir);
661
+ }
662
+ opts.routesDir = normalizePath(opts.routesDir);
663
+ if (typeof opts.serverPluginsDir !== "string") {
664
+ opts.serverPluginsDir = opts.routesDir;
665
+ } else if (!isAbsolute(opts.serverPluginsDir)) {
666
+ opts.serverPluginsDir = resolve(rootDir, opts.serverPluginsDir);
667
+ }
668
+ opts.serverPluginsDir = normalizePath(opts.serverPluginsDir);
669
+ if (typeof opts.baseUrl === "string") {
670
+ opts.basePathname = opts.baseUrl;
671
+ }
672
+ if (typeof opts.basePathname !== "string") {
673
+ opts.basePathname = viteBasePath;
674
+ }
675
+ if (!opts.basePathname.endsWith("/")) {
676
+ console.error(
677
+ `Warning: qwik-router plugin basePathname must end with /. This will be an error in v2`
678
+ );
679
+ opts.basePathname += "/";
680
+ }
681
+ const url = new URL(opts.basePathname, "https://qwik.dev/");
682
+ opts.basePathname = url.pathname;
683
+ opts.mdx = opts.mdx || {};
684
+ opts.platform = opts.platform || {};
685
+ return opts;
686
+ }
687
+
688
+ function parseFrontmatter(ctx) {
689
+ return (mdast, vfile) => {
690
+ const attrs = {};
691
+ visit(mdast, "yaml", (node) => {
692
+ const parsedAttrs = parseFrontmatterAttrs(node.value);
693
+ for (const k in parsedAttrs) {
694
+ attrs[k] = parsedAttrs[k];
695
+ }
696
+ });
697
+ if (Object.keys(attrs).length > 0) {
698
+ ctx.frontmatter.set(normalizePath(vfile.path), attrs);
699
+ }
595
700
  };
596
701
  }
597
- function dt(e) {
598
- return typeof e == "string" && (e = e.trim(), e !== "") ? Pe(e) : null;
599
- }
600
- const ht = {
601
- author: !0,
602
- creator: !0,
603
- "color-scheme": !0,
604
- description: !0,
605
- generator: !0,
606
- keywords: !0,
607
- publisher: !0,
608
- referrer: !0,
609
- robots: !0,
610
- "theme-color": !0,
611
- viewport: !0
702
+ function parseFrontmatterAttrs(yaml) {
703
+ if (typeof yaml === "string") {
704
+ yaml = yaml.trim();
705
+ if (yaml !== "") {
706
+ return parse(yaml);
707
+ }
708
+ }
709
+ return null;
710
+ }
711
+ const metaNames = {
712
+ author: true,
713
+ creator: true,
714
+ "color-scheme": true,
715
+ description: true,
716
+ generator: true,
717
+ keywords: true,
718
+ publisher: true,
719
+ referrer: true,
720
+ robots: true,
721
+ "theme-color": true,
722
+ viewport: true
612
723
  };
613
- function pt(e) {
614
- if (e != null && typeof e == "object") {
615
- const t = Object.keys(e);
616
- if (t.length > 0) {
617
- const r = {
724
+ function frontmatterAttrsToDocumentHead(attrs) {
725
+ if (attrs != null && typeof attrs === "object") {
726
+ const attrNames = Object.keys(attrs);
727
+ if (attrNames.length > 0) {
728
+ const head = {
618
729
  title: "",
619
730
  meta: [],
620
731
  styles: [],
@@ -622,108 +733,153 @@ function pt(e) {
622
733
  scripts: [],
623
734
  frontmatter: {}
624
735
  };
625
- for (const n of t) {
626
- const s = e[n];
627
- if (s != null)
628
- if (n === "title")
629
- r.title = s.toString(), r.title = r.title.replace(/\\@/g, "@");
630
- else if (n === "og" || n === "opengraph") {
631
- if (typeof s == "object") {
632
- for (const i of Array.isArray(s) ? s : [s])
633
- if (i != null && typeof i == "object" && !Array.isArray(i))
634
- for (const [o, a] of Object.entries(i))
635
- (o === "title" || o === "description") && a === !0 ? t.includes(o) && r.meta.push({
636
- property: `og:${o}`,
637
- content: e[o]?.toString()
638
- }) : r.meta.push({
639
- property: `og:${o}`,
640
- content: a?.toString()
641
- });
736
+ for (const attrName of attrNames) {
737
+ const attrValue = attrs[attrName];
738
+ if (attrValue != null) {
739
+ if (attrName === "title") {
740
+ head.title = attrValue.toString();
741
+ head.title = head.title.replace(/\\@/g, "@");
742
+ } else if (attrName === "og" || attrName === "opengraph") {
743
+ if (typeof attrValue === "object") {
744
+ for (const opengraph of Array.isArray(attrValue) ? attrValue : [attrValue]) {
745
+ if (opengraph != null && typeof opengraph === "object" && !Array.isArray(opengraph)) {
746
+ for (const [property, content] of Object.entries(opengraph)) {
747
+ if ((property === "title" || property === "description") && content === true) {
748
+ if (attrNames.includes(property)) {
749
+ head.meta.push({
750
+ property: `og:${property}`,
751
+ content: attrs[property]?.toString()
752
+ });
753
+ }
754
+ } else {
755
+ head.meta.push({
756
+ property: `og:${property}`,
757
+ content: content?.toString()
758
+ });
759
+ }
760
+ }
761
+ }
762
+ }
642
763
  }
643
- } else ht[n] ? r.meta.push({
644
- name: n,
645
- content: s.toString()
646
- }) : r.frontmatter[n] = s;
764
+ } else if (metaNames[attrName]) {
765
+ head.meta.push({
766
+ name: attrName,
767
+ content: attrValue.toString()
768
+ });
769
+ } else {
770
+ head.frontmatter[attrName] = attrValue;
771
+ }
772
+ }
647
773
  }
648
- return r;
774
+ return head;
649
775
  }
650
776
  }
651
777
  return null;
652
778
  }
653
- function mt() {
654
- return (e) => {
655
- const t = e, r = new xe();
656
- E(t, "element", (n) => {
657
- if (te(n) && n.properties) {
658
- const i = B(n);
659
- de(n, "id") || (n.properties.id = r.slug(i));
779
+
780
+ function rehypeSlug() {
781
+ return (ast) => {
782
+ const mdast = ast;
783
+ const slugs = new Slugger();
784
+ visit(mdast, "element", (node) => {
785
+ const level = headingRank(node);
786
+ if (level && node.properties) {
787
+ const text = toString(node);
788
+ if (!hasProperty(node, "id")) {
789
+ node.properties.id = slugs.slug(text);
790
+ }
660
791
  }
661
792
  });
662
793
  };
663
794
  }
664
- function gt(e) {
665
- return (t, r) => {
666
- const n = t, s = v(r.path);
667
- vt(n, e.opts, s), bt(e, n, s), Pt(e, n, s), xt(n);
795
+ function rehypePage(ctx) {
796
+ return (ast, vfile) => {
797
+ const mdast = ast;
798
+ const sourcePath = normalizePath(vfile.path);
799
+ updateContentLinks(mdast, ctx.opts, sourcePath);
800
+ exportFrontmatter(ctx, mdast, sourcePath);
801
+ exportContentHead(ctx, mdast, sourcePath);
802
+ exportContentHeadings(mdast);
668
803
  };
669
804
  }
670
- function yt() {
671
- return (e) => {
672
- E(e, "element", (r) => {
673
- r.properties && r.properties.className && (r.properties.class = r.properties.className, r.properties.className = void 0);
805
+ function renameClassname() {
806
+ return (ast) => {
807
+ const mdast = ast;
808
+ visit(mdast, "element", (node) => {
809
+ if (node.properties) {
810
+ if (node.properties.className) {
811
+ node.properties.class = node.properties.className;
812
+ node.properties.className = void 0;
813
+ }
814
+ }
674
815
  });
675
816
  };
676
817
  }
677
- function wt() {
678
- return (e) => {
679
- E(e, "element", (r) => {
680
- if (r.tagName === "table" && !r.done) {
681
- const n = { ...r };
682
- n.done = !0, r.tagName = "div", r.properties = { className: "table-wrapper" }, r.children = [n];
818
+ function wrapTableWithDiv() {
819
+ return (ast) => {
820
+ const mdast = ast;
821
+ visit(mdast, "element", (node) => {
822
+ if (node.tagName === "table" && !node.done) {
823
+ const table = { ...node };
824
+ table.done = true;
825
+ node.tagName = "div";
826
+ node.properties = { className: "table-wrapper" };
827
+ node.children = [table];
683
828
  }
684
829
  });
685
830
  };
686
831
  }
687
- function vt(e, t, r) {
688
- E(e, "element", (n) => {
689
- if ((n && n.type === "element" && n.tagName.toLowerCase()) === "a") {
690
- const i = (n.properties && n.properties.href || "").trim();
691
- if (se(i)) {
692
- const o = $(i);
693
- J(o) && (n.properties.href = C(
694
- t,
695
- r,
696
- n.properties.href,
697
- !0
698
- ));
832
+ function updateContentLinks(mdast, opts, sourcePath) {
833
+ visit(mdast, "element", (node) => {
834
+ const tagName = node && node.type === "element" && node.tagName.toLowerCase();
835
+ if (tagName === "a") {
836
+ const href = (node.properties && node.properties.href || "").trim();
837
+ if (isSameOriginUrl(href)) {
838
+ const ext = getExtension(href);
839
+ if (isMarkdownExt(ext)) {
840
+ node.properties.href = getMarkdownRelativeUrl(
841
+ opts,
842
+ sourcePath,
843
+ node.properties.href,
844
+ true
845
+ );
846
+ }
699
847
  }
700
848
  }
701
849
  });
702
850
  }
703
- function bt(e, t, r) {
704
- const n = e.frontmatter.get(r);
705
- z(t, "frontmatter", n);
706
- }
707
- function Pt(e, t, r) {
708
- const n = e.frontmatter.get(r), s = pt(n);
709
- s && z(t, "head", s);
710
- }
711
- function xt(e) {
712
- const t = [];
713
- E(e, "element", (r) => {
714
- const n = te(r);
715
- if (n && r.properties && de(r, "id")) {
716
- const s = B(r);
717
- t.push({
718
- text: s,
719
- id: r.properties.id,
720
- level: n
721
- });
851
+ function exportFrontmatter(ctx, mdast, sourcePath) {
852
+ const attrs = ctx.frontmatter.get(sourcePath);
853
+ createExport(mdast, "frontmatter", attrs);
854
+ }
855
+ function exportContentHead(ctx, mdast, sourcePath) {
856
+ const attrs = ctx.frontmatter.get(sourcePath);
857
+ const head = frontmatterAttrsToDocumentHead(attrs);
858
+ if (head) {
859
+ createExport(mdast, "head", head);
860
+ }
861
+ }
862
+ function exportContentHeadings(mdast) {
863
+ const headings = [];
864
+ visit(mdast, "element", (node) => {
865
+ const level = headingRank(node);
866
+ if (level && node.properties) {
867
+ if (hasProperty(node, "id")) {
868
+ const text = toString(node);
869
+ headings.push({
870
+ text,
871
+ id: node.properties.id,
872
+ level
873
+ });
874
+ }
722
875
  }
723
- }), t.length > 0 && z(e, "headings", t);
876
+ });
877
+ if (headings.length > 0) {
878
+ createExport(mdast, "headings", headings);
879
+ }
724
880
  }
725
- function z(e, t, r) {
726
- const n = {
881
+ function createExport(mdast, identifierName, val) {
882
+ const mdxjsEsm = {
727
883
  type: "mdxjsEsm",
728
884
  value: "",
729
885
  data: {
@@ -742,8 +898,8 @@ function z(e, t, r) {
742
898
  declarations: [
743
899
  {
744
900
  type: "VariableDeclarator",
745
- id: { type: "Identifier", name: t },
746
- init: Se(r)
901
+ id: { type: "Identifier", name: identifierName },
902
+ init: valueToEstree(val)
747
903
  }
748
904
  ]
749
905
  }
@@ -752,393 +908,431 @@ function z(e, t, r) {
752
908
  }
753
909
  }
754
910
  };
755
- e.children.unshift(n);
756
- }
757
- const St = {}.hasOwnProperty;
758
- function de(e, t) {
759
- const r = e && typeof e == "object" && e.type === "element" && e.properties && St.call(e.properties, t) && e.properties[t];
760
- return r != null && r !== !1;
761
- }
762
- function kt() {
763
- return F.register(ke), async (e) => {
764
- E(e, "element", (t, r, n) => {
765
- if (!(!n || n.tagName !== "pre" || t.tagName !== "code" || !Array.isArray(t.properties.className)))
766
- for (let s = 0; s < t.properties.className.length; s++) {
767
- const i = t.properties.className[s], o = It(i);
768
- if (o && F.registered(o)) {
769
- t.properties.className[s] = "language-" + o, Et(t, o);
770
- return;
771
- }
911
+ mdast.children.unshift(mdxjsEsm);
912
+ }
913
+ const own = {}.hasOwnProperty;
914
+ function hasProperty(node, propName) {
915
+ const value = node && typeof node === "object" && node.type === "element" && node.properties && own.call(node.properties, propName) && node.properties[propName];
916
+ return value != null && value !== false;
917
+ }
918
+
919
+ function rehypeSyntaxHighlight() {
920
+ refractor.register(tsxLang);
921
+ return async (ast) => {
922
+ visit(ast, "element", (node, _index, parent) => {
923
+ if (!parent || parent.tagName !== "pre" || node.tagName !== "code" || !Array.isArray(node.properties.className)) {
924
+ return;
925
+ }
926
+ for (let i = 0; i < node.properties.className.length; i++) {
927
+ const className = node.properties.className[i];
928
+ const lang = getLanguage(className);
929
+ if (lang && refractor.registered(lang)) {
930
+ node.properties.className[i] = "language-" + lang;
931
+ syntaxHighlight(node, lang);
932
+ return;
772
933
  }
934
+ }
773
935
  });
774
936
  };
775
937
  }
776
- function Et(e, t) {
777
- const r = B(e), n = F.highlight(r, t);
778
- n && Array.isArray(e.children) && (e.children = n.children);
938
+ function syntaxHighlight(node, lang) {
939
+ const code = toString(node);
940
+ const result = refractor.highlight(code, lang);
941
+ if (result && Array.isArray(node.children)) {
942
+ node.children = result.children;
943
+ }
779
944
  }
780
- function It(e) {
781
- return typeof e == "string" && (e = e.toLowerCase(), e.startsWith("language-")) ? e.slice(9) : null;
945
+ function getLanguage(className) {
946
+ if (typeof className === "string") {
947
+ className = className.toLowerCase();
948
+ if (className.startsWith("language-")) {
949
+ return className.slice(9);
950
+ }
951
+ }
952
+ return null;
782
953
  }
783
- async function Nt(e) {
784
- const { compile: t } = await import("@mdx-js/mdx"), { default: r } = await import("remark-frontmatter"), { default: n } = await import("remark-gfm"), { default: s } = await import("rehype-autolink-headings"), { VFile: i } = await import("vfile"), o = e.opts.mdx, a = o.remarkPlugins || [], u = o.rehypePlugins || [], c = e.opts.mdxPlugins, l = [];
785
- (typeof c?.remarkGfm > "u" || c.remarkGfm) && l.push(n);
786
- const f = [];
787
- (typeof c?.rehypeSyntaxHighlight > "u" || c.rehypeSyntaxHighlight) && f.push(kt), (typeof c?.rehypeAutolinkHeadings > "u" || c.rehypeAutolinkHeadings) && f.push(s);
788
- const p = {
789
- SourceMapGenerator: be,
954
+
955
+ async function createMdxTransformer(ctx) {
956
+ const { compile } = await import('@mdx-js/mdx');
957
+ const { default: remarkFrontmatter } = await import('remark-frontmatter');
958
+ const { default: remarkGfm } = await import('remark-gfm');
959
+ const { default: rehypeAutolinkHeadings } = await import('rehype-autolink-headings');
960
+ const { VFile } = await import('vfile');
961
+ const userMdxOpts = ctx.opts.mdx;
962
+ const userRemarkPlugins = userMdxOpts.remarkPlugins || [];
963
+ const userRehypePlugins = userMdxOpts.rehypePlugins || [];
964
+ const coreMdxPlugins = ctx.opts.mdxPlugins;
965
+ const coreRemarkPlugins = [];
966
+ if (typeof coreMdxPlugins?.remarkGfm === "undefined" || coreMdxPlugins.remarkGfm) {
967
+ coreRemarkPlugins.push(remarkGfm);
968
+ }
969
+ const coreRehypePlugins = [];
970
+ if (typeof coreMdxPlugins?.rehypeSyntaxHighlight === "undefined" || coreMdxPlugins.rehypeSyntaxHighlight) {
971
+ coreRehypePlugins.push(rehypeSyntaxHighlight);
972
+ }
973
+ if (typeof coreMdxPlugins?.rehypeAutolinkHeadings === "undefined" || coreMdxPlugins.rehypeAutolinkHeadings) {
974
+ coreRehypePlugins.push(rehypeAutolinkHeadings);
975
+ }
976
+ const options = {
977
+ SourceMapGenerator,
790
978
  jsxImportSource: "@qwik.dev/core",
791
- ...o,
979
+ ...userMdxOpts,
792
980
  elementAttributeNameCase: "html",
793
981
  remarkPlugins: [
794
- ...a,
795
- ...l,
796
- r,
797
- [ft, e]
982
+ ...userRemarkPlugins,
983
+ ...coreRemarkPlugins,
984
+ remarkFrontmatter,
985
+ [parseFrontmatter, ctx]
798
986
  ],
799
987
  rehypePlugins: [
800
- mt,
801
- ...u,
802
- ...f,
803
- [gt, e],
804
- yt,
805
- wt
988
+ rehypeSlug,
989
+ ...userRehypePlugins,
990
+ ...coreRehypePlugins,
991
+ [rehypePage, ctx],
992
+ renameClassname,
993
+ wrapTableWithDiv
806
994
  ]
807
995
  };
808
- return async function(w, d) {
809
- const h = $(d);
810
- if ([".mdx", ".md", ".markdown"].includes(h)) {
811
- const m = new i({ value: w, path: d }), g = await t(m, p), b = String(g.value), P = `import { jsx } from '@qwik.dev/core';
812
- `, y = `
996
+ return async function(code, id) {
997
+ const ext = getExtension(id);
998
+ if ([".mdx", ".md", ".markdown"].includes(ext)) {
999
+ const file = new VFile({ value: code, path: id });
1000
+ const compiled = await compile(file, options);
1001
+ const output = String(compiled.value);
1002
+ const addImport = `import { jsx } from '@qwik.dev/core';
1003
+ `;
1004
+ const newDefault = `
813
1005
  function _missingMdxReference(id, component, place) {
814
- throw new Error("${d}: Expected " + (component ? "component" : "object") + " \`" + id + "\` to be defined: you likely forgot to import, pass, or provide it." + (place ? "\\nIt’s referenced in your code at \`" + place + "\`" : ""));
1006
+ throw new Error("${id}: Expected " + (component ? "component" : "object") + " \`" + id + "\` to be defined: you likely forgot to import, pass, or provide it." + (place ? "\\nIt’s referenced in your code at \`" + place + "\`" : ""));
815
1007
  }
816
1008
  const WrappedMdxContent = () => {
817
1009
  const content = _createMdxContent({});
818
1010
  return typeof MDXLayout === 'function' ? jsx(MDXLayout, {children: content}) : content;
819
1011
  };
820
1012
  export default WrappedMdxContent;
821
- `, _ = b.lastIndexOf("export default ");
822
- if (_ === -1)
1013
+ `;
1014
+ const exportIndex = output.lastIndexOf("export default ");
1015
+ if (exportIndex === -1) {
823
1016
  throw new Error("Could not find default export in mdx output");
1017
+ }
1018
+ const wrappedOutput = addImport + output.slice(0, exportIndex) + newDefault;
824
1019
  return {
825
- code: P + b.slice(0, _) + y,
826
- map: g.map
1020
+ code: wrappedOutput,
1021
+ map: compiled.map
827
1022
  };
828
1023
  }
829
1024
  };
830
1025
  }
831
- function Rt(e, t) {
832
- const r = e.target === "client", n = [...e.entries, ...e.serviceWorkers];
833
- r && n.length > 0 && (t.push(`
834
- /** Qwik Router Entries Entry */`), t.push(`export const e = () => import("@qwik-router-entries");
835
- `));
836
- }
837
- function $t(e) {
838
- const t = [], r = [...e.entries, ...e.serviceWorkers];
839
- t.push(`
840
- /** Qwik Router Entries (${r.length}) */`);
841
- for (let n = 0; n < r.length; n++) {
842
- const s = r[n];
843
- t.push(`export const ${s.id} = () => import(${JSON.stringify(s.filePath)});`);
844
- }
845
- return t.join(`
846
- `) + `
847
- `;
1026
+
1027
+ function createEntries(ctx, c) {
1028
+ const isClient = ctx.target === "client";
1029
+ const entries = [...ctx.entries, ...ctx.serviceWorkers];
1030
+ if (isClient && entries.length > 0) {
1031
+ c.push(`
1032
+ /** Qwik Router Entries Entry */`);
1033
+ c.push(`export const e = () => import("@qwik-router-entries");
1034
+ `);
1035
+ }
848
1036
  }
849
- function W(e) {
850
- const t = e.toLowerCase();
851
- return t.endsWith(".tsx") || t.endsWith(".jsx") ? e.slice(0, e.length - 4) : t.endsWith(".ts") ? e.slice(0, e.length - 3) : e;
852
- }
853
- function _t(e, t, r, n) {
854
- t.push(`
855
- /** Qwik Router Menus (${e.menus.length}) */`), t.push("export const menus = [");
856
- const s = !n, i = e.opts.routesDir;
857
- for (const o of e.menus) {
858
- const a = JSON.stringify(W(o.filePath));
859
- if (s)
860
- t.push(` [${JSON.stringify(o.pathname)}, ()=>import(${a})],`);
861
- else {
862
- const u = I(i, o.filePath);
863
- r.push(`import * as ${u} from ${a};`), t.push(` [${JSON.stringify(o.pathname)}, ()=>${u}],`);
864
- }
1037
+ function generateQwikRouterEntries(ctx) {
1038
+ const c = [];
1039
+ const entries = [...ctx.entries, ...ctx.serviceWorkers];
1040
+ c.push(`
1041
+ /** Qwik Router Entries (${entries.length}) */`);
1042
+ for (let i = 0; i < entries.length; i++) {
1043
+ const entry = entries[i];
1044
+ c.push(`export const ${entry.id} = () => import(${JSON.stringify(entry.filePath)});`);
865
1045
  }
866
- t.push("];");
1046
+ return c.join("\n") + "\n";
867
1047
  }
868
- function Dt(e, t, r, n, s) {
869
- const i = s, o = e.dynamicImports;
870
- if (e.layouts.length > 0) {
871
- r.push(`
872
- /** Qwik Router Layouts (${e.layouts.length}) */`);
873
- for (const a of e.layouts) {
874
- const u = JSON.stringify(W(a.filePath));
875
- o ? r.push(`const ${a.id} = ()=>import(${u});`) : (n.push(`import * as ${a.id}_ from ${u};`), r.push(`const ${a.id} = ()=>${a.id}_;`));
1048
+
1049
+ function getImportPath(importPath) {
1050
+ const lowerCasePath = importPath.toLowerCase();
1051
+ if (lowerCasePath.endsWith(".tsx") || lowerCasePath.endsWith(".jsx")) {
1052
+ return importPath.slice(0, importPath.length - 4);
1053
+ }
1054
+ if (lowerCasePath.endsWith(".ts")) {
1055
+ return importPath.slice(0, importPath.length - 3);
1056
+ }
1057
+ return importPath;
1058
+ }
1059
+
1060
+ function createMenus(ctx, c, esmImports, isSSR) {
1061
+ c.push(`
1062
+ /** Qwik Router Menus (${ctx.menus.length}) */`);
1063
+ c.push(`export const menus = [`);
1064
+ const dynamicImports = !isSSR;
1065
+ const routesDir = ctx.opts.routesDir;
1066
+ for (const m of ctx.menus) {
1067
+ const importPath = JSON.stringify(getImportPath(m.filePath));
1068
+ if (dynamicImports) {
1069
+ c.push(` [${JSON.stringify(m.pathname)}, ()=>import(${importPath})],`);
1070
+ } else {
1071
+ const id = createFileId(routesDir, m.filePath);
1072
+ esmImports.push(`import * as ${id} from ${importPath};`);
1073
+ c.push(` [${JSON.stringify(m.pathname)}, ()=>${id}],`);
1074
+ }
1075
+ }
1076
+ c.push(`];`);
1077
+ }
1078
+
1079
+ function createRoutes(ctx, qwikPlugin, c, esmImports, isSSR) {
1080
+ const includeEndpoints = isSSR;
1081
+ const dynamicImports = ctx.dynamicImports;
1082
+ if (ctx.layouts.length > 0) {
1083
+ c.push(`
1084
+ /** Qwik Router Layouts (${ctx.layouts.length}) */`);
1085
+ for (const layout of ctx.layouts) {
1086
+ const importPath = JSON.stringify(getImportPath(layout.filePath));
1087
+ if (dynamicImports) {
1088
+ c.push(`const ${layout.id} = ()=>import(${importPath});`);
1089
+ } else {
1090
+ esmImports.push(`import * as ${layout.id}_ from ${importPath};`);
1091
+ c.push(`const ${layout.id} = ()=>${layout.id}_;`);
1092
+ }
876
1093
  }
877
1094
  }
878
- r.push(`
879
- /** Qwik Router Routes (${e.routes.length}) */`), r.push("export const routes = [");
880
- for (const a of e.routes) {
881
- const u = [];
882
- if (Ce(a.ext)) {
883
- for (const l of a.layouts)
884
- u.push(l.id);
885
- const c = W(a.filePath);
886
- o ? u.push(`()=>import(${JSON.stringify(c)})`) : (n.push(`import * as ${a.id} from ${JSON.stringify(c)};`), u.push(`()=>${a.id}`));
887
- } else if (i && U(a.ext)) {
888
- const c = W(a.filePath);
889
- n.push(`import * as ${a.id} from ${JSON.stringify(c)};`);
890
- for (const l of a.layouts)
891
- u.push(l.id);
892
- u.push(`()=>${a.id}`);
1095
+ c.push(`
1096
+ /** Qwik Router Routes (${ctx.routes.length}) */`);
1097
+ c.push(`export const routes = [`);
1098
+ for (const route of ctx.routes) {
1099
+ const layouts = [];
1100
+ if (isPageExt(route.ext)) {
1101
+ for (const layout of route.layouts) {
1102
+ layouts.push(layout.id);
1103
+ }
1104
+ const importPath = getImportPath(route.filePath);
1105
+ if (dynamicImports) {
1106
+ layouts.push(`()=>import(${JSON.stringify(importPath)})`);
1107
+ } else {
1108
+ esmImports.push(`import * as ${route.id} from ${JSON.stringify(importPath)};`);
1109
+ layouts.push(`()=>${route.id}`);
1110
+ }
1111
+ } else if (includeEndpoints && isModuleExt(route.ext)) {
1112
+ const importPath = getImportPath(route.filePath);
1113
+ esmImports.push(`import * as ${route.id} from ${JSON.stringify(importPath)};`);
1114
+ for (const layout of route.layouts) {
1115
+ layouts.push(layout.id);
1116
+ }
1117
+ layouts.push(`()=>${route.id}`);
893
1118
  }
894
- u.length > 0 && r.push(` ${Ot(t, a, u, s)},`);
895
- }
896
- r.push("];");
897
- }
898
- function Ot(e, t, r, n) {
899
- const s = JSON.stringify(t.routeName), i = `[ ${r.join(", ")} ]`;
900
- if (n) {
901
- const o = JSON.stringify(t.pathname), a = JSON.stringify(Tt(e, t));
902
- return `[ ${s}, ${i}, ${o}, ${a} ]`;
903
- }
904
- return `[ ${s}, ${i} ]`;
905
- }
906
- function Tt(e, t) {
907
- const r = [], n = e.api.getManifest();
908
- if (n) {
909
- const s = Object.keys(n.bundles), i = (o) => {
910
- o = S(o);
911
- for (const a of s) {
912
- const u = n.bundles[a];
913
- if (u.origins)
914
- for (const c of u.origins) {
915
- const l = S(c);
916
- o.endsWith(l) && (r.includes(a) || r.push(a));
1119
+ if (layouts.length > 0) {
1120
+ c.push(` ${createRouteData(qwikPlugin, route, layouts, isSSR)},`);
1121
+ }
1122
+ }
1123
+ c.push(`];`);
1124
+ }
1125
+ function createRouteData(qwikPlugin, r, layouts, isSsr) {
1126
+ const routeName = JSON.stringify(r.routeName);
1127
+ const moduleLayouts = `[ ${layouts.join(", ")} ]`;
1128
+ if (isSsr) {
1129
+ const originalPathname = JSON.stringify(r.pathname);
1130
+ const clientBundleNames = JSON.stringify(getClientRouteBundleNames(qwikPlugin, r));
1131
+ return `[ ${routeName}, ${moduleLayouts}, ${originalPathname}, ${clientBundleNames} ]`;
1132
+ }
1133
+ return `[ ${routeName}, ${moduleLayouts} ]`;
1134
+ }
1135
+ function getClientRouteBundleNames(qwikPlugin, r) {
1136
+ const bundlesNames = [];
1137
+ const manifest = qwikPlugin.api.getManifest();
1138
+ if (manifest) {
1139
+ const manifestBundleNames = Object.keys(manifest.bundles);
1140
+ const addRouteFile = (filePath) => {
1141
+ filePath = removeExtension(filePath);
1142
+ for (const bundleName of manifestBundleNames) {
1143
+ const bundle = manifest.bundles[bundleName];
1144
+ if (bundle.origins) {
1145
+ for (const bundleOrigin of bundle.origins) {
1146
+ const originPath = removeExtension(bundleOrigin);
1147
+ if (filePath.endsWith(originPath)) {
1148
+ if (!bundlesNames.includes(bundleName)) {
1149
+ bundlesNames.push(bundleName);
1150
+ }
1151
+ }
917
1152
  }
1153
+ }
918
1154
  }
919
1155
  };
920
- for (const o of t.layouts)
921
- i(o.filePath);
922
- i(t.filePath);
923
- }
924
- return r;
925
- }
926
- function Wt(e, t, r, n, s) {
927
- if (r.push(`
928
- /** Qwik Router ServerPlugins (${e.serverPlugins.length}) */`), r.push("export const serverPlugins = ["), s) {
929
- for (const i of e.serverPlugins) {
930
- const o = JSON.stringify(W(i.filePath));
931
- n.push(`import * as ${i.id} from ${o};`);
1156
+ for (const layout of r.layouts) {
1157
+ addRouteFile(layout.filePath);
932
1158
  }
933
- for (const i of e.serverPlugins)
934
- r.push(` ${i.id},`);
935
- }
936
- r.push("];");
937
- }
938
- function jt(e, t, r) {
939
- const n = [], s = [];
940
- return s.push(`
941
- /** Qwik Router Config */`), s.push(`
942
- import { isDev } from '@qwik.dev/core/build';`), Wt(e, t, s, n, r), Dt(e, t, s, n, r), _t(e, s, n, r), Rt(e, s), s.push(`export const trailingSlash = ${JSON.stringify(!globalThis.__NO_TRAILING_SLASH__)};`), s.push(`export const basePathname = ${JSON.stringify(e.opts.basePathname)};`), s.push("export const cacheModules = !isDev;"), s.push(
943
- "export default { routes, serverPlugins, menus, trailingSlash, basePathname, cacheModules };"
944
- ), n.join(`
945
- `) + s.join(`
946
- `);
947
- }
948
- function At(e, t) {
949
- let r, n = "/service-worker.js";
950
- if (e.serviceWorkers.length === 0)
951
- r = Ct;
952
- else {
953
- r = t;
954
- const s = e.serviceWorkers.sort(
955
- (i, o) => i.chunkFileName.length < o.chunkFileName.length ? -1 : 1
1159
+ addRouteFile(r.filePath);
1160
+ }
1161
+ return bundlesNames;
1162
+ }
1163
+
1164
+ function createServerPlugins(ctx, _qwikPlugin, c, esmImports, isSSR) {
1165
+ c.push(`
1166
+ /** Qwik Router ServerPlugins (${ctx.serverPlugins.length}) */`);
1167
+ c.push(`export const serverPlugins = [`);
1168
+ if (isSSR) {
1169
+ for (const file of ctx.serverPlugins) {
1170
+ const importPath = JSON.stringify(getImportPath(file.filePath));
1171
+ esmImports.push(`import * as ${file.id} from ${importPath};`);
1172
+ }
1173
+ for (const file of ctx.serverPlugins) {
1174
+ c.push(` ${file.id},`);
1175
+ }
1176
+ }
1177
+ c.push(`];`);
1178
+ }
1179
+
1180
+ function generateQwikRouterConfig(ctx, qwikPlugin, isSSR) {
1181
+ const esmImports = [];
1182
+ const c = [];
1183
+ c.push(`
1184
+ /** Qwik Router Config */`);
1185
+ c.push(`
1186
+ import { isDev } from '@qwik.dev/core/build';`);
1187
+ createServerPlugins(ctx, qwikPlugin, c, esmImports, isSSR);
1188
+ createRoutes(ctx, qwikPlugin, c, esmImports, isSSR);
1189
+ createMenus(ctx, c, esmImports, isSSR);
1190
+ createEntries(ctx, c);
1191
+ c.push(`export const trailingSlash = ${JSON.stringify(!globalThis.__NO_TRAILING_SLASH__)};`);
1192
+ c.push(`export const basePathname = ${JSON.stringify(ctx.opts.basePathname)};`);
1193
+ c.push(`export const cacheModules = !isDev;`);
1194
+ c.push(
1195
+ `export default { routes, serverPlugins, menus, trailingSlash, basePathname, cacheModules };`
1196
+ );
1197
+ return esmImports.join("\n") + c.join("\n");
1198
+ }
1199
+
1200
+ function generateServiceWorkerRegister(ctx, swRegister) {
1201
+ let swReg;
1202
+ let swUrl = "/service-worker.js";
1203
+ if (ctx.serviceWorkers.length === 0) {
1204
+ swReg = SW_UNREGISTER;
1205
+ } else {
1206
+ swReg = swRegister;
1207
+ const sw = ctx.serviceWorkers.sort(
1208
+ (a, b) => a.chunkFileName.length < b.chunkFileName.length ? -1 : 1
956
1209
  )[0];
957
- n = e.opts.basePathname + s.chunkFileName;
1210
+ swUrl = ctx.opts.basePathname + sw.chunkFileName;
958
1211
  }
959
- return r = r.replace("__url", n), `export default ${JSON.stringify(r)};`;
1212
+ swReg = swReg.replace("__url", swUrl);
1213
+ return `export default ${JSON.stringify(swReg)};`;
960
1214
  }
961
- const Ct = `
1215
+ const SW_UNREGISTER = `
962
1216
  "serviceWorker"in navigator&&navigator.serviceWorker.getRegistrations().then(r=>{for(const e of r){const c='__url'.split("/").pop();e.active?.scriptURL.endsWith(c||"service-worker.js")&&e.unregister().catch(console.error)}}),"caches"in window&&caches.keys().then(r=>{const e=r.find(c=>c.startsWith("QwikBuild"));e&&caches.delete(e).catch(console.error)}).catch(console.error)
963
1217
  `;
964
- function Mt(e, t) {
965
- const r = {};
966
- e.forEach((n) => {
967
- const s = S(n.filePath), i = n.layouts ? n.layouts.map((u) => S(u.filePath)) : [], o = [s, ...i], a = [];
968
- for (const [u, c] of Object.entries(t.bundles))
969
- Lt(c, o) && a.push(u);
970
- a.length > 0 && (r[n.routeName] = { dynamicImports: a });
1218
+
1219
+ function getRouteImports(routes, manifest) {
1220
+ const result = {};
1221
+ routes.forEach((route) => {
1222
+ const routePath = removeExtension(route.filePath);
1223
+ const layoutPaths = route.layouts ? route.layouts.map((layout) => removeExtension(layout.filePath)) : [];
1224
+ const routeAndLayoutPaths = [routePath, ...layoutPaths];
1225
+ const bundles = [];
1226
+ for (const [bundleName, bundle] of Object.entries(manifest.bundles)) {
1227
+ if (isBundlePartOfRoute(bundle, routeAndLayoutPaths)) {
1228
+ bundles.push(bundleName);
1229
+ }
1230
+ }
1231
+ if (bundles.length > 0) {
1232
+ result[route.routeName] = { dynamicImports: bundles };
1233
+ }
971
1234
  });
972
- for (const n of Object.keys(t.bundles)) {
973
- const s = t.bundles[n];
974
- if (s.origins?.some((i) => i.endsWith(N))) {
975
- r[n] = {
976
- ...s,
977
- dynamicImports: s.dynamicImports?.filter(
978
- (i) => t.bundles[i].origins?.some((o) => o.endsWith("menu.md"))
1235
+ for (const bundleName of Object.keys(manifest.bundles)) {
1236
+ const bundle = manifest.bundles[bundleName];
1237
+ if (bundle.origins?.some((s) => s.endsWith(QWIK_ROUTER_CONFIG_ID))) {
1238
+ result[bundleName] = {
1239
+ ...bundle,
1240
+ dynamicImports: bundle.dynamicImports?.filter(
1241
+ (d) => manifest.bundles[d].origins?.some((s) => s.endsWith("menu.md"))
979
1242
  )
980
1243
  };
981
1244
  break;
982
1245
  }
983
1246
  }
984
- return r;
985
- }
986
- function Lt(e, t) {
987
- if (!e.origins)
988
- return !1;
989
- for (const r of e.origins) {
990
- const n = S(r);
991
- return t.some((s) => s.endsWith(n));
992
- }
993
- }
994
- const Ht = (e) => {
995
- const t = e.stack;
996
- if (typeof t == "string") {
997
- const r = t.split(`
998
- `).filter((n) => !n.includes("/node_modules/") && !n.includes("(node:"));
999
- for (let n = 1; n < r.length; n++) {
1000
- const s = r[n].replace("file:///", "/");
1001
- if (/^\s+at/.test(s)) {
1002
- const i = s.indexOf("/"), o = s.lastIndexOf(")", i);
1003
- if (i > 0) {
1004
- const u = s.slice(i, o).split(":"), c = V(u[u.length - 1]), l = V(u[u.length - 2]);
1005
- return typeof c == "number" && typeof l == "number" ? (u.length -= 2, {
1006
- file: u.join(":"),
1007
- line: l,
1008
- column: c
1009
- }) : typeof c == "number" ? (u.length -= 1, {
1010
- file: u.join(":"),
1011
- line: c,
1012
- column: void 0
1013
- }) : {
1014
- file: u.join(":"),
1015
- line: void 0,
1016
- column: void 0
1017
- };
1018
- }
1019
- }
1020
- }
1247
+ return result;
1248
+ }
1249
+ function isBundlePartOfRoute(bundle, routeAndLayoutPaths) {
1250
+ if (!bundle.origins) {
1251
+ return false;
1021
1252
  }
1022
- }, V = (e) => {
1023
- try {
1024
- return parseInt(e, 10);
1025
- } catch {
1026
- return;
1253
+ for (const bundleOrigin of bundle.origins) {
1254
+ const originPath = removeExtension(bundleOrigin);
1255
+ return routeAndLayoutPaths.some((path) => path.endsWith(originPath));
1027
1256
  }
1028
- }, he = /\r?\n/, Y = 2;
1029
- function qt(e, t) {
1030
- if (typeof t == "number")
1031
- return t;
1032
- if (t.lo != null)
1033
- return t.lo;
1034
- const r = e.split(he), { line: n, column: s } = t;
1035
- let i = 0;
1036
- for (let o = 0; o < n - 1 && o < r.length; o++)
1037
- i += r[o].length + 1;
1038
- return i + s;
1039
- }
1040
- function Ft(e, t = 0, r) {
1041
- t = qt(e, t), r = r || t;
1042
- const n = e.split(he);
1043
- let s = 0;
1044
- const i = [];
1045
- for (let o = 0; o < n.length; o++)
1046
- if (s += n[o].length + 1, s >= t) {
1047
- for (let a = o - Y; a <= o + Y || r > s; a++) {
1048
- if (a < 0 || a >= n.length)
1049
- continue;
1050
- const u = a + 1;
1051
- i.push(`${u}${" ".repeat(Math.max(3 - String(u).length, 0))}| ${n[a]}`);
1052
- const c = n[a].length;
1053
- if (a === o) {
1054
- const l = Math.max(t - (s - c) + 1, 0), f = Math.max(1, r > s ? c - l : r - t);
1055
- i.push(" | " + " ".repeat(l) + "^".repeat(f));
1056
- } else if (a > o) {
1057
- if (r > s) {
1058
- const l = Math.max(Math.min(r - s, c), 1);
1059
- i.push(" | " + "^".repeat(l));
1060
- }
1061
- s += c + 1;
1062
- }
1063
- }
1064
- break;
1065
- }
1066
- return i.join(`
1067
- `);
1068
- }
1069
- function X(e) {
1070
- const [t, r] = e.split("?"), n = r || "";
1071
- return {
1072
- originalId: e,
1073
- pathId: t,
1074
- query: n ? `?${r}` : "",
1075
- params: new URLSearchParams(n)
1076
- };
1077
1257
  }
1078
- function Bt(e) {
1079
- const t = [".jpg", ".jpeg", ".png", ".webp", ".gif", ".avif", ".tiff"];
1258
+
1259
+ function imagePlugin(userOpts) {
1260
+ const supportedExtensions = [".jpg", ".jpeg", ".png", ".webp", ".gif", ".avif", ".tiff"];
1080
1261
  return [
1081
- import("vite-imagetools").then(
1082
- ({ imagetools: r }) => r({
1262
+ import('vite-imagetools').then(
1263
+ ({ imagetools }) => imagetools({
1083
1264
  exclude: [],
1084
- extendOutputFormats(n) {
1085
- return {
1086
- ...n,
1087
- jsx: () => (i) => {
1088
- const o = i.map((c) => `${c.src} ${c.width}w`).join(", ");
1089
- let a, u = 0;
1090
- for (let c = 0; c < i.length; c++) {
1091
- const l = i[c];
1092
- l.width > u && (a = l, u = l.width);
1265
+ extendOutputFormats(builtins) {
1266
+ const jsx = () => (metadatas) => {
1267
+ const srcSet = metadatas.map((meta) => `${meta.src} ${meta.width}w`).join(", ");
1268
+ let largestImage;
1269
+ let largestImageSize = 0;
1270
+ for (let i = 0; i < metadatas.length; i++) {
1271
+ const m = metadatas[i];
1272
+ if (m.width > largestImageSize) {
1273
+ largestImage = m;
1274
+ largestImageSize = m.width;
1093
1275
  }
1094
- return {
1095
- srcSet: o,
1096
- width: a?.width,
1097
- height: a?.height
1098
- };
1099
1276
  }
1277
+ return {
1278
+ srcSet,
1279
+ width: largestImage === null || largestImage === void 0 ? void 0 : largestImage.width,
1280
+ height: largestImage === null || largestImage === void 0 ? void 0 : largestImage.height
1281
+ };
1282
+ };
1283
+ return {
1284
+ ...builtins,
1285
+ jsx
1100
1286
  };
1101
1287
  },
1102
- defaultDirectives: (n) => {
1103
- if (n.searchParams.has("jsx")) {
1104
- const { jsx: s, ...i } = Object.fromEntries(n.searchParams.entries());
1288
+ defaultDirectives: (url) => {
1289
+ if (url.searchParams.has("jsx")) {
1290
+ const { jsx: _, ...params } = Object.fromEntries(url.searchParams.entries());
1105
1291
  return new URLSearchParams({
1106
1292
  format: "webp",
1107
1293
  quality: "75",
1108
1294
  w: "200;400;600;800;1200",
1109
1295
  withoutEnlargement: "",
1110
- ...e?.imageOptimization?.jsxDirectives,
1111
- ...i,
1296
+ ...userOpts?.imageOptimization?.jsxDirectives,
1297
+ ...params,
1112
1298
  as: "jsx"
1113
1299
  });
1114
1300
  }
1115
1301
  return new URLSearchParams();
1116
1302
  }
1117
1303
  })
1118
- ).catch((r) => (console.error("Error loading vite-imagetools, image imports are not available", r), null)),
1304
+ ).catch((err) => {
1305
+ console.error("Error loading vite-imagetools, image imports are not available", err);
1306
+ return null;
1307
+ }),
1119
1308
  {
1120
1309
  name: "qwik-router-image-jsx",
1121
1310
  load: {
1122
1311
  order: "pre",
1123
- handler: async (r) => {
1124
- const { params: n, pathId: s } = X(r);
1125
- if (Q.extname(s).toLowerCase() === ".svg" && n.has("jsx"))
1312
+ handler: async (id) => {
1313
+ const { params, pathId } = parseId(id);
1314
+ const extension = path.extname(pathId).toLowerCase();
1315
+ if (extension === ".svg" && params.has("jsx")) {
1316
+ const code = await fs.promises.readFile(pathId, "utf-8");
1126
1317
  return {
1127
- code: await x.promises.readFile(s, "utf-8"),
1128
- moduleSideEffects: !1
1318
+ code,
1319
+ moduleSideEffects: false
1129
1320
  };
1321
+ }
1130
1322
  }
1131
1323
  },
1132
- transform(r, n) {
1133
- n = n.toLowerCase();
1134
- const { params: s, pathId: i } = X(n);
1135
- if (s.has("jsx")) {
1136
- const o = Q.extname(i).toLowerCase();
1137
- if (t.includes(o)) {
1138
- r.includes("srcSet") || this.error(`Image '${n}' could not be optimized to JSX`);
1139
- const a = r.indexOf("export default");
1324
+ transform(code, id) {
1325
+ id = id.toLowerCase();
1326
+ const { params, pathId } = parseId(id);
1327
+ if (params.has("jsx")) {
1328
+ const extension = path.extname(pathId).toLowerCase();
1329
+ if (supportedExtensions.includes(extension)) {
1330
+ if (!code.includes("srcSet")) {
1331
+ this.error(`Image '${id}' could not be optimized to JSX`);
1332
+ }
1333
+ const index = code.indexOf("export default");
1140
1334
  return {
1141
- code: r.slice(0, a) + `
1335
+ code: code.slice(0, index) + `
1142
1336
  import { _jsxSorted } from '@qwik.dev/core';
1143
1337
  const PROPS = {srcSet, width, height};
1144
1338
  export default function (props, key, _, dev) {
@@ -1146,12 +1340,12 @@ function Bt(e) {
1146
1340
  }`,
1147
1341
  map: null
1148
1342
  };
1149
- } else if (o === ".svg") {
1150
- const { svgAttributes: a } = Ut({ code: r, path: i }, e);
1343
+ } else if (extension === ".svg") {
1344
+ const { svgAttributes } = optimizeSvg({ code, path: pathId }, userOpts);
1151
1345
  return {
1152
1346
  code: `
1153
1347
  import { _jsxSorted } from '@qwik.dev/core';
1154
- const PROPS = ${JSON.stringify(a)};
1348
+ const PROPS = ${JSON.stringify(svgAttributes)};
1155
1349
  export default function (props, key, _, dev) {
1156
1350
  return _jsxSorted('svg', props, PROPS, undefined, 3, key, dev);
1157
1351
  }`,
@@ -1164,81 +1358,93 @@ function Bt(e) {
1164
1358
  }
1165
1359
  ];
1166
1360
  }
1167
- function Ut({ code: e, path: t }, r) {
1168
- const n = {}, s = r?.imageOptimization?.svgo?.prefixIds, i = s !== !1 ? [{ name: "prefixIds", params: s }] : [], o = r?.imageOptimization?.svgo?.plugins?.filter((u) => u === "preset-default" || typeof u == "object" && u.name === "preset-default" ? (console.warn(
1169
- "You are trying to use the preset-default SVGO plugin. This plugin is already included by default, you can customize it through the defaultPresetOverrides option."
1170
- ), !1) : u === "prefixIds" || typeof u == "object" && u.name === "prefixIds" ? (console.warn(
1171
- "You are trying to use the preset-default SVGO plugin. This plugin is already included by default, you can customize it through the prefixIds option."
1172
- ), !1) : !0) || [], a = Ee(e, {
1173
- floatPrecision: r?.imageOptimization?.svgo?.floatPrecision,
1174
- multipass: r?.imageOptimization?.svgo?.multipass,
1175
- path: t,
1361
+ function optimizeSvg({ code, path: path2 }, userOpts) {
1362
+ const svgAttributes = {};
1363
+ const prefixIdsConfiguration = userOpts?.imageOptimization?.svgo?.prefixIds;
1364
+ const maybePrefixIdsPlugin = prefixIdsConfiguration !== false ? [{ name: "prefixIds", params: prefixIdsConfiguration }] : [];
1365
+ const userPlugins = userOpts?.imageOptimization?.svgo?.plugins?.filter((plugin) => {
1366
+ if (plugin === "preset-default" || typeof plugin === "object" && plugin.name === "preset-default") {
1367
+ console.warn(
1368
+ `You are trying to use the preset-default SVGO plugin. This plugin is already included by default, you can customize it through the defaultPresetOverrides option.`
1369
+ );
1370
+ return false;
1371
+ }
1372
+ if (plugin === "prefixIds" || typeof plugin === "object" && plugin.name === "prefixIds") {
1373
+ console.warn(
1374
+ `You are trying to use the preset-default SVGO plugin. This plugin is already included by default, you can customize it through the prefixIds option.`
1375
+ );
1376
+ return false;
1377
+ }
1378
+ return true;
1379
+ }) || [];
1380
+ const data = optimize(code, {
1381
+ floatPrecision: userOpts?.imageOptimization?.svgo?.floatPrecision,
1382
+ multipass: userOpts?.imageOptimization?.svgo?.multipass,
1383
+ path: path2,
1176
1384
  plugins: [
1177
1385
  {
1178
1386
  name: "preset-default",
1179
1387
  params: {
1180
1388
  overrides: {
1181
- removeViewBox: !1,
1182
- ...r?.imageOptimization?.svgo?.defaultPresetOverrides
1389
+ removeViewBox: false,
1390
+ ...userOpts?.imageOptimization?.svgo?.defaultPresetOverrides
1183
1391
  }
1184
1392
  }
1185
1393
  },
1186
1394
  {
1187
1395
  name: "customPluginName",
1188
- fn: () => ({
1189
- element: {
1190
- exit: (u) => {
1191
- u.name === "svg" && (u.name = "g", Object.assign(n, u.attributes), u.attributes = {});
1396
+ fn: () => {
1397
+ return {
1398
+ element: {
1399
+ exit: (node) => {
1400
+ if (node.name === "svg") {
1401
+ node.name = "g";
1402
+ Object.assign(svgAttributes, node.attributes);
1403
+ node.attributes = {};
1404
+ }
1405
+ }
1192
1406
  }
1193
- }
1194
- })
1407
+ };
1408
+ }
1195
1409
  },
1196
- ...i,
1197
- ...o
1410
+ ...maybePrefixIdsPlugin,
1411
+ ...userPlugins
1198
1412
  ]
1199
1413
  }).data;
1200
- return n.dangerouslySetInnerHTML = a.slice(3, -4), {
1201
- data: a,
1202
- svgAttributes: n
1414
+ svgAttributes.dangerouslySetInnerHTML = data.slice(3, -4);
1415
+ return {
1416
+ data,
1417
+ svgAttributes
1203
1418
  };
1204
1419
  }
1205
- async function Jt(e) {
1206
- if (typeof e.routesDir != "string")
1207
- throw new Error('qwikRouter plugin "routesDir" option missing');
1208
- if (!q(e.routesDir))
1420
+
1421
+ async function validatePlugin(opts) {
1422
+ if (typeof opts.routesDir !== "string") {
1423
+ throw new Error(`qwikRouter plugin "routesDir" option missing`);
1424
+ }
1425
+ if (!isAbsolute(opts.routesDir)) {
1209
1426
  throw new Error(
1210
- `qwikRouter plugin "routesDir" option must be an absolute path: ${e.routesDir}`
1427
+ `qwikRouter plugin "routesDir" option must be an absolute path: ${opts.routesDir}`
1211
1428
  );
1429
+ }
1212
1430
  try {
1213
- if (!(await x.promises.stat(e.routesDir)).isDirectory())
1431
+ const s = await fs.promises.stat(opts.routesDir);
1432
+ if (!s.isDirectory()) {
1214
1433
  throw new Error(
1215
- `qwikRouter plugin "routesDir" option must be a directory: ${e.routesDir}`
1434
+ `qwikRouter plugin "routesDir" option must be a directory: ${opts.routesDir}`
1216
1435
  );
1217
- } catch (t) {
1218
- throw new Error(`qwikRouter plugin "routesDir" not found: ${t}`);
1219
- }
1220
- }
1221
- function Gt(e) {
1222
- if (e instanceof Error) {
1223
- const t = e;
1224
- let r = t.loc;
1225
- if (!t.frame && !t.plugin && (r || (r = Ht(t)), r && (t.loc = r, r.file))) {
1226
- t.id = v(t.loc.file);
1227
- try {
1228
- const n = x.readFileSync(t.loc.file, "utf-8");
1229
- t.frame = Ft(n, t.loc);
1230
- } catch {
1231
- }
1232
1436
  }
1437
+ } catch (e) {
1438
+ throw new Error(`qwikRouter plugin "routesDir" not found: ${e}`);
1233
1439
  }
1234
- return e;
1235
1440
  }
1236
- class zt {
1237
- state = 0;
1441
+
1442
+ class HtmlTransformPatcher {
1443
+ state = 0 /* BUFFERING */;
1238
1444
  buffer = "";
1239
1445
  headInnerIndex = -1;
1240
1446
  bodyInnerIndex = -1;
1241
- isHtmlResponse = !1;
1447
+ isHtmlResponse = false;
1242
1448
  bodyPostContent = "";
1243
1449
  response;
1244
1450
  server;
@@ -1248,384 +1454,570 @@ class zt {
1248
1454
  origSetHeader;
1249
1455
  origWriteHead;
1250
1456
  processingPromise = null;
1251
- constructor(t, r, n) {
1252
- this.request = t, this.response = r, this.server = n, this.origWrite = this.response.write.bind(this.response), this.origEnd = this.response.end.bind(this.response), this.origSetHeader = this.response.setHeader.bind(this.response), this.origWriteHead = this.response.writeHead.bind(this.response), this.response.setHeader = (s, i) => {
1253
- if (s.toLowerCase() === "content-type") {
1254
- const o = String(i).toLowerCase();
1255
- this.isHtmlResponse = o.includes("text/html");
1457
+ constructor(req, res, server) {
1458
+ this.request = req;
1459
+ this.response = res;
1460
+ this.server = server;
1461
+ this.origWrite = this.response.write.bind(this.response);
1462
+ this.origEnd = this.response.end.bind(this.response);
1463
+ this.origSetHeader = this.response.setHeader.bind(this.response);
1464
+ this.origWriteHead = this.response.writeHead.bind(this.response);
1465
+ this.response.setHeader = (name, value) => {
1466
+ if (name.toLowerCase() === "content-type") {
1467
+ const contentType = String(value).toLowerCase();
1468
+ this.isHtmlResponse = contentType.includes("text/html");
1256
1469
  }
1257
- return this.origSetHeader(s, i);
1258
- }, this.response.writeHead = (s, i, o) => {
1259
- if (typeof i == "object" && i !== null && (o = i, i = void 0), o && typeof o == "object") {
1260
- for (const [a, u] of Object.entries(o))
1261
- if (a.toLowerCase() === "content-type") {
1262
- const c = String(u).toLowerCase();
1263
- this.isHtmlResponse = c.includes("text/html");
1470
+ return this.origSetHeader(name, value);
1471
+ };
1472
+ this.response.writeHead = (statusCode, statusMessage, headers) => {
1473
+ if (typeof statusMessage === "object" && statusMessage !== null) {
1474
+ headers = statusMessage;
1475
+ statusMessage = void 0;
1476
+ }
1477
+ if (headers && typeof headers === "object") {
1478
+ for (const [key, value] of Object.entries(headers)) {
1479
+ if (key.toLowerCase() === "content-type") {
1480
+ const contentType = String(value).toLowerCase();
1481
+ this.isHtmlResponse = contentType.includes("text/html");
1264
1482
  }
1483
+ }
1265
1484
  }
1266
- return this.origWriteHead(s, i, o);
1267
- }, this.response.write = this.handleWrite.bind(this), this.response.end = (s, i, o) => (this.handleEnd(s, i, o).catch((a) => {
1268
- console.error("Error in handleEnd:", a), this.transitionToPassthrough(), this.origEnd(s, i, o);
1269
- }), this.response);
1270
- }
1271
- handleWrite(t, r, n) {
1272
- if (!this.isHtmlResponse || this.state === 3)
1273
- return this.origWrite(t, r, n);
1274
- typeof r == "function" && (n = r, r = void 0);
1275
- let s;
1276
- switch (t instanceof ArrayBuffer || t instanceof Uint8Array || t instanceof Uint16Array || t instanceof Uint32Array ? s = new TextDecoder().decode(t) : Buffer.isBuffer(t) ? s = t.toString(r || "utf8") : typeof t == "string" ? s = t : s = t?.toString() || "", this.buffer += s, this.state) {
1277
- case 0:
1485
+ return this.origWriteHead(statusCode, statusMessage, headers);
1486
+ };
1487
+ this.response.write = this.handleWrite.bind(this);
1488
+ this.response.end = (chunk, encoding, callback) => {
1489
+ this.handleEnd(chunk, encoding, callback).catch((error) => {
1490
+ console.error("Error in handleEnd:", error);
1491
+ this.transitionToPassthrough();
1492
+ this.origEnd(chunk, encoding, callback);
1493
+ });
1494
+ return this.response;
1495
+ };
1496
+ }
1497
+ handleWrite(chunk, encoding, callback) {
1498
+ if (!this.isHtmlResponse || this.state === 3 /* PASSTHROUGH */) {
1499
+ return this.origWrite(chunk, encoding, callback);
1500
+ }
1501
+ if (typeof encoding === "function") {
1502
+ callback = encoding;
1503
+ encoding = void 0;
1504
+ }
1505
+ let data;
1506
+ if (chunk instanceof ArrayBuffer || chunk instanceof Uint8Array || chunk instanceof Uint16Array || chunk instanceof Uint32Array) {
1507
+ data = new TextDecoder().decode(chunk);
1508
+ } else if (Buffer.isBuffer(chunk)) {
1509
+ data = chunk.toString(encoding || "utf8");
1510
+ } else if (typeof chunk === "string") {
1511
+ data = chunk;
1512
+ } else {
1513
+ data = chunk?.toString() || "";
1514
+ }
1515
+ this.buffer += data;
1516
+ switch (this.state) {
1517
+ case 0 /* BUFFERING */:
1278
1518
  if (this.headInnerIndex === -1) {
1279
- const i = this.buffer.match(/<head[^>]*>/i);
1280
- if (i) {
1281
- const o = this.buffer.indexOf(i[0]);
1282
- this.headInnerIndex = o + i[0].length;
1519
+ const headMatch = this.buffer.match(/<head[^>]*>/i);
1520
+ if (headMatch) {
1521
+ const headOuterIndex = this.buffer.indexOf(headMatch[0]);
1522
+ this.headInnerIndex = headOuterIndex + headMatch[0].length;
1283
1523
  }
1284
1524
  }
1285
1525
  if (this.headInnerIndex !== -1) {
1286
- const i = this.buffer.slice(this.headInnerIndex).match(/<body[^>]*>/i);
1287
- if (i) {
1288
- this.state = 1;
1289
- const o = this.buffer.indexOf(i[0]);
1290
- this.bodyInnerIndex = o + i[0].length, this.processingPromise = this.processHead();
1526
+ const bodyMatch = this.buffer.slice(this.headInnerIndex).match(/<body[^>]*>/i);
1527
+ if (bodyMatch) {
1528
+ this.state = 1 /* PROCESSING_HEAD */;
1529
+ const bodyOuterIndex = this.buffer.indexOf(bodyMatch[0]);
1530
+ this.bodyInnerIndex = bodyOuterIndex + bodyMatch[0].length;
1531
+ this.processingPromise = this.processHead();
1291
1532
  }
1292
1533
  }
1293
1534
  break;
1294
- case 1:
1535
+ case 1 /* PROCESSING_HEAD */:
1295
1536
  break;
1296
- case 2:
1537
+ case 2 /* STREAMING_BODY */:
1297
1538
  this.handleStreamingBodyState();
1298
1539
  break;
1299
1540
  default:
1300
1541
  throw new Error(`Invalid state: ${this.state}`);
1301
1542
  }
1302
- return n?.(), !0;
1543
+ callback?.();
1544
+ return true;
1303
1545
  }
1304
1546
  async processHead() {
1305
1547
  try {
1306
- const r = await this.server.transformIndexHtml(
1548
+ const fakeHtml = "<html><head>[FAKE_HEAD]</head><body>[FAKE_BODY]</body></html>";
1549
+ const transformedHtml = await this.server.transformIndexHtml(
1307
1550
  this.request.url || "/",
1308
- "<html><head>[FAKE_HEAD]</head><body>[FAKE_BODY]</body></html>"
1309
- ), n = r.indexOf("[FAKE_HEAD]"), s = r.indexOf("</head>", n);
1310
- if (n === -1 || s === -1)
1551
+ fakeHtml
1552
+ );
1553
+ const fakeHeadIndex = transformedHtml.indexOf("[FAKE_HEAD]");
1554
+ const fakeHeadCloseIndex = transformedHtml.indexOf("</head>", fakeHeadIndex);
1555
+ if (fakeHeadIndex === -1 || fakeHeadCloseIndex === -1) {
1311
1556
  throw new Error("Transformed HTML does not contain [FAKE_HEAD]...</head>");
1312
- const i = r.slice(12, n).trim(), o = r.slice(
1313
- n + 11,
1314
- s
1315
- ), a = r.indexOf("<body>", s), u = r.indexOf("[FAKE_BODY]", a), c = r.indexOf("</body>", u);
1316
- if (u === -1 || c === -1)
1557
+ }
1558
+ const headPreContent = transformedHtml.slice("<html><head>".length, fakeHeadIndex).trim();
1559
+ const headPostContent = transformedHtml.slice(
1560
+ fakeHeadIndex + "[FAKE_HEAD]".length,
1561
+ fakeHeadCloseIndex
1562
+ );
1563
+ const fakeBodyStartIndex = transformedHtml.indexOf("<body>", fakeHeadCloseIndex);
1564
+ const fakeBodyIndex = transformedHtml.indexOf("[FAKE_BODY]", fakeBodyStartIndex);
1565
+ const fakeBodyEndIndex = transformedHtml.indexOf("</body>", fakeBodyIndex);
1566
+ if (fakeBodyIndex === -1 || fakeBodyEndIndex === -1) {
1317
1567
  throw new Error("Transformed HTML does not contain [FAKE_BODY]...</body>");
1318
- const l = r.slice(
1319
- a + 6,
1320
- u
1568
+ }
1569
+ const bodyPreContent = transformedHtml.slice(
1570
+ fakeBodyStartIndex + "<body>".length,
1571
+ fakeBodyIndex
1321
1572
  );
1322
- this.bodyPostContent = r.slice(
1323
- u + 11,
1324
- c
1573
+ this.bodyPostContent = transformedHtml.slice(
1574
+ fakeBodyIndex + "[FAKE_BODY]".length,
1575
+ fakeBodyEndIndex
1325
1576
  );
1326
- const f = this.buffer.indexOf("</head>", this.headInnerIndex);
1327
- if (f === -1)
1577
+ const headCloseIndex = this.buffer.indexOf("</head>", this.headInnerIndex);
1578
+ if (headCloseIndex === -1) {
1328
1579
  throw new Error("Buffered HTML does not contain </head>");
1329
- if (this.buffer = this.buffer.slice(0, this.headInnerIndex) + i + this.buffer.slice(this.headInnerIndex, f) + o + this.buffer.slice(f, this.bodyInnerIndex) + l + this.buffer.slice(this.bodyInnerIndex), this.bodyPostContent.length > 0) {
1330
- this.state = 2, this.handleStreamingBodyState();
1580
+ }
1581
+ this.buffer = this.buffer.slice(0, this.headInnerIndex) + headPreContent + this.buffer.slice(this.headInnerIndex, headCloseIndex) + headPostContent + this.buffer.slice(headCloseIndex, this.bodyInnerIndex) + bodyPreContent + this.buffer.slice(this.bodyInnerIndex);
1582
+ if (this.bodyPostContent.length > 0) {
1583
+ this.state = 2 /* STREAMING_BODY */;
1584
+ this.handleStreamingBodyState();
1331
1585
  return;
1332
1586
  }
1333
1587
  this.transitionToPassthrough();
1334
1588
  return;
1335
- } catch (t) {
1336
- console.error("Error transforming HTML:", t), this.transitionToPassthrough();
1589
+ } catch (error) {
1590
+ console.error("Error transforming HTML:", error);
1591
+ this.transitionToPassthrough();
1337
1592
  return;
1338
1593
  }
1339
1594
  }
1340
1595
  handleStreamingBodyState() {
1341
- const t = this.buffer.match(/<\/body>/i);
1342
- if (t) {
1343
- const r = this.buffer.indexOf(t[0]);
1344
- this.buffer = this.buffer.slice(0, r) + this.bodyPostContent + this.buffer.slice(r), this.transitionToPassthrough();
1596
+ const bodyEndMatch = this.buffer.match(/<\/body>/i);
1597
+ if (bodyEndMatch) {
1598
+ const bodyEndPos = this.buffer.indexOf(bodyEndMatch[0]);
1599
+ this.buffer = this.buffer.slice(0, bodyEndPos) + this.bodyPostContent + this.buffer.slice(bodyEndPos);
1600
+ this.transitionToPassthrough();
1345
1601
  return;
1346
1602
  }
1347
1603
  this.flushBuffer(6);
1348
1604
  }
1349
1605
  transitionToPassthrough() {
1350
- this.state = 3, this.flushBuffer();
1606
+ this.state = 3 /* PASSTHROUGH */;
1607
+ this.flushBuffer();
1351
1608
  }
1352
- flushBuffer(t = 0) {
1353
- this.buffer.length > t && (t > 0 ? (this.origWrite(this.buffer.slice(0, -t)), this.buffer = this.buffer.slice(-t)) : (this.origWrite(this.buffer), this.buffer = ""));
1609
+ flushBuffer(keep = 0) {
1610
+ if (this.buffer.length > keep) {
1611
+ if (keep > 0) {
1612
+ this.origWrite(this.buffer.slice(0, -keep));
1613
+ this.buffer = this.buffer.slice(-keep);
1614
+ } else {
1615
+ this.origWrite(this.buffer);
1616
+ this.buffer = "";
1617
+ }
1618
+ }
1354
1619
  }
1355
- async handleEnd(t, r, n) {
1356
- typeof r == "function" && (n = r, r = void 0), t && this.handleWrite(t, r), await this.processingPromise, this.flushBuffer(), this.origEnd(n);
1620
+ async handleEnd(chunk, encoding, callback) {
1621
+ if (typeof encoding === "function") {
1622
+ callback = encoding;
1623
+ encoding = void 0;
1624
+ }
1625
+ if (chunk) {
1626
+ this.handleWrite(chunk, encoding);
1627
+ }
1628
+ await this.processingPromise;
1629
+ this.flushBuffer();
1630
+ this.origEnd(callback);
1357
1631
  }
1358
1632
  }
1359
- function Qt(e, t, r) {
1360
- return new zt(e, t, r), t;
1633
+ function wrapResponseForHtmlTransform(request, response, server) {
1634
+ new HtmlTransformPatcher(request, response, server);
1635
+ return response;
1361
1636
  }
1362
- const Kt = (e, t) => async (r, n, s) => {
1363
- const i = await e.ssrLoadModule("src/entry.ssr");
1364
- if (!i.default)
1365
- return console.error("No default export found in src/entry.ssr"), s();
1366
- const o = i.default;
1367
- t.isDirty && (await fe(t), t.isDirty = !1);
1368
- const a = t.entries.find((c) => r.url === `${e.config.base}${c.chunkFileName}`);
1369
- if (a) {
1370
- const c = await e.transformRequest(
1371
- `/@fs${a.filePath.startsWith("/") ? "" : "/"}${a.filePath}`
1637
+
1638
+ const makeRouterDevMiddleware = (server, ctx) => async (req, res, next) => {
1639
+ const mod = await server.ssrLoadModule("src/entry.ssr");
1640
+ if (!mod.default) {
1641
+ console.error("No default export found in src/entry.ssr");
1642
+ return next();
1643
+ }
1644
+ const renderer = mod.default;
1645
+ if (ctx.isDirty) {
1646
+ await updateRoutingContext(ctx);
1647
+ ctx.isDirty = false;
1648
+ }
1649
+ const entry = ctx.entries.find((e) => req.url === `${server.config.base}${e.chunkFileName}`);
1650
+ if (entry) {
1651
+ const entryContents = await server.transformRequest(
1652
+ `/@fs${entry.filePath.startsWith("/") ? "" : "/"}${entry.filePath}`
1372
1653
  );
1373
- c ? (n.setHeader("Content-Type", "text/javascript"), n.end(c.code)) : s();
1654
+ if (entryContents) {
1655
+ res.setHeader("Content-Type", "text/javascript");
1656
+ res.end(entryContents.code);
1657
+ } else {
1658
+ next();
1659
+ }
1374
1660
  return;
1375
1661
  }
1376
- if (r.url === `${e.config.base}service-worker.js`) {
1377
- n.setHeader("Content-Type", "text/javascript"), n.end(
1378
- "/* Qwik Router Dev Service Worker */self.addEventListener('install', () => self.skipWaiting());self.addEventListener('activate', (ev) => ev.waitUntil(self.clients.claim()));"
1662
+ if (req.url === `${server.config.base}service-worker.js`) {
1663
+ res.setHeader("Content-Type", "text/javascript");
1664
+ res.end(
1665
+ `/* Qwik Router Dev Service Worker */self.addEventListener('install', () => self.skipWaiting());self.addEventListener('activate', (ev) => ev.waitUntil(self.clients.claim()));`
1379
1666
  );
1380
1667
  return;
1381
1668
  }
1382
1669
  globalThis.__qwik = void 0;
1383
- const { createQwikRouter: u } = await e.ssrLoadModule(
1670
+ const { createQwikRouter } = await server.ssrLoadModule(
1384
1671
  "@qwik.dev/router/middleware/node"
1385
1672
  );
1386
1673
  try {
1387
- const c = (async (d) => await o(d)), { router: l, staticFile: f, notFound: p } = u({ render: c }), w = Qt(r, n, e);
1388
- f(r, w, () => {
1389
- l(r, w, () => {
1390
- p(r, w, s);
1674
+ const render = (async (opts) => {
1675
+ return await renderer(opts);
1676
+ });
1677
+ const { router, staticFile, notFound } = createQwikRouter({
1678
+ render,
1679
+ // inject the platform from dev middleware options
1680
+ platform: ctx.opts.platform
1681
+ });
1682
+ const wrappedRes = wrapResponseForHtmlTransform(req, res, server);
1683
+ staticFile(req, wrappedRes, () => {
1684
+ router(req, wrappedRes, () => {
1685
+ notFound(req, wrappedRes, next);
1391
1686
  });
1392
1687
  });
1393
- } catch (c) {
1394
- c instanceof Error && (e.ssrFixStacktrace(c), Gt(c)), s(c);
1688
+ } catch (e) {
1689
+ if (e instanceof Error) {
1690
+ server.ssrFixStacktrace(e);
1691
+ formatError(e);
1692
+ }
1693
+ next(e);
1395
1694
  return;
1396
1695
  }
1397
- }, Vt = [".css", ".scss", ".sass", ".less", ".styl", ".stylus"], Yt = /\.[mc]?[tj]sx?$/, Z = (e) => Vt.some((t) => e.endsWith(t)), Xt = (e) => {
1398
- const t = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Set();
1399
- return Array.from(e.moduleGraph.fileToModulesMap.entries()).forEach(([n, s]) => {
1400
- s.forEach((i) => {
1401
- const [o, a] = i.url.split("?");
1402
- if (!a && Z(o)) {
1403
- const u = i.importers.size === 0, c = Array.from(i.importers).some((f) => {
1404
- const p = f.url || f.file, w = p && Z(p);
1405
- return w && i.url && r.add(i.url), w;
1406
- }), l = Array.from(i.importers).some((f) => {
1407
- const p = f.url || f.file;
1408
- return p && Yt.test(p);
1696
+ };
1697
+ const CSS_EXTENSIONS = [".css", ".scss", ".sass", ".less", ".styl", ".stylus"];
1698
+ const JS_EXTENSIONS = /\.[mc]?[tj]sx?$/;
1699
+ const isCssPath = (url) => CSS_EXTENSIONS.some((ext) => url.endsWith(ext));
1700
+ const getCssUrls = (server) => {
1701
+ const cssModules = /* @__PURE__ */ new Set();
1702
+ const cssImportedByCSS = /* @__PURE__ */ new Set();
1703
+ Array.from(server.moduleGraph.fileToModulesMap.entries()).forEach(([_name, modules]) => {
1704
+ modules.forEach((mod) => {
1705
+ const [pathId, query] = mod.url.split("?");
1706
+ if (!query && isCssPath(pathId)) {
1707
+ const isEntryCSS = mod.importers.size === 0;
1708
+ const hasCSSImporter = Array.from(mod.importers).some((importer) => {
1709
+ const importerPath = importer.url || importer.file;
1710
+ const isCSS = importerPath && isCssPath(importerPath);
1711
+ if (isCSS && mod.url) {
1712
+ cssImportedByCSS.add(mod.url);
1713
+ }
1714
+ return isCSS;
1715
+ });
1716
+ const hasJSImporter = Array.from(mod.importers).some((importer) => {
1717
+ const importerPath = importer.url || importer.file;
1718
+ return importerPath && JS_EXTENSIONS.test(importerPath);
1409
1719
  });
1410
- (u || l) && !c && !r.has(i.url) && t.add(i);
1720
+ if ((isEntryCSS || hasJSImporter) && !hasCSSImporter && !cssImportedByCSS.has(mod.url)) {
1721
+ cssModules.add(mod);
1722
+ }
1411
1723
  }
1412
1724
  });
1413
- }), [...t].map(
1414
- ({ url: n, lastHMRTimestamp: s }) => `${n}${s ? `?t=${s}` : ""}`
1725
+ });
1726
+ return [...cssModules].map(
1727
+ ({ url, lastHMRTimestamp }) => `${url}${lastHMRTimestamp ? `?t=${lastHMRTimestamp}` : ""}`
1415
1728
  );
1416
- }, Zt = (e) => Xt(e).map((r) => ({
1417
- tag: "link",
1418
- attrs: { rel: "stylesheet", href: r }
1419
- })), N = "@qwik-router-config", D = "@qwik-router-entries", H = "@qwik.dev/router", O = "@qwik-router-sw-register";
1420
- function wr(e) {
1421
- return er(e);
1422
- }
1423
- function er(e) {
1424
- return [tr(e), ...Bt(e)];
1425
- }
1426
- function tr(e) {
1427
- let t = null, r = null, n = null, s, i = "esm", o = null, a, u = null, c = e?.devSsrServer;
1428
- const l = e?.routesDir ?? "src/routes", f = e?.serverPluginsDir ?? l;
1429
- return {
1729
+ };
1730
+ const getRouterIndexTags = (server) => {
1731
+ const cssUrls = getCssUrls(server);
1732
+ return cssUrls.map((url) => ({
1733
+ tag: "link",
1734
+ attrs: { rel: "stylesheet", href: url }
1735
+ }));
1736
+ };
1737
+
1738
+ const QWIK_ROUTER_CONFIG_ID = "@qwik-router-config";
1739
+ const QWIK_ROUTER_ENTRIES_ID = "@qwik-router-entries";
1740
+ const QWIK_ROUTER = "@qwik.dev/router";
1741
+ const QWIK_ROUTER_SW_REGISTER = "@qwik-router-sw-register";
1742
+ function qwikCity(userOpts) {
1743
+ return qwikRouter(userOpts);
1744
+ }
1745
+ function qwikRouter(userOpts) {
1746
+ return [qwikRouterPlugin(userOpts), ...imagePlugin(userOpts)];
1747
+ }
1748
+ function qwikRouterPlugin(userOpts) {
1749
+ let ctx = null;
1750
+ let mdxTransform = null;
1751
+ let rootDir = null;
1752
+ let qwikPlugin;
1753
+ let outDir = null;
1754
+ let viteCommand;
1755
+ let devServer = null;
1756
+ let devSsrServer = userOpts?.devSsrServer;
1757
+ const routesDir = userOpts?.routesDir ?? "src/routes";
1758
+ const serverPluginsDir = userOpts?.serverPluginsDir ?? routesDir;
1759
+ const api = {
1760
+ getBasePathname: () => ctx?.opts.basePathname ?? "/",
1761
+ getRoutes: () => {
1762
+ return ctx?.routes.slice() ?? [];
1763
+ },
1764
+ getServiceWorkers: () => {
1765
+ return ctx?.serviceWorkers.slice() ?? [];
1766
+ }
1767
+ };
1768
+ const plugin = {
1430
1769
  name: "vite-plugin-qwik-router",
1431
1770
  enforce: "pre",
1432
- api: {
1433
- getBasePathname: () => t?.opts.basePathname ?? "/",
1434
- getRoutes: () => t?.routes.slice() ?? [],
1435
- getServiceWorkers: () => t?.serviceWorkers.slice() ?? []
1436
- },
1437
- async config(d, h) {
1438
- return a = h.command, {
1771
+ api,
1772
+ async config(_viteConfig, viteEnv) {
1773
+ viteCommand = viteEnv.command;
1774
+ const updatedViteConfig = {
1439
1775
  define: {
1440
1776
  "globalThis.__DEFAULT_LOADERS_SERIALIZATION_STRATEGY__": JSON.stringify(
1441
- e?.defaultLoadersSerializationStrategy || "never"
1777
+ userOpts?.defaultLoadersSerializationStrategy || "never"
1442
1778
  ),
1443
- "globalThis.__NO_TRAILING_SLASH__": JSON.stringify(e?.trailingSlash === !1)
1779
+ "globalThis.__NO_TRAILING_SLASH__": JSON.stringify(userOpts?.trailingSlash === false)
1444
1780
  },
1445
1781
  appType: "custom",
1446
1782
  resolve: {
1447
- dedupe: [H, "@builder.io/qwik-city"],
1783
+ dedupe: [QWIK_ROUTER, "@builder.io/qwik-city"],
1448
1784
  alias: [
1449
1785
  { find: "@builder.io/qwik-city", replacement: "@qwik.dev/router" },
1450
1786
  { find: /^@builder\.io\/qwik-city\/(.*)/, replacement: "@qwik.dev/router/$1" },
1451
- { find: "@qwik-city-plan", replacement: N },
1452
- { find: "@qwik-city-entries", replacement: D },
1453
- { find: "@qwik-city-sw-register", replacement: O }
1787
+ { find: "@qwik-city-plan", replacement: QWIK_ROUTER_CONFIG_ID },
1788
+ { find: "@qwik-city-entries", replacement: QWIK_ROUTER_ENTRIES_ID },
1789
+ { find: "@qwik-city-sw-register", replacement: QWIK_ROUTER_SW_REGISTER }
1454
1790
  ]
1455
1791
  },
1456
1792
  optimizeDeps: {
1457
1793
  // Let Vite find all app deps, these are not part of the static imports from `src/root`
1458
1794
  entries: [
1459
- `${l}/**/index*`,
1460
- `${l}/**/layout*`,
1461
- `${f}/plugin@*`
1795
+ `${routesDir}/**/index*`,
1796
+ `${routesDir}/**/layout*`,
1797
+ `${serverPluginsDir}/plugin@*`
1462
1798
  ],
1463
1799
  // These need processing by the optimizer during dev
1464
1800
  exclude: [
1465
- H,
1466
- N,
1467
- D,
1468
- O
1801
+ QWIK_ROUTER,
1802
+ QWIK_ROUTER_CONFIG_ID,
1803
+ QWIK_ROUTER_ENTRIES_ID,
1804
+ QWIK_ROUTER_SW_REGISTER
1469
1805
  ]
1470
1806
  },
1471
1807
  ssr: {
1472
1808
  external: ["node:async_hooks"],
1473
1809
  noExternal: [
1474
- H,
1475
- N,
1476
- D,
1477
- O
1810
+ QWIK_ROUTER,
1811
+ QWIK_ROUTER_CONFIG_ID,
1812
+ QWIK_ROUTER_ENTRIES_ID,
1813
+ QWIK_ROUTER_SW_REGISTER,
1814
+ // We've had reports of bundling issues with zod
1815
+ "zod"
1478
1816
  ]
1479
1817
  },
1480
1818
  server: {
1481
1819
  watch: {
1482
1820
  // needed for recursive watching of index and layout files in the src/routes directory
1483
- disableGlobbing: !1
1821
+ disableGlobbing: false
1484
1822
  }
1485
1823
  }
1486
1824
  };
1825
+ return updatedViteConfig;
1487
1826
  },
1488
- async configResolved(d) {
1489
- Object.assign(process.env, me(d.mode, process.cwd(), "")), n = T(d.root);
1490
- const h = d.build?.ssr || d.mode === "ssr" ? "ssr" : "client";
1491
- if (t = ut(
1492
- n,
1493
- d.base,
1494
- e,
1495
- h,
1496
- !e?.staticImportRoutes
1497
- ), await Jt(t.opts), r = await Nt(t), s = d.plugins.find(
1498
- (m) => m.name === "vite-plugin-qwik"
1499
- ), !s)
1827
+ async configResolved(config) {
1828
+ Object.assign(process.env, loadEnv(config.mode, process.cwd(), ""));
1829
+ rootDir = resolve(config.root);
1830
+ const target = config.build?.ssr || config.mode === "ssr" ? "ssr" : "client";
1831
+ ctx = createBuildContext(
1832
+ rootDir,
1833
+ config.base,
1834
+ userOpts,
1835
+ target,
1836
+ !userOpts?.staticImportRoutes
1837
+ );
1838
+ await validatePlugin(ctx.opts);
1839
+ mdxTransform = await createMdxTransformer(ctx);
1840
+ qwikPlugin = config.plugins.find(
1841
+ (p) => p.name === "vite-plugin-qwik"
1842
+ );
1843
+ if (!qwikPlugin) {
1500
1844
  throw new Error("Missing vite-plugin-qwik");
1501
- typeof c != "boolean" && (c = s.api._oldDevSsrServer()), s.api.registerBundleGraphAdder?.((m) => Mt(t.routes, m)), d.ssr?.format === "cjs" && (i = "cjs"), o = d.build?.outDir;
1845
+ }
1846
+ if (typeof devSsrServer !== "boolean") {
1847
+ devSsrServer = qwikPlugin.api._oldDevSsrServer();
1848
+ }
1849
+ qwikPlugin.api.registerBundleGraphAdder?.((manifest) => {
1850
+ return getRouteImports(ctx.routes, manifest);
1851
+ });
1852
+ outDir = config.build?.outDir;
1502
1853
  },
1503
- async configureServer(d) {
1504
- u = d;
1505
- const h = T(
1506
- n,
1854
+ async configureServer(server) {
1855
+ devServer = server;
1856
+ const toWatch = resolve(
1857
+ rootDir,
1507
1858
  "src/routes/**/{index,layout,entry,service-worker}{.,@,-}*"
1508
1859
  );
1509
- if (d.watcher.add(h), await new Promise((m) => setTimeout(m, 1e3)), d.watcher.on("change", (m) => {
1510
- if (!/\/(index[.@]|layout[.-]|entry\.|service-worker\.)[^/]*$/.test(m))
1860
+ server.watcher.add(toWatch);
1861
+ await new Promise((resolve2) => setTimeout(resolve2, 1e3));
1862
+ server.watcher.on("change", (path) => {
1863
+ if (!/\/(index[.@]|layout[.-]|entry\.|service-worker\.)[^/]*$/.test(path)) {
1511
1864
  return;
1512
- t.isDirty = !0;
1513
- const g = d.environments?.ssr?.moduleGraph;
1514
- if (g) {
1515
- const b = g.getModuleById("@qwik-router-config");
1516
- b && g.invalidateModule(b);
1517
1865
  }
1518
- }), e?.devSsrServer !== !1)
1866
+ ctx.isDirty = true;
1867
+ const graph = server.environments?.ssr?.moduleGraph;
1868
+ if (graph) {
1869
+ const mod = graph.getModuleById("@qwik-router-config");
1870
+ if (mod) {
1871
+ graph.invalidateModule(mod);
1872
+ }
1873
+ }
1874
+ });
1875
+ if (userOpts?.devSsrServer !== false) {
1519
1876
  return () => {
1520
- d.middlewares.use(Kt(d, t));
1877
+ server.middlewares.use(makeRouterDevMiddleware(server, ctx));
1521
1878
  };
1879
+ }
1522
1880
  },
1523
1881
  transformIndexHtml() {
1524
- if (a === "serve")
1525
- return Zt(u);
1882
+ if (viteCommand !== "serve") {
1883
+ return;
1884
+ }
1885
+ return getRouterIndexTags(devServer);
1526
1886
  },
1527
1887
  buildStart() {
1528
- lt(t);
1888
+ resetBuildContext(ctx);
1529
1889
  },
1530
- resolveId(d) {
1531
- return d === N || d === D ? {
1532
- id: d,
1533
- // user entries added in the routes, like src/routes/service-worker.ts
1534
- // are added as dynamic imports to the qwik-router-config as a way to create
1535
- // a new entry point for the build. Ensure these are not treeshaken.
1536
- moduleSideEffects: "no-treeshake"
1537
- } : d === O ? d : null;
1890
+ resolveId(id) {
1891
+ if (id === QWIK_ROUTER_CONFIG_ID || id === QWIK_ROUTER_ENTRIES_ID) {
1892
+ return {
1893
+ id,
1894
+ // user entries added in the routes, like src/routes/service-worker.ts
1895
+ // are added as dynamic imports to the qwik-router-config as a way to create
1896
+ // a new entry point for the build. Ensure these are not treeshaken.
1897
+ moduleSideEffects: "no-treeshake"
1898
+ };
1899
+ }
1900
+ if (id === QWIK_ROUTER_SW_REGISTER) {
1901
+ return id;
1902
+ }
1903
+ return null;
1538
1904
  },
1539
- async load(d, h) {
1540
- if (t) {
1541
- if (d.endsWith(D))
1542
- return $t(t);
1543
- const m = d.endsWith(N), g = d.endsWith(O);
1544
- if (m || g) {
1545
- if (t.isDirty && (await fe(t), t.isDirty = !1, t.diagnostics.forEach((b) => {
1546
- this.warn(b.message);
1547
- })), m)
1548
- return jt(t, s, h?.ssr ?? !1);
1549
- if (g)
1550
- return At(t, Ie);
1905
+ async load(id, opts) {
1906
+ if (ctx) {
1907
+ if (id.endsWith(QWIK_ROUTER_ENTRIES_ID)) {
1908
+ return generateQwikRouterEntries(ctx);
1909
+ }
1910
+ const isRouterConfig = id.endsWith(QWIK_ROUTER_CONFIG_ID);
1911
+ const isSwRegister = id.endsWith(QWIK_ROUTER_SW_REGISTER);
1912
+ if (isRouterConfig || isSwRegister) {
1913
+ if (ctx.isDirty) {
1914
+ await parseRoutesDir(ctx);
1915
+ ctx.isDirty = false;
1916
+ ctx.diagnostics.forEach((d) => {
1917
+ this.warn(d.message);
1918
+ });
1919
+ }
1920
+ if (isRouterConfig) {
1921
+ return generateQwikRouterConfig(ctx, qwikPlugin, opts?.ssr ?? false);
1922
+ }
1923
+ if (isSwRegister) {
1924
+ return generateServiceWorkerRegister(ctx, swRegister);
1925
+ }
1551
1926
  }
1552
1927
  }
1553
1928
  return null;
1554
1929
  },
1555
- async transform(d, h) {
1556
- if (h.startsWith("\0"))
1930
+ async transform(code, id) {
1931
+ const isVirtualId = id.startsWith("\0");
1932
+ if (isVirtualId) {
1557
1933
  return;
1558
- const g = we(h).toLowerCase();
1559
- if (t && (g === ".md" || g === ".mdx")) {
1560
- const P = M(h);
1561
- if (ue(P))
1562
- return { code: await Ue(t.opts, h, d), map: null };
1563
- if (r)
1934
+ }
1935
+ const ext = extname(id).toLowerCase();
1936
+ const isMD = ext === ".md" || ext === ".mdx";
1937
+ if (ctx && isMD) {
1938
+ const fileName = basename(id);
1939
+ if (isMenuFileName(fileName)) {
1940
+ const menuCode = await transformMenu(ctx.opts, id, code);
1941
+ return { code: menuCode, map: null };
1942
+ }
1943
+ if (mdxTransform) {
1564
1944
  try {
1565
- return await r(d, h);
1566
- } catch (y) {
1567
- if (y && typeof y == "object" && "position" in y && "reason" in y) {
1568
- const _ = y.position?.start.column, k = y.position?.start.line, A = Object.assign(new Error(y.reason), {
1569
- id: h,
1945
+ const mdxResult = await mdxTransform(code, id);
1946
+ return mdxResult;
1947
+ } catch (e) {
1948
+ if (e && typeof e == "object" && "position" in e && "reason" in e) {
1949
+ const column = e.position?.start.column;
1950
+ const line = e.position?.start.line;
1951
+ const err = Object.assign(new Error(e.reason), {
1952
+ id,
1570
1953
  plugin: "qwik-router-mdx",
1571
1954
  loc: {
1572
- column: _,
1573
- line: k
1955
+ column,
1956
+ line
1574
1957
  },
1575
1958
  stack: ""
1576
1959
  });
1577
- this.error(A);
1578
- } else y instanceof Error ? this.error(y) : this.error(String(y));
1960
+ this.error(err);
1961
+ } else if (e instanceof Error) {
1962
+ this.error(e);
1963
+ } else {
1964
+ this.error(String(e));
1965
+ }
1579
1966
  }
1967
+ }
1580
1968
  }
1581
1969
  return null;
1582
1970
  },
1583
- generateBundle(d, h) {
1584
- if (t?.target === "client") {
1585
- const m = [...t.entries, ...t.serviceWorkers].map((g) => ({
1586
- chunkFileName: g.chunkFileName,
1587
- extensionlessFilePath: S(g.filePath)
1588
- }));
1589
- for (const g of m)
1590
- for (const b in h) {
1591
- const P = h[b];
1592
- if (P.type === "chunk" && P.isDynamicEntry && P.facadeModuleId) {
1593
- const y = S(v(P.facadeModuleId));
1594
- if (g.extensionlessFilePath === y) {
1595
- P.fileName = g.chunkFileName;
1971
+ generateBundle(_, bundles) {
1972
+ if (ctx?.target === "client") {
1973
+ const entries = [...ctx.entries, ...ctx.serviceWorkers].map((entry) => {
1974
+ return {
1975
+ chunkFileName: entry.chunkFileName,
1976
+ extensionlessFilePath: removeExtension(entry.filePath)
1977
+ };
1978
+ });
1979
+ for (const entry of entries) {
1980
+ for (const fileName in bundles) {
1981
+ const c = bundles[fileName];
1982
+ if (c.type === "chunk" && c.isDynamicEntry && c.facadeModuleId) {
1983
+ const extensionlessFilePath = removeExtension(normalizePath(c.facadeModuleId));
1984
+ if (entry.extensionlessFilePath === extensionlessFilePath) {
1985
+ c.fileName = entry.chunkFileName;
1596
1986
  continue;
1597
1987
  }
1598
1988
  }
1599
1989
  }
1990
+ }
1600
1991
  }
1601
1992
  },
1602
1993
  closeBundle: {
1603
- sequential: !0,
1994
+ sequential: true,
1604
1995
  async handler() {
1605
- t?.target === "ssr" && o && await rr(o, i);
1996
+ if (ctx?.target === "ssr" && outDir) {
1997
+ await generateServerPackageJson(outDir);
1998
+ }
1606
1999
  }
1607
2000
  }
1608
2001
  };
1609
- }
1610
- async function rr(e, t) {
1611
- await x.promises.mkdir(e, { recursive: !0 });
1612
- const r = j(e, "package.json");
1613
- let n = {};
1614
- if (x.existsSync(r)) {
1615
- const i = await x.promises.readFile(r, "utf-8");
1616
- n = {
1617
- ...JSON.parse(i)
2002
+ return plugin;
2003
+ }
2004
+ async function generateServerPackageJson(outDir) {
2005
+ await fs.promises.mkdir(outDir, { recursive: true });
2006
+ const serverPackageJsonPath = join(outDir, "package.json");
2007
+ let packageJson = {};
2008
+ if (fs.existsSync(serverPackageJsonPath)) {
2009
+ const content = await fs.promises.readFile(serverPackageJsonPath, "utf-8");
2010
+ const contentAsJson = JSON.parse(content);
2011
+ packageJson = {
2012
+ ...contentAsJson
1618
2013
  };
1619
2014
  }
1620
- n = {
1621
- ...n,
1622
- type: t == "cjs" ? "commonjs" : "module"
2015
+ packageJson = {
2016
+ ...packageJson,
2017
+ type: "module"
1623
2018
  };
1624
- const s = JSON.stringify(n, null, 2);
1625
- await x.promises.writeFile(r, s);
2019
+ const serverPackageJsonCode = JSON.stringify(packageJson, null, 2);
2020
+ await fs.promises.writeFile(serverPackageJsonPath, serverPackageJsonCode);
1626
2021
  }
1627
- export {
1628
- yr as extendConfig,
1629
- wr as qwikCity,
1630
- er as qwikRouter
1631
- };
2022
+
2023
+ export { extendConfig, qwikCity, qwikRouter };