@commercebuild/extension 0.0.14 → 0.0.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.
@@ -0,0 +1,20 @@
1
+ {
2
+ "//": "AUTO-GENERATED from packages/extension-host-libs — do not edit. Regenerate with `yarn generate-extension-host-types`.",
3
+ "external": [
4
+ "react",
5
+ "react-dom",
6
+ "react-dom/client",
7
+ "firebase/app",
8
+ "firebase/firestore"
9
+ ],
10
+ "globals": {
11
+ "react": "cb.lib.React",
12
+ "react-dom": "cb.lib.ReactDOM",
13
+ "react-dom/client": "cb.lib.ReactDOM",
14
+ "firebase/app": "cb.lib.firebase.app",
15
+ "firebase/firestore": "cb.lib.firebase.firestore"
16
+ },
17
+ "forbidden": [
18
+ "firebase/firestore/lite"
19
+ ]
20
+ }
@@ -3,7 +3,7 @@ import ViteHMRNotifierPlugin from "../scripts/vite-plugin-hmr-notifier.mjs";
3
3
  import react from "@vitejs/plugin-react";
4
4
  import tailwindcss from "@tailwindcss/vite";
5
5
  // import dts from "vite-plugin-dts";
6
- // import { fileURLToPath } from "url";
6
+ import { fileURLToPath } from "url";
7
7
  import path from "path";
8
8
  import { existsSync, readFileSync } from "fs";
9
9
  import chalk from "chalk";
@@ -30,8 +30,107 @@ const cbMetaBanner = cbApiVersion
30
30
  cbApiVersion,
