@webjsdev/server 0.8.3 → 0.8.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webjsdev/server",
3
- "version": "0.8.3",
3
+ "version": "0.8.4",
4
4
  "type": "module",
5
5
  "description": "webjs dev/prod server: SSR, router, API, server actions, live reload",
6
6
  "main": "index.js",
@@ -132,6 +132,21 @@ const CLIENT_ROUTER_IMPORTS = ['navigate', 'enableClientRouter', 'disableClientR
132
132
 
133
133
  /** Identifiers that only exist in a browser; their presence means client work. */
134
134
  const CLIENT_GLOBAL_RE = /\b(?:window|document|navigator|localStorage|sessionStorage|customElements|matchMedia|addEventListener)\b/;
135
+
136
+ /**
137
+ * Cross-module observation of a component's registration. A module that
138
+ * observes another component's tag forces that component to register on the
139
+ * client, so the observed component cannot be elided even when its own render
140
+ * is display-only (eliding it drops its `customElements.define`, after which
141
+ * the observation silently fails). These scan for the three statically visible
142
+ * forms; the captured group is the observed tag (or class) name.
143
+ * - `customElements.whenDefined('my-tag')` / `whenDefined("my-tag")`
144
+ * - a CSS `my-tag:defined { … }` selector
145
+ * - `x instanceof MyClass` (mapped back to a tag via the component's className)
146
+ */
147
+ const WHEN_DEFINED_RE = /\bwhenDefined\s*\(\s*['"`]([a-z][a-z0-9]*-[a-z0-9-]*)['"`]/g;
148
+ const TAG_DEFINED_RE = /\b([a-z][a-z0-9]*-[a-z0-9-]*):defined\b/g;
149
+ const INSTANCEOF_RE = /\binstanceof\s+([A-Z][A-Za-z0-9_$]*)/g;
135
150
  /** Same, for component source, minus `customElements` (the registration call
136
151
  * `customElements.define(...)` legitimately uses it and must not force ship). */
137
152
  const COMPONENT_CLIENT_GLOBAL_RE = /\b(?:window|document|navigator|localStorage|sessionStorage|matchMedia|addEventListener)\b/;
@@ -598,9 +613,12 @@ export async function analyzeElision(components, routeModules, moduleGraph, read
598
613
  const componentFiles = new Set();
599
614
  /** @type {Map<string, string>} */
600
615
  const tagToFile = new Map();
616
+ /** @type {Map<string, string>} className -> file, for instanceof observation */
617
+ const classToFile = new Map();
601
618
  for (const c of components) {
602
619
  componentFiles.add(c.file);
603
620
  tagToFile.set(c.tag, c.file);
621
+ if (c.className) classToFile.set(c.className, c.file);
604
622
  }
605
623
 
606
624
  /** @type {Set<string>} */
@@ -615,6 +633,9 @@ export async function analyzeElision(components, routeModules, moduleGraph, read
615
633
  const clientGlobalOrBareFiles = new Set();
616
634
  /** @type {Set<string>} */
617
635
  const serverFiles = new Set();
636
+ /** @type {Set<string>} component files forced to ship because some module
637
+ * observes their registration (whenDefined / :defined / instanceof). */
638
+ const observedComponentFiles = new Set();
618
639
 
619
640
  /** @type {Set<string>} */
620
641
  const allFiles = new Set(componentFiles);
@@ -646,8 +667,29 @@ export async function analyzeElision(components, routeModules, moduleGraph, read
646
667
  if (componentFiles.has(file) && analyzeComponentSource(src).interactive) {
647
668
  mustShip.add(file);
648
669
  }
670
+ // Cross-module registration observation (#169): if THIS module observes
671
+ // another component's tag, that component must register client-side, so
672
+ // it cannot be elided. Map each observed tag/class back to its component
673
+ // file. Resolution against tagToFile / classToFile happens after the loop
674
+ // (all components are known up front, but we collect here while we hold
675
+ // each source). Verdict-safe: only ever forces MORE components to ship.
676
+ for (const m of src.matchAll(WHEN_DEFINED_RE)) {
677
+ const f = tagToFile.get(m[1]); if (f) observedComponentFiles.add(f);
678
+ }
679
+ for (const m of src.matchAll(TAG_DEFINED_RE)) {
680
+ const f = tagToFile.get(m[1]); if (f) observedComponentFiles.add(f);
681
+ }
682
+ for (const m of src.matchAll(INSTANCEOF_RE)) {
683
+ const f = classToFile.get(m[1]); if (f) observedComponentFiles.add(f);
684
+ }
649
685
  }
650
686
 
687
+ // Force every observed component to ship before the fixpoint runs, so the
688
+ // render/import rules propagate from it too. Dynamic tag strings and external
689
+ // (non graph-reachable) stylesheets remain an author-facing caveat, since
690
+ // static analysis cannot see them.
691
+ for (const f of observedComponentFiles) mustShip.add(f);
692
+
651
693
  // Reverse import edges (who imports each file), built once from the graph.
652
694
  // Drives both the closure-client-work reachability below and the fixpoint's
653
695
  // import rule, each in O(N+E) rather than a per-component closure walk.