@flight-framework/cli 0.4.6 → 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 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 = (targetUrl, originalRes, attemptExtensions = false) => {
951
+ const proxyRequest = async (targetUrl, originalRes, retry = true) => {
950
952
  const proxyReq = http.request(
951
953
  {
952
954
  hostname: "localhost",
@@ -955,45 +957,46 @@ async function devCommand(options) {
955
957
  method: req.method,
956
958
  headers: req.headers
957
959
  },
958
- (proxyRes) => {
959
- if (proxyRes.statusCode !== 404 || !attemptExtensions) {
960
+ async (proxyRes) => {
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
966
  proxyRes.resume();
965
- const commonExtensions = [".mjs", ".js", ".ts", ".tsx", ".jsx", "/index.js"];
966
- const tryNextExtension = (index) => {
967
- if (index >= commonExtensions.length) {
968
- res.writeHead(404, proxyRes.headers);
969
- res.end("Not Found");
970
- return;
967
+ try {
968
+ let resolvedUrl = null;
969
+ if (targetUrl.startsWith("/node_modules/")) {
970
+ const importPath = targetUrl.replace("/node_modules/", "");
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
+ }
971
979
  }
972
- const ext = commonExtensions[index];
973
- const fallbackUrl = targetUrl + ext;
974
- const fallbackReq = http.request(
975
- {
976
- hostname: "localhost",
977
- port: transformPort,
978
- path: fallbackUrl,
979
- method: "GET",
980
- // Always GET for fallbacks
981
- headers: req.headers
982
- },
983
- (fallbackRes) => {
984
- if (fallbackRes.statusCode === 200) {
985
- res.writeHead(200, fallbackRes.headers);
986
- fallbackRes.pipe(res);
987
- } else {
988
- fallbackRes.resume();
989
- tryNextExtension(index + 1);
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;
990
988
  }
991
989
  }
992
- );
993
- fallbackReq.on("error", () => tryNextExtension(index + 1));
994
- fallbackReq.end();
995
- };
996
- tryNextExtension(0);
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");
999
+ }
997
1000
  }
998
1001
  );
999
1002
  proxyReq.on("error", (err) => {
@@ -1009,8 +1012,7 @@ async function devCommand(options) {
1009
1012
  proxyReq.end();
1010
1013
  }
1011
1014
  };
1012
- const shouldTryExtensions = !url.includes(".") && !url.endsWith("/");
1013
- proxyRequest(url, res, shouldTryExtensions);
1015
+ proxyRequest(url, res);
1014
1016
  });
1015
1017
  const cliPort = port;
1016
1018
  await new Promise((resolve10) => server.listen(cliPort, () => resolve10()));