@openelement/compiler 1.0.0-alpha.14 → 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.
Files changed (28) hide show
  1. package/package.json +3 -2
  2. package/src/compiler.d.ts +8 -4
  3. package/src/compiler.js +8 -4
  4. package/src/internal/compiler/compiled-artifact-class-candidates.d.ts +36 -0
  5. package/src/internal/compiler/compiled-artifact-class-candidates.js +60 -0
  6. package/src/internal/compiler/compiled-artifact-style-requests.d.ts +17 -0
  7. package/src/internal/compiler/compiled-artifact-style-requests.js +48 -2
  8. package/src/internal/compiler/plugin.d.ts +47 -9
  9. package/src/internal/compiler/plugin.js +102 -24
  10. package/src/internal/compiler/semantic-core/analyze-module.d.ts +117 -0
  11. package/src/internal/compiler/semantic-core/analyze-module.js +55 -10
  12. package/src/internal/compiler/semantic-core/compile.d.ts +10 -2
  13. package/src/internal/compiler/semantic-core/compile.js +6 -4
  14. package/src/internal/compiler/semantic-core/diagnostics/index.d.ts +21 -1
  15. package/src/internal/compiler/semantic-core/diagnostics/index.js +32 -1
  16. package/src/internal/compiler/semantic-core/emit-module.js +4 -0
  17. package/src/internal/compiler/semantic-core/emit-program.js +23 -3
  18. package/src/internal/compiler/semantic-core/lower-program.d.ts +169 -0
  19. package/src/internal/compiler/semantic-core/lower-program.js +168 -56
  20. package/src/internal/compiler/semantic-core/module-analysis.d.ts +55 -0
  21. package/src/internal/compiler/semantic-core/module-analysis.js +74 -4
  22. package/src/internal/compiler/semantic-core/server-component.d.ts +114 -0
  23. package/src/internal/compiler/semantic-core/server-component.js +481 -0
  24. package/src/internal/compiler/semantic-core/type-check.js +1 -1
  25. package/src/internal/compiler/style-requests.d.ts +43 -7
  26. package/src/internal/compiler/style-requests.js +70 -11
  27. package/src/internal/compiler/tailwind-candidates.d.ts +84 -0
  28. package/src/internal/compiler/tailwind-candidates.js +54 -0
@@ -2,10 +2,20 @@
2
2
  const requests = /* @__PURE__ */ new Map();
3
3
  /** Second index: which importing modules carry edges (the intercept's gate). */
4
4
  const importers = /* @__PURE__ */ new Set();
5
+ /**
6
+ * Third index: the light-root scope tag an importing module's edges carry.
7
+ * The intercept resolves a `.css` import from `(importer, specifier)` alone,
8
+ * so the scope must be readable from the importer — the registry key and the
9
+ * intercept's key derivation would otherwise disagree.
10
+ */
11
+ const importerScopes = /* @__PURE__ */ new Map();
5
12
  /** Register one generated edge (idempotent per module id — recompiles overwrite). */
6
13
  function registerStyleRequest(request) {
7
14
  requests.set(request.moduleId, request);
8
- importers.add(cleanImporterId(request.importer));
15
+ const importer = cleanImporterId(request.importer);
16
+ importers.add(importer);
17
+ if (request.scopeTag === void 0) importerScopes.delete(importer);
18
+ else importerScopes.set(importer, request.scopeTag);
9
19
  }
10
20
  /** The edge a resolved sheet module id carries, if any. */
