@valancex/mesh-compiler 0.7.0 → 0.9.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.
package/README.md CHANGED
@@ -32,6 +32,7 @@ The [Using MESH from JavaScript](https://github.com/ValanceX/Mesh/blob/main/docs
32
32
  - **`check(input)`** returns a promise of the diagnostics document, the one [`schemas/diagnostics-v1.schema.json`](https://github.com/ValanceX/Mesh/blob/main/schemas/diagnostics-v1.schema.json) describes. Anything wrong with your MPRX or your manifest is a diagnostic in it, never an exception. Diagnostics about the manifest carry the manifest's `path`. Each position has a `byte` offset, a 1-based `line` and `column`, and, for JavaScript, `utf16` (an offset into your string) and `utf16Column`.
33
33
  - **`compile(input)`** takes `check`'s input, with `model` required, and returns a promise of `{ diagnostics, template? }`. `diagnostics` is the document `check` returns for the same input. `template` is the `template-v1` document ([`schemas/template-v1.schema.json`](https://github.com/ValanceX/Mesh/blob/main/schemas/template-v1.schema.json)), present exactly when the diagnostics have no error; warnings don't stop it. A template is data to store and pass to the runtime, not to interpret. The `Template` type describes it.
34
34
  - **`checkProgram({ model, root, templates })`** checks a program of templates: the manifest's text, the root component, and the templates' texts in order. It returns a promise of the runtime diagnostics document ([`schemas/runtime-diagnostics-v1.schema.json`](https://github.com/ValanceX/Mesh/blob/main/schemas/runtime-diagnostics-v1.schema.json)), empty when the program is valid: exactly what `mesh check-program --format json` prints, and what the runtime's render reports for the same program. The `RuntimeDiagnosticsDocument` type describes it.
35
+ - **`compileProgram({ model, root, components })`** compiles a program's components (each `{ component, source, path }`, in order) against one manifest (`model: { manifest, path }`), checks the program they make with `checkProgram`, and returns a promise of `{ components, assembly?, program? }`. `components` holds each component's diagnostics document, as `compile` returns it; `assembly` is `checkProgram`'s document, present when no component had an error; `program` is `{ model, root, templates }`, the parts `checkProgram` and the runtime take (the templates as text), present when nothing had an error. A component that is not listed has no template, so it is a primitive. The `CompileProgramInput` and `CompileProgramResult` types describe it.
35
36
  - **`init(module)`** loads the WebAssembly module: a URL, its bytes, or a compiled `WebAssembly.Module`. In Node you don't need it; the package loads its own. In a browser, call it once before the first check, with the URL of `@valancex/mesh-compiler/mesh.wasm` as your setup serves it. Automatic loading by bundlers isn't part of this package's contract.
36
37
  - **`version`**: the package's version.
37
38
  - **`MeshVersionError`**: the WebAssembly module isn't this version's. No check runs against it.
package/dist/index.d.ts CHANGED
@@ -2,8 +2,9 @@
2
2
  * The MESH compiler, in WebAssembly: check MPRX against a component
3
3
  * manifest, and get back exactly the diagnostics `mesh check --format
4
4
  * json` prints for the same inputs; compile it, and get the template
5
- * `mesh compile` writes too; or check a program of templates, as
6
- * `mesh check-program` does.
5
+ * `mesh compile` writes too; check a program of templates, as `mesh
6
+ * check-program` does; or compile a whole program's components, and get
7
+ * the program's parts for the runtime.
7
8
  *
8
9
  * ```js
9
10
  * import { check } from "@valancex/mesh-compiler";
@@ -23,9 +24,11 @@
23
24
  */
24
25
  import type { CheckInput, CompileInput, CompileResult, ModuleSource, ProgramInput } from "./engine.js";
25
26
  import type { DiagnosticsDocument } from "./document.js";
27
+ import type { CompileProgramInput, CompileProgramResult } from "./program.js";
26
28
  import type { RuntimeDiagnosticsDocument } from "./runtime-document.js";
27
29
  export type { Diagnostic, DiagnosticsDocument, Position, Severity, Span, Suggestion, } from "./document.js";
28
30
  export type { CheckInput, CompileInput, CompileResult, ModuleSource, ProgramInput } from "./engine.js";
31
+ export type { CompileProgramInput, CompileProgramResult, ProgramComponent } from "./program.js";
29
32
  export type { ModelPosition, ModelSpan, RuntimeDiagnostic, RuntimeDiagnosticsDocument, RuntimeLocation, SourceOffset, SourceSpan, } from "./runtime-document.js";
30
33
  export type { Offset, Template, TemplateChild, TemplateElement, TemplateExpression, TemplateSpan, } from "./template.js";
31
34
  export { MeshInternalError, MeshVersionError } from "./engine.js";
@@ -63,6 +66,25 @@ export declare function compile(input: CompileInput): Promise<CompileResult>;
63
66
  * It produces nothing else. It rejects as `check` does.
64
67
  */
65
68
  export declare function checkProgram(input: ProgramInput): Promise<RuntimeDiagnosticsDocument>;
69
+ /**
70
+ * Compiles a program's components against one manifest, checks the program
71
+ * they make, and returns the program's parts: the input `checkProgram` and
72
+ * the runtime's `render` take, with the templates as text, so a host passes
73
+ * the result on as it is.
74
+ *
75
+ * Each of `input.components` is compiled as {@link compile} compiles it, and
76
+ * `components` in the result holds each one's diagnostics document, in
77
+ * order, whether it has errors or not (warnings don't stop a template). Only
78
+ * when none has an error are the templates checked as a program, by
79
+ * {@link checkProgram}, whose document is `assembly`. `program` is present
80
+ * exactly when neither found an error. This adds no rule of its own, and
81
+ * needs no module but the compiler's: in a browser, call {@link init} first,
82
+ * as for any check.
83
+ *
84
+ * It takes the sources it is given and finds none: a component that isn't
85
+ * listed has no template, and is a primitive. It rejects as `check` does.
86
+ */
87
+ export declare function compileProgram(input: CompileProgramInput): Promise<CompileProgramResult>;
66
88
  /**
67
89
  * Loads the WebAssembly module from `source`: a URL (or a string resolved
68
90
  * against the page), its bytes, or a compiled `WebAssembly.Module`, and
package/dist/index.js CHANGED
@@ -2,8 +2,9 @@
2
2
  * The MESH compiler, in WebAssembly: check MPRX against a component
3
3
  * manifest, and get back exactly the diagnostics `mesh check --format
4
4
  * json` prints for the same inputs; compile it, and get the template
5
- * `mesh compile` writes too; or check a program of templates, as
6
- * `mesh check-program` does.
5
+ * `mesh compile` writes too; check a program of templates, as `mesh
6
+ * check-program` does; or compile a whole program's components, and get
7
+ * the program's parts for the runtime.
7
8
  *
8
9
  * ```js
9
10
  * import { check } from "@valancex/mesh-compiler";
@@ -22,6 +23,7 @@
22
23
  * @packageDocumentation
23
24
  */
24
25
  import { check as checkWith, checkProgram as checkProgramWith, compile as compileWith, init as initWith, } from "./engine.js";
26
+ import { compileProgram as compileProgramWith } from "./program.js";
25
27
  export { MeshInternalError, MeshVersionError } from "./engine.js";
26
28
  export { version } from "./version.js";
27
29
  /**
@@ -63,6 +65,27 @@ export function compile(input) {
63
65
  export function checkProgram(input) {
64
66
  return checkProgramWith(input);
65
67
  }
68
+ /**
69
+ * Compiles a program's components against one manifest, checks the program
70
+ * they make, and returns the program's parts: the input `checkProgram` and
71
+ * the runtime's `render` take, with the templates as text, so a host passes
72
+ * the result on as it is.
73
+ *
74
+ * Each of `input.components` is compiled as {@link compile} compiles it, and
75
+ * `components` in the result holds each one's diagnostics document, in
76
+ * order, whether it has errors or not (warnings don't stop a template). Only
77
+ * when none has an error are the templates checked as a program, by
78
+ * {@link checkProgram}, whose document is `assembly`. `program` is present
79
+ * exactly when neither found an error. This adds no rule of its own, and
80
+ * needs no module but the compiler's: in a browser, call {@link init} first,
81
+ * as for any check.
82
+ *
83
+ * It takes the sources it is given and finds none: a component that isn't
84
+ * listed has no template, and is a primitive. It rejects as `check` does.
85
+ */
86
+ export function compileProgram(input) {
87
+ return compileProgramWith(input);
88
+ }
66
89
  /**
67
90
  * Loads the WebAssembly module from `source`: a URL (or a string resolved
68
91
  * against the page), its bytes, or a compiled `WebAssembly.Module`, and
package/dist/mesh.wasm CHANGED
Binary file
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Compiling a program: the components' templates, and the program's parts.
3
+ *
4
+ * A program has no document of its own (spec §9, "Programs"): a host gives the
5
+ * compiler's program check and the runtime its parts, the root's name and the
6
+ * templates' texts, with the manifest's text. This composes the operations
7
+ * that already exist, in the one order a host needs them: `compile` for each
8
+ * component, then `checkProgram` for the templates it made. It adds no rule
9
+ * and no check of its own, and it reads nothing but its input.
10
+ *
11
+ * Not API on its own: `compileProgram` is the package's.
12
+ */
13
+ import type { DiagnosticsDocument } from "./document.js";
14
+ import type { ProgramInput } from "./engine.js";
15
+ import type { RuntimeDiagnosticsDocument } from "./runtime-document.js";
16
+ /** One component's MPRX, to be compiled into the program as that component's template. */
17
+ export interface ProgramComponent {
18
+ /** The component whose template the source is. Always explicit. */
19
+ component: string;
20
+ /** The MPRX source text. */
21
+ source: string;
22
+ /** An opaque identifier for the source, used only to name it in diagnostics. Never read. */
23
+ path: string;
24
+ }
25
+ /** One program compile's inputs. */
26
+ export interface CompileProgramInput {
27
+ /** The model: the manifest every template is compiled against, which is also the program's own. */
28
+ model: {
29
+ /** The manifest's text. */
30
+ manifest: string;
31
+ /** An opaque identifier for the manifest, used only to name it in diagnostics. Never read. */
32
+ path: string;
33
+ };
34
+ /** The root component: the one the program renders. */
35
+ root: string;
36
+ /**
37
+ * The components that have a template in the program, in order. A component that is not listed has no template, and so is a primitive
38
+ * (spec §9, "Composite or primitive"): nothing here finds components, so a composite's template is listed or it is absent.
39
+ */
40
+ components: readonly ProgramComponent[];
41
+ }
42
+ /** What a program compile gives. */
43
+ export interface CompileProgramResult {
44
+ /** What `compile` returned for each component's source, in the order of the input's `components`: its diagnostics document. */
45
+ components: ReadonlyArray<{
46
+ component: string;
47
+ diagnostics: DiagnosticsDocument;
48
+ }>;
49
+ /**
50
+ * What `checkProgram` returned for the templates, as it would for the same parts. Present exactly when no component had an error, since only
51
+ * then are there templates to check.
52
+ */
53
+ assembly?: RuntimeDiagnosticsDocument;
54
+ /**
55
+ * The program, as `checkProgram` and the runtime take it: the model's text, the root, and the templates as text, in the order of the input's
56
+ * `components`. Present exactly when no component had an error and the program check found none.
57
+ */
58
+ program?: ProgramInput;
59
+ }
60
+ /** Compiles a program. See the package's `compileProgram`. */
61
+ export declare function compileProgram(input: CompileProgramInput): Promise<CompileProgramResult>;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Compiling a program: the components' templates, and the program's parts.
3
+ *
4
+ * A program has no document of its own (spec §9, "Programs"): a host gives the
5
+ * compiler's program check and the runtime its parts, the root's name and the
6
+ * templates' texts, with the manifest's text. This composes the operations
7
+ * that already exist, in the one order a host needs them: `compile` for each
8
+ * component, then `checkProgram` for the templates it made. It adds no rule
9
+ * and no check of its own, and it reads nothing but its input.
10
+ *
11
+ * Not API on its own: `compileProgram` is the package's.
12
+ */
13
+ import { checkProgram, compile } from "./engine.js";
14
+ function validate(input) {
15
+ if (typeof input !== "object" || input === null) {
16
+ throw new TypeError("compileProgram() takes an object: { model, root, components }");
17
+ }
18
+ const model = input.model;
19
+ if (typeof model !== "object" || model === null) {
20
+ throw new TypeError("model must be an object: { manifest, path }");
21
+ }
22
+ const strings = [
23
+ ["model.manifest", model.manifest],
24
+ ["model.path", model.path],
25
+ ["root", input.root],
26
+ ];
27
+ if (!Array.isArray(input.components)) {
28
+ throw new TypeError("components must be an array of { component, source, path }");
29
+ }
30
+ input.components.forEach((entry, index) => {
31
+ if (typeof entry !== "object" || entry === null) {
32
+ throw new TypeError(`components[${index}] must be an object: { component, source, path }`);
33
+ }
34
+ const written = entry;
35
+ strings.push([`components[${index}].component`, written["component"]], [`components[${index}].source`, written["source"]], [`components[${index}].path`, written["path"]]);
36
+ });
37
+ for (const [name, value] of strings) {
38
+ if (typeof value !== "string") {
39
+ throw new TypeError(`${name} must be a string`);
40
+ }
41
+ }
42
+ }
43
+ /** Compiles a program. See the package's `compileProgram`. */
44
+ export async function compileProgram(input) {
45
+ validate(input);
46
+ const { manifest, path } = input.model;
47
+ const compiled = [];
48
+ // Every component is compiled, so that one run reports every component's errors, not the first's.
49
+ for (const entry of input.components) {
50
+ compiled.push(await compile({ source: entry.source, path: entry.path, model: { manifest, path, component: entry.component } }));
51
+ }
52
+ const components = compiled.map((result, index) => ({
53
+ component: input.components[index].component,
54
+ diagnostics: result.diagnostics,
55
+ }));
56
+ const templates = [];
57
+ for (const result of compiled) {
58
+ if (result.template === undefined) {
59
+ return { components };
60
+ }
61
+ // The text the runtime and the program check take: the template as the compiler returned it, written out as JSON.
62
+ templates.push(JSON.stringify(result.template));
63
+ }
64
+ const parts = { model: manifest, root: input.root, templates };
65
+ const assembly = await checkProgram(parts);
66
+ return assembly.diagnostics.length === 0 ? { components, assembly, program: parts } : { components, assembly };
67
+ }
package/dist/version.d.ts CHANGED
@@ -3,4 +3,4 @@
3
3
  * (outline v0.4 I10). `crates/mesh-cli/tests/packages.rs` pins it to
4
4
  * `package.json` and to the Rust workspace.
5
5
  */
6
- export declare const version = "0.7.0";
6
+ export declare const version = "0.9.0";
package/dist/version.js CHANGED
@@ -3,4 +3,4 @@
3
3
  * (outline v0.4 I10). `crates/mesh-cli/tests/packages.rs` pins it to
4
4
  * `package.json` and to the Rust workspace.
5
5
  */
6
- export const version = "0.7.0";
6
+ export const version = "0.9.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valancex/mesh-compiler",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "The MESH compiler, built for WebAssembly: checks MPRX against a component manifest and returns its diagnostics.",
5
5
  "type": "module",
6
6
  "license": "MIT",