Skip to content

Commit 88c709a

Browse files
committed
Only generate types for route modules in a projects app directory
1 parent 2d3bcaa commit 88c709a

File tree

1 file changed

+20
-6
lines changed
  • packages/react-router-dev/typegen

1 file changed

+20
-6
lines changed

packages/react-router-dev/typegen/index.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { createConfigLoader } from "../config/config";
99
import { generate } from "./generate";
1010
import type { Context } from "./context";
1111
import { getTypesDir, getTypesPath } from "./paths";
12+
import type { RouteManifestEntry } from "../config/routes";
1213

1314
export async function run(rootDirectory: string) {
1415
const ctx = await createContext({ rootDirectory, watch: false });
@@ -71,14 +72,27 @@ async function createContext({
7172
};
7273
}
7374

75+
function isRouteInAppDirectory(ctx: Context, route: RouteManifestEntry) {
76+
const absoluteRoutePath = Path.isAbsolute(route.file)
77+
? route.file
78+
: Path.resolve(ctx.config.appDirectory, route.file);
79+
80+
return absoluteRoutePath.startsWith(ctx.config.appDirectory);
81+
}
82+
7483
async function writeAll(ctx: Context): Promise<void> {
7584
const typegenDir = getTypesDir(ctx);
7685

7786
fs.rmSync(typegenDir, { recursive: true, force: true });
78-
Object.values(ctx.config.routes).forEach((route) => {
79-
const typesPath = getTypesPath(ctx, route);
80-
const content = generate(ctx, route);
81-
fs.mkdirSync(Path.dirname(typesPath), { recursive: true });
82-
fs.writeFileSync(typesPath, content);
83-
});
87+
Object.values(ctx.config.routes)
88+
.filter((route) => {
89+
// Only generate types for routes in the app directory
90+
return isRouteInAppDirectory(ctx, route);
91+
})
92+
.forEach((route) => {
93+
const typesPath = getTypesPath(ctx, route);
94+
const content = generate(ctx, route);
95+
fs.mkdirSync(Path.dirname(typesPath), { recursive: true });
96+
fs.writeFileSync(typesPath, content);
97+
});
8498
}

0 commit comments

Comments
 (0)