Skip to content

Commit

Permalink
fix: strip double extensions (#54)
Browse files Browse the repository at this point in the history
Previously, extensions such as `.test.tsx` would result in generated
paths that included the `.test` extension. Now, multiple levels of extensions are stripped and only one route is generated for any path overlaps.
  • Loading branch information
tatethurston authored Aug 8, 2022
1 parent 9f9c0b6 commit f724a77
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Bug Fix: quote query segments in generated types. See [#49](https://github.com/tatethurston/nextjs-routes/issues/49) for more context.
- Bug Fix: don't generate routes for non navigable routes (`_error`, `_app`, `_document`).
- Bug Fix: don't generate routes for test files that are co-located in pages directory. See [#50](https://github.com/tatethurston/nextjs-routes/pull/50) for more context.

## 0.0.18

Expand Down
9 changes: 7 additions & 2 deletions e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ describe("nextRoutes", () => {
const { pathname } = nextRoutes(pages, "src\\pages")[0];
expect(pathname).toEqual("/[foo]/bar");
});

it("colocated test files", () => {
const pages = ["pages/index.tsx", "pages/index.test.tsx"];
expect(nextRoutes(pages, "pages")).toMatchInlineSnapshot(`
Array [
Object {
"pathname": "/",
"query": Object {},
},
]
`);
});
});

describe("route generation", () => {
Expand Down
10 changes: 6 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeFileSync } from "fs";
import { join, parse } from "path";
import { join } from "path";
import { findFiles, getPagesDirectory } from "./utils.js";

const NEXTJS_NON_ROUTABLE = ["/_app", "/_document", "/_error", "/middleware"];
Expand All @@ -20,11 +20,13 @@ export function nextRoutes(files: string[], pagesDirectory: string): Route[] {
const pathnames = files
// remove page directory path
.map((file) => file.replace(pagesDirectory, ""))
// remove file extension
.map((file) => file.replace(parse(file).ext, ""))
// remove file extensions (.tsx, .test.tsx)
.map((file) => file.replace(/(\.\w+)+$/, ""))
// remove duplicates from file extension removal (eg foo.ts and foo.test.ts)
.filter((file, idx, array) => array.indexOf(file) === idx)
// normalize paths from windows users
.map(convertWindowsPathToUnix)
// remove trailign slash
// remove index if present (/foos/index.ts is the same as /foos.ts)
.map((file) => file.replace(/index$/, ""))
// remove trailing slash if present
.map((file) =>
Expand Down

0 comments on commit f724a77

Please sign in to comment.