@hubspot/cms-dev-server 1.1.0-prerelease.4 → 1.1.0-prerelease.6
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/LICENSE +12 -0
- package/dist/lib/createIslandsGlobalJsForClientInDev.js +32 -0
- package/dist/lib/templates.js +2 -3
- package/dist/lib/traverseFieldsJson.js +78 -0
- package/dist/proxyPage/proxyPage.js +1 -1
- package/dist/render/renderOnServer.js +40 -0
- package/dist/render/renderOnServer.test.js +24 -0
- package/dist/render/renderStreamToPromise.js +35 -0
- package/dist/resolveSharedDeps.js +28 -0
- package/package.json +5 -5
- package/dist/plugins/devMockClientEntryPlugin.js +0 -52
package/LICENSE
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Copyright 2022 HubSpot, Inc.
|
|
2
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
you may not use this file except in compliance with the License.
|
|
4
|
+
You may obtain a copy of the License at
|
|
5
|
+
|
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
|
|
8
|
+
Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
See the License for the specific language governing permissions and
|
|
12
|
+
limitations under the License.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const writeIslandDataToJsGlobal = (tweakedIslandsArray, indent = 0) => {
|
|
2
|
+
const indentString = new Array(indent).fill(' ').join('');
|
|
3
|
+
return `window.__islands = ${JSON.stringify(tweakedIslandsArray.map(({ id, moduleName, moduleId, props, hydrateOn, clientOnly, priority, url, }) => {
|
|
4
|
+
return {
|
|
5
|
+
id,
|
|
6
|
+
moduleName,
|
|
7
|
+
moduleId,
|
|
8
|
+
props,
|
|
9
|
+
hydrateOn,
|
|
10
|
+
clientOnly,
|
|
11
|
+
priority,
|
|
12
|
+
url,
|
|
13
|
+
};
|
|
14
|
+
}), null, 2).replace(/\n/gm, `\n${indentString}`)};`;
|
|
15
|
+
};
|
|
16
|
+
function getIslandUrls(roots, viteDevServer) {
|
|
17
|
+
return roots.map(island => {
|
|
18
|
+
const islandModule = viteDevServer.moduleGraph.getModuleById(island.moduleId);
|
|
19
|
+
if (!islandModule) {
|
|
20
|
+
throw new Error(`Unknown island moduleId: ${island.moduleId}`);
|
|
21
|
+
}
|
|
22
|
+
const tweakedIsland = {
|
|
23
|
+
...island,
|
|
24
|
+
url: islandModule.url,
|
|
25
|
+
};
|
|
26
|
+
return tweakedIsland;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
export const createIslandsGlobalJsForClientInDev = (roots, viteDevServer, indent) => {
|
|
30
|
+
// Assuming island roots have already been sorted before being passed in here
|
|
31
|
+
return writeIslandDataToJsGlobal(getIslandUrls(roots, viteDevServer), indent);
|
|
32
|
+
};
|
package/dist/lib/templates.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* hs-eslint ignored failing-rules */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-shadow */
|
|
3
3
|
import { promises as fsPromises } from 'fs';
|
|
4
|
-
import {
|
|
4
|
+
import { createIslandsGlobalJsForClientInDev, getServerGlobalsToInject, } from '@hubspot/cms-js-internal';
|
|
5
5
|
const { readFile } = fsPromises;
|
|
6
6
|
const ISLAND_RUNTIME_AND_SETUP_SCRIPT_BLOCK = `<script type="module">
|
|
7
7
|
import { setupIslandHydration } from '@hubspot/cms-components/island-runtime';
|
|
@@ -25,9 +25,8 @@ export const createHtmlShellDependingOnRenderContext = async (baseTemplatePath,
|
|
|
25
25
|
let htmlTemplate;
|
|
26
26
|
if (renderContext.islands.length) {
|
|
27
27
|
htmlTemplate = await createIslandHtmlTemplate(baseTemplatePath, title);
|
|
28
|
-
const sortedIslandRoots = sortIslandRootsByPriority(renderContext.islands);
|
|
29
28
|
htmlTemplate = htmlTemplate.replace('<!--islandsglobal-->', `<script>
|
|
30
|
-
${createIslandsGlobalJsForClientInDev(
|
|
29
|
+
${createIslandsGlobalJsForClientInDev(renderContext.islands, viteServer, 4)}
|
|
31
30
|
</script>
|
|
32
31
|
${getServerGlobalsToInject(renderContext.pageUrl)}
|
|
33
32
|
`);
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const traverseFieldsJSONHelper = (fields, values, handle, path // Number path elements are for repeaters
|
|
2
|
+
) => {
|
|
3
|
+
if (!fields) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
for (const field of fields) {
|
|
7
|
+
let value = values ? values[field.name] : undefined;
|
|
8
|
+
if (value === undefined) {
|
|
9
|
+
value = getDefaultValue(field);
|
|
10
|
+
}
|
|
11
|
+
if (field.occurrence) {
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
value.forEach((v, i) => {
|
|
14
|
+
handle([...path, field.name, i], v, field);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
else if (field.type === 'group') {
|
|
19
|
+
traverseFieldsJSONHelper(field.children, value, handle, [
|
|
20
|
+
...path,
|
|
21
|
+
field.name,
|
|
22
|
+
]);
|
|
23
|
+
}
|
|
24
|
+
else if (value !== undefined) {
|
|
25
|
+
handle([...path, field.name], value, field);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const traverseFieldsJSON = (fields, values, handle) => {
|
|
30
|
+
return traverseFieldsJSONHelper(fields, values, handle, []);
|
|
31
|
+
};
|
|
32
|
+
const getDefaultValue = (field) => {
|
|
33
|
+
if (field.type === 'group') {
|
|
34
|
+
if (field.occurrence) {
|
|
35
|
+
const count = field.occurrence.default || field.occurrence.min;
|
|
36
|
+
return new Array(count).fill(field.default[0]);
|
|
37
|
+
}
|
|
38
|
+
const result = {};
|
|
39
|
+
for (const childField of field.children) {
|
|
40
|
+
if (childField.type !== 'group' && childField.default) {
|
|
41
|
+
result[childField.name] = getDefaultValue(childField);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
else if (field.occurrence) {
|
|
47
|
+
const scalarValue = getDefaultScalarValue(field);
|
|
48
|
+
const count = field.occurrence.default || field.occurrence.min;
|
|
49
|
+
return new Array(count).fill(scalarValue);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return field.default;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
// If occurrence.default is set, then default value could be an array
|
|
56
|
+
// and we just want to grab the scalar value from that array
|
|
57
|
+
function getDefaultScalarValue(field) {
|
|
58
|
+
if (Array.isArray(field.default)) {
|
|
59
|
+
const firstValue = field.default[0];
|
|
60
|
+
// email field values are arrays of strings (email addresses)
|
|
61
|
+
// so check if it's an array of arrays
|
|
62
|
+
if (valueIsArray(field)) {
|
|
63
|
+
return Array.isArray(firstValue) ? firstValue : field.default;
|
|
64
|
+
}
|
|
65
|
+
return firstValue;
|
|
66
|
+
}
|
|
67
|
+
return field.default;
|
|
68
|
+
}
|
|
69
|
+
const CHOICE_FIELD_TYPE = 'choice';
|
|
70
|
+
const EMAIL_FIELD_TYPE = 'email';
|
|
71
|
+
const SIMPLE_MENU_FIELD_TYPE = 'simple_menu';
|
|
72
|
+
// Stolen from https://git.hubteam.com/HubSpot/ContentUILib/blob/ede7354d6265d7976c29bc55b692602885c28c96/ContentUtils/static/js/helpers/FieldHelpers.js#L56
|
|
73
|
+
function valueIsArray(field) {
|
|
74
|
+
return (field.type === EMAIL_FIELD_TYPE ||
|
|
75
|
+
field.type === SIMPLE_MENU_FIELD_TYPE ||
|
|
76
|
+
(field.type === CHOICE_FIELD_TYPE && field.multiple));
|
|
77
|
+
}
|
|
78
|
+
export default traverseFieldsJSON;
|
|
@@ -98,7 +98,7 @@ export default async function proxySpecificPage(response, portalId, contentId, w
|
|
|
98
98
|
const { previewKey } = previewInfo;
|
|
99
99
|
const pageHtml = await makeProxyPreviewRequest(accountId, urlToProxy, previewKey, contentId);
|
|
100
100
|
// Loading through vite so all of nested `@hubspot/cms-*` imports work
|
|
101
|
-
const { sortIslandRootsByPriority } = await viteServer.ssrLoadModule('@hubspot/cms-
|
|
101
|
+
const { sortIslandRootsByPriority } = await viteServer.ssrLoadModule('@hubspot/cms-components');
|
|
102
102
|
const localRenderPlaceholders = findLocalRenderPlaceholders(pageHtml);
|
|
103
103
|
const numPlaceholders = localRenderPlaceholders.length;
|
|
104
104
|
let numJsRenderFailures = 0;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { renderStreamToPromise } from '@hubspot/cms-js-internal';
|
|
3
|
+
import { ServerRenderContext, createServerRenderContext, } from '@hubspot/cms-components';
|
|
4
|
+
// Slightly useful to have this so some files don't have to be tsx just to make
|
|
5
|
+
// the renderOnServer call (i.e. need JSX to do `<Component {...props} />`)
|
|
6
|
+
export async function renderComponentOnServer(Component, props, pageUrl) {
|
|
7
|
+
const componentDebugName = Component.displayName || Component.name;
|
|
8
|
+
return renderOnServer(_jsx(Component, { ...props }), pageUrl, componentDebugName);
|
|
9
|
+
}
|
|
10
|
+
export async function renderOnServer(elements, pageUrl, debugName) {
|
|
11
|
+
const renderContext = createServerRenderContext(pageUrl);
|
|
12
|
+
let html = await renderStreamToPromise(_jsx(ServerRenderContext.Provider, { value: renderContext, children: elements }));
|
|
13
|
+
if (renderContext.messagesForBrowser.length > 0) {
|
|
14
|
+
html = `${html}${scriptTagToLogServerMessages(renderContext.messagesForBrowser, `Server messages rendering: ${debugName}`)}`;
|
|
15
|
+
}
|
|
16
|
+
return { html, renderContext };
|
|
17
|
+
}
|
|
18
|
+
function scriptTagToLogServerMessages(messagesForBrowser, groupLabel) {
|
|
19
|
+
if (!messagesForBrowser) {
|
|
20
|
+
return '';
|
|
21
|
+
}
|
|
22
|
+
const consoleLines = [];
|
|
23
|
+
for (const message of messagesForBrowser) {
|
|
24
|
+
const funcString = message.level === 'error' ? 'console.error' : 'console.warn';
|
|
25
|
+
const argString = message.parameters
|
|
26
|
+
.map(arg => JSON.stringify(arg))
|
|
27
|
+
.join(', ');
|
|
28
|
+
consoleLines.push(` ${funcString}(${argString});`);
|
|
29
|
+
}
|
|
30
|
+
let groupStart = '';
|
|
31
|
+
let groupEnd = '';
|
|
32
|
+
if (groupLabel !== null) {
|
|
33
|
+
groupStart = `\n console.group(${JSON.stringify(groupLabel)});`;
|
|
34
|
+
groupEnd = ` console.groupEnd()\n`;
|
|
35
|
+
}
|
|
36
|
+
return `<script>${groupStart}
|
|
37
|
+
${consoleLines.join('\n')}
|
|
38
|
+
${groupEnd}</script>
|
|
39
|
+
`;
|
|
40
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { renderComponentOnServer, renderOnServer } from './renderOnServer.js';
|
|
4
|
+
import { getByText } from '@testing-library/dom';
|
|
5
|
+
import { JSDOM } from 'jsdom';
|
|
6
|
+
function TestComponent({ text }) {
|
|
7
|
+
return _jsx("div", { children: text });
|
|
8
|
+
}
|
|
9
|
+
describe('renderOnServer', () => {
|
|
10
|
+
it('should render elements', async () => {
|
|
11
|
+
const result = await renderOnServer(_jsx(TestComponent, { text: 'test' }), 'http://test.com', 'debug_name');
|
|
12
|
+
const dom = new JSDOM(result.html);
|
|
13
|
+
expect(getByText(dom.window.document.body, 'test')).toBeTruthy();
|
|
14
|
+
expect(result.renderContext.pageUrl).toBe('http://test.com');
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
describe('renderComponentOnServer', () => {
|
|
18
|
+
it('should render components', async () => {
|
|
19
|
+
const result = await renderComponentOnServer(TestComponent, { text: 'test' }, 'http://test.com');
|
|
20
|
+
const dom = new JSDOM(result.html);
|
|
21
|
+
expect(getByText(dom.window.document.body, 'test')).toBeTruthy();
|
|
22
|
+
expect(result.renderContext.pageUrl).toBe('http://test.com');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import stream from 'stream';
|
|
2
|
+
import { renderToPipeableStream } from 'react-dom/server';
|
|
3
|
+
const promiseForNewWritableStream = () => {
|
|
4
|
+
const chunks = [];
|
|
5
|
+
const writableStream = new stream.Writable();
|
|
6
|
+
writableStream._write = (chunk, encoding, done) => {
|
|
7
|
+
chunks.push(Buffer.from(chunk));
|
|
8
|
+
done();
|
|
9
|
+
};
|
|
10
|
+
const streamDonePromise = new Promise((resolve, reject) => {
|
|
11
|
+
writableStream.on('error', error => {
|
|
12
|
+
reject(error);
|
|
13
|
+
});
|
|
14
|
+
writableStream._final = done => {
|
|
15
|
+
resolve(Buffer.concat(chunks).toString('utf8'));
|
|
16
|
+
done();
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
return { streamDonePromise, writableStream };
|
|
20
|
+
};
|
|
21
|
+
export const renderStreamToPromise = async (reactElements) => {
|
|
22
|
+
const { streamDonePromise, writableStream } = promiseForNewWritableStream();
|
|
23
|
+
const { pipe /* abort */ } = renderToPipeableStream(reactElements, {
|
|
24
|
+
onShellReady() {
|
|
25
|
+
// customOutputStream.write('<!-- After onCompleteShell -->\n');
|
|
26
|
+
},
|
|
27
|
+
onAllReady() {
|
|
28
|
+
pipe(writableStream);
|
|
29
|
+
},
|
|
30
|
+
// We will log the error sent to the writable, any need to do something in these callbacks?
|
|
31
|
+
// onShellError(error) {},
|
|
32
|
+
// onError(error) {},
|
|
33
|
+
});
|
|
34
|
+
return streamDonePromise;
|
|
35
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import enhancedResolve from 'enhanced-resolve';
|
|
2
|
+
import { cwd } from 'process';
|
|
3
|
+
/**
|
|
4
|
+
* Vite plugin that resolves dependencies to cms-js-platform over webRoot
|
|
5
|
+
* client build: resolve deps to cms-js-platform version
|
|
6
|
+
* server build: mark deps as external
|
|
7
|
+
*/
|
|
8
|
+
export default function resolveSharedDeps(absoluteWebRoot) {
|
|
9
|
+
console.log(absoluteWebRoot);
|
|
10
|
+
const sharedDeps = [
|
|
11
|
+
'@hubspot/cms-components',
|
|
12
|
+
'@hubspot/cms-js-internal',
|
|
13
|
+
'react',
|
|
14
|
+
'react-dom',
|
|
15
|
+
];
|
|
16
|
+
return {
|
|
17
|
+
name: 'resolve-shared-deps',
|
|
18
|
+
enforce: 'pre',
|
|
19
|
+
async resolveId(source, __importer, __options) {
|
|
20
|
+
if (sharedDeps.some(sharedDep => source.startsWith(sharedDep))) {
|
|
21
|
+
const resolvedSource = enhancedResolve.sync(cwd(), source);
|
|
22
|
+
console.log(`RESOLVING::: ${source} TO ${resolvedSource}`);
|
|
23
|
+
return { id: resolvedSource, external: 'relative' };
|
|
24
|
+
}
|
|
25
|
+
return Promise.resolve(null);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubspot/cms-dev-server",
|
|
3
|
-
"version": "1.1.0-prerelease.
|
|
3
|
+
"version": "1.1.0-prerelease.6",
|
|
4
|
+
"license": "Apache-2.0",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"main": "./dist/createViteDevServer.js",
|
|
6
7
|
"scripts": {
|
|
@@ -17,8 +18,8 @@
|
|
|
17
18
|
},
|
|
18
19
|
"dependencies": {
|
|
19
20
|
"@hubspot/cli-lib": "^3.0.12",
|
|
20
|
-
"@hubspot/cms-components": "^1.1.0-prerelease.
|
|
21
|
-
"@hubspot/cms-js-internal": "^1.1.0-prerelease.
|
|
21
|
+
"@hubspot/cms-components": "^1.1.0-prerelease.6",
|
|
22
|
+
"@hubspot/cms-js-internal": "^1.1.0-prerelease.6",
|
|
22
23
|
"@vitejs/plugin-react": "^2.0.0",
|
|
23
24
|
"ansi-to-html": "0.7.x",
|
|
24
25
|
"express": "^4.17.3",
|
|
@@ -36,7 +37,6 @@
|
|
|
36
37
|
"copyfiles": "^2.4.1",
|
|
37
38
|
"nodemon": "^2.0.15",
|
|
38
39
|
"ts-node": "^10.7.0",
|
|
39
|
-
"ts-paths-esm-loader": "^1.3.0",
|
|
40
40
|
"vitest": "0.23.4"
|
|
41
41
|
},
|
|
42
42
|
"bin": {
|
|
@@ -61,5 +61,5 @@
|
|
|
61
61
|
"publishConfig": {
|
|
62
62
|
"access": "public"
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "5c7ad978bb67ee8308c91cdffd226c00bc47d20c"
|
|
65
65
|
}
|
|
@@ -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
|
-
}
|