@hubspot/cms-dev-server 0.6.3 → 0.6.5

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/run.js CHANGED
@@ -61,7 +61,8 @@ import { readFileSync, existsSync, readdirSync, lstatSync } from "fs";
61
61
  import * as jsxRuntime from "react/jsx-runtime";
62
62
  import stream from "stream";
63
63
  import { renderToPipeableStream, renderToStaticMarkup } from "react-dom/server";
64
- import { createServerRenderContextV2, ServerRenderContext } from "@hubspot/cms-components";
64
+ import * as cmsComponents from "@hubspot/cms-components";
65
+ import { ServerRenderContext } from "@hubspot/cms-components";
65
66
  import { createHash } from "crypto";
66
67
  import { join } from "path/posix";
67
68
  import { cloneElement } from "react";
@@ -140,18 +141,46 @@ function getIslandUrls(roots, viteDevServer) {
140
141
  return tweakedIsland;
141
142
  });
142
143
  }
144
+ function getPerServerRequestUrls(perServerRequestData, viteDevServer) {
145
+ if (!perServerRequestData.has("sharedStateIDs")) {
146
+ return new Map(perServerRequestData);
147
+ }
148
+ const perServerRequestDataWithUrls = new Map(
149
+ perServerRequestData
150
+ );
151
+ perServerRequestData.get("sharedStateIDs").forEach((id) => {
152
+ const reducer = perServerRequestData.get(id);
153
+ const reducerWithUrl = {
154
+ ...reducer,
155
+ reducerFuncModuleUrl: viteDevServer.moduleGraph.getModuleById(
156
+ reducer.reducerFuncModuleId
157
+ ).url
158
+ };
159
+ perServerRequestDataWithUrls.set(id, reducerWithUrl);
160
+ });
161
+ return perServerRequestDataWithUrls;
162
+ }
143
163
  var createIslandsGlobalJsForClientInDev = (roots, perServerRequestData, viteDevServer, indent) => {
144
164
  const islands = getIslandUrls(roots, viteDevServer);
165
+ const perServerRequestDataWithUrls = getPerServerRequestUrls(
166
+ perServerRequestData,
167
+ viteDevServer
168
+ );
145
169
  return `window.__islands = ${serializeIslandDataToJSON(
146
170
  islands,
147
171
  indent
148
- )}${writeSharedStateToJsGlobal(islands, perServerRequestData, indent)}`;
172
+ )}${writeSharedStateToJsGlobal(
173
+ islands,
174
+ perServerRequestDataWithUrls,
175
+ indent
176
+ )}`;
149
177
  };
150
178
  var ENABLED_FEATURES_GATE_MAP = {
151
179
  "cms:jsr:sharedDeps": "CMS:JSRenderer:SharedDeps",
152
180
  "cms:jsr:getServerSideProps": "CMS:JSRenderer:GetServerSideProps",
153
181
  "cms:jsr:localProxySchemaVersion1": "CMS:JSRenderer:LocalProxySchemaVersion1",
154
- "cms:jsr:graphqQLQueryBuild": "CMS:JSRenderer:GraphQLQueryBuild"
182
+ "cms:jsr:graphqQLQueryBuild": "CMS:JSRenderer:GraphQLQueryBuild",
183
+ "cms:jsr:separateIslandRenderOption": "CMS:JSRenderer:SeparateIslandRenderOption"
155
184
  };
156
185
  function isUngatedFor(gates, gate) {
157
186
  return gates.includes(gate);
@@ -289,10 +318,10 @@ ${groupEnd}</script>
289
318
  `;
290
319
  };