31
31
  )}});}}catch(e){}})();`
32
32
  : "";
33
- // const __filename = fileURLToPath(import.meta.url);
34
- // const __dirname = dirname(__filename);
33
+
34
+ // Host-provided libraries (import specifier → cb.* global), generated
35
+ // from the cb-store host-lib registry so this build externalizes
36
+ // exactly what the storefront provides. Regenerated by cb-store's
37
+ // `yarn generate-extension-host-types`; do not edit host-libs.json.
38
+ const hostLibs = JSON.parse(
39
+ readFileSync(
40
+ path.join(path.dirname(fileURLToPath(import.meta.url)), "host-libs.json"),
41
+ "utf8",
42
+ ),
43
+ );
44
+
45
+ /**
46
+ * Emit dist/cb-manifest.json alongside the bundle: the host libs this
47
+ * build actually externalized (the storefront preloads the lazy ones
48
+ * BEFORE injecting index.js) plus the app's data-connection
49
+ * declarations copied from config.json. The manifest travels with the
50
+ * artifact — versioned by uploadId like index.js itself — so nothing
51
+ * app-side needs to be written to Firestore, and a store pinned to an
52
+ * old upload gets exactly that upload's declarations.
53
+ */
54
+ function cbManifestPlugin() {
55
+ return {
56
+ name: "cb-emit-manifest",
57
+ generateBundle(_options, bundle) {
58
+ // The chunk's own `imports` lists exactly the modules rollup left
59
+ // external — the ground truth of what the artifact expects the
60
+ // host to provide (a resolveId hook can't see them: rollup
61
+ // matches the `external` option before plugins run).
62
+ const used = new Set();
63
+ for (const output of Object.values(bundle)) {
64
+ if (output.type !== "chunk") continue;
65
+ for (const spec of [...output.imports, ...output.dynamicImports]) {
66
+ used.add(spec);
67
+ }
68
+ }
69
+ let connections = [];
70
+ let declaredHostLibs = [];
71
+ try {
72
+ const configPath = path.resolve(process.cwd(), "config.json");
73
+ if (existsSync(configPath)) {
74
+ const config = JSON.parse(readFileSync(configPath, "utf8"));
75
+ if (Array.isArray(config.connections)) {
76
+ connections = config.connections.filter(
77
+ (c) => c && typeof c === "object" && typeof c.name === "string",
78
+ );
79
+ }
80
+ }
81
+ const manifestPath = path.resolve(process.cwd(), "commercebuild.json");
82
+ if (existsSync(manifestPath)) {
83
+ const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
84
+ // Optional explicit override for anything the resolve hook
85
+ // can't see (kept for parity with the editor compiler).
86
+ if (Array.isArray(manifest.hostLibs)) {
87
+ declaredHostLibs = manifest.hostLibs.filter(
88
+ (s) => typeof s === "string",
89
+ );
90
+ }
91
+ }
92
+ } catch {
93
+ // A broken config fails the type-check/build on its own terms.
94
+ }
95
+ this.emitFile({
96
+ type: "asset",
97
+ fileName: "cb-manifest.json",
98
+ source:
99
+ JSON.stringify(
100
+ {
101
+ cbApiVersion,
102
+ hostLibs: [...new Set([...used, ...declaredHostLibs])].sort(),
103
+ connections,
104
+ },
105
+ null,
106
+ 2,
107
+ ) + "\n",
108
+ });
109
+ },
110
+ };
111
+ }
112
+
113
+ // Packages that must never be bundled OR externalized — each would
114
+ // silently conflict with a host lib at runtime (e.g. firestore lite
115
+ // registers the same 'firestore' component as the full SDK, and
116
+ // whichever loads first wins with no error).
117
+ function forbiddenImportsPlugin() {
118
+ const forbidden = new Set(hostLibs.forbidden ?? []);
119
+ return {
120
+ name: "cb-forbidden-imports",
121
+ enforce: "pre",
122
+ resolveId(source) {
123
+ if (forbidden.has(source)) {
124
+ throw new Error(
125
+ `"${source}" cannot be used in a commercebuild extension — ` +
126
+ `the host provides a conflicting implementation. ` +
127
+ `Import the host-provided module instead (see host-libs.json).`,
128
+ );
129
+ }
130
+ return null;
131
+ },
132
+ };
133
+ }
35
134
 
36
135
  const jsEntry = path.resolve(process.cwd(), "src/index.js");
37
136
  const tsEntry = path.resolve(process.cwd(), "src/index.ts");
@@ -48,13 +147,10 @@ const baseBuildConfig = {
48
147
  minify: "esbuild",
49
148
  sourcemap: false,
50
149
  rollupOptions: {
51
- external: ["react", "react-dom"],
150
+ external: hostLibs.external,
52
151
  output: {
53
152
  banner: cbMetaBanner,
54
- globals: {
55
- react: "cb.lib.React",
56
- "react-dom": "cb.lib.ReactDom",
57
- },
153
+ globals: hostLibs.globals,
58
154
  assetFileNames: (assetInfo) => {
59
155
  if (assetInfo.name.endsWith(".css")) {
60
156
  return assetInfo.originalFileName || assetInfo.name || "asset.css";
@@ -67,6 +163,8 @@ const baseBuildConfig = {
67
163
 
68
164
  const prodConfig = {
69
165
  plugins: [
166
+ forbiddenImportsPlugin(),
167
+ cbManifestPlugin(),
70
168
  react({
71
169
  jsxRuntime: "classic",
72
170
  }),
@@ -106,6 +204,8 @@ export default defineConfig(({ mode, command }) => {
106
204
  return {
107
205
  root: ".",
108
206
  plugins: [
207
+ forbiddenImportsPlugin(),
208
+ cbManifestPlugin(),
109
209
  ViteHMRNotifierPlugin(),
110
210
  react({
111
211
  jsxRuntime: "classic",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercebuild/extension",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "types": "./types/index.d.ts",
5
5
  "exports": {
6
6
  ".": {
@@ -22,6 +22,7 @@
22
22
  "@vitejs/plugin-react": "^4.5.2",
23
23
  "chalk": "^5.4.1",
24
24
  "esbuild": "^0.25.6",
25
+ "firebase": "^12.2.1",
25
26
  "lucide-react": "^0.525.0",
26
27
  "react": "^19.1.0",
27
28
  "react-dom": "^19.1.0",
@@ -34,4 +35,4 @@
34
35
  "bin": {
35
36
  "commercebuild-extension": "./scripts/cli.js"
36
37
  }
37
- }
38
+ }
@@ -0,0 +1,113 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "title": "commercebuild extension configuration",
4
+ "description": "Data-connection declarations (config.json) or development overrides (config.development.json — never deployed, matched to declarations by name).",
5
+ "type": "object",
6
+ "properties": {
7
+ "$schema": {
8
+ "type": "string"
9
+ },
10
+ "connections": {
11
+ "type": "array",
12
+ "description": "Named data connections. In config.json each entry declares a connection; in config.development.json each entry overrides the declaration with the same name (scratch names may be introduced for testing).",
13
+ "items": {
14
+ "$ref": "#/$defs/connection"
15
+ }
16
+ }
17
+ },
18
+ "additionalProperties": false,
19
+ "$defs": {
20
+ "connection": {
21
+ "type": "object",
22
+ "required": [
23
+ "name"
24
+ ],
25
+ "properties": {
26
+ "name": {
27
+ "type": "string",
28
+ "pattern": "^[A-Za-z][A-Za-z0-9_-]*$",
29
+ "description": "Connection name the app code passes to cb.utils.useFirebaseApp(name). With exactly one connection configured, useFirebaseApp() with no argument resolves it; otherwise the no-argument form looks for \"default\"."
30
+ },
31
+ "provider": {
32
+ "description": "Backing service. Absent means \"firebase\".",
33
+ "enum": [
34
+ "firebase"
35
+ ]
36
+ },
37
+ "scope": {
38
+ "description": "\"store\" (the default): the merchant supplies the config per store under Installed Apps → Configure data — do not put a config here. \"app\": the config is fixed in this declaration (required below), owned by the app and not merchant-overridable.",
39
+ "enum": [
40
+ "store",
41
+ "app"
42
+ ],
43
+ "default": "store"
44
+ },
45
+ "label": {
46
+ "type": "string",
47
+ "description": "Shown as the connection's title in the merchant's config form."
48
+ },
49
+ "description": {
50
+ "type": "string",
51
+ "description": "Shown under the label in the merchant's config form."
52
+ },
53
+ "config": {
54
+ "$ref": "#/$defs/firebaseConfig"
55
+ }
56
+ },
57
+ "additionalProperties": false,
58
+ "allOf": [
59
+ {
60
+ "if": {
61
+ "properties": {
62
+ "scope": {
63
+ "const": "app"
64
+ }
65
+ },
66
+ "required": [
67
+ "scope"
68
+ ]
69
+ },
70
+ "then": {
71
+ "required": [
72
+ "config"
73
+ ]
74
+ }
75
+ }
76
+ ]
77
+ },
78
+ "firebaseConfig": {
79
+ "type": "object",
80
+ "description": "Firebase web config (the firebaseConfig snippet from the Firebase console). Public by design — access control lives in the project's Security Rules.",
81
+ "required": [
82
+ "apiKey",
83
+ "projectId"
84
+ ],
85
+ "properties": {
86
+ "apiKey": {
87
+ "type": "string",
88
+ "minLength": 1
89
+ },
90
+ "projectId": {
91
+ "type": "string",
92
+ "minLength": 1
93
+ },
94
+ "authDomain": {
95
+ "type": "string"
96
+ },
97
+ "storageBucket": {
98
+ "type": "string"
99
+ },
100
+ "messagingSenderId": {
101
+ "type": "string"
102
+ },
103
+ "appId": {
104
+ "type": "string"
105
+ },
106
+ "measurementId": {
107
+ "type": "string"
108
+ }
109
+ },
110
+ "additionalProperties": true
111
+ }
112
+ }
113
+ }
package/types/global.d.ts CHANGED
@@ -6,6 +6,8 @@
6
6
  import * as React from "react";
7
7
  import React__default, { CSSProperties } from "react";
8
8
  import * as ReactDOM from "react-dom";
9
+ import * as FirebaseAppNs from "firebase/app";
10
+ import * as FirebaseFirestoreNs from "firebase/firestore";
9
11
  import { cart_v2, catalog_v1, Platform } from "@commercebuild/platform-api";
10
12
  import * as icons from "lucide-react";
11
13
  import * as uis from "@commercebuild/ui";
@@ -283,6 +285,37 @@ declare function revalidatePath(
283
285
  ): Promise<void>;
284
286
  declare function revalidate(): Promise<void>;
285
287
 
288
+ /**
289
+ * Per-extension render scope.
290
+ *
291
+ * Extension code has no runtime identity of its own — components are
292
+ * bare functions and `cb` is one page-global object, while a single
293
+ * React tree can hold components from several different extensions at
294
+ * once. This context supplies that identity lexically: the host wraps
295
+ * every extension render site (renderExtensionComponent's boundary and
296
+ * the extension page route) in a provider carrying the registration id
297
+ * and the extension's resolved per-store configuration. Contract hooks
298
+ * like cb.utils.useFirebaseApp() read it, so the same component gets
299
+ * its own extension's connections wherever it renders — including the
300
+ * synthetic "preview" / "local" registrations, which just carry their
301
+ * own scope value.
302
+ */
303
+
304
+ /**
305
+ * Merchant-supplied Firebase web config (the Firebase-console snippet
306
+ * subset). Not a secret — it ships to every browser by design; access
307
+ * control lives entirely in the merchant's Firestore Security Rules.
308
+ */
309
+ interface CbFirebaseConfig {
310
+ apiKey: string;
311
+ projectId: string;
312
+ authDomain?: string;
313
+ storageBucket?: string;
314
+ messagingSenderId?: string;
315
+ appId?: string;
316
+ measurementId?: string;
317
+ }
318
+
286
319
  /**
287
320
  * The extension host API contract (`window.cb`).
288
321
  *
@@ -427,6 +460,17 @@ type CbImage = React.ForwardRefExoticComponent<
427
460
  lazyRoot?: string;
428
461
  } & React.RefAttributes<HTMLImageElement | null>
429
462
  >;
463
+
464
+ /**
465
+ * Import specifier → module namespace for every runtime host lib, so
466
+ * `cb.requireLib("firebase/firestore")` returns the fully typed module.
467
+ */
468
+ interface CbHostLibs {
469
+ react: typeof React;
470
+ "react-dom": typeof ReactDOM;
471
+ "firebase/app": typeof FirebaseAppNs;
472
+ "firebase/firestore": typeof FirebaseFirestoreNs;
473
+ }
430
474
  interface CbHostApi {
431
475
  /** Host-contract semver — see docs/extension-host-api/versioning.md. */
432
476
  version: string;
@@ -435,12 +479,28 @@ interface CbHostApi {
435
479
  * shares the host's single instance instead of bundling its own (critical for
436
480
  * React — two instances break hooks). The extension build maps
437
481
  * `import ... from "react"` → `cb.lib.React` and `"react-dom"` →
438
- * `cb.lib.ReactDom`.
482
+ * `cb.lib.ReactDOM`.
439
483
  */
440
484
  lib: {
441
485
  React: typeof React;
442
- ReactDom: typeof ReactDOM;
486
+ ReactDOM: typeof ReactDOM;
487
+ /**
488
+ * Lazy host libs, bound by the extension build — do not read
489
+ * directly. The host import()s each one before injecting a bundle
490
+ * that declared it; until then a member read throws a descriptive
491
+ * error naming the missing lib. Use `cb.requireLib(...)` for an
492
+ * explicit, typed lookup.
493
+ */
494
+ firebase: {
495
+ app: typeof FirebaseAppNs;
496
+ firestore: typeof FirebaseFirestoreNs;
497
+ };
443
498
  };
499
+ /**
500
+ * The module namespace for a runtime host lib, throwing a descriptive
501
+ * error when it is not loaded (the extension didn't declare it).
502
+ */
503
+ requireLib: <S extends keyof CbHostLibs>(spec: S) => CbHostLibs[S];
444
504
  /** The platform (commerce backend) API SDK client. */
445
505
  platform: Platform;
446
506
  /**
@@ -459,6 +519,23 @@ interface CbHostApi {
459
519
  revalidate: typeof revalidate;
460
520
  revalidateTag: typeof revalidateTag;
461
521
  revalidatePath: typeof revalidatePath;
522
+ /**
523
+ * React hook: connection names the store configured for the current
524
+ * extension (via storeadmin → Installed Apps → Configure data).
525
+ * Empty outside an extension subtree.
526
+ */
527
+ useFirebaseConnections: () => string[];
528
+ /**
529
+ * React hook: the host-initialized FirebaseApp for a named
530
+ * connection, or undefined when it is not configured — always
531
+ * render a fallback for that case. With no argument: the store's
532
+ * only connection when exactly one is configured, else the one
533
+ * named "default". Never call initializeApp yourself; pass this app
534
+ * to the product entry points (e.g. getFirestore(app)).
535
+ */
536
+ useFirebaseApp: (
537
+ connection?: string,
538
+ ) => FirebaseAppNs.FirebaseApp | undefined;
462
539
  };
463
540
  com: {
464
541
  Cart: {
@@ -521,4 +598,4 @@ declare global {
521
598
  }
522
599
  }
523
600
 
524
- export type { CbHostApi, CbHostDeps, CbRouter };
601
+ export type { CbFirebaseConfig, CbHostApi, CbHostDeps, CbHostLibs, CbRouter };