@qwik.dev/router 0.0.0 → 2.0.0-alpha.0

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 (98) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +15 -0
  3. package/adapters/azure-swa/vite.d.ts +2 -0
  4. package/adapters/bun-server/vite.d.ts +2 -0
  5. package/adapters/cloud-run/vite.d.ts +2 -0
  6. package/adapters/cloudflare-pages/vite.d.ts +2 -0
  7. package/adapters/deno-server/vite.d.ts +2 -0
  8. package/adapters/netlify-edge/vite.d.ts +2 -0
  9. package/adapters/node-server/vite.d.ts +2 -0
  10. package/adapters/shared/vite.d.ts +2 -0
  11. package/adapters/static/vite.d.ts +2 -0
  12. package/adapters/vercel-edge/vite.d.ts +2 -0
  13. package/index.d.ts +2 -0
  14. package/lib/adapters/azure-swa/vite/index.cjs +96 -0
  15. package/lib/adapters/azure-swa/vite/index.d.ts +13 -0
  16. package/lib/adapters/azure-swa/vite/index.mjs +61 -0
  17. package/lib/adapters/bun-server/vite/index.cjs +50 -0
  18. package/lib/adapters/bun-server/vite/index.d.ts +14 -0
  19. package/lib/adapters/bun-server/vite/index.mjs +25 -0
  20. package/lib/adapters/cloud-run/vite/index.cjs +47 -0
  21. package/lib/adapters/cloud-run/vite/index.d.ts +13 -0
  22. package/lib/adapters/cloud-run/vite/index.mjs +22 -0
  23. package/lib/adapters/cloudflare-pages/vite/index.cjs +115 -0
  24. package/lib/adapters/cloudflare-pages/vite/index.d.ts +27 -0
  25. package/lib/adapters/cloudflare-pages/vite/index.mjs +80 -0
  26. package/lib/adapters/deno-server/vite/index.cjs +62 -0
  27. package/lib/adapters/deno-server/vite/index.d.ts +14 -0
  28. package/lib/adapters/deno-server/vite/index.mjs +37 -0
  29. package/lib/adapters/netlify-edge/vite/index.cjs +129 -0
  30. package/lib/adapters/netlify-edge/vite/index.d.ts +44 -0
  31. package/lib/adapters/netlify-edge/vite/index.mjs +92 -0
  32. package/lib/adapters/node-server/vite/index.cjs +50 -0
  33. package/lib/adapters/node-server/vite/index.d.ts +14 -0
  34. package/lib/adapters/node-server/vite/index.mjs +25 -0
  35. package/lib/adapters/shared/vite/index.cjs +371 -0
  36. package/lib/adapters/shared/vite/index.d.ts +114 -0
  37. package/lib/adapters/shared/vite/index.mjs +331 -0
  38. package/lib/adapters/static/vite/index.cjs +361 -0
  39. package/lib/adapters/static/vite/index.d.ts +10 -0
  40. package/lib/adapters/static/vite/index.mjs +324 -0
  41. package/lib/adapters/vercel-edge/vite/index.cjs +118 -0
  42. package/lib/adapters/vercel-edge/vite/index.d.ts +45 -0
  43. package/lib/adapters/vercel-edge/vite/index.mjs +83 -0
  44. package/lib/index.d.ts +803 -0
  45. package/lib/index.qwik.cjs +1876 -0
  46. package/lib/index.qwik.mjs +1857 -0
  47. package/lib/middleware/aws-lambda/index.d.ts +50 -0
  48. package/lib/middleware/aws-lambda/index.mjs +55 -0
  49. package/lib/middleware/azure-swa/index.d.ts +28 -0
  50. package/lib/middleware/azure-swa/index.mjs +297 -0
  51. package/lib/middleware/bun/index.d.ts +35 -0
  52. package/lib/middleware/bun/index.mjs +208 -0
  53. package/lib/middleware/cloudflare-pages/index.d.ts +35 -0
  54. package/lib/middleware/cloudflare-pages/index.mjs +107 -0
  55. package/lib/middleware/deno/index.d.ts +47 -0
  56. package/lib/middleware/deno/index.mjs +195 -0
  57. package/lib/middleware/firebase/index.d.ts +26 -0
  58. package/lib/middleware/firebase/index.mjs +36 -0
  59. package/lib/middleware/netlify-edge/index.d.ts +27 -0
  60. package/lib/middleware/netlify-edge/index.mjs +83 -0
  61. package/lib/middleware/node/index.cjs +314 -0
  62. package/lib/middleware/node/index.d.ts +66 -0
  63. package/lib/middleware/node/index.mjs +287 -0
  64. package/lib/middleware/request-handler/index.cjs +1544 -0
  65. package/lib/middleware/request-handler/index.d.ts +681 -0
  66. package/lib/middleware/request-handler/index.mjs +1501 -0
  67. package/lib/middleware/vercel-edge/index.d.ts +26 -0
  68. package/lib/middleware/vercel-edge/index.mjs +109 -0
  69. package/lib/modules.d.ts +38 -0
  70. package/lib/service-worker.cjs +267 -0
  71. package/lib/service-worker.d.ts +4 -0
  72. package/lib/service-worker.mjs +265 -0
  73. package/lib/static/deno.mjs +8 -0
  74. package/lib/static/index.cjs +67 -0
  75. package/lib/static/index.d.ts +98 -0
  76. package/lib/static/index.mjs +48 -0
  77. package/lib/static/node.cjs +1124 -0
  78. package/lib/static/node.mjs +1086 -0
  79. package/lib/vite/index.cjs +27496 -0
  80. package/lib/vite/index.d.ts +154 -0
  81. package/lib/vite/index.mjs +27484 -0
  82. package/middleware/aws-lambda.d.ts +2 -0
  83. package/middleware/azure-swa.d.ts +2 -0
  84. package/middleware/bun.d.ts +2 -0
  85. package/middleware/cloudflare-pages.d.ts +2 -0
  86. package/middleware/deno.d.ts +2 -0
  87. package/middleware/firebase.d.ts +2 -0
  88. package/middleware/netlify-edge.d.ts +2 -0
  89. package/middleware/node.d.ts +2 -0
  90. package/middleware/request-handler/generated/not-found-paths.ts +7 -0
  91. package/middleware/request-handler/generated/static-paths.ts +35 -0
  92. package/middleware/request-handler.d.ts +2 -0
  93. package/middleware/vercel-edge.d.ts +2 -0
  94. package/modules.d.ts +38 -0
  95. package/package.json +171 -165
  96. package/service-worker.d.ts +2 -0
  97. package/static.d.ts +2 -0
  98. package/vite.d.ts +2 -0