11
21
  function getStyleRequest(moduleId) {
@@ -18,33 +28,82 @@ function hasStyleImporter(importerId) {
18
28
  /** Every edge whose sheet file is the given path — the dev HMR reverse lookup. */
19
29
  function styleEdgesForFile(file) {
20
30
  const clean = file.split("?", 1)[0];
21
- return [...requests.values()].filter((request) => request.moduleId === clean);
31
+ return [...requests.values()].filter((request) => request.file === clean);
32
+ }
33
+ /**
34
+ * Every edge one importing module declared. The host build uses it to emit the
35
+ * style resources of modules it admitted for their edges alone (S-C1.4: a Page
36
+ * or Layout the server renders and no island imports).
37
+ */
38
+ function styleRequestsForImporter(importerId) {
39
+ const clean = importerId.split("?", 1)[0];
40
+ return [...requests.values()].filter((request) => request.importer === clean);
22
41
  }
23
42
  /** Drop every registered edge (a build's bookkeeping is its own lifetime). */
24
43
  function clearStyleRequests() {
25
44
  requests.clear();
26
45
  importers.clear();
46
+ importerScopes.clear();
27
47
  }
28
48
  function cleanImporterId(importerId) {
29
49
  return importerId.split("?", 1)[0];
30
50
  }
31
51
  /**
32
- * The registry key for one authored edge: the sheet file's resolved path.
33
- * Query suffixes on the importer id (dev-time cache busting) are stripped,
34
- * so recompiles re-register under one key; the full relative path keeps
35
- * same-named sheets in sibling subdirectories distinct edges.
52
+ * The separator between a sheet file and the light-root scope tag in one
53
+ * edge's registry key. `\0` can never appear in a path or a tag name, so a
54
+ * scoped key can never collide with a plain file key.
55
+ */
56
+ const SCOPE_KEY_SEPARATOR = "\0";
57
+ /**
58
+ * The registry key for one authored edge: the sheet file's resolved path,
59
+ * plus the importing component's light-root scope tag when it has one. Query
60
+ * suffixes on the importer id (dev-time cache busting) are stripped, so
61
+ * recompiles re-register under one key; the full relative path keeps
62
+ * same-named sheets in sibling subdirectories distinct edges. The scope
63
+ * tag rides the key because the emitted asset is a different resource per
64
+ * scope: a light root's sheet is served wrapped in `@scope (<tag>)`.
65
+ *
66
+ * Registering an edge passes the tag explicitly (the caller is the compile
67
+ * that just proved it); the intercept resolves a `.css` import from
68
+ * `(importer, specifier)` alone, so it reads the same fact back through the
69
+ * importer index. Both paths land on one key for one edge, and a disagreement
70
+ * can only fail closed at the intercept — never emit an unscoped asset.
71
+ */
72
+ function styleRequestModuleId(importerId, specifier, scopeTag) {
73
+ const file = styleRequestFile(importerId, specifier);
74
+ const scope = scopeTag ?? importerScopes.get(cleanImporterId(importerId));
75
+ return scope === void 0 ? file : `${file}${SCOPE_KEY_SEPARATOR}${scope}`;
76
+ }
77
+ /**
78
+ * The light-root scope tag of a compiled Part Program — undefined for a
79
+ * shadow-root program, whose sheets ship exactly as authored (they already
80
+ * have a boundary). One reader for the fact, shared by both host bindings of
81
+ * the compiled-element transform (the build plugin and the dev/HMR plugin)
82
+ * and by the compiled-artifact read.
36
83
  */
37
- function styleRequestModuleId(importerId, specifier) {
38
- return styleRequestFile(importerId, specifier);
84
+ function lightRootScopeTag(program) {
85
+ const candidate = program;
86
+ const tag = candidate?.tag;
87
+ if (typeof tag !== "string" || tag === "") return void 0;
88
+ return candidate?.root?.kind === "light" ? tag : void 0;
39
89
  }
40
90
  /**
41
91
  * The sheet file's absolute path for one authored edge: the importing
42
92
  * module's directory joined with the authored specifier. Relative specifiers
43
- * only (the compiler refuses bare ones), so the join is total.
93
+ * only (the compiler refuses bare ones), so the join is total. Parent
94
+ * segments (`../theme.css`) resolve lexically against the directory — the key
95
+ * must be the path the bundler's resolver lands on, never a verbatim `..`
96
+ * id no lookup can match.
44
97
  */
45
98
  function styleRequestFile(importerId, specifier) {
46
99
  const clean = cleanImporterId(importerId);
47
- return `${clean.slice(0, clean.lastIndexOf("/"))}/${specifier.replace(/^\.\//, "")}`;
100
+ const segments = clean.slice(0, clean.lastIndexOf("/")).split("/");
101
+ for (const segment of specifier.split("/")) {
102
+ if (segment === "" || segment === ".") continue;
103
+ if (segment === "..") segments.pop();
104
+ else segments.push(segment);
105
+ }
106
+ return segments.join("/");
48
107
  }
49
108
  //#endregion
50
- export { clearStyleRequests, getStyleRequest, hasStyleImporter, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId };
109
+ export { clearStyleRequests, getStyleRequest, hasStyleImporter, lightRootScopeTag, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId, styleRequestsForImporter };
@@ -0,0 +1,84 @@
1
+ //#region src/internal/compiler/tailwind-candidates.d.ts
2
+ /**
3
+ * Tailwind class-candidate channel (alpha15 S lane).
4
+ *
5
+ * The compiler never interprets Tailwind's class grammar: a compiled module's
6
+ * compile-time `class` values are collected as OPAQUE STRINGS by the lowering
7
+ * (static literals, primitive-literal attributes, both branches of an admitted
8
+ * conditional class, `each` item-template literals) and travel to the build
9
+ * layer through this registry — the same one-writer/one-reader shape as the
10
+ * style-edge registry. The build layer hands the list to the OFFICIAL Tailwind
11
+ * compiler as explicit candidates (`@source inline(…)`), which owns candidate
12
+ * syntax, utility generation, theme compilation and optimization.
13
+ *
14
+ * Root scope is part of the fact, because it decides which sheet an engine can
15
+ * use: a light-root component's classes are covered by the document entry's own
16
+ * pipeline when its DOM is the document's DOM ({@linkcode
17
+ * ClassCandidateRegistry.allLight} — the document-sheet input for an engine
18
+ * that generates its own document CSS), while a shadow-root component's
19
+ * classes can only reach its tree through the component stylesheet.
20
+ *
21
+ * The COMPONENT sheet compiles from every admitted candidate
22
+ * ({@linkcode ClassCandidateRegistry.all}), not from the shadow-root subset:
23
+ * a light-root component's DOM does not always end up in the document. The
24
+ * layout convention puts every page inside the layout's shadow template, and a
25
+ * shadow component may render a light-root child, so a document-level class
26
+ * can be rendered inside a shadow tree where only the component sheet reaches
27
+ * it. The sheet is emitted only when a shadow root exists, so an app without
28
+ * one pays nothing for the wider set.
29
+ *
30
+ * OWNERSHIP (defect C): the registry is an explicit object with a lifetime its
31
+ * owner chooses — one per build, one per dev server — instead of a
32
+ * process-global map. Two builds (or two dev servers) in one process therefore
33
+ * cannot inherit each other's facts, and a caller never has to clear shared
34
+ * state another live build owns. Registering a module's CURRENT facts is
35
+ * authoritative: a module that now carries no class token drops its previous
36
+ * facts, so an emptied or edited module cannot leave a stale candidate behind
37
+ * (and a deleted file is forgotten by registering its absence —
38
+ * {@linkcode ClassCandidateRegistry.remove}).
39
+ *
40
+ * The registry only carries FACTS the compiler could prove statically. A
41
+ * `class={this.<field>}` binding contributes nothing — the compiler cannot
42
+ * know its runtime value, and it must not guess. Completeness for such
43
+ * bindings is the author's explicit declaration through Tailwind's own
44
+ * `@source` mechanisms in the app's CSS entry.
45
+ */
46
+ /**
47
+ * One owner's class-candidate facts: the build (or dev server) that created it
48
+ * is the only writer, its stylesheet emitter the only reader.
49
+ */
50
+ export interface ClassCandidateRegistry {
51
+ /**
52
+ * Register ONE module's statically admitted class candidates. The call is
53
+ * authoritative for that module: an empty list means the module now carries
54
+ * no statically admitted class token, so its previous facts are dropped
55
+ * rather than left to inflate the next stylesheet.
56
+ */
57
+ register(moduleId: string, candidates: readonly string[], options: {
58
+ shadow: boolean;
59
+ }): void;
60
+ /** Forget one module (a deleted file, a module that left the graph). */
61
+ remove(moduleId: string): void;
62
+ /** The candidates one module contributed, or an empty list. */
63
+ forModule(moduleId: string): readonly string[];
64
+ /** Every candidate registered so far, first-seen ordered and deduplicated. */
65
+ all(): readonly string[];
66
+ /**
67
+ * The SHADOW-root candidates only. The COMPONENT sheet no longer compiles
68
+ * from this view (a light-root class can render inside a shadow tree, so the
69
+ * sheet takes {@linkcode ClassCandidateRegistry.all}); it stays as the
70
+ * shadow-only view of the facts, for a consumer that wants exactly that.
71
+ */
72
+ allShadow(): readonly string[];
73
+ /**
74
+ * The LIGHT-root candidates only — the document sheet's input for an engine
75
+ * that generates its own document CSS (a shadow component's classes are
76
+ * delivered by the component stylesheet and never inflate the document one).
77
+ */
78
+ allLight(): readonly string[];
79
+ /** True when any module contributed candidates. */
80
+ has(): boolean;
81
+ }
82
+ /** Create one owner's registry (see the lifetime note above). */
83
+ export declare function createClassCandidateRegistry(): ClassCandidateRegistry;
84
+ //#endregion
@@ -0,0 +1,54 @@
1
+ //#region src/internal/compiler/tailwind-candidates.ts
2
+ /** Create one owner's registry (see the lifetime note above). */
3
+ function createClassCandidateRegistry() {
4
+ const byModule = /* @__PURE__ */ new Map();
5
+ const union = (include) => {
6
+ const seen = /* @__PURE__ */ new Set();
7
+ for (const entry of byModule.values()) {
8
+ if (!include(entry)) continue;
9
+ for (const candidate of entry.tokens) seen.add(candidate);
10
+ }
11
+ return [...seen];
12
+ };
13
+ return {
14
+ register(moduleId, candidates, options) {
15
+ const id = cleanModuleId(moduleId);
16
+ if (candidates.length === 0) {
17
+ byModule.delete(id);
18
+ return;
19
+ }
20
+ byModule.set(id, {
21
+ shadow: options.shadow,
22
+ tokens: [...candidates]
23
+ });
24
+ },
25
+ remove(moduleId) {
26
+ byModule.delete(cleanModuleId(moduleId));
27
+ },
28
+ forModule(moduleId) {
29
+ return byModule.get(cleanModuleId(moduleId))?.tokens ?? [];
30
+ },
31
+ all() {
32
+ return union(() => true);
33
+ },
34
+ allShadow() {
35
+ return union((entry) => entry.shadow);
36
+ },
37
+ allLight() {
38
+ return union((entry) => !entry.shadow);
39
+ },
40
+ has() {
41
+ return byModule.size > 0;
42
+ }
43
+ };
44
+ }
45
+ /**
46
+ * The module-id normalization every lookup shares: query suffixes on the module
47
+ * id (dev-time cache busting) are stripped, so the authored and dev-served
48
+ * spellings answer identically.
49
+ */
50
+ function cleanModuleId(moduleId) {
51
+ return moduleId.split("?", 1)[0];
52
+ }
53
+ //#endregion
54
+ export { createClassCandidateRegistry };