Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(remix-dev): flat routes #4880

Merged
merged 62 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
45e1669
feat: add flat routes convention
kiliman Nov 30, 2022
2a0c205
chore: include future v2_routeConvention flag
kiliman Dec 13, 2022
7e08ddb
fix: update readConfig test to include new v2_routeConvention flag
kiliman Dec 13, 2022
30319da
fix: replace default route dir
kiliman Dec 13, 2022
c330861
chore: slim
mcansh Dec 14, 2022
88c0cbe
chore: remove unused eslint-disable coment
mcansh Dec 14, 2022
50a4393
chore(flat-routes): add support for optional and escape route segments
mcansh Dec 20, 2022
c525b4c
initial import
mcansh Dec 21, 2022
ba964ae
chore: index routes use underscores
mcansh Dec 21, 2022
459030c
feat: add support for optional segments
mcansh Dec 21, 2022
1db590d
chore: flat routes
mcansh Dec 21, 2022
c1df59d
feat: nested escaping inside optional
mcansh Dec 21, 2022
f6a3f94
chore: remove TODO comment
mcansh Dec 21, 2022
434836f
feat: handle opting out of parent layout, but keeping url segment
mcansh Dec 21, 2022
940d794
chore: use `isSegmentSeparator` to account for windows paths
mcansh Dec 22, 2022
fae3dfa
chore: helpers no longer need to be exported from `routesConvention`
mcansh Dec 22, 2022
a4b0b35
chore: don't return separators from `getRouteSegments`
mcansh Dec 22, 2022
bf0ba0a
chore: rename test
mcansh Dec 22, 2022
e64fe6a
chore: don't treat escaped separator as new segment
mcansh Dec 22, 2022
34d3db5
chore: undo some unneeded changes to `routesConvention`
mcansh Dec 22, 2022
b9892a1
test: route hierarchy
mcansh Dec 22, 2022
c377df9
test: add more test cases
mcansh Dec 22, 2022
5911430
fix: glob
mcansh Dec 22, 2022
da0716d
chore: handle opting out of layout
mcansh Dec 22, 2022
c8660ec
chore: lil clean
mcansh Dec 22, 2022
81c83b4
chore: more tests, add substate type
mcansh Jan 3, 2023
72d2790
chore: re-add lookahead/lookbehind when exiting optional/escape modes
mcansh Jan 3, 2023
647b78d
chore: remove duplicate cases
mcansh Jan 3, 2023
413d3f8
test: update test name
mcansh Jan 3, 2023
e9246d3
chore: advance char earlier, fix bugggg
mcansh Jan 3, 2023
9ceb89f
chore: prevent `/` in route segments
mcansh Jan 4, 2023
3afc886
test: update test name
mcansh Jan 4, 2023
83cd4d8
test: add cases for splitBySegments
mcansh Jan 4, 2023
e00255b
test: add cases for joinByDelimiter
mcansh Jan 4, 2023
0100169
Update packages/remix-dev/config/flat-routes.ts
mcansh Jan 4, 2023
e03efca
Update packages/remix-dev/config/flat-routes.ts
mcansh Jan 4, 2023
a34b7ad
Update packages/remix-dev/config/flat-routes.ts
mcansh Jan 4, 2023
5ab63af
test: remove duplicate routes
mcansh Jan 4, 2023
1436c31
chore: combine jacob's and kiliman's approaches
mcansh Jan 4, 2023
16a97d3
chore: remove console.log
mcansh Jan 4, 2023
ac0cc8c
test: add tests for generically creating route paths
mcansh Jan 4, 2023
7a24b9e
test: add case for skipping layouts other than the top level
mcansh Jan 4, 2023
8679fa0
chore: make tests pass on windows
mcansh Jan 4, 2023
388d89f
chore: use for...of to be consistent
mcansh Jan 4, 2023
e575458
chore: for...of on routeMap
mcansh Jan 5, 2023
4f569fd
fixup! chore: for...of on routeMap
mcansh Jan 5, 2023
6fc6b9f
chore: extract getRouteMap
mcansh Jan 5, 2023
d4d87d0
chore: address feedback
mcansh Jan 5, 2023
4779e24
chore: set default parentId to "root" in defineRoutes
mcansh Jan 5, 2023
0d6c4fb
test: update defineConventionalRoutes test for default root parentId
mcansh Jan 5, 2023
0a8edbf
chore: remove comment
mcansh Jan 5, 2023
7b54735
chore: remove unnecessary else block
mcansh Jan 5, 2023
be8f00c
test: update defineRoutes test per 4779e2458499d15ba31791ff785cdcdf3b…
mcansh Jan 5, 2023
4c9d619
chore: add brief description of what each state/substate means
mcansh Jan 5, 2023
f521c20
chore: remove nesting via early return
mcansh Jan 5, 2023
758a90a
chore: condense state/substate
mcansh Jan 5, 2023
f6e2615
chore: update routePaths to account for folders
mcansh Jan 5, 2023
d54ba0f
chore: error when escaping `:` or `*`
mcansh Jan 5, 2023
c4955a6
chore: remove console.log
mcansh Jan 5, 2023
7515c19
chore: include routeId in error message
mcansh Jan 5, 2023
df340a4
chore: error when using `:` or `*`, not just escaping
mcansh Jan 5, 2023
3f3129e
chore: normalize error messages, move tests to getRouteSegments
mcansh Jan 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions packages/remix-dev/__tests__/flat-routes-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,6 @@ describe("flatRoutes", () => {
path: ".",
},
],
// TODO: talk with @pcattori
// [
// "routes/about.[*].tsx",
// {
// id: "routes/about.[*]",
// parentId: "routes/about",
// path: "*",
// },
// ],
[
"routes/about.[.[.*].].tsx",
{
Expand Down Expand Up @@ -570,10 +561,12 @@ describe("flatRoutes", () => {
],
];

let files = testFiles.map(([file, route]) => {
let filepath = file.split("/").join(path.sep);
return [filepath, { ...route, file: filepath }];
});
let files: [string, Omit<ConfigRoute, "file">][] = testFiles.map(
([file, route]) => {
let filepath = file.split("/").join(path.sep);
return [filepath, { ...route, file: filepath }];
}
);

let routeManifest = flatRoutesUniversal(
APP_DIR,
Expand Down Expand Up @@ -604,4 +597,26 @@ describe("flatRoutes", () => {
`Route segment cannot contain a slash: .lol?/what? (in route ($[$dollabills]).([.]lol)[/](what)/([$]).$)`
);
});

test("should error when using `*` in an escaped route segment", () => {
let files: string[] = ["routes/about.[*].tsx"];

expect(() =>
flatRoutesUniversal(
APP_DIR,
files.map((file) => path.join(APP_DIR, file))
)
).toThrowError("Escaped route segment cannot contain `*`");
});

test("should error when using `:` in an escaped route segment", () => {
let files: string[] = ["routes/about.[:name].tsx"];

expect(() =>
flatRoutesUniversal(
APP_DIR,
files.map((file) => path.join(APP_DIR, file))
)
).toThrowError("Escaped route segment cannot contain `:`");
});
mcansh marked this conversation as resolved.
Show resolved Hide resolved
});
26 changes: 25 additions & 1 deletion packages/remix-dev/config/flat-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@ export function getRouteSegments(routeId: string) {
let routeSegments: string[] = [];
let index = 0;
let routeSegment = "";
let rawRouteSegment = "";
let state: State = "NORMAL";
let pushRouteSegment = (routeSegment: string) => {
if (!routeSegment) return;
if (routeSegment.includes("/")) {
console.log({ routeSegment, rawRouteSegment });

if (rawRouteSegment.includes("/")) {
throw new Error(
`Route segment cannot contain a slash: ${routeSegment} (in route ${routeId})`
);
Expand All @@ -141,6 +144,7 @@ export function getRouteSegments(routeId: string) {
if (isSegmentSeparator(char)) {
pushRouteSegment(routeSegment);
routeSegment = "";
rawRouteSegment = "";
state = "NORMAL";
break;
}
Expand All @@ -155,25 +159,40 @@ export function getRouteSegments(routeId: string) {
if (!routeSegment && char == paramPrefixChar) {
if (index === routeId.length) {
routeSegment += "*";
rawRouteSegment += char;
} else {
routeSegment += ":";
rawRouteSegment += char;
}
break;
}

routeSegment += char;
rawRouteSegment += char;
break;
}
case "ESCAPE": {
if (char === escapeEnd) {
console.log({ rawRouteSegment });

if (rawRouteSegment === "*") {
throw new Error("Escaped route segment cannot contain `*`");
}
if (rawRouteSegment.includes(":")) {
throw new Error("Escaped route segment cannot contain `:`");
}
state = "NORMAL";
break;
}

routeSegment += char;
rawRouteSegment += char;
break;
}
case "OPTIONAL": {
if (char === optionalEnd) {
routeSegment += "?";
rawRouteSegment += "?";
state = "NORMAL";
break;
}
Expand All @@ -186,21 +205,26 @@ export function getRouteSegments(routeId: string) {
if (!routeSegment && char === paramPrefixChar) {
if (index === routeId.length) {
routeSegment += "*";
rawRouteSegment += char;
} else {
routeSegment += ":";
rawRouteSegment += char;
}
break;
}

routeSegment += char;
rawRouteSegment += char;
break;
}
case "OPTIONAL_ESCAPE": {
if (char === escapeEnd) {
state = "OPTIONAL";
break;
}

routeSegment += char;
rawRouteSegment += char;
break;
}
}
Expand Down