@hubspot/cms-dev-server 0.2.11 → 0.2.12

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.
@@ -1,4 +1 @@
1
- export const VALID_PROXY_DOMAIN_SUFFIXES = [
2
- 'localhost',
3
- // 'hslocal.dev', // TODO not real yet
4
- ];
1
+ export const VALID_PROXY_DOMAIN_SUFFIXES = ['localhost', 'hslocal.net'];
@@ -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
- if (req.accepts().includes('text/html') || req.hostname === 'localhost') {
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.11",
3
+ "version": "0.2.12",
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.11",
25
- "@hubspot/cms-js-internal": "^0.2.11",
24
+ "@hubspot/cms-components": "^0.2.12",
25
+ "@hubspot/cms-js-internal": "^0.2.12",
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": "12a8018212555054fb781aa65524179669bcc76d"
67
+ "gitHead": "0eb77bb46f29a3260d895f404ed9a0b116e895be"
68
68
  }
@@ -1,6 +0,0 @@
1
- export const getLongArgumentFrom = (args, argToLookFor) => {
2
- const argIndex = process.argv.indexOf(argToLookFor);
3
- if (argIndex > 0 && argIndex <= args.length) {
4
- return args[argIndex + 1];
5
- }
6
- };
@@ -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
- }