diff --git a/lib/framework.js b/lib/framework.js index 5fb14121..f4c703bb 100644 --- a/lib/framework.js +++ b/lib/framework.js @@ -2,13 +2,12 @@ import {parse as parseUrl} from "node:url"; import http from "node:http"; import https from "node:https"; import httpProxy from "http-proxy"; -import {statSync, readFileSync} from "node:fs"; +import {statSync, readFileSync, existsSync} from "node:fs"; import path from "node:path"; import {fileURLToPath} from "node:url"; import yaml from "js-yaml"; import {Router} from "express"; import {ErrorMessage} from "./errors.js"; - const __dirname = path.dirname(fileURLToPath(import.meta.url)); export default class Framework { @@ -547,12 +546,42 @@ export default class Framework { req.url = this.rewriteUrl(req.url); next(); } - + /** + * For Maven-managed UI5 libraries (fiori pipeline), dependencies are extracted to the + * target/dependency/META-INF/resources directory inside the project. This middleware + * returns the file contents if found inside the dependency directory. + * + * @param {http.ClientRequest} req Client request object + * @param {http.ServerResponse} res Server response object + * @param {Function} next Next middleware + * @returns {void} + */ + middlewareMavenDependencies(req, res, next) { + const sMvnDependencyDir = path.resolve("./target/dependency/META-INF/resources"); + if (req.method !== "GET" || !existsSync(sMvnDependencyDir)) { + next(); + return; + } + const oReqUrl = parseUrl(req.url); + const sPath = oReqUrl.pathname; + if (sPath.startsWith("/base/resources")) { + const sSearchPath = path.resolve(sMvnDependencyDir + sPath.substring("/base/resources".length)); + if (existsSync(sSearchPath)) { + const oBody = readFileSync(sSearchPath); + res.write(oBody); + res.statusCode = 200; + res.end(); + return; + } + } + next(); + } async setupMiddleware() { const config = this.config; if (config.ui5.type === "library") { config.ui5._beforeMiddleware = new Router(); + config.ui5._beforeMiddleware.use(this.middlewareMavenDependencies.bind(this)); config.ui5._beforeMiddleware.use(this.beforeMiddlewareRewriteUrl.bind(this)); config.beforeMiddleware.push("ui5--beforeMiddleware"); }