@hubspot/cms-dev-server 0.3.2 → 0.3.3
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/createDevServer.js +4 -0
- package/dist/createViteDevServer.js +1 -1
- package/dist/lib/renderComponentWithCSS.js +2 -2
- package/dist/lib/renderModule.js +2 -2
- package/dist/lib/renderPartial.js +6 -3
- package/dist/lib/templates.js +23 -14
- package/dist/proxyPage/fetchContentMetadata.js +10 -4
- package/dist/proxyPage/proxyPage.js +11 -7
- package/dist/proxyPage/utils.js +2 -2
- package/dist/routes/homeRouteHandler.js +1 -1
- package/dist/routes/linksToAllModulesRouteHandler.js +1 -1
- package/dist/routes/linksToAllPartialsRouteHandler.js +1 -1
- package/dist/routes/module.js +4 -2
- package/dist/routes/partial.js +4 -2
- package/dist/routes/proxyPageRouteHandler.js +2 -2
- package/dist/storybook/getFieldsArgTypes.js +54 -0
- package/dist/storybook/getFieldsArgTypes.test.js +91 -0
- package/dist/storybook/getNestedFieldObjectForArgs.js +22 -0
- package/dist/storybook/getNestedFieldObjectForArgs.test.js +40 -0
- package/dist/storybook/index.js +29 -22
- package/dist/storybook/main.js +3 -0
- package/dist/storybook/moduleStory.js +4 -92
- package/dist/storybook/types.js +1 -0
- package/package.json +6 -6
- package/types/storybook/getFieldsArgTypes.d.ts +9 -0
- package/types/storybook/getNestedFieldObjectForArgs.d.ts +6 -0
- package/types/storybook/moduleStory.d.ts +0 -10
- package/types/storybook/types.d.ts +3 -0
package/dist/createDevServer.js
CHANGED
|
@@ -43,6 +43,7 @@ If needed you can refer to another config file with --config.
|
|
|
43
43
|
|
|
44
44
|
Local proxy functionality will not work without setting up authentication.`);
|
|
45
45
|
}
|
|
46
|
+
const authedHublet = 'na1';
|
|
46
47
|
const absoluteWebRoot = normalizePath(path.resolve(process.cwd(), webRoot));
|
|
47
48
|
const [__webRootProjectFolder, webRootProjectComponentFolder] = absoluteWebRoot.replace(/\/$/, '').split('/').slice(-2);
|
|
48
49
|
const expressServer = express();
|
|
@@ -51,6 +52,9 @@ Local proxy functionality will not work without setting up authentication.`);
|
|
|
51
52
|
expressServer.locals.devServerPort = PORT;
|
|
52
53
|
expressServer.locals.webRoot = webRoot;
|
|
53
54
|
expressServer.locals.viteServer = viteServer;
|
|
55
|
+
expressServer.locals.authedPortalId = authedPortalId;
|
|
56
|
+
expressServer.locals.authedEnv = authedEnv;
|
|
57
|
+
expressServer.locals.authedHublet = authedHublet;
|
|
54
58
|
expressServer.locals.storybookEnabled = storybook;
|
|
55
59
|
expressServer.locals.storybookPort = STORYBOOK_PORT;
|
|
56
60
|
expressServer.locals.projectName = getProjectNameFromHSConfig(path.join(webRoot, '../hsproject.json'));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createServer as createViteServer
|
|
1
|
+
import { createServer as createViteServer } from 'vite';
|
|
2
2
|
import { default as react } from '@vitejs/plugin-react';
|
|
3
3
|
import { resolveIslandPlugin } from '@hubspot/cms-js-internal';
|
|
4
4
|
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { hasComponentExtension, removeComponentExtension, } from '@hubspot/cms-js-internal';
|
|
2
2
|
import { findFullPathInModuleGraph } from './moduleGraph.js';
|
|
3
|
-
async function renderComponentWithCSS(viteServer, componentPath, Component, props, pageUrl) {
|
|
3
|
+
async function renderComponentWithCSS(viteServer, componentPath, Component, props, pageUrl, environmentConfig) {
|
|
4
4
|
// Need to make sure rendering happens in Vite SSR context
|
|
5
5
|
const { renderComponentOnServer, collectCssInDev } = await viteServer.ssrLoadModule('@hubspot/cms-js-internal');
|
|
6
6
|
let componentPathWithExtension;
|
|
@@ -13,7 +13,7 @@ async function renderComponentWithCSS(viteServer, componentPath, Component, prop
|
|
|
13
13
|
else {
|
|
14
14
|
componentPathNoExtension = componentPath;
|
|
15
15
|
}
|
|
16
|
-
const { html, renderContext } = await renderComponentOnServer(Component, props, pageUrl);
|
|
16
|
+
const { html, renderContext } = await renderComponentOnServer(Component, props, pageUrl, environmentConfig);
|
|
17
17
|
if (!componentPathWithExtension) {
|
|
18
18
|
componentPathWithExtension = findFullPathInModuleGraph(componentPathNoExtension, viteServer.moduleGraph);
|
|
19
19
|
if (!componentPathWithExtension) {
|
package/dist/lib/renderModule.js
CHANGED
|
@@ -15,7 +15,7 @@ const lookForModuleInFolder = async (modulePath, viteServer) => {
|
|
|
15
15
|
}
|
|
16
16
|
return [componentModule, insideFolderPath];
|
|
17
17
|
};
|
|
18
|
-
async function renderModule(viteServer, requestedModulePath, fieldOverrides = {}, pageUrl) {
|
|
18
|
+
async function renderModule(viteServer, requestedModulePath, fieldOverrides = {}, pageUrl, environmentConfig) {
|
|
19
19
|
console.log('Rendering module', requestedModulePath);
|
|
20
20
|
let componentModule;
|
|
21
21
|
let foundPath;
|
|
@@ -44,7 +44,7 @@ async function renderModule(viteServer, requestedModulePath, fieldOverrides = {}
|
|
|
44
44
|
}
|
|
45
45
|
const fieldsJSON = renderFieldsToJson(fields);
|
|
46
46
|
const props = getPropsForModule(fieldsJSON, fieldOverrides);
|
|
47
|
-
return renderComponentWithCSS(viteServer, foundPath, Component, props, pageUrl);
|
|
47
|
+
return renderComponentWithCSS(viteServer, foundPath, Component, props, pageUrl, environmentConfig);
|
|
48
48
|
}
|
|
49
49
|
function getPropsForModule(fieldsJSON, fieldOverrides = {}) {
|
|
50
50
|
const props = {};
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import renderComponentWithCSS from './renderComponentWithCSS.js';
|
|
2
2
|
import { logPartialInfo } from './renderHelpers.js';
|
|
3
|
-
async function renderPartial(viteServer, partialPath, props, pageUrl) {
|
|
3
|
+
async function renderPartial(viteServer, partialPath, props, pageUrl, environmentConfig) {
|
|
4
4
|
logPartialInfo(partialPath, props);
|
|
5
5
|
const componentModule = await viteServer.ssrLoadModule(partialPath);
|
|
6
|
-
const Component = componentModule.default;
|
|
7
6
|
if (!componentModule) {
|
|
8
7
|
throw new Error(`Couldn't find partial: ${partialPath}`);
|
|
9
8
|
}
|
|
10
|
-
|
|
9
|
+
const Component = componentModule.default;
|
|
10
|
+
if (!Component) {
|
|
11
|
+
throw new Error(`Couldn't find default export on partial: ${partialPath}`);
|
|
12
|
+
}
|
|
13
|
+
return renderComponentWithCSS(viteServer, partialPath, Component, props, pageUrl, environmentConfig);
|
|
11
14
|
}
|
|
12
15
|
export default renderPartial;
|
package/dist/lib/templates.js
CHANGED
|
@@ -3,20 +3,28 @@
|
|
|
3
3
|
import { promises as fsPromises } from 'fs';
|
|
4
4
|
import { createIslandsGlobalJsForClientInDev, getServerGlobalsToInject, } from '@hubspot/cms-js-internal';
|
|
5
5
|
const { readFile } = fsPromises;
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const generateIslandSetupScriptBlock = () => {
|
|
7
|
+
return `<script type="module">
|
|
8
|
+
import { initConfigSingletonFromJSON } from '@hubspot/cms-components/config';
|
|
9
|
+
initConfigSingletonFromJSON(window.__hsEnvConfig);
|
|
10
|
+
</script>
|
|
11
|
+
<script type="module">
|
|
12
|
+
import { setupIslandHydration } from '@hubspot/cms-components/island-runtime';
|
|
13
|
+
setupIslandHydration();
|
|
14
|
+
</script>
|
|
10
15
|
`;
|
|
11
|
-
|
|
16
|
+
};
|
|
17
|
+
const createdPlainHtmlTemplate = async (baseTemplatePath, title, globalScriptBlock) => {
|
|
12
18
|
const htmlTemplateBase = await readFile(baseTemplatePath, 'utf-8');
|
|
13
|
-
return htmlTemplateBase.replace('<!--title-->', title)
|
|
19
|
+
return htmlTemplateBase.replace('<!--title-->', title).replace('<!--bodyend-->', `
|
|
20
|
+
${globalScriptBlock}
|
|
21
|
+
<!--bodyend-->`);
|
|
14
22
|
};
|
|
15
|
-
const createIslandHtmlTemplate = async (baseTemplatePath, title) => {
|
|
16
|
-
let htmlTemplate = await createdPlainHtmlTemplate(baseTemplatePath, title);
|
|
23
|
+
const createIslandHtmlTemplate = async (baseTemplatePath, title, globalScriptBlock) => {
|
|
24
|
+
let htmlTemplate = await createdPlainHtmlTemplate(baseTemplatePath, title, globalScriptBlock);
|
|
17
25
|
htmlTemplate = htmlTemplate.replace('<!--bodyend-->', `
|
|
18
|
-
|
|
19
|
-
|
|
26
|
+
${generateIslandSetupScriptBlock()}
|
|
27
|
+
<!-- ISLAND_RUNTIME_AND_SETUP_SCRIPT_BLOCK -->
|
|
20
28
|
<!--islandsglobal-->
|
|
21
29
|
`);
|
|
22
30
|
return htmlTemplate;
|
|
@@ -24,15 +32,16 @@ const createIslandHtmlTemplate = async (baseTemplatePath, title) => {
|
|
|
24
32
|
export const createHtmlShellDependingOnRenderContext = async (baseTemplatePath, url, title, renderContext, viteServer) => {
|
|
25
33
|
let htmlTemplate;
|
|
26
34
|
if (renderContext.islands.length) {
|
|
27
|
-
|
|
35
|
+
const globalScriptBlock = getServerGlobalsToInject(renderContext.environmentConfig, renderContext.pageUrl);
|
|
36
|
+
htmlTemplate = await createIslandHtmlTemplate(baseTemplatePath, title, globalScriptBlock);
|
|
28
37
|
htmlTemplate = htmlTemplate.replace('<!--islandsglobal-->', `<script>
|
|
29
38
|
${createIslandsGlobalJsForClientInDev(renderContext.islands, viteServer, 4)}
|
|
30
39
|
</script>
|
|
31
|
-
${getServerGlobalsToInject(renderContext.pageUrl)}
|
|
32
40
|
`);
|
|
33
41
|
}
|
|
34
42
|
else {
|
|
35
|
-
|
|
43
|
+
const globalScriptBlock = getServerGlobalsToInject(renderContext.environmentConfig);
|
|
44
|
+
htmlTemplate = await createdPlainHtmlTemplate(baseTemplatePath, title, globalScriptBlock);
|
|
36
45
|
}
|
|
37
46
|
return viteServer.transformIndexHtml(url, htmlTemplate);
|
|
38
47
|
};
|
|
@@ -40,7 +49,7 @@ export const getIslandRuntimeAndGlobalToInject = async (sortedIslandRoots, viteS
|
|
|
40
49
|
const serverDomainAndPort = `${devServerHost}:${devServerPort}`;
|
|
41
50
|
// Loading through vite so all of nested `@hubspot/cms-*` imports work
|
|
42
51
|
const { createIslandsGlobalJsForClientInDev } = await viteServer.ssrLoadModule('@hubspot/cms-js-internal');
|
|
43
|
-
let islandRuntimeScript = await viteServer.transformIndexHtml('/',
|
|
52
|
+
let islandRuntimeScript = await viteServer.transformIndexHtml('/', generateIslandSetupScriptBlock());
|
|
44
53
|
islandRuntimeScript = islandRuntimeScript.replace('import RefreshRuntime from "/@react-refresh"', `import RefreshRuntime from "http://${serverDomainAndPort}/@react-refresh"`);
|
|
45
54
|
return `${islandRuntimeScript}
|
|
46
55
|
<script>${createIslandsGlobalJsForClientInDev(sortedIslandRoots, viteServer, 4)}</script>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import http from 'http';
|
|
2
2
|
import https from 'https';
|
|
3
|
-
export default async function
|
|
3
|
+
export default async function fetchContentMetadata(url, redirects = 0, originalUrl) {
|
|
4
4
|
if (redirects > 5) {
|
|
5
5
|
throw new Error(`Hit too many redirects to HEAD requests for ${originalUrl}`);
|
|
6
6
|
}
|
|
@@ -13,13 +13,13 @@ export default async function getContentMetadata(url, redirects = 0, originalUrl
|
|
|
13
13
|
// Keep this user agent, so we don't get 403s from the request
|
|
14
14
|
['User-Agent']: 'cms-dev-server',
|
|
15
15
|
},
|
|
16
|
-
}, res => {
|
|
16
|
+
}, async (res) => {
|
|
17
17
|
// Use a lib to better follow various redirects?
|
|
18
18
|
if (res.statusCode >= 300 &&
|
|
19
19
|
res.statusCode < 400 &&
|
|
20
20
|
res.headers.location) {
|
|
21
21
|
console.log(`Redirecting to ${res.headers.location} (from ${url})`);
|
|
22
|
-
return resolve(
|
|
22
|
+
return resolve(fetchContentMetadata(res.headers.location, redirects + 1, originalUrl ?? url));
|
|
23
23
|
}
|
|
24
24
|
const portalIdHeader = res.headers['x-hs-hub-id'];
|
|
25
25
|
const contentId = res.headers['x-hs-content-id'];
|
|
@@ -39,7 +39,13 @@ export default async function getContentMetadata(url, redirects = 0, originalUrl
|
|
|
39
39
|
else if (isNaN(portalId)) {
|
|
40
40
|
return reject(new Error(`Hub ID from the HEAD request is not a number: '${portalId}' (${url})`));
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
// TODO, the hubs-hublets/hublets/find/<portalId> API is internal auth only.🤔...
|
|
43
|
+
// Prior implementation of fetchHubletForPortalId that would work execept for an internal auth issue:
|
|
44
|
+
// https://git.hubteam.com/HubSpot/cms-js-platform/blob/ef6e8029df6c09fe30d65a80073606f41ca324a6/cms-dev-server/src/proxyPage/fetchHubletForPortalId.ts
|
|
45
|
+
//
|
|
46
|
+
// const hublet = await fetchHubletForPortalId(portalId);
|
|
47
|
+
const hublet = 'na1';
|
|
48
|
+
return resolve({ portalId, contentId, hublet });
|
|
43
49
|
}
|
|
44
50
|
})
|
|
45
51
|
.on('error', err => {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
3
|
/* eslint-disable no-loop-func */
|
|
4
4
|
import path from 'path';
|
|
5
|
-
import { getAccountId } from '@hubspot/cli-lib/lib/config.js';
|
|
5
|
+
import { getAccountId, getEnv } from '@hubspot/cli-lib/lib/config.js';
|
|
6
6
|
import { get as hsGet } from '@hubspot/cli-lib/http.js';
|
|
7
7
|
import { findLocalRenderPlaceholders, } from '../lib/proxyHelpers.js';
|
|
8
8
|
import renderPartial from '../lib/renderPartial.js';
|
|
@@ -12,6 +12,7 @@ import { ENTRY_POINTS_PATH, MODULE_FOLDER_PATH, getServerGlobalsToInject, } from
|
|
|
12
12
|
import { fakeRenderJsComponent } from './utils.js';
|
|
13
13
|
import renderLocalError from './renderLocalError.js';
|
|
14
14
|
import fetchPreviewInfo from './fetchPreviewInfo.js';
|
|
15
|
+
import { buildStaticEnvConfig, } from '@hubspot/cms-components/config';
|
|
15
16
|
const makeProxyPreviewRequest = (accountId, urlToProxy, previewKey, contentId) => {
|
|
16
17
|
const url = new URL(urlToProxy);
|
|
17
18
|
console.log(`Proxying request to ${url.href}`, {
|
|
@@ -52,7 +53,7 @@ const makeProxyPreviewRequest = (accountId, urlToProxy, previewKey, contentId) =
|
|
|
52
53
|
}
|
|
53
54
|
});
|
|
54
55
|
};
|
|
55
|
-
const renderComponentForPlaceholder = (indexForThisRender, localRenderPlaceholder, webRoot, viteServer, pageUrl, hsProjectJsonProjectName, webRootProjectComponent) => {
|
|
56
|
+
const renderComponentForPlaceholder = (indexForThisRender, localRenderPlaceholder, webRoot, viteServer, pageUrl, environmentConfig, hsProjectJsonProjectName, webRootProjectComponent) => {
|
|
56
57
|
const { path: placeholderPath } = localRenderPlaceholder;
|
|
57
58
|
const [__projectIdentifier, projectFolder, projectComponentFolder, componentFolder, componentTypeFolder, componentFileName,] = placeholderPath.split('/');
|
|
58
59
|
const partialPath = `${componentFolder}/${componentTypeFolder}/`;
|
|
@@ -67,19 +68,21 @@ const renderComponentForPlaceholder = (indexForThisRender, localRenderPlaceholde
|
|
|
67
68
|
return fakeRenderJsComponent(indexForThisRender);
|
|
68
69
|
}
|
|
69
70
|
else if (partialPath === ENTRY_POINTS_PATH) {
|
|
70
|
-
return renderPartial(viteServer, resolvedPath, localRenderPlaceholder.json, pageUrl);
|
|
71
|
+
return renderPartial(viteServer, resolvedPath, localRenderPlaceholder.json, pageUrl, environmentConfig);
|
|
71
72
|
}
|
|
72
73
|
else if (partialPath === MODULE_FOLDER_PATH) {
|
|
73
|
-
return renderModule(viteServer, resolvedPath, localRenderPlaceholder.json, pageUrl);
|
|
74
|
+
return renderModule(viteServer, resolvedPath, localRenderPlaceholder.json, pageUrl, environmentConfig);
|
|
74
75
|
}
|
|
75
76
|
else {
|
|
76
77
|
throw new Error(`Unimplemented render of ${placeholderPath}.`);
|
|
77
78
|
}
|
|
78
79
|
};
|
|
79
|
-
export default async function proxySpecificPage(response, portalId, contentId, webRoot, viteServer, devServerHost, devServerPort, projectName, projectComponentPath, urlToProxy, fullOriginalUrl) {
|
|
80
|
+
export default async function proxySpecificPage(response, portalId, hublet, contentId, webRoot, viteServer, devServerHost, devServerPort, projectName, projectComponentPath, urlToProxy, fullOriginalUrl) {
|
|
80
81
|
let accountId;
|
|
82
|
+
let proxyEnv;
|
|
81
83
|
try {
|
|
82
84
|
accountId = getAccountId(portalId);
|
|
85
|
+
proxyEnv = getEnv(accountId);
|
|
83
86
|
}
|
|
84
87
|
catch (e) {
|
|
85
88
|
response
|
|
@@ -97,6 +100,7 @@ export default async function proxySpecificPage(response, portalId, contentId, w
|
|
|
97
100
|
const previewInfo = await fetchPreviewInfo(accountId, portalId, contentId);
|
|
98
101
|
const { previewKey } = previewInfo;
|
|
99
102
|
const pageHtml = await makeProxyPreviewRequest(accountId, urlToProxy, previewKey, contentId);
|
|
103
|
+
const environmentConfig = buildStaticEnvConfig(portalId, hublet, proxyEnv, false);
|
|
100
104
|
// Loading through vite so all of nested `@hubspot/cms-*` imports work
|
|
101
105
|
const { sortIslandRootsByPriority } = await viteServer.ssrLoadModule('@hubspot/cms-components');
|
|
102
106
|
const localRenderPlaceholders = findLocalRenderPlaceholders(pageHtml);
|
|
@@ -110,7 +114,7 @@ export default async function proxySpecificPage(response, portalId, contentId, w
|
|
|
110
114
|
for (let indexForThisRender = 0; indexForThisRender < numPlaceholders; indexForThisRender++) {
|
|
111
115
|
const localRenderPlaceholder = localRenderPlaceholders[indexForThisRender];
|
|
112
116
|
pageChunks[indexForThisRender] = pageHtml.slice(offsetSoFar, localRenderPlaceholder.startOffset);
|
|
113
|
-
const renderPromise = renderComponentForPlaceholder(indexForThisRender, localRenderPlaceholder, webRoot, viteServer, fullOriginalUrl, projectName, projectComponentPath);
|
|
117
|
+
const renderPromise = renderComponentForPlaceholder(indexForThisRender, localRenderPlaceholder, webRoot, viteServer, fullOriginalUrl, environmentConfig, projectName, projectComponentPath);
|
|
114
118
|
renderPromisesByChunkIndex[indexForThisRender] = renderPromise
|
|
115
119
|
.then(promiseResult => {
|
|
116
120
|
// eslint-disable-next-line prefer-const
|
|
@@ -141,7 +145,7 @@ export default async function proxySpecificPage(response, portalId, contentId, w
|
|
|
141
145
|
// console.log("JS rendered chunk", i, foobar);
|
|
142
146
|
response.write(foobar);
|
|
143
147
|
}
|
|
144
|
-
response.write(getServerGlobalsToInject(fullOriginalUrl));
|
|
148
|
+
response.write(getServerGlobalsToInject(environmentConfig, fullOriginalUrl));
|
|
145
149
|
if (islands.length > 0) {
|
|
146
150
|
const sortedIslandRoots = sortIslandRootsByPriority(islands);
|
|
147
151
|
const islandRuntimeAndGlobalScriptHtml = await getIslandRuntimeAndGlobalToInject(sortedIslandRoots, viteServer, devServerHost, devServerPort);
|
package/dist/proxyPage/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* hs-eslint ignored failing-rules */
|
|
2
2
|
/* eslint-disable prefer-promise-reject-errors */
|
|
3
|
-
import {
|
|
3
|
+
import { createEmptyServerRenderContext } from '@hubspot/cms-components';
|
|
4
4
|
import { VALID_PROXY_DOMAIN_SUFFIXES } from './constants.js';
|
|
5
5
|
export const getSubDomainFromValidLocalDomain = hostname => {
|
|
6
6
|
for (const validProxyDomainSuffix of VALID_PROXY_DOMAIN_SUFFIXES) {
|
|
@@ -22,7 +22,7 @@ export const fakeRenderJsComponent = (indexForThisRender) => {
|
|
|
22
22
|
resolve({
|
|
23
23
|
html: fakeJsRenderedOutput,
|
|
24
24
|
cssNeeded: '',
|
|
25
|
-
renderContext:
|
|
25
|
+
renderContext: createEmptyServerRenderContext(),
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
catch (e) {
|
|
@@ -6,7 +6,7 @@ const homeRouteHandler = async (request, response) => {
|
|
|
6
6
|
const { webRoot, storybookEnabled, devServerPort } = request.app.locals;
|
|
7
7
|
const { partialNames } = findAllPartialsInRoot(webRoot);
|
|
8
8
|
const { moduleNames } = findAllModulesInRoot(webRoot);
|
|
9
|
-
const { html: responseHtml } = await renderOnServer(_jsxs(_Fragment, { children: [storybookEnabled ? (_jsx("h2", { children: _jsx("a", { href: `//storybook.${request.hostname}:${devServerPort}`, children: "Storybook" }) })) : null, _jsx("h2", { children: "Partials" }), _jsx(PartialsList, { webRoot: webRoot, partialNames: partialNames }), _jsx("h2", { children: "Modules" }), _jsx(ModulesList, { webRoot: webRoot, moduleNames: moduleNames })] }), null);
|
|
9
|
+
const { html: responseHtml } = await renderOnServer(_jsxs(_Fragment, { children: [storybookEnabled ? (_jsx("h2", { children: _jsx("a", { href: `//storybook.${request.hostname}:${devServerPort}`, children: "Storybook" }) })) : null, _jsx("h2", { children: "Partials" }), _jsx(PartialsList, { webRoot: webRoot, partialNames: partialNames }), _jsx("h2", { children: "Modules" }), _jsx(ModulesList, { webRoot: webRoot, moduleNames: moduleNames })] }), null, null);
|
|
10
10
|
response.status(200).set({ 'Content-Type': 'text/html' }).end(responseHtml);
|
|
11
11
|
};
|
|
12
12
|
export default homeRouteHandler;
|
|
@@ -4,7 +4,7 @@ import ModulesList from '../components/ModulesList.js';
|
|
|
4
4
|
const linksToAllModuleRouteHandler = async (request, response) => {
|
|
5
5
|
const { webRoot } = request.app.locals;
|
|
6
6
|
const { moduleNames } = findAllModulesInRoot(webRoot);
|
|
7
|
-
const { html: responseHtml } = await renderOnServer(_jsx(ModulesList, { webRoot: webRoot, moduleNames: moduleNames }), null);
|
|
7
|
+
const { html: responseHtml } = await renderOnServer(_jsx(ModulesList, { webRoot: webRoot, moduleNames: moduleNames }), null, null);
|
|
8
8
|
response.status(200).set({ 'Content-Type': 'text/html' }).end(responseHtml);
|
|
9
9
|
};
|
|
10
10
|
export default linksToAllModuleRouteHandler;
|
|
@@ -4,7 +4,7 @@ import PartialsList from '../components/PartialsList.js';
|
|
|
4
4
|
const linksToAllPartialRouteHandler = async (request, response) => {
|
|
5
5
|
const { webRoot } = request.app.locals;
|
|
6
6
|
const { partialNames } = findAllPartialsInRoot(webRoot);
|
|
7
|
-
const { html: responseHtml } = await renderOnServer(_jsx(PartialsList, { webRoot: webRoot, partialNames: partialNames }), null);
|
|
7
|
+
const { html: responseHtml } = await renderOnServer(_jsx(PartialsList, { webRoot: webRoot, partialNames: partialNames }), null, null);
|
|
8
8
|
response.status(200).set({ 'Content-Type': 'text/html' }).end(responseHtml);
|
|
9
9
|
};
|
|
10
10
|
export default linksToAllPartialRouteHandler;
|
package/dist/routes/module.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { buildStaticEnvConfig } from '@hubspot/cms-components/config';
|
|
1
2
|
import path from 'path';
|
|
2
3
|
import url from 'url';
|
|
3
4
|
import { normalizePath } from 'vite';
|
|
@@ -5,12 +6,13 @@ import fullOriginalUrlFromRequest from '../lib/fullOriginalUrlFromRequest.js';
|
|
|
5
6
|
import renderModule from '../lib/renderModule.js';
|
|
6
7
|
import { createHtmlShellDependingOnRenderContext } from '../lib/templates.js';
|
|
7
8
|
const moduleRouteHandler = async (request, response) => {
|
|
8
|
-
const { webRoot, viteServer } = request.app.locals;
|
|
9
|
+
const { authedPortalId, authedEnv, authedHublet, webRoot, viteServer } = request.app.locals;
|
|
9
10
|
const moduleName = request.params.moduleName;
|
|
10
11
|
const baseTemplatePath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), '../html/baseTemplate.html');
|
|
11
12
|
const modulePath = normalizePath(path.resolve(webRoot, 'components/modules/', moduleName));
|
|
13
|
+
const environmentConfig = buildStaticEnvConfig(authedPortalId, authedHublet, authedEnv, false);
|
|
12
14
|
try {
|
|
13
|
-
const { html: renderedHtml, cssNeeded, renderContext, } = await renderModule(viteServer, modulePath, null, fullOriginalUrlFromRequest(request));
|
|
15
|
+
const { html: renderedHtml, cssNeeded, renderContext, } = await renderModule(viteServer, modulePath, null, fullOriginalUrlFromRequest(request), environmentConfig);
|
|
14
16
|
const htmlShell = await createHtmlShellDependingOnRenderContext(baseTemplatePath, request.originalUrl, `Module: ${modulePath}`, renderContext, viteServer);
|
|
15
17
|
const html = htmlShell
|
|
16
18
|
.replace('<!--headend-->', cssNeeded)
|
package/dist/routes/partial.js
CHANGED
|
@@ -4,13 +4,15 @@ import { normalizePath } from 'vite';
|
|
|
4
4
|
import fullOriginalUrlFromRequest from '../lib/fullOriginalUrlFromRequest.js';
|
|
5
5
|
import renderPartial from '../lib/renderPartial.js';
|
|
6
6
|
import { createHtmlShellDependingOnRenderContext } from '../lib/templates.js';
|
|
7
|
+
import { buildStaticEnvConfig } from '@hubspot/cms-components/config';
|
|
7
8
|
const partialRouteHandler = async (request, response) => {
|
|
8
|
-
const { webRoot, viteServer } = request.app.locals;
|
|
9
|
+
const { authedPortalId, authedEnv, authedHublet, webRoot, viteServer } = request.app.locals;
|
|
9
10
|
const partialName = request.params.partial;
|
|
10
11
|
const baseTemplatePath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), '../html/baseTemplate.html');
|
|
11
12
|
const partialPath = normalizePath(path.resolve(webRoot, 'components/partials/', partialName));
|
|
13
|
+
const environmentConfig = buildStaticEnvConfig(authedPortalId, authedHublet, authedEnv, false);
|
|
12
14
|
try {
|
|
13
|
-
const { html: renderedHtml, cssNeeded, renderContext, } = await renderPartial(viteServer, partialPath, {}, fullOriginalUrlFromRequest(request));
|
|
15
|
+
const { html: renderedHtml, cssNeeded, renderContext, } = await renderPartial(viteServer, partialPath, {}, fullOriginalUrlFromRequest(request), environmentConfig);
|
|
14
16
|
const htmlShell = await createHtmlShellDependingOnRenderContext(baseTemplatePath, request.originalUrl, `Partial: ${partialPath}`, renderContext, viteServer);
|
|
15
17
|
const html = htmlShell
|
|
16
18
|
.replace('<!--headend-->', cssNeeded)
|
|
@@ -31,8 +31,8 @@ export default async function proxyPageRouteHandler(request, response, next) {
|
|
|
31
31
|
return response.status(400).send(message);
|
|
32
32
|
}
|
|
33
33
|
try {
|
|
34
|
-
const { portalId, contentId } = await fetchContentMetadata(urlToProxy);
|
|
35
|
-
await proxyPage(response, portalId, contentId, webRoot, viteServer, devServerHost, devServerPort, projectName, projectComponentPath, urlToProxy, fullOriginalUrl);
|
|
34
|
+
const { portalId, contentId, hublet } = await fetchContentMetadata(urlToProxy);
|
|
35
|
+
await proxyPage(response, portalId, hublet, contentId, webRoot, viteServer, devServerHost, devServerPort, projectName, projectComponentPath, urlToProxy, fullOriginalUrl);
|
|
36
36
|
}
|
|
37
37
|
catch (e) {
|
|
38
38
|
next(e);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export default function getFieldsArgTypes(fieldsJson, currentPathSegments = []) {
|
|
2
|
+
let resultArgTypes = {};
|
|
3
|
+
for (const field of fieldsJson) {
|
|
4
|
+
// storybook doesn't support nested controls, so emulate our nested
|
|
5
|
+
// structure with group1.group2.field structure
|
|
6
|
+
const fullFieldName = [...currentPathSegments, field.name].join('.');
|
|
7
|
+
// should this definition of fieldType => argType live next to fields in cms-components?
|
|
8
|
+
switch (field.type) {
|
|
9
|
+
case 'group': {
|
|
10
|
+
if (!field.children) {
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
const groupResult = getFieldsArgTypes(field.children, [
|
|
14
|
+
...currentPathSegments,
|
|
15
|
+
field.name,
|
|
16
|
+
]);
|
|
17
|
+
resultArgTypes = {
|
|
18
|
+
...resultArgTypes,
|
|
19
|
+
...groupResult,
|
|
20
|
+
};
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
case 'boolean': {
|
|
24
|
+
resultArgTypes[fullFieldName] = {
|
|
25
|
+
control: 'boolean',
|
|
26
|
+
defaultValue: field.default,
|
|
27
|
+
};
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case 'text': {
|
|
31
|
+
resultArgTypes[fullFieldName] = {
|
|
32
|
+
control: 'text',
|
|
33
|
+
defaultValue: field.default,
|
|
34
|
+
};
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
case 'number': {
|
|
38
|
+
resultArgTypes[fullFieldName] = {
|
|
39
|
+
control: 'number',
|
|
40
|
+
defaultValue: field.default,
|
|
41
|
+
};
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
default: {
|
|
45
|
+
resultArgTypes[fullFieldName] = {
|
|
46
|
+
control: 'object',
|
|
47
|
+
defaultValue: field.default,
|
|
48
|
+
};
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return resultArgTypes;
|
|
54
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { ModuleFields, BooleanField, TextField, FieldGroup, NumberField, ColorField, } from '@hubspot/cms-components/fields';
|
|
4
|
+
import { renderFieldsToJson } from '@hubspot/cms-js-internal';
|
|
5
|
+
import getFieldsArgTypes from './getFieldsArgTypes.js';
|
|
6
|
+
describe('getFieldsArgTypes', () => {
|
|
7
|
+
it('creates argType for boolean field', () => {
|
|
8
|
+
const fields = (_jsx(ModuleFields, { children: _jsx(BooleanField, { name: "testBooleanField", label: "field", default: false }) }));
|
|
9
|
+
const argTypes = getFieldsArgTypes(renderFieldsToJson(fields));
|
|
10
|
+
expect(argTypes).toStrictEqual({
|
|
11
|
+
testBooleanField: {
|
|
12
|
+
control: 'boolean',
|
|
13
|
+
defaultValue: false,
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
it('creates text argType for text field', () => {
|
|
18
|
+
const fields = (_jsx(ModuleFields, { children: _jsx(TextField, { name: "testStringField", label: "field", default: 'test' }) }));
|
|
19
|
+
const argTypes = getFieldsArgTypes(renderFieldsToJson(fields));
|
|
20
|
+
expect(argTypes).toStrictEqual({
|
|
21
|
+
testStringField: {
|
|
22
|
+
control: 'text',
|
|
23
|
+
defaultValue: 'test',
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
it('creates number argType for number field', () => {
|
|
28
|
+
const fields = (_jsx(ModuleFields, { children: _jsx(NumberField, { name: "testNumberField", label: "field", default: 123 }) }));
|
|
29
|
+
const argTypes = getFieldsArgTypes(renderFieldsToJson(fields));
|
|
30
|
+
expect(argTypes).toStrictEqual({
|
|
31
|
+
testNumberField: {
|
|
32
|
+
control: 'number',
|
|
33
|
+
defaultValue: 123,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
it('creates object argType for unknown field', () => {
|
|
38
|
+
const fields = (_jsx(ModuleFields, { children: _jsx(ColorField, { name: "testColorField", label: "field", default: {
|
|
39
|
+
color: '#fff',
|
|
40
|
+
} }) }));
|
|
41
|
+
const argTypes = getFieldsArgTypes(renderFieldsToJson(fields));
|
|
42
|
+
expect(argTypes).toStrictEqual({
|
|
43
|
+
testColorField: {
|
|
44
|
+
control: 'object',
|
|
45
|
+
defaultValue: {
|
|
46
|
+
color: '#fff',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
it('prefixes fields in a group with . separated group names', () => {
|
|
52
|
+
const fields = (_jsx(ModuleFields, { children: _jsxs(FieldGroup, { name: "mygroup", label: "field group", children: [_jsx(TextField, { name: "testStringField", label: "field", default: 'test' }), _jsx(BooleanField, { name: "testBooleanField", label: "field", default: false })] }) }));
|
|
53
|
+
const argTypes = getFieldsArgTypes(renderFieldsToJson(fields));
|
|
54
|
+
expect(argTypes).toStrictEqual({
|
|
55
|
+
'mygroup.testStringField': {
|
|
56
|
+
control: 'text',
|
|
57
|
+
defaultValue: 'test',
|
|
58
|
+
},
|
|
59
|
+
'mygroup.testBooleanField': {
|
|
60
|
+
control: 'boolean',
|
|
61
|
+
defaultValue: false,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
it('handles multiple nesting alongside top level values', () => {
|
|
66
|
+
const fields = (_jsxs(ModuleFields, { children: [_jsx(TextField, { name: "toplevel", label: "top level text field", default: "defaultvalue" }), _jsxs(FieldGroup, { name: "mygroup", label: "field group", children: [_jsx(TextField, { name: "testStringField", label: "field", default: 'test' }), _jsx(BooleanField, { name: "testBooleanField", label: "field", default: false }), _jsxs(FieldGroup, { name: "nestedgroup", label: "nested group", children: [_jsx(TextField, { name: "testStringField", label: "field", default: 'test' }), _jsx(BooleanField, { name: "testBooleanField", label: "field", default: false })] })] })] }));
|
|
67
|
+
const argTypes = getFieldsArgTypes(renderFieldsToJson(fields));
|
|
68
|
+
expect(argTypes).toStrictEqual({
|
|
69
|
+
toplevel: {
|
|
70
|
+
control: 'text',
|
|
71
|
+
defaultValue: 'defaultvalue',
|
|
72
|
+
},
|
|
73
|
+
'mygroup.testStringField': {
|
|
74
|
+
control: 'text',
|
|
75
|
+
defaultValue: 'test',
|
|
76
|
+
},
|
|
77
|
+
'mygroup.testBooleanField': {
|
|
78
|
+
control: 'boolean',
|
|
79
|
+
defaultValue: false,
|
|
80
|
+
},
|
|
81
|
+
'mygroup.nestedgroup.testStringField': {
|
|
82
|
+
control: 'text',
|
|
83
|
+
defaultValue: 'test',
|
|
84
|
+
},
|
|
85
|
+
'mygroup.nestedgroup.testBooleanField': {
|
|
86
|
+
control: 'boolean',
|
|
87
|
+
defaultValue: false,
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// default values come through via `defaultValue` in the story's argTypes, we
|
|
2
|
+
// need to re-nest them to match the field structure
|
|
3
|
+
export default function getNestedFieldObjectForArgs(_args) {
|
|
4
|
+
const result = {};
|
|
5
|
+
for (const argPath in _args) {
|
|
6
|
+
if (Object.prototype.hasOwnProperty.call(_args, argPath)) {
|
|
7
|
+
const argPathSegments = argPath.split('.');
|
|
8
|
+
const nestedSegments = argPathSegments.slice(0, -1);
|
|
9
|
+
const fieldName = argPathSegments[argPathSegments.length - 1];
|
|
10
|
+
const fieldValue = _args[argPath];
|
|
11
|
+
let currentNest = result;
|
|
12
|
+
for (const segment of nestedSegments) {
|
|
13
|
+
if (!Object.prototype.hasOwnProperty.call(currentNest, segment)) {
|
|
14
|
+
currentNest[segment] = {};
|
|
15
|
+
}
|
|
16
|
+
currentNest = currentNest[segment];
|
|
17
|
+
}
|
|
18
|
+
currentNest[fieldName] = fieldValue;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import getNestedFieldObjectForArgs from './getNestedFieldObjectForArgs.js';
|
|
3
|
+
describe('getNestedFieldObjectForArgs', () => {
|
|
4
|
+
it('leaves top level args as is', () => {
|
|
5
|
+
const argMap = {
|
|
6
|
+
testField: 'testValue',
|
|
7
|
+
testField2: 'testValue',
|
|
8
|
+
};
|
|
9
|
+
const result = getNestedFieldObjectForArgs(argMap);
|
|
10
|
+
expect(result).toStrictEqual(argMap);
|
|
11
|
+
});
|
|
12
|
+
it('turns a . separated arg name into a nested field', () => {
|
|
13
|
+
const argMap = {
|
|
14
|
+
'group.testField': 'testValue',
|
|
15
|
+
topLevelField: 'testValue',
|
|
16
|
+
};
|
|
17
|
+
const result = getNestedFieldObjectForArgs(argMap);
|
|
18
|
+
expect(result).toStrictEqual({
|
|
19
|
+
group: {
|
|
20
|
+
testField: 'testValue',
|
|
21
|
+
},
|
|
22
|
+
topLevelField: 'testValue',
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
it('keeps nested fields with the same group prefix nested in the same object', () => {
|
|
26
|
+
const argMap = {
|
|
27
|
+
'group.testField': 'testValue',
|
|
28
|
+
'group.secondField': 'testValue',
|
|
29
|
+
topLevelField: 'testValue',
|
|
30
|
+
};
|
|
31
|
+
const result = getNestedFieldObjectForArgs(argMap);
|
|
32
|
+
expect(result).toStrictEqual({
|
|
33
|
+
group: {
|
|
34
|
+
testField: 'testValue',
|
|
35
|
+
secondField: 'testValue',
|
|
36
|
+
},
|
|
37
|
+
topLevelField: 'testValue',
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
package/dist/storybook/index.js
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
|
-
//
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
1
|
+
// ../cms-js-internal/dist/index.js
|
|
2
|
+
import { renderToPipeableStream } from "react-dom/server";
|
|
3
|
+
import {
|
|
4
|
+
ServerRenderContext,
|
|
5
|
+
createServerRenderContext
|
|
6
|
+
} from "@hubspot/cms-components";
|
|
4
7
|
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
import { cloneElement } from "react";
|
|
9
|
+
import { renderToStaticMarkup } from "react-dom/server";
|
|
10
|
+
function renderFieldsToJson(fields) {
|
|
11
|
+
let moduleJson;
|
|
12
|
+
const outputCallback = (callbackJson) => {
|
|
13
|
+
moduleJson = callbackJson;
|
|
14
|
+
};
|
|
15
|
+
renderToStaticMarkup(
|
|
16
|
+
cloneElement(fields, {
|
|
17
|
+
outputCallback
|
|
18
|
+
})
|
|
19
|
+
);
|
|
20
|
+
return moduleJson;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/storybook/getFieldsArgTypes.ts
|
|
5
24
|
function getFieldsArgTypes(fieldsJson, currentPathSegments = []) {
|
|
6
25
|
let resultArgTypes = {};
|
|
7
26
|
for (const field of fieldsJson) {
|
|
@@ -53,6 +72,8 @@ function getFieldsArgTypes(fieldsJson, currentPathSegments = []) {
|
|
|
53
72
|
}
|
|
54
73
|
return resultArgTypes;
|
|
55
74
|
}
|
|
75
|
+
|
|
76
|
+
// src/storybook/getNestedFieldObjectForArgs.ts
|
|
56
77
|
function getNestedFieldObjectForArgs(_args) {
|
|
57
78
|
const result = {};
|
|
58
79
|
for (const argPath in _args) {
|
|
@@ -73,27 +94,15 @@ function getNestedFieldObjectForArgs(_args) {
|
|
|
73
94
|
}
|
|
74
95
|
return result;
|
|
75
96
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
moduleJson = callbackJson;
|
|
80
|
-
};
|
|
81
|
-
renderToStaticMarkup(
|
|
82
|
-
React.cloneElement(fields, {
|
|
83
|
-
outputCallback
|
|
84
|
-
})
|
|
85
|
-
);
|
|
86
|
-
if (!moduleJson) {
|
|
87
|
-
throw new Error("couldn't render module fields definition to fields.json");
|
|
88
|
-
}
|
|
89
|
-
return moduleJson;
|
|
90
|
-
}
|
|
97
|
+
|
|
98
|
+
// src/storybook/moduleStory.tsx
|
|
99
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
91
100
|
function moduleStory(ModuleComponent, fields) {
|
|
92
|
-
const moduleJson =
|
|
101
|
+
const moduleJson = renderFieldsToJson(fields);
|
|
93
102
|
const argTypes = getFieldsArgTypes(moduleJson);
|
|
94
103
|
const story = (_args) => {
|
|
95
104
|
const fieldValues = getNestedFieldObjectForArgs(_args);
|
|
96
|
-
return /* @__PURE__ */
|
|
105
|
+
return /* @__PURE__ */ jsx2(ModuleComponent, {
|
|
97
106
|
...fieldValues
|
|
98
107
|
});
|
|
99
108
|
};
|
|
@@ -103,7 +112,5 @@ function moduleStory(ModuleComponent, fields) {
|
|
|
103
112
|
return storyWithArgs;
|
|
104
113
|
}
|
|
105
114
|
export {
|
|
106
|
-
getNestedFieldObjectForArgs,
|
|
107
|
-
moduleFieldsToJson,
|
|
108
115
|
moduleStory
|
|
109
116
|
};
|
package/dist/storybook/main.js
CHANGED
|
@@ -1,97 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
let resultArgTypes = {};
|
|
6
|
-
for (const field of fieldsJson) {
|
|
7
|
-
// storybook doesn't support nested controls, so emulate our nested
|
|
8
|
-
// structure with group1.group2.field structure
|
|
9
|
-
const fullFieldName = [...currentPathSegments, field.name].join('.');
|
|
10
|
-
// should this definition of fieldType => argType live next to fields in cms-components?
|
|
11
|
-
switch (field.type) {
|
|
12
|
-
case 'group': {
|
|
13
|
-
if (!field.children) {
|
|
14
|
-
continue;
|
|
15
|
-
}
|
|
16
|
-
const groupResult = getFieldsArgTypes(field.children, [
|
|
17
|
-
...currentPathSegments,
|
|
18
|
-
field.name,
|
|
19
|
-
]);
|
|
20
|
-
resultArgTypes = {
|
|
21
|
-
...resultArgTypes,
|
|
22
|
-
...groupResult,
|
|
23
|
-
};
|
|
24
|
-
break;
|
|
25
|
-
}
|
|
26
|
-
case 'boolean': {
|
|
27
|
-
resultArgTypes[fullFieldName] = {
|
|
28
|
-
control: 'boolean',
|
|
29
|
-
defaultValue: field.default,
|
|
30
|
-
};
|
|
31
|
-
break;
|
|
32
|
-
}
|
|
33
|
-
case 'text': {
|
|
34
|
-
resultArgTypes[fullFieldName] = {
|
|
35
|
-
control: 'text',
|
|
36
|
-
defaultValue: field.default,
|
|
37
|
-
};
|
|
38
|
-
break;
|
|
39
|
-
}
|
|
40
|
-
case 'number': {
|
|
41
|
-
resultArgTypes[fullFieldName] = {
|
|
42
|
-
control: 'number',
|
|
43
|
-
defaultValue: field.default,
|
|
44
|
-
};
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
default: {
|
|
48
|
-
resultArgTypes[fullFieldName] = {
|
|
49
|
-
control: 'object',
|
|
50
|
-
defaultValue: field.default,
|
|
51
|
-
};
|
|
52
|
-
break;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return resultArgTypes;
|
|
57
|
-
}
|
|
58
|
-
// default values come through via `defaultValue` in the story's argTypes, we
|
|
59
|
-
// need to re-nest them to match the field structure
|
|
60
|
-
export function getNestedFieldObjectForArgs(_args) {
|
|
61
|
-
const result = {};
|
|
62
|
-
for (const argPath in _args) {
|
|
63
|
-
if (Object.prototype.hasOwnProperty.call(_args, argPath)) {
|
|
64
|
-
const argPathSegments = argPath.split('.');
|
|
65
|
-
const nestedSegments = argPathSegments.slice(0, -1);
|
|
66
|
-
const fieldName = argPathSegments[argPathSegments.length - 1];
|
|
67
|
-
const fieldValue = _args[argPath];
|
|
68
|
-
let currentNest = result;
|
|
69
|
-
for (const segment of nestedSegments) {
|
|
70
|
-
if (!Object.prototype.hasOwnProperty.call(currentNest, segment)) {
|
|
71
|
-
currentNest[segment] = {};
|
|
72
|
-
}
|
|
73
|
-
currentNest = currentNest[segment];
|
|
74
|
-
}
|
|
75
|
-
currentNest[fieldName] = fieldValue;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return result;
|
|
79
|
-
}
|
|
80
|
-
export function moduleFieldsToJson(fields) {
|
|
81
|
-
let moduleJson;
|
|
82
|
-
const outputCallback = (callbackJson) => {
|
|
83
|
-
moduleJson = callbackJson;
|
|
84
|
-
};
|
|
85
|
-
renderToStaticMarkup(React.cloneElement(fields, {
|
|
86
|
-
outputCallback,
|
|
87
|
-
}));
|
|
88
|
-
if (!moduleJson) {
|
|
89
|
-
throw new Error("couldn't render module fields definition to fields.json");
|
|
90
|
-
}
|
|
91
|
-
return moduleJson;
|
|
92
|
-
}
|
|
2
|
+
import { renderFieldsToJson } from '@hubspot/cms-js-internal';
|
|
3
|
+
import getFieldsArgTypes from './getFieldsArgTypes.js';
|
|
4
|
+
import getNestedFieldObjectForArgs from './getNestedFieldObjectForArgs.js';
|
|
93
5
|
export function moduleStory(ModuleComponent, fields) {
|
|
94
|
-
const moduleJson =
|
|
6
|
+
const moduleJson = renderFieldsToJson(fields);
|
|
95
7
|
const argTypes = getFieldsArgTypes(moduleJson);
|
|
96
8
|
const story = (_args) => {
|
|
97
9
|
const fieldValues = getNestedFieldObjectForArgs(_args);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubspot/cms-dev-server",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -16,13 +16,13 @@
|
|
|
16
16
|
"es-watch": "esbuild --outfile=dist-esbuild/runWhenDeveloping.cjs --bundle --watch --platform=node --external:fsevents --external:vite --external:react-refresh runWhenDeveloping.ts",
|
|
17
17
|
"package": "npm run build && npm pack",
|
|
18
18
|
"prepare": "npm run build",
|
|
19
|
-
"runBuild": "node run.
|
|
20
|
-
"test": "NODE_ENV=test vitest"
|
|
19
|
+
"runBuild": "node dist/run.js",
|
|
20
|
+
"test": "NODE_ENV=test vitest run"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@hubspot/cli-lib": "^3.0.12",
|
|
24
|
-
"@hubspot/cms-components": "^0.3.
|
|
25
|
-
"@hubspot/cms-js-internal": "^0.3.
|
|
24
|
+
"@hubspot/cms-components": "^0.3.3",
|
|
25
|
+
"@hubspot/cms-js-internal": "^0.3.3",
|
|
26
26
|
"@storybook/addon-actions": "^7.0.0-beta.19",
|
|
27
27
|
"@storybook/addon-essentials": "^7.0.0-beta.19",
|
|
28
28
|
"@storybook/addon-interactions": "^7.0.0-beta.19",
|
|
@@ -85,5 +85,5 @@
|
|
|
85
85
|
"publishConfig": {
|
|
86
86
|
"access": "public"
|
|
87
87
|
},
|
|
88
|
-
"gitHead": "
|
|
88
|
+
"gitHead": "d5b1da55b81aef5a486b5298a3c65f6e3f8e5ef7"
|
|
89
89
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Field } from '@hubspot/cms-components/fields';
|
|
2
|
+
declare type ArgTypes = {
|
|
3
|
+
[fieldPath: string]: {
|
|
4
|
+
control: string;
|
|
5
|
+
defaultValue: any;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export default function getFieldsArgTypes(fieldsJson: Field[], currentPathSegments?: string[]): ArgTypes;
|
|
9
|
+
export {};
|
|
@@ -1,12 +1,2 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { Field } from '@hubspot/cms-components/fields';
|
|
3
|
-
declare type Args = {
|
|
4
|
-
[fieldPath: string]: any;
|
|
5
|
-
};
|
|
6
|
-
declare type FieldValues = {
|
|
7
|
-
[fieldNameOrGroupName: string]: FieldValues;
|
|
8
|
-
};
|
|
9
|
-
export declare function getNestedFieldObjectForArgs(_args: Args): FieldValues;
|
|
10
|
-
export declare function moduleFieldsToJson(fields: JSX.Element): Field[];
|
|
11
2
|
export declare function moduleStory(ModuleComponent: React.ComponentType, fields: JSX.Element): any;
|
|
12
|
-
export {};
|