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

routes.ts: Move API to route-config package #10118

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions integration/helpers/vite-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"devDependencies": {
"@remix-run/dev": "workspace:*",
"@remix-run/eslint-config": "workspace:*",
"@remix-run/route-config": "workspace:*",
"@types/react": "^18.2.20",
"@types/react-dom": "^18.2.7",
"eslint": "^8.38.0",
Expand Down
32 changes: 10 additions & 22 deletions integration/vite-route-config-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test.describe("route config", () => {
})]
}
`,
"app/routes.ts": `export default INVALID(`,
"app/routes.ts": `export const routes = [];`,
});
let buildResult = viteBuild({ cwd });
expect(buildResult.status).toBe(1);
Expand All @@ -70,7 +70,7 @@ test.describe("route config", () => {
})]
}
`,
"app/routes.ts": `export default INVALID(`,
"app/routes.ts": `export const routes = [];`,
});
let devError: Error | undefined;
try {
Expand Down Expand Up @@ -121,13 +121,10 @@ test.describe("route config", () => {
let files: Files = async ({ port }) => ({
"vite.config.js": await viteConfig.basic({ port }),
"app/routes.ts": js`
import { type RouteConfig } from "@react-router/dev/routes";
import { type RouteConfig, index } from "@remix-run/route-config";

export const routes: RouteConfig = [
{
file: "test-route-1.tsx",
index: true,
},
index("test-route-1.tsx"),
];
`,
"app/test-route-1.tsx": `
Expand Down Expand Up @@ -185,13 +182,10 @@ test.describe("route config", () => {
export { routes } from "./actual-routes";
`,
"app/actual-routes.ts": js`
import { type RouteConfig } from "@react-router/dev/routes";
import { type RouteConfig, index } from "@remix-run/route-config";

export const routes: RouteConfig = [
{
file: "test-route-1.tsx",
index: true,
},
index("test-route-1.tsx"),
];
`,
"app/test-route-1.tsx": `
Expand Down Expand Up @@ -246,13 +240,10 @@ test.describe("route config", () => {
let files: Files = async ({ port }) => ({
"vite.config.js": await viteConfig.basic({ port }),
"app/routes.ts": js`
import { type RouteConfig } from "@react-router/dev/routes";
import { type RouteConfig, index } from "@remix-run/route-config";

export const routes: RouteConfig = [
{
file: "test-route-1.tsx",
index: true,
},
index("test-route-1.tsx"),
];
`,
"app/test-route-1.tsx": `
Expand Down Expand Up @@ -325,13 +316,10 @@ test.describe("route config", () => {
"vite.config.js": await viteConfig.basic({ port }),
"app/routes.ts": js`
import path from "node:path";
import { type RouteConfig } from "@react-router/dev/routes";
import { type RouteConfig, index } from "@remix-run/route-config";

export const routes: RouteConfig = [
{
file: path.resolve(import.meta.dirname, "test-route.tsx"),
index: true,
},
index(path.resolve(import.meta.dirname, "test-route.tsx")),
];
`,
"app/test-route.tsx": `
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
"packages/remix-express",
"packages/remix-node",
"packages/remix-react",
"packages/remix-route-config",
"packages/remix-serve",
"packages/remix-server-runtime",
"packages/remix-testing",
Expand Down
141 changes: 141 additions & 0 deletions packages/remix-dev/__tests__/validateRouteConfig-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { validateRouteConfig } from "../config/routes";

describe("validateRouteConfig", () => {
it("validates a route config", () => {
expect(
validateRouteConfig({
routeConfigFile: "routes.ts",
routeConfig: [
{
path: "parent",
file: "parent.tsx",
children: [
{
path: "child",
file: "child.tsx",
},
],
},
],
}).valid
).toBe(true);
});

it("is invalid when not an array", () => {
let result = validateRouteConfig({
routeConfigFile: "routes.ts",
routeConfig: { path: "path", file: "file.tsx" },
});

expect(result.valid).toBe(false);
expect(!result.valid && result.message).toMatchInlineSnapshot(
`"Route config in "routes.ts" must be an array."`
);
});

it("is invalid when route is a promise", () => {
let result = validateRouteConfig({
routeConfigFile: "routes.ts",
routeConfig: [
{
path: "parent",
file: "parent.tsx",
children: [Promise.resolve({})],
},
],
});

expect(result.valid).toBe(false);
expect(!result.valid && result.message).toMatchInlineSnapshot(`
"Route config in "routes.ts" is invalid.

Path: routes.0.children.0
Invalid type: Expected object but received a promise. Did you forget to await?"
`);
});

it("is invalid when file is missing", () => {
let result = validateRouteConfig({
routeConfigFile: "routes.ts",
routeConfig: [
{
path: "parent",
file: "parent.tsx",
children: [
{
id: "child",
},
],
},
],
});

expect(result.valid).toBe(false);
expect(!result.valid && result.message).toMatchInlineSnapshot(`
"Route config in "routes.ts" is invalid.

Path: routes.0.children.0.file
Invalid type: Expected string but received undefined"
`);
});

it("is invalid when property is wrong type", () => {
let result = validateRouteConfig({
routeConfigFile: "routes.ts",
routeConfig: [
{
path: "parent",
file: "parent.tsx",
children: [
{
file: 123,
},
],
},
],
});

expect(result.valid).toBe(false);
expect(!result.valid && result.message).toMatchInlineSnapshot(`
"Route config in "routes.ts" is invalid.

Path: routes.0.children.0.file
Invalid type: Expected string but received 123"
`);
});

it("shows multiple error messages", () => {
let result = validateRouteConfig({
routeConfigFile: "routes.ts",
routeConfig: [
{
path: "parent",
file: "parent.tsx",
children: [
{
id: "child",
},
{
file: 123,
},
Promise.resolve(),
],
},
],
});

expect(result.valid).toBe(false);
expect(!result.valid && result.message).toMatchInlineSnapshot(`
"Route config in "routes.ts" is invalid.

Path: routes.0.children.0.file
Invalid type: Expected string but received undefined

Path: routes.0.children.1.file
Invalid type: Expected string but received 123

Path: routes.0.children.2
Invalid type: Expected object but received a promise. Did you forget to await?"
`);
});
});
4 changes: 2 additions & 2 deletions packages/remix-dev/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
type RouteManifest,
type RouteConfig,
type DefineRoutesFunction,
setAppDirectory,
setRouteConfigAppDirectory,
validateRouteConfig,
configRoutesToRouteManifest,
defineRoutes,
Expand Down Expand Up @@ -577,7 +577,7 @@ export async function resolveConfig(
root: { path: "", id: "root", file: rootRouteFile },
};

setAppDirectory(appDirectory);
setRouteConfigAppDirectory(appDirectory);
let routeConfigFile = findEntry(appDirectory, "routes");
if (routesViteNodeContext && vite && routeConfigFile) {
class FriendlyError extends Error {}
Expand Down
Loading