291
320
  async function renderOnServer(elements, pageUrl, environmentConfig, entryPointBasePath, debugName, renderOptions) {
292
- const renderContext = createServerRenderContextV2(pageUrl, {
321
+ const renderContext = cmsComponents.createServerRenderContextV2 ? cmsComponents.createServerRenderContextV2(pageUrl, {
293
322
  environmentConfig,
294
323
  entryPointBasePath
295
- });
324
+ }) : cmsComponents.createServerRenderContext(pageUrl, environmentConfig);
296
325
  let html = await renderStreamToPromise(
297
326
  /* @__PURE__ */ jsx2(ServerRenderContext.Provider, { value: renderContext, children: elements }),
298
327
  renderOptions
@@ -612,7 +641,10 @@ var generateIslandSetupScriptBlock = (gates) => {
612
641
  initConfigSingletonFromJSON(window.__hsEnvConfig);
613
642
  </script>
614
643
  <script type="module">
615
- import { setupIslandHydration } from '@hubspot/cms-components/island-runtime-shared';
644
+ import { setupIslandHydration } from '${packagePath(
645
+ import.meta.url,
646
+ "island-runtime"
647
+ )}';
616
648
  setupIslandHydration();
617
649
  </script>
618
650
  `;
@@ -862,6 +894,34 @@ import { normalizePath as normalizePath3 } from "vite";
862
894
  // src/lib/renderModule.ts
863
895
  import { join as joinPosix } from "path/posix";
864
896
  import { traverseFieldsJSON } from "@hubspot/cms-components/fields";
897
+
898
+ // src/lib/fetchModuleGraphQLData.ts
899
+ import cliHttp from "@hubspot/cli-lib/http.js";
900
+ var graphQLCache = /* @__PURE__ */ new Map();
901
+ async function fetchModuleGraphQLData(accountId, query, requestedModulePath, pageUrl) {
902
+ const { searchParams } = new URL(pageUrl);
903
+ const hsLocalQueryKey = searchParams.get("hsLocalQueryKey");
904
+ const compositeQueryKey = `${hsLocalQueryKey}:${query}`;
905
+ let dataQueryResult;
906
+ if (graphQLCache.has(compositeQueryKey)) {
907
+ console.log(`Using cached GraphQL Query for ${requestedModulePath}`);
908
+ return Promise.resolve(graphQLCache.get(compositeQueryKey));
909
+ }
910
+ try {
911
+ dataQueryResult = await cliHttp.post(accountId, {
912
+ uri: "/collector/v3/graphql",
913
+ body: {
914
+ query
915
+ }
916
+ });
917
+ } catch (e) {
918
+ throw new Error(e);
919
+ }
920
+ graphQLCache.set(compositeQueryKey, dataQueryResult);
921
+ return dataQueryResult;
922
+ }
923
+
924
+ // src/lib/renderModule.ts
865
925
  var lookForModuleInFolder = async (modulePath, viteServer) => {
866
926
  let componentModule;
867
927
  const insideFolderPath = joinPosix(modulePath, "index");
@@ -882,6 +942,7 @@ async function renderModule(viteServer, requestedModulePath, fieldOverrides = {}
882
942
  );
883
943
  let serverSideProps;
884
944
  let componentModule;
945
+ let dataQueryResult;
885
946
  let foundPath;
886
947
  if (!hasComponentExtension(requestedModulePath)) {
887
948
  [componentModule, foundPath] = await lookForModuleInFolder(
@@ -921,11 +982,32 @@ async function renderModule(viteServer, requestedModulePath, fieldOverrides = {}
921
982
  console.error(e);
922
983
  }
923
984
  }
985
+ if (componentModule.query && typeof componentModule.query === "string") {
986
+ try {
987
+ dataQueryResult = await fetchModuleGraphQLData(
988
+ environmentConfig.getHubID(),
989
+ componentModule.query,
990
+ requestedModulePath,
991
+ pageUrl
992
+ );
993
+ if (dataQueryResult.errors && dataQueryResult.errors.length) {
994
+ dataQueryResult.errors.forEach((graphQLError) => {
995
+ console.error(graphQLError);
996
+ });
997
+ console.error("Your GraphQL query resulted in the above errors");
998
+ }
999
+ } catch (error) {
1000
+ console.error(error.message);
1001
+ console.error(
1002
+ `Failed to execute query for ${requestedModulePath}. Check the above error message for more information.`
1003
+ );
1004
+ }
1005
+ }
924
1006
  return renderComponentWithCSS_default(
925
1007
  viteServer,
926
1008
  foundPath,
927
1009
  Component,
928
- { ...props, ...serverSideProps },
1010
+ { ...props, ...serverSideProps, dataQueryResult },
929
1011
  pageUrl,
930
1012
  basePath,
931
1013
  environmentConfig,
@@ -2142,7 +2224,7 @@ async function createDevServer(webRoot2, storybook2, hubspotCliConfigPathOption2
2142
2224
  );
2143
2225
  let authedPortalId = null;
2144
2226
  let authedEnv = null;
2145
- let jsRenderingGates = [];
2227
+ let jsRenderingGates = ["CMS:JSRenderer:SharedDeps"];
2146
2228
  let personalAccessKey = null;
2147
2229
  if (hubspotCliConfigPathOption2 && hubspotCliAccountNameOption2) {
2148
2230
  console.log(
@@ -2259,7 +2341,9 @@ Local proxy functionality will not work without setting up authentication.`
2259
2341
  httpServer.listen(PORT);
2260
2342
  httpServers.push(httpServer);
2261
2343
  }
