@yannosay/sinth 0.0.13 → 0.0.15
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/package.json +1 -1
- package/src/sinth.ts +46 -37
package/package.json
CHANGED
package/src/sinth.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// Sinth Compiler v0.0.
|
|
2
|
+
// Sinth Compiler v0.0.15 — enum fix, raw-block detection, import resolution, CSS conversion, slot escaping, custom-elements, HTML5 map
|
|
3
3
|
|
|
4
4
|
import * as fs from "fs";
|
|
5
5
|
import * as path from "path";
|
|
@@ -11,7 +11,7 @@ import * as http from "http";
|
|
|
11
11
|
|
|
12
12
|
interface Loc { file: string; line: number; col: number }
|
|
13
13
|
|
|
14
|
-
// === v0.0.
|
|
14
|
+
// === v0.0.15: Must be plain `enum TT`, NOT `const enum TT` — const enum breaks TT[t] string indexing ===
|
|
15
15
|
enum TT {
|
|
16
16
|
LBRACE, RBRACE, LPAREN, RPAREN, COLON, COMMA, EQUALS, DOT,
|
|
17
17
|
STRING, NUMBER, BOOL_TRUE, BOOL_FALSE, NULL_LIT,
|
|
@@ -20,7 +20,7 @@ enum TT {
|
|
|
20
20
|
KW_IMPORT, KW_AS, KW_CSS,
|
|
21
21
|
KW_COMPONENT, KW_STYLE, KW_SCRIPT,
|
|
22
22
|
KW_GLOBAL, KW_PAGE, KW_LANG,
|
|
23
|
-
// === v0.0.
|
|
23
|
+
// === v0.0.15: KW_CUSTOM_EL and KW_CUSTOM ensured present ===
|
|
24
24
|
KW_CUSTOM_EL,
|
|
25
25
|
KW_CUSTOM,
|
|
26
26
|
EOF,
|
|
@@ -102,7 +102,7 @@ class SinthError extends Error {
|
|
|
102
102
|
return s;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
// === v0.0.
|
|
105
|
+
// === v0.0.15: Attach source line to produce error preview ===
|
|
106
106
|
withSource(lines: string[]): SinthError {
|
|
107
107
|
if (!this.loc || this.sourceLine !== undefined) return this;
|
|
108
108
|
const line = lines[this.loc.line - 1] ?? "";
|
|
@@ -173,7 +173,7 @@ const KEYWORDS: Record<string, TT> = {
|
|
|
173
173
|
"true": TT.BOOL_TRUE,
|
|
174
174
|
"false": TT.BOOL_FALSE,
|
|
175
175
|
"null": TT.NULL_LIT,
|
|
176
|
-
// === v0.0.
|
|
176
|
+
// === v0.0.15: custom-element and custom lexed as hyphenated identifier ===
|
|
177
177
|
"custom-element": TT.KW_CUSTOM_EL,
|
|
178
178
|
"custom": TT.KW_CUSTOM,
|
|
179
179
|
};
|
|
@@ -221,7 +221,7 @@ export class Lexer {
|
|
|
221
221
|
|
|
222
222
|
if (this.isIdentStart(ch)) {
|
|
223
223
|
const tok = this.readIdent(loc);
|
|
224
|
-
// === v0.0.
|
|
224
|
+
// === v0.0.15: Also set rawBlockPending for component, custom-element, custom ===
|
|
225
225
|
if (
|
|
226
226
|
tok.type === TT.KW_STYLE ||
|
|
227
227
|
tok.type === TT.KW_SCRIPT ||
|
|
@@ -386,7 +386,7 @@ export class Lexer {
|
|
|
386
386
|
|
|
387
387
|
private readIdent(loc: Loc): Token {
|
|
388
388
|
let s = "";
|
|
389
|
-
// === v0.0.
|
|
389
|
+
// === v0.0.15: isIdentCont includes '-' so "custom-element" is read as one token ===
|
|
390
390
|
while (this.pos < this.src.length && this.isIdentCont(this.src[this.pos])) {
|
|
391
391
|
s += this.src[this.pos]; this.adv();
|
|
392
392
|
}
|
|
@@ -407,7 +407,7 @@ export class Lexer {
|
|
|
407
407
|
private loc(): Loc { return { file: this.file, line: this.line, col: this.col }; }
|
|
408
408
|
private isDigit(c: string) { return c >= "0" && c <= "9"; }
|
|
409
409
|
private isIdentStart(c: string) { return /[a-zA-Z_\u00C0-\uFFFF]/.test(c); }
|
|
410
|
-
// === v0.0.
|
|
410
|
+
// === v0.0.15: '-' allowed in identifiers so "custom-element" is a single token ===
|
|
411
411
|
private isIdentCont(c: string) { return /[a-zA-Z0-9_\-\u00C0-\uFFFF]/.test(c); }
|
|
412
412
|
}
|
|
413
413
|
|
|
@@ -503,14 +503,14 @@ export class Parser {
|
|
|
503
503
|
const loc = this.consume(TT.KW_COMPONENT).loc;
|
|
504
504
|
const name = this.consume(TT.IDENT).value;
|
|
505
505
|
const params = this.parseParamDecls();
|
|
506
|
-
// === v0.0.
|
|
506
|
+
// === v0.0.15: rawBlockPending was set by the lexer when KW_COMPONENT was read;
|
|
507
507
|
// the next token is therefore RAW_BLOCK, not LBRACE ===
|
|
508
508
|
const rawTok = this.consume(TT.RAW_BLOCK);
|
|
509
509
|
const { body, styles, scripts } = this.parseCompBody(rawTok.value, rawTok.loc);
|
|
510
510
|
return { name, params, body, styles, scripts, loc };
|
|
511
511
|
}
|
|
512
512
|
|
|
513
|
-
// === v0.0.
|
|
513
|
+
// === v0.0.15: Parse custom-element / custom declarations ===
|
|
514
514
|
private parseCustomEl(): CustomElDecl {
|
|
515
515
|
const loc = this.peek().loc;
|
|
516
516
|
if (this.check(TT.KW_CUSTOM_EL)) this.consume(TT.KW_CUSTOM_EL);
|
|
@@ -690,7 +690,7 @@ interface ResolvedImports {
|
|
|
690
690
|
// Module-level import stack for circular detection
|
|
691
691
|
const IMPORT_STACK: string[] = [];
|
|
692
692
|
|
|
693
|
-
// === v0.0.
|
|
693
|
+
// === v0.0.15: parseFile catches errors and attaches source line preview ===
|
|
694
694
|
function parseFile(filePath: string): SinthFile {
|
|
695
695
|
if (!fs.existsSync(filePath)) throw new SinthError(`File not found: ${filePath}`);
|
|
696
696
|
const src = fs.readFileSync(filePath, "utf-8");
|
|
@@ -744,7 +744,7 @@ function resolveImports(
|
|
|
744
744
|
const src = resolveLibrary(imp.name, cfg);
|
|
745
745
|
jsLinks.push({ src, attrs: {} });
|
|
746
746
|
|
|
747
|
-
// === v0.0.
|
|
747
|
+
// === v0.0.15: Auto-import companion .sinth for JS libraries (e.g. sinthui.sinth) ===
|
|
748
748
|
for (const libDir of cfg.libraryPaths) {
|
|
749
749
|
const companion = path.join(libDir, imp.name + ".sinth");
|
|
750
750
|
if (fs.existsSync(companion) && !visited.has(companion)) {
|
|
@@ -802,7 +802,7 @@ function resolveLibrary(name: string, cfg: ResolverConfig): string {
|
|
|
802
802
|
return `/libraries/${base}`;
|
|
803
803
|
}
|
|
804
804
|
|
|
805
|
-
// === v0.0.
|
|
805
|
+
// === v0.0.15: resolveSinthPath — first try relative to importing file, then library paths ===
|
|
806
806
|
function resolveSinthPath(p: string, fromFile: string, cfg: ResolverConfig): string {
|
|
807
807
|
// Explicit relative path
|
|
808
808
|
if (p.startsWith("./") || p.startsWith("../")) {
|
|
@@ -813,7 +813,7 @@ function resolveSinthPath(p: string, fromFile: string, cfg: ResolverConfig): str
|
|
|
813
813
|
throw new SinthError(`Cannot resolve import '${p}' from '${fromFile}'`);
|
|
814
814
|
}
|
|
815
815
|
|
|
816
|
-
// === v0.0.
|
|
816
|
+
// === v0.0.15: Non-relative paths (e.g. "components/Card.sinth"): try relative to importing
|
|
817
817
|
// file's directory FIRST, then fall back to library paths ===
|
|
818
818
|
const relToFile = path.resolve(path.dirname(fromFile), p);
|
|
819
819
|
if (fs.existsSync(relToFile)) return relToFile;
|
|
@@ -839,7 +839,7 @@ function resolveSinthPath(p: string, fromFile: string, cfg: ResolverConfig): str
|
|
|
839
839
|
|
|
840
840
|
interface BuiltinInfo { tag: string; defaultClass?: string; voidEl?: boolean }
|
|
841
841
|
|
|
842
|
-
// === v0.0.
|
|
842
|
+
// === v0.0.15: Complete HTML5 map; Col = void <col>; Column = grid div; added SubHeading, CardGrid ===
|
|
843
843
|
const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
844
844
|
// ── Structural ──────────────────────────────────────────
|
|
845
845
|
Main: { tag: "main" },
|
|
@@ -857,12 +857,12 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
857
857
|
Flex: { tag: "div", defaultClass: "flex" },
|
|
858
858
|
Stack: { tag: "div", defaultClass: "stack" },
|
|
859
859
|
Row: { tag: "div", defaultClass: "row" },
|
|
860
|
-
// === v0.0.
|
|
860
|
+
// === v0.0.15: Column = grid helper div; Col = HTML void table col ===
|
|
861
861
|
Column: { tag: "div", defaultClass: "col" },
|
|
862
862
|
// ── Typography ────────────────────────────────────────────
|
|
863
863
|
Heading: { tag: "h1" }, // resolved dynamically by level attr
|
|
864
864
|
Paragraph: { tag: "p" },
|
|
865
|
-
// === v0.0.
|
|
865
|
+
// === v0.0.15: SubHeading convenience component ===
|
|
866
866
|
SubHeading: { tag: "p", defaultClass: "subheading" },
|
|
867
867
|
Lead: { tag: "p", defaultClass: "lead" },
|
|
868
868
|
Small: { tag: "small" },
|
|
@@ -929,7 +929,7 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
929
929
|
Wbr: { tag: "wbr", voidEl: true },
|
|
930
930
|
Source: { tag: "source", voidEl: true },
|
|
931
931
|
Embed: { tag: "embed", voidEl: true },
|
|
932
|
-
// === v0.0.
|
|
932
|
+
// === v0.0.15: Col = HTML void table column (NOT the grid div) ===
|
|
933
933
|
Col: { tag: "col", voidEl: true },
|
|
934
934
|
Area: { tag: "area", voidEl: true },
|
|
935
935
|
// ── Lists ─────────────────────────────────────────────────
|
|
@@ -949,9 +949,9 @@ const BUILTIN_MAP: Record<string, BuiltinInfo> = {
|
|
|
949
949
|
Template: { tag: "template" },
|
|
950
950
|
Slot: { tag: "slot" },
|
|
951
951
|
NoScript: { tag: "noscript" },
|
|
952
|
-
// === v0.0.
|
|
952
|
+
// === v0.0.15: CardGrid convenience layout wrapper ===
|
|
953
953
|
CardGrid: { tag: "div", defaultClass: "card-grid" },
|
|
954
|
-
// === v0.0.
|
|
954
|
+
// === v0.0.15: RawHTML marker — handled specially in renderCompUse ===
|
|
955
955
|
RawHTML: { tag: "__RAW__" },
|
|
956
956
|
};
|
|
957
957
|
|
|
@@ -970,7 +970,7 @@ function eventAttrName(name: string): string | null {
|
|
|
970
970
|
// SECTION 7: STYLE PROCESSOR
|
|
971
971
|
// ═══════════════════════════════════════════════════════════
|
|
972
972
|
|
|
973
|
-
// === v0.0.
|
|
973
|
+
// === v0.0.15: Completely rewritten convertCSSProps — bulletproof line-by-line approach ===
|
|
974
974
|
// Rules:
|
|
975
975
|
// - Match lines of the form: [whitespace] camelCaseProp : "value" [;]
|
|
976
976
|
// - Convert property name camelCase → kebab-case
|
|
@@ -1024,7 +1024,7 @@ function convertCSSProps(css: string): string {
|
|
|
1024
1024
|
}).join("\n");
|
|
1025
1025
|
}
|
|
1026
1026
|
|
|
1027
|
-
// === v0.0.
|
|
1027
|
+
// === v0.0.15: processStyleBlock — correct order: interpolate {param} → camelCase convert → SCSS → scope ===
|
|
1028
1028
|
function processStyleBlock(
|
|
1029
1029
|
block: StyleBlock,
|
|
1030
1030
|
hash: string,
|
|
@@ -1210,7 +1210,7 @@ function renderAttr(attr: Attr, paramMap: Map<string, string>): string {
|
|
|
1210
1210
|
return `${name}="${escAttr(raw)}"`;
|
|
1211
1211
|
}
|
|
1212
1212
|
|
|
1213
|
-
// === v0.0.
|
|
1213
|
+
// === v0.0.15: renderText — "$slot" protected from double-escaping via placeholder approach ===
|
|
1214
1214
|
function renderText(text: string, params: Map<string, string>): string {
|
|
1215
1215
|
// Map of placeholder → raw HTML for slot-like params
|
|
1216
1216
|
const rawSlots = new Map<string, string>();
|
|
@@ -1248,7 +1248,7 @@ function renderText(text: string, params: Map<string, string>): string {
|
|
|
1248
1248
|
// Step 5: Restore \$ → $
|
|
1249
1249
|
s = s.replace(/\x00DOLLAR\x00/g, "$");
|
|
1250
1250
|
|
|
1251
|
-
// === v0.0.
|
|
1251
|
+
// === v0.0.15: Warn about {identifier} patterns remaining after substitution ===
|
|
1252
1252
|
const braceRe = /\{([a-zA-Z_][a-zA-Z0-9_]*)\}/g;
|
|
1253
1253
|
let m: RegExpExecArray | null;
|
|
1254
1254
|
while ((m = braceRe.exec(s)) !== null) {
|
|
@@ -1284,7 +1284,7 @@ function renderCompUse(
|
|
|
1284
1284
|
params: Map<string, string>,
|
|
1285
1285
|
depth: number,
|
|
1286
1286
|
): string {
|
|
1287
|
-
// === v0.0.
|
|
1287
|
+
// === v0.0.15: RawHTML — output content attribute verbatim, no wrapping tag, no scoping ===
|
|
1288
1288
|
if (use.name === "RawHTML") {
|
|
1289
1289
|
const ca = use.attrs.find(a => a.name === "content");
|
|
1290
1290
|
if (!ca || !ca.value) return "";
|
|
@@ -1329,7 +1329,7 @@ function renderCompUse(
|
|
|
1329
1329
|
const classes = [defaultClass, userClass].filter(Boolean).join(" ");
|
|
1330
1330
|
if (classes) attrParts.push(`class="${escAttr(classes)}"`);
|
|
1331
1331
|
|
|
1332
|
-
// === v0.0.
|
|
1332
|
+
// === v0.0.15: Default type="button" for <button> elements ===
|
|
1333
1333
|
if (tag === "button" && !use.attrs.some(a => a.name === "type")) {
|
|
1334
1334
|
attrParts.push(`type="button"`);
|
|
1335
1335
|
}
|
|
@@ -1391,7 +1391,7 @@ function expandUserComp(
|
|
|
1391
1391
|
const slotHTML = use.children.map(c => renderChild(c, ctx, params, depth)).join("");
|
|
1392
1392
|
local.set("slot", slotHTML);
|
|
1393
1393
|
|
|
1394
|
-
// === v0.0.
|
|
1394
|
+
// === v0.0.15: Process component style blocks with instance params and push to extraCSS ===
|
|
1395
1395
|
for (const block of def.styles) {
|
|
1396
1396
|
ctx.extraCSS.push(processStyleBlock(block, ctx.scopeHash, local));
|
|
1397
1397
|
}
|
|
@@ -1492,7 +1492,7 @@ function buildHeadData(meta: MetaEntry[]): HeadData {
|
|
|
1492
1492
|
return { title, fav, lang, charset, viewport, metaTags, metaProps };
|
|
1493
1493
|
}
|
|
1494
1494
|
|
|
1495
|
-
// === v0.0.
|
|
1495
|
+
// === v0.0.15: .jpg and .jpeg return "image/jpeg" ===
|
|
1496
1496
|
function faviconType(p: string): string {
|
|
1497
1497
|
const ext = path.extname(p).toLowerCase();
|
|
1498
1498
|
switch (ext) {
|
|
@@ -1555,7 +1555,7 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
1555
1555
|
const { allDefs, customEls, cssLinks, jsLinks } = resolveImports(file, cfg);
|
|
1556
1556
|
const hash = fnv1a(absPath);
|
|
1557
1557
|
|
|
1558
|
-
// === v0.0.
|
|
1558
|
+
// === v0.0.15: extraCSS collected during rendering (per-instance component styles) ===
|
|
1559
1559
|
const ctx: CompileCtx = {
|
|
1560
1560
|
allDefs, customEls, cssLinks, jsLinks,
|
|
1561
1561
|
scopeHash: hash, pageFile: absPath, extraCSS: [],
|
|
@@ -1591,7 +1591,16 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
1591
1591
|
return rel.startsWith(".") ? rel : "./" + rel;
|
|
1592
1592
|
});
|
|
1593
1593
|
|
|
1594
|
-
|
|
1594
|
+
// Convert JS absolute paths to relative paths from the HTML output
|
|
1595
|
+
const relativeJsLinks = jsLinks.map(js => ({
|
|
1596
|
+
...js,
|
|
1597
|
+
src: (() => {
|
|
1598
|
+
const rel = path.relative(path.dirname(absPath), js.src).replace(/\\/g, "/");
|
|
1599
|
+
return rel.startsWith(".") ? rel : "./" + rel;
|
|
1600
|
+
})(),
|
|
1601
|
+
}));
|
|
1602
|
+
|
|
1603
|
+
const head = renderHead(headData, relativeCssLinks, relativeJsLinks, scopedCSS, companionJS);
|
|
1595
1604
|
const scriptTags: string[] = [];
|
|
1596
1605
|
|
|
1597
1606
|
if (componentScripts.length > 0) {
|
|
@@ -1626,7 +1635,7 @@ function minifyHTML(html: string): string {
|
|
|
1626
1635
|
// SECTION 12: FILE DISCOVERY & ASSET COPY
|
|
1627
1636
|
// ═══════════════════════════════════════════════════════════
|
|
1628
1637
|
|
|
1629
|
-
// === v0.0.
|
|
1638
|
+
// === v0.0.15: findSinthPages skips outDir to prevent infinite watcher loops ===
|
|
1630
1639
|
function findSinthPages(dir: string, outDir?: string): string[] {
|
|
1631
1640
|
const results: string[] = [];
|
|
1632
1641
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
@@ -1642,7 +1651,7 @@ function findSinthPages(dir: string, outDir?: string): string[] {
|
|
|
1642
1651
|
return results;
|
|
1643
1652
|
}
|
|
1644
1653
|
|
|
1645
|
-
// === v0.0.
|
|
1654
|
+
// === v0.0.15: Recursive directory copy for assets/ ===
|
|
1646
1655
|
function copyDir(src: string, dest: string): void {
|
|
1647
1656
|
if (!fs.existsSync(src)) return;
|
|
1648
1657
|
fs.mkdirSync(dest, { recursive: true });
|
|
@@ -1701,7 +1710,7 @@ async function startDevServer(opts: CompileOptions & { port: number, files?: str
|
|
|
1701
1710
|
|
|
1702
1711
|
compileAll();
|
|
1703
1712
|
|
|
1704
|
-
// === v0.0.
|
|
1713
|
+
// === v0.0.15: Watch ignores changes whose absolute path is inside outDir ===
|
|
1705
1714
|
const resolvedOut = path.resolve(opts.outDir);
|
|
1706
1715
|
// Use fs.watch with fs.watchFile fallback for Windows reliability
|
|
1707
1716
|
let watchReady = false;
|
|
@@ -1863,7 +1872,7 @@ async function main(): Promise<void> {
|
|
|
1863
1872
|
}
|
|
1864
1873
|
}
|
|
1865
1874
|
|
|
1866
|
-
// === v0.0.
|
|
1875
|
+
// === v0.0.15: Copy assets/ to outDir/assets/ ===
|
|
1867
1876
|
const assetsIn = path.join(cwd, "assets");
|
|
1868
1877
|
const assetsOut = path.join(outDir, "assets");
|
|
1869
1878
|
if (fs.existsSync(assetsIn)) {
|
|
@@ -1902,11 +1911,11 @@ async function main(): Promise<void> {
|
|
|
1902
1911
|
break;
|
|
1903
1912
|
}
|
|
1904
1913
|
|
|
1905
|
-
// === v0.0.
|
|
1914
|
+
// === v0.0.15: sinth version command ===
|
|
1906
1915
|
case "version":
|
|
1907
1916
|
case "--version":
|
|
1908
1917
|
case "-v": {
|
|
1909
|
-
process.stdout.write("Sinth Compiler v0.0.
|
|
1918
|
+
process.stdout.write("Sinth Compiler v0.0.15\n");
|
|
1910
1919
|
break;
|
|
1911
1920
|
}
|
|
1912
1921
|
|
|
@@ -1919,7 +1928,7 @@ async function main(): Promise<void> {
|
|
|
1919
1928
|
|
|
1920
1929
|
default: {
|
|
1921
1930
|
process.stdout.write(`
|
|
1922
|
-
\x1b[1mSinth Compiler v0.0.
|
|
1931
|
+
\x1b[1mSinth Compiler v0.0.15\x1b[0m
|
|
1923
1932
|
|
|
1924
1933
|
\x1b[1mCommands:\x1b[0m
|
|
1925
1934
|
sinth build [--out ./dist] [--prod] Compile all .sinth pages + copy assets/
|