cds-plugin-ui5 0.6.9 → 0.6.10
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/CHANGELOG.md +11 -0
- package/cds-plugin.js +134 -128
- package/lib/rewriteHTML.js +3 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [0.6.10](https://github.com/ui5-community/ui5-ecosystem-showcase/compare/cds-plugin-ui5@0.6.9...cds-plugin-ui5@0.6.10) (2023-09-15)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* no cache for rewrite, cds server lookup ([#843](https://github.com/ui5-community/ui5-ecosystem-showcase/issues/843)) ([fc7b9b5](https://github.com/ui5-community/ui5-ecosystem-showcase/commit/fc7b9b5e8c362a499e2fbb70ece764e9f17b0f6e))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## [0.6.9](https://github.com/ui5-community/ui5-ecosystem-showcase/compare/cds-plugin-ui5@0.6.8...cds-plugin-ui5@0.6.9) (2023-09-15)
|
|
7
18
|
|
|
8
19
|
|
package/cds-plugin.js
CHANGED
|
@@ -11,142 +11,148 @@ const rewriteHTML = require("./lib/rewriteHTML");
|
|
|
11
11
|
// to disable the ui5-middleware-cap if used in apps
|
|
12
12
|
process.env["cds-plugin-ui5"] = true;
|
|
13
13
|
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
await
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
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
|
-
margin: 0.25rem;
|
|
102
|
-
color: #1873B4;
|
|
103
|
-
}
|
|
104
|
-
@media (prefers-color-scheme: dark) {
|
|
14
|
+
// identify whether the execution should be skipped
|
|
15
|
+
let skip = false;
|
|
16
|
+
if (process.env["ui5-middleware-cap"]) {
|
|
17
|
+
log.info("Skip execution of plugin because is has been started via ui5-middleware-cap!");
|
|
18
|
+
skip = true;
|
|
19
|
+
} else if (process.env["dev-approuter"]) {
|
|
20
|
+
log.info("Skip execution of plugin because is has been started via dev-approuter!");
|
|
21
|
+
skip = true;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// only hook into lifecycle if the plugin should not be skipped
|
|
25
|
+
if (!skip) {
|
|
26
|
+
// promise to await the bootstrap and lock the
|
|
27
|
+
// served event to delay the startup a bit
|
|
28
|
+
let bootstrapped;
|
|
29
|
+
const bootstrapCompleted = new Promise((r) => {
|
|
30
|
+
bootstrapped = r;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// hook into the "served" event to delay the startup of the
|
|
34
|
+
// CDS server until the bootstrap is completed and the UI5
|
|
35
|
+
// middlewares for the UI5 applications are properly available
|
|
36
|
+
cds.on("served", async function served(/* cdsServices */) {
|
|
37
|
+
log.debug("served");
|
|
38
|
+
await bootstrapCompleted;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// hook into the "bootstrap" event to startup the UI5 middlewares
|
|
42
|
+
// for the available UI5 applications in the CDS server and its deps
|
|
43
|
+
cds.on("bootstrap", async function bootstrap(app) {
|
|
44
|
+
log.debug("bootstrap");
|
|
45
|
+
|
|
46
|
+
const cwd = process.cwd();
|
|
47
|
+
const ui5Modules = await findUI5Modules({ cwd });
|
|
48
|
+
const { localApps, config } = ui5Modules;
|
|
49
|
+
|
|
50
|
+
const links = [];
|
|
51
|
+
|
|
52
|
+
// register the UI5 modules via their own router/middlewares
|
|
53
|
+
for await (const ui5Module of ui5Modules) {
|
|
54
|
+
const { moduleId, mountPath, modulePath } = ui5Module;
|
|
55
|
+
|
|
56
|
+
// mounting the Router for the UI5 application to the CDS server
|
|
57
|
+
log.info(`Mounting ${mountPath} to UI5 app ${modulePath} (id=${moduleId})${config[moduleId] ? ` using config=${JSON.stringify(config[moduleId])}` : ""}`);
|
|
58
|
+
|
|
59
|
+
// create a patched router
|
|
60
|
+
const router = await createPatchedRouter();
|
|
61
|
+
|
|
62
|
+
// apply the UI5 middlewares to the router and
|
|
63
|
+
// retrieve the available HTML pages
|
|
64
|
+
const appInfo = await applyUI5Middleware(router, {
|
|
65
|
+
cwd,
|
|
66
|
+
basePath: modulePath,
|
|
67
|
+
...(config[moduleId] || {}),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// register the router to the specified mount path
|
|
71
|
+
app.use(mountPath, router);
|
|
72
|
+
|
|
73
|
+
// append the HTML pages to the links
|
|
74
|
+
appInfo.pages.forEach((page) => {
|
|
75
|
+
const prefix = mountPath !== "/" ? mountPath : "";
|
|
76
|
+
links.push(`${prefix}${page}`);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// identify whether the welcome page should be rewritten
|
|
81
|
+
let rewrite = links.length > 0;
|
|
82
|
+
|
|
83
|
+
// rewrite the welcome page
|
|
84
|
+
if (rewrite) {
|
|
85
|
+
// register the custom middleware (similar like in @sap/cds/server.js)
|
|
86
|
+
app.get("/", function appendLinksToIndex(req, res, next) {
|
|
87
|
+
req._cds_plugin_ui5 = true; // marker for patched router to ignore
|
|
88
|
+
rewriteHTML(
|
|
89
|
+
req,
|
|
90
|
+
res,
|
|
91
|
+
() => true,
|
|
92
|
+
(doc) => {
|
|
93
|
+
// the first <ul> element contains the links to the
|
|
94
|
+
// application pages which is fully under control of
|
|
95
|
+
// our plugin now and we keep all links to static
|
|
96
|
+
// pages to ensure coop with classic apps
|
|
97
|
+
const head = doc.getElementsByTagName("head")?.[0];
|
|
98
|
+
head?.insertAdjacentHTML(
|
|
99
|
+
"beforeend",
|
|
100
|
+
`<style>
|
|
105
101
|
a.ui5:after {
|
|
106
|
-
|
|
102
|
+
content: "UI5";
|
|
103
|
+
font-weight: bold;
|
|
104
|
+
font-size: 0.5rem;
|
|
105
|
+
vertical-align: super;
|
|
106
|
+
margin: 0.25rem;
|
|
107
|
+
color: #1873B4;
|
|
107
108
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const ul = doc.getElementsByTagName("ul")?.[0];
|
|
112
|
-
if (ul) {
|
|
113
|
-
const newLis = [];
|
|
114
|
-
const lis = ul.getElementsByTagName("li");
|
|
115
|
-
lis?.forEach((li) => {
|
|
116
|
-
const appDir = li.firstChild?.text?.split("/")?.[1];
|
|
117
|
-
if (localApps.has(appDir)) {
|
|
118
|
-
newLis.push(li.toString());
|
|
109
|
+
@media (prefers-color-scheme: dark) {
|
|
110
|
+
a.ui5:after {
|
|
111
|
+
color: #FF5A37;
|
|
119
112
|
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
113
|
+
}
|
|
114
|
+
</style>`
|
|
115
|
+
);
|
|
116
|
+
const ul = doc.getElementsByTagName("ul")?.[0];
|
|
117
|
+
if (ul) {
|
|
118
|
+
const newLis = [];
|
|
119
|
+
const lis = ul.getElementsByTagName("li");
|
|
120
|
+
lis?.forEach((li) => {
|
|
121
|
+
const appDir = li.firstChild?.text?.split("/")?.[1];
|
|
122
|
+
if (localApps.has(appDir)) {
|
|
123
|
+
newLis.push(li.toString());
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
newLis.push(...links.sort().map((link) => `<li><a class="ui5" href="${link}">${link}</a></li>`));
|
|
127
|
+
ul.innerHTML = newLis.join("\n");
|
|
128
|
+
} else {
|
|
129
|
+
log.warn(`Failed to inject application links into CDS index page!`);
|
|
130
|
+
}
|
|
125
131
|
}
|
|
132
|
+
);
|
|
133
|
+
next();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// move our middleware before the CDS index serve middleware to
|
|
137
|
+
// allow that we can intercept the response and modify it to
|
|
138
|
+
// inject our application pages an remove the exitsing ones
|
|
139
|
+
const middlewareStack = app?._router?.stack;
|
|
140
|
+
if (Array.isArray(middlewareStack)) {
|
|
141
|
+
const cmw = middlewareStack.pop();
|
|
142
|
+
const idxOfServeStatic = middlewareStack.findIndex((layer) => layer.name === "serveStatic");
|
|
143
|
+
if (idxOfServeStatic !== -1) {
|
|
144
|
+
middlewareStack.splice(idxOfServeStatic, 0, cmw);
|
|
145
|
+
} else {
|
|
146
|
+
log.error(`Failed to determine welcome page middleware! The links of the application pages may not work properly!`);
|
|
126
147
|
}
|
|
127
|
-
);
|
|
128
|
-
next();
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
// move our middleware before the CDS index serve middleware to
|
|
132
|
-
// allow that we can intercept the response and modify it to
|
|
133
|
-
// inject our application pages an remove the exitsing ones
|
|
134
|
-
const middlewareStack = app?._router?.stack;
|
|
135
|
-
if (Array.isArray(middlewareStack)) {
|
|
136
|
-
const cmw = middlewareStack.pop();
|
|
137
|
-
const idxOfServeStatic = middlewareStack.findIndex((layer) => layer.name === "serveStatic");
|
|
138
|
-
if (idxOfServeStatic !== -1) {
|
|
139
|
-
middlewareStack.splice(idxOfServeStatic, 0, cmw);
|
|
140
148
|
} else {
|
|
141
|
-
log.error(`Failed to
|
|
149
|
+
log.error(`Failed to inject application pages to welcome page! The links of the application pages may not work properly!`);
|
|
142
150
|
}
|
|
143
|
-
} else {
|
|
144
|
-
log.error(`Failed to inject application pages to welcome page! The links of the application pages may not work properly!`);
|
|
145
151
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
152
|
+
// bootstrap completed, unlock the "served" event
|
|
153
|
+
bootstrapped();
|
|
154
|
+
});
|
|
155
|
+
}
|
|
150
156
|
|
|
151
157
|
// return callback for plugin activation
|
|
152
158
|
module.exports = {
|
package/lib/rewriteHTML.js
CHANGED
|
@@ -45,10 +45,12 @@ module.exports = async function rewriteHTML(req, res, rewriteCondition, rewriteC
|
|
|
45
45
|
// buffer to store the received content in
|
|
46
46
|
const contentBuffer = [];
|
|
47
47
|
|
|
48
|
-
// remove the content-length header
|
|
48
|
+
// remove the content-length and etag header / disable caching
|
|
49
49
|
res.writeHead = function () {
|
|
50
50
|
if (res.statusCode !== 200 || !rewriteCondition(res)) return writeHead.apply(this, arguments);
|
|
51
51
|
res.removeHeader("content-length");
|
|
52
|
+
res.removeHeader("etag");
|
|
53
|
+
res.setHeader("cache-control", "no-cache");
|
|
52
54
|
return writeHead.apply(this, arguments);
|
|
53
55
|
};
|
|
54
56
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cds-plugin-ui5",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.10",
|
|
4
4
|
"description": "A CDS server plugin to inject the middlewares of all related UI5 tooling based projects.",
|
|
5
5
|
"author": "Peter Muessig",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -24,5 +24,5 @@
|
|
|
24
24
|
"@sap/cds": ">=6.8.2",
|
|
25
25
|
"express": ">=4.18.2"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "4e614b76248207ceeaf36bcedd22abfee7f4c226"
|
|
28
28
|
}
|