@glasshome/widget-sdk 1.4.2 → 1.6.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/dist/types.d.ts CHANGED
@@ -7,11 +7,23 @@ export interface GridSize {
7
7
  w: number;
8
8
  h: number;
9
9
  }
10
+ /**
11
+ * A curated showcase state for preview-image generation. Config references
12
+ * demo-fixture entity ids; the preview harness renders it at `size` and
13
+ * screenshots light + dark. `examples[0]` is the thumbnail.
14
+ *
15
+ * @template C - Widget configuration type
16
+ */
17
+ export interface WidgetExample<C = Record<string, unknown>> {
18
+ config: C;
19
+ size: GridSize;
20
+ label?: string;
21
+ }
10
22
  /**
11
23
  * Widget manifest — declarative metadata about a widget.
12
24
  * Framework-agnostic: no SolidJS imports.
13
25
  */
14
- export interface WidgetManifest {
26
+ export interface WidgetManifest<C = Record<string, unknown>> {
15
27
  name: string;
16
28
  description?: string;
17
29
  minSize: GridSize;
@@ -24,6 +36,7 @@ export interface WidgetManifest {
24
36
  configVersion?: number;
25
37
  capabilities?: CapabilityGrant[];
26
38
  cssUrl?: string;
39
+ examples?: WidgetExample<C>[];
27
40
  }
28
41
  /**
29
42
  * Runtime context provided to widgets by the host.
@@ -49,7 +62,7 @@ export interface WidgetContext {
49
62
  * @template C - Widget configuration type
50
63
  */
51
64
  export interface WidgetDefinition<C = Record<string, unknown>> {
52
- manifest: WidgetManifest;
65
+ manifest: WidgetManifest<C>;
53
66
  configSchema?: ZodType<C, unknown>;
54
67
  migrate?: (config: Record<string, unknown>, fromConfigVersion: number) => Record<string, unknown>;
55
68
  component: (props: {
@@ -3,7 +3,13 @@ export interface GlasshomeWidgetOptions {
3
3
  /** Entry file for the widget (default: "src/index.tsx") */
4
4
  entry?: string;
5
5
  }
6
- /** Packages provided by the host import map — widgets must not bundle these. */
6
+ /**
7
+ * Packages provided by the host import map — widgets must not bundle these.
8
+ * Delegates to the shared list in @glasshome/widget-contract so the host's
9
+ * import map and the widget build can never disagree again. Exact match only:
10
+ * a specifier the import map does not serve (e.g. "@glasshome/ui/tokens")
11
+ * must be bundled, because leaving it external is a runtime 404.
12
+ */
7
13
  export declare function isWidgetExternal(id: string): boolean;
8
14
  interface DiscoveredWidget {
9
15
  name: string;
@@ -1,26 +1,26 @@
1
- import { formatSchemaError as e, widgetManifestSchema as t } from "@glasshome/widget-contract";
2
- import { createHash as n } from "node:crypto";
3
- import { existsSync as r, mkdirSync as i, readFileSync as a, readdirSync as o, rmSync as s, statSync as c, writeFileSync as l } from "node:fs";
4
- import { createRequire as u } from "node:module";
5
- import { dirname as d, join as f, resolve as p, sep as m } from "node:path";
6
- import { fileURLToPath as h } from "node:url";
7
- import g from "@tailwindcss/vite";
1
+ import { formatSchemaError as e, isHostProvidedModule as t, widgetManifestSchema as n } from "@glasshome/widget-contract";
2
+ import { createHash as r } from "node:crypto";
3
+ import { existsSync as i, mkdirSync as a, readFileSync as o, readdirSync as s, rmSync as c, statSync as l, writeFileSync as u } from "node:fs";
4
+ import { createRequire as d } from "node:module";
5
+ import { dirname as f, join as p, resolve as m, sep as h } from "node:path";
6
+ import { fileURLToPath as g } from "node:url";
7
+ import _ from "@tailwindcss/vite";
8
8
  //#region src/vite/index.ts
9
- function _(e) {
9
+ function v(e) {
10
10
  let t = JSON.stringify(e, Object.keys(e).sort());
11
- return n("sha256").update(t).digest("hex").slice(0, 16);
11
+ return r("sha256").update(t).digest("hex").slice(0, 16);
12
12
  }
13
- var v = "virtual:glasshome-widget", y = "\0virtual:glasshome-widget", b = "/@glasshome/preview";
14
- function x(e) {
15
- return m === "\\" ? e.split(m).join("/") : e;
13
+ var y = "virtual:glasshome-widget", b = "\0virtual:glasshome-widget", x = "/@glasshome/preview";
14
+ function S(e) {
15
+ return h === "\\" ? e.split(h).join("/") : e;
16
16
  }
17
- function S() {
18
- return p(d(h(import.meta.url)), "../../preview");
17
+ function C() {
18
+ return m(f(g(import.meta.url)), "../../preview");
19
19
  }
20
- function C(e) {
21
- return e === "solid-js" || e.startsWith("solid-js/") || e === "@glasshome/widget-sdk" || e.startsWith("@glasshome/widget-sdk/") || e === "@glasshome/ui" || e.startsWith("@glasshome/ui/");
20
+ function w(e) {
21
+ return t(e);
22
22
  }
23
- function w() {
23
+ function T() {
24
24
  return {
25
25
  name: "glasshome-widget:sync-layer-guard",
26
26
  apply: "build",
@@ -30,15 +30,15 @@ function w() {
30
30
  }
31
31
  };
32
32
  }
33
- var T = "node_modules/.cache/glasshome-widgets";
34
- function E(e) {
33
+ var E = "node_modules/.cache/glasshome-widgets";
34
+ function D(e) {
35
35
  try {
36
- return u(f(process.cwd(), "package.json")).resolve(e);
36
+ return d(p(process.cwd(), "package.json")).resolve(e);
37
37
  } catch {
38
38
  return null;
39
39
  }
40
40
  }
41
- var D = [
41
+ var O = [
42
42
  "--radius",
43
43
  "--background",
44
44
  "--foreground",
@@ -48,124 +48,124 @@ var D = [
48
48
  "--muted",
49
49
  "--accent"
50
50
  ];
51
- function O(e, t) {
52
- let n = f(e, `${t}.css`);
53
- if (!r(n)) return;
54
- let i = a(n, "utf-8");
55
- for (let e of i.matchAll(/:host[^{]*\{([^}]*)\}/g)) {
56
- let n = e[1] ?? "", r = D.find((e) => n.includes(`${e}:`));
51
+ function k(e, t) {
52
+ let n = p(e, `${t}.css`);
53
+ if (!i(n)) return;
54
+ let r = o(n, "utf-8");
55
+ for (let e of r.matchAll(/:host[^{]*\{([^}]*)\}/g)) {
56
+ let n = e[1] ?? "", r = O.find((e) => n.includes(`${e}:`));
57
57
  if (r) throw Error(`[glasshome-widget] "${t}.css" defines theme variable "${r}" on :host. Theme variables must inherit from the document so widgets follow the household theme — never re-scope them into the widget's shadow root.`);
58
58
  }
59
59
  }
60
- function k(e, t, n) {
61
- let a = p(e, T);
62
- i(a, { recursive: !0 });
63
- let o = E("@glasshome/ui/styles");
60
+ function A(e, t, n) {
61
+ let r = m(e, E);
62
+ a(r, { recursive: !0 });
63
+ let o = D("@glasshome/ui/styles");
64
64
  if (!o) throw Error("[widget-sdk] @glasshome/ui is required to build widgets (it provides the theme every GlassHome widget ships with). Add it to your widget's dependencies.");
65
- let s = p(d(o), "../.."), c = E("@glasshome/widget-sdk/tailwind-sources"), u = c ? d(c) : null, m = [
66
- f(s, "src"),
67
- f(s, "dist"),
68
- ...u ? [f(u, "src"), f(u, "dist")] : [],
69
- d(n)
70
- ].filter((e) => r(e)), h = p(a, `${t}.tailwind.css`);
71
- l(h, [
65
+ let s = m(f(o), "../.."), c = D("@glasshome/widget-sdk/tailwind-sources"), l = c ? f(c) : null, d = [
66
+ p(s, "src"),
67
+ p(s, "dist"),
68
+ ...l ? [p(l, "src"), p(l, "dist")] : [],
69
+ f(n)
70
+ ].filter((e) => i(e)), h = m(r, `${t}.tailwind.css`);
71
+ u(h, [
72
72
  "@import \"tailwindcss\" source(none);",
73
73
  "@import \"tw-animate-css\";",
74
74
  "@import \"@glasshome/ui/styles/theme\";",
75
- ...m.map((e) => `@source "${x(e)}";`),
75
+ ...d.map((e) => `@source "${S(e)}";`),
76
76
  "@custom-variant dark {",
77
77
  " &:is(.dark *) { @slot }",
78
78
  " :host(.dark) & { @slot }",
79
79
  "}",
80
80
  ""
81
81
  ].join("\n"));
82
- let g = p(a, `${t}.entry.ts`);
83
- return l(g, [
84
- `import "${x(h)}";`,
85
- `export { default } from "${x(n)}";`,
82
+ let g = m(r, `${t}.entry.ts`);
83
+ return u(g, [
84
+ `import "${S(h)}";`,
85
+ `export { default } from "${S(n)}";`,
86
86
  ""
87
87
  ].join("\n")), g;
88
88
  }
89
- function A() {
90
- let e = g();
89
+ function j() {
90
+ let e = _();
91
91
  for (let t of e) t.apply ??= "build";
92
92
  return e;
93
93
  }
94
- function j(e) {
94
+ function M(e) {
95
95
  let t = [];
96
- if (!r(e)) return t;
97
- for (let n of o(e)) {
98
- let i = p(e, n);
99
- if (!c(i).isDirectory()) continue;
100
- let a = p(i, "index.tsx"), o = p(i, "manifest.json");
101
- r(a) && r(o) && t.push({
96
+ if (!i(e)) return t;
97
+ for (let n of s(e)) {
98
+ let r = m(e, n);
99
+ if (!l(r).isDirectory()) continue;
100
+ let a = m(r, "index.tsx"), o = m(r, "manifest.json");
101
+ i(a) && i(o) && t.push({
102
102
  name: n,
103
103
  entry: a
104
104
  });
105
105
  }
106
106
  return t;
107
107
  }
108
- function M(n, s) {
109
- let u = [];
110
- if (r(n)) for (let i of o(n)) {
111
- if (!c(f(n, i)).isDirectory()) continue;
112
- let o = f(n, i, "manifest.json");
113
- if (!r(o)) continue;
114
- let l = t.safeParse(JSON.parse(a(o, "utf-8")));
115
- if (!l.success) throw Error(`[registry] Invalid manifest for "${i}": ${e(l.error)}`);
116
- let d = r(f(s, `${i}.css`));
117
- u.push({
118
- ...l.data,
119
- bundleUrl: `./${i}.js`,
120
- ...d ? { cssUrl: `./${i}.css` } : {}
108
+ function N(t, r) {
109
+ let c = [];
110
+ if (i(t)) for (let a of s(t)) {
111
+ if (!l(p(t, a)).isDirectory()) continue;
112
+ let s = p(t, a, "manifest.json");
113
+ if (!i(s)) continue;
114
+ let u = n.safeParse(JSON.parse(o(s, "utf-8")));
115
+ if (!u.success) throw Error(`[registry] Invalid manifest for "${a}": ${e(u.error)}`);
116
+ let d = i(p(r, `${a}.css`));
117
+ c.push({
118
+ ...u.data,
119
+ bundleUrl: `./${a}.js`,
120
+ ...d ? { cssUrl: `./${a}.css` } : {}
121
121
  });
122
122
  }
123
123
  let d = {
124
124
  version: 1,
125
125
  generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
126
126
  baseUrl: "./",
127
- widgets: u
127
+ widgets: c
128
128
  };
129
- r(s) || i(s, { recursive: !0 }), l(f(s, "registry.json"), JSON.stringify(d, null, 2)), console.log(`[registry] Generated registry.json with ${u.length} widget(s)`);
129
+ i(r) || a(r, { recursive: !0 }), u(p(r, "registry.json"), JSON.stringify(d, null, 2)), console.log(`[registry] Generated registry.json with ${c.length} widget(s)`);
130
130
  }
131
- async function N(e) {
132
- let { build: t } = await import("vite"), n = process.cwd(), a = p(n, e?.srcDir ?? "src"), o = p(n, e?.outDir ?? "dist"), c = j(a);
133
- if (c.length === 0) {
134
- console.warn("[glasshome-widgets] No widgets found in", a);
131
+ async function P(e) {
132
+ let { build: t } = await import("vite"), n = process.cwd(), r = m(n, e?.srcDir ?? "src"), o = m(n, e?.outDir ?? "dist"), s = M(r);
133
+ if (s.length === 0) {
134
+ console.warn("[glasshome-widgets] No widgets found in", r);
135
135
  return;
136
136
  }
137
137
  if (e?.only) {
138
138
  let t = new Set(e.only);
139
- c = c.filter((e) => t.has(e.name));
139
+ s = s.filter((e) => t.has(e.name));
140
140
  }
141
- e?.only || r(o) && s(o, { recursive: !0 }), r(o) || i(o, { recursive: !0 });
142
- for (let r of c) await t({
141
+ e?.only || i(o) && c(o, { recursive: !0 }), i(o) || a(o, { recursive: !0 });
142
+ for (let r of s) await t({
143
143
  configFile: !1,
144
144
  root: n,
145
145
  plugins: [
146
- ...A(),
146
+ ...j(),
147
147
  ...e?.plugins ?? [],
148
- w()
148
+ T()
149
149
  ],
150
150
  ...e?.viteConfig,
151
151
  build: {
152
152
  lib: {
153
- entry: k(n, r.name, r.entry),
153
+ entry: A(n, r.name, r.entry),
154
154
  formats: ["es"],
155
155
  fileName: r.name,
156
156
  cssFileName: r.name
157
157
  },
158
- rollupOptions: { external: C },
158
+ rollupOptions: { external: w },
159
159
  outDir: o,
160
160
  emptyOutDir: !1,
161
161
  copyPublicDir: !1,
162
162
  ...e?.viteConfig?.build
163
163
  },
164
164
  logLevel: "warn"
165
- }), O(o, r.name);
166
- M(a, o);
165
+ }), k(o, r.name);
166
+ N(r, o);
167
167
  }
168
- function P(e) {
168
+ function F(e) {
169
169
  let t = e?.entry ?? "src/index.tsx", n = {
170
170
  name: "glasshome-widget:build",
171
171
  apply: "build",
@@ -173,31 +173,31 @@ function P(e) {
173
173
  let e = process.cwd();
174
174
  return { build: {
175
175
  lib: {
176
- entry: k(e, "index", p(e, t)),
176
+ entry: A(e, "index", m(e, t)),
177
177
  formats: ["es"],
178
178
  fileName: "index",
179
179
  cssFileName: "index"
180
180
  },
181
- rollupOptions: { external: C }
181
+ rollupOptions: { external: w }
182
182
  } };
183
183
  }
184
- }, i = {
184
+ }, r = {
185
185
  name: "glasshome-widget:dev",
186
186
  apply: "serve",
187
187
  enforce: "pre",
188
188
  config() {
189
- let e = S(), t = process.cwd();
189
+ let e = C(), t = process.cwd();
190
190
  return { server: { fs: { allow: [
191
191
  e,
192
192
  t,
193
- p(t, "..", "..")
193
+ m(t, "..", "..")
194
194
  ] } } };
195
195
  },
196
196
  configureServer(e) {
197
- let t = f(S(), "preview.html");
197
+ let t = p(C(), "preview.html");
198
198
  e.middlewares.use(async (n, r, i) => {
199
199
  if (n.url === "/" || n.url === "/index.html") try {
200
- let i = a(t, "utf-8");
200
+ let i = o(t, "utf-8");
201
201
  i = await e.transformIndexHtml(n.url, i), r.setHeader("Content-Type", "text/html"), r.statusCode = 200, r.end(i);
202
202
  } catch (e) {
203
203
  i(e);
@@ -206,44 +206,44 @@ function P(e) {
206
206
  });
207
207
  },
208
208
  resolveId(e) {
209
- if (e === b) return x(f(S(), "host.tsx"));
210
- if (e === v) return y;
209
+ if (e === x) return S(p(C(), "host.tsx"));
210
+ if (e === y) return b;
211
211
  },
212
212
  load(e) {
213
- if (e === y) return `export { default } from "${x(p(process.cwd(), t))}";`;
213
+ if (e === b) return `export { default } from "${S(m(process.cwd(), t))}";`;
214
214
  }
215
- }, o = {
215
+ }, a = {
216
216
  name: "glasshome-widget:schema",
217
217
  apply: "build",
218
218
  async closeBundle() {
219
- O(p(process.cwd(), "dist"), "index");
220
- let e = p(process.cwd(), "dist", "index.js");
221
- if (r(e)) try {
219
+ k(m(process.cwd(), "dist"), "index");
220
+ let e = m(process.cwd(), "dist", "index.js");
221
+ if (i(e)) try {
222
222
  let t = (await import(`${e}?t=${Date.now()}`)).default;
223
223
  if (!t?.configSchema) return;
224
- let { z: n } = await import("zod"), i = n.toJSONSchema(t.configSchema, { unrepresentable: "any" }), o = p(process.cwd(), "manifest.json");
225
- if (r(o)) {
226
- let e = JSON.parse(a(o, "utf-8"));
227
- e.schema = i, l(o, JSON.stringify(e, null, 2));
224
+ let { z: n } = await import("zod"), r = n.toJSONSchema(t.configSchema, { unrepresentable: "any" }), a = m(process.cwd(), "manifest.json");
225
+ if (i(a)) {
226
+ let e = JSON.parse(o(a, "utf-8"));
227
+ e.schema = r, u(a, JSON.stringify(e, null, 2));
228
228
  }
229
- let s = _(i), c = p(process.cwd(), ".schema-hash");
230
- if (r(c) && a(c, "utf-8").trim() !== s) {
229
+ let s = v(r), c = m(process.cwd(), ".schema-hash");
230
+ if (i(c) && o(c, "utf-8").trim() !== s) {
231
231
  let e = t.manifest?.name ?? "unknown";
232
232
  console.warn(`[widget-sdk] Schema shape changed for "${e}" — verify configVersion was bumped`);
233
233
  }
234
- l(c, s);
234
+ u(c, s);
235
235
  } catch {}
236
236
  }
237
237
  };
238
238
  return [
239
- ...A(),
239
+ ...j(),
240
240
  n,
241
- w(),
242
- o,
243
- i
241
+ T(),
242
+ a,
243
+ r
244
244
  ];
245
245
  }
246
- function F(e) {
246
+ function I(e) {
247
247
  let t = e?.srcDir ?? "src", n = e?.outDir ?? "dist", r = [];
248
248
  return [{
249
249
  name: "glasshome-widgets:build",
@@ -262,7 +262,7 @@ function F(e) {
262
262
  if (e === "\0glasshome-noop") return "export {}";
263
263
  },
264
264
  async closeBundle() {
265
- await N({
265
+ await P({
266
266
  srcDir: t,
267
267
  outDir: n,
268
268
  plugins: r
@@ -271,4 +271,4 @@ function F(e) {
271
271
  }];
272
272
  }
273
273
  //#endregion
274
- export { N as buildWidgets, j as discoverWidgets, M as generateRegistry, P as glasshomeWidget, F as glasshomeWidgets, C as isWidgetExternal };
274
+ export { P as buildWidgets, M as discoverWidgets, N as generateRegistry, F as glasshomeWidget, I as glasshomeWidgets, w as isWidgetExternal };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glasshome/widget-sdk",
3
- "version": "1.4.2",
3
+ "version": "1.6.0",
4
4
  "description": "SDK for building GlassHome dashboard widgets with SolidJS",
5
5
  "keywords": [
6
6
  "glasshome",
@@ -52,11 +52,11 @@
52
52
  "typecheck:test": "tsc --noEmit -p tsconfig.test.json"
53
53
  },
54
54
  "peerDependencies": {
55
- "@glasshome/sync-layer": "0.3.1",
55
+ "@glasshome/sync-layer": "0.3.2",
56
56
  "solid-js": "^1.9.11"
57
57
  },
58
58
  "dependencies": {
59
- "@glasshome/widget-contract": "0.1.0",
59
+ "@glasshome/widget-contract": "0.2.0",
60
60
  "@modular-forms/solid": "^0.25.1",
61
61
  "clsx": "^2.1.1",
62
62
  "tailwind-merge": "^3.5.0",