2262
- startShadowDevServer(PORT, SHADOW_PORT, projectName, jsRenderingGates);
2344
+ if (!process.env.IN_CODESANDBOX) {
2345
+ startShadowDevServer(PORT, SHADOW_PORT, projectName, jsRenderingGates);
2346
+ }
2263
2347
  let extraServerInfo = "";
2264
2348
  const protocol = sslEnabledOption2 ? "https" : "http";
2265
2349
  if (authedPortalId) {
@@ -59,6 +59,15 @@ function getFieldsArgTypes(fieldsJson, currentPathSegments = []) {
59
59
  };
60
60
  break;
61
61
  }
62
+ case "choice": {
63
+ const options = field.choices ? field.choices.map((choice) => choice[1]) : [];
64
+ resultArgTypes[fullFieldName] = {
65
+ control: "select",
66
+ defaultValue: field.default,
67
+ options
68
+ };
69
+ break;
70
+ }
62
71
  default: {
63
72
  resultArgTypes[fullFieldName] = {
64
73
  control: "object",
@@ -52,7 +52,8 @@ import { readFileSync, existsSync, readdirSync, lstatSync } from "fs";
52
52
  import * as jsxRuntime from "react/jsx-runtime";
53
53
  import stream from "stream";
54
54
  import { renderToPipeableStream, renderToStaticMarkup } from "react-dom/server";
55
- import { createServerRenderContextV2, ServerRenderContext } from "@hubspot/cms-components";
55
+ import * as cmsComponents from "@hubspot/cms-components";
56
+ import { ServerRenderContext } from "@hubspot/cms-components";
56
57
  import { createHash } from "crypto";
57
58
  import { join } from "path/posix";
58
59
  import { cloneElement } from "react";
@@ -93,6 +94,25 @@ function getIslandUrls(roots, viteDevServer) {
93
94
  return tweakedIsland;
94
95
  });
95
96
  }
97
+ function getPerServerRequestUrls(perServerRequestData, viteDevServer) {
98
+ if (!perServerRequestData.has("sharedStateIDs")) {
99
+ return new Map(perServerRequestData);
100
+ }
101
+ const perServerRequestDataWithUrls = new Map(
102
+ perServerRequestData
103
+ );
104
+ perServerRequestData.get("sharedStateIDs").forEach((id) => {
105
+ const reducer = perServerRequestData.get(id);
106
+ const reducerWithUrl = {
107
+ ...reducer,
108
+ reducerFuncModuleUrl: viteDevServer.moduleGraph.getModuleById(
109
+ reducer.reducerFuncModuleId
110
+ ).url
111
+ };
112
+ perServerRequestDataWithUrls.set(id, reducerWithUrl);
113
+ });
114
+ return perServerRequestDataWithUrls;
115
+ }
96
116
  function isUngatedFor(gates, gate) {
97
117
  return gates.includes(gate);
98
118
  }
@@ -188,10 +208,10 @@ async function renderComponentOnServer(Component, props, pageUrl, environmentCon
188
208
  );
189
209
  }
