@openelement/compiler 0.0.0-stage → 1.0.0-alpha.12

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 (29) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +20 -2
  3. package/package.json +48 -4
  4. package/src/compiler.d.ts +8 -0
  5. package/src/compiler.js +8 -0
  6. package/src/internal/compiler/plugin.d.ts +95 -0
  7. package/src/internal/compiler/plugin.js +133 -0
  8. package/src/internal/compiler/semantic-core/analyze-module.js +479 -0
  9. package/src/internal/compiler/semantic-core/compile.d.ts +39 -0
  10. package/src/internal/compiler/semantic-core/compile.js +98 -0
  11. package/src/internal/compiler/semantic-core/compiler-diagnostics.d.ts +31 -0
  12. package/src/internal/compiler/semantic-core/compiler-diagnostics.js +50 -0
  13. package/src/internal/compiler/semantic-core/diagnostics/index.d.ts +54 -0
  14. package/src/internal/compiler/semantic-core/diagnostics/index.js +97 -0
  15. package/src/internal/compiler/semantic-core/emit-module.js +259 -0
  16. package/src/internal/compiler/semantic-core/emit-program.js +121 -0
  17. package/src/internal/compiler/semantic-core/lower-program.js +815 -0
  18. package/src/internal/compiler/semantic-core/module-analysis.d.ts +115 -0
  19. package/src/internal/compiler/semantic-core/module-analysis.js +263 -0
  20. package/src/internal/compiler/semantic-core/parse-module.js +37 -0
  21. package/src/internal/compiler/semantic-core/source-map.d.ts +53 -0
  22. package/src/internal/compiler/semantic-core/source-map.js +93 -0
  23. package/src/internal/compiler/semantic-core/style-imports.js +87 -0
  24. package/src/internal/compiler/semantic-core/type-check.d.ts +50 -0
  25. package/src/internal/compiler/semantic-core/type-check.js +106 -0
  26. package/src/internal/compiler/style-requests.d.ts +53 -0
  27. package/src/internal/compiler/style-requests.js +50 -0
  28. package/src/vite.d.ts +2 -0
  29. package/src/vite.js +2 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 openelement author
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,21 @@
1
- # Temporary Holding Version
1
+ # @openelement/compiler
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Build-time compiler for OpenElement: transforms TSX modules that opt in with a
4
+ canonically bound `@element(...)` decorator into Part Program modules (the
5
+ compiled element model, #1473). The only TypeScript-dependent package —
6
+ this subpath is host-side tooling and must never be reached from a browser
7
+ runtime graph.
8
+
9
+ ## Subpaths
10
+
11
+ | Subpath | Import it for |
12
+ | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
13
+ | `@openelement/compiler` | The semantic core tooling entry: `compileElementProgram` / `compileElementModule` (the one transform the Vite plugin and the build lanes share), `analyzeModuleSemantics`, diagnostics, the style-edge registry, and `stableModuleId`. |
14
+ | `@openelement/compiler/vite` | The standalone `element()` Vite plugin (`compiledElementPlugin`) — the `open:compiled-element` transform for hosts that compose their own Vite config. No Router, SSG or deployment imports. |
15
+
16
+ ## Boundary
17
+
18
+ Admission is binding-provenance based (#1209): a module is compiled only when
19
+ its `@element` decorator resolves to a runtime named import from
20
+ `@openelement/element`. Unsupported grammar fails closed with source-located
21
+ `OEC9xxx` diagnostics — there is no runtime fallback renderer.
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@openelement/compiler",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0-alpha.12",
4
+ "type": "module",
5
+ "main": "./src/compiler.js",
6
+ "types": "./src/compiler.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./src/compiler.d.ts",
10
+ "import": "./src/compiler.js",
11
+ "default": "./src/compiler.js"
12
+ },
13
+ "./vite": {
14
+ "types": "./src/vite.d.ts",
15
+ "import": "./src/vite.js",
16
+ "default": "./src/vite.js"
17
+ }
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/open-element/openelement.git"
22
+ },
23
+ "homepage": "https://openelement.org",
24
+ "bugs": "https://github.com/open-element/openelement/issues",
25
+ "license": "MIT",
26
+ "keywords": [
27
+ "openelement",
28
+ "web-components",
29
+ "compiler",
30
+ "tsx",
31
+ "vite",
32
+ "part-program"
33
+ ],
34
+ "engines": {
35
+ "node": ">=24.2"
36
+ },
37
+ "sideEffects": false,
38
+ "peerDependencies": {
39
+ "vite": "^8.0.0"
40
+ },
41
+ "peerDependenciesMeta": {
42
+ "vite": {
43
+ "optional": true
44
+ }
45
+ },
46
+ "dependencies": {
47
+ "@openelement/protocol": "1.0.0-alpha.12",
48
+ "typescript": "6.0.3"
49
+ }
50
+ }
@@ -0,0 +1,8 @@
1
+ import { ModuleSemanticFacts, ModuleSemanticsOptions, ModuleVocabularyDescriptor, SemanticCoreOptions, StaticSidecarDescriptor, analyzeModuleSemantics } from "./internal/compiler/semantic-core/module-analysis.js";
2
+ import { CompiledElementError, ElementCompilerDiagnostic } from "./internal/compiler/semantic-core/compiler-diagnostics.js";
3
+ import { CompileElementResult, compileElementProgram } from "./internal/compiler/semantic-core/compile.js";
4
+ import { COMPILED_ELEMENT_MARKER, compileElementModule, compiledElementPlugin, hasElementDecoratorApplication, isCompiledElementModule, stableModuleId, stripInlineSourceMapComment } from "./internal/compiler/plugin.js";
5
+ import { RegisteredStyleRequest, clearStyleRequests, getStyleRequest, hasStyleImporter, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId } from "./internal/compiler/style-requests.js";
6
+ import { EmittedModuleDiagnostic, EmittedModuleTypeCheckOptions, emittedModuleTypeChecks, typeCheckEmittedModule } from "./internal/compiler/semantic-core/type-check.js";
7
+ import { validatePartProgram } from "@openelement/protocol/part-program";
8
+ export { COMPILED_ELEMENT_MARKER, type CompileElementResult, CompiledElementError, type ElementCompilerDiagnostic, type EmittedModuleDiagnostic, type EmittedModuleTypeCheckOptions, type ModuleSemanticFacts, type ModuleSemanticsOptions, type ModuleVocabularyDescriptor, type RegisteredStyleRequest, type SemanticCoreOptions, type StaticSidecarDescriptor, analyzeModuleSemantics, clearStyleRequests, compileElementModule, compileElementProgram, compiledElementPlugin, emittedModuleTypeChecks, getStyleRequest, hasElementDecoratorApplication, hasStyleImporter, isCompiledElementModule, registerStyleRequest, stableModuleId, stripInlineSourceMapComment, styleEdgesForFile, styleRequestFile, styleRequestModuleId, typeCheckEmittedModule, validatePartProgram };
@@ -0,0 +1,8 @@
1
+ import { CompiledElementError } from "./internal/compiler/semantic-core/compiler-diagnostics.js";
2
+ import { analyzeModuleSemantics } from "./internal/compiler/semantic-core/module-analysis.js";
3
+ import { compileElementProgram } from "./internal/compiler/semantic-core/compile.js";
4
+ import { emittedModuleTypeChecks, typeCheckEmittedModule } from "./internal/compiler/semantic-core/type-check.js";
5
+ import { clearStyleRequests, getStyleRequest, hasStyleImporter, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId } from "./internal/compiler/style-requests.js";
6
+ import { COMPILED_ELEMENT_MARKER, compileElementModule, compiledElementPlugin, hasElementDecoratorApplication, isCompiledElementModule, stableModuleId, stripInlineSourceMapComment } from "./internal/compiler/plugin.js";
7
+ import { validatePartProgram } from "@openelement/protocol/part-program";
8
+ export { COMPILED_ELEMENT_MARKER, CompiledElementError, analyzeModuleSemantics, clearStyleRequests, compileElementModule, compileElementProgram, compiledElementPlugin, emittedModuleTypeChecks, getStyleRequest, hasElementDecoratorApplication, hasStyleImporter, isCompiledElementModule, registerStyleRequest, stableModuleId, stripInlineSourceMapComment, styleEdgesForFile, styleRequestFile, styleRequestModuleId, typeCheckEmittedModule, validatePartProgram };
@@ -0,0 +1,95 @@
1
+ import { SemanticCoreOptions, StaticSidecarDescriptor } from "./semantic-core/module-analysis.js";
2
+ import { CompileElementResult } from "./semantic-core/compile.js";
3
+ import { Plugin } from "vite";
4
+ //#region src/internal/compiler/plugin.d.ts
5
+ /** The authored substring every compiled element module contains (`@element(`), used as the cheap prefilter. */
6
+ export declare const COMPILED_ELEMENT_MARKER = "@element(";
7
+ /**
8
+ * Cheap first stage only — NOT a recognizer. The substring match exists to
9
+ * keep plain modules off the AST path and may false-positive (string literals
10
+ * and comments match); it may also false-negative on namespace-qualified
11
+ * spellings (`@ns.element(...)`), which are unsupported by the grammar anyway
12
+ * (#1209). Binding provenance and admission are decided exclusively by the
13
+ * semantic core.
14
+ */
15
+ export declare function isCompiledElementModule(code: string, id: string): boolean;
16
+ /**
17
+ * Precise second stage behind the substring prefilter: a canonically bound
18
+ * `@element(...)` decorator application on a class declaration (provenance
19
+ * decided by the semantic core's intrinsic-binding model). Modules that only
20
+ * mention the marker in a string literal or comment, or spell it through a
21
+ * foreign/local binding, do not reach the compiler.
22
+ */
23
+ export declare function hasElementDecoratorApplication(code: string, id: string): boolean;
24
+ /**
25
+ * Compile one opted-in module without binding the caller to Vite. The core
26
+ * adapter hook and the inline SSR/client builds all use this same function,
27
+ * which prevents duplicate compiler implementations from drifting. Returns
28
+ * null for modules without a canonically bound @element decorator
29
+ * application, so marker-mentioning and foreign-binding modules pass through
30
+ * untouched. Modules whose @element spelling carries unsupported or ambiguous
31
+ * provenance are compiled anyway so the compiler boundary fails closed with
32
+ * the OEC9027 provenance diagnostic. `options.staticSidecars` admits
33
+ * host-declared sidecar policy statements; the default core admits none.
34
+ */
35
+ export declare function compileElementModule(code: string, id: string, options?: SemanticCoreOptions): CompileElementResult | null;
36
+ /**
37
+ * Strip the inline map comment from a compiled module at the Vite boundary.
38
+ * The core artifact embeds its real Source Map v3 inline for standalone
39
+ * consumers; the Vite transform returns that same map as its `map` output so
40
+ * Vite composes it with the rest of the pipeline — leaving the comment in the
41
+ * served code would create a second, conflicting map story (#1210).
42
+ */
43
+ export declare function stripInlineSourceMapComment(code: string): string;
44
+ /**
45
+ * Compile-time source identity for a module: absolute Vite ids are converted
46
+ * to project-relative POSIX paths so Part Program `metadata.sourceFile` and
47
+ * `sourceMap.file` never embed the build machine's path. Module resolution,
48
+ * diagnostics, and HMR keys keep using the caller's own id.
49
+ *
50
+ * Anchors are explicit, never guessed from path substrings (a checkout living
51
+ * under e.g. `/srv/www/` must not fool the cut): the Vite project root first,
52
+ * then the workspace root when the caller knows it (a module outside the
53
+ * project root is typically a linked workspace package). Callers that hand
54
+ * the compiler a path outside every known root (synthetic ids, projects
55
+ * without a Vite root) get the id back unchanged — there is no
56
+ * correct relative form to invent, and the library boundary must not reject
57
+ * paths it cannot anchor. This module stays runtime-neutral: resolving a
58
+ * workspace root from disk is the caller's job.
59
+ */
60
+ export declare function stableModuleId(file: string, root: string | undefined, workspaceRoot?: string): string;
61
+ export interface CompiledElementPluginOptions {
62
+ /**
63
+ * Workspace root used as the second identity anchor: Vite ids outside the
64
+ * project root are typically linked workspace packages, and anchoring them
65
+ * on the workspace keeps machine paths out of emitted source maps. Callers
66
+ * that do not know a workspace root omit it and get ids passed through.
67
+ */
68
+ workspaceRoot?: string;
69
+ /**
70
+ * Type-check every module the compiler emits and fail the build when one
71
+ * does not compile (#1386 item 2). Off by default: it runs a TypeScript
72
+ * program per emitted module, which a dev-server transform must not pay.
73
+ * A build or verification pass turns it on, so the emitted program is
74
+ * checked against the declarations the consumer compiles against.
75
+ */
76
+ typeCheckEmitted?: boolean;
77
+ /** Specifier → candidate files map used when `typeCheckEmitted` is on. */
78
+ resolutionPaths?: Record<string, string[]>;
79
+ /**
80
+ * Static-sidecar descriptors the host application admits into the compiled
81
+ * module grammar (e.g. its island delivery policy factory). The default
82
+ * core knows none: without an injected descriptor a module carrying a
83
+ * sidecar policy statement fails closed with OEC9008.
84
+ */
85
+ staticSidecars?: readonly StaticSidecarDescriptor[];
86
+ }
87
+ /**
88
+ * The `open:compiled-element` Vite plugin: runs at `enforce: 'pre'` so the
89
+ * compiler sees authored TSX before Vite's own TS/JSX lowering, compiles every
90
+ * module with a canonically bound `@element` decorator, and leaves all other
91
+ * modules untouched. `workspaceRoot` anchors generated source-map ids for
92
+ * linked workspace packages.
93
+ */
94
+ export declare function compiledElementPlugin(options?: CompiledElementPluginOptions): Plugin;
95
+ //#endregion
@@ -0,0 +1,133 @@
1
+ import { diagnosticPluginError } from "./semantic-core/diagnostics/index.js";
2
+ import { CompiledElementError } from "./semantic-core/compiler-diagnostics.js";
3
+ import { analyzeModuleSemantics } from "./semantic-core/module-analysis.js";
4
+ import { compileElementProgram } from "./semantic-core/compile.js";
5
+ import { typeCheckEmittedModule } from "./semantic-core/type-check.js";
6
+ import { registerStyleRequest, styleRequestFile, styleRequestModuleId } from "./style-requests.js";
7
+ //#region src/internal/compiler/plugin.ts
8
+ /** The authored substring every compiled element module contains (`@element(`), used as the cheap prefilter. */
9
+ const COMPILED_ELEMENT_MARKER = "@element(";
10
+ /**
11
+ * Cheap first stage only — NOT a recognizer. The substring match exists to
12
+ * keep plain modules off the AST path and may false-positive (string literals
13
+ * and comments match); it may also false-negative on namespace-qualified
14
+ * spellings (`@ns.element(...)`), which are unsupported by the grammar anyway
15
+ * (#1209). Binding provenance and admission are decided exclusively by the
16
+ * semantic core.
17
+ */
18
+ function isCompiledElementModule(code, id) {
19
+ return /\.tsx(?:\?|$)/.test(id) && code.includes("@element(");
20
+ }
21
+ /**
22
+ * Precise second stage behind the substring prefilter: a canonically bound
23
+ * `@element(...)` decorator application on a class declaration (provenance
24
+ * decided by the semantic core's intrinsic-binding model). Modules that only
25
+ * mention the marker in a string literal or comment, or spell it through a
26
+ * foreign/local binding, do not reach the compiler.
27
+ */
28
+ function hasElementDecoratorApplication(code, id) {
29
+ return analyzeModuleSemantics(code, id).compiledElementDecorator;
30
+ }
31
+ /**
32
+ * Compile one opted-in module without binding the caller to Vite. The core
33
+ * adapter hook and the inline SSR/client builds all use this same function,
34
+ * which prevents duplicate compiler implementations from drifting. Returns
35
+ * null for modules without a canonically bound @element decorator
36
+ * application, so marker-mentioning and foreign-binding modules pass through
37
+ * untouched. Modules whose @element spelling carries unsupported or ambiguous
38
+ * provenance are compiled anyway so the compiler boundary fails closed with
39
+ * the OEC9027 provenance diagnostic. `options.staticSidecars` admits
40
+ * host-declared sidecar policy statements; the default core admits none.
41
+ */
42
+ function compileElementModule(code, id, options = {}) {
43
+ if (!isCompiledElementModule(code, id)) return null;
44
+ const facts = analyzeModuleSemantics(code, id);
45
+ if (!facts.compiledElementDecorator && facts.unsupportedElementDecorator === void 0) return null;
46
+ return compileElementProgram(code, id, options);
47
+ }
48
+ /**
49
+ * Strip the inline map comment from a compiled module at the Vite boundary.
50
+ * The core artifact embeds its real Source Map v3 inline for standalone
51
+ * consumers; the Vite transform returns that same map as its `map` output so
52
+ * Vite composes it with the rest of the pipeline — leaving the comment in the
53
+ * served code would create a second, conflicting map story (#1210).
54
+ */
55
+ function stripInlineSourceMapComment(code) {
56
+ return code.replace(/\n\/\/# sourceMappingURL=data:application\/json;base64,[^\n]*(?=\n?$)/, "");
57
+ }
58
+ /**
59
+ * Compile-time source identity for a module: absolute Vite ids are converted
60
+ * to project-relative POSIX paths so Part Program `metadata.sourceFile` and
61
+ * `sourceMap.file` never embed the build machine's path. Module resolution,
62
+ * diagnostics, and HMR keys keep using the caller's own id.
63
+ *
64
+ * Anchors are explicit, never guessed from path substrings (a checkout living
65
+ * under e.g. `/srv/www/` must not fool the cut): the Vite project root first,
66
+ * then the workspace root when the caller knows it (a module outside the
67
+ * project root is typically a linked workspace package). Callers that hand
68
+ * the compiler a path outside every known root (synthetic ids, projects
69
+ * without a Vite root) get the id back unchanged — there is no
70
+ * correct relative form to invent, and the library boundary must not reject
71
+ * paths it cannot anchor. This module stays runtime-neutral: resolving a
72
+ * workspace root from disk is the caller's job.
73
+ */
74
+ function stableModuleId(file, root, workspaceRoot) {
75
+ const clean = file.split("?", 1)[0];
76
+ if (!clean.startsWith("/")) return clean;
77
+ for (const base of [root, workspaceRoot]) {
78
+ if (!base) continue;
79
+ const prefix = base.endsWith("/") ? base : `${base}/`;
80
+ if (clean.startsWith(prefix)) return clean.slice(prefix.length);
81
+ }
82
+ return clean;
83
+ }
84
+ /**
85
+ * The `open:compiled-element` Vite plugin: runs at `enforce: 'pre'` so the
86
+ * compiler sees authored TSX before Vite's own TS/JSX lowering, compiles every
87
+ * module with a canonically bound `@element` decorator, and leaves all other
88
+ * modules untouched. `workspaceRoot` anchors generated source-map ids for
89
+ * linked workspace packages.
90
+ */
91
+ function compiledElementPlugin(options = {}) {
92
+ let viteRoot;
93
+ const workspaceRoot = options.workspaceRoot;
94
+ return {
95
+ name: "open:compiled-element",
96
+ configResolved(config) {
97
+ viteRoot = config.root;
98
+ },
99
+ enforce: "pre",
100
+ transform(code, id) {
101
+ try {
102
+ const moduleId = stableModuleId(id, viteRoot, workspaceRoot);
103
+ const compiled = compileElementModule(code, moduleId, { staticSidecars: options.staticSidecars });
104
+ if (!compiled) return null;
105
+ if (compiled.styleRequests !== void 0) for (const specifier of compiled.styleRequests) registerStyleRequest({
106
+ moduleId: styleRequestModuleId(id, specifier),
107
+ importer: id,
108
+ specifier,
109
+ file: styleRequestFile(id, specifier)
110
+ });
111
+ if (options.typeCheckEmitted) {
112
+ const diagnostics = typeCheckEmittedModule(compiled.code, moduleId, { paths: options.resolutionPaths });
113
+ const first = diagnostics[0];
114
+ if (first) this.error({
115
+ id,
116
+ loc: {
117
+ file: id,
118
+ line: first.line - 1,
119
+ column: first.character - 1
120
+ },
121
+ message: `[open:compiled-element] the compiler emitted a module that does not type-check (${diagnostics.length} diagnostic(s); TS${first.code}: ${first.message}). This is a compiler defect — report it with the authored module that triggered it.`
122
+ });
123
+ }
124
+ return compiled.code;
125
+ } catch (error) {
126
+ if (error instanceof CompiledElementError) this.error(diagnosticPluginError(error.diagnostics, code, id) ?? error.message);
127
+ throw error;
128
+ }
129
+ }
130
+ };
131
+ }
132
+ //#endregion
133
+ export { COMPILED_ELEMENT_MARKER, compileElementModule, compiledElementPlugin, hasElementDecoratorApplication, isCompiledElementModule, stableModuleId, stripInlineSourceMapComment };