@hubspot/cms-dev-server 0.2.11 → 0.2.13
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/lib/renderModule.js +2 -34
- package/dist/proxyPage/constants.js +1 -4
- package/dist/routes/proxyPageResourceRedirect.js +11 -1
- package/dist/storybook/index.js +97 -0
- package/dist/storybook/main.js +22 -0
- package/dist/storybook/moduleStory.js +92 -0
- package/dist/storybook/preview.js +9 -0
- package/package.json +4 -4
- package/dist/lib/argHelpers.js +0 -6
- package/dist/lib/htmlHelpers.js +0 -10
- package/dist/plugins/devMockClientEntryPlugin.js +0 -52
package/dist/lib/renderModule.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { join as joinPosix } from 'path/posix';
|
|
2
|
-
import { cloneElement } from 'react';
|
|
3
|
-
import { renderToStaticMarkup } from 'react-dom/server';
|
|
4
2
|
import renderComponentWithCSS from './renderComponentWithCSS.js';
|
|
5
3
|
import { traverseFieldsJSON } from '@hubspot/cms-components/fields';
|
|
6
|
-
import { hasComponentExtension } from '@hubspot/cms-js-internal';
|
|
4
|
+
import { hasComponentExtension, setIn, renderFieldsToJson, } from '@hubspot/cms-js-internal';
|
|
7
5
|
const lookForModuleInFolder = async (modulePath, viteServer) => {
|
|
8
6
|
let componentModule;
|
|
9
7
|
const insideFolderPath = joinPosix(modulePath, 'index');
|
|
@@ -48,41 +46,11 @@ async function renderModule(viteServer, requestedModulePath, fieldOverrides = {}
|
|
|
48
46
|
const props = getPropsForModule(fieldsJSON, fieldOverrides);
|
|
49
47
|
return renderComponentWithCSS(viteServer, foundPath, Component, props, pageUrl);
|
|
50
48
|
}
|
|
51
|
-
// TODO, move to cms-js-internal?
|
|
52
|
-
function setInHelper(object, nestedKeysArray, value) {
|
|
53
|
-
let curr = object;
|
|
54
|
-
const finalKey = nestedKeysArray.pop();
|
|
55
|
-
for (let index = 0; index < nestedKeysArray.length; index++) {
|
|
56
|
-
const currentKey = nestedKeysArray[index];
|
|
57
|
-
const nextKey = nestedKeysArray[index + 1];
|
|
58
|
-
if (Number.isInteger(nextKey) || (!nextKey && Number.isInteger(finalKey))) {
|
|
59
|
-
if (curr[currentKey] === undefined) {
|
|
60
|
-
curr[currentKey] = [];
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (curr[currentKey] === undefined) {
|
|
64
|
-
curr[currentKey] = {};
|
|
65
|
-
}
|
|
66
|
-
curr = curr[currentKey];
|
|
67
|
-
}
|
|
68
|
-
curr[finalKey] = value;
|
|
69
|
-
}
|
|
70
49
|
function getPropsForModule(fieldsJSON, fieldOverrides = {}) {
|
|
71
50
|
const props = {};
|
|
72
51
|
traverseFieldsJSON(fieldsJSON, fieldOverrides, (fieldPathArray, value) => {
|
|
73
|
-
|
|
52
|
+
setIn(props, fieldPathArray, value);
|
|
74
53
|
});
|
|
75
54
|
return props;
|
|
76
55
|
}
|
|
77
|
-
// TODO, move to cms-js-internal? (also in resolveModulesPlugin.ts)
|
|
78
|
-
function renderFieldsToJson(fields) {
|
|
79
|
-
let moduleJson;
|
|
80
|
-
const outputCallback = callbackJson => {
|
|
81
|
-
moduleJson = callbackJson;
|
|
82
|
-
};
|
|
83
|
-
renderToStaticMarkup(cloneElement(fields, {
|
|
84
|
-
outputCallback,
|
|
85
|
-
}));
|
|
86
|
-
return moduleJson;
|
|
87
|
-
}
|
|
88
56
|
export default renderModule;
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import request from 'request';
|
|
2
2
|
import { getSubDomainFromValidLocalDomain } from '../proxyPage/utils.js';
|
|
3
|
+
import { VALID_PROXY_DOMAIN_SUFFIXES } from '../proxyPage/constants.js';
|
|
4
|
+
// From(ish) https://git.hubteam.com/HubSpot/cloudflare-workers/blob/master/worker-lib/src/Constants.ts
|
|
5
|
+
const HCMS_PATH = '/_hcms/';
|
|
6
|
+
const HS_FS_PATH = '/hs-fs/';
|
|
7
|
+
const HUB_FS_PATH = '/hubfs/';
|
|
8
|
+
const isInternalCMSRoute = (req) => req.path.startsWith(HCMS_PATH) ||
|
|
9
|
+
req.path.startsWith(HS_FS_PATH) ||
|
|
10
|
+
req.path.startsWith(HUB_FS_PATH);
|
|
3
11
|
export default function proxyPageResourceRedirect(req, res, next) {
|
|
4
|
-
|
|
12
|
+
const isAHtmlRequest = req.accepts().includes('text/html');
|
|
13
|
+
if ((isAHtmlRequest && !isInternalCMSRoute(req)) ||
|
|
14
|
+
VALID_PROXY_DOMAIN_SUFFIXES.includes(req.hostname)) {
|
|
5
15
|
next();
|
|
6
16
|
return;
|
|
7
17
|
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// src/storybook/moduleStory.tsx
|
|
2
|
+
import React from "react";
|
|
3
|
+
import { renderToStaticMarkup } from "react-dom/server";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
function getFieldsArgTypes(fieldsJson, currentPathSegments = []) {
|
|
6
|
+
let resultArgTypes = {};
|
|
7
|
+
for (const field of fieldsJson) {
|
|
8
|
+
const fullFieldName = [...currentPathSegments, field.name].join(".");
|
|
9
|
+
switch (field.type) {
|
|
10
|
+
case "group": {
|
|
11
|
+
if (!field.children) {
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
const groupResult = getFieldsArgTypes(field.children, [
|
|
15
|
+
...currentPathSegments,
|
|
16
|
+
field.name
|
|
17
|
+
]);
|
|
18
|
+
resultArgTypes = {
|
|
19
|
+
...resultArgTypes,
|
|
20
|
+
...groupResult
|
|
21
|
+
};
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
case "boolean": {
|
|
25
|
+
resultArgTypes[fullFieldName] = {
|
|
26
|
+
control: "boolean",
|
|
27
|
+
defaultValue: field.default
|
|
28
|
+
};
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
case "text": {
|
|
32
|
+
resultArgTypes[fullFieldName] = {
|
|
33
|
+
control: "text",
|
|
34
|
+
defaultValue: field.default
|
|
35
|
+
};
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
default:
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return resultArgTypes;
|
|
43
|
+
}
|
|
44
|
+
function getNestedFieldObjectForArgs(_args) {
|
|
45
|
+
const result = {};
|
|
46
|
+
for (const argPath in _args) {
|
|
47
|
+
if (Object.prototype.hasOwnProperty.call(_args, argPath)) {
|
|
48
|
+
const argPathSegments = argPath.split(".");
|
|
49
|
+
const nestedSegments = argPathSegments.slice(0, -1);
|
|
50
|
+
const fieldName = argPathSegments[argPathSegments.length - 1];
|
|
51
|
+
const fieldValue = _args[argPath];
|
|
52
|
+
let currentNest = result;
|
|
53
|
+
for (const segment of nestedSegments) {
|
|
54
|
+
if (!Object.prototype.hasOwnProperty.call(currentNest, segment)) {
|
|
55
|
+
currentNest[segment] = {};
|
|
56
|
+
}
|
|
57
|
+
currentNest = currentNest[segment];
|
|
58
|
+
}
|
|
59
|
+
currentNest[fieldName] = fieldValue;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
function moduleFieldsToJson(fields) {
|
|
65
|
+
let moduleJson;
|
|
66
|
+
const outputCallback = (callbackJson) => {
|
|
67
|
+
moduleJson = callbackJson;
|
|
68
|
+
};
|
|
69
|
+
renderToStaticMarkup(
|
|
70
|
+
React.cloneElement(fields, {
|
|
71
|
+
outputCallback
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
if (!moduleJson) {
|
|
75
|
+
throw new Error("couldn't render module fields definition to fields.json");
|
|
76
|
+
}
|
|
77
|
+
return moduleJson;
|
|
78
|
+
}
|
|
79
|
+
function moduleStory(ModuleComponent, fields) {
|
|
80
|
+
const moduleJson = moduleFieldsToJson(fields);
|
|
81
|
+
const argTypes = getFieldsArgTypes(moduleJson);
|
|
82
|
+
const story = (_args) => {
|
|
83
|
+
const fieldValues = getNestedFieldObjectForArgs(_args);
|
|
84
|
+
return /* @__PURE__ */ jsx(ModuleComponent, {
|
|
85
|
+
...fieldValues
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
const storyWithArgs = story.bind({});
|
|
89
|
+
storyWithArgs.argTypes = argTypes;
|
|
90
|
+
storyWithArgs.args = {};
|
|
91
|
+
return storyWithArgs;
|
|
92
|
+
}
|
|
93
|
+
export {
|
|
94
|
+
getNestedFieldObjectForArgs,
|
|
95
|
+
moduleFieldsToJson,
|
|
96
|
+
moduleStory
|
|
97
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { mergeConfig } from 'vite';
|
|
3
|
+
const webRoot = process.env.WEB_ROOT;
|
|
4
|
+
const storyPath = path.resolve(process.cwd(), webRoot, '**/*.stories.*');
|
|
5
|
+
export default {
|
|
6
|
+
addons: ['@storybook/addon-essentials'],
|
|
7
|
+
framework: '@storybook/react-vite',
|
|
8
|
+
stories: [storyPath],
|
|
9
|
+
viteFinal: async (config) => {
|
|
10
|
+
const { resolveIslandPlugin } = await import('@hubspot/cms-js-internal');
|
|
11
|
+
const viteConfig = {
|
|
12
|
+
resolve: {
|
|
13
|
+
dedupe: ['react', 'react-dom', '@hubspot/cms-components'],
|
|
14
|
+
},
|
|
15
|
+
optimizeDeps: {
|
|
16
|
+
include: ['react', 'react-dom', 'react-dom/client'],
|
|
17
|
+
},
|
|
18
|
+
plugins: [resolveIslandPlugin()],
|
|
19
|
+
};
|
|
20
|
+
return mergeConfig(config, viteConfig);
|
|
21
|
+
},
|
|
22
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
4
|
+
function getFieldsArgTypes(fieldsJson, currentPathSegments = []) {
|
|
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
|
+
default:
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return resultArgTypes;
|
|
45
|
+
}
|
|
46
|
+
// default values come through via `defaultValue` in the story's argTypes, we
|
|
47
|
+
// need to re-nest them to match the field structure
|
|
48
|
+
export function getNestedFieldObjectForArgs(_args) {
|
|
49
|
+
const result = {};
|
|
50
|
+
for (const argPath in _args) {
|
|
51
|
+
if (Object.prototype.hasOwnProperty.call(_args, argPath)) {
|
|
52
|
+
const argPathSegments = argPath.split('.');
|
|
53
|
+
const nestedSegments = argPathSegments.slice(0, -1);
|
|
54
|
+
const fieldName = argPathSegments[argPathSegments.length - 1];
|
|
55
|
+
const fieldValue = _args[argPath];
|
|
56
|
+
let currentNest = result;
|
|
57
|
+
for (const segment of nestedSegments) {
|
|
58
|
+
if (!Object.prototype.hasOwnProperty.call(currentNest, segment)) {
|
|
59
|
+
currentNest[segment] = {};
|
|
60
|
+
}
|
|
61
|
+
currentNest = currentNest[segment];
|
|
62
|
+
}
|
|
63
|
+
currentNest[fieldName] = fieldValue;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
export function moduleFieldsToJson(fields) {
|
|
69
|
+
let moduleJson;
|
|
70
|
+
const outputCallback = (callbackJson) => {
|
|
71
|
+
moduleJson = callbackJson;
|
|
72
|
+
};
|
|
73
|
+
renderToStaticMarkup(React.cloneElement(fields, {
|
|
74
|
+
outputCallback,
|
|
75
|
+
}));
|
|
76
|
+
if (!moduleJson) {
|
|
77
|
+
throw new Error("couldn't render module fields definition to fields.json");
|
|
78
|
+
}
|
|
79
|
+
return moduleJson;
|
|
80
|
+
}
|
|
81
|
+
export function moduleStory(ModuleComponent, fields) {
|
|
82
|
+
const moduleJson = moduleFieldsToJson(fields);
|
|
83
|
+
const argTypes = getFieldsArgTypes(moduleJson);
|
|
84
|
+
const story = (_args) => {
|
|
85
|
+
const fieldValues = getNestedFieldObjectForArgs(_args);
|
|
86
|
+
return _jsx(ModuleComponent, { ...fieldValues });
|
|
87
|
+
};
|
|
88
|
+
const storyWithArgs = story.bind({});
|
|
89
|
+
storyWithArgs.argTypes = argTypes;
|
|
90
|
+
storyWithArgs.args = {};
|
|
91
|
+
return storyWithArgs;
|
|
92
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { ServerRenderContext, createServerRenderContext, ClientRenderContextProvider, } from '@hubspot/cms-components';
|
|
3
|
+
export const parameters = {
|
|
4
|
+
actions: { argTypesRegex: '^on[A-Z].*' },
|
|
5
|
+
};
|
|
6
|
+
const contextValue = createServerRenderContext('http://testUrl.com');
|
|
7
|
+
export const decorators = [
|
|
8
|
+
Story => (_jsx(ClientRenderContextProvider, { pageUrl: "http://test.com", islands: [], children: _jsx(ServerRenderContext.Provider, { value: contextValue, children: _jsx(Story, {}) }) })),
|
|
9
|
+
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubspot/cms-dev-server",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.13",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/createViteDevServer.js",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@hubspot/cli-lib": "^3.0.12",
|
|
24
|
-
"@hubspot/cms-components": "^0.2.
|
|
25
|
-
"@hubspot/cms-js-internal": "^0.2.
|
|
24
|
+
"@hubspot/cms-components": "^0.2.13",
|
|
25
|
+
"@hubspot/cms-js-internal": "^0.2.13",
|
|
26
26
|
"@vitejs/plugin-react": "^2.0.0",
|
|
27
27
|
"ansi-to-html": "0.7.x",
|
|
28
28
|
"express": "^4.17.3",
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"publishConfig": {
|
|
65
65
|
"access": "public"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "64c2a9408378aad4431a7b0ed04a35178cebdbae"
|
|
68
68
|
}
|
package/dist/lib/argHelpers.js
DELETED
package/dist/lib/htmlHelpers.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export const rewriteAbsoluteJsAndCssPaths = (body, host, protocol = 'https://') => {
|
|
2
|
-
// TODO, do we need to worry about any fully relative references? (./whatever/...)
|
|
3
|
-
// Maybe the renderer doesn't typically output those, and when customers do that manually
|
|
4
|
-
// it (probably?) is after the `<base>` element?
|
|
5
|
-
return body.replace(/(src|href)="(\/[^/])/g, `$1="${protocol}${host}$2`);
|
|
6
|
-
};
|
|
7
|
-
export const addBaseTag = (html, urlToProxy) => {
|
|
8
|
-
const baseDomain = new URL(urlToProxy).origin;
|
|
9
|
-
return html.replace('<head>', `<head>\n<base href="${baseDomain}">\n`);
|
|
10
|
-
};
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { readdirSync } from 'fs';
|
|
3
|
-
const PARTIAL_SOURCE_DIR = 'components/partials';
|
|
4
|
-
function getAllComponentFilenamesIn(dirPath) {
|
|
5
|
-
return readdirSync(dirPath).filter(filename => {
|
|
6
|
-
const ext = path.extname(filename);
|
|
7
|
-
return ext === '.jsx' || ext === '.tsx';
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
const buildMockPartialModule = entryPointsFileNames => {
|
|
11
|
-
return entryPointsFileNames
|
|
12
|
-
.map(filename => {
|
|
13
|
-
const ext = path.extname(filename);
|
|
14
|
-
const withoutExtension = path.basename(filename, ext);
|
|
15
|
-
return `export const ${withoutExtension} = () => import('/${PARTIAL_SOURCE_DIR}/${filename}');`;
|
|
16
|
-
})
|
|
17
|
-
.join('\n');
|
|
18
|
-
};
|
|
19
|
-
// Not needed in dev unless some code wants to do `import { SomePartial } from 'components/partials'`
|
|
20
|
-
export default function devMockClientEntryPlugin(webRoot) {
|
|
21
|
-
const ENTRY_POINTS_PATH = path.resolve(webRoot, PARTIAL_SOURCE_DIR);
|
|
22
|
-
const MOCK_ENTRY_PATH = `${ENTRY_POINTS_PATH}/index.js`;
|
|
23
|
-
return {
|
|
24
|
-
enforce: 'pre',
|
|
25
|
-
name: 'client-entry',
|
|
26
|
-
options(options) {
|
|
27
|
-
if (!options.input) {
|
|
28
|
-
return {
|
|
29
|
-
...options,
|
|
30
|
-
input: [MOCK_ENTRY_PATH],
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
return options;
|
|
34
|
-
},
|
|
35
|
-
async resolveId(source, importer, options) {
|
|
36
|
-
if (source === PARTIAL_SOURCE_DIR /*&& options.isEntry*/) {
|
|
37
|
-
return {
|
|
38
|
-
id: source,
|
|
39
|
-
moduleSideEffects: 'no-treeshake',
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
return null;
|
|
43
|
-
},
|
|
44
|
-
load(id) {
|
|
45
|
-
if (id === PARTIAL_SOURCE_DIR) {
|
|
46
|
-
const entryPointsFileNames = getAllComponentFilenamesIn(ENTRY_POINTS_PATH);
|
|
47
|
-
return buildMockPartialModule(entryPointsFileNames);
|
|
48
|
-
}
|
|
49
|
-
return null;
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
}
|