190
210
  async function renderOnServer(elements, pageUrl, environmentConfig, entryPointBasePath, debugName, renderOptions) {
191
- const renderContext = createServerRenderContextV2(pageUrl, {
211
+ const renderContext = cmsComponents.createServerRenderContextV2 ? cmsComponents.createServerRenderContextV2(pageUrl, {
192
212
  environmentConfig,
193
213
  entryPointBasePath
194
- });
214
+ }) : cmsComponents.createServerRenderContext(pageUrl, environmentConfig);
195
215
  let html = await renderStreamToPromise(
196
216
  /* @__PURE__ */ jsx2(ServerRenderContext.Provider, { value: renderContext, children: elements }),
197
217
  renderOptions
@@ -397,10 +417,18 @@ ${indentString}`);
397
417
  };
398
418
  createIslandsGlobalJsForClientInDev = (roots, perServerRequestData, viteDevServer, indent) => {
399
419
  const islands = getIslandUrls(roots, viteDevServer);
420
+ const perServerRequestDataWithUrls = getPerServerRequestUrls(
421
+ perServerRequestData,
422
+ viteDevServer
423
+ );
400
424
  return `window.__islands = ${serializeIslandDataToJSON(
401
425
  islands,
402
426
  indent
403
- )}${writeSharedStateToJsGlobal(islands, perServerRequestData, indent)}`;
427
+ )}${writeSharedStateToJsGlobal(
428
+ islands,
429
+ perServerRequestDataWithUrls,
430
+ indent
431
+ )}`;
404
432
  };
405
433
  GATES = [
406
434
  "CMS:JSRenderer:SharedDeps",
@@ -413,7 +441,8 @@ ${indentString}`);
413
441
  "cms:jsr:sharedDeps": "CMS:JSRenderer:SharedDeps",
414
442
  "cms:jsr:getServerSideProps": "CMS:JSRenderer:GetServerSideProps",
415
443
  "cms:jsr:localProxySchemaVersion1": "CMS:JSRenderer:LocalProxySchemaVersion1",
416
- "cms:jsr:graphqQLQueryBuild": "CMS:JSRenderer:GraphQLQueryBuild"
444
+ "cms:jsr:graphqQLQueryBuild": "CMS:JSRenderer:GraphQLQueryBuild",
445
+ "cms:jsr:separateIslandRenderOption": "CMS:JSRenderer:SeparateIslandRenderOption"
417
446
  };
418
447
  BUILD_INFO_PATH = "/build-info.json";
419
448
  clientRenderIsland = "import type { RenderableIsland } from '@hubspot/cms-components';\nimport {\n ClientRenderContextProvider,\n buildIslandIdentifierPrefix,\n} from '@hubspot/cms-components';\nimport { Suspense } from 'react';\nimport ReactDOMClient from 'react-dom/client';\n\nexport function clientRenderIsland(\n renderableIsland: RenderableIsland,\n Component: any\n) {\n const { island, rootElement, postHydration, pageUrl, islands, basePath } =\n renderableIsland;\n let reactRoot: any;\n\n const rootOptions = {\n // Ensure useId is unique across island roots\n identifierPrefix: buildIslandIdentifierPrefix(renderableIsland.island.id),\n };\n\n const elementsToHydrate = (\n <Suspense fallback={`client hydrate fail/waiting for ${island.id}`}>\n <ClientRenderContextProvider\n pageUrl={pageUrl}\n islands={islands}\n onTopLevelHydrationFinish={postHydration}\n island={island}\n basePath={basePath}\n >\n <Component islandId={island.id} {...island.props} />\n </ClientRenderContextProvider>\n </Suspense>\n );\n\n if (island.clientOnly) {\n reactRoot = ReactDOMClient.createRoot(rootElement, rootOptions);\n reactRoot.render(elementsToHydrate);\n } else {\n reactRoot = ReactDOMClient.hydrateRoot(\n rootElement,\n elementsToHydrate,\n rootOptions\n );\n }\n return reactRoot;\n}\n";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hubspot/cms-dev-server",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "engines": {
@@ -20,11 +20,12 @@
20
20
  "runBuild": "node dist/run.js",
21
21
  "start": "ts-node-esm --transpileOnly runWhenDeveloping.ts",
22
22
  "start-inspect": "node --loader ts-node/esm --inspect runWhenDeveloping.ts",
23
- "test": "NODE_ENV=test vitest run"
23
+ "test": "NODE_ENV=test vitest run",
24
+ "test:watch": "NODE_ENV=test vitest watch"
24
25
  },
25
26
  "dependencies": {
26
27
  "@hubspot/cli-lib": "^3.0.12",
27
- "@hubspot/cms-components": "^0.6.3",
28
+ "@hubspot/cms-components": "^0.6.5",
28
29
  "@storybook/addon-actions": "7.0.7",
29
30
  "@storybook/addon-essentials": "7.0.7",
30
31
  "@storybook/addon-interactions": "7.0.7",
@@ -43,7 +44,7 @@
43
44
  "react-dom": "^18.1.0",
44
45
  "request": "^2.88.2",
45
46
  "typescript": "~4.7.0",
46
- "vite": "4.2.2",
47
+ "vite": "4.3.4",
47
48
  "vite-plugin-inspect": "^0.4.3",
48
49
  "vite-plugin-mkcert": "^1.10.1"
49
50
  },
@@ -96,5 +97,5 @@
96
97
  "publishConfig": {
97
98
  "access": "public"
98
99
  },
99
- "gitHead": "7bfea5c5f3b209544fd2fed9c7f98cdba01fb62d"
100
+ "gitHead": "08f0ed34d2c4f3c7ddf6cec03b6e07d83ff46922"
100
101
  }
@@ -0,0 +1,2 @@
1
+ import { setupIslandHydration } from '@hubspot/cms-js-internal/island-runtime';
2
+ export { setupIslandHydration };
@@ -5,6 +5,7 @@ export declare type ArgTypes = {
5
5
  [fieldPath: string]: {
6
6
  control: string;
7
7
  defaultValue: any;
8
+ options?: any;
8
9
  };
9
10
  };
10
11
  export declare type ModuleStory = {
Binary file