@hubspot/cms-dev-server 1.1.0-prerelease.7 → 1.1.0-prerelease.9
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/createIslandsGlobalJsForClientInDev.js +32 -0
- package/dist/lib/renderHelpers.js +34 -10
- package/dist/lib/traverseFieldsJson.js +78 -0
- 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/dist/routes/module.js +2 -1
- package/dist/routes/partial.js +2 -1
- package/package.json +4 -4
- package/dist/plugins/devMockClientEntryPlugin.js +0 -52
- package/dist/routes/proxyPage.js +0 -205
- package/dist/run.mjs +0 -10
- package/dist/src/components/ModulesList.js +0 -14
- package/dist/src/components/PartialsList.js +0 -13
- package/dist/src/createDevServer.js +0 -45
- package/dist/src/lib/htmlHelpers.js +0 -10
- package/dist/src/lib/moduleGraph.js +0 -9
- package/dist/src/lib/proxyHelpers.js +0 -55
- package/dist/src/lib/proxyHelpers.test.js +0 -136
- package/dist/src/lib/renderComponentWithCSS.js +0 -26
- package/dist/src/lib/renderModule.js +0 -84
- package/dist/src/lib/renderPartial.js +0 -11
- package/dist/src/lib/templates.js +0 -50
- package/dist/src/plugins/devMockClientEntryPlugin.js +0 -52
- package/dist/src/proxyPage/constants.js +0 -4
- package/dist/src/proxyPage/fetchContentMetadata.js +0 -40
- package/dist/src/proxyPage/fetchPreviewInfo.js +0 -25
- package/dist/src/proxyPage/proxyPage.js +0 -147
- package/dist/src/proxyPage/renderLocalError.js +0 -1
- package/dist/src/proxyPage/utils.js +0 -36
- package/dist/src/routes/createDevServerRoutes.js +0 -19
- package/dist/src/routes/homeRouteHandler.js +0 -12
- package/dist/src/routes/linksToAllModulesRouteHandler.js +0 -10
- package/dist/src/routes/linksToAllPartialsRouteHandler.js +0 -10
- package/dist/src/routes/module.js +0 -26
- package/dist/src/routes/partial.js +0 -26
- package/dist/src/routes/proxyPageRouteHandler.js +0 -28
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import express from 'express';
|
|
2
|
-
import { createServer as createViteServer } from 'vite';
|
|
3
|
-
import { default as react } from '@vitejs/plugin-react';
|
|
4
|
-
import createDevServerRoutes from './routes/createDevServerRoutes.js';
|
|
5
|
-
import devMockClientEntryPlugin from './plugins/devMockClientEntryPlugin.js';
|
|
6
|
-
import { resolveIslandPlugin, resolveWebRootDepsPlugin, } from '@hubspot/cms-js-internal';
|
|
7
|
-
const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD;
|
|
8
|
-
const BASE_PATH = '/';
|
|
9
|
-
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
|
|
10
|
-
async function createDevServer(packageRoot, webRoot) {
|
|
11
|
-
const expressServer = express();
|
|
12
|
-
const viteServer = await createViteServer({
|
|
13
|
-
appType: 'custom',
|
|
14
|
-
server: {
|
|
15
|
-
middlewareMode: true,
|
|
16
|
-
origin: `http://localhost:${PORT}`,
|
|
17
|
-
},
|
|
18
|
-
ssr: {
|
|
19
|
-
// TODO: might need more entries here to get things to resolve through
|
|
20
|
-
// resolveWebRootDepsPlugin
|
|
21
|
-
noExternal: '@hubspot/cms-components',
|
|
22
|
-
},
|
|
23
|
-
optimizeDeps: {
|
|
24
|
-
include: ['react', 'react-dom', 'react-dom/client'],
|
|
25
|
-
},
|
|
26
|
-
build: {},
|
|
27
|
-
plugins: [
|
|
28
|
-
react({ jsxRuntime: 'automatic' }),
|
|
29
|
-
// @ts-ignore
|
|
30
|
-
devMockClientEntryPlugin(webRoot),
|
|
31
|
-
// @ts-ignore
|
|
32
|
-
resolveIslandPlugin(),
|
|
33
|
-
resolveWebRootDepsPlugin(webRoot, packageRoot),
|
|
34
|
-
],
|
|
35
|
-
});
|
|
36
|
-
expressServer.locals.devServerHost = 'localhost';
|
|
37
|
-
expressServer.locals.devServerPort = PORT;
|
|
38
|
-
expressServer.locals.packageRoot = packageRoot;
|
|
39
|
-
expressServer.locals.webRoot = webRoot;
|
|
40
|
-
expressServer.locals.viteServer = viteServer;
|
|
41
|
-
expressServer.use(BASE_PATH, await createDevServerRoutes(packageRoot, webRoot, viteServer));
|
|
42
|
-
expressServer.listen(PORT);
|
|
43
|
-
console.log(`HubSpot CMS dev server listening at http://localhost:${PORT}`);
|
|
44
|
-
}
|
|
45
|
-
export default createDevServer;
|
|
@@ -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,9 +0,0 @@
|
|
|
1
|
-
import { COMPONENT_EXTENSIONS } from '@hubspot/cms-js-internal';
|
|
2
|
-
export const findFullPathInModuleGraph = (modulePathNoExtension, moduleGraph) => {
|
|
3
|
-
for (let extension of COMPONENT_EXTENSIONS) {
|
|
4
|
-
const pathToTry = `${modulePathNoExtension}${extension}`;
|
|
5
|
-
if (moduleGraph.fileToModulesMap.has(pathToTry)) {
|
|
6
|
-
return pathToTry;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
};
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
const localJsRenderStartString = '<!-- proxy local render of "';
|
|
2
|
-
// const localJsRenderStartRegex = /<!-- proxy local render of "([^"]+)"/;
|
|
3
|
-
const localJsRenderEndString = 'end proxy local render -->';
|
|
4
|
-
export const findLocalRenderPlaceholders = (body) => {
|
|
5
|
-
let offsetSoFar = 0;
|
|
6
|
-
const result = [];
|
|
7
|
-
while (offsetSoFar < body.length && offsetSoFar !== -1) {
|
|
8
|
-
// Get start of local render block
|
|
9
|
-
const startOffset = body.indexOf(localJsRenderStartString, offsetSoFar);
|
|
10
|
-
offsetSoFar = startOffset;
|
|
11
|
-
if (startOffset >= 0) {
|
|
12
|
-
const nextNewlineOffset = body.indexOf('\n', startOffset + localJsRenderStartString.length);
|
|
13
|
-
const pathEndOffset = body.indexOf('"', startOffset + localJsRenderStartString.length);
|
|
14
|
-
const nearestNewlineOrQuoteAfterBlockOpen = Math.min(nextNewlineOffset, pathEndOffset);
|
|
15
|
-
// Keep on moving our "where we've scanned so far" pointer
|
|
16
|
-
offsetSoFar = nearestNewlineOrQuoteAfterBlockOpen;
|
|
17
|
-
if (pathEndOffset >= 0 && pathEndOffset < nextNewlineOffset) {
|
|
18
|
-
// Pull the path out of double quotes after the block opening
|
|
19
|
-
const path = body.substring(startOffset + localJsRenderStartString.length, pathEndOffset);
|
|
20
|
-
offsetSoFar = pathEndOffset + 1;
|
|
21
|
-
// console.log(`${startOffset}: path = ${path}`);
|
|
22
|
-
// If we're in a valid bloc so far that exists and has a path...
|
|
23
|
-
if (startOffset >= 0 && path) {
|
|
24
|
-
const nextCommentEndOffset = body.indexOf('-->', pathEndOffset);
|
|
25
|
-
const endOffset = body.indexOf(localJsRenderEndString, pathEndOffset);
|
|
26
|
-
offsetSoFar = Math.min(nextCommentEndOffset, endOffset);
|
|
27
|
-
// Make sure that there is an ending to the block and there is not another
|
|
28
|
-
// HTML end comment string before it
|
|
29
|
-
if (endOffset >= startOffset && endOffset < nextCommentEndOffset) {
|
|
30
|
-
// Then pull out the JSON in the middle and try to parse it
|
|
31
|
-
const jsonString = body
|
|
32
|
-
.substring(pathEndOffset + 1, endOffset)
|
|
33
|
-
.trim();
|
|
34
|
-
// console.log(`${endOffset}: jsonString = ${jsonString}`);
|
|
35
|
-
try {
|
|
36
|
-
const json = JSON.parse(jsonString);
|
|
37
|
-
result.push({
|
|
38
|
-
startOffset,
|
|
39
|
-
endOffset: endOffset + localJsRenderEndString.length,
|
|
40
|
-
path,
|
|
41
|
-
json,
|
|
42
|
-
});
|
|
43
|
-
offsetSoFar = endOffset + localJsRenderEndString.length;
|
|
44
|
-
}
|
|
45
|
-
catch (e) {
|
|
46
|
-
console.error(`Invalid JSON inside <!-- proxy local render .. --> string (around offset ${pathEndOffset})`);
|
|
47
|
-
offsetSoFar = endOffset;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return result;
|
|
55
|
-
};
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { findLocalRenderPlaceholders } from './proxyHelpers.js';
|
|
3
|
-
describe('proxyHelpers', () => {
|
|
4
|
-
describe('findLocalRenderPlaceholders', () => {
|
|
5
|
-
it('should do nothing with an empty string', () => {
|
|
6
|
-
const result = findLocalRenderPlaceholders('');
|
|
7
|
-
expect(result).toEqual([]);
|
|
8
|
-
});
|
|
9
|
-
it('should do nothing with a regular HTML comment', () => {
|
|
10
|
-
const result = findLocalRenderPlaceholders('<!-- -->');
|
|
11
|
-
expect(result).toEqual([]);
|
|
12
|
-
});
|
|
13
|
-
it('should do nothing with a regular HTML comment across newlines', () => {
|
|
14
|
-
const result = findLocalRenderPlaceholders(`<!--
|
|
15
|
-
-->`);
|
|
16
|
-
expect(result).toEqual([]);
|
|
17
|
-
});
|
|
18
|
-
it('should do nothing with a never ending HTML comment', () => {
|
|
19
|
-
const result = findLocalRenderPlaceholders(`<!--`);
|
|
20
|
-
expect(result).toEqual([]);
|
|
21
|
-
});
|
|
22
|
-
it('extracts placeholder with nothing before or after it', () => {
|
|
23
|
-
const result = findLocalRenderPlaceholders(`<!-- proxy local render of "/some/path/to/entry.js"
|
|
24
|
-
{
|
|
25
|
-
"the": "props",
|
|
26
|
-
"that": ["need", "to"],
|
|
27
|
-
"be": {
|
|
28
|
-
"passed": "along"
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
end proxy local render -->`);
|
|
32
|
-
expect(result.length).toBe(1);
|
|
33
|
-
expect(result[0]).toMatchSnapshot();
|
|
34
|
-
});
|
|
35
|
-
it('extracts placeholder with some HTML around it', () => {
|
|
36
|
-
const result = findLocalRenderPlaceholders(`
|
|
37
|
-
<p>asdf</p>
|
|
38
|
-
<div>
|
|
39
|
-
<!-- proxy local render of "/some/path/to/entry.js"
|
|
40
|
-
{
|
|
41
|
-
"the": "props",
|
|
42
|
-
"that": ["need", "to"],
|
|
43
|
-
"be": {
|
|
44
|
-
"passed": "along"
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
end proxy local render -->
|
|
48
|
-
</div>
|
|
49
|
-
|
|
50
|
-
<em>FIN</em>
|
|
51
|
-
`);
|
|
52
|
-
expect(result.length).toBe(1);
|
|
53
|
-
expect(result[0]).toMatchSnapshot();
|
|
54
|
-
});
|
|
55
|
-
it('extacts two placholder blocks immediately after one another', () => {
|
|
56
|
-
const result = findLocalRenderPlaceholders(`<p>asdf</p>
|
|
57
|
-
<div>
|
|
58
|
-
<!-- proxy local render of "/some/path/to/entry.js"
|
|
59
|
-
{
|
|
60
|
-
"the": "props",
|
|
61
|
-
"that": ["need", "to"],
|
|
62
|
-
"be": {
|
|
63
|
-
"passed": "along"
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
end proxy local render --><!-- proxy local render of "/some/path/to/entry2.js"
|
|
67
|
-
{
|
|
68
|
-
"the": "props22222",
|
|
69
|
-
"that": ["need", "to"],
|
|
70
|
-
"be": {
|
|
71
|
-
"passed": "along"
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
end proxy local render -->
|
|
75
|
-
</div>
|
|
76
|
-
|
|
77
|
-
<em>FIN</em>
|
|
78
|
-
`);
|
|
79
|
-
expect(result.length).toBe(2);
|
|
80
|
-
expect(result[0]).toMatchSnapshot();
|
|
81
|
-
expect(result[1]).toMatchSnapshot();
|
|
82
|
-
});
|
|
83
|
-
it('extacts a placeholder with empty json', () => {
|
|
84
|
-
const result = findLocalRenderPlaceholders(`<!-- proxy local render of "/some/path/to/entry.js"
|
|
85
|
-
{
|
|
86
|
-
}
|
|
87
|
-
end proxy local render -->`);
|
|
88
|
-
expect(result.length).toBe(1);
|
|
89
|
-
expect(result[0]).toMatchSnapshot();
|
|
90
|
-
});
|
|
91
|
-
it('extacts a placeholder with empty json - 2', () => {
|
|
92
|
-
const result = findLocalRenderPlaceholders(`<!-- proxy local render of "/some/path/to/entry.js"
|
|
93
|
-
{}
|
|
94
|
-
end proxy local render -->`);
|
|
95
|
-
expect(result.length).toBe(1);
|
|
96
|
-
expect(result[0]).toMatchSnapshot();
|
|
97
|
-
});
|
|
98
|
-
it('extacts one placholder after a broken incomplete path', () => {
|
|
99
|
-
const result = findLocalRenderPlaceholders(`<!-- proxy local render of "incomplete path
|
|
100
|
-
-->
|
|
101
|
-
<!-- proxy local render of "/some/path/to/afterMissingPath.js"
|
|
102
|
-
{
|
|
103
|
-
}
|
|
104
|
-
end proxy local render -->`);
|
|
105
|
-
expect(result.length).toBe(1);
|
|
106
|
-
expect(result[0]).toMatchSnapshot();
|
|
107
|
-
});
|
|
108
|
-
it('extacts one placholder after a uncompleted regular HTML comment', () => {
|
|
109
|
-
const result = findLocalRenderPlaceholders(`<!-- WHATEVER
|
|
110
|
-
-->
|
|
111
|
-
<!-- proxy local render of "/some/path/to/afterBustedHtmlComment.js"
|
|
112
|
-
{
|
|
113
|
-
}
|
|
114
|
-
end proxy local render -->`);
|
|
115
|
-
expect(result.length).toBe(1);
|
|
116
|
-
expect(result[0]).toMatchSnapshot();
|
|
117
|
-
});
|
|
118
|
-
it('extacts one placeholder after one with broken missing json', () => {
|
|
119
|
-
const result = findLocalRenderPlaceholders(`<!-- proxy local render of "path/Before/missingJson"
|
|
120
|
-
-->
|
|
121
|
-
<!-- proxy local render of "/some/path/to/afterMissingJson.js"
|
|
122
|
-
{
|
|
123
|
-
}
|
|
124
|
-
end proxy local render -->`);
|
|
125
|
-
expect(result.length).toBe(1);
|
|
126
|
-
expect(result[0]).toMatchSnapshot();
|
|
127
|
-
});
|
|
128
|
-
it('does not extact broken placeholder with invalid json', () => {
|
|
129
|
-
const result = findLocalRenderPlaceholders(`<!-- proxy local render of "/some/path/to/entry.js"
|
|
130
|
-
Invalid json
|
|
131
|
-
}
|
|
132
|
-
end proxy local render -->`);
|
|
133
|
-
expect(result).toEqual([]);
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { hasComponentExtension, removeComponentExtension, } from '@hubspot/cms-js-internal';
|
|
2
|
-
import { findFullPathInModuleGraph } from './moduleGraph.js';
|
|
3
|
-
async function renderComponentWithCSS(viteServer, componentPath, Component, props, pageUrl) {
|
|
4
|
-
// Need to make sure rendering happens in Vite SSR context
|
|
5
|
-
const { renderComponentOnServer, collectCssInDev } = await viteServer.ssrLoadModule('@hubspot/cms-js-internal');
|
|
6
|
-
let componentPathWithExtension;
|
|
7
|
-
let componentPathNoExtension;
|
|
8
|
-
// Support paths with or without an extension (do we need/want both?)
|
|
9
|
-
if (hasComponentExtension(componentPath)) {
|
|
10
|
-
componentPathWithExtension = componentPath;
|
|
11
|
-
componentPathNoExtension = removeComponentExtension(componentPath);
|
|
12
|
-
}
|
|
13
|
-
else {
|
|
14
|
-
componentPathNoExtension = componentPath;
|
|
15
|
-
}
|
|
16
|
-
const { html, renderInfoContext } = await renderComponentOnServer(Component, props, pageUrl);
|
|
17
|
-
if (!componentPathWithExtension) {
|
|
18
|
-
componentPathWithExtension = findFullPathInModuleGraph(componentPathNoExtension, viteServer.moduleGraph);
|
|
19
|
-
if (!componentPathWithExtension) {
|
|
20
|
-
throw new Error(`Couldn't find full path to component: ${componentPathNoExtension}`);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
const cssNeeded = collectCssInDev(componentPathWithExtension, viteServer);
|
|
24
|
-
return { html, cssNeeded, renderInfoContext };
|
|
25
|
-
}
|
|
26
|
-
export default renderComponentWithCSS;
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { join as joinPath } from 'path';
|
|
2
|
-
import { cloneElement } from 'react';
|
|
3
|
-
import { renderToStaticMarkup } from 'react-dom/server';
|
|
4
|
-
import renderComponentWithCSS from './renderComponentWithCSS.js';
|
|
5
|
-
import { traverseFieldsJSON } from '@hubspot/cms-components/fields';
|
|
6
|
-
import { hasComponentExtension } from '@hubspot/cms-js-internal';
|
|
7
|
-
const lookForModuleInFolder = async (modulePath, viteServer) => {
|
|
8
|
-
let componentModule;
|
|
9
|
-
const insideFolderPath = joinPath(modulePath, 'index');
|
|
10
|
-
try {
|
|
11
|
-
componentModule = await viteServer.ssrLoadModule(insideFolderPath);
|
|
12
|
-
}
|
|
13
|
-
catch (e) {
|
|
14
|
-
if (!e.message.includes('failed to load module')) {
|
|
15
|
-
throw e;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return [componentModule, insideFolderPath];
|
|
19
|
-
};
|
|
20
|
-
async function renderModule(viteServer, requestedModulePath, pageUrl, fieldOverrides = {}) {
|
|
21
|
-
console.log('Rendering module', requestedModulePath);
|
|
22
|
-
let componentModule, foundPath;
|
|
23
|
-
if (!hasComponentExtension(requestedModulePath)) {
|
|
24
|
-
[componentModule, foundPath] = await lookForModuleInFolder(requestedModulePath, viteServer);
|
|
25
|
-
}
|
|
26
|
-
if (!componentModule) {
|
|
27
|
-
// Look for non-folder module
|
|
28
|
-
componentModule = await viteServer.ssrLoadModule(requestedModulePath);
|
|
29
|
-
foundPath = componentModule ? requestedModulePath : undefined;
|
|
30
|
-
}
|
|
31
|
-
if (!componentModule) {
|
|
32
|
-
throw new Error(`Couldn't find module: ${requestedModulePath}`);
|
|
33
|
-
}
|
|
34
|
-
const Component = componentModule.Component;
|
|
35
|
-
const fields = componentModule.fields;
|
|
36
|
-
const meta = componentModule.meta;
|
|
37
|
-
if (!Component) {
|
|
38
|
-
throw new Error(`Module does not have a named Component export: ${foundPath}`);
|
|
39
|
-
}
|
|
40
|
-
else if (!fields) {
|
|
41
|
-
throw new Error(`Module does not have a named fields export: ${foundPath}`);
|
|
42
|
-
}
|
|
43
|
-
else if (!meta) {
|
|
44
|
-
throw new Error(`Module does not have a named meta export: ${foundPath}`);
|
|
45
|
-
}
|
|
46
|
-
const fieldsJSON = renderFieldsToJson(fields);
|
|
47
|
-
const props = getPropsForModule(fieldsJSON, fieldOverrides);
|
|
48
|
-
return renderComponentWithCSS(viteServer, foundPath, Component, props, pageUrl);
|
|
49
|
-
}
|
|
50
|
-
// TODO, move to cms-js-internal?
|
|
51
|
-
function setInHelper(object, nestedKeysArray, value) {
|
|
52
|
-
let curr = object;
|
|
53
|
-
const finalKey = nestedKeysArray.pop();
|
|
54
|
-
for (let nestedKey of nestedKeysArray) {
|
|
55
|
-
if (Number.isInteger(nestedKey)) {
|
|
56
|
-
// TODO support occurences/groups with numeric path keys
|
|
57
|
-
throw new Error('TODO, support traversal of fields with occurances');
|
|
58
|
-
}
|
|
59
|
-
if (curr[nestedKey] === undefined) {
|
|
60
|
-
curr[nestedKey] = {};
|
|
61
|
-
}
|
|
62
|
-
curr = curr[nestedKey];
|
|
63
|
-
}
|
|
64
|
-
curr[finalKey] = value;
|
|
65
|
-
}
|
|
66
|
-
function getPropsForModule(fieldsJSON, fieldOverrides = {}) {
|
|
67
|
-
const props = {};
|
|
68
|
-
traverseFieldsJSON(fieldsJSON, fieldOverrides, (fieldPathArray, value, field) => {
|
|
69
|
-
setInHelper(props, fieldPathArray, value);
|
|
70
|
-
});
|
|
71
|
-
return props;
|
|
72
|
-
}
|
|
73
|
-
// TODO, move to cms-js-internal? (also in resolveModulesPlugin.ts)
|
|
74
|
-
function renderFieldsToJson(fields) {
|
|
75
|
-
let moduleJson;
|
|
76
|
-
const outputCallback = (callbackJson) => {
|
|
77
|
-
moduleJson = callbackJson;
|
|
78
|
-
};
|
|
79
|
-
renderToStaticMarkup(cloneElement(fields, {
|
|
80
|
-
outputCallback,
|
|
81
|
-
}));
|
|
82
|
-
return moduleJson;
|
|
83
|
-
}
|
|
84
|
-
export default renderModule;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import renderComponentWithCSS from './renderComponentWithCSS.js';
|
|
2
|
-
async function renderPartial(viteServer, partialPath, props, pageUrl) {
|
|
3
|
-
console.log('Rendering partial', partialPath);
|
|
4
|
-
const componentModule = await viteServer.ssrLoadModule(partialPath);
|
|
5
|
-
const Component = componentModule.default;
|
|
6
|
-
if (!componentModule) {
|
|
7
|
-
throw new Error(`Couldn't find partial: ${partialPath}`);
|
|
8
|
-
}
|
|
9
|
-
return renderComponentWithCSS(viteServer, partialPath, Component, props, pageUrl);
|
|
10
|
-
}
|
|
11
|
-
export default renderPartial;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { promises as fsPromises } from 'fs';
|
|
2
|
-
import { rewriteAbsoluteJsAndCssPaths } from './htmlHelpers.js';
|
|
3
|
-
import { sortIslandRootsByPriority, createIslandsGlobalJsForClientInDev, } from '@hubspot/cms-js-internal';
|
|
4
|
-
const { readFile } = fsPromises;
|
|
5
|
-
const ISLAND_RUNTIME_AND_SETUP_SCRIPT_BLOCK = `<script type="module">
|
|
6
|
-
import { setupIslandHydration } from '@hubspot/cms-components/island-runtime';
|
|
7
|
-
setupIslandHydration();
|
|
8
|
-
</script>
|
|
9
|
-
`;
|
|
10
|
-
const createdPlainHtmlTemplate = async (baseTemplatePath, title) => {
|
|
11
|
-
const htmlTemplateBase = await readFile(baseTemplatePath, 'utf-8');
|
|
12
|
-
return htmlTemplateBase.replace('<!--title-->', title);
|
|
13
|
-
};
|
|
14
|
-
const createIslandHtmlTemplate = async (baseTemplatePath, title) => {
|
|
15
|
-
let htmlTemplate = await createdPlainHtmlTemplate(baseTemplatePath, title);
|
|
16
|
-
htmlTemplate = htmlTemplate.replace('<!--bodyend-->', `
|
|
17
|
-
${ISLAND_RUNTIME_AND_SETUP_SCRIPT_BLOCK}
|
|
18
|
-
<!-- ISLAND_RUNTIME_AND_SETUP_SCRIPT_BLOCK -->
|
|
19
|
-
<!--islandsglobal-->
|
|
20
|
-
`);
|
|
21
|
-
return htmlTemplate;
|
|
22
|
-
};
|
|
23
|
-
export const createHtmlShellDependingOnRenderContext = async (baseTemplatePath, url, title, renderInfoContext, viteServer) => {
|
|
24
|
-
let htmlTemplate;
|
|
25
|
-
if (renderInfoContext.islands.length) {
|
|
26
|
-
htmlTemplate = await createIslandHtmlTemplate(baseTemplatePath, title);
|
|
27
|
-
const sortedIslandRoots = sortIslandRootsByPriority(renderInfoContext.islands);
|
|
28
|
-
htmlTemplate = htmlTemplate.replace('<!--islandsglobal-->', `<script>
|
|
29
|
-
${createIslandsGlobalJsForClientInDev(sortedIslandRoots, viteServer, 4)}
|
|
30
|
-
</script>`);
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
htmlTemplate = await createdPlainHtmlTemplate(baseTemplatePath, title);
|
|
34
|
-
}
|
|
35
|
-
return await viteServer.transformIndexHtml(url, htmlTemplate);
|
|
36
|
-
};
|
|
37
|
-
export const getIslandRuntimeAndGlobalToInject = async (sortedIslandRoots, viteServer, devServerHost, devServerPort) => {
|
|
38
|
-
const serverDomainAndPort = `${devServerHost}:${devServerPort}`;
|
|
39
|
-
// Loading through vite so all of nested `@hubspot/cms-*` imports work
|
|
40
|
-
const { createIslandsGlobalJsForClientInDev } = await viteServer.ssrLoadModule('@hubspot/cms-js-internal');
|
|
41
|
-
let islandRuntimeScript = await viteServer.transformIndexHtml('/', ISLAND_RUNTIME_AND_SETUP_SCRIPT_BLOCK);
|
|
42
|
-
// Need to write JS module URLs to be absolute to localhost for when this island code
|
|
43
|
-
// is injected into a HTML document with a differnt <base> url set
|
|
44
|
-
// Note, :sadtrombone: that https://github.com/vitejs/vite/issues/2196 was closed
|
|
45
|
-
// and that https://github.com/vitejs/vite/pull/5104 doesn't help for JS assets
|
|
46
|
-
islandRuntimeScript = rewriteAbsoluteJsAndCssPaths(islandRuntimeScript, serverDomainAndPort, 'http://');
|
|
47
|
-
islandRuntimeScript = islandRuntimeScript.replace('import RefreshRuntime from "/@react-refresh"', `import RefreshRuntime from "http://${serverDomainAndPort}/@react-refresh"`);
|
|
48
|
-
return `${islandRuntimeScript}
|
|
49
|
-
<script>${createIslandsGlobalJsForClientInDev(sortedIslandRoots, viteServer, 4)}</script>`;
|
|
50
|
-
};
|
|
@@ -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
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import https from 'https';
|
|
3
|
-
export default async function getContentMetadata(url, redirects = 0, originalUrl) {
|
|
4
|
-
if (redirects > 5) {
|
|
5
|
-
throw new Error(`Hit too many redirects to HEAD requests for ${originalUrl}`);
|
|
6
|
-
}
|
|
7
|
-
return new Promise((resolve, reject) => {
|
|
8
|
-
const protocolAPI = url.startsWith('https://') ? https : http;
|
|
9
|
-
protocolAPI
|
|
10
|
-
.request(url, { method: 'HEAD' }, (res) => {
|
|
11
|
-
// Use a lib to better follow various redirects?
|
|
12
|
-
if (res.statusCode >= 300 &&
|
|
13
|
-
res.statusCode < 400 &&
|
|
14
|
-
res.headers.location) {
|
|
15
|
-
console.log(`Redirecting to ${res.headers.location} (from ${url})`);
|
|
16
|
-
return resolve(getContentMetadata(res.headers.location, redirects + 1, originalUrl ?? url));
|
|
17
|
-
}
|
|
18
|
-
const portalIdHeader = res.headers['x-hs-hub-id'];
|
|
19
|
-
const contentId = res.headers['x-hs-content-id'];
|
|
20
|
-
if (Array.isArray(portalIdHeader) || Array.isArray(contentId)) {
|
|
21
|
-
return reject(new Error(`There were multiple hub ID or content ID headers in the HEAD request to ${url}`));
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
const portalId = parseInt(portalIdHeader, 10);
|
|
25
|
-
if (!portalId || !contentId) {
|
|
26
|
-
return reject(new Error(`Missing hub ID or content ID headers on HEAD request to ${url}`));
|
|
27
|
-
}
|
|
28
|
-
else if (isNaN(portalId)) {
|
|
29
|
-
return reject(new Error(`Hud ID from the HEAD request is not a number: '${portalId}' (${url})`));
|
|
30
|
-
}
|
|
31
|
-
return resolve({ portalId, contentId });
|
|
32
|
-
}
|
|
33
|
-
})
|
|
34
|
-
.on('error', (err) => {
|
|
35
|
-
console.error(err);
|
|
36
|
-
reject(err);
|
|
37
|
-
})
|
|
38
|
-
.end();
|
|
39
|
-
});
|
|
40
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { get as hsGet } from '@hubspot/cli-lib/http.js';
|
|
2
|
-
export default async (accountId, portalId, contentId) => {
|
|
3
|
-
try {
|
|
4
|
-
// Works with oauth if any scopes present [blog-access, landingpages-access, service-knowledge-access, sitepages-access, pages-access]
|
|
5
|
-
const content = await hsGet(accountId, {
|
|
6
|
-
uri: `/content/api/v4/contents/${contentId}`,
|
|
7
|
-
query: { portalId },
|
|
8
|
-
json: true,
|
|
9
|
-
});
|
|
10
|
-
return {
|
|
11
|
-
previewDomain: content.previewDomain ?? content.resolvedDomain,
|
|
12
|
-
previewKey: content.previewKey,
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
catch (error) {
|
|
16
|
-
console.log(`error`, error);
|
|
17
|
-
if (error.options) {
|
|
18
|
-
console.log(`error.options`, error.options);
|
|
19
|
-
}
|
|
20
|
-
if (error.response && error.response.headers) {
|
|
21
|
-
console.log(`error.response.headers`, error.response.headers);
|
|
22
|
-
}
|
|
23
|
-
throw error;
|
|
24
|
-
}
|
|
25
|
-
};
|