@cadence-app/compiler 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +54 -0
  3. package/dist/build.d.ts +31 -0
  4. package/dist/build.js +437 -0
  5. package/dist/cli.d.ts +2 -0
  6. package/dist/cli.js +180 -0
  7. package/dist/dev-server.d.ts +16 -0
  8. package/dist/dev-server.js +81 -0
  9. package/dist/engine-budget.d.ts +9 -0
  10. package/dist/engine-budget.js +8 -0
  11. package/dist/hash.d.ts +2 -0
  12. package/dist/hash.js +12 -0
  13. package/dist/host/browser-mock-host.d.ts +1 -0
  14. package/dist/host/browser-mock-host.js +175 -0
  15. package/dist/host/dev-client.d.ts +1 -0
  16. package/dist/host/dev-client.js +6 -0
  17. package/dist/host/engine-entry.d.ts +1 -0
  18. package/dist/host/engine-entry.js +199 -0
  19. package/dist/index.d.ts +23 -0
  20. package/dist/index.js +14 -0
  21. package/dist/manifest.d.ts +47 -0
  22. package/dist/manifest.js +202 -0
  23. package/dist/publish/credentials.d.ts +17 -0
  24. package/dist/publish/credentials.js +87 -0
  25. package/dist/publish/distribution-client.d.ts +45 -0
  26. package/dist/publish/distribution-client.js +113 -0
  27. package/dist/publish/publish-bundle.d.ts +27 -0
  28. package/dist/publish/publish-bundle.js +44 -0
  29. package/dist/publish/publish-errors.d.ts +41 -0
  30. package/dist/publish/publish-errors.js +60 -0
  31. package/dist/publish/read-secret.d.ts +7 -0
  32. package/dist/publish/read-secret.js +30 -0
  33. package/dist/style/compile-style.d.ts +13 -0
  34. package/dist/style/compile-style.js +135 -0
  35. package/dist/style/theme-sheet.d.ts +6 -0
  36. package/dist/style/theme-sheet.js +8 -0
  37. package/dist/template/ast.d.ts +47 -0
  38. package/dist/template/ast.js +1 -0
  39. package/dist/template/codegen.d.ts +8 -0
  40. package/dist/template/codegen.js +220 -0
  41. package/dist/template/entities.d.ts +12 -0
  42. package/dist/template/entities.js +103 -0
  43. package/dist/template/expression.d.ts +16 -0
  44. package/dist/template/expression.js +196 -0
  45. package/dist/template/parser.d.ts +10 -0
  46. package/dist/template/parser.js +437 -0
  47. package/package.json +62 -0