@@ -0,0 +1,331 @@
1
+ // packages/qwik-router/src/adapters/shared/vite/index.ts
2
+ import fs2 from "node:fs";
3
+ import { basename, dirname, join as join2, resolve } from "node:path";
4
+
5
+ // packages/qwik-router/src/adapters/shared/vite/post-build.ts
6
+ import { getErrorHtml } from "../../../middleware/request-handler/index.mjs";
7
+ import fs from "node:fs";
8
+ import { join } from "node:path";
9
+ async function postBuild(clientOutDir, pathName, userStaticPaths, format, cleanStatic) {
10
+ if (pathName && !pathName.endsWith("/")) {
11
+ pathName += "/";
12
+ }
13
+ const ignorePathnames = /* @__PURE__ */ new Set([pathName + "build/", pathName + "assets/"]);
14
+ const staticPaths = new Set(userStaticPaths.map(normalizeTrailingSlash));
15
+ const notFounds = [];
16
+ const loadItem = async (fsDir, fsName, pathname) => {
17
+ pathname = normalizeTrailingSlash(pathname);
18
+ if (ignorePathnames.has(pathname)) {
19
+ return;
20
+ }
21
+ const fsPath = join(fsDir, fsName);
22
+ if (fsName === "index.html" || fsName === "q-data.json") {
23
+ if (!staticPaths.has(pathname) && cleanStatic) {
24
+ await fs.promises.unlink(fsPath);
25
+ }
26
+ return;
27
+ }
28
+ if (fsName === "404.html") {
29
+ const notFoundHtml = await fs.promises.readFile(fsPath, "utf-8");
30
+ notFounds.push([pathname, notFoundHtml]);
31
+ return;
32
+ }
33
+ const stat = await fs.promises.stat(fsPath);
34
+ if (stat.isDirectory()) {
35
+ await loadDir(fsPath, pathname + fsName + "/");
36
+ } else if (stat.isFile()) {
37
+ staticPaths.add(pathname + fsName);
38
+ }
39
+ };
40
+ const loadDir = async (fsDir, pathname) => {
41
+ const itemNames = await fs.promises.readdir(fsDir);
42
+ await Promise.all(itemNames.map((i) => loadItem(fsDir, i, pathname)));
43
+ };
44
+ if (fs.existsSync(clientOutDir)) {
45
+ await loadDir(clientOutDir, pathName);
46
+ }
47
+ const notFoundPathsCode = createNotFoundPathsModule(pathName, notFounds, format);
48
+ const staticPathsCode = createStaticPathsModule(pathName, staticPaths, format);
49
+ return {
50
+ notFoundPathsCode,
51
+ staticPathsCode
52
+ };
53
+ }
54
+ function normalizeTrailingSlash(pathname) {
55
+ if (!pathname.endsWith("/")) {
56
+ return pathname + "/";
57
+ }
58
+ return pathname;
59
+ }
60
+ function createNotFoundPathsModule(basePathname, notFounds, format) {
61
+ notFounds.sort((a, b) => {
62
+ if (a[0].length > b[0].length) {
63
+ return -1;
64
+ }
65
+ if (a[0].length < b[0].length) {
66
+ return 1;
67
+ }
68
+ if (a[0] < b[0]) {
69
+ return -1;
70
+ }
71
+ if (a[0] > b[0]) {
72
+ return 1;
73
+ }
74
+ return 0;
75
+ });
76
+ if (!notFounds.some((r) => r[0] === basePathname)) {
77
+ const html = getErrorHtml(404, "Resource Not Found");
78
+ notFounds.push([basePathname, html]);
79
+ }
80
+ const c = [];
81
+ c.push(`const notFounds = ${JSON.stringify(notFounds, null, 2)};`);
82
+ c.push(`function getNotFound(p) {`);
83
+ c.push(` for (const r of notFounds) {`);
84
+ c.push(` if (p.startsWith(r[0])) {`);
85
+ c.push(` return r[1];`);
86
+ c.push(` }`);
87
+ c.push(` }`);
88
+ c.push(` return "Resource Not Found";`);
89
+ c.push(`}`);
90
+ if (format === "cjs") {
91
+ c.push("exports.getNotFound = getNotFound;");
92
+ } else {
93
+ c.push("export { getNotFound };");
94
+ }
95
+ return c.join("\n");
96
+ }
97
+ function createStaticPathsModule(basePathname, staticPaths, format) {
98
+ const assetsPath = basePathname + "assets/";
99
+ const baseBuildPath = basePathname + "build/";
100
+ const c = [];
101
+ c.push(
102
+ `const staticPaths = new Set(${JSON.stringify(
103
+ Array.from(new Set(staticPaths)).sort()
104
+ )});`
105
+ );
106
+ c.push(`function isStaticPath(method, url) {`);
107
+ c.push(` if (method.toUpperCase() !== 'GET') {`);
108
+ c.push(` return false;`);
109
+ c.push(` }`);
110
+ c.push(` const p = url.pathname;`);
111
+ c.push(` if (p.startsWith(${JSON.stringify(baseBuildPath)})) {`);
112
+ c.push(` return true;`);
113
+ c.push(` }`);
114
+ c.push(` if (p.startsWith(${JSON.stringify(assetsPath)})) {`);
115
+ c.push(` return true;`);
116
+ c.push(` }`);
117
+ c.push(` if (staticPaths.has(p)) {`);
118
+ c.push(` return true;`);
119
+ c.push(` }`);
120
+ c.push(` if (p.endsWith('/q-data.json')) {`);
121
+ c.push(` const pWithoutQdata = p.replace(/\\/q-data.json$/, '');`);
122
+ c.push(` if (staticPaths.has(pWithoutQdata + '/')) {`);
123
+ c.push(` return true;`);
124
+ c.push(` }`);
125
+ c.push(` if (staticPaths.has(pWithoutQdata)) {`);
126
+ c.push(` return true;`);
127
+ c.push(` }`);
128
+ c.push(` }`);
129
+ c.push(` return false;`);
130
+ c.push(`}`);
131
+ if (format === "cjs") {
132
+ c.push("exports.isStaticPath = isStaticPath;");
133
+ } else {
134
+ c.push("export { isStaticPath };");
135
+ }
136
+ return c.join("\n");
137
+ }
138
+
139
+ // packages/qwik-router/src/adapters/shared/vite/index.ts
140
+ function viteAdapter(opts) {
141
+ let qwikRouterPlugin = null;
142
+ let qwikVitePlugin = null;
143
+ let serverOutDir = null;
144
+ let renderModulePath = null;
145
+ let qwikRouterConfigModulePath = null;
146
+ let isSsrBuild = false;
147
+ let format = "esm";
148
+ const outputEntries = [];
149
+ const plugin = {
150
+ name: `vite-plugin-qwik-router-${opts.name}`,
151
+ enforce: "post",
152
+ apply: "build",
153
+ config(config) {
154
+ if (typeof opts.config === "function") {
155
+ config = opts.config(config);
156
+ }
157
+ config.define = {
158
+ "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || "production"),
159
+ ...config.define
160
+ };
161
+ return config;
162
+ },
163
+ configResolved(config) {
164
+ var _a, _b, _c, _d;
165
+ isSsrBuild = !!config.build.ssr;
166
+ if (isSsrBuild) {
167
+ qwikRouterPlugin = config.plugins.find(
168
+ (p) => p.name === "vite-plugin-qwik-router"
169
+ );
170
+ if (!qwikRouterPlugin) {
171
+ throw new Error("Missing vite-plugin-qwik-router");
172
+ }
173
+ qwikVitePlugin = config.plugins.find(
174
+ (p) => p.name === "vite-plugin-qwik"
175
+ );
176
+ if (!qwikVitePlugin) {
177
+ throw new Error("Missing vite-plugin-qwik");
178
+ }
179
+ serverOutDir = config.build.outDir;
180
+ if (((_a = config.build) == null ? void 0 : _a.ssr) !== true) {
181
+ throw new Error(
182
+ `"build.ssr" must be set to "true" in order to use the "${opts.name}" adapter.`
183
+ );
184
+ }
185
+ if (!((_c = (_b = config.build) == null ? void 0 : _b.rollupOptions) == null ? void 0 : _c.input)) {
186
+ throw new Error(
187
+ `"build.rollupOptions.input" must be set in order to use the "${opts.name}" adapter.`
188
+ );
189
+ }
190
+ if (((_d = config.ssr) == null ? void 0 : _d.format) === "cjs") {
191
+ format = "cjs";
192
+ }
193
+ }
194
+ },
195
+ generateBundle(_, bundles) {
196
+ if (isSsrBuild) {
197
+ outputEntries.length = 0;
198
+ for (const fileName in bundles) {
199
+ const chunk = bundles[fileName];
200
+ if (chunk.type === "chunk" && chunk.isEntry) {
201
+ outputEntries.push(fileName);
202
+ if (chunk.name === "entry.ssr") {
203
+ renderModulePath = join2(serverOutDir, fileName);
204
+ } else if (chunk.name === "@qwik-router-config") {
205
+ qwikRouterConfigModulePath = join2(serverOutDir, fileName);
206
+ }
207
+ }
208
+ }
209
+ if (!renderModulePath) {
210
+ throw new Error(
211
+ 'Unable to find "entry.ssr" entry point. Did you forget to add it to "build.rollupOptions.input"?'
212
+ );
213
+ }
214
+ if (!qwikRouterConfigModulePath) {
215
+ throw new Error(
216
+ 'Unable to find "@qwik-router-config" entry point. Did you forget to add it to "build.rollupOptions.input"?'
217
+ );
218
+ }
219
+ }
220
+ },
221
+ closeBundle: {
222
+ sequential: true,
223
+ async handler() {
224
+ var _a;
225
+ if (isSsrBuild && opts.ssg !== null && serverOutDir && (qwikRouterPlugin == null ? void 0 : qwikRouterPlugin.api) && (qwikVitePlugin == null ? void 0 : qwikVitePlugin.api)) {
226
+ const staticPaths = opts.staticPaths || [];
227
+ const routes = qwikRouterPlugin.api.getRoutes();
228
+ const basePathname = qwikRouterPlugin.api.getBasePathname();
229
+ const clientOutDir = qwikVitePlugin.api.getClientOutDir();
230
+ const clientPublicOutDir = qwikVitePlugin.api.getClientPublicOutDir();
231
+ const assetsDir = qwikVitePlugin.api.getAssetsDir();
232
+ const rootDir = qwikVitePlugin.api.getRootDir() ?? void 0;
233
+ if (renderModulePath && qwikRouterConfigModulePath && clientOutDir && clientPublicOutDir) {
234
+ let ssgOrigin = ((_a = opts.ssg) == null ? void 0 : _a.origin) ?? opts.origin;
235
+ if (!ssgOrigin) {
236
+ ssgOrigin = `https://yoursite.qwik.dev`;
237
+ }
238
+ if (ssgOrigin.length > 0 && !/:\/\//.test(ssgOrigin)) {
239
+ ssgOrigin = `https://${ssgOrigin}`;
240
+ }
241
+ if (ssgOrigin.startsWith("//")) {
242
+ ssgOrigin = `https:${ssgOrigin}`;
243
+ }
244
+ try {
245
+ ssgOrigin = new URL(ssgOrigin).origin;
246
+ } catch (e) {
247
+ this.warn(
248
+ `Invalid "origin" option: "${ssgOrigin}". Using default origin: "https://yoursite.qwik.dev"`
249
+ );
250
+ ssgOrigin = `https://yoursite.qwik.dev`;
251
+ }
252
+ const staticGenerate = await import("../../../static/index.mjs");
253
+ const generateOpts = {
254
+ maxWorkers: opts.maxWorkers,
255
+ basePathname,
256
+ outDir: clientPublicOutDir,
257
+ rootDir,
258
+ ...opts.ssg,
259
+ origin: ssgOrigin,
260
+ renderModulePath,
261
+ qwikRouterConfigModulePath
262
+ };
263
+ const staticGenerateResult = await staticGenerate.generate(generateOpts);
264
+ if (staticGenerateResult.errors > 0) {
265
+ const err = new Error(
266
+ `Error while running SSG from "${opts.name}" adapter. At least one path failed to render.`
267
+ );
268
+ err.stack = void 0;
269
+ this.error(err);
270
+ }
271
+ staticPaths.push(...staticGenerateResult.staticPaths);
272
+ const { staticPathsCode, notFoundPathsCode } = await postBuild(
273
+ clientPublicOutDir,
274
+ assetsDir ? join2(basePathname, assetsDir) : basePathname,
275
+ staticPaths,
276
+ format,
277
+ !!opts.cleanStaticGenerated
278
+ );
279
+ await Promise.all([
280
+ fs2.promises.writeFile(join2(serverOutDir, RESOLVED_STATIC_PATHS_ID), staticPathsCode),
281
+ fs2.promises.writeFile(
282
+ join2(serverOutDir, RESOLVED_NOT_FOUND_PATHS_ID),
283
+ notFoundPathsCode
284
+ )
285
+ ]);
286
+ if (typeof opts.generate === "function") {
287
+ await opts.generate({
288
+ outputEntries,
289
+ serverOutDir,
290
+ clientOutDir,
291
+ clientPublicOutDir,
292
+ basePathname,
293
+ routes,
294
+ assetsDir,
295
+ warn: (message) => this.warn(message),
296
+ error: (message) => this.error(message)
297
+ });
298
+ }
299
+ }
300
+ }
301
+ }
302
+ }
303
+ };
304
+ return plugin;
305
+ }
306
+ function getParentDir(startDir, dirName) {
307
+ const root = resolve("/");
308
+ let dir = startDir;
309
+ for (let i = 0; i < 20; i++) {
310
+ dir = dirname(dir);
311
+ if (basename(dir) === dirName) {
312
+ return dir;
313
+ }
314
+ if (dir === root) {
315
+ break;
316
+ }
317
+ }
318
+ throw new Error(`Unable to find "${dirName}" directory from "${startDir}"`);
319
+ }
320
+ var STATIC_PATHS_ID = "@qwik-router-static-paths";
321
+ var RESOLVED_STATIC_PATHS_ID = `${STATIC_PATHS_ID}.js`;
322
+ var NOT_FOUND_PATHS_ID = "@qwik-router-not-found-paths";
323
+ var RESOLVED_NOT_FOUND_PATHS_ID = `${NOT_FOUND_PATHS_ID}.js`;
324
+ export {
325
+ NOT_FOUND_PATHS_ID,
326
+ RESOLVED_NOT_FOUND_PATHS_ID,
327
+ RESOLVED_STATIC_PATHS_ID,
328
+ STATIC_PATHS_ID,
329
+ getParentDir,
330
+ viteAdapter
331
+ };
@@ -0,0 +1,361 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // packages/qwik-router/src/adapters/static/vite/index.ts
31
+ var vite_exports = {};
32
+ __export(vite_exports, {
33
+ staticAdapter: () => staticAdapter
34
+ });
35
+ module.exports = __toCommonJS(vite_exports);
36
+
37
+ // packages/qwik-router/src/adapters/shared/vite/index.ts
38
+ var import_node_fs2 = __toESM(require("node:fs"), 1);
39
+ var import_node_path2 = require("node:path");
40
+
41
+ // packages/qwik-router/src/adapters/shared/vite/post-build.ts
42
+ var import_request_handler = require("@qwik.dev/router/middleware/request-handler");
43
+ var import_node_fs = __toESM(require("node:fs"), 1);
44
+ var import_node_path = require("node:path");
45
+ async function postBuild(clientOutDir, pathName, userStaticPaths, format, cleanStatic) {
46
+ if (pathName && !pathName.endsWith("/")) {
47
+ pathName += "/";
48
+ }
49
+ const ignorePathnames = /* @__PURE__ */ new Set([pathName + "build/", pathName + "assets/"]);
50
+ const staticPaths = new Set(userStaticPaths.map(normalizeTrailingSlash));
51
+ const notFounds = [];
52
+ const loadItem = async (fsDir, fsName, pathname) => {
53
+ pathname = normalizeTrailingSlash(pathname);
54
+ if (ignorePathnames.has(pathname)) {
55
+ return;
56
+ }
57
+ const fsPath = (0, import_node_path.join)(fsDir, fsName);
58
+ if (fsName === "index.html" || fsName === "q-data.json") {
59
+ if (!staticPaths.has(pathname) && cleanStatic) {
60
+ await import_node_fs.default.promises.unlink(fsPath);
61
+ }
62
+ return;
63
+ }
64
+ if (fsName === "404.html") {
65
+ const notFoundHtml = await import_node_fs.default.promises.readFile(fsPath, "utf-8");
66
+ notFounds.push([pathname, notFoundHtml]);
67
+ return;
68
+ }
69
+ const stat = await import_node_fs.default.promises.stat(fsPath);
70
+ if (stat.isDirectory()) {
71
+ await loadDir(fsPath, pathname + fsName + "/");
72
+ } else if (stat.isFile()) {
73
+ staticPaths.add(pathname + fsName);
74
+ }
75
+ };
76
+ const loadDir = async (fsDir, pathname) => {
77
+ const itemNames = await import_node_fs.default.promises.readdir(fsDir);
78
+ await Promise.all(itemNames.map((i) => loadItem(fsDir, i, pathname)));
79
+ };
80
+ if (import_node_fs.default.existsSync(clientOutDir)) {
81
+ await loadDir(clientOutDir, pathName);
82
+ }
83
+ const notFoundPathsCode = createNotFoundPathsModule(pathName, notFounds, format);
84
+ const staticPathsCode = createStaticPathsModule(pathName, staticPaths, format);
85
+ return {
86
+ notFoundPathsCode,
87
+ staticPathsCode
88
+ };
89
+ }
90
+ function normalizeTrailingSlash(pathname) {
91
+ if (!pathname.endsWith("/")) {
92
+ return pathname + "/";
93
+ }
94
+ return pathname;
95
+ }
96
+ function createNotFoundPathsModule(basePathname, notFounds, format) {
97
+ notFounds.sort((a, b) => {
98
+ if (a[0].length > b[0].length) {
99
+ return -1;
100
+ }
101
+ if (a[0].length < b[0].length) {
102
+ return 1;
103
+ }
104
+ if (a[0] < b[0]) {
105
+ return -1;
106
+ }
107
+ if (a[0] > b[0]) {
108
+ return 1;
109
+ }
110
+ return 0;
111
+ });
112
+ if (!notFounds.some((r) => r[0] === basePathname)) {
113
+ const html = (0, import_request_handler.getErrorHtml)(404, "Resource Not Found");
114
+ notFounds.push([basePathname, html]);
115
+ }
116
+ const c = [];
117
+ c.push(`const notFounds = ${JSON.stringify(notFounds, null, 2)};`);
118
+ c.push(`function getNotFound(p) {`);
119
+ c.push(` for (const r of notFounds) {`);
120
+ c.push(` if (p.startsWith(r[0])) {`);
121
+ c.push(` return r[1];`);
122
+ c.push(` }`);
123
+ c.push(` }`);
124
+ c.push(` return "Resource Not Found";`);
125
+ c.push(`}`);
126
+ if (format === "cjs") {
127
+ c.push("exports.getNotFound = getNotFound;");
128
+ } else {
129
+ c.push("export { getNotFound };");
130
+ }
131
+ return c.join("\n");
132
+ }
133
+ function createStaticPathsModule(basePathname, staticPaths, format) {
134
+ const assetsPath = basePathname + "assets/";
135
+ const baseBuildPath = basePathname + "build/";
136
+ const c = [];
137
+ c.push(
138
+ `const staticPaths = new Set(${JSON.stringify(
139
+ Array.from(new Set(staticPaths)).sort()
140
+ )});`
141
+ );
142
+ c.push(`function isStaticPath(method, url) {`);
143
+ c.push(` if (method.toUpperCase() !== 'GET') {`);
144
+ c.push(` return false;`);
145
+ c.push(` }`);
146
+ c.push(` const p = url.pathname;`);
147
+ c.push(` if (p.startsWith(${JSON.stringify(baseBuildPath)})) {`);
148
+ c.push(` return true;`);
149
+ c.push(` }`);
150
+ c.push(` if (p.startsWith(${JSON.stringify(assetsPath)})) {`);
151
+ c.push(` return true;`);
152
+ c.push(` }`);
153
+ c.push(` if (staticPaths.has(p)) {`);
154
+ c.push(` return true;`);
155
+ c.push(` }`);
156
+ c.push(` if (p.endsWith('/q-data.json')) {`);
157
+ c.push(` const pWithoutQdata = p.replace(/\\/q-data.json$/, '');`);
158
+ c.push(` if (staticPaths.has(pWithoutQdata + '/')) {`);
159
+ c.push(` return true;`);
160
+ c.push(` }`);
161
+ c.push(` if (staticPaths.has(pWithoutQdata)) {`);
162
+ c.push(` return true;`);
163
+ c.push(` }`);
164
+ c.push(` }`);
165
+ c.push(` return false;`);
166
+ c.push(`}`);
167
+ if (format === "cjs") {
168
+ c.push("exports.isStaticPath = isStaticPath;");
169
+ } else {
170
+ c.push("export { isStaticPath };");
171
+ }
172
+ return c.join("\n");
173
+ }
174
+
175
+ // packages/qwik-router/src/adapters/shared/vite/index.ts
176
+ function viteAdapter(opts) {
177
+ let qwikRouterPlugin = null;
178
+ let qwikVitePlugin = null;
179
+ let serverOutDir = null;
180
+ let renderModulePath = null;
181
+ let qwikRouterConfigModulePath = null;
182
+ let isSsrBuild = false;
183
+ let format = "esm";
184
+ const outputEntries = [];
185
+ const plugin = {
186
+ name: `vite-plugin-qwik-router-${opts.name}`,
187
+ enforce: "post",
188
+ apply: "build",
189
+ config(config) {
190
+ if (typeof opts.config === "function") {
191
+ config = opts.config(config);
192
+ }
193
+ config.define = {
194
+ "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || "production"),
195
+ ...config.define
196
+ };
197
+ return config;
198
+ },
199
+ configResolved(config) {
200
+ var _a, _b, _c, _d;
201
+ isSsrBuild = !!config.build.ssr;
202
+ if (isSsrBuild) {
203
+ qwikRouterPlugin = config.plugins.find(
204
+ (p) => p.name === "vite-plugin-qwik-router"
205
+ );
206
+ if (!qwikRouterPlugin) {
207
+ throw new Error("Missing vite-plugin-qwik-router");
208
+ }
209
+ qwikVitePlugin = config.plugins.find(
210
+ (p) => p.name === "vite-plugin-qwik"
211
+ );
212
+ if (!qwikVitePlugin) {
213
+ throw new Error("Missing vite-plugin-qwik");
214
+ }
215
+ serverOutDir = config.build.outDir;
216
+ if (((_a = config.build) == null ? void 0 : _a.ssr) !== true) {
217
+ throw new Error(
218
+ `"build.ssr" must be set to "true" in order to use the "${opts.name}" adapter.`
219
+ );
220
+ }
221
+ if (!((_c = (_b = config.build) == null ? void 0 : _b.rollupOptions) == null ? void 0 : _c.input)) {
222
+ throw new Error(
223
+ `"build.rollupOptions.input" must be set in order to use the "${opts.name}" adapter.`
224
+ );
225
+ }
226
+ if (((_d = config.ssr) == null ? void 0 : _d.format) === "cjs") {
227
+ format = "cjs";
228
+ }
229
+ }
230
+ },
231
+ generateBundle(_, bundles) {
232
+ if (isSsrBuild) {
233
+ outputEntries.length = 0;
234
+ for (const fileName in bundles) {
235
+ const chunk = bundles[fileName];
236
+ if (chunk.type === "chunk" && chunk.isEntry) {
237
+ outputEntries.push(fileName);
238
+ if (chunk.name === "entry.ssr") {
239
+ renderModulePath = (0, import_node_path2.join)(serverOutDir, fileName);
240
+ } else if (chunk.name === "@qwik-router-config") {
241
+ qwikRouterConfigModulePath = (0, import_node_path2.join)(serverOutDir, fileName);
242
+ }
243
+ }
244
+ }
245
+ if (!renderModulePath) {
246
+ throw new Error(
247
+ 'Unable to find "entry.ssr" entry point. Did you forget to add it to "build.rollupOptions.input"?'
248
+ );
249
+ }
250
+ if (!qwikRouterConfigModulePath) {
251
+ throw new Error(
252
+ 'Unable to find "@qwik-router-config" entry point. Did you forget to add it to "build.rollupOptions.input"?'
253
+ );
254
+ }
255
+ }
256
+ },
257
+ closeBundle: {
258
+ sequential: true,
259
+ async handler() {
260
+ var _a;
261
+ if (isSsrBuild && opts.ssg !== null && serverOutDir && (qwikRouterPlugin == null ? void 0 : qwikRouterPlugin.api) && (qwikVitePlugin == null ? void 0 : qwikVitePlugin.api)) {
262
+ const staticPaths = opts.staticPaths || [];
263
+ const routes = qwikRouterPlugin.api.getRoutes();
264
+ const basePathname = qwikRouterPlugin.api.getBasePathname();
265
+ const clientOutDir = qwikVitePlugin.api.getClientOutDir();
266
+ const clientPublicOutDir = qwikVitePlugin.api.getClientPublicOutDir();
267
+ const assetsDir = qwikVitePlugin.api.getAssetsDir();
268
+ const rootDir = qwikVitePlugin.api.getRootDir() ?? void 0;
269
+ if (renderModulePath && qwikRouterConfigModulePath && clientOutDir && clientPublicOutDir) {
270
+ let ssgOrigin = ((_a = opts.ssg) == null ? void 0 : _a.origin) ?? opts.origin;
271
+ if (!ssgOrigin) {
272
+ ssgOrigin = `https://yoursite.qwik.dev`;
273
+ }
274
+ if (ssgOrigin.length > 0 && !/:\/\//.test(ssgOrigin)) {
275
+ ssgOrigin = `https://${ssgOrigin}`;
276
+ }
277
+ if (ssgOrigin.startsWith("//")) {
278
+ ssgOrigin = `https:${ssgOrigin}`;
279
+ }
280
+ try {
281
+ ssgOrigin = new URL(ssgOrigin).origin;
282
+ } catch (e) {
283
+ this.warn(
284
+ `Invalid "origin" option: "${ssgOrigin}". Using default origin: "https://yoursite.qwik.dev"`
285
+ );
286
+ ssgOrigin = `https://yoursite.qwik.dev`;
287
+ }
288
+ const staticGenerate = await import("../../../static/index.cjs");
289
+ const generateOpts = {
290
+ maxWorkers: opts.maxWorkers,
291
+ basePathname,
292
+ outDir: clientPublicOutDir,
293
+ rootDir,
294
+ ...opts.ssg,
295
+ origin: ssgOrigin,
296
+ renderModulePath,
297
+ qwikRouterConfigModulePath
298
+ };
299
+ const staticGenerateResult = await staticGenerate.generate(generateOpts);
300
+ if (staticGenerateResult.errors > 0) {
301
+ const err = new Error(
302
+ `Error while running SSG from "${opts.name}" adapter. At least one path failed to render.`
303
+ );
304
+ err.stack = void 0;
305
+ this.error(err);
306
+ }
307
+ staticPaths.push(...staticGenerateResult.staticPaths);
308
+ const { staticPathsCode, notFoundPathsCode } = await postBuild(
309
+ clientPublicOutDir,
310
+ assetsDir ? (0, import_node_path2.join)(basePathname, assetsDir) : basePathname,
311
+ staticPaths,
312
+ format,
313
+ !!opts.cleanStaticGenerated
314
+ );
315
+ await Promise.all([
316
+ import_node_fs2.default.promises.writeFile((0, import_node_path2.join)(serverOutDir, RESOLVED_STATIC_PATHS_ID), staticPathsCode),
317
+ import_node_fs2.default.promises.writeFile(
318
+ (0, import_node_path2.join)(serverOutDir, RESOLVED_NOT_FOUND_PATHS_ID),
319
+ notFoundPathsCode
320
+ )
321
+ ]);
322
+ if (typeof opts.generate === "function") {
323
+ await opts.generate({
324
+ outputEntries,
325
+ serverOutDir,
326
+ clientOutDir,
327
+ clientPublicOutDir,
328
+ basePathname,
329
+ routes,
330
+ assetsDir,
331
+ warn: (message) => this.warn(message),
332
+ error: (message) => this.error(message)
333
+ });
334
+ }
335
+ }
336
+ }
337
+ }
338
+ }
339
+ };
340
+ return plugin;
341
+ }
342
+ var STATIC_PATHS_ID = "@qwik-router-static-paths";
343
+ var RESOLVED_STATIC_PATHS_ID = `${STATIC_PATHS_ID}.js`;
344
+ var NOT_FOUND_PATHS_ID = "@qwik-router-not-found-paths";
345
+ var RESOLVED_NOT_FOUND_PATHS_ID = `${NOT_FOUND_PATHS_ID}.js`;
346
+
347
+ // packages/qwik-router/src/adapters/static/vite/index.ts
348
+ function staticAdapter(opts) {
349
+ return viteAdapter({
350
+ name: "static-generate",
351
+ origin: opts.origin,
352
+ ssg: {
353
+ include: ["/*"],
354
+ ...opts
355
+ }
356
+ });
357
+ }
358
+ // Annotate the CommonJS export names for ESM import in node:
359
+ 0 && (module.exports = {
360
+ staticAdapter
361
+ });
@@ -0,0 +1,10 @@
1
+ import type { StaticGenerateRenderOptions } from '../../../static';
2
+
3
+ /** @public */
4
+ export declare function staticAdapter(opts: StaticGenerateAdapterOptions): any;
5
+
6
+ /** @public */
7
+ export declare interface StaticGenerateAdapterOptions extends Omit<StaticGenerateRenderOptions, 'outDir'> {
8
+ }
9
+
10
+ export { }