@yannosay/sinth 5.3.1 → 5.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +40 -0
- package/README.md +13 -3
- package/dist/browser/sinth.js +84 -66
- package/dist/cli/index.js +17 -7
- package/dist/compiler/codegen.js +47 -11
- package/dist/compiler/prerender.d.ts +1 -0
- package/dist/compiler/prerender.js +12 -5
- package/dist/deploy/cloudflare.d.ts +2 -0
- package/dist/deploy/cloudflare.js +103 -18
- package/dist/deploy/platforms.d.ts +20 -0
- package/dist/deploy/platforms.js +203 -0
- package/dist/project/build.js +62 -18
- package/dist/project/config.d.ts +13 -0
- package/dist/project/config.js +51 -6
- package/dist/project/project.d.ts +7 -1
- package/dist/project/project.js +24 -10
- package/dist/runtime/index.js +41 -23
- package/dist/runtime/router.js +17 -1
- package/dist/server/dev.js +3 -2
- package/dist/server/functions-router.d.ts +60 -0
- package/dist/server/functions-router.js +161 -0
- package/dist/server/functions.d.ts +6 -11
- package/dist/server/functions.js +28 -116
- package/package.json +2 -1
- package/schema/sinth.config.schema.json +3 -1
- package/skills/agents.md +56 -0
- package/skills/sinth/SKILL.md +83 -0
- package/skills/sinth/references/language.md +174 -0
- package/skills/sinth/references/project-and-deploy.md +71 -0
package/dist/cli/index.js
CHANGED
|
@@ -67,6 +67,12 @@ function openProject(args, mode) {
|
|
|
67
67
|
config.minify = !!args.flags.minify;
|
|
68
68
|
if (args.flags.sourcemap !== undefined)
|
|
69
69
|
config.sourcemap = !!args.flags.sourcemap;
|
|
70
|
+
if (typeof args.flags.platform === "string") {
|
|
71
|
+
const platform = args.flags.platform;
|
|
72
|
+
if (!config_1.PLATFORMS.includes(platform))
|
|
73
|
+
throw new diagnostics_1.SinthError("E0900", `Unknown platform '${args.flags.platform}'`, undefined, `Use one of: ${config_1.PLATFORMS.join(", ")}`);
|
|
74
|
+
config.platform = platform;
|
|
75
|
+
}
|
|
70
76
|
return new project_1.Project(root, config, mode === "development");
|
|
71
77
|
}
|
|
72
78
|
const commands = {
|
|
@@ -98,13 +104,14 @@ const commands = {
|
|
|
98
104
|
},
|
|
99
105
|
build: {
|
|
100
106
|
summary: "Build the site for production",
|
|
101
|
-
usage: "sinth build [--out dist] [--no-minify] [--sourcemap]",
|
|
107
|
+
usage: "sinth build [--out dist] [--platform name] [--no-minify] [--sourcemap]",
|
|
102
108
|
flags: {
|
|
103
109
|
...ROOT_FLAG,
|
|
104
110
|
out: { type: "string", description: "Output directory" },
|
|
105
111
|
minify: { type: "boolean", description: "Minify output (default: on)" },
|
|
106
112
|
sourcemap: { type: "boolean", description: "Emit source maps" },
|
|
107
113
|
verbose: { type: "boolean", description: "List every page, even on large sites" },
|
|
114
|
+
platform: { type: "string", description: "Override \"platform\" from sinth.config.json" },
|
|
108
115
|
},
|
|
109
116
|
async run(args) {
|
|
110
117
|
console.log(`\n ${(0, ui_1.brand)("build", VERSION)}\n`);
|
|
@@ -163,13 +170,14 @@ const commands = {
|
|
|
163
170
|
},
|
|
164
171
|
},
|
|
165
172
|
deploy: {
|
|
166
|
-
summary: "Build and deploy
|
|
167
|
-
usage: "sinth deploy [--preview] [--branch name] [--project name] [--create] [--skip-build]",
|
|
173
|
+
summary: "Build and deploy (Cloudflare Pages or Workers, Vercel, Netlify)",
|
|
174
|
+
usage: "sinth deploy [--platform name] [--preview] [--branch name] [--project name] [--create] [--skip-build]",
|
|
168
175
|
flags: {
|
|
169
176
|
...ROOT_FLAG,
|
|
170
177
|
preview: { type: "boolean", description: "Deploy to a preview URL instead of production" },
|
|
171
178
|
branch: { type: "string", description: "Branch name for the deployment" },
|
|
172
|
-
project: { type: "string", description: "Cloudflare Pages project name" },
|
|
179
|
+
project: { type: "string", description: "Cloudflare Pages project or Worker name" },
|
|
180
|
+
platform: { type: "string", description: "Override \"platform\" from sinth.config.json" },
|
|
173
181
|
create: { type: "boolean", description: "Create the Pages project first" },
|
|
174
182
|
"skip-build": { type: "boolean", description: "Deploy the existing build without rebuilding" },
|
|
175
183
|
},
|
|
@@ -182,7 +190,7 @@ const commands = {
|
|
|
182
190
|
(0, ui_1.printDiagnostic)(w);
|
|
183
191
|
ui_1.log.success(`Built ${out.pages.length} page(s) in ${(0, ui_1.formatMs)(out.durationMs)}`);
|
|
184
192
|
}
|
|
185
|
-
await (0, cloudflare_1.
|
|
193
|
+
await (0, cloudflare_1.deploy)(project.root, project.config, {
|
|
186
194
|
preview: !!args.flags.preview,
|
|
187
195
|
branch: args.flags.branch,
|
|
188
196
|
projectName: args.flags.project,
|
|
@@ -250,7 +258,8 @@ const commands = {
|
|
|
250
258
|
if (kind === "component" && name) {
|
|
251
259
|
if (!/^[A-Z][A-Za-z0-9]*$/.test(name))
|
|
252
260
|
throw new diagnostics_1.SinthError("E1301", `Component names must be PascalCase, like 'ProductCard'`);
|
|
253
|
-
|
|
261
|
+
const { config } = (0, config_1.loadConfig)(root, "development");
|
|
262
|
+
writeNew(path.join(config.srcDir, "components", name + ".sinth"), (0, templates_1.componentTemplate)(name));
|
|
254
263
|
return 0;
|
|
255
264
|
}
|
|
256
265
|
throw new diagnostics_1.SinthError("E1302", "Usage: sinth new page <path> | sinth new component <Name>");
|
|
@@ -277,7 +286,8 @@ const commands = {
|
|
|
277
286
|
["Platform", `${process.platform} ${process.arch}`],
|
|
278
287
|
["Project", root],
|
|
279
288
|
["Config", file ? path.relative(root, file) : ui_1.c.gray("none (defaults)")],
|
|
280
|
-
["
|
|
289
|
+
["Source", path.relative(root, config.srcDir) || "."],
|
|
290
|
+
["Pages", `${pages} in ${path.relative(root, fs.existsSync(config.pagesDir) ? config.pagesDir : config.srcDir) || "."}`],
|
|
281
291
|
["Output", path.relative(root, config.outDir)],
|
|
282
292
|
].map(([k, v]) => [ui_1.c.gray(k), v])));
|
|
283
293
|
return 0;
|
package/dist/compiler/codegen.js
CHANGED
|
@@ -79,6 +79,15 @@ function collectExports(file) {
|
|
|
79
79
|
return exports;
|
|
80
80
|
}
|
|
81
81
|
const EVENT_NAME_MAP = { doubleclick: "dblclick", dblclick: "dblclick" };
|
|
82
|
+
/** Common DOM events, used to suggest a fix for typos like onClik. Other names still work as custom events. */
|
|
83
|
+
const KNOWN_EVENTS = [
|
|
84
|
+
"Click", "DoubleClick", "Dblclick", "ContextMenu", "MouseDown", "MouseUp", "MouseMove", "MouseEnter", "MouseLeave", "MouseOver", "MouseOut",
|
|
85
|
+
"PointerDown", "PointerUp", "PointerMove", "PointerEnter", "PointerLeave", "PointerCancel", "TouchStart", "TouchEnd", "TouchMove",
|
|
86
|
+
"KeyDown", "KeyUp", "KeyPress", "Input", "Change", "Submit", "Reset", "Focus", "Blur", "FocusIn", "FocusOut", "Scroll", "Wheel",
|
|
87
|
+
"DragStart", "Drag", "DragEnd", "DragEnter", "DragLeave", "DragOver", "Drop", "Load", "Error", "Resize", "Select", "Invalid",
|
|
88
|
+
"Play", "Pause", "Ended", "TimeUpdate", "Toggle", "Close", "Cancel", "AnimationEnd", "TransitionEnd", "Copy", "Cut", "Paste",
|
|
89
|
+
].map(e => "on" + e);
|
|
90
|
+
const KNOWN_EVENT_SET = new Set(KNOWN_EVENTS.map(e => e.toLowerCase()));
|
|
82
91
|
function eventName(attr) {
|
|
83
92
|
const raw = attr.slice(2).toLowerCase();
|
|
84
93
|
return EVENT_NAME_MAP[raw] ?? raw;
|
|
@@ -154,7 +163,15 @@ class ModuleCompiler {
|
|
|
154
163
|
body.push(this.genComponent(c));
|
|
155
164
|
this.componentInfo[c.name] = { interactive: this.interactive, uses: [...this.uses] };
|
|
156
165
|
}
|
|
157
|
-
|
|
166
|
+
// Page styles are compiled before render() so their reactive values (pageCssVars) get wired up there.
|
|
167
|
+
let moduleBase = base;
|
|
168
|
+
for (const s of file.styles) {
|
|
169
|
+
this.interactive = false;
|
|
170
|
+
this.addStyle(s, undefined, this.moduleScope, file.isPage ? "page" : "module");
|
|
171
|
+
if (this.interactive)
|
|
172
|
+
moduleBase = true;
|
|
173
|
+
}
|
|
174
|
+
this.interactive = this.pageCssVars.length > 0 && file.isPage;
|
|
158
175
|
this.uses = new Set();
|
|
159
176
|
const stateVars = file.vars.filter(v => v.kind === "var");
|
|
160
177
|
if (file.vars.length) {
|
|
@@ -194,13 +211,6 @@ class ModuleCompiler {
|
|
|
194
211
|
this.interactive = true;
|
|
195
212
|
}
|
|
196
213
|
const pageInfo = file.isPage ? { interactive: this.interactive, uses: [...this.uses] } : undefined;
|
|
197
|
-
let moduleBase = base;
|
|
198
|
-
for (const s of file.styles) {
|
|
199
|
-
this.interactive = false;
|
|
200
|
-
this.addStyle(s, undefined, this.moduleScope, file.isPage ? "page" : "module");
|
|
201
|
-
if (this.interactive)
|
|
202
|
-
moduleBase = true;
|
|
203
|
-
}
|
|
204
214
|
if (file.components.some(c => c.customTag))
|
|
205
215
|
moduleBase = true;
|
|
206
216
|
if (this.css.length)
|
|
@@ -551,11 +561,25 @@ class ModuleCompiler {
|
|
|
551
561
|
}
|
|
552
562
|
return node.children.every(c => this.isStaticNode(c, ctx));
|
|
553
563
|
}
|
|
564
|
+
/**
|
|
565
|
+
* Void elements (Input, Img, ...) cannot have children. Static markup used to drop them silently,
|
|
566
|
+
* so there it stays a warning to keep existing builds working; dynamic elements were always an error.
|
|
567
|
+
*/
|
|
568
|
+
checkVoidChildren(node, ctx, strict) {
|
|
569
|
+
if (!node.children.length || !elements_1.VOID_TAGS.has(this.elementShape(node, ctx).tag))
|
|
570
|
+
return;
|
|
571
|
+
const message = `${node.name} is a void element and cannot have children`;
|
|
572
|
+
const hint = node.name === "Input" || node.name === "Checkbox" || node.name === "Radio" ? "Put the text in a Label next to it, e.g. Label { Checkbox(model: done) \" Done\" }" : undefined;
|
|
573
|
+
if (strict)
|
|
574
|
+
this.fail("E0331", message, node.loc, hint);
|
|
575
|
+
this.diagnostics.warn("W0331", message + " (they are ignored)", node.loc, hint);
|
|
576
|
+
}
|
|
554
577
|
staticHtml(node, ctx) {
|
|
555
578
|
if (node.k === "text")
|
|
556
579
|
return node.expr.k === "lit" && node.expr.value !== null ? (0, html_1.escapeHtml)(String(node.expr.value)) : "";
|
|
557
580
|
if (node.k !== "element")
|
|
558
581
|
return "";
|
|
582
|
+
this.checkVoidChildren(node, ctx, false);
|
|
559
583
|
const { tag, attrs } = this.elementShape(node, ctx);
|
|
560
584
|
const attrStr = Object.entries(attrs).map(([k, v]) => (v === "" && elements_1.BOOLEAN_ATTRS.has(k) ? ` ${k}` : ` ${k}="${(0, html_1.escapeAttr)(v)}"`)).join("");
|
|
561
585
|
if (elements_1.VOID_TAGS.has(tag))
|
|
@@ -646,6 +670,8 @@ class ModuleCompiler {
|
|
|
646
670
|
}
|
|
647
671
|
if (node.slots.length)
|
|
648
672
|
this.fail("E0311", "slot(...) fills can only be used inside components that declare $slot", node.slots[0].loc);
|
|
673
|
+
if (!this.isStaticNode(node, ctx))
|
|
674
|
+
this.checkVoidChildren(node, ctx, true);
|
|
649
675
|
if (this.isStaticNode(node, ctx)) {
|
|
650
676
|
const html = this.staticHtml(node, ctx);
|
|
651
677
|
if (roots) {
|
|
@@ -663,15 +689,17 @@ class ModuleCompiler {
|
|
|
663
689
|
lines.push(`const ${v} = ${isSvg ? `$$.svg(${JSON.stringify(tag)})` : `$$.el(${JSON.stringify(tag)})`};`);
|
|
664
690
|
for (const [k, val] of Object.entries(attrs))
|
|
665
691
|
lines.push(`${v}.setAttribute(${JSON.stringify(k)}, ${JSON.stringify(val)});`);
|
|
666
|
-
|
|
692
|
+
const dynamic = this.genDynamicAttrs(node, v, tag, ctx);
|
|
693
|
+
// A <select>'s value can only be set once its options exist, so it is bound after the children.
|
|
694
|
+
const afterChildren = tag === "select" ? dynamic.filter(l => l.startsWith("$$.model(") || l.startsWith(`$$.prop(${v}, "value"`)) : [];
|
|
695
|
+
lines.push(...dynamic.filter(l => !afterChildren.includes(l)));
|
|
667
696
|
const childCtx = { ...ctx, svg: isSvg && tag !== "foreignObject" };
|
|
668
697
|
const htmlAttr = node.attrs.find(a => a.name === "html");
|
|
669
698
|
if (htmlAttr && node.children.length)
|
|
670
699
|
this.fail("E0330", "An element with 'html' cannot also have children", htmlAttr.loc);
|
|
671
|
-
if (elements_1.VOID_TAGS.has(tag) && node.children.length)
|
|
672
|
-
this.fail("E0331", `<${tag}> is a void element and cannot have children`, node.loc);
|
|
673
700
|
if (node.children.length)
|
|
674
701
|
lines.push(this.genNodes(node.children, v, childCtx));
|
|
702
|
+
lines.push(...afterChildren);
|
|
675
703
|
lines.push(`${parent}.appendChild(${v});`);
|
|
676
704
|
return lines.filter(Boolean).join("\n");
|
|
677
705
|
}
|
|
@@ -692,6 +720,11 @@ class ModuleCompiler {
|
|
|
692
720
|
continue;
|
|
693
721
|
if (a.handler) {
|
|
694
722
|
this.interactive = true;
|
|
723
|
+
if (!KNOWN_EVENT_SET.has(a.name.toLowerCase())) {
|
|
724
|
+
const s = (0, diagnostics_1.suggest)(a.name, KNOWN_EVENTS);
|
|
725
|
+
if (s)
|
|
726
|
+
this.diagnostics.warn("W0302", `Unknown event '${a.name}'`, a.loc, `Did you mean '${s}'? Other names listen for custom events`);
|
|
727
|
+
}
|
|
695
728
|
lines.push(`$$.on(${v}, ${JSON.stringify(eventName(a.name))}, ${this.genHandler(a, scope)}${a.modifiers.length ? ", " + JSON.stringify(a.modifiers) : ""});`);
|
|
696
729
|
continue;
|
|
697
730
|
}
|
|
@@ -766,6 +799,9 @@ class ModuleCompiler {
|
|
|
766
799
|
const ex = stmts[0].expr;
|
|
767
800
|
if (ex.k === "arrow")
|
|
768
801
|
return this.expr(ex, scope);
|
|
802
|
+
if (ex.k === "lit" && typeof ex.value !== "string") {
|
|
803
|
+
this.fail("E0352", `Event handlers must be code, not ${ex.value === null ? "null" : typeof ex.value}`, ex.loc, `Write the handler as code, e.g. ${a.name}: count += 1`);
|
|
804
|
+
}
|
|
769
805
|
if (ex.k === "lit" || ex.k === "tpl") {
|
|
770
806
|
const raw = ex.k === "lit" ? String(ex.value) : "";
|
|
771
807
|
const name = raw.replace(/\(\s*\)\s*;?$/, "");
|
|
@@ -53,7 +53,11 @@ class Prerenderer {
|
|
|
53
53
|
}
|
|
54
54
|
renderPage(file, params) {
|
|
55
55
|
const env = this.moduleEnv(file, params);
|
|
56
|
-
|
|
56
|
+
const body = this.renderNodes(file.body, { env: new Env(env), file });
|
|
57
|
+
// Initial values of reactive page styles, so the page looks right before the script takes over.
|
|
58
|
+
const names = new Set(file.vars.map(v => v.name));
|
|
59
|
+
const vars = this.evalStyleVars(file.styles, names, `--sv-${(0, html_1.hashString)(file.path + "page").slice(0, 5)}-`, file, env);
|
|
60
|
+
return vars ? `<style>:root{${escapeStyleText(vars)}}</style>${body}` : body;
|
|
57
61
|
}
|
|
58
62
|
/** Evaluates metadata in the page's module scope. Values that need the browser come back as `undefined` with `known: false`. */
|
|
59
63
|
evalMeta(file, params) {
|
|
@@ -754,11 +758,11 @@ class Prerenderer {
|
|
|
754
758
|
}
|
|
755
759
|
styleVars(decl, file, env) {
|
|
756
760
|
const names = new Set([...decl.params.map(p => p.name), ...decl.vars.map(v => v.name)]);
|
|
761
|
+
return this.evalStyleVars(decl.styles.filter(st => !st.global), names, `--sv-${(0, html_1.hashString)(file.path + decl.name).slice(0, 5)}-`, file, env);
|
|
762
|
+
}
|
|
763
|
+
evalStyleVars(styles, names, varPrefix, file, env) {
|
|
757
764
|
const parts = [];
|
|
758
|
-
for (const s of
|
|
759
|
-
if (s.global)
|
|
760
|
-
continue;
|
|
761
|
-
const varPrefix = `--sv-${(0, html_1.hashString)(file.path + decl.name).slice(0, 5)}-`;
|
|
765
|
+
for (const s of styles) {
|
|
762
766
|
try {
|
|
763
767
|
const res = (0, style_1.compileStyle)(s.raw, s.lang === "scss" ? "css" : s.lang, { reactiveNames: names, varPrefix, loc: s.loc, sinthSyntax: s.lang === "sinth" });
|
|
764
768
|
for (const e of res.expressions) {
|
|
@@ -776,6 +780,9 @@ class Prerenderer {
|
|
|
776
780
|
}
|
|
777
781
|
}
|
|
778
782
|
exports.Prerenderer = Prerenderer;
|
|
783
|
+
function escapeStyleText(css) {
|
|
784
|
+
return css.replace(/<\//gi, "<\\/");
|
|
785
|
+
}
|
|
779
786
|
function defaultValue(v) {
|
|
780
787
|
const t = v.type;
|
|
781
788
|
if (!t || t.nullable)
|
|
@@ -6,4 +6,6 @@ export interface DeployOptions {
|
|
|
6
6
|
projectName?: string;
|
|
7
7
|
}
|
|
8
8
|
export declare function defaultProjectName(root: string): string;
|
|
9
|
+
/** Uploads a finished build to the host set by `platform`. */
|
|
10
|
+
export declare function deploy(root: string, config: SinthConfig, opts: DeployOptions): Promise<void>;
|
|
9
11
|
export declare function deployToCloudflare(root: string, outDir: string, config: SinthConfig, opts: DeployOptions): Promise<void>;
|
|
@@ -34,16 +34,20 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.defaultProjectName = defaultProjectName;
|
|
37
|
+
exports.deploy = deploy;
|
|
37
38
|
exports.deployToCloudflare = deployToCloudflare;
|
|
38
39
|
const child_process_1 = require("child_process");
|
|
39
40
|
const fs = __importStar(require("fs"));
|
|
40
41
|
const path = __importStar(require("path"));
|
|
41
42
|
const diagnostics_1 = require("../compiler/diagnostics");
|
|
42
43
|
const ui_1 = require("../cli/ui");
|
|
44
|
+
const platforms_1 = require("./platforms");
|
|
43
45
|
const PROJECT_NAME = /^[a-z0-9][a-z0-9-]{0,56}[a-z0-9]$/;
|
|
44
46
|
const BRANCH_NAME = /^[A-Za-z0-9][A-Za-z0-9._/-]{0,99}$/;
|
|
45
47
|
const ACCOUNT_ID = /^[a-f0-9]{32}$/;
|
|
46
48
|
const WRANGLER = "wrangler@4";
|
|
49
|
+
const VERCEL = "vercel@latest";
|
|
50
|
+
const NETLIFY = "netlify-cli@latest";
|
|
47
51
|
function defaultProjectName(root) {
|
|
48
52
|
let name = path.basename(root);
|
|
49
53
|
const pkg = path.join(root, "package.json");
|
|
@@ -63,49 +67,130 @@ function sanitizeName(name) {
|
|
|
63
67
|
const cleaned = name.toLowerCase().replace(/[^a-z0-9-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 58);
|
|
64
68
|
return cleaned.length >= 2 ? cleaned : "sinth-site";
|
|
65
69
|
}
|
|
66
|
-
|
|
70
|
+
/** Runs a host's CLI through npx, so it does not have to be installed. */
|
|
71
|
+
function runNpx(pkg, args, cwd, env) {
|
|
67
72
|
for (const a of args) {
|
|
68
73
|
if (!/^[A-Za-z0-9@._/:=\\ -]+$/.test(a))
|
|
69
|
-
throw new diagnostics_1.SinthError("E1100", `Refusing to pass unsafe argument to
|
|
74
|
+
throw new diagnostics_1.SinthError("E1100", `Refusing to pass unsafe argument to ${pkg}: ${a}`);
|
|
70
75
|
}
|
|
71
76
|
const isWin = process.platform === "win32";
|
|
72
77
|
const cmd = isWin ? "npx.cmd" : "npx";
|
|
73
|
-
const quoted = isWin ? ["--yes",
|
|
78
|
+
const quoted = isWin ? ["--yes", pkg, ...args].map(a => (/\s/.test(a) ? `"${a}"` : a)) : ["--yes", pkg, ...args];
|
|
74
79
|
return new Promise((resolve, reject) => {
|
|
75
80
|
const child = (0, child_process_1.spawn)(cmd, quoted, { cwd, env, stdio: "inherit", shell: isWin });
|
|
76
81
|
child.on("error", reject);
|
|
77
82
|
child.on("exit", code => resolve(code ?? 1));
|
|
78
83
|
});
|
|
79
84
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const branch = opts.branch ?? (opts.preview ? "preview" : cf.productionBranch ?? "main");
|
|
84
|
-
if (!PROJECT_NAME.test(projectName)) {
|
|
85
|
-
throw new diagnostics_1.SinthError("E1101", `Invalid Cloudflare Pages project name '${projectName}'`, undefined, "Use 2-58 lowercase letters, numbers and dashes, e.g. \"my-site\"");
|
|
85
|
+
function assertBuilt(outDir) {
|
|
86
|
+
if (!fs.existsSync(outDir) || (!fs.existsSync(path.join(outDir, "index.html")) && !fs.readdirSync(outDir).some(f => f.endsWith(".html")))) {
|
|
87
|
+
throw new diagnostics_1.SinthError("E1104", `Nothing to deploy in ${outDir}`, undefined, "Run sinth build first, or deploy without --skip-build");
|
|
86
88
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
+
}
|
|
90
|
+
function cloudflareEnv(config) {
|
|
91
|
+
const cf = config.cloudflare;
|
|
89
92
|
if (cf.accountId && !ACCOUNT_ID.test(cf.accountId))
|
|
90
93
|
throw new diagnostics_1.SinthError("E1103", "cloudflare.accountId must be a 32-character hex ID from the Cloudflare dashboard");
|
|
91
|
-
if (!fs.existsSync(path.join(outDir, "index.html")) && !fs.readdirSync(outDir).some(f => f.endsWith(".html"))) {
|
|
92
|
-
throw new diagnostics_1.SinthError("E1104", `Nothing to deploy in ${outDir}`);
|
|
93
|
-
}
|
|
94
94
|
const env = { ...process.env };
|
|
95
95
|
if (cf.accountId)
|
|
96
96
|
env.CLOUDFLARE_ACCOUNT_ID = cf.accountId;
|
|
97
|
+
if (!env.CLOUDFLARE_API_TOKEN)
|
|
98
|
+
ui_1.log.step(ui_1.c.gray("Using your wrangler login. In CI, set CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID."));
|
|
99
|
+
return env;
|
|
100
|
+
}
|
|
101
|
+
function projectNameFor(root, config, opts, what) {
|
|
102
|
+
const name = opts.projectName ?? config.cloudflare.projectName ?? defaultProjectName(root);
|
|
103
|
+
if (!PROJECT_NAME.test(name)) {
|
|
104
|
+
throw new diagnostics_1.SinthError("E1101", `Invalid ${what} name '${name}'`, undefined, "Use 2-58 lowercase letters, numbers and dashes, e.g. \"my-site\"");
|
|
105
|
+
}
|
|
106
|
+
return name;
|
|
107
|
+
}
|
|
108
|
+
/** Uploads a finished build to the host set by `platform`. */
|
|
109
|
+
async function deploy(root, config, opts) {
|
|
110
|
+
const platform = config.platform;
|
|
111
|
+
switch (platform) {
|
|
112
|
+
case "cloudflare":
|
|
113
|
+
return deployToCloudflare(root, config.outDir, config, opts);
|
|
114
|
+
case "cloudflare-workers":
|
|
115
|
+
return deployToWorkers(root, config, opts);
|
|
116
|
+
case "vercel":
|
|
117
|
+
return deployToVercel(root, opts);
|
|
118
|
+
case "netlify":
|
|
119
|
+
return deployToNetlify(root, config, opts);
|
|
120
|
+
case "github-pages":
|
|
121
|
+
case "static":
|
|
122
|
+
throw new diagnostics_1.SinthError("E1106", `sinth deploy has no upload step for platform "${platform}"`, undefined, platform === "github-pages"
|
|
123
|
+
? "Publish dist/ with GitHub Actions (actions/upload-pages-artifact and actions/deploy-pages). See https://sinth.yannosay.com/docs/production/deploy"
|
|
124
|
+
: "Upload the contents of your outDir to your host, or set \"platform\" in sinth.config.json");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async function deployToCloudflare(root, outDir, config, opts) {
|
|
128
|
+
const cf = config.cloudflare;
|
|
129
|
+
const projectName = projectNameFor(root, config, opts, "Cloudflare Pages project");
|
|
130
|
+
const branch = opts.branch ?? (opts.preview ? "preview" : cf.productionBranch ?? "main");
|
|
131
|
+
if (!BRANCH_NAME.test(branch))
|
|
132
|
+
throw new diagnostics_1.SinthError("E1102", `Invalid branch name '${branch}'`);
|
|
133
|
+
assertBuilt(outDir);
|
|
134
|
+
const env = cloudflareEnv(config);
|
|
97
135
|
if (opts.create) {
|
|
98
136
|
ui_1.log.info(`Creating Cloudflare Pages project ${ui_1.c.bold(projectName)}`);
|
|
99
|
-
const code = await
|
|
137
|
+
const code = await runNpx(WRANGLER, ["pages", "project", "create", projectName, "--production-branch", cf.productionBranch ?? "main"], root, env);
|
|
100
138
|
if (code !== 0)
|
|
101
139
|
ui_1.log.warn("Project creation failed or the project already exists, continuing with the deploy");
|
|
102
140
|
}
|
|
103
141
|
ui_1.log.info(`Deploying ${ui_1.c.bold(path.relative(root, outDir) || outDir)} to ${ui_1.c.bold(projectName)} ${ui_1.c.gray(`(branch ${branch})`)}`);
|
|
104
|
-
|
|
105
|
-
ui_1.log.step(ui_1.c.gray("Using your wrangler login. In CI, set CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID."));
|
|
106
|
-
const code = await runWrangler(["pages", "deploy", path.relative(root, outDir) || ".", "--project-name", projectName, "--branch", branch, "--commit-dirty=true"], root, env);
|
|
142
|
+
const code = await runNpx(WRANGLER, ["pages", "deploy", path.relative(root, outDir) || ".", "--project-name", projectName, "--branch", branch, "--commit-dirty=true"], root, env);
|
|
107
143
|
if (code !== 0) {
|
|
108
144
|
throw new diagnostics_1.SinthError("E1105", "Cloudflare deployment failed", undefined, `Check the output above. Not logged in? Run: npx ${WRANGLER} login. New project? Run: sinth deploy --create`);
|
|
109
145
|
}
|
|
110
146
|
ui_1.log.success(`Deployed to Cloudflare Pages (${projectName})`);
|
|
111
147
|
}
|
|
148
|
+
async function deployToWorkers(root, config, opts) {
|
|
149
|
+
assertBuilt(config.outDir);
|
|
150
|
+
const env = cloudflareEnv(config);
|
|
151
|
+
const own = (0, platforms_1.userWranglerConfig)(root);
|
|
152
|
+
const configFile = own ?? path.join(root, platforms_1.DEPLOY_DIR, "wrangler.json");
|
|
153
|
+
if (!fs.existsSync(configFile))
|
|
154
|
+
throw new diagnostics_1.SinthError("E1104", "No Worker config found", undefined, "Run sinth build with \"platform\": \"cloudflare-workers\" first");
|
|
155
|
+
const args = opts.preview ? ["versions", "upload"] : ["deploy"];
|
|
156
|
+
args.push("--config", path.relative(root, configFile));
|
|
157
|
+
if (opts.projectName)
|
|
158
|
+
args.push("--name", projectNameFor(root, config, opts, "Worker"));
|
|
159
|
+
ui_1.log.info(`${opts.preview ? "Uploading a preview version of" : "Deploying"} the Worker ${ui_1.c.gray(`(config ${path.relative(root, configFile)})`)}`);
|
|
160
|
+
if (own)
|
|
161
|
+
ui_1.log.step(ui_1.c.gray(`Using your ${path.basename(own)}. It should point "assets.directory" at ${path.relative(root, config.outDir)}; see the deploy docs.`));
|
|
162
|
+
const code = await runNpx(WRANGLER, args, root, env);
|
|
163
|
+
if (code !== 0)
|
|
164
|
+
throw new diagnostics_1.SinthError("E1105", "Cloudflare Workers deployment failed", undefined, `Check the output above. Not logged in? Run: npx ${WRANGLER} login`);
|
|
165
|
+
ui_1.log.success(opts.preview ? "Uploaded a preview version to Cloudflare Workers" : "Deployed to Cloudflare Workers");
|
|
166
|
+
}
|
|
167
|
+
async function deployToVercel(root, opts) {
|
|
168
|
+
if (!fs.existsSync(path.join(root, ".vercel", "output", "config.json"))) {
|
|
169
|
+
throw new diagnostics_1.SinthError("E1104", "No Vercel build output found", undefined, "Run sinth build with \"platform\": \"vercel\" first");
|
|
170
|
+
}
|
|
171
|
+
const args = ["deploy", "--prebuilt"];
|
|
172
|
+
if (!opts.preview)
|
|
173
|
+
args.push("--prod");
|
|
174
|
+
ui_1.log.info(`Deploying ${ui_1.c.bold(".vercel/output")} to Vercel${opts.preview ? " (preview)" : ""}`);
|
|
175
|
+
if (!process.env.VERCEL_TOKEN)
|
|
176
|
+
ui_1.log.step(ui_1.c.gray("Using your Vercel login. The first deploy asks which project to link."));
|
|
177
|
+
else
|
|
178
|
+
args.push("--token", process.env.VERCEL_TOKEN);
|
|
179
|
+
const code = await runNpx(VERCEL, args, root, { ...process.env });
|
|
180
|
+
if (code !== 0)
|
|
181
|
+
throw new diagnostics_1.SinthError("E1105", "Vercel deployment failed", undefined, `Check the output above. Not logged in? Run: npx ${VERCEL} login`);
|
|
182
|
+
ui_1.log.success("Deployed to Vercel");
|
|
183
|
+
}
|
|
184
|
+
async function deployToNetlify(root, config, opts) {
|
|
185
|
+
assertBuilt(config.outDir);
|
|
186
|
+
const args = ["deploy", "--dir", path.relative(root, config.outDir) || ".", "--no-build"];
|
|
187
|
+
if (!opts.preview)
|
|
188
|
+
args.push("--prod");
|
|
189
|
+
ui_1.log.info(`Deploying ${ui_1.c.bold(path.relative(root, config.outDir))} to Netlify${opts.preview ? " (draft)" : ""}`);
|
|
190
|
+
if (!process.env.NETLIFY_AUTH_TOKEN)
|
|
191
|
+
ui_1.log.step(ui_1.c.gray("Using your Netlify login. In CI, set NETLIFY_AUTH_TOKEN and NETLIFY_SITE_ID."));
|
|
192
|
+
const code = await runNpx(NETLIFY, args, root, { ...process.env });
|
|
193
|
+
if (code !== 0)
|
|
194
|
+
throw new diagnostics_1.SinthError("E1105", "Netlify deployment failed", undefined, `Check the output above. Not logged in? Run: npx ${NETLIFY} login`);
|
|
195
|
+
ui_1.log.success("Deployed to Netlify");
|
|
196
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Diagnostic } from "../compiler/diagnostics";
|
|
2
|
+
import type { OutputFile } from "../project/build";
|
|
3
|
+
import { Platform } from "../project/config";
|
|
4
|
+
import type { Project } from "../project/project";
|
|
5
|
+
import { scanFunctions } from "../server/functions";
|
|
6
|
+
export declare const DEPLOY_DIR: string;
|
|
7
|
+
/** Hosts that read a `_headers` file from the output folder. */
|
|
8
|
+
export declare function readsHeadersFile(platform: Platform): boolean;
|
|
9
|
+
/** Hosts that can run the server routes in functions/. */
|
|
10
|
+
export declare function runsFunctions(platform: Platform): boolean;
|
|
11
|
+
export declare function securityHeaders(csp: string | undefined): Record<string, string>;
|
|
12
|
+
/** A `_headers` file (Cloudflare Pages and Workers, Netlify). */
|
|
13
|
+
export declare function headersFile(headers: Record<string, string>, base: string): string;
|
|
14
|
+
export declare function functionRoutes(project: Project): ReturnType<typeof scanFunctions>;
|
|
15
|
+
/**
|
|
16
|
+
* Writes host-specific output next to the regular build: `.vercel/output` for Vercel and the
|
|
17
|
+
* Worker plus its wrangler config for Cloudflare Workers. Returns warnings for the build summary.
|
|
18
|
+
*/
|
|
19
|
+
export declare function writePlatformOutput(project: Project, files: Map<string, OutputFile>, headers: Record<string, string>, write: boolean): Promise<Diagnostic[]>;
|
|
20
|
+
export declare function userWranglerConfig(root: string): string | undefined;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.DEPLOY_DIR = void 0;
|
|
37
|
+
exports.readsHeadersFile = readsHeadersFile;
|
|
38
|
+
exports.runsFunctions = runsFunctions;
|
|
39
|
+
exports.securityHeaders = securityHeaders;
|
|
40
|
+
exports.headersFile = headersFile;
|
|
41
|
+
exports.functionRoutes = functionRoutes;
|
|
42
|
+
exports.writePlatformOutput = writePlatformOutput;
|
|
43
|
+
exports.userWranglerConfig = userWranglerConfig;
|
|
44
|
+
const esbuild = __importStar(require("esbuild"));
|
|
45
|
+
const fs = __importStar(require("fs"));
|
|
46
|
+
const path = __importStar(require("path"));
|
|
47
|
+
const diagnostics_1 = require("../compiler/diagnostics");
|
|
48
|
+
const functions_1 = require("../server/functions");
|
|
49
|
+
const functions_router_1 = require("../server/functions-router");
|
|
50
|
+
const cloudflare_1 = require("./cloudflare");
|
|
51
|
+
/** Wrangler compatibility date for generated Worker configs. */
|
|
52
|
+
const COMPATIBILITY_DATE = "2025-09-01";
|
|
53
|
+
const IMMUTABLE = "public, max-age=31536000, immutable";
|
|
54
|
+
exports.DEPLOY_DIR = path.join(".sinth", "deploy");
|
|
55
|
+
/** Hosts that read a `_headers` file from the output folder. */
|
|
56
|
+
function readsHeadersFile(platform) {
|
|
57
|
+
return platform === "cloudflare" || platform === "cloudflare-workers" || platform === "netlify" || platform === "static";
|
|
58
|
+
}
|
|
59
|
+
/** Hosts that can run the server routes in functions/. */
|
|
60
|
+
function runsFunctions(platform) {
|
|
61
|
+
return platform === "cloudflare" || platform === "cloudflare-workers";
|
|
62
|
+
}
|
|
63
|
+
function securityHeaders(csp) {
|
|
64
|
+
const headers = {
|
|
65
|
+
"X-Content-Type-Options": "nosniff",
|
|
66
|
+
"Referrer-Policy": "strict-origin-when-cross-origin",
|
|
67
|
+
"X-Frame-Options": "SAMEORIGIN",
|
|
68
|
+
"Cross-Origin-Opener-Policy": "same-origin",
|
|
69
|
+
};
|
|
70
|
+
if (csp)
|
|
71
|
+
headers["Content-Security-Policy"] = csp;
|
|
72
|
+
return headers;
|
|
73
|
+
}
|
|
74
|
+
/** A `_headers` file (Cloudflare Pages and Workers, Netlify). */
|
|
75
|
+
function headersFile(headers, base) {
|
|
76
|
+
const lines = ["/*", ...Object.entries(headers).map(([k, v]) => ` ${k}: ${v}`)];
|
|
77
|
+
lines.push("", `${base}_sinth/*`, ` Cache-Control: ${IMMUTABLE}`, "");
|
|
78
|
+
return lines.join("\n");
|
|
79
|
+
}
|
|
80
|
+
function functionRoutes(project) {
|
|
81
|
+
return (0, functions_1.scanFunctions)(path.join(project.root, functions_1.FUNCTIONS_DIR));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Writes host-specific output next to the regular build: `.vercel/output` for Vercel and the
|
|
85
|
+
* Worker plus its wrangler config for Cloudflare Workers. Returns warnings for the build summary.
|
|
86
|
+
*/
|
|
87
|
+
async function writePlatformOutput(project, files, headers, write) {
|
|
88
|
+
const cfg = project.config;
|
|
89
|
+
const warnings = [];
|
|
90
|
+
const routes = functionRoutes(project);
|
|
91
|
+
if (routes.length && !runsFunctions(cfg.platform)) {
|
|
92
|
+
warnings.push({
|
|
93
|
+
severity: "warning", code: "W0810",
|
|
94
|
+
message: `The routes in ${functions_1.FUNCTIONS_DIR}/ are not deployed on platform "${cfg.platform}"`,
|
|
95
|
+
hint: "Server functions run on Cloudflare: set \"platform\" to \"cloudflare\" or \"cloudflare-workers\" in sinth.config.json",
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
if (!write)
|
|
99
|
+
return warnings;
|
|
100
|
+
if (cfg.platform === "vercel")
|
|
101
|
+
writeVercelOutput(project, files, headers);
|
|
102
|
+
if (cfg.platform === "cloudflare-workers")
|
|
103
|
+
await writeWorker(project, routes);
|
|
104
|
+
return warnings;
|
|
105
|
+
}
|
|
106
|
+
/** Vercel Build Output API v3: https://vercel.com/docs/build-output-api */
|
|
107
|
+
function writeVercelOutput(project, files, headers) {
|
|
108
|
+
const cfg = project.config;
|
|
109
|
+
const out = path.join(project.root, ".vercel", "output");
|
|
110
|
+
fs.rmSync(out, { recursive: true, force: true });
|
|
111
|
+
const prefix = cfg.base.replace(/^\/|\/$/g, "");
|
|
112
|
+
const staticDir = path.join(out, "static", prefix);
|
|
113
|
+
const overrides = {};
|
|
114
|
+
for (const [rel, f] of files) {
|
|
115
|
+
const target = path.join(staticDir, rel);
|
|
116
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
117
|
+
fs.writeFileSync(target, f.contents);
|
|
118
|
+
// Clean URLs: /about serves about.html. index.html files are served for their folder already.
|
|
119
|
+
if (rel.endsWith(".html") && !/(^|\/)(index|404)\.html$/.test(rel)) {
|
|
120
|
+
const key = (prefix ? prefix + "/" : "") + rel;
|
|
121
|
+
overrides[key] = { path: key.slice(0, -5) };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const escapedBase = cfg.base.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
125
|
+
const routes = [
|
|
126
|
+
{ src: `^${escapedBase}_sinth/(.*)$`, headers: { "Cache-Control": IMMUTABLE }, continue: true },
|
|
127
|
+
];
|
|
128
|
+
if (Object.keys(headers).length)
|
|
129
|
+
routes.push({ src: "^/(.*)$", headers, continue: true });
|
|
130
|
+
routes.push({ handle: "filesystem" });
|
|
131
|
+
if (files.has("404.html"))
|
|
132
|
+
routes.push({ src: "^/(.*)$", status: 404, dest: `${cfg.base}404.html` });
|
|
133
|
+
const config = { version: 3, routes, overrides };
|
|
134
|
+
fs.writeFileSync(path.join(out, "config.json"), JSON.stringify(config, null, 2) + "\n");
|
|
135
|
+
}
|
|
136
|
+
function userWranglerConfig(root) {
|
|
137
|
+
return ["wrangler.jsonc", "wrangler.json", "wrangler.toml"].map(f => path.join(root, f)).find(f => fs.existsSync(f));
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Cloudflare Workers with static assets. With functions/, the routes are bundled into a Worker that
|
|
141
|
+
* runs only for their URLs; everything else is served straight from the assets.
|
|
142
|
+
*/
|
|
143
|
+
async function writeWorker(project, routes) {
|
|
144
|
+
const cfg = project.config;
|
|
145
|
+
const dir = path.join(project.root, exports.DEPLOY_DIR);
|
|
146
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
147
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
148
|
+
const rel = (p) => path.relative(dir, p).split(path.sep).join("/") || ".";
|
|
149
|
+
const assets = {
|
|
150
|
+
directory: rel(cfg.outDir),
|
|
151
|
+
not_found_handling: "404-page",
|
|
152
|
+
html_handling: "auto-trailing-slash",
|
|
153
|
+
};
|
|
154
|
+
const config = {
|
|
155
|
+
$schema: rel(path.join(project.root, "node_modules", "wrangler", "config-schema.json")),
|
|
156
|
+
name: workerName(project),
|
|
157
|
+
compatibility_date: COMPATIBILITY_DATE,
|
|
158
|
+
assets,
|
|
159
|
+
};
|
|
160
|
+
if (!fs.existsSync(path.join(project.root, "node_modules", "wrangler")))
|
|
161
|
+
delete config.$schema;
|
|
162
|
+
if (routes.length) {
|
|
163
|
+
await bundleWorker(project, routes, path.join(dir, "worker.js"));
|
|
164
|
+
config.main = "worker.js";
|
|
165
|
+
assets.binding = "ASSETS";
|
|
166
|
+
assets.run_worker_first = (0, functions_router_1.routePatterns)(routes).map(p => (cfg.base === "/" ? p : cfg.base.slice(0, -1) + p));
|
|
167
|
+
}
|
|
168
|
+
fs.writeFileSync(path.join(dir, "wrangler.json"), JSON.stringify(config, null, 2) + "\n");
|
|
169
|
+
}
|
|
170
|
+
function workerName(project) {
|
|
171
|
+
return project.config.cloudflare.projectName ?? (0, cloudflare_1.defaultProjectName)(project.root);
|
|
172
|
+
}
|
|
173
|
+
async function bundleWorker(project, routes, outfile) {
|
|
174
|
+
const router = path.join(__dirname, "..", "server", "functions-router.js");
|
|
175
|
+
const imports = routes.map((r, i) => `import * as m${i} from ${JSON.stringify(r.file)};`);
|
|
176
|
+
const table = routes.map((r, i) => ` { rel: ${JSON.stringify(r.rel)}, segments: ${JSON.stringify(r.segments)}, middleware: ${r.middleware}, module: m${i} },`);
|
|
177
|
+
const contents = [
|
|
178
|
+
`import { dispatch } from ${JSON.stringify(router)};`,
|
|
179
|
+
...imports,
|
|
180
|
+
"const routes = [",
|
|
181
|
+
...table,
|
|
182
|
+
"];",
|
|
183
|
+
"export default {",
|
|
184
|
+
" async fetch(request, env, ctx) {",
|
|
185
|
+
" const assets = r => env.ASSETS.fetch(r);",
|
|
186
|
+
" const response = await dispatch(routes, request, { env, assets, waitUntil: p => ctx.waitUntil(p), passThroughOnException: () => ctx.passThroughOnException() });",
|
|
187
|
+
" return response ?? assets(request);",
|
|
188
|
+
" },",
|
|
189
|
+
"};",
|
|
190
|
+
].join("\n");
|
|
191
|
+
try {
|
|
192
|
+
await esbuild.build({
|
|
193
|
+
stdin: { contents, resolveDir: project.root, sourcefile: "sinth-worker.js", loader: "js" },
|
|
194
|
+
bundle: true, format: "esm", platform: "neutral", target: "es2022", outfile,
|
|
195
|
+
mainFields: ["module", "main"], conditions: ["workerd", "worker", "browser"],
|
|
196
|
+
minify: project.config.minify, logLevel: "silent", absWorkingDir: project.root,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
const first = err.errors?.[0];
|
|
201
|
+
throw new diagnostics_1.SinthError("E0803", `Function build failed: ${first?.text ?? err.message}`, first?.location ? { file: path.resolve(project.root, first.location.file), line: first.location.line, col: first.location.column + 1 } : undefined);
|
|
202
|
+
}
|
|
203
|
+
}
|