@flight-framework/cli 0.4.7 → 0.4.8
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/bin.js +36 -26
- package/dist/bin.js.map +1 -1
- package/dist/index.js +36 -26
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -510,7 +510,7 @@ Happy flying!
|
|
|
510
510
|
}
|
|
511
511
|
|
|
512
512
|
// src/commands/dev.ts
|
|
513
|
-
import { resolve as resolve3, join as join3 } from "path";
|
|
513
|
+
import { resolve as resolve3, join as join3, relative } from "path";
|
|
514
514
|
import { readFileSync as readFileSync3, existsSync as existsSync3 } from "fs";
|
|
515
515
|
import { consola as consola2 } from "consola";
|
|
516
516
|
import { loadConfig } from "@flight-framework/core/config";
|
|
@@ -918,8 +918,10 @@ async function devCommand(options) {
|
|
|
918
918
|
const http = await import("http");
|
|
919
919
|
const fs = await import("fs/promises");
|
|
920
920
|
const path = await import("path");
|
|
921
|
+
const { createRequire } = await import("module");
|
|
921
922
|
const indexHtmlPath = path.join(root, "index.html");
|
|
922
923
|
const publicIndexPath = path.join(root, "public", "index.html");
|
|
924
|
+
const require2 = createRequire(root + "/");
|
|
923
925
|
const server = http.createServer(async (req, res) => {
|
|
924
926
|
const url = req.url || "/";
|
|
925
927
|
const isSpaRoute = url === "/" || url === "/index.html" || !url.includes(".") && !url.startsWith("/@") && !url.startsWith("/__") && !url.startsWith("/src/") && !url.startsWith("/node_modules/");
|
|
@@ -946,7 +948,7 @@ async function devCommand(options) {
|
|
|
946
948
|
}
|
|
947
949
|
return;
|
|
948
950
|
}
|
|
949
|
-
const proxyRequest = async (targetUrl, originalRes) => {
|
|
951
|
+
const proxyRequest = async (targetUrl, originalRes, retry = true) => {
|
|
950
952
|
const proxyReq = http.request(
|
|
951
953
|
{
|
|
952
954
|
hostname: "localhost",
|
|
@@ -956,37 +958,45 @@ async function devCommand(options) {
|
|
|
956
958
|
headers: req.headers
|
|
957
959
|
},
|
|
958
960
|
async (proxyRes) => {
|
|
959
|
-
if (proxyRes.statusCode !== 404) {
|
|
961
|
+
if (proxyRes.statusCode !== 404 || !retry) {
|
|
960
962
|
res.writeHead(proxyRes.statusCode || 200, proxyRes.headers);
|
|
961
963
|
proxyRes.pipe(res);
|
|
962
964
|
return;
|
|
963
965
|
}
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
const require2 = createRequire(root + "/");
|
|
966
|
+
proxyRes.resume();
|
|
967
|
+
try {
|
|
968
|
+
let resolvedUrl = null;
|
|
969
|
+
if (targetUrl.startsWith("/node_modules/")) {
|
|
969
970
|
const importPath = targetUrl.replace("/node_modules/", "");
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
"Cache-Control": "no-cache"
|
|
979
|
-
});
|
|
980
|
-
res.end(content);
|
|
981
|
-
return;
|
|
982
|
-
} catch (resolveError) {
|
|
983
|
-
res.writeHead(404, proxyRes.headers);
|
|
984
|
-
res.end("Not Found");
|
|
985
|
-
return;
|
|
971
|
+
try {
|
|
972
|
+
const resolvedPath = require2.resolve(importPath);
|
|
973
|
+
if (resolvedPath.startsWith(root)) {
|
|
974
|
+
let rel = relative(root, resolvedPath);
|
|
975
|
+
resolvedUrl = "/" + rel.replace(/\\/g, "/");
|
|
976
|
+
}
|
|
977
|
+
} catch (e) {
|
|
978
|
+
}
|
|
986
979
|
}
|
|
980
|
+
if (!resolvedUrl && !targetUrl.includes(".")) {
|
|
981
|
+
const commonExtensions = [".mjs", ".js", ".ts", ".tsx", ".jsx", "/index.js"];
|
|
982
|
+
const relPath = targetUrl.startsWith("/") ? targetUrl.slice(1) : targetUrl;
|
|
983
|
+
const absBase = join3(root, relPath);
|
|
984
|
+
for (const ext of commonExtensions) {
|
|
985
|
+
if (existsSync3(absBase + ext)) {
|
|
986
|
+
resolvedUrl = targetUrl + ext;
|
|
987
|
+
break;
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
if (resolvedUrl && resolvedUrl !== targetUrl) {
|
|
992
|
+
return proxyRequest(resolvedUrl, originalRes, false);
|
|
993
|
+
}
|
|
994
|
+
res.writeHead(404, proxyRes.headers);
|
|
995
|
+
res.end("Not Found");
|
|
996
|
+
} catch (resolveError) {
|
|
997
|
+
res.writeHead(404, proxyRes.headers);
|
|
998
|
+
res.end("Not Found");
|
|
987
999
|
}
|
|
988
|
-
res.writeHead(proxyRes.statusCode || 404, proxyRes.headers);
|
|
989
|
-
proxyRes.pipe(res);
|
|
990
1000
|
}
|
|
991
1001
|
);
|
|
992
1002
|
proxyReq.on("error", (err) => {
|