Skip to content

Commit

Permalink
Using endsWith instead of match
Browse files Browse the repository at this point in the history
  • Loading branch information
Haider Ali committed Jan 17, 2024
1 parent 4abfecb commit 2fff4e0
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ async function resolveConfig(): Promise<ViteConfig> {
),
);
}
} catch (e) {
1;
}
} catch (e) {}

try {
const config = fs.readFileSync(getViteConfigPath(), "utf8");
Expand Down Expand Up @@ -174,7 +172,7 @@ async function injectStaticMiddleware(
const config = await getViteConfig();

app.use(config.base, (req, res, next) =>
req.path.match(/(\.html|\/)$/) ? next() : middleware(req, res, next),
req.path.endsWith(".html") ? next() : middleware(req, res, next),
);

const stubMiddlewareLayer = app._router.stack.find(
Expand All @@ -197,10 +195,13 @@ function isIgnoredPath(path: string, req: express.Request) {
: Config.ignorePaths(path, req);
}

function findClosestIndexToRoot(
reqPath: string,
root: string,
): string | undefined {
function findFilePath(reqPath: string, root: string): string | undefined {
if (reqPath.endsWith(".html")) {
const pathToTest = path.join(root, reqPath);
if (fs.existsSync(pathToTest)) return pathToTest;
}

// find closest index to root
const basePath = reqPath.slice(0, reqPath.lastIndexOf("/"));
const dirs = basePath.split("/");

Expand All @@ -213,15 +214,6 @@ function findClosestIndexToRoot(
return undefined;
}

function findFilePath(reqPath: string, root: string): string | undefined {
if (reqPath.match(/\.html$/)) {
const pathToTest = path.join(root, reqPath);
if (fs.existsSync(pathToTest)) return pathToTest;
}

return findClosestIndexToRoot(reqPath, root);
}

async function injectViteIndexMiddleware(
app: core.Express,
server: ViteDevServer,
Expand Down

0 comments on commit 2fff4e0

Please sign in to comment.