@yannosay/sinth 4.1.0 → 5.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.
- package/CHANGELOG.md +89 -0
- package/DOCS.md +1359 -0
- package/README.md +83 -74
- package/bin/sinth.js +2 -1
- package/dist/cli/args.d.ts +11 -0
- package/dist/cli/args.js +74 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +437 -0
- package/dist/cli/templates.d.ts +10 -0
- package/dist/cli/templates.js +193 -0
- package/dist/cli/ui.d.ts +28 -0
- package/dist/cli/ui.js +61 -0
- package/dist/cli/update-check.d.ts +2 -0
- package/dist/cli/update-check.js +124 -0
- package/dist/compiler/ast.d.ts +315 -0
- package/dist/compiler/ast.js +4 -0
- package/dist/compiler/codegen.d.ts +143 -0
- package/dist/compiler/codegen.js +1326 -0
- package/dist/compiler/diagnostics.d.ts +36 -0
- package/dist/compiler/diagnostics.js +142 -0
- package/dist/compiler/elements.d.ts +14 -0
- package/dist/compiler/elements.js +78 -0
- package/dist/compiler/globals.d.ts +2 -0
- package/dist/compiler/globals.js +27 -0
- package/dist/compiler/html.d.ts +37 -0
- package/dist/compiler/html.js +152 -0
- package/dist/compiler/lexer.d.ts +42 -0
- package/dist/compiler/lexer.js +404 -0
- package/dist/compiler/parser.d.ts +95 -0
- package/dist/compiler/parser.js +1374 -0
- package/dist/compiler/prerender.d.ts +72 -0
- package/dist/compiler/prerender.js +910 -0
- package/dist/compiler/script-analysis.d.ts +6 -0
- package/dist/compiler/script-analysis.js +106 -0
- package/dist/compiler/style.d.ts +39 -0
- package/dist/compiler/style.js +463 -0
- package/dist/deploy/cloudflare.d.ts +9 -0
- package/dist/deploy/cloudflare.js +111 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +23 -0
- package/dist/project/build.d.ts +31 -0
- package/dist/project/build.js +425 -0
- package/dist/project/config.d.ts +33 -0
- package/dist/project/config.js +142 -0
- package/dist/project/project.d.ts +74 -0
- package/dist/project/project.js +555 -0
- package/dist/runtime/index.d.ts +68 -0
- package/dist/runtime/index.js +912 -0
- package/dist/server/client.d.ts +1 -0
- package/dist/server/client.js +112 -0
- package/dist/server/dev.d.ts +8 -0
- package/dist/server/dev.js +244 -0
- package/dist/server/preview.d.ts +6 -0
- package/dist/server/preview.js +106 -0
- package/dist/server/static.d.ts +10 -0
- package/dist/server/static.js +136 -0
- package/lib/ui/index.sinth +436 -0
- package/package.json +33 -15
- package/schema/sinth.config.schema.json +54 -0
- package/dist/browser/compile.js +0 -63
- package/dist/cli.js +0 -509
- package/dist/core/builtins.js +0 -117
- package/dist/core/cli-compiler.js +0 -705
- package/dist/core/compiler.js +0 -1489
- package/dist/core/expr.js +0 -129
- package/dist/core/head-builder.js +0 -130
- package/dist/core/lexer.js +0 -410
- package/dist/core/parser.js +0 -1646
- package/dist/core/runtime/custom-element.js +0 -81
- package/dist/core/runtime/functions.js +0 -99
- package/dist/core/runtime/helpers.js +0 -347
- package/dist/core/runtime/render.js +0 -136
- package/dist/core/style-processor.js +0 -317
- package/dist/core/types.js +0 -93
- package/dist/resolver.js +0 -175
- package/dist/server.js +0 -174
- package/dist/update-check.js +0 -109
- package/dist/utils.js +0 -80
|
@@ -0,0 +1,555 @@
|
|
|
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.Project = exports.LIB_ROOT = exports.RUNTIME_PATH = exports.PACKAGE_ROOT = void 0;
|
|
37
|
+
exports.routeParams = routeParams;
|
|
38
|
+
exports.fontHref = fontHref;
|
|
39
|
+
const fs = __importStar(require("fs"));
|
|
40
|
+
const path = __importStar(require("path"));
|
|
41
|
+
const module_1 = require("module");
|
|
42
|
+
const codegen_1 = require("../compiler/codegen");
|
|
43
|
+
const diagnostics_1 = require("../compiler/diagnostics");
|
|
44
|
+
const prerender_1 = require("../compiler/prerender");
|
|
45
|
+
const parser_1 = require("../compiler/parser");
|
|
46
|
+
exports.PACKAGE_ROOT = path.resolve(__dirname, "..", "..");
|
|
47
|
+
exports.RUNTIME_PATH = path.join(exports.PACKAGE_ROOT, "dist", "runtime", "index.js");
|
|
48
|
+
exports.LIB_ROOT = path.join(exports.PACKAGE_ROOT, "lib");
|
|
49
|
+
const LEGACY_LIBRARIES = { SinthUI: "sinth:ui" };
|
|
50
|
+
const IGNORED_DIRS = new Set(["node_modules", ".git", ".sinth", ".wrangler", ".vscode", ".idea"]);
|
|
51
|
+
const ROOT_MODULE_DIRS = new Set(["components", "libraries", "lib", "styles", "assets", "public"]);
|
|
52
|
+
class Project {
|
|
53
|
+
root;
|
|
54
|
+
config;
|
|
55
|
+
dev;
|
|
56
|
+
parsed = new Map();
|
|
57
|
+
compiled = new Map();
|
|
58
|
+
resolved = new Map();
|
|
59
|
+
scssCompiler = null;
|
|
60
|
+
constructor(root, config, dev) {
|
|
61
|
+
this.root = root;
|
|
62
|
+
this.config = config;
|
|
63
|
+
this.dev = dev;
|
|
64
|
+
}
|
|
65
|
+
invalidate() {
|
|
66
|
+
this.compiled = new Map();
|
|
67
|
+
this.resolved.clear();
|
|
68
|
+
}
|
|
69
|
+
getFile(file) {
|
|
70
|
+
const abs = path.resolve(file);
|
|
71
|
+
let stat;
|
|
72
|
+
try {
|
|
73
|
+
stat = fs.statSync(abs);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
throw new diagnostics_1.SinthError("E0600", `File not found: ${path.relative(this.root, abs) || abs}`);
|
|
77
|
+
}
|
|
78
|
+
const cached = this.parsed.get(abs);
|
|
79
|
+
if (cached && cached.mtime === stat.mtimeMs)
|
|
80
|
+
return cached.file;
|
|
81
|
+
const source = fs.readFileSync(abs, "utf-8");
|
|
82
|
+
const parsed = (0, parser_1.parseSinth)(source, abs);
|
|
83
|
+
parsed.isPage = this.isPageFile(abs, parsed);
|
|
84
|
+
if (parsed.isPage) {
|
|
85
|
+
const rel = relativeInside(this.pagesBase(), abs);
|
|
86
|
+
const params = rel ? routeParams(rel) : [];
|
|
87
|
+
if (params.length)
|
|
88
|
+
parsed.params = params;
|
|
89
|
+
}
|
|
90
|
+
this.parsed.set(abs, { mtime: stat.mtimeMs, file: parsed });
|
|
91
|
+
return parsed;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A file is a page when it lives in the pages folder (or, without one, in the project root),
|
|
95
|
+
* is not hidden behind a leading underscore, and is not a pure component module. The Sinth 4
|
|
96
|
+
* `page` keyword still forces a page anywhere.
|
|
97
|
+
*/
|
|
98
|
+
isPageFile(abs, parsed) {
|
|
99
|
+
if (parsed.isPage)
|
|
100
|
+
return true;
|
|
101
|
+
const pagesDir = this.config.pagesDir;
|
|
102
|
+
const hasPagesDir = fs.existsSync(pagesDir);
|
|
103
|
+
const base = hasPagesDir ? pagesDir : this.root;
|
|
104
|
+
const rel = relativeInside(base, abs);
|
|
105
|
+
if (!rel)
|
|
106
|
+
return false;
|
|
107
|
+
const parts = rel.split(/[\\/]/);
|
|
108
|
+
if (parts.some(p => p.startsWith("_") || p.startsWith(".") || IGNORED_DIRS.has(p)))
|
|
109
|
+
return false;
|
|
110
|
+
if (relativeInside(this.config.outDir, abs))
|
|
111
|
+
return false;
|
|
112
|
+
if (!hasPagesDir && parts.length > 1 && ROOT_MODULE_DIRS.has(parts[0]))
|
|
113
|
+
return false;
|
|
114
|
+
const hasPageContent = parsed.body.length > 0 || parsed.meta.length > 0;
|
|
115
|
+
if (hasPageContent)
|
|
116
|
+
return true;
|
|
117
|
+
const isModule = parsed.components.length > 0 || parsed.functions.length > 0;
|
|
118
|
+
return hasPagesDir && !isModule;
|
|
119
|
+
}
|
|
120
|
+
scss() {
|
|
121
|
+
if (this.scssCompiler !== null)
|
|
122
|
+
return this.scssCompiler;
|
|
123
|
+
const loaders = [(0, module_1.createRequire)(path.join(this.root, "package.json")), (0, module_1.createRequire)(__filename)];
|
|
124
|
+
for (const load of loaders) {
|
|
125
|
+
try {
|
|
126
|
+
const sass = load("sass");
|
|
127
|
+
this.scssCompiler = (src, file) => {
|
|
128
|
+
try {
|
|
129
|
+
return sass.compileString(src, { loadPaths: [path.dirname(file), this.root], style: "expanded" }).css;
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
throw new diagnostics_1.SinthError("E0402", `SCSS error: ${err.message.split("\n")[0]}`, { file, line: 1, col: 1 });
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
return this.scssCompiler;
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
this.scssCompiler = undefined;
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
findPages() {
|
|
145
|
+
const pagesDir = this.config.pagesDir;
|
|
146
|
+
const base = fs.existsSync(pagesDir) ? pagesDir : this.root;
|
|
147
|
+
const out = [];
|
|
148
|
+
const walk = (dir) => {
|
|
149
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
150
|
+
const full = path.join(dir, entry.name);
|
|
151
|
+
if (entry.isDirectory()) {
|
|
152
|
+
if (IGNORED_DIRS.has(entry.name) || entry.name.startsWith(".") || full === this.config.outDir)
|
|
153
|
+
continue;
|
|
154
|
+
if (base === this.root && ROOT_MODULE_DIRS.has(entry.name))
|
|
155
|
+
continue;
|
|
156
|
+
walk(full);
|
|
157
|
+
}
|
|
158
|
+
else if (entry.name.endsWith(".sinth") && !entry.name.startsWith("_")) {
|
|
159
|
+
if (!this.getFile(full).isPage)
|
|
160
|
+
continue;
|
|
161
|
+
out.push(this.pageInfo(full, base));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
walk(base);
|
|
166
|
+
return out.sort((a, b) => a.route.localeCompare(b.route));
|
|
167
|
+
}
|
|
168
|
+
pagesBase() {
|
|
169
|
+
return fs.existsSync(this.config.pagesDir) ? this.config.pagesDir : this.root;
|
|
170
|
+
}
|
|
171
|
+
pageInfo(file, base = this.pagesBase()) {
|
|
172
|
+
const rel = path.relative(base, file).replace(/\\/g, "/").replace(/\.sinth$/, "");
|
|
173
|
+
const outRel = rel + ".html";
|
|
174
|
+
const route = "/" + (rel === "index" ? "" : rel.endsWith("/index") ? rel.slice(0, -6) : rel);
|
|
175
|
+
return { file: path.resolve(file), route, outFile: outRel };
|
|
176
|
+
}
|
|
177
|
+
resolveFile(candidate, exts) {
|
|
178
|
+
const tries = [candidate, ...exts.map(e => candidate + e), ...exts.map(e => path.join(candidate, "index" + e))];
|
|
179
|
+
for (const t of tries) {
|
|
180
|
+
try {
|
|
181
|
+
if (fs.statSync(t).isFile())
|
|
182
|
+
return t;
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
resolveBare(spec, from) {
|
|
191
|
+
for (const lib of this.config.libraryPaths) {
|
|
192
|
+
const sinth = this.resolveFile(path.join(lib, spec), [".sinth"]);
|
|
193
|
+
if (sinth)
|
|
194
|
+
return { kind: "sinth", path: sinth };
|
|
195
|
+
const js = this.resolveFile(path.join(lib, spec), [".js", ".mjs", ".ts"]);
|
|
196
|
+
if (js)
|
|
197
|
+
return { kind: "module", path: js };
|
|
198
|
+
}
|
|
199
|
+
const parts = spec.split("/");
|
|
200
|
+
const pkgName = spec.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
201
|
+
const sub = spec.slice(pkgName.length + 1);
|
|
202
|
+
for (let dir = path.dirname(from);; dir = path.dirname(dir)) {
|
|
203
|
+
const pkgDir = path.join(dir, "node_modules", pkgName);
|
|
204
|
+
const pkgJson = path.join(pkgDir, "package.json");
|
|
205
|
+
if (fs.existsSync(pkgJson)) {
|
|
206
|
+
if (sub.endsWith(".sinth"))
|
|
207
|
+
return { kind: "sinth", path: path.join(pkgDir, sub) };
|
|
208
|
+
try {
|
|
209
|
+
const meta = JSON.parse(fs.readFileSync(pkgJson, "utf-8"));
|
|
210
|
+
if (!sub && typeof meta.sinth === "string")
|
|
211
|
+
return { kind: "sinth", path: path.join(pkgDir, meta.sinth) };
|
|
212
|
+
if (sub) {
|
|
213
|
+
const sinth = this.resolveFile(path.join(pkgDir, sub), [".sinth"]);
|
|
214
|
+
if (sinth)
|
|
215
|
+
return { kind: "sinth", path: sinth };
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
return undefined;
|
|
220
|
+
}
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
if (path.dirname(dir) === dir)
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
resolveImports(file) {
|
|
228
|
+
const cached = this.resolved.get(file.path);
|
|
229
|
+
if (cached)
|
|
230
|
+
return cached;
|
|
231
|
+
const out = [];
|
|
232
|
+
for (const decl of file.imports)
|
|
233
|
+
out.push(this.resolveImport(decl, file));
|
|
234
|
+
this.resolved.set(file.path, out);
|
|
235
|
+
return out;
|
|
236
|
+
}
|
|
237
|
+
notFound(decl, tried) {
|
|
238
|
+
throw new diagnostics_1.SinthError("E0601", `Cannot find '${decl.source}'`, decl.loc, tried ? `Looked for ${path.relative(this.root, tried) || tried}` : undefined);
|
|
239
|
+
}
|
|
240
|
+
resolveImport(decl, file) {
|
|
241
|
+
const from = file.path;
|
|
242
|
+
let source = decl.source;
|
|
243
|
+
if (decl.kind === "library") {
|
|
244
|
+
const mapped = LEGACY_LIBRARIES[source];
|
|
245
|
+
if (!mapped) {
|
|
246
|
+
const s = (0, diagnostics_1.suggest)(source, Object.keys(LEGACY_LIBRARIES));
|
|
247
|
+
throw new diagnostics_1.SinthError("E0605", `Unknown library '${source}'`, decl.loc, s ? `Did you mean '${s}'?` : "Import files with a path, e.g. import \"./components/Card.sinth\"");
|
|
248
|
+
}
|
|
249
|
+
source = mapped;
|
|
250
|
+
decl = { ...decl, kind: "sinth", source, sideEffect: true };
|
|
251
|
+
}
|
|
252
|
+
if (source.startsWith("sinth:")) {
|
|
253
|
+
const name = source.slice(6);
|
|
254
|
+
const target = this.resolveFile(path.join(exports.LIB_ROOT, name), [".sinth"]);
|
|
255
|
+
if (!target) {
|
|
256
|
+
const available = fs.existsSync(exports.LIB_ROOT) ? fs.readdirSync(exports.LIB_ROOT) : [];
|
|
257
|
+
throw new diagnostics_1.SinthError("E0605", `Unknown built-in library '${source}'`, decl.loc, available.length ? `Available: ${available.map(a => "sinth:" + a).join(", ")}` : undefined);
|
|
258
|
+
}
|
|
259
|
+
return this.sinthImport({ ...decl, kind: "sinth" }, target);
|
|
260
|
+
}
|
|
261
|
+
const isRelative = source.startsWith("./") || source.startsWith("../");
|
|
262
|
+
const isRootAbs = source.startsWith("/") && !source.startsWith("//");
|
|
263
|
+
const isUrl = /^(https?:)?\/\//.test(source);
|
|
264
|
+
const localBase = isRootAbs ? path.join(this.root, source) : path.resolve(path.dirname(from), source);
|
|
265
|
+
switch (decl.kind) {
|
|
266
|
+
case "style": {
|
|
267
|
+
if (isUrl)
|
|
268
|
+
return { decl, kind: "style", specifier: source };
|
|
269
|
+
if (isRelative || isRootAbs) {
|
|
270
|
+
const target = this.resolveFile(localBase, [".css", ".scss", ".sass"]);
|
|
271
|
+
if (!target)
|
|
272
|
+
this.notFound(decl, localBase);
|
|
273
|
+
return { decl, kind: "style", specifier: target };
|
|
274
|
+
}
|
|
275
|
+
return { decl, kind: "style", specifier: source };
|
|
276
|
+
}
|
|
277
|
+
case "font":
|
|
278
|
+
return { decl, kind: "font", specifier: source };
|
|
279
|
+
case "json": {
|
|
280
|
+
const target = this.resolveFile(localBase, []);
|
|
281
|
+
if (!target)
|
|
282
|
+
this.notFound(decl, localBase);
|
|
283
|
+
try {
|
|
284
|
+
return { decl, kind: "json", specifier: target, json: JSON.parse(fs.readFileSync(target, "utf-8")) };
|
|
285
|
+
}
|
|
286
|
+
catch (err) {
|
|
287
|
+
throw new diagnostics_1.SinthError("E0606", `Invalid JSON in '${source}': ${err.message}`, decl.loc);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
case "script": {
|
|
291
|
+
if (isUrl)
|
|
292
|
+
return { decl, kind: "script", specifier: source };
|
|
293
|
+
const target = this.resolveFile(localBase, [".js", ".mjs", ".ts"]);
|
|
294
|
+
if (!target)
|
|
295
|
+
this.notFound(decl, localBase);
|
|
296
|
+
return { decl: { ...decl, kind: "module" }, kind: "module", specifier: target };
|
|
297
|
+
}
|
|
298
|
+
case "sinth": {
|
|
299
|
+
if (isRelative || isRootAbs) {
|
|
300
|
+
const target = this.resolveFile(localBase, [".sinth"]);
|
|
301
|
+
if (!target) {
|
|
302
|
+
const js = this.resolveFile(localBase, [".js", ".mjs", ".ts", ".tsx", ".jsx"]);
|
|
303
|
+
if (js)
|
|
304
|
+
return { decl: { ...decl, kind: "module" }, kind: "module", specifier: js };
|
|
305
|
+
this.notFound(decl, localBase + (localBase.endsWith(".sinth") ? "" : ".sinth"));
|
|
306
|
+
}
|
|
307
|
+
return this.sinthImport(decl, target);
|
|
308
|
+
}
|
|
309
|
+
const bare = this.resolveBare(source, from);
|
|
310
|
+
if (bare?.kind === "sinth")
|
|
311
|
+
return this.sinthImport(decl, bare.path);
|
|
312
|
+
if (bare)
|
|
313
|
+
return { decl: { ...decl, kind: "module" }, kind: "module", specifier: bare.path };
|
|
314
|
+
throw new diagnostics_1.SinthError("E0601", `Cannot find Sinth module '${source}'`, decl.loc);
|
|
315
|
+
}
|
|
316
|
+
case "module": {
|
|
317
|
+
if (isUrl)
|
|
318
|
+
return { decl, kind: "script", specifier: source };
|
|
319
|
+
if (isRelative || isRootAbs) {
|
|
320
|
+
const target = this.resolveFile(localBase, [".js", ".mjs", ".ts", ".tsx", ".jsx"]);
|
|
321
|
+
if (!target) {
|
|
322
|
+
const sinth = this.resolveFile(localBase, [".sinth"]);
|
|
323
|
+
if (sinth)
|
|
324
|
+
return this.sinthImport({ ...decl, kind: "sinth" }, sinth);
|
|
325
|
+
this.notFound(decl, localBase);
|
|
326
|
+
}
|
|
327
|
+
return { decl, kind: "module", specifier: target };
|
|
328
|
+
}
|
|
329
|
+
const bare = this.resolveBare(source, from);
|
|
330
|
+
if (bare?.kind === "sinth")
|
|
331
|
+
return this.sinthImport({ ...decl, kind: "sinth" }, bare.path);
|
|
332
|
+
if (bare)
|
|
333
|
+
return { decl, kind: "module", specifier: bare.path };
|
|
334
|
+
if (LEGACY_LIBRARIES[source])
|
|
335
|
+
return this.resolveImport({ ...decl, kind: "library" }, file);
|
|
336
|
+
return { decl, kind: "module", specifier: source };
|
|
337
|
+
}
|
|
338
|
+
default:
|
|
339
|
+
throw new diagnostics_1.SinthError("E0607", `Unsupported import '${source}'`, decl.loc);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
sinthImport(decl, target) {
|
|
343
|
+
const other = this.getFile(target);
|
|
344
|
+
if (other.isPage)
|
|
345
|
+
throw new diagnostics_1.SinthError("E0608", `Cannot import page '${decl.source}'; pages are entry points`, decl.loc, "Pages are entry points. Move shared components to components/ (or a file starting with '_') and import that");
|
|
346
|
+
const exports = (0, codegen_1.collectExports)(other);
|
|
347
|
+
return { decl: { ...decl, kind: "sinth" }, kind: "sinth", specifier: target, exports };
|
|
348
|
+
}
|
|
349
|
+
persistPrefix(file) {
|
|
350
|
+
return "sinth:" + path.relative(this.root, file).replace(/\\/g, "/").replace(/\.sinth$/, "");
|
|
351
|
+
}
|
|
352
|
+
compile(file, stack = []) {
|
|
353
|
+
const abs = path.resolve(file);
|
|
354
|
+
const cached = this.compiled.get(abs);
|
|
355
|
+
if (cached)
|
|
356
|
+
return cached;
|
|
357
|
+
if (stack.includes(abs)) {
|
|
358
|
+
const cycle = [...stack.slice(stack.indexOf(abs)), abs].map(p => path.relative(this.root, p)).join(" -> ");
|
|
359
|
+
throw new diagnostics_1.SinthError("E0609", `Circular import: ${cycle}`);
|
|
360
|
+
}
|
|
361
|
+
const parsed = this.getFile(abs);
|
|
362
|
+
const imports = this.resolveImports(parsed);
|
|
363
|
+
for (const imp of imports)
|
|
364
|
+
if (imp.kind === "sinth")
|
|
365
|
+
this.compile(imp.specifier, [...stack, abs]);
|
|
366
|
+
const out = (0, codegen_1.compileModule)(parsed, imports, {
|
|
367
|
+
runtime: "sinth-runtime",
|
|
368
|
+
dev: this.dev,
|
|
369
|
+
scss: this.hasScss(parsed) ? this.scss() : undefined,
|
|
370
|
+
persistPrefix: this.persistPrefix(abs),
|
|
371
|
+
});
|
|
372
|
+
this.compiled.set(abs, out);
|
|
373
|
+
return out;
|
|
374
|
+
}
|
|
375
|
+
hasScss(file) {
|
|
376
|
+
return file.styles.some(s => s.lang === "scss") || file.components.some(c => c.styles.some(s => s.lang === "scss"));
|
|
377
|
+
}
|
|
378
|
+
moduleGraph(entry) {
|
|
379
|
+
const seen = new Set();
|
|
380
|
+
const order = [];
|
|
381
|
+
const visit = (f) => {
|
|
382
|
+
if (seen.has(f))
|
|
383
|
+
return;
|
|
384
|
+
seen.add(f);
|
|
385
|
+
for (const imp of this.resolveImports(this.getFile(f)))
|
|
386
|
+
if (imp.kind === "sinth")
|
|
387
|
+
visit(imp.specifier);
|
|
388
|
+
order.push(f);
|
|
389
|
+
};
|
|
390
|
+
visit(path.resolve(entry));
|
|
391
|
+
return order;
|
|
392
|
+
}
|
|
393
|
+
compilePage(page) {
|
|
394
|
+
const module = this.compile(page.file);
|
|
395
|
+
const graph = this.moduleGraph(page.file);
|
|
396
|
+
let interactive = this.needsRuntime(page.file);
|
|
397
|
+
const styleImports = [];
|
|
398
|
+
const externalStyles = [];
|
|
399
|
+
const externalScripts = [];
|
|
400
|
+
const fonts = [];
|
|
401
|
+
const warnings = [];
|
|
402
|
+
for (const f of graph) {
|
|
403
|
+
const out = this.compile(f);
|
|
404
|
+
warnings.push(...out.diagnostics.warnings);
|
|
405
|
+
for (const imp of this.resolveImports(this.getFile(f))) {
|
|
406
|
+
if (imp.kind === "style")
|
|
407
|
+
(/^(https?:)?\/\//.test(imp.specifier) ? externalStyles : styleImports).push(imp.specifier);
|
|
408
|
+
if (imp.kind === "script")
|
|
409
|
+
externalScripts.push({ src: imp.specifier, attrs: imp.decl.attrs });
|
|
410
|
+
if (imp.kind === "font")
|
|
411
|
+
fonts.push(imp.specifier);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const host = { getFile: f => this.getFile(f), getImports: f => this.resolveImports(f) };
|
|
415
|
+
const file = this.getFile(page.file);
|
|
416
|
+
const instances = [];
|
|
417
|
+
for (const params of this.expandParams(page, file, host)) {
|
|
418
|
+
const pre = (0, prerender_1.prerenderPage)(file, host, params);
|
|
419
|
+
if (!pre.complete)
|
|
420
|
+
interactive = true;
|
|
421
|
+
const inst = { page: params ? fillRoute(page, params) : page, prerendered: pre.html, meta: pre.meta, params };
|
|
422
|
+
for (const m of inst.meta) {
|
|
423
|
+
if (!m.known)
|
|
424
|
+
throw new diagnostics_1.SinthError("E0700", `Metadata '${m.key}' must be computable at build time`, m.loc, "Use literals, imported JSON, constants and route params. Values that need the browser (fetch, localStorage, user input) can't go in the page head");
|
|
425
|
+
}
|
|
426
|
+
instances.push(inst);
|
|
427
|
+
}
|
|
428
|
+
return {
|
|
429
|
+
page, module, graph, interactive, instances, styleImports: unique(styleImports),
|
|
430
|
+
externalStyles: unique(externalStyles), externalScripts, fonts: unique(fonts), warnings,
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
expandParams(page, file, host) {
|
|
434
|
+
const names = file.params;
|
|
435
|
+
const pathsMeta = file.meta.find(m => m.key === "paths");
|
|
436
|
+
if (!names?.length) {
|
|
437
|
+
if (pathsMeta)
|
|
438
|
+
throw new diagnostics_1.SinthError("E0713", "'paths' is only used by dynamic routes", pathsMeta.loc, "Name the file with a [param] segment, e.g. pages/blog/[slug].sinth");
|
|
439
|
+
return [undefined];
|
|
440
|
+
}
|
|
441
|
+
const where = path.relative(this.root, page.file).replace(/\\/g, "/");
|
|
442
|
+
if (!pathsMeta) {
|
|
443
|
+
throw new diagnostics_1.SinthError("E0710", `Dynamic route '${where}' needs 'paths'`, { file: page.file, line: 1, col: 1 }, names.length === 1 ? `List the values to generate, e.g. paths = posts.map((p) => p.${names[0]})` : `List objects with ${names.join(", ")}, e.g. paths = [{ ${names.map(n => n + ": \"...\"").join(", ")} }]`);
|
|
444
|
+
}
|
|
445
|
+
const res = (0, prerender_1.evaluatePaths)(file, host, names);
|
|
446
|
+
if (!res.known || !Array.isArray(res.value)) {
|
|
447
|
+
throw new diagnostics_1.SinthError("E0711", "'paths' must be an array that can be computed at build time", pathsMeta.loc, "Build it from literals, imported JSON or constants");
|
|
448
|
+
}
|
|
449
|
+
const seen = new Set();
|
|
450
|
+
return res.value.map((item, i) => {
|
|
451
|
+
const params = {};
|
|
452
|
+
for (const n of names) {
|
|
453
|
+
const raw = typeof item === "object" && item !== null ? item[n] : names.length === 1 ? item : undefined;
|
|
454
|
+
if (typeof raw !== "string" && typeof raw !== "number") {
|
|
455
|
+
throw new diagnostics_1.SinthError("E0712", `paths[${i}] has no string value for '${n}'`, pathsMeta.loc, names.length === 1 ? "Use strings, or objects with that key" : `Each item needs ${names.join(", ")}`);
|
|
456
|
+
}
|
|
457
|
+
const v = String(raw);
|
|
458
|
+
// eslint-disable-next-line no-control-regex
|
|
459
|
+
if (!v || v === "." || v === ".." || /[\\/?#\u0000-\u001f]/.test(v)) {
|
|
460
|
+
throw new diagnostics_1.SinthError("E0712", `paths[${i}].${n} is not a valid URL segment: ${JSON.stringify(v)}`, pathsMeta.loc, "Values can't be empty or contain / \\ ? #");
|
|
461
|
+
}
|
|
462
|
+
params[n] = v;
|
|
463
|
+
}
|
|
464
|
+
const key = JSON.stringify(names.map(n => params[n]));
|
|
465
|
+
if (seen.has(key))
|
|
466
|
+
throw new diagnostics_1.SinthError("E0712", `paths contains a duplicate: ${JSON.stringify(params)}`, pathsMeta.loc);
|
|
467
|
+
seen.add(key);
|
|
468
|
+
return params;
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
needsRuntime(pageFile) {
|
|
472
|
+
const seen = new Set();
|
|
473
|
+
const visit = (file, component) => {
|
|
474
|
+
const key = file + "#" + (component ?? "");
|
|
475
|
+
if (seen.has(key))
|
|
476
|
+
return false;
|
|
477
|
+
seen.add(key);
|
|
478
|
+
const out = this.compile(file);
|
|
479
|
+
if (out.base)
|
|
480
|
+
return true;
|
|
481
|
+
const info = component ? out.components[component] : out.page;
|
|
482
|
+
if (!info)
|
|
483
|
+
return true;
|
|
484
|
+
if (info.interactive)
|
|
485
|
+
return true;
|
|
486
|
+
return info.uses.some(ref => {
|
|
487
|
+
const i = ref.lastIndexOf("#");
|
|
488
|
+
return visit(ref.slice(0, i), ref.slice(i + 1));
|
|
489
|
+
});
|
|
490
|
+
};
|
|
491
|
+
return visit(path.resolve(pageFile));
|
|
492
|
+
}
|
|
493
|
+
resolveAsset(value, fromFile) {
|
|
494
|
+
if (/^([a-z]+:|\/\/|#|data:)/i.test(value) || value.startsWith("/"))
|
|
495
|
+
return value;
|
|
496
|
+
const candidates = [path.resolve(path.dirname(fromFile), value), path.resolve(this.root, value)];
|
|
497
|
+
for (const c of candidates) {
|
|
498
|
+
if (!fs.existsSync(c))
|
|
499
|
+
continue;
|
|
500
|
+
if (c.startsWith(this.config.publicDir + path.sep))
|
|
501
|
+
return this.config.base + path.relative(this.config.publicDir, c).replace(/\\/g, "/");
|
|
502
|
+
for (const d of this.config.staticDirs) {
|
|
503
|
+
const dir = path.resolve(this.root, d);
|
|
504
|
+
if (c.startsWith(dir + path.sep))
|
|
505
|
+
return this.config.base + path.relative(this.root, c).replace(/\\/g, "/");
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return this.config.base + value.replace(/^\.\//, "");
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
exports.Project = Project;
|
|
512
|
+
/**
|
|
513
|
+
* Path of `file` relative to `dir`, or undefined when it is outside. Falls back to real paths,
|
|
514
|
+
* because bundlers resolve symlinks (macOS /var -> /private/var, junctions, linked workspaces).
|
|
515
|
+
*/
|
|
516
|
+
function relativeInside(dir, file) {
|
|
517
|
+
const check = (d, f) => {
|
|
518
|
+
const rel = path.relative(d, f);
|
|
519
|
+
return rel && !rel.startsWith("..") && !path.isAbsolute(rel) ? rel : undefined;
|
|
520
|
+
};
|
|
521
|
+
const direct = check(dir, file);
|
|
522
|
+
if (direct)
|
|
523
|
+
return direct;
|
|
524
|
+
try {
|
|
525
|
+
return check(fs.realpathSync.native(dir), fs.realpathSync.native(file));
|
|
526
|
+
}
|
|
527
|
+
catch {
|
|
528
|
+
return undefined;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
function routeParams(rel) {
|
|
532
|
+
const names = [];
|
|
533
|
+
const shown = rel.replace(/\\/g, "/");
|
|
534
|
+
for (const m of rel.replace(/\.sinth$/, "").matchAll(/\[([^\]]*)\]/g)) {
|
|
535
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(m[1]))
|
|
536
|
+
throw new diagnostics_1.SinthError("E0714", `Invalid route parameter '[${m[1]}]' in ${shown}`, undefined, "Use a plain name like [slug] or [id]");
|
|
537
|
+
if (names.includes(m[1]))
|
|
538
|
+
throw new diagnostics_1.SinthError("E0714", `Route parameter '${m[1]}' is used twice in ${shown}`);
|
|
539
|
+
names.push(m[1]);
|
|
540
|
+
}
|
|
541
|
+
return names;
|
|
542
|
+
}
|
|
543
|
+
function fillRoute(page, params) {
|
|
544
|
+
const fill = (s, enc) => s.replace(/\[([A-Za-z_][A-Za-z0-9_]*)\]/g, (_, n) => enc(params[n]));
|
|
545
|
+
return { file: page.file, route: fill(page.route, encodeURIComponent), outFile: fill(page.outFile, v => v) };
|
|
546
|
+
}
|
|
547
|
+
function unique(items) {
|
|
548
|
+
return [...new Set(items)];
|
|
549
|
+
}
|
|
550
|
+
function fontHref(spec) {
|
|
551
|
+
const [family, weights] = spec.split(":");
|
|
552
|
+
const fam = family.trim().replace(/\s+/g, "+");
|
|
553
|
+
const w = (weights ?? "400;700").split(/[;,]/).map(s => s.trim()).filter(s => /^\d{3}$/.test(s)).sort();
|
|
554
|
+
return `https://fonts.googleapis.com/css2?family=${encodeURIComponent(fam).replace(/%2B/g, "+")}:wght@${w.join(";")}&display=swap`;
|
|
555
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
type Getter<T = unknown> = () => T;
|
|
2
|
+
type Cleanup = () => void;
|
|
3
|
+
type Render = (parent: ParentNode) => void;
|
|
4
|
+
export interface LoopContext {
|
|
5
|
+
item: unknown;
|
|
6
|
+
index: number;
|
|
7
|
+
key: unknown;
|
|
8
|
+
}
|
|
9
|
+
export interface Props {
|
|
10
|
+
[name: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
export type Slots = Record<string, Render | undefined>;
|
|
13
|
+
export type Component = (parent: ParentNode, props: Props, slots: Slots, fallthrough?: (el: Element) => void) => void;
|
|
14
|
+
export declare function update(): void;
|
|
15
|
+
export declare function schedule(): void;
|
|
16
|
+
export declare function toText(value: unknown): string;
|
|
17
|
+
export declare function el(tag: string): HTMLElement;
|
|
18
|
+
export declare function svg(tag: string): Element;
|
|
19
|
+
export declare function tpl(html: string, svgContext?: boolean): DocumentFragment;
|
|
20
|
+
export declare function text(parent: ParentNode, get: Getter): void;
|
|
21
|
+
export declare function sanitizeUrl(value: string): string;
|
|
22
|
+
export declare function attr(node: Element, name: string, get: Getter): void;
|
|
23
|
+
export declare function prop(node: Element, name: string, get: Getter): void;
|
|
24
|
+
export declare function classValue(value: unknown): string;
|
|
25
|
+
export declare function cls(node: Element, get: Getter): void;
|
|
26
|
+
export declare function style(node: HTMLElement | SVGElement, get: Getter): void;
|
|
27
|
+
export declare function styleProp(node: HTMLElement | SVGElement, name: string, get: Getter): void;
|
|
28
|
+
export declare function cssVars(nodes: (Element | null)[], vars: Record<string, Getter>): void;
|
|
29
|
+
export declare function show(node: HTMLElement | SVGElement, get: Getter): void;
|
|
30
|
+
export declare function on(node: EventTarget, event: string, handler: (e: Event) => unknown, modifiers?: string[]): void;
|
|
31
|
+
export declare function model(node: HTMLElement, get: Getter, set: (v: unknown) => void, numeric?: boolean): void;
|
|
32
|
+
export declare function ref(node: Element, set: (el: Element | null) => void): void;
|
|
33
|
+
interface IfOptions {
|
|
34
|
+
persist?: boolean;
|
|
35
|
+
delay?: Getter<number>;
|
|
36
|
+
transition?: string;
|
|
37
|
+
}
|
|
38
|
+
export declare function when(parent: ParentNode, branches: [Getter | null, Render][], options?: IfOptions): void;
|
|
39
|
+
export declare function iterate(value: unknown): LoopContext[];
|
|
40
|
+
export declare function each(parent: ParentNode, list: Getter, render: (parent: ParentNode, ctx: LoopContext) => void, keyOf?: (ctx: LoopContext) => unknown, filter?: (ctx: LoopContext) => unknown, empty?: Render): void;
|
|
41
|
+
export declare function html(parent: ParentNode, get: Getter<unknown>): void;
|
|
42
|
+
export declare function component(parent: ParentNode, comp: Component, props: Props, slots: Slots, fallthrough?: (el: Element) => void): Element | null;
|
|
43
|
+
export declare function slot(parent: ParentNode, slots: Slots, name: string, fallback?: Render): void;
|
|
44
|
+
export declare function onMount(fn: () => unknown): void;
|
|
45
|
+
export declare function onDestroy(fn: Cleanup): void;
|
|
46
|
+
export declare function watch(get: Getter, fn: (value: unknown, previous: unknown) => unknown): void;
|
|
47
|
+
export declare function persisted<T>(key: string, fallback: T): T;
|
|
48
|
+
export declare function persist(key: string, get: Getter): void;
|
|
49
|
+
export declare function memo<T>(site: string, fn: () => T, args: unknown[]): T;
|
|
50
|
+
export declare function range(a: number, b?: number, step?: number): number[];
|
|
51
|
+
export declare function iter(value: unknown): [unknown, number, unknown][];
|
|
52
|
+
export declare function injectCss(id: string, css: string): void;
|
|
53
|
+
export interface MountedApp {
|
|
54
|
+
update(): void;
|
|
55
|
+
dispose(): void;
|
|
56
|
+
}
|
|
57
|
+
export interface PageModule {
|
|
58
|
+
render: Render;
|
|
59
|
+
state?: () => Record<string, unknown>;
|
|
60
|
+
restore?: (state: Record<string, unknown>) => void;
|
|
61
|
+
dev?: boolean;
|
|
62
|
+
}
|
|
63
|
+
export declare function mount(target: Element, render: Render): MountedApp;
|
|
64
|
+
export declare function boot(page: PageModule): void;
|
|
65
|
+
export declare function defineElement(tag: string, comp: Component, params: [string, string][], css?: [string, string]): void;
|
|
66
|
+
/** Reads a route parameter of a dynamic page (`pages/blog/[slug].sinth`). */
|
|
67
|
+
export declare function param(name: string): string;
|
|
68
|
+
export {};
|