@demiurgejs/core 0.1.0 → 0.1.1

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.
@@ -1,53 +1,54 @@
1
- import { existsSync as A } from "node:fs";
2
- import { mkdir as L, writeFile as U, readdir as C, readFile as G } from "node:fs/promises";
3
- import { dirname as W, resolve as f, relative as y, sep as $, join as B } from "node:path";
1
+ import { existsSync as U } from "node:fs";
2
+ import { mkdir as tt, writeFile as et, readdir as B, readFile as rt } from "node:fs/promises";
3
+ import { dirname as G, resolve as p, relative as g, sep as $, join as nt } from "node:path";
4
+ import ot from "magic-string";
4
5
  import { parseAst as Y } from "vite";
5
- import { b as q, d as K } from "../chunks/ssr-O3d7Q73U.js";
6
- import { r as z, f as Q, h as X, c as Z } from "../chunks/testing-BBnJ5WUR.js";
6
+ import { b as st, d as it } from "../chunks/ssr-BJejKlkZ.js";
7
+ import { e as x, r as at, f as ct, h as ut, c as lt } from "../chunks/testing-DR3eQAvY.js";
7
8
  import "react";
8
9
  import "react-dom/server";
9
10
  import "react/jsx-runtime";
10
- import { t as S, U as N, w as x, b as F, c as tt } from "../chunks/streaming-27pwhjGC.js";
11
- const m = "@demiurgejs/core";
12
- async function et(t) {
13
- const n = (await rt(t.routesDir)).map((o) => nt(t.routesDir, o)).filter((o) => !!o).sort((o, a) => o.pattern.localeCompare(a.pattern)), r = ot(n);
14
- return await L(W(t.outputFile), { recursive: !0 }), await U(t.outputFile, r), {
11
+ import { t as I, U as O, w as _, b as F, c as ft } from "../chunks/streaming-DN1DnbRE.js";
12
+ const h = "@demiurgejs/core";
13
+ async function dt(t) {
14
+ const n = (await pt(t.routesDir)).map((r) => mt(t.routesDir, r)).filter((r) => !!r).sort((r, s) => r.pattern.localeCompare(s.pattern)), o = yt(n);
15
+ return await tt(G(t.outputFile), { recursive: !0 }), await et(t.outputFile, o), {
15
16
  routes: n,
16
- source: r
17
+ source: o
17
18
  };
18
19
  }
19
- async function rt(t) {
20
- return (await C(t, {
20
+ async function pt(t) {
21
+ return (await B(t, {
21
22
  recursive: !0,
22
23
  withFileTypes: !0
23
- })).filter((n) => n.isFile() && /\.tsx?$/.test(n.name)).map((n) => f(n.parentPath, n.name));
24
+ })).filter((n) => n.isFile() && /\.tsx?$/.test(n.name)).map((n) => p(n.parentPath, n.name));
24
25
  }
25
- function nt(t, e) {
26
- const r = y(t, e).split($).join("/").replace(/\.tsx?$/, "").split("/");
27
- if (r.at(-1)?.startsWith("@"))
26
+ function mt(t, e) {
27
+ const o = g(t, e).split($).join("/").replace(/\.tsx?$/, "").split("/");
28
+ if (o.at(-1)?.startsWith("@"))
28
29
  return null;
29
- const a = r.flatMap((s) => O(s) ? [] : s === "index" ? [] : s.startsWith("[...") && s.endsWith("]") ? [s] : s.startsWith("[") && s.endsWith("]") ? [s] : [s]);
30
+ const s = o.flatMap((i) => P(i) ? [] : i === "index" ? [] : i.startsWith("[...") && i.endsWith("]") ? [i] : i.startsWith("[") && i.endsWith("]") ? [i] : [i]);
30
31
  return {
31
- pathVars: r.flatMap((s) => O(s) ? [] : s.startsWith("[...") && s.endsWith("]") ? [s.slice(4, -1)] : s.startsWith("[") && s.endsWith("]") ? [s.slice(1, -1)] : []),
32
- pattern: `/${a.join("/")}`.replace(/\/$/, "") || "/"
32
+ pathVars: o.flatMap((i) => P(i) ? [] : i.startsWith("[...") && i.endsWith("]") ? [i.slice(4, -1)] : i.startsWith("[") && i.endsWith("]") ? [i.slice(1, -1)] : []),
33
+ pattern: `/${s.join("/")}`.replace(/\/$/, "") || "/"
33
34
  };
34
35
  }
35
- function O(t) {
36
+ function P(t) {
36
37
  return t.startsWith("(") && t.endsWith(")");
37
38
  }
38
- function ot(t) {
39
+ function yt(t) {
39
40
  return `// Generated by Demiurge. Do not edit by hand.
40
- import "${m}";
41
- import type { PathValue } from "${m}";
41
+ import "${h}";
42
+ import type { PathValue } from "${h}";
42
43
 
43
- declare module "${m}" {
44
+ declare module "${h}" {
44
45
  interface RoutePathVars {
45
- ${t.map(st).join(`
46
+ ${t.map(ht).join(`
46
47
  `)}
47
48
  }
48
49
 
49
50
  interface RouteConcretePaths {
50
- ${t.map(at).join(`
51
+ ${t.map(gt).join(`
51
52
  `)}
52
53
  }
53
54
  }
@@ -55,61 +56,64 @@ ${t.map(at).join(`
55
56
  export {};
56
57
  `;
57
58
  }
58
- function st(t) {
59
+ function ht(t) {
59
60
  if (t.pathVars.length === 0)
60
61
  return ` ${JSON.stringify(t.pattern)}: {};`;
61
62
  const e = t.pathVars.map((n) => `${n}: PathValue`).join("; ");
62
63
  return ` ${JSON.stringify(t.pattern)}: { ${e} };`;
63
64
  }
64
- function at(t) {
65
- return ` ${JSON.stringify(t.pattern)}: ${it(t.pattern)};`;
65
+ function gt(t) {
66
+ return ` ${JSON.stringify(t.pattern)}: ${Rt(t.pattern)};`;
66
67
  }
67
- function it(t) {
68
- return t.includes("[") ? `\`${t.split(/(\[\.\.\.[^\]]+\]|\[[^\]]+\])/g).filter(Boolean).map((n) => n.startsWith("[") ? "${PathValue}" : ut(n)).join("")}\`` : JSON.stringify(t);
68
+ function Rt(t) {
69
+ return t.includes("[") ? `\`${t.split(/(\[\.\.\.[^\]]+\]|\[[^\]]+\])/g).filter(Boolean).map((n) => n.startsWith("[") ? "${PathValue}" : wt(n)).join("")}\`` : JSON.stringify(t);
69
70
  }
70
- function ut(t) {
71
+ function wt(t) {
71
72
  return t.replace(/[`$\\]/g, "\\$&");
72
73
  }
73
- const g = "virtual:demiurge/client-entry", E = `\0${g}`, V = "virtual:demiurge/server-entry", P = `\0${V}`, I = ".demiurge/route-manifest.d.ts";
74
- function Yt(t = {}) {
74
+ const E = "virtual:demiurge/client-entry", v = `/@id/${E}`, D = `\0${E}`, q = "virtual:demiurge/server-entry", M = `\0${q}`, C = ".demiurge/route-manifest.d.ts";
75
+ function ue(t = {}) {
75
76
  let e = process.cwd(), n = !1;
77
+ const o = `demiurge-${x()}`;
76
78
  return {
77
79
  enforce: "post",
78
80
  name: "demiurge",
79
- config(r) {
80
- return gt(r);
81
+ config(r, s) {
82
+ return It(r, s, o);
81
83
  },
82
84
  configResolved(r) {
83
85
  e = r.root, n = r.command === "build";
84
86
  },
85
87
  resolveId(r) {
86
- return r === g ? E : r === V ? P : null;
88
+ return r === E ? D : r === q ? M : null;
87
89
  },
88
90
  load(r) {
89
- return r === E ? Rt(e, t) : r === P ? $t(e, t) : null;
91
+ return r === D ? Ot(e, t) : r === M ? _t(e, t) : null;
90
92
  },
91
93
  transform: {
92
94
  order: "post",
93
- handler(r, o, a) {
94
- if (a?.ssr || !ft(e, t, o))
95
+ handler(r, s, a) {
96
+ if (!Et(e, t, s))
95
97
  return null;
96
- const i = ct(r);
98
+ if (a?.ssr)
99
+ return n ? null : $t(r, s);
100
+ const i = bt(r);
97
101
  return i === r ? null : { code: i, map: null };
98
102
  }
99
103
  },
100
- generateBundle(r, o) {
101
- const a = Object.values(o).find(
102
- (u) => u.type === "chunk" && u.isEntry && u.facadeModuleId === E
104
+ generateBundle(r, s) {
105
+ const a = Object.values(s).find(
106
+ (c) => c.type === "chunk" && c.isEntry && c.facadeModuleId === D
103
107
  );
104
108
  if (!a)
105
109
  return;
106
- const i = Object.values(o).filter(
107
- (u) => u.type === "asset" && u.fileName.endsWith(".css")
108
- ).map((u) => `/${u.fileName}`), s = `/${a.fileName}`;
110
+ const i = Object.values(s).filter(
111
+ (c) => c.type === "asset" && c.fileName.endsWith(".css")
112
+ ).map((c) => `/${c.fileName}`), u = `/${a.fileName}`;
109
113
  this.emitFile({
110
114
  fileName: "index.html",
111
- source: vt({
112
- entrySrc: s,
115
+ source: qt({
116
+ entrySrc: u,
113
117
  lang: t.document?.lang,
114
118
  styles: i,
115
119
  title: t.document?.title
@@ -117,221 +121,262 @@ function Yt(t = {}) {
117
121
  type: "asset"
118
122
  }), this.emitFile({
119
123
  fileName: "demiurge-manifest.json",
120
- source: JSON.stringify({ clientEntry: s, styles: i }, null, 2),
124
+ source: JSON.stringify({ clientEntry: u, styles: i }, null, 2),
121
125
  type: "asset"
122
126
  });
123
127
  },
124
128
  async buildStart() {
125
- n && await Ft(e, t), t.typedRoutes && await R(e, t);
129
+ n && await Ht(e, t), t.typedRoutes && await w(e, t);
126
130
  },
127
131
  configureServer(r) {
128
132
  if (t.typedRoutes) {
129
- const o = f(
133
+ const s = p(
130
134
  r.config.root,
131
135
  t.routesDir ?? "src/routes"
132
136
  );
133
- R(r.config.root, t), r.watcher.add(o), r.watcher.on("add", (a) => {
134
- D(o, a) && R(r.config.root, t);
137
+ w(r.config.root, t), r.watcher.add(s), r.watcher.on("add", (a) => {
138
+ N(s, a) && w(r.config.root, t);
135
139
  }), r.watcher.on("change", (a) => {
136
- D(o, a) && R(r.config.root, t);
140
+ N(s, a) && w(r.config.root, t);
137
141
  }), r.watcher.on("unlink", (a) => {
138
- D(o, a) && R(r.config.root, t);
142
+ N(s, a) && w(r.config.root, t);
139
143
  });
140
144
  }
141
- return r.middlewares.use(async (o, a, i) => {
145
+ return r.middlewares.use(async (s, a, i) => {
142
146
  try {
143
- let s;
147
+ let u;
144
148
  try {
145
- s = S(o, {
149
+ u = I(s, {
146
150
  // Vite performs its own configured host allowlist check before
147
151
  // Demiurge's middleware runs. Production Node listeners require
148
152
  // an explicit application allowlist instead.
149
- allowedHosts: [o.headers.host ?? "localhost"]
153
+ allowedHosts: [s.headers.host ?? "localhost"]
150
154
  });
151
- } catch (l) {
152
- if (l instanceof N) {
153
- x(a);
155
+ } catch (R) {
156
+ if (R instanceof O) {
157
+ _(a);
154
158
  return;
155
159
  }
156
- throw l;
160
+ throw R;
157
161
  }
158
- const u = f(
162
+ const c = p(
159
163
  r.config.root,
160
164
  t.routesDir ?? "src/routes"
161
- ), c = await v(r, o, u), p = await Tt(
162
- c,
163
- s,
164
- St(r, t)
165
+ ), l = await k(r, s, c), d = j(
166
+ r,
167
+ o
168
+ ), y = await zt(
169
+ l,
170
+ u,
171
+ Tt(t, d.transform)
165
172
  );
166
- if (p === "next") {
173
+ if (y === "next") {
167
174
  i();
168
175
  return;
169
176
  }
170
- await F(a, p);
171
- } catch (s) {
172
- i(s);
177
+ W(y, d.nonce), await F(a, y);
178
+ } catch (u) {
179
+ i(u);
173
180
  }
174
181
  }), () => {
175
- r.middlewares.use(async (o, a, i) => {
182
+ r.middlewares.use(async (s, a, i) => {
176
183
  try {
177
- let s;
184
+ let u;
178
185
  try {
179
- s = S(o, {
180
- allowedHosts: [o.headers.host ?? "localhost"]
186
+ u = I(s, {
187
+ allowedHosts: [s.headers.host ?? "localhost"]
181
188
  });
182
- } catch (p) {
183
- if (p instanceof N) {
184
- x(a);
189
+ } catch (R) {
190
+ if (R instanceof O) {
191
+ _(a);
185
192
  return;
186
193
  }
187
- throw p;
194
+ throw R;
188
195
  }
189
- const u = f(
196
+ const c = p(
190
197
  r.config.root,
191
198
  t.routesDir ?? "src/routes"
192
- ), c = await v(r, o, u);
193
- xt(c, u), await F(
194
- a,
195
- await z(
196
- c,
197
- s,
198
- Nt(r, t)
199
- )
199
+ ), l = await k(r, s, c);
200
+ kt(l, c);
201
+ const d = j(
202
+ r,
203
+ o
204
+ ), y = await at(
205
+ l,
206
+ u,
207
+ Lt(t, d.transform)
200
208
  );
201
- } catch (s) {
202
- i(s);
209
+ W(
210
+ y,
211
+ d.nonce
212
+ ), await F(a, y);
213
+ } catch (u) {
214
+ i(u);
203
215
  }
204
216
  });
205
217
  };
206
218
  }
207
219
  };
208
220
  }
209
- function ct(t) {
210
- const e = Y(t), n = [], r = lt(e), o = [];
211
- w(e, (s) => {
212
- if (s.type === "ImportDeclaration" && pt(s)) {
213
- o.push(s);
221
+ function $t(t, e) {
222
+ if (!t.includes("import.meta"))
223
+ return null;
224
+ const n = Y(t), o = m(n.body).find(
225
+ (c) => c.type === "ImportDeclaration" && m(c.specifiers).some(
226
+ (l) => f(l.local)?.type === "Identifier" && f(l.local)?.name === "meta"
227
+ )
228
+ ), r = o && m(o.specifiers).find(
229
+ (c) => f(c.local)?.type === "Identifier" && f(c.local)?.name === "meta"
230
+ ), s = r && f(r.local);
231
+ if (!o || !r || !s)
232
+ return null;
233
+ let a = "__demiurge_imported_meta__";
234
+ for (; new RegExp(`\\b${a}\\b`).test(t); )
235
+ a += "_";
236
+ const i = new ot(t), u = f(r.imported);
237
+ return i.overwrite(
238
+ s.start,
239
+ s.end,
240
+ u?.start === s.start ? `meta as ${a}` : a
241
+ ), i.appendLeft(o.end, `
242
+ const meta = ${a};`), {
243
+ code: i.toString(),
244
+ map: i.generateMap({
245
+ hires: "boundary",
246
+ includeContent: !0,
247
+ source: e
248
+ })
249
+ };
250
+ }
251
+ function bt(t) {
252
+ const e = Y(t), n = [], o = St(e), r = [];
253
+ b(e, (i) => {
254
+ if (i.type === "ImportDeclaration" && Nt(i)) {
255
+ r.push(i);
214
256
  return;
215
257
  }
216
- if (s.type !== "CallExpression" || !dt(s.callee))
258
+ if (i.type !== "CallExpression" || !Dt(i.callee))
217
259
  return;
218
- const u = h(s.arguments)[0];
260
+ const u = m(i.arguments)[0];
219
261
  if (u?.type === "ObjectExpression")
220
- for (const c of h(u.properties))
221
- c.type === "Property" && b(c.key) === "data" && n.push({ end: c.end, start: c.start });
262
+ for (const c of m(u.properties))
263
+ c.type === "Property" && S(c.key) === "data" && n.push({ end: c.end, start: c.start });
222
264
  });
223
- const a = [...n, ...r];
224
- if (a.length === 0)
265
+ const s = [...n, ...o];
266
+ if (s.length === 0)
225
267
  return t;
226
- const i = [...a];
227
- for (const s of o) {
228
- const u = h(s.specifiers).map((l) => d(l.local)).filter((l) => l?.type === "Identifier").map((l) => String(l.name)), c = mt(t, [
229
- ...a,
230
- { end: s.end, start: s.start }
231
- ]), p = u.find(
232
- (l) => new RegExp(`\\b${yt(l)}\\b`).test(c)
268
+ const a = [...s];
269
+ for (const i of r) {
270
+ const u = m(i.specifiers).map((d) => f(d.local)).filter((d) => d?.type === "Identifier").map((d) => String(d.name)), c = xt(t, [
271
+ ...s,
272
+ { end: i.end, start: i.start }
273
+ ]), l = u.find(
274
+ (d) => new RegExp(`\\b${K(d)}\\b`).test(c)
233
275
  );
234
- if (p)
276
+ if (l)
235
277
  throw new Error(
236
- `Server-only import ${JSON.stringify(p)} is used by client route code. Move that use into page data, a document contribution, or a client-safe module.`
278
+ `Server-only import ${JSON.stringify(l)} is used by client route code. Move that use into page data, a document contribution, or a client-safe module.`
237
279
  );
238
- i.push({ end: s.end, start: s.start });
280
+ a.push({ end: i.end, start: i.start });
239
281
  }
240
- return ht(
282
+ return vt(
241
283
  t,
242
- i,
243
- (s) => n.includes(s) ? "data: undefined" : r.includes(s) ? "undefined" : ""
284
+ a,
285
+ (i) => n.includes(i) ? "data: undefined" : o.includes(i) ? "undefined" : ""
244
286
  );
245
287
  }
246
- const M = /* @__PURE__ */ new Set(["links", "metadata", "scripts"]);
247
- function lt(t) {
288
+ const T = /* @__PURE__ */ new Set(["links", "metadata", "scripts"]);
289
+ function St(t) {
248
290
  const e = /* @__PURE__ */ new Set();
249
- for (const r of h(t.body)) {
250
- if (r.type !== "ExportNamedDeclaration") continue;
251
- const o = d(r.declaration);
252
- if (o?.type === "VariableDeclaration")
253
- for (const a of h(o.declarations)) {
254
- const i = d(a.id);
255
- i?.type === "Identifier" && M.has(String(i.name)) && e.add(String(i.name));
291
+ for (const o of m(t.body)) {
292
+ if (o.type !== "ExportNamedDeclaration") continue;
293
+ const r = f(o.declaration);
294
+ if (r?.type === "VariableDeclaration")
295
+ for (const s of m(r.declarations)) {
296
+ const a = f(s.id);
297
+ a?.type === "Identifier" && T.has(String(a.name)) && e.add(String(a.name));
256
298
  }
257
- for (const a of h(r.specifiers)) {
258
- const i = b(a.exported), s = b(a.local);
259
- i && s && M.has(i) && e.add(s);
299
+ for (const s of m(o.specifiers)) {
300
+ const a = S(s.exported), i = S(s.local);
301
+ a && i && T.has(a) && e.add(i);
260
302
  }
261
303
  }
262
304
  const n = [];
263
- return w(t, (r) => {
264
- if (r.type !== "VariableDeclarator") return;
265
- const o = d(r.id), a = d(r.init);
266
- o?.type === "Identifier" && e.has(String(o.name)) && a && n.push({ end: a.end, start: a.start });
305
+ return b(t, (o) => {
306
+ if (o.type !== "VariableDeclarator") return;
307
+ const r = f(o.id), s = f(o.init);
308
+ r?.type === "Identifier" && e.has(String(r.name)) && s && n.push({ end: s.end, start: s.start });
267
309
  }), n;
268
310
  }
269
- function ft(t, e, n) {
270
- const r = n.split("?", 1)[0] ?? n, o = f(t, e.routesDir ?? "src/routes"), a = y(o, r);
271
- return a !== "" && a !== ".." && !a.startsWith(`..${$}`);
311
+ function Et(t, e, n) {
312
+ const o = n.split("?", 1)[0] ?? n, r = p(t, e.routesDir ?? "src/routes"), s = g(r, o);
313
+ return s !== "" && s !== ".." && !s.startsWith(`..${$}`);
272
314
  }
273
- function w(t, e) {
315
+ function b(t, e) {
274
316
  if (!t || typeof t != "object") return;
275
317
  if (Array.isArray(t)) {
276
- for (const r of t) w(r, e);
318
+ for (const o of t) b(o, e);
277
319
  return;
278
320
  }
279
321
  const n = t;
280
322
  if (typeof n.type == "string") {
281
323
  e(n);
282
- for (const [r, o] of Object.entries(n))
283
- (/* @__PURE__ */ new Set(["end", "loc", "start", "type"])).has(r) || w(o, e);
324
+ for (const [o, r] of Object.entries(n))
325
+ (/* @__PURE__ */ new Set(["end", "loc", "start", "type"])).has(o) || b(r, e);
284
326
  }
285
327
  }
286
- function d(t) {
328
+ function f(t) {
287
329
  return t && typeof t == "object" && "type" in t ? t : void 0;
288
330
  }
289
- function h(t) {
290
- return Array.isArray(t) ? t.map(d).filter((e) => !!e) : [];
331
+ function m(t) {
332
+ return Array.isArray(t) ? t.map(f).filter((e) => !!e) : [];
291
333
  }
292
- function dt(t) {
293
- const e = d(t);
294
- return e?.type === "Identifier" ? e.name === "page" : e?.type === "MemberExpression" && b(e.property) === "page";
334
+ function Dt(t) {
335
+ const e = f(t);
336
+ return e?.type === "Identifier" ? e.name === "page" : e?.type === "MemberExpression" && S(e.property) === "page";
295
337
  }
296
- function b(t) {
297
- const e = d(t);
338
+ function S(t) {
339
+ const e = f(t);
298
340
  return e?.type === "Identifier" || e?.type === "Literal" ? String(e.name ?? e.value) : void 0;
299
341
  }
300
- function pt(t) {
301
- const e = d(t.source)?.value;
342
+ function Nt(t) {
343
+ const e = f(t.source)?.value;
302
344
  return typeof e == "string" && /(?:^|[/.-])server(?:[/.-]|$)/.test(e);
303
345
  }
304
- function mt(t, e) {
346
+ function xt(t, e) {
305
347
  const n = [...t];
306
- for (const { end: r, start: o } of e)
307
- n.fill(" ", o, r);
348
+ for (const { end: o, start: r } of e)
349
+ n.fill(" ", r, o);
308
350
  return n.join("");
309
351
  }
310
- function ht(t, e, n) {
311
- return [...e].sort((r, o) => o.start - r.start).reduce(
312
- (r, o) => `${r.slice(0, o.start)}${n(o)}${r.slice(o.end)}`,
352
+ function vt(t, e, n) {
353
+ return [...e].sort((o, r) => r.start - o.start).reduce(
354
+ (o, r) => `${o.slice(0, r.start)}${n(r)}${o.slice(r.end)}`,
313
355
  t
314
356
  );
315
357
  }
316
- function yt(t) {
358
+ function K(t) {
317
359
  return t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
318
360
  }
319
- function gt(t) {
361
+ function It(t, e, n) {
320
362
  return {
321
363
  appType: "custom",
322
364
  build: {
365
+ assetsInlineLimit: t.build?.assetsInlineLimit ?? 0,
366
+ ...e.isSsrBuild && t.build?.target === void 0 && t.ssr?.target !== "webworker" ? { target: "node22.13" } : {},
323
367
  rollupOptions: {
324
- input: t.build?.rollupOptions?.input ?? g
368
+ input: t.build?.rollupOptions?.input ?? E
325
369
  }
326
- }
370
+ },
371
+ ...e.command === "serve" ? { html: { cspNonce: n } } : {}
327
372
  };
328
373
  }
329
- function Rt(t, e = {}) {
330
- const n = e.routesDir ?? "src/routes", r = Dt(t, e);
331
- return `import { hydrateFileRouter } from "${m}";
332
- ${r}
374
+ function Ot(t, e = {}) {
375
+ const n = e.routesDir ?? "src/routes", o = Ct(t, e);
376
+ return `import { hydrateFileRouter } from "${h}";
377
+ ${o}
333
378
 
334
- ${k(n, {
379
+ ${z(n, {
335
380
  exportRoutes: !1,
336
381
  includeServerOnly: !1
337
382
  })}
@@ -339,11 +384,11 @@ ${k(n, {
339
384
  void hydrateFileRouter({ routes });
340
385
  `;
341
386
  }
342
- function $t(t, e = {}) {
387
+ function _t(t, e = {}) {
343
388
  const n = e.routesDir ?? "src/routes";
344
- return `import { createRequestHandler } from "${m}";
389
+ return `import { createRequestHandler } from "${h}";
345
390
 
346
- ${k(n, {
391
+ ${z(n, {
347
392
  exportRoutes: !0,
348
393
  includeServerOnly: !0
349
394
  })}
@@ -364,13 +409,13 @@ export function createHandler(options = {}) {
364
409
  }
365
410
  `;
366
411
  }
367
- function k(t, {
412
+ function z(t, {
368
413
  exportRoutes: e,
369
414
  includeServerOnly: n
370
415
  }) {
371
- const r = bt(t, n), o = Et(t);
372
- return `const routeModules = import.meta.glob(${JSON.stringify(r)});
373
- const routePrefix = ${JSON.stringify(o)};
416
+ const o = Pt(t, n), r = Mt(t);
417
+ return `const routeModules = import.meta.glob(${JSON.stringify(o)});
418
+ const routePrefix = ${JSON.stringify(r)};
374
419
  ${e ? "export " : ""}const routes = Object.fromEntries(
375
420
  Object.entries(routeModules).map(([file, load]) => [
376
421
  \`./routes/\${file.slice(routePrefix.length)}\`,
@@ -378,120 +423,241 @@ ${e ? "export " : ""}const routes = Object.fromEntries(
378
423
  ]),
379
424
  );`;
380
425
  }
381
- const wt = ["@middleware.ts", "@policy.ts"];
382
- function bt(t, e) {
383
- const n = `/${t.replace(/^\/|\/$/g, "")}`, r = [`${n}/**/*.{ts,tsx}`];
384
- return e || r.push(
385
- ...wt.flatMap((o) => [
386
- `!${n}/${o}`,
387
- `!${n}/**/${o}`
426
+ const Ft = ["@middleware.ts", "@policy.ts"];
427
+ function Pt(t, e) {
428
+ const n = `/${t.replace(/^\/|\/$/g, "")}`, o = [`${n}/**/*.{ts,tsx}`];
429
+ return e || o.push(
430
+ ...Ft.flatMap((r) => [
431
+ `!${n}/${r}`,
432
+ `!${n}/**/${r}`
388
433
  ])
389
- ), r;
434
+ ), o;
390
435
  }
391
- function Et(t) {
436
+ function Mt(t) {
392
437
  return `/${t.replace(/^\/|\/$/g, "")}/`;
393
438
  }
394
- function Dt(t, e) {
439
+ function Ct(t, e) {
395
440
  if (e.styles === !1)
396
441
  return "";
397
- const n = e.styles ?? "src/styles.css", r = f(t, n);
398
- return A(r) ? `import ${JSON.stringify(`/${n.replace(/^\/+/, "")}`)};` : "";
442
+ const n = e.styles ?? "src/styles.css", o = p(t, n);
443
+ return U(o) ? `import ${JSON.stringify(`/${n.replace(/^\/+/, "")}`)};` : "";
399
444
  }
400
- function St(t, e) {
445
+ function Tt(t, e) {
401
446
  return {
402
447
  dev: !0,
403
- ssr: H(e),
404
- transformDocument: (n) => t.transformIndexHtml("/", n),
405
- renderPage: async (n, r) => {
448
+ ssr: X(t),
449
+ transformDocument: e,
450
+ renderPage: async (n, o) => {
406
451
  if (n.render.mode === "streaming")
407
- return await tt(n, {
408
- ...r,
409
- clientEntry: `/${g}`,
410
- transformDocument: (a) => t.transformIndexHtml("/", a)
452
+ return await ft(n, {
453
+ ...o,
454
+ clientEntry: v,
455
+ transformDocument: e
411
456
  });
412
- const o = await t.transformIndexHtml(
413
- "/",
414
- K(n, {
415
- ...r,
416
- clientEntry: `/${g}`
457
+ const r = await e(
458
+ it(n, {
459
+ ...o,
460
+ clientEntry: v
417
461
  })
418
462
  );
419
- return new Response(o, {
463
+ return new Response(r, {
420
464
  headers: { "content-type": "text/html; charset=utf-8" }
421
465
  });
422
466
  }
423
467
  };
424
468
  }
425
- const _ = Symbol.for("demiurge.devManifest");
426
- async function v(t, e, n) {
427
- const r = e, o = r[_];
428
- if (o)
429
- return o;
430
- const a = Z(
431
- await jt(t, n)
469
+ function j(t, e) {
470
+ const n = x(), o = x();
471
+ return {
472
+ nonce: n,
473
+ transform: async (r) => {
474
+ const s = jt(r, o), a = await t.transformIndexHtml("/", s);
475
+ return A(
476
+ A(a, o),
477
+ e,
478
+ n
479
+ );
480
+ }
481
+ };
482
+ }
483
+ function jt(t, e) {
484
+ const n = t.toLowerCase();
485
+ let o = 0, r = "";
486
+ for (; o < t.length; ) {
487
+ const s = t.indexOf("<", o);
488
+ if (s === -1)
489
+ return r + t.slice(o);
490
+ if (r += t.slice(o, s), n.startsWith("<!--", s)) {
491
+ const c = n.indexOf("-->", s + 4), l = c === -1 ? t.length : c + 3;
492
+ r += t.slice(s, l), o = l;
493
+ continue;
494
+ }
495
+ const a = n.slice(s).match(/^<\s*(script|style|link)\b/)?.[1];
496
+ if (!a) {
497
+ r += "<", o = s + 1;
498
+ continue;
499
+ }
500
+ const i = At(t, s);
501
+ if (i === -1)
502
+ return r + t.slice(s);
503
+ const u = t.slice(s, i + 1);
504
+ if (r += /(?:^|\s)nonce(?:\s|=|>)/i.test(u) ? u : Wt(u, e), o = i + 1, a === "script" || a === "style") {
505
+ const c = n.indexOf(`</${a}`, o);
506
+ if (c === -1)
507
+ return r + t.slice(o);
508
+ r += t.slice(o, c), o = c;
509
+ }
510
+ }
511
+ return r;
512
+ }
513
+ function At(t, e) {
514
+ let n;
515
+ for (let o = e + 1; o < t.length; o++) {
516
+ const r = t[o];
517
+ if (n) {
518
+ r === n && (n = void 0);
519
+ continue;
520
+ }
521
+ if (r === '"' || r === "'")
522
+ n = r;
523
+ else if (r === ">")
524
+ return o;
525
+ }
526
+ return -1;
527
+ }
528
+ function Wt(t, e) {
529
+ const n = /\/\s*>$/.test(t) ? t.lastIndexOf("/") : t.length - 1;
530
+ return `${t.slice(0, n)} nonce="${e}"${t.slice(n)}`;
531
+ }
532
+ function A(t, e, n) {
533
+ const o = new RegExp(
534
+ `(\\snonce\\s*=\\s*)(["'])${K(e)}\\2`,
535
+ "g"
536
+ );
537
+ return t.replace(
538
+ o,
539
+ n ? `$1"${n}"` : ""
432
540
  );
433
- return r[_] = a, a;
434
541
  }
435
- function H(t) {
542
+ function W(t, e) {
543
+ const n = t.headers.get("content-type"), o = t.headers.get("content-security-policy");
544
+ if (!n?.toLowerCase().startsWith("text/html") || !o)
545
+ return;
546
+ const r = `'nonce-${e}'`, s = V(
547
+ o,
548
+ ["script-src-elem", "script-src"],
549
+ "script-src",
550
+ r
551
+ ), a = Vt(
552
+ s,
553
+ ["style-src-elem", "style-src"]
554
+ ) ? s : V(
555
+ s,
556
+ ["style-src-elem", "style-src"],
557
+ "style-src",
558
+ r
559
+ );
560
+ t.headers.set("content-security-policy", a), t.headers.set("cache-control", "private, no-store");
561
+ }
562
+ function V(t, e, n, o) {
563
+ const r = t.split(";").map((l) => l.trim()), s = Q(r, e), a = r.find(
564
+ (l) => l.split(/\s+/, 1)[0]?.toLowerCase() === "default-src"
565
+ );
566
+ if (s === -1 && !a) return t;
567
+ const [i, ...u] = (s === -1 ? a : r[s]).split(/\s+/), c = [
568
+ s === -1 ? n : i,
569
+ .../* @__PURE__ */ new Set([
570
+ ...u.filter((l) => l !== "'none'"),
571
+ o
572
+ ])
573
+ ].join(" ");
574
+ return s === -1 ? r.push(c) : r[s] = c, r.join("; ");
575
+ }
576
+ function Vt(t, e) {
577
+ const n = t.split(";").map((a) => a.trim()), o = Q(n, e), s = (o === -1 ? n.find(
578
+ (a) => a.split(/\s+/, 1)[0]?.toLowerCase() === "default-src"
579
+ ) : n[o])?.split(/\s+/).slice(1) ?? [];
580
+ return s.includes("'unsafe-inline'") && !s.some((a) => /^'(?:nonce-|sha(?:256|384|512)-)/i.test(a));
581
+ }
582
+ function Q(t, e) {
583
+ for (const n of e) {
584
+ const o = t.findIndex(
585
+ (r) => r.split(/\s+/, 1)[0]?.toLowerCase() === n
586
+ );
587
+ if (o !== -1) return o;
588
+ }
589
+ return -1;
590
+ }
591
+ const L = Symbol.for("demiurge.devManifest");
592
+ async function k(t, e, n) {
593
+ const o = e, r = o[L];
594
+ if (r)
595
+ return r;
596
+ const s = lt(
597
+ await Kt(t, n)
598
+ );
599
+ return o[L] = s, s;
600
+ }
601
+ function X(t) {
436
602
  return {
437
- clientEntry: `/${g}`,
603
+ clientEntry: v,
438
604
  lang: t.document?.lang,
439
605
  title: t.document?.title
440
606
  };
441
607
  }
442
- function Nt(t, e) {
608
+ function Lt(t, e) {
443
609
  return {
444
- ...H(e),
610
+ ...X(t),
445
611
  dev: !0,
446
- transformDocument: (n) => t.transformIndexHtml("/", n)
612
+ transformDocument: e
447
613
  };
448
614
  }
449
- const j = /* @__PURE__ */ new Set();
450
- function xt(t, e) {
451
- j.has(e) || t.fallbacks.notFound.some(
615
+ const H = /* @__PURE__ */ new Set();
616
+ function kt(t, e) {
617
+ H.has(e) || t.fallbacks.notFound.some(
452
618
  (n) => n.fileSegments.length === 0
453
- ) || (j.add(e), console.warn(_t(B(e, "@not-found.tsx"))));
619
+ ) || (H.add(e), console.warn(Yt(nt(e, "@not-found.tsx"))));
454
620
  }
455
- async function Ft(t, e = {}) {
456
- const n = e.routesDir ?? "src/routes", r = f(t, n);
457
- if (!A(r))
621
+ async function Ht(t, e = {}) {
622
+ const n = e.routesDir ?? "src/routes", o = p(t, n);
623
+ if (!U(o))
458
624
  return;
459
- const o = await J(r);
460
- if (o.map(
461
- (s) => y(r, s).split($).join("/")
462
- ).some((s) => /^@not-found\.tsx?$/.test(s)))
625
+ const r = await Z(o);
626
+ if (r.map(
627
+ (i) => g(o, i).split($).join("/")
628
+ ).some((i) => /^@not-found\.tsx?$/.test(i)))
463
629
  return;
464
- const i = await Ot(o);
465
- if (i)
630
+ const a = await Jt(r);
631
+ if (a)
466
632
  throw new Error(
467
- Mt(
633
+ Gt(
468
634
  n,
469
- y(r, i).split($).join("/")
635
+ g(o, a).split($).join("/")
470
636
  )
471
637
  );
472
638
  }
473
- async function Ot(t) {
639
+ async function Jt(t) {
474
640
  for (const e of t)
475
- if (!y(W(e), e).startsWith("@") && It(await G(e, "utf8")))
641
+ if (!g(G(e), e).startsWith("@") && Bt(await rt(e, "utf8")))
476
642
  return e;
477
643
  }
478
- const Pt = m.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), T = new RegExp(
479
- `import\\s*\\{([^}]*)\\}\\s*from\\s*["']${Pt}["']`,
644
+ const Ut = h.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), J = new RegExp(
645
+ `import\\s*\\{([^}]*)\\}\\s*from\\s*["']${Ut}["']`,
480
646
  "g"
481
647
  );
482
- function It(t) {
483
- const e = [...t.matchAll(T)].flatMap(
484
- (r) => r[1].split(",").flatMap((o) => {
485
- const [a, i] = o.trim().split(/\s+as\s+/);
486
- return a.replace(/^type\s+/, "") === "page" ? [i ?? a] : [];
648
+ function Bt(t) {
649
+ const e = [...t.matchAll(J)].flatMap(
650
+ (o) => o[1].split(",").flatMap((r) => {
651
+ const [s, a] = r.trim().split(/\s+as\s+/);
652
+ return s.replace(/^type\s+/, "") === "page" ? [a ?? s] : [];
487
653
  })
488
654
  );
489
655
  if (e.length === 0)
490
656
  return !1;
491
- const n = t.replace(T, "");
492
- return e.some((r) => new RegExp(`\\b${r}\\s*\\(`).test(n));
657
+ const n = t.replace(J, "");
658
+ return e.some((o) => new RegExp(`\\b${o}\\s*\\(`).test(n));
493
659
  }
494
- function Mt(t, e) {
660
+ function Gt(t, e) {
495
661
  return `Demiurge will not build an app with page routes and no ${t}/@not-found.tsx.
496
662
 
497
663
  "${e}" declares a page, so a browser can reach a path this app does not serve. Decide what that looks like rather than shipping the framework built-in.
@@ -504,7 +670,7 @@ Create ${t}/@not-found.tsx:
504
670
 
505
671
  It renders inside the layouts above the requested path. Opt out with "export const layout = false".`;
506
672
  }
507
- function _t(t) {
673
+ function Yt(t) {
508
674
  return `Demiurge is serving its built-in 404 because ${t} does not exist.
509
675
 
510
676
  Create it before building for production:
@@ -514,67 +680,67 @@ Create it before building for production:
514
680
  }
515
681
  `;
516
682
  }
517
- function vt({
683
+ function qt({
518
684
  entrySrc: t,
519
685
  lang: e,
520
686
  links: n,
521
- metadata: r,
522
- nonce: o,
523
- scripts: a,
524
- styles: i,
525
- title: s
687
+ metadata: o,
688
+ nonce: r,
689
+ scripts: s,
690
+ styles: a,
691
+ title: i
526
692
  }) {
527
- return q({
693
+ return st({
528
694
  entrySrc: t,
529
695
  lang: e,
530
696
  links: n,
531
- metadata: r,
532
- nonce: o,
533
- scripts: a,
534
- styles: i,
535
- title: s
697
+ metadata: o,
698
+ nonce: r,
699
+ scripts: s,
700
+ styles: a,
701
+ title: i
536
702
  });
537
703
  }
538
- async function R(t, e) {
539
- const n = f(t, e.routesDir ?? "src/routes"), r = f(
704
+ async function w(t, e) {
705
+ const n = p(t, e.routesDir ?? "src/routes"), o = p(
540
706
  t,
541
- typeof e.typedRoutes == "object" ? e.typedRoutes.outputFile ?? I : I
707
+ typeof e.typedRoutes == "object" ? e.typedRoutes.outputFile ?? C : C
542
708
  );
543
- await et({ outputFile: r, routesDir: n });
709
+ await dt({ outputFile: o, routesDir: n });
544
710
  }
545
- function D(t, e) {
711
+ function N(t, e) {
546
712
  return e.startsWith(t) && /\.tsx?$/.test(e);
547
713
  }
548
- async function jt(t, e) {
549
- const n = await J(e), r = {};
550
- for (const o of n) {
551
- const a = At(e, o);
552
- r[a] = async () => await t.ssrLoadModule(o);
714
+ async function Kt(t, e) {
715
+ const n = await Z(e), o = {};
716
+ for (const r of n) {
717
+ const s = Qt(e, r);
718
+ o[s] = async () => await t.ssrLoadModule(r);
553
719
  }
554
- return r;
720
+ return o;
555
721
  }
556
- async function Tt(t, e, n = {}) {
557
- const r = new URL(e.url);
558
- return Q(t.routes, r.pathname) ? await X(t, e, n) : "next";
722
+ async function zt(t, e, n = {}) {
723
+ const o = new URL(e.url);
724
+ return ct(t.routes, o.pathname) ? await ut(t, e, n) : "next";
559
725
  }
560
- async function J(t) {
561
- return (await C(t, {
726
+ async function Z(t) {
727
+ return (await B(t, {
562
728
  recursive: !0,
563
729
  withFileTypes: !0
564
- })).filter((n) => n.isFile() && /\.tsx?$/.test(n.name)).map((n) => f(n.parentPath, n.name));
730
+ })).filter((n) => n.isFile() && /\.tsx?$/.test(n.name)).map((n) => p(n.parentPath, n.name));
565
731
  }
566
- function At(t, e) {
567
- return `./routes/${y(t, e).split($).join("/")}`;
732
+ function Qt(t, e) {
733
+ return `./routes/${g(t, e).split($).join("/")}`;
568
734
  }
569
735
  export {
570
- Yt as demiurge,
571
- Ft as unstable_assertRootNotFoundRoute,
572
- Rt as unstable_createClientEntrySource,
573
- jt as unstable_createDevRouteImporters,
574
- vt as unstable_createDocumentHtml,
575
- $t as unstable_createServerEntrySource,
576
- It as unstable_declaresPageRoute,
577
- Tt as unstable_handleDevRequest,
578
- ct as unstable_stripClientPageData
736
+ ue as demiurge,
737
+ Ht as unstable_assertRootNotFoundRoute,
738
+ Ot as unstable_createClientEntrySource,
739
+ Kt as unstable_createDevRouteImporters,
740
+ qt as unstable_createDocumentHtml,
741
+ _t as unstable_createServerEntrySource,
742
+ Bt as unstable_declaresPageRoute,
743
+ zt as unstable_handleDevRequest,
744
+ bt as unstable_stripClientPageData
579
745
  };
580
746
  //# sourceMappingURL=index.js.map