-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
135 lines (122 loc) · 4.04 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const express = require('express');
const jsdom = require('jsdom');
const fetch = (...args) =>
import('node-fetch').then(({ default: fetch }) => fetch(...args));
const vite = require('vite');
const { createServer } = vite;
const { JSDOM } = jsdom;
async function init(url, host, clientlibs, entry, headers, wcmmode) {
// fetch with basic auth admin:admin
let page;
const fetchUrl = wcmmode
? `${host}${url}?wcmmode=${wcmmode}`
: `${host}${url}`;
try {
page = await (
await fetch(fetchUrl, {
headers,
referrer: `${host}`,
})
).text();
} catch (e) {
console.error(e);
return;
}
const dom = new JSDOM(page);
[...dom.window.document.querySelectorAll('script')].forEach((script) => {
if (script.src.startsWith('/etc.clientlibs')) {
for (const clientlib of clientlibs) {
if (script.src.includes(clientlib)) {
script.remove();
}
}
script.src = `${host}${script.src}`;
}
});
[...dom.window.document.querySelectorAll('link')].forEach((link) => {
if (link.href.startsWith('/etc.clientlibs')) {
for (const clientlib of clientlibs) {
if (link.href.includes(clientlib)) {
link.remove();
}
}
link.href = `${host}${link.href}`;
}
});
// const loadScript = dom.window.document.createElement('script');
// loadScript.setAttribute('type', 'text/javascript');
// loadScript.setAttribute('entry', entry);
// loadScript.setAttribute('id', 'loadScript');
// loadScript.innerHTML = `if(!window.CQ?.Siteadmin) {
// const script = document.createElement('script');
// script.setAttribute('type', 'module');
// script.setAttribute('src', document.querySelector('#loadScript').getAttribute('entry'));
// const div = document.createElement('div');
// div.setAttribute('id', 'root');
// div.setAttribute('class', 'app');
// document.body.appendChild(div);
// document.body.appendChild(script);
// }`;
// dom.window.document.body.appendChild(loadScript);
// if (!globalThis['CQ']?.Siteadmin) {
// const script = dom.window.document.createElement('script');
// script.setAttribute('type', 'module');
// script.setAttribute('src', entry);
// const div = dom.window.document.createElement('div');
// div.setAttribute('id', 'root');
// div.setAttribute('class', 'app');
// dom.window.document.body.appendChild(div);
// dom.window.document.body.appendChild(script);
// }
const script = dom.window.document.createElement('script');
script.setAttribute('type', 'module');
script.setAttribute('src', entry);
const div = dom.window.document.createElement('div');
div.setAttribute('id', 'root');
div.setAttribute('class', 'app');
dom.window.document.body.appendChild(div);
dom.window.document.body.appendChild(script);
return dom.serialize();
}
async function createMyServer(
host,
clientlibs,
port,
entry,
headers,
wcmmode = null
) {
const app = express();
// Create Vite server in middleware mode. This disables Vite's own HTML
// serving logic and let the parent server take control.
//
// In middleware mode, if you want to use Vite's own HTML serving logic
// use `'html'` as the `middlewareMode` (ref https://vitejs.dev/config/#server-middlewaremode)
const vite = await createServer({
server: { middlewareMode: 'ssr' },
});
// use vite's connect instance as middleware
app.use(vite.middlewares);
app.use(async function (req, res, next) {
const url = req.originalUrl;
const reg = new RegExp(/.*(\/|\.html|\.html\/.*|\.html\?.*)$/i);
if (req.url === '/' || reg.test(req.url)) {
const page = await init(
req.url,
host,
clientlibs,
entry,
headers,
wcmmode
);
const template = await vite.transformIndexHtml(url, page);
res.send(template.replace('<!-- APP -->', page));
}
next();
});
app.listen(port, () => {
console.log(`Server listening http://localhost:${port}`);
});
}
// const createMyServer = () => {};
export default createMyServer;