@openelement/compiler 1.0.0-alpha.13 → 1.0.0-alpha.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 +3 -2
- package/src/compiler.d.ts +8 -4
- package/src/compiler.js +8 -4
- package/src/internal/compiler/compiled-artifact-class-candidates.d.ts +36 -0
- package/src/internal/compiler/compiled-artifact-class-candidates.js +60 -0
- package/src/internal/compiler/compiled-artifact-style-requests.d.ts +17 -0
- package/src/internal/compiler/compiled-artifact-style-requests.js +48 -2
- package/src/internal/compiler/plugin.d.ts +47 -9
- package/src/internal/compiler/plugin.js +102 -24
- package/src/internal/compiler/semantic-core/analyze-module.d.ts +117 -0
- package/src/internal/compiler/semantic-core/analyze-module.js +55 -10
- package/src/internal/compiler/semantic-core/compile.d.ts +10 -2
- package/src/internal/compiler/semantic-core/compile.js +6 -4
- package/src/internal/compiler/semantic-core/diagnostics/index.d.ts +21 -1
- package/src/internal/compiler/semantic-core/diagnostics/index.js +32 -1
- package/src/internal/compiler/semantic-core/emit-module.js +4 -0
- package/src/internal/compiler/semantic-core/emit-program.js +23 -3
- package/src/internal/compiler/semantic-core/lower-program.d.ts +169 -0
- package/src/internal/compiler/semantic-core/lower-program.js +168 -56
- package/src/internal/compiler/semantic-core/module-analysis.d.ts +55 -0
- package/src/internal/compiler/semantic-core/module-analysis.js +74 -4
- package/src/internal/compiler/semantic-core/server-component.d.ts +114 -0
- package/src/internal/compiler/semantic-core/server-component.js +481 -0
- package/src/internal/compiler/semantic-core/type-check.js +1 -1
- package/src/internal/compiler/style-requests.d.ts +43 -7
- package/src/internal/compiler/style-requests.js +70 -11
- package/src/internal/compiler/tailwind-candidates.d.ts +84 -0
- package/src/internal/compiler/tailwind-candidates.js +54 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openelement/compiler",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/compiler.js",
|
|
6
6
|
"types": "./src/compiler.d.ts",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"homepage": "https://openelement.org",
|
|
24
24
|
"bugs": "https://github.com/open-element/openelement/issues",
|
|
25
25
|
"license": "MIT",
|
|
26
|
+
"description": "Build-time compiler for OpenElement: transforms TSX with @element decorators into Part Program modules.",
|
|
26
27
|
"keywords": [
|
|
27
28
|
"openelement",
|
|
28
29
|
"web-components",
|
|
@@ -44,7 +45,7 @@
|
|
|
44
45
|
}
|
|
45
46
|
},
|
|
46
47
|
"dependencies": {
|
|
47
|
-
"@openelement/protocol": "1.0.0-alpha.
|
|
48
|
+
"@openelement/protocol": "1.0.0-alpha.15",
|
|
48
49
|
"typescript": "6.0.3"
|
|
49
50
|
}
|
|
50
51
|
}
|
package/src/compiler.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { ModuleSemanticFacts, ModuleSemanticsOptions, ModuleVocabularyDescriptor, SemanticCoreOptions, StaticSidecarDescriptor, analyzeModuleSemantics } from "./internal/compiler/semantic-core/module-analysis.js";
|
|
2
|
+
import { PluginDiagnosticError, clearCompilerDiagnostics, compilerDiagnosticsToEnvelopes, diagnosticPluginError, recordCompilerDiagnostics, takeCompilerDiagnostics } from "./internal/compiler/semantic-core/diagnostics/index.js";
|
|
2
3
|
import { CompiledElementError, ElementCompilerDiagnostic } from "./internal/compiler/semantic-core/compiler-diagnostics.js";
|
|
3
4
|
import { CompileElementResult, compileElementProgram } from "./internal/compiler/semantic-core/compile.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { ClassCandidateRegistry, createClassCandidateRegistry } from "./internal/compiler/tailwind-candidates.js";
|
|
6
|
+
import { compileElementModule, compiledElementPlugin, hasElementDecoratorApplication, isCompiledElementModule, registerStyleEdges, stableModuleId, stripInlineSourceMapComment } from "./internal/compiler/plugin.js";
|
|
7
|
+
import { compiledArtifactStyleRequests, readCompiledArtifactStyles } from "./internal/compiler/compiled-artifact-style-requests.js";
|
|
8
|
+
import { compiledArtifactClassCandidates } from "./internal/compiler/compiled-artifact-class-candidates.js";
|
|
9
|
+
import { CompileServerComponentResult, ServerComponentCompileOptions, compileServerComponentIfAdmitted, compileServerComponentModule } from "./internal/compiler/semantic-core/server-component.js";
|
|
10
|
+
import { RegisteredStyleRequest, clearStyleRequests, getStyleRequest, hasStyleImporter, lightRootScopeTag, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId, styleRequestsForImporter } from "./internal/compiler/style-requests.js";
|
|
7
11
|
import { EmittedModuleDiagnostic, EmittedModuleTypeCheckOptions, emittedModuleTypeChecks, typeCheckEmittedModule } from "./internal/compiler/semantic-core/type-check.js";
|
|
8
12
|
import { validatePartProgram } from "@openelement/protocol/part-program";
|
|
9
|
-
export {
|
|
13
|
+
export { type ClassCandidateRegistry, type CompileElementResult, type CompileServerComponentResult, CompiledElementError, type ElementCompilerDiagnostic, type EmittedModuleDiagnostic, type EmittedModuleTypeCheckOptions, type ModuleSemanticFacts, type ModuleSemanticsOptions, type ModuleVocabularyDescriptor, type PluginDiagnosticError, type RegisteredStyleRequest, type SemanticCoreOptions, type ServerComponentCompileOptions, type StaticSidecarDescriptor, analyzeModuleSemantics, clearCompilerDiagnostics, clearStyleRequests, compileElementModule, compileElementProgram, compileServerComponentIfAdmitted, compileServerComponentModule, compiledArtifactClassCandidates, compiledArtifactStyleRequests, compiledElementPlugin, compilerDiagnosticsToEnvelopes, createClassCandidateRegistry, diagnosticPluginError, emittedModuleTypeChecks, getStyleRequest, hasElementDecoratorApplication, hasStyleImporter, isCompiledElementModule, lightRootScopeTag, readCompiledArtifactStyles, recordCompilerDiagnostics, registerStyleEdges, registerStyleRequest, stableModuleId, stripInlineSourceMapComment, styleEdgesForFile, styleRequestFile, styleRequestModuleId, styleRequestsForImporter, takeCompilerDiagnostics, typeCheckEmittedModule, validatePartProgram };
|
package/src/compiler.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { analyzeModuleSemantics } from "./internal/compiler/semantic-core/module-analysis.js";
|
|
2
|
+
import { clearCompilerDiagnostics, compilerDiagnosticsToEnvelopes, diagnosticPluginError, recordCompilerDiagnostics, takeCompilerDiagnostics } from "./internal/compiler/semantic-core/diagnostics/index.js";
|
|
2
3
|
import { CompiledElementError } from "./internal/compiler/semantic-core/compiler-diagnostics.js";
|
|
3
4
|
import { compileElementProgram } from "./internal/compiler/semantic-core/compile.js";
|
|
5
|
+
import { compileServerComponentIfAdmitted, compileServerComponentModule } from "./internal/compiler/semantic-core/server-component.js";
|
|
4
6
|
import { emittedModuleTypeChecks, typeCheckEmittedModule } from "./internal/compiler/semantic-core/type-check.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
7
|
+
import { compiledArtifactClassCandidates } from "./internal/compiler/compiled-artifact-class-candidates.js";
|
|
8
|
+
import { compiledArtifactStyleRequests, readCompiledArtifactStyles } from "./internal/compiler/compiled-artifact-style-requests.js";
|
|
9
|
+
import { clearStyleRequests, getStyleRequest, hasStyleImporter, lightRootScopeTag, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId, styleRequestsForImporter } from "./internal/compiler/style-requests.js";
|
|
10
|
+
import { compileElementModule, compiledElementPlugin, hasElementDecoratorApplication, isCompiledElementModule, registerStyleEdges, stableModuleId, stripInlineSourceMapComment } from "./internal/compiler/plugin.js";
|
|
11
|
+
import { createClassCandidateRegistry } from "./internal/compiler/tailwind-candidates.js";
|
|
8
12
|
import { validatePartProgram } from "@openelement/protocol/part-program";
|
|
9
|
-
export {
|
|
13
|
+
export { CompiledElementError, analyzeModuleSemantics, clearCompilerDiagnostics, clearStyleRequests, compileElementModule, compileElementProgram, compileServerComponentIfAdmitted, compileServerComponentModule, compiledArtifactClassCandidates, compiledArtifactStyleRequests, compiledElementPlugin, compilerDiagnosticsToEnvelopes, createClassCandidateRegistry, diagnosticPluginError, emittedModuleTypeChecks, getStyleRequest, hasElementDecoratorApplication, hasStyleImporter, isCompiledElementModule, lightRootScopeTag, readCompiledArtifactStyles, recordCompilerDiagnostics, registerStyleEdges, registerStyleRequest, stableModuleId, stripInlineSourceMapComment, styleEdgesForFile, styleRequestFile, styleRequestModuleId, styleRequestsForImporter, takeCompilerDiagnostics, typeCheckEmittedModule, validatePartProgram };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/internal/compiler/compiled-artifact-class-candidates.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The class-candidate facts of an already-compiled artifact module (alpha15 S
|
|
4
|
+
* lane, S5-B).
|
|
5
|
+
*
|
|
6
|
+
* Packages ship the compiler's OWN emitted modules. A consumer build that
|
|
7
|
+
* resolves one has no authored grammar left to compile, yet the module's
|
|
8
|
+
* statically admitted class tokens are exactly the fact the component
|
|
9
|
+
* stylesheet needs — they were proven when the package was compiled, and
|
|
10
|
+
* re-deriving them from the shipped markup would mean a second parser. The
|
|
11
|
+
* compiler therefore emits them as an explicit static
|
|
12
|
+
* (`static __oeClassCandidates: readonly string[] = [...]`, alongside the
|
|
13
|
+
* `__oeUtilityClasses` flag it has always emitted for shadow-root
|
|
14
|
+
* components), and this module recovers them from the artifact text — the
|
|
15
|
+
* same second-input shape as the style-edge recovery
|
|
16
|
+
* (compiled-artifact-style-requests.ts).
|
|
17
|
+
*
|
|
18
|
+
* All-or-nothing per module: a candidates member that is not an array of
|
|
19
|
+
* string literals is not adopted, so a module outside the emitted shape keeps
|
|
20
|
+
* riding the graph's normal channels exactly as before. Never throws, never
|
|
21
|
+
* guesses: an artifact without the member contributes nothing.
|
|
22
|
+
*
|
|
23
|
+
* Root scope is structural: the compiler emits the member ONLY for
|
|
24
|
+
* shadow-root components with admitted candidates (light-root candidates never
|
|
25
|
+
* need the shadow sheet), so a recovered member is by construction a
|
|
26
|
+
* shadow-root fact.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* The admitted class candidates of one compiled artifact module, or undefined
|
|
30
|
+
* when the module does not carry the emitted fact. Tolerates both artifact
|
|
31
|
+
* forms the pack pipeline produces (the compiler's raw TypeScript emission and
|
|
32
|
+
* the packed, type-stripped module) — the parse is ScriptKind.TSX for exactly
|
|
33
|
+
* the reason the style-edge recovery documents.
|
|
34
|
+
*/
|
|
35
|
+
export declare function compiledArtifactClassCandidates(source: string, fileName: string): readonly string[] | undefined;
|
|
36
|
+
//#endregion
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { typescriptParser } from "./semantic-core/parse-module.js";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
//#region src/internal/compiler/compiled-artifact-class-candidates.ts
|
|
4
|
+
/**
|
|
5
|
+
* The class-candidate facts of an already-compiled artifact module (alpha15 S
|
|
6
|
+
* lane, S5-B).
|
|
7
|
+
*
|
|
8
|
+
* Packages ship the compiler's OWN emitted modules. A consumer build that
|
|
9
|
+
* resolves one has no authored grammar left to compile, yet the module's
|
|
10
|
+
* statically admitted class tokens are exactly the fact the component
|
|
11
|
+
* stylesheet needs — they were proven when the package was compiled, and
|
|
12
|
+
* re-deriving them from the shipped markup would mean a second parser. The
|
|
13
|
+
* compiler therefore emits them as an explicit static
|
|
14
|
+
* (`static __oeClassCandidates: readonly string[] = [...]`, alongside the
|
|
15
|
+
* `__oeUtilityClasses` flag it has always emitted for shadow-root
|
|
16
|
+
* components), and this module recovers them from the artifact text — the
|
|
17
|
+
* same second-input shape as the style-edge recovery
|
|
18
|
+
* (compiled-artifact-style-requests.ts).
|
|
19
|
+
*
|
|
20
|
+
* All-or-nothing per module: a candidates member that is not an array of
|
|
21
|
+
* string literals is not adopted, so a module outside the emitted shape keeps
|
|
22
|
+
* riding the graph's normal channels exactly as before. Never throws, never
|
|
23
|
+
* guesses: an artifact without the member contributes nothing.
|
|
24
|
+
*
|
|
25
|
+
* Root scope is structural: the compiler emits the member ONLY for
|
|
26
|
+
* shadow-root components with admitted candidates (light-root candidates never
|
|
27
|
+
* need the shadow sheet), so a recovered member is by construction a
|
|
28
|
+
* shadow-root fact.
|
|
29
|
+
*/
|
|
30
|
+
/** The emitted static's member name (the cheap prefilter and the lookup key). */
|
|
31
|
+
const CANDIDATE_MEMBER_NAME = "__oeClassCandidates";
|
|
32
|
+
/**
|
|
33
|
+
* The admitted class candidates of one compiled artifact module, or undefined
|
|
34
|
+
* when the module does not carry the emitted fact. Tolerates both artifact
|
|
35
|
+
* forms the pack pipeline produces (the compiler's raw TypeScript emission and
|
|
36
|
+
* the packed, type-stripped module) — the parse is ScriptKind.TSX for exactly
|
|
37
|
+
* the reason the style-edge recovery documents.
|
|
38
|
+
*/
|
|
39
|
+
function compiledArtifactClassCandidates(source, fileName) {
|
|
40
|
+
if (!source.includes(CANDIDATE_MEMBER_NAME)) return void 0;
|
|
41
|
+
const { sourceFile: sf } = typescriptParser.parseModule(source, fileName);
|
|
42
|
+
let found;
|
|
43
|
+
const visit = (node) => {
|
|
44
|
+
if (found !== void 0) return;
|
|
45
|
+
if (ts.isPropertyDeclaration(node) && node.name !== void 0 && ts.isIdentifier(node.name) && node.name.text === CANDIDATE_MEMBER_NAME && node.initializer !== void 0 && ts.isArrayLiteralExpression(node.initializer)) {
|
|
46
|
+
const tokens = [];
|
|
47
|
+
for (const element of node.initializer.elements) {
|
|
48
|
+
if (!ts.isStringLiteral(element)) return;
|
|
49
|
+
tokens.push(element.text);
|
|
50
|
+
}
|
|
51
|
+
found = tokens.length === 0 ? void 0 : tokens;
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
ts.forEachChild(node, visit);
|
|
55
|
+
};
|
|
56
|
+
ts.forEachChild(sf, visit);
|
|
57
|
+
return found;
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
export { compiledArtifactClassCandidates };
|
|
@@ -30,6 +30,15 @@
|
|
|
30
30
|
* compiled-element transform's host binding) registers them, exactly as it
|
|
31
31
|
* registers an authored compile's `styleRequests`.
|
|
32
32
|
*/
|
|
33
|
+
/**
|
|
34
|
+
* What one compiled artifact module declares about its style edges: the
|
|
35
|
+
* relative `.css` specifiers in authored order, and the light-root scope tag
|
|
36
|
+
* the module's Part Program names (undefined for a shadow-root artifact).
|
|
37
|
+
*/
|
|
38
|
+
export interface CompiledArtifactStyleRead {
|
|
39
|
+
readonly requests: readonly string[];
|
|
40
|
+
readonly scopeTag?: string;
|
|
41
|
+
}
|
|
33
42
|
/**
|
|
34
43
|
* The `.css` style edges of one compiled artifact module, in authored order —
|
|
35
44
|
* or undefined when the module is not a shape this recovery adopts. Never
|
|
@@ -47,4 +56,12 @@
|
|
|
47
56
|
* extension would refuse the raw form and silently drop its edges.
|
|
48
57
|
*/
|
|
49
58
|
export declare function compiledArtifactStyleRequests(source: string, fileName: string): readonly string[] | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* The same recovery, read once for both facts an artifact states: its edges
|
|
61
|
+
* and its root scope (alpha15 S lane, S-C1.2). The scope comes from the
|
|
62
|
+
* module's own Part Program literal — the emitter writes it verbatim, so the
|
|
63
|
+
* packed form needs no second source of truth and no assumption about which
|
|
64
|
+
* root kind a shipped package used.
|
|
65
|
+
*/
|
|
66
|
+
export declare function readCompiledArtifactStyles(source: string, fileName: string): CompiledArtifactStyleRead | undefined;
|
|
50
67
|
//#endregion
|
|
@@ -86,6 +86,16 @@ function collectSheetBindings(sf) {
|
|
|
86
86
|
* extension would refuse the raw form and silently drop its edges.
|
|
87
87
|
*/
|
|
88
88
|
function compiledArtifactStyleRequests(source, fileName) {
|
|
89
|
+
return readCompiledArtifactStyles(source, fileName)?.requests;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The same recovery, read once for both facts an artifact states: its edges
|
|
93
|
+
* and its root scope (alpha15 S lane, S-C1.2). The scope comes from the
|
|
94
|
+
* module's own Part Program literal — the emitter writes it verbatim, so the
|
|
95
|
+
* packed form needs no second source of truth and no assumption about which
|
|
96
|
+
* root kind a shipped package used.
|
|
97
|
+
*/
|
|
98
|
+
function readCompiledArtifactStyles(source, fileName) {
|
|
89
99
|
if (!source.includes(COMPILED_ARTIFACT_MARKER)) return void 0;
|
|
90
100
|
if (!CSS_SPECIFIER_HINT.test(source)) return void 0;
|
|
91
101
|
const { sourceFile: sf } = typescriptParser.parseModule(source, fileName);
|
|
@@ -104,7 +114,9 @@ function compiledArtifactStyleRequests(source, fileName) {
|
|
|
104
114
|
return true;
|
|
105
115
|
};
|
|
106
116
|
let shapeFollowed = true;
|
|
117
|
+
let program;
|
|
107
118
|
const visit = (node) => {
|
|
119
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.name.text === PART_PROGRAM_BINDING && node.initializer !== void 0) program = node.initializer;
|
|
108
120
|
if (ts.isClassDeclaration(node) || ts.isClassExpression(node)) for (const member of node.members) {
|
|
109
121
|
if (!ts.isPropertyDeclaration(member)) continue;
|
|
110
122
|
if (!ts.isIdentifier(member.name) || member.name.text !== STYLES_MEMBER_NAME) continue;
|
|
@@ -123,7 +135,41 @@ function compiledArtifactStyleRequests(source, fileName) {
|
|
|
123
135
|
};
|
|
124
136
|
visit(sf);
|
|
125
137
|
if (!shapeFollowed || claimed.size !== sheetBindings.size) return void 0;
|
|
126
|
-
|
|
138
|
+
if (edges.length === 0) return void 0;
|
|
139
|
+
const scopeTag = programScopeTag(program);
|
|
140
|
+
return scopeTag === void 0 ? { requests: edges } : {
|
|
141
|
+
requests: edges,
|
|
142
|
+
scopeTag
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/** The Part Program's module-local binding, as the emitter writes it. */
|
|
146
|
+
const PART_PROGRAM_BINDING = "__partProgram";
|
|
147
|
+
/**
|
|
148
|
+
* The light-root scope tag inside an artifact's Part Program literal. Only a
|
|
149
|
+
* statically visible `tag` string with `root.kind === "light"` counts; every
|
|
150
|
+
* other shape (a computed literal, a shadow root, a missing field) reads as
|
|
151
|
+
* "no scope", which is the shadow-root default.
|
|
152
|
+
*/
|
|
153
|
+
function programScopeTag(program) {
|
|
154
|
+
if (program === void 0) return void 0;
|
|
155
|
+
let expression = program;
|
|
156
|
+
while (ts.isParenthesizedExpression(expression) || ts.isAsExpression(expression)) expression = expression.expression;
|
|
157
|
+
if (!ts.isObjectLiteralExpression(expression)) return void 0;
|
|
158
|
+
const tag = propertyOf(expression, "tag");
|
|
159
|
+
const root = propertyOf(expression, "root");
|
|
160
|
+
if (tag === void 0 || !ts.isStringLiteral(tag) || tag.text === "") return void 0;
|
|
161
|
+
if (root === void 0 || !ts.isObjectLiteralExpression(root)) return void 0;
|
|
162
|
+
const kind = propertyOf(root, "kind");
|
|
163
|
+
if (kind === void 0 || !ts.isStringLiteral(kind) || kind.text !== "light") return void 0;
|
|
164
|
+
return tag.text;
|
|
165
|
+
}
|
|
166
|
+
/** One property's initializer of an object literal, or undefined. */
|
|
167
|
+
function propertyOf(object, name) {
|
|
168
|
+
for (const property of object.properties) {
|
|
169
|
+
if (!ts.isPropertyAssignment(property)) continue;
|
|
170
|
+
if (ts.isIdentifier(property.name) && property.name.text === name) return property.initializer;
|
|
171
|
+
if (ts.isStringLiteral(property.name) && property.name.text === name) return property.initializer;
|
|
172
|
+
}
|
|
127
173
|
}
|
|
128
174
|
//#endregion
|
|
129
|
-
export { compiledArtifactStyleRequests };
|
|
175
|
+
export { compiledArtifactStyleRequests, readCompiledArtifactStyles };
|
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
import { SemanticCoreOptions, StaticSidecarDescriptor } from "./semantic-core/module-analysis.js";
|
|
2
2
|
import { CompileElementResult } from "./semantic-core/compile.js";
|
|
3
|
+
import { ClassCandidateRegistry } from "./tailwind-candidates.js";
|
|
3
4
|
import { Plugin } from "vite";
|
|
4
5
|
//#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
6
|
/**
|
|
8
|
-
* Cheap first stage only — NOT a recognizer. The
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* (#1209). Binding provenance and admission are decided exclusively by the
|
|
13
|
-
* semantic core.
|
|
7
|
+
* Cheap first stage only — NOT a recognizer. The match keeps plain modules off
|
|
8
|
+
* the AST path and may false-positive (any '@' — inside a string literal,
|
|
9
|
+
* comment, or import specifier — matches); binding provenance and admission
|
|
10
|
+
* are decided exclusively by the semantic core.
|
|
14
11
|
*/
|
|
15
12
|
export declare function isCompiledElementModule(code: string, id: string): boolean;
|
|
16
13
|
/**
|
|
17
|
-
* Precise second stage behind the
|
|
14
|
+
* Precise second stage behind the prefilter: a canonically bound
|
|
18
15
|
* `@element(...)` decorator application on a class declaration (provenance
|
|
19
16
|
* decided by the semantic core's intrinsic-binding model). Modules that only
|
|
20
17
|
* mention the marker in a string literal or comment, or spell it through a
|
|
@@ -83,7 +80,48 @@ export interface CompiledElementPluginOptions {
|
|
|
83
80
|
* sidecar policy statement fails closed with OEC9008.
|
|
84
81
|
*/
|
|
85
82
|
staticSidecars?: readonly StaticSidecarDescriptor[];
|
|
83
|
+
/**
|
|
84
|
+
* The class-candidate registry this plugin instance writes its compiled
|
|
85
|
+
* modules' statically admitted class tokens into (alpha15 S lane). The
|
|
86
|
+
* registry is OWNED by the host that created it — one per build, one per dev
|
|
87
|
+
* server — so two live builds in one process never inherit each other's
|
|
88
|
+
* facts (defect C). Omitted: this instance collects no candidates, which is
|
|
89
|
+
* the right shape for a build that consumes none (e.g. the SSR half).
|
|
90
|
+
*/
|
|
91
|
+
candidates?: ClassCandidateRegistry;
|
|
92
|
+
/**
|
|
93
|
+
* Host-supplied resolver for the server function component lane's nested
|
|
94
|
+
* imports (`resolveModule` of {@link compileServerComponentModule}): given
|
|
95
|
+
* a specifier and the importing module id, return that module's SOURCE so
|
|
96
|
+
* a nested server function component composes by direct call. Omitting it
|
|
97
|
+
* (or answering undefined) keeps nested composition a coded rejection.
|
|
98
|
+
* The compiler itself never reads the filesystem — resolution stays the
|
|
99
|
+
* host's job, so this option carries the host's capability rather than a
|
|
100
|
+
* path convention.
|
|
101
|
+
*/
|
|
102
|
+
resolveServerComponentModule?: (specifier: string, importer: string) => string | null;
|
|
103
|
+
/**
|
|
104
|
+
* The custom-element host tags the host build admitted as @element compiled
|
|
105
|
+
* components: JSX children in a server function component ride
|
|
106
|
+
* `__ssr(tag, props)` for those tags and an emitted-as-is tag otherwise.
|
|
107
|
+
*/
|
|
108
|
+
compiledTags?: readonly string[];
|
|
86
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Register one module's style edges (the registry's one write path, S-C1.4).
|
|
112
|
+
*
|
|
113
|
+
* The importing component's root scope is part of every edge it registers
|
|
114
|
+
* (alpha15 S lane, S-C1.2): a light-root component's sheets leave the build
|
|
115
|
+
* wrapped in `@scope (<tag>)`, so the tag travels to the intercept through the
|
|
116
|
+
* registry key. A shipped artifact states the same fact in its Part Program
|
|
117
|
+
* literal.
|
|
118
|
+
*
|
|
119
|
+
* The Vite transform is the caller for a module the bundler processes; a build
|
|
120
|
+
* host that admits a module for its ADMISSION FACTS alone (a Page or Layout the
|
|
121
|
+
* server renders and no island imports) calls the same step with the same
|
|
122
|
+
* compiled program — no second writer, no second registry.
|
|
123
|
+
*/
|
|
124
|
+
export declare function registerStyleEdges(id: string, specifiers: readonly string[], scopeTag: string | undefined): void;
|
|
87
125
|
/**
|
|
88
126
|
* The `open:compiled-element` Vite plugin: runs at `enforce: 'pre'` so the
|
|
89
127
|
* compiler sees authored TSX before Vite's own TS/JSX lowering, compiles every
|
|
@@ -1,26 +1,52 @@
|
|
|
1
1
|
import { analyzeModuleSemantics } from "./semantic-core/module-analysis.js";
|
|
2
|
-
import { diagnosticPluginError } from "./semantic-core/diagnostics/index.js";
|
|
2
|
+
import { diagnosticPluginError, recordCompilerDiagnostics } from "./semantic-core/diagnostics/index.js";
|
|
3
3
|
import { CompiledElementError } from "./semantic-core/compiler-diagnostics.js";
|
|
4
4
|
import { compileElementProgram } from "./semantic-core/compile.js";
|
|
5
|
+
import { compileServerComponentIfAdmitted } from "./semantic-core/server-component.js";
|
|
5
6
|
import { typeCheckEmittedModule } from "./semantic-core/type-check.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
7
|
+
import { compiledArtifactClassCandidates } from "./compiled-artifact-class-candidates.js";
|
|
8
|
+
import { readCompiledArtifactStyles } from "./compiled-artifact-style-requests.js";
|
|
9
|
+
import { lightRootScopeTag, registerStyleRequest, styleRequestFile, styleRequestModuleId } from "./style-requests.js";
|
|
8
10
|
//#region src/internal/compiler/plugin.ts
|
|
9
|
-
/** The authored substring every compiled element module contains (`@element(`), used as the cheap prefilter. */
|
|
10
|
-
const COMPILED_ELEMENT_MARKER = "@element(";
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
12
|
+
* The cheap first stage — NOT a recognizer.
|
|
13
|
+
*
|
|
14
|
+
* It answers one question: could this module be a compiled element module? The
|
|
15
|
+
* one grammar-independent fact every authored form shares is the '@' character
|
|
16
|
+
* of its decorator application. Every spelling of the authored form contains
|
|
17
|
+
* it — `@element(...)`, an aliased binding (`import { element as
|
|
18
|
+
* defineElement }` with `@defineElement(...)`), a Unicode alias
|
|
19
|
+
* (`import { element as 元件 }` with `@元件(...)`) — and so does every spelling
|
|
20
|
+
* the semantic layer REJECTS but must still see in order to fail closed with
|
|
21
|
+
* its provenance diagnostic (the namespace form, OEC9027). JavaScript lexing
|
|
22
|
+
* makes any finer shape un-modelable in a prefilter: identifiers are not ASCII
|
|
23
|
+
* (Unicode aliases are legal), and the lexer skips comments, so a block comment
|
|
24
|
+
* between the decorator name and its argument list is a legal decorator
|
|
25
|
+
* application no identifier-shaped regex admits.
|
|
26
|
+
*
|
|
27
|
+
* The stages this replaced modeled the decorator-application SHAPE — a literal
|
|
28
|
+
* `@element(` substring, then an ASCII-identifier-plus-whitespace regex — and
|
|
29
|
+
* therefore answered "does this module spell the decorator this way?" instead
|
|
30
|
+
* of "could this module be a compiled element module?", narrowing the CONTRACT
|
|
31
|
+
* rather than the AST path: legal spellings the semantic core admits never
|
|
32
|
+
* reached the layer that admits them. Admission has exactly one authority; a
|
|
33
|
+
* prefilter may only decide whether a module is worth parsing. A false positive
|
|
34
|
+
* here (any '@' in a string literal, comment, or import specifier) costs one
|
|
35
|
+
* parse and nothing else; a false negative hides a legal module from its only
|
|
36
|
+
* admission authority.
|
|
37
|
+
*/
|
|
38
|
+
const SUPPORTED_MODULE_ID = /\.tsx(?:\?|$)/;
|
|
39
|
+
/**
|
|
40
|
+
* Cheap first stage only — NOT a recognizer. The match keeps plain modules off
|
|
41
|
+
* the AST path and may false-positive (any '@' — inside a string literal,
|
|
42
|
+
* comment, or import specifier — matches); binding provenance and admission
|
|
43
|
+
* are decided exclusively by the semantic core.
|
|
18
44
|
*/
|
|
19
45
|
function isCompiledElementModule(code, id) {
|
|
20
|
-
return
|
|
46
|
+
return SUPPORTED_MODULE_ID.test(id) && code.includes("@");
|
|
21
47
|
}
|
|
22
48
|
/**
|
|
23
|
-
* Precise second stage behind the
|
|
49
|
+
* Precise second stage behind the prefilter: a canonically bound
|
|
24
50
|
* `@element(...)` decorator application on a class declaration (provenance
|
|
25
51
|
* decided by the semantic core's intrinsic-binding model). Modules that only
|
|
26
52
|
* mention the marker in a string literal or comment, or spell it through a
|
|
@@ -83,6 +109,29 @@ function stableModuleId(file, root, workspaceRoot) {
|
|
|
83
109
|
return clean;
|
|
84
110
|
}
|
|
85
111
|
/**
|
|
112
|
+
* Register one module's style edges (the registry's one write path, S-C1.4).
|
|
113
|
+
*
|
|
114
|
+
* The importing component's root scope is part of every edge it registers
|
|
115
|
+
* (alpha15 S lane, S-C1.2): a light-root component's sheets leave the build
|
|
116
|
+
* wrapped in `@scope (<tag>)`, so the tag travels to the intercept through the
|
|
117
|
+
* registry key. A shipped artifact states the same fact in its Part Program
|
|
118
|
+
* literal.
|
|
119
|
+
*
|
|
120
|
+
* The Vite transform is the caller for a module the bundler processes; a build
|
|
121
|
+
* host that admits a module for its ADMISSION FACTS alone (a Page or Layout the
|
|
122
|
+
* server renders and no island imports) calls the same step with the same
|
|
123
|
+
* compiled program — no second writer, no second registry.
|
|
124
|
+
*/
|
|
125
|
+
function registerStyleEdges(id, specifiers, scopeTag) {
|
|
126
|
+
for (const specifier of specifiers) registerStyleRequest({
|
|
127
|
+
moduleId: styleRequestModuleId(id, specifier, scopeTag),
|
|
128
|
+
importer: id,
|
|
129
|
+
specifier,
|
|
130
|
+
file: styleRequestFile(id, specifier),
|
|
131
|
+
...scopeTag === void 0 ? {} : { scopeTag }
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
86
135
|
* The `open:compiled-element` Vite plugin: runs at `enforce: 'pre'` so the
|
|
87
136
|
* compiler sees authored TSX before Vite's own TS/JSX lowering, compiles every
|
|
88
137
|
* module with a canonically bound `@element` decorator, and leaves all other
|
|
@@ -102,13 +151,39 @@ function compiledElementPlugin(options = {}) {
|
|
|
102
151
|
try {
|
|
103
152
|
const moduleId = stableModuleId(id, viteRoot, workspaceRoot);
|
|
104
153
|
const compiled = compileElementModule(code, moduleId, { staticSidecars: options.staticSidecars });
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
154
|
+
const serverCompiled = compiled === null ? compileServerComponentIfAdmitted(code, moduleId, {
|
|
155
|
+
...options.resolveServerComponentModule === void 0 ? {} : { resolveModule: (specifier) => {
|
|
156
|
+
return options.resolveServerComponentModule(specifier, moduleId) ?? void 0;
|
|
157
|
+
} },
|
|
158
|
+
...options.compiledTags === void 0 ? {} : { compiledTags: options.compiledTags }
|
|
159
|
+
}) : null;
|
|
160
|
+
const artifact = compiled || serverCompiled ? void 0 : readCompiledArtifactStyles(code, id);
|
|
161
|
+
const styleRequests = compiled?.styleRequests ?? artifact?.requests;
|
|
162
|
+
if (styleRequests !== void 0) registerStyleEdges(id, styleRequests, compiled ? lightRootScopeTag(compiled.program) : artifact?.scopeTag);
|
|
163
|
+
if (options.candidates !== void 0) {
|
|
164
|
+
if (compiled !== null) options.candidates.register(id, compiled.classCandidates ?? [], { shadow: compiled.program.root.kind !== "light" });
|
|
165
|
+
else if (serverCompiled !== null) options.candidates.register(id, serverCompiled.classCandidates ?? [], { shadow: false });
|
|
166
|
+
else options.candidates.register(id, compiledArtifactClassCandidates(code, id) ?? [], { shadow: true });
|
|
167
|
+
}
|
|
168
|
+
if (serverCompiled !== null) {
|
|
169
|
+
if (options.typeCheckEmitted) {
|
|
170
|
+
const diagnostics = typeCheckEmittedModule(serverCompiled.code, moduleId, { paths: options.resolutionPaths });
|
|
171
|
+
const first = diagnostics[0];
|
|
172
|
+
if (first) this.error({
|
|
173
|
+
id,
|
|
174
|
+
loc: {
|
|
175
|
+
file: id,
|
|
176
|
+
line: first.line,
|
|
177
|
+
column: first.character
|
|
178
|
+
},
|
|
179
|
+
message: `[open:compiled-element] the server component lane 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.`
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
code: serverCompiled.code,
|
|
184
|
+
map: serverCompiled.map
|
|
185
|
+
};
|
|
186
|
+
}
|
|
112
187
|
if (!compiled) return null;
|
|
113
188
|
if (options.typeCheckEmitted) {
|
|
114
189
|
const diagnostics = typeCheckEmittedModule(compiled.code, moduleId, { paths: options.resolutionPaths });
|
|
@@ -117,19 +192,22 @@ function compiledElementPlugin(options = {}) {
|
|
|
117
192
|
id,
|
|
118
193
|
loc: {
|
|
119
194
|
file: id,
|
|
120
|
-
line: first.line
|
|
121
|
-
column: first.character
|
|
195
|
+
line: first.line,
|
|
196
|
+
column: first.character
|
|
122
197
|
},
|
|
123
198
|
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.`
|
|
124
199
|
});
|
|
125
200
|
}
|
|
126
201
|
return compiled.code;
|
|
127
202
|
} catch (error) {
|
|
128
|
-
if (error instanceof CompiledElementError)
|
|
203
|
+
if (error instanceof CompiledElementError) {
|
|
204
|
+
recordCompilerDiagnostics(error.diagnostics);
|
|
205
|
+
this.error(diagnosticPluginError(error.diagnostics, code, id) ?? error.message);
|
|
206
|
+
}
|
|
129
207
|
throw error;
|
|
130
208
|
}
|
|
131
209
|
}
|
|
132
210
|
};
|
|
133
211
|
}
|
|
134
212
|
//#endregion
|
|
135
|
-
export {
|
|
213
|
+
export { compileElementModule, compiledElementPlugin, hasElementDecoratorApplication, isCompiledElementModule, registerStyleEdges, stableModuleId, stripInlineSourceMapComment };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { ModuleIntrinsicBindings } from "./module-analysis.js";
|
|
2
|
+
import { CompilerFail } from "./compiler-diagnostics.js";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
import { PropertyValueType, SerializableValue } from "@openelement/protocol/part-program";
|
|
5
|
+
//#region src/internal/compiler/semantic-core/analyze-module.d.ts
|
|
6
|
+
export interface CompiledField {
|
|
7
|
+
name: string;
|
|
8
|
+
reflect: boolean;
|
|
9
|
+
attribute: string | null;
|
|
10
|
+
type: PropertyValueType;
|
|
11
|
+
converter: PropertyValueType;
|
|
12
|
+
typeConstructor: 'String' | 'Number' | 'Boolean' | 'Array' | 'Object';
|
|
13
|
+
accessibility: 'public' | 'protected' | 'private' | '';
|
|
14
|
+
typeText: string;
|
|
15
|
+
initializerText: string;
|
|
16
|
+
defaultValue: SerializableValue;
|
|
17
|
+
node: ts.PropertyDeclaration;
|
|
18
|
+
/**
|
|
19
|
+
* Computed field (alpha.8): the source initializer is
|
|
20
|
+
* `computed(() => <expr>)` (optionally behind a type assertion). The
|
|
21
|
+
* compiled accessor returns the derived value. The compiler records the
|
|
22
|
+
* source-signal dependencies and emits a `__computedFields` factory over the
|
|
23
|
+
* instance's signal record; no field initializer runs on the generated class.
|
|
24
|
+
*/
|
|
25
|
+
computed?: {
|
|
26
|
+
deps: string[];
|
|
27
|
+
factoryText: string;
|
|
28
|
+
body: ts.Expression;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** The compiler face of the one canonical attribute-name predicate. */
|
|
32
|
+
declare function isSafeAttributeName(value: string): boolean;
|
|
33
|
+
declare function unwrapExpression(expr: ts.Expression): ts.Expression;
|
|
34
|
+
/** Return a JSON-safe literal, or undefined when the expression is not literal. */
|
|
35
|
+
declare function literalValue(expr: ts.Expression, sf: ts.SourceFile): SerializableValue | undefined;
|
|
36
|
+
declare function primitiveText(value: SerializableValue): string | null;
|
|
37
|
+
/**
|
|
38
|
+
* Message actors for computed-body diagnostics. One validation walk serves
|
|
39
|
+
* both computed-field initializers and the inline conditional class (#1556);
|
|
40
|
+
* the subjects keep each caller's diagnostic prose precise while the shared
|
|
41
|
+
* defaults reproduce the computed-field messages byte-identically.
|
|
42
|
+
*/
|
|
43
|
+
export interface ComputedBodySubjects {
|
|
44
|
+
/** Actor for shape violations ("X may not nest non-arrow functions"). */
|
|
45
|
+
readonly actor: string;
|
|
46
|
+
/** Actor for computed-field reads ("X may not read computed field ..."). */
|
|
47
|
+
readonly single: string;
|
|
48
|
+
/** Actor for undeclared reads ("X may only read this.<declared property>"). */
|
|
49
|
+
readonly plural: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Validate one computed-body expression and derive its factory text (#1556
|
|
53
|
+
* split of parseComputedInitializer). The body may read `this.<field>` of any
|
|
54
|
+
* NON-computed declared field (rewritten to the signal-record read) and use
|
|
55
|
+
* any module-scope values; `this` in any other position, reads of computed
|
|
56
|
+
* fields, and nested non-arrow functions fail closed (OEC9024). Returns the
|
|
57
|
+
* source-signal dependency list (declaration order) and the body text with
|
|
58
|
+
* every `this.<field>` read rewritten to `__s.<field>.value`.
|
|
59
|
+
*
|
|
60
|
+
* `binding` (alpha15 server lane) switches the read accessor from
|
|
61
|
+
* `this.<field>` to `<parameter>.<field>` (a server function component's
|
|
62
|
+
* props parameter): the same declared-field table and the same messages —
|
|
63
|
+
* only the accessor spelling differs, and no signal-record rewrite applies
|
|
64
|
+
* (the generated constructor reads the props object directly). The default
|
|
65
|
+
* keeps the class path's behavior and messages byte-identical.
|
|
66
|
+
*/
|
|
67
|
+
declare function validateComputedExpressionBody(body: ts.Expression, sf: ts.SourceFile, plainFieldNames: ReadonlySet<string>, computedFieldNames: ReadonlySet<string>, fail: CompilerFail, subjects?: ComputedBodySubjects, binding?: {
|
|
68
|
+
readonly parameter: string;
|
|
69
|
+
}): {
|
|
70
|
+
deps: string[];
|
|
71
|
+
bodyText: string;
|
|
72
|
+
};
|
|
73
|
+
/** The `@element` root ownership mode (alpha.8). */
|
|
74
|
+
export type ElementRootKind = 'light' | 'shadow-open' | 'shadow-closed';
|
|
75
|
+
/** Everything the lowering and emission stages consume from one authored module. */
|
|
76
|
+
export interface AnalyzedModule {
|
|
77
|
+
/** Runtime statements admitted as verbatim passthrough (the island policy). */
|
|
78
|
+
passthroughStatements: ts.Statement[];
|
|
79
|
+
/** The one decorated class. */
|
|
80
|
+
classNode: ts.ClassDeclaration;
|
|
81
|
+
/** The class's `@element(...)` decorator application. */
|
|
82
|
+
decorator: ts.Decorator;
|
|
83
|
+
/** The custom-element tag from the decorator's first argument. */
|
|
84
|
+
tag: string;
|
|
85
|
+
rootKind: ElementRootKind;
|
|
86
|
+
delegatesFocus: boolean;
|
|
87
|
+
formAssociated: boolean;
|
|
88
|
+
/** True when the class is `export default class`. */
|
|
89
|
+
isDefaultExport: boolean;
|
|
90
|
+
/** The local name the canonical `OpenElement` base binding carries. */
|
|
91
|
+
openElementLocalName: string;
|
|
92
|
+
className: string;
|
|
93
|
+
fields: CompiledField[];
|
|
94
|
+
methods: ts.MethodDeclaration[];
|
|
95
|
+
render: ts.MethodDeclaration;
|
|
96
|
+
stylesText?: string;
|
|
97
|
+
stylesNode?: ts.Expression;
|
|
98
|
+
stylesTypeText?: string;
|
|
99
|
+
/**
|
|
100
|
+
* The module's style edges (#1558): the authored `.css` imports the class's
|
|
101
|
+
* `static styles` arrays, in authored order. The host build's style-asset
|
|
102
|
+
* plugin intercepts these edges and owns the emitted `.css` artifacts; the
|
|
103
|
+
* generated module copies the authored imports and initializer verbatim.
|
|
104
|
+
*/
|
|
105
|
+
styleRequests: readonly string[];
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Admit one parsed module to the compiled grammar and lift its shape
|
|
109
|
+
* (#1473 split): the top-level
|
|
110
|
+
* statement admission (OEC9008 fail-closed, island policy passthrough), the
|
|
111
|
+
* exactly-one-`@element`-class rule with its decorator options and canonical
|
|
112
|
+
* heritage provenance, and the {@link propertyFields} inventory. Component
|
|
113
|
+
* styles are admitted in one form only (#1558): `.css` file imports arrayed
|
|
114
|
+
* in `static styles` — any inline sheet shape fails closed with OEC9029.
|
|
115
|
+
*/
|
|
116
|
+
declare function analyzeCompiledModule(sf: ts.SourceFile, intrinsics: ModuleIntrinsicBindings, fail: CompilerFail): AnalyzedModule;
|
|
117
|
+
//#endregion
|