@@ -0,0 +1,437 @@
1
+ import { decodeHtmlEntities } from "./entities.js";
2
+ export class TemplateParseError extends Error {
3
+ /** Où le parseur a buté, en coordonnées d'éditeur : la première ligne est 1. */
4
+ line;
5
+ column;
6
+ /** Le motif seul, sans la position que le message répète. */
7
+ reason;
8
+ constructor(message, line, column) {
9
+ super(`${message} (line ${line}, column ${column})`);
10
+ this.name = 'TemplateParseError';
11
+ this.reason = message;
12
+ this.line = line;
13
+ this.column = column;
14
+ }
15
+ }
16
+ const INTERPOLATION_OPEN = '{{';
17
+ const INTERPOLATION_CLOSE = '}}';
18
+ const VOID_ELEMENTS = new Set([
19
+ 'area',
20
+ 'base',
21
+ 'br',
22
+ 'col',
23
+ 'embed',
24
+ 'hr',
25
+ 'image',
26
+ 'img',
27
+ 'input',
28
+ 'link',
29
+ 'meta',
30
+ 'source',
31
+ 'track',
32
+ 'wbr',
33
+ ]);
34
+ const BLOCK_KEYWORD_PATTERN = /^@(if|else|for|switch|case|default)\b/;
35
+ const TAG_NAME_PATTERN = /^[a-zA-Z][a-zA-Z0-9-]*/;
36
+ const ATTR_NAME_PATTERN = /^[^\s=/>"']+/;
37
+ const FOR_HEADER_PATTERN = /^\s*([A-Za-z_$][\w$]*)\s+of\s+([\s\S]+?)\s*;\s*track\s+([\s\S]+?)\s*$/;
38
+ const RESERVED_LOOP_NAMES = new Set(['ctx', 'rt', 'rc', '$event', '$index']);
39
+ export function parseTemplate(source) {
40
+ return new TemplateParser(source).parseTopLevel();
41
+ }
42
+ class TemplateParser {
43
+ source;
44
+ position = 0;
45
+ constructor(source) {
46
+ this.source = source;
47
+ }
48
+ parseTopLevel() {
49
+ const nodes = this.parseChildren(null, false);
50
+ if (!this.eof()) {
51
+ this.fail(`unexpected '${this.source[this.position]}'`);
52
+ }
53
+ return nodes;
54
+ }
55
+ parseChildren(closingTag, insideBlock) {
56
+ const nodes = [];
57
+ while (!this.eof()) {
58
+ if (this.startsWith('</')) {
59
+ if (closingTag === null) {
60
+ this.fail('unexpected closing tag');
61
+ }
62
+ return nodes;
63
+ }
64
+ if (insideBlock && this.startsWith('}')) {
65
+ return nodes;
66
+ }
67
+ if (this.startsWith('<!--')) {
68
+ this.skipComment();
69
+ continue;
70
+ }
71
+ if (this.startsWith('<')) {
72
+ nodes.push(this.parseElement());
73
+ continue;
74
+ }
75
+ if (this.atBlockKeyword()) {
76
+ nodes.push(this.parseBlock());
77
+ continue;
78
+ }
79
+ nodes.push(...this.parseTextRun(insideBlock));
80
+ }
81
+ if (closingTag !== null) {
82
+ this.fail(`missing closing tag </${closingTag}>`);
83
+ }
84
+ if (insideBlock) {
85
+ this.fail("missing '}' to close the block");
86
+ }
87
+ return nodes;
88
+ }
89
+ parseTextRun(insideBlock) {
90
+ const nodes = [];
91
+ let textStart = this.position;
92
+ const flushText = (end) => {
93
+ const raw = this.source.slice(textStart, end);
94
+ if (raw.length > 0 && !(raw.trim() === '' && raw.includes('\n'))) {
95
+ nodes.push({ kind: 'text', text: this.decodeEntities(raw) });
96
+ }
97
+ };
98
+ while (!this.eof()) {
99
+ if (this.startsWith(INTERPOLATION_OPEN)) {
100
+ flushText(this.position);
101
+ nodes.push(this.parseInterpolation());
102
+ textStart = this.position;
103
+ continue;
104
+ }
105
+ if (this.startsWith('<') || this.atBlockKeyword() || (insideBlock && this.startsWith('}'))) {
106
+ break;
107
+ }
108
+ this.position += 1;
109
+ }
110
+ flushText(this.position);
111
+ return nodes;
112
+ }
113
+ parseInterpolation() {
114
+ this.expect(INTERPOLATION_OPEN);
115
+ const end = this.source.indexOf(INTERPOLATION_CLOSE, this.position);
116
+ if (end === -1) {
117
+ this.fail(`missing '${INTERPOLATION_CLOSE}' to close the interpolation`);
118
+ }
119
+ const expression = this.decodeEntities(this.source.slice(this.position, end)).trim();
120
+ if (expression.length === 0) {
121
+ this.fail('empty interpolation');
122
+ }
123
+ this.position = end + INTERPOLATION_CLOSE.length;
124
+ return { kind: 'interpolation', expression };
125
+ }
126
+ parseElement() {
127
+ this.expect('<');
128
+ const tag = this.matchPattern(TAG_NAME_PATTERN);
129
+ if (tag === null) {
130
+ this.fail('expected a tag name after <');
131
+ }
132
+ const attrs = {};
133
+ const bindings = {};
134
+ const events = {};
135
+ let slotTarget = null;
136
+ let selfClosed = false;
137
+ for (;;) {
138
+ this.skipWhitespace();
139
+ if (this.startsWith('/>')) {
140
+ this.position += 2;
141
+ selfClosed = true;
142
+ break;
143
+ }
144
+ if (this.startsWith('>')) {
145
+ this.position += 1;
146
+ break;
147
+ }
148
+ if (this.eof()) {
149
+ this.fail(`unterminated opening tag <${tag}>`);
150
+ }
151
+ if (this.startsWith('[')) {
152
+ const name = this.readDelimitedName('[', ']');
153
+ bindings[name] = this.readExpressionAttrValue(`[${name}]`);
154
+ }
155
+ else if (this.startsWith('(')) {
156
+ const name = this.readDelimitedName('(', ')');
157
+ events[name] = this.readExpressionAttrValue(`(${name})`);
158
+ }
159
+ else {
160
+ const name = this.matchPattern(ATTR_NAME_PATTERN);
161
+ if (name === null) {
162
+ this.fail(`unexpected character in <${tag}> attributes`);
163
+ }
164
+ const value = this.startsWith('=') ? this.readAttrValue(name) : '';
165
+ if (value.includes(INTERPOLATION_OPEN)) {
166
+ this.failOnInterpolatedAttribute(name);
167
+ }
168
+ if (name === 'slot') {
169
+ slotTarget = value;
170
+ }
171
+ else {
172
+ attrs[name] = value;
173
+ }
174
+ }
175
+ }
176
+ const hasChildren = !selfClosed && !VOID_ELEMENTS.has(tag);
177
+ const children = hasChildren ? this.parseChildren(tag, false) : [];
178
+ if (hasChildren) {
179
+ this.consumeClosingTag(tag);
180
+ }
181
+ if (tag === 'slot') {
182
+ const name = attrs['name'];
183
+ const slotNode = { kind: 'slot', name: name ?? null, fallback: children };
184
+ return slotNode;
185
+ }
186
+ const element = {
187
+ kind: 'element',
188
+ tag,
189
+ attrs,
190
+ bindings,
191
+ events,
192
+ children,
193
+ slotTarget,
194
+ };
195
+ return element;
196
+ }
197
+ consumeClosingTag(tag) {
198
+ this.expect('</');
199
+ const closing = this.matchPattern(TAG_NAME_PATTERN);
200
+ if (closing !== tag) {
201
+ this.fail(`expected </${tag}> but found </${closing ?? ''}>`);
202
+ }
203
+ this.skipWhitespace();
204
+ this.expect('>');
205
+ }
206
+ parseBlock() {
207
+ if (this.startsWith('@if')) {
208
+ return this.parseIf();
209
+ }
210
+ if (this.startsWith('@for')) {
211
+ return this.parseFor();
212
+ }
213
+ if (this.startsWith('@switch')) {
214
+ return this.parseSwitch();
215
+ }
216
+ this.fail('@else, @case and @default are only valid inside their parent block');
217
+ }
218
+ parseIf() {
219
+ this.expect('@if');
220
+ const branches = [
221
+ { condition: this.readParenGroup(), children: this.readBraceBlock() },
222
+ ];
223
+ for (;;) {
224
+ const beforeElse = this.position;
225
+ this.skipWhitespace();
226
+ if (!this.startsWith('@else')) {
227
+ this.position = beforeElse;
228
+ break;
229
+ }
230
+ this.position += '@else'.length;
231
+ this.skipWhitespace();
232
+ if (this.startsWith('if')) {
233
+ this.position += 'if'.length;
234
+ branches.push({ condition: this.readParenGroup(), children: this.readBraceBlock() });
235
+ }
236
+ else {
237
+ branches.push({ condition: null, children: this.readBraceBlock() });
238
+ break;
239
+ }
240
+ }
241
+ return { kind: 'if', branches };
242
+ }
243
+ parseFor() {
244
+ this.expect('@for');
245
+ const header = this.readParenGroup();
246
+ const match = FOR_HEADER_PATTERN.exec(header);
247
+ if (match === null) {
248
+ this.fail(`invalid @for header '(${header})': expected '@for (item of items(); track item.id)' — track is mandatory`);
249
+ }
250
+ const [, itemName, itemsExpression, trackExpression] = match;
251
+ if (itemName === undefined || itemsExpression === undefined || trackExpression === undefined) {
252
+ this.fail(`invalid @for header '(${header})'`);
253
+ }
254
+ if (RESERVED_LOOP_NAMES.has(itemName)) {
255
+ this.fail(`'${itemName}' is a reserved name and cannot be a loop variable`);
256
+ }
257
+ const children = this.readBraceBlock();
258
+ return { kind: 'for', itemName, itemsExpression, trackExpression, children };
259
+ }
260
+ parseSwitch() {
261
+ this.expect('@switch');
262
+ const expression = this.readParenGroup();
263
+ this.skipWhitespace();
264
+ this.expect('{');
265
+ const cases = [];
266
+ for (;;) {
267
+ this.skipWhitespace();
268
+ if (this.startsWith('}')) {
269
+ this.position += 1;
270
+ break;
271
+ }
272
+ if (this.startsWith('@case')) {
273
+ this.position += '@case'.length;
274
+ cases.push({ match: this.readParenGroup(), children: this.readBraceBlock() });
275
+ }
276
+ else if (this.startsWith('@default')) {
277
+ this.position += '@default'.length;
278
+ cases.push({ match: null, children: this.readBraceBlock() });
279
+ }
280
+ else {
281
+ this.fail('only @case and @default are allowed inside @switch');
282
+ }
283
+ }
284
+ return { kind: 'switch', expression, cases };
285
+ }
286
+ readBraceBlock() {
287
+ this.skipWhitespace();
288
+ this.expect('{');
289
+ const children = this.parseChildren(null, true);
290
+ this.expect('}');
291
+ return children;
292
+ }
293
+ readParenGroup() {
294
+ this.skipWhitespace();
295
+ this.expect('(');
296
+ const start = this.position;
297
+ let depth = 1;
298
+ while (!this.eof()) {
299
+ const char = this.source[this.position];
300
+ if (char === "'" || char === '"' || char === '`') {
301
+ this.skipStringLiteral(char);
302
+ continue;
303
+ }
304
+ if (char === '(') {
305
+ depth += 1;
306
+ }
307
+ else if (char === ')') {
308
+ depth -= 1;
309
+ if (depth === 0) {
310
+ const inner = this.source.slice(start, this.position).trim();
311
+ this.position += 1;
312
+ if (inner.length === 0) {
313
+ this.fail('empty parentheses');
314
+ }
315
+ return inner;
316
+ }
317
+ }
318
+ this.position += 1;
319
+ }
320
+ this.fail("missing ')'");
321
+ }
322
+ skipStringLiteral(quote) {
323
+ this.position += 1;
324
+ while (!this.eof() && this.source[this.position] !== quote) {
325
+ this.position += this.source[this.position] === '\\' ? 2 : 1;
326
+ }
327
+ if (this.eof()) {
328
+ this.fail('unterminated string literal');
329
+ }
330
+ this.position += 1;
331
+ }
332
+ readDelimitedName(open, close) {
333
+ this.expect(open);
334
+ const end = this.source.indexOf(close, this.position);
335
+ if (end === -1) {
336
+ this.fail(`missing '${close}'`);
337
+ }
338
+ const name = this.source.slice(this.position, end).trim();
339
+ if (name.length === 0) {
340
+ this.fail(`empty ${open}${close} attribute name`);
341
+ }
342
+ this.position = end + 1;
343
+ return name;
344
+ }
345
+ /**
346
+ * Une valeur d'attribut ordinaire est du texte : le moteur la pose telle
347
+ * quelle. Un `{{ }}` y passait donc mot pour mot, sans erreur — et le dégât
348
+ * dépassait le cosmétique : un `slot` interpolé projette vers un slot qui
349
+ * n'existe pas, donc le contenu disparaît de la page.
350
+ */
351
+ failOnInterpolatedAttribute(name) {
352
+ if (name === 'slot') {
353
+ this.fail('interpolation is not supported in slot: a slot name is chosen at compile time, ' +
354
+ 'so project a different element under @if instead');
355
+ }
356
+ this.fail(`interpolation is not supported in an attribute value: write [${name}]="…" to bind it`);
357
+ }
358
+ /** La valeur d'une liaison ou d'un événement est déjà une expression. */
359
+ readExpressionAttrValue(attrLabel) {
360
+ const value = this.readAttrValue(attrLabel);
361
+ if (value.includes(INTERPOLATION_OPEN)) {
362
+ this.fail(`${attrLabel} is already an expression: drop the ${INTERPOLATION_OPEN} ${INTERPOLATION_CLOSE}`);
363
+ }
364
+ return value;
365
+ }
366
+ readAttrValue(attrLabel) {
367
+ this.skipWhitespace();
368
+ this.expect('=');
369
+ this.skipWhitespace();
370
+ const quote = this.source[this.position];
371
+ if (quote !== '"' && quote !== "'") {
372
+ this.fail(`the value of ${attrLabel} must be quoted`);
373
+ }
374
+ this.position += 1;
375
+ const end = this.source.indexOf(quote, this.position);
376
+ if (end === -1) {
377
+ this.fail(`unterminated value for ${attrLabel}`);
378
+ }
379
+ const value = this.decodeEntities(this.source.slice(this.position, end));
380
+ this.position = end + 1;
381
+ return value;
382
+ }
383
+ skipComment() {
384
+ const end = this.source.indexOf('-->', this.position);
385
+ if (end === -1) {
386
+ this.fail('unterminated HTML comment');
387
+ }
388
+ this.position = end + 3;
389
+ }
390
+ atBlockKeyword() {
391
+ if (this.source[this.position] !== '@') {
392
+ return false;
393
+ }
394
+ return BLOCK_KEYWORD_PATTERN.test(this.source.slice(this.position));
395
+ }
396
+ matchPattern(pattern) {
397
+ const match = pattern.exec(this.source.slice(this.position));
398
+ if (match === null || match.index !== 0) {
399
+ return null;
400
+ }
401
+ this.position += match[0].length;
402
+ return match[0];
403
+ }
404
+ startsWith(prefix) {
405
+ return this.source.startsWith(prefix, this.position);
406
+ }
407
+ expect(expected) {
408
+ if (!this.startsWith(expected)) {
409
+ this.fail(`expected '${expected}'`);
410
+ }
411
+ this.position += expected.length;
412
+ }
413
+ skipWhitespace() {
414
+ while (!this.eof() && /\s/.test(this.source[this.position])) {
415
+ this.position += 1;
416
+ }
417
+ }
418
+ eof() {
419
+ return this.position >= this.source.length;
420
+ }
421
+ /** Rejoue les erreurs de décodage en erreur de parsing, pour la position. */
422
+ decodeEntities(raw) {
423
+ try {
424
+ return decodeHtmlEntities(raw);
425
+ }
426
+ catch (error) {
427
+ this.fail(error instanceof Error ? error.message : String(error));
428
+ }
429
+ }
430
+ fail(message) {
431
+ const consumed = this.source.slice(0, this.position);
432
+ const line = consumed.split('\n').length;
433
+ const lastNewline = consumed.lastIndexOf('\n');
434
+ const column = this.position - lastNewline;
435
+ throw new TemplateParseError(message, line, column);
436
+ }
437
+ }
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@cadence-app/compiler",
3
+ "version": "0.1.0",
4
+ "description": "Compilation des templates en instructions, SCSS scopé, bundling",
5
+ "keywords": [
6
+ "compiler",
7
+ "template",
8
+ "scss",
9
+ "bundler",
10
+ "mini program",
11
+ "cadence"
12
+ ],
13
+ "license": "MIT",
14
+ "author": "dickode78",
15
+ "homepage": "https://github.com/issadicko/cadence/tree/main/packages/compiler#readme",
16
+ "bugs": "https://github.com/issadicko/cadence/issues",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/issadicko/cadence.git",
20
+ "directory": "packages/compiler"
21
+ },
22
+ "type": "module",
23
+ "engines": {
24
+ "node": ">=20"
25
+ },
26
+ "main": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "default": "./dist/index.js"
32
+ },
33
+ "./template": {
34
+ "types": "./dist/template/parser.d.ts",
35
+ "default": "./dist/template/parser.js"
36
+ }
37
+ },
38
+ "bin": {
39
+ "mini": "./dist/cli.js"
40
+ },
41
+ "files": [
42
+ "dist"
43
+ ],
44
+ "publishConfig": {
45
+ "access": "public"
46
+ },
47
+ "dependencies": {
48
+ "@cadence-app/bridge": "0.1.0",
49
+ "@cadence-app/core": "0.1.0",
50
+ "@cadence-app/router": "0.1.0",
51
+ "@cadence-app/runtime": "0.1.0",
52
+ "@cadence-app/theme": "0.1.0",
53
+ "esbuild": "^0.25.0",
54
+ "jszip": "^3.10.1",
55
+ "postcss": "^8.5.1",
56
+ "postcss-selector-parser": "^7.0.0",
57
+ "sass": "^1.83.4"
58
+ },
59
+ "scripts": {
60
+ "build": "tsc --project tsconfig.build.json && node --disable-warning=ExperimentalWarning --experimental-transform-types ../../scripts/rewrite-dts-extensions.ts"
61
+ }
62
+ }