-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
Skip typegen for routes outside the app directory #12996
Open
wilcoxmd
wants to merge
5
commits into
remix-run:dev
Choose a base branch
from
wilcoxmd:mwilcox/skip-imported-route-typegen
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+361
−264
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74fed50
Add failing test for workspace route library
wilcoxmd 6902a87
Only generate types for route modules in a projects app directory
wilcoxmd f8909be
Add changeset
wilcoxmd 9d0dc64
Add username to contributors.yml
wilcoxmd 7f1f4b9
Merge remote-tracking branch 'upstream/dev' into mwilcox/skip-importe…
wilcoxmd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@react-router/dev": patch | ||
--- | ||
|
||
Prevent typegen with route files are outside the app directory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -332,6 +332,7 @@ | |
- vonagam | ||
- WalkAlone0325 | ||
- whxhlgy | ||
- wilcoxmd | ||
- willemarcel | ||
- williamsdyyz | ||
- willsawyerrrr | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import fse from "fs-extra"; | ||
import stripIndent from "strip-indent"; | ||
import path from "node:path"; | ||
import url from "node:url"; | ||
|
||
const __dirname = url.fileURLToPath(new URL(".", import.meta.url)); | ||
const root = path.resolve(__dirname, "../.."); | ||
const TMP_DIR = path.join(root, ".tmp/integration"); | ||
|
||
export async function writeTestFiles( | ||
files: Record<string, string> | undefined, | ||
dir: string | ||
) { | ||
await Promise.all( | ||
Object.keys(files ?? {}).map(async (filename) => { | ||
let filePath = path.join(dir, filename); | ||
await fse.ensureDir(path.dirname(filePath)); | ||
let file = files![filename]; | ||
|
||
await fse.writeFile(filePath, stripIndent(file)); | ||
}) | ||
); | ||
} | ||
|
||
export async function getAllFilesInDir(dirPath: string): Promise<string[]> { | ||
const entries = await fse.promises.readdir(dirPath, { withFileTypes: true }); | ||
const files = await Promise.all( | ||
entries.map((entry) => { | ||
const resolvedPath = path.resolve(dirPath, entry.name); | ||
if (entry.isDirectory()) { | ||
return getAllFilesInDir(resolvedPath); | ||
} else { | ||
return [resolvedPath]; | ||
} | ||
}) | ||
); | ||
return files.flat(); | ||
} | ||
|
||
export async function createTestDirectory() { | ||
let folderName = `rr-${Math.random().toString(32).slice(2)}`; | ||
let testDir = path.join(TMP_DIR, folderName); | ||
await fse.ensureDir(testDir); | ||
return testDir; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
integration/helpers/workspace-routes-template/apps/test-app/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules | ||
|
||
/.cache | ||
/build | ||
.env | ||
.react-router |
19 changes: 19 additions & 0 deletions
19
integration/helpers/workspace-routes-template/apps/test-app/app/root.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Links, Meta, Outlet, Scripts, ScrollRestoration } from "react-router"; | ||
|
||
export default function App() { | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
<meta charSet="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<Meta /> | ||
<Links /> | ||
</head> | ||
<body> | ||
<Outlet /> | ||
<ScrollRestoration /> | ||
<Scripts /> | ||
</body> | ||
</html> | ||
); | ||
} |
4 changes: 4 additions & 0 deletions
4
integration/helpers/workspace-routes-template/apps/test-app/app/routes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { type RouteConfig } from "@react-router/dev/routes"; | ||
import { flatRoutes } from "@react-router/fs-routes"; | ||
|
||
export default flatRoutes() satisfies RouteConfig; |
16 changes: 16 additions & 0 deletions
16
integration/helpers/workspace-routes-template/apps/test-app/app/routes/_index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { MetaFunction } from "react-router"; | ||
|
||
export const meta: MetaFunction = () => { | ||
return [ | ||
{ title: "New React Router App" }, | ||
{ name: "description", content: "Welcome to React Router!" }, | ||
]; | ||
}; | ||
|
||
export default function Index() { | ||
return ( | ||
<div style={{ fontFamily: "system-ui, sans-serif", lineHeight: "1.8" }}> | ||
<h1>Welcome to React Router</h1> | ||
</div> | ||
); | ||
} |
2 changes: 2 additions & 0 deletions
2
integration/helpers/workspace-routes-template/apps/test-app/env.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/// <reference types="@react-router/node" /> | ||
/// <reference types="vite/client" /> |
42 changes: 42 additions & 0 deletions
42
integration/helpers/workspace-routes-template/apps/test-app/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "integration-workspace-test-app-template", | ||
"version": "0.0.0", | ||
"private": true, | ||
"sideEffects": false, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "react-router dev", | ||
"build": "react-router build", | ||
"start": "react-router-serve ./build/server/index.js", | ||
"typecheck": "react-router typegen && tsc" | ||
}, | ||
"dependencies": { | ||
"@react-router/express": "workspace:*", | ||
"@react-router/node": "workspace:*", | ||
"@react-router/serve": "workspace:*", | ||
"@vanilla-extract/css": "^1.10.0", | ||
"@vanilla-extract/vite-plugin": "^3.9.2", | ||
"express": "^4.19.2", | ||
"isbot": "^5.1.11", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"react-router": "workspace:*", | ||
"test-route-library": "workspace:*", | ||
"serialize-javascript": "^6.0.1" | ||
}, | ||
"devDependencies": { | ||
"@react-router/dev": "workspace:*", | ||
"@react-router/fs-routes": "workspace:*", | ||
"@react-router/remix-routes-option-adapter": "workspace:*", | ||
"@types/react": "^18.2.20", | ||
"@types/react-dom": "^18.2.7", | ||
"eslint": "^8.38.0", | ||
"typescript": "^5.1.6", | ||
"vite": "^6.0.0", | ||
"vite-env-only": "^3.0.1", | ||
"vite-tsconfig-paths": "^4.2.1" | ||
}, | ||
"engines": { | ||
"node": ">=20.0.0" | ||
} | ||
} |
Binary file added
BIN
+14.7 KB
integration/helpers/workspace-routes-template/apps/test-app/public/favicon.ico
Binary file not shown.
27 changes: 27 additions & 0 deletions
27
integration/helpers/workspace-routes-template/apps/test-app/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"include": [ | ||
"env.d.ts", | ||
"**/*.ts", | ||
"**/*.tsx", | ||
".react-router/types/**/*.d.ts" | ||
], | ||
"compilerOptions": { | ||
"lib": ["DOM", "DOM.Iterable", "ES2022"], | ||
"verbatimModuleSyntax": true, | ||
"esModuleInterop": true, | ||
"jsx": "react-jsx", | ||
"module": "ESNext", | ||
"moduleResolution": "Bundler", | ||
"resolveJsonModule": true, | ||
"target": "ES2022", | ||
"strict": true, | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"baseUrl": ".", | ||
"paths": { | ||
"~/*": ["./app/*"] | ||
}, | ||
"noEmit": true, | ||
"rootDirs": [".", ".react-router/types/"] | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
integration/helpers/workspace-routes-template/apps/test-app/vite.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { reactRouter } from "@react-router/dev/vite"; | ||
import { defineConfig } from "vite"; | ||
import tsconfigPaths from "vite-tsconfig-paths"; | ||
|
||
export default defineConfig({ | ||
plugins: [reactRouter(), tsconfigPaths()], | ||
}); |
1 change: 1 addition & 0 deletions
1
integration/helpers/workspace-routes-template/libs/test-route-library/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export function sum(a: number, b: number): number; |
3 changes: 3 additions & 0 deletions
3
integration/helpers/workspace-routes-template/libs/test-route-library/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function sum(a, b) { | ||
return a + b; | ||
} |
12 changes: 12 additions & 0 deletions
12
integration/helpers/workspace-routes-template/libs/test-route-library/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "test-route-library", | ||
"type": "module", | ||
"private": true, | ||
"exports": { | ||
".": "./index.js" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18.2.0", | ||
"@react-router/dev": "workspace:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import pc from "picocolors"; | |
import type vite from "vite"; | ||
|
||
import { createConfigLoader } from "../config/config"; | ||
import type { RouteManifestEntry } from "../config/routes"; | ||
import * as Babel from "../vite/babel"; | ||
|
||
import { generate } from "./generate"; | ||
|
@@ -75,11 +76,21 @@ async function createContext({ | |
}; | ||
} | ||
|
||
function isRouteInAppDirectory(ctx: Context, route: RouteManifestEntry) { | ||
const absoluteRoutePath = Path.resolve(ctx.config.appDirectory, route.file); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return absoluteRoutePath.startsWith(ctx.config.appDirectory); | ||
} | ||
|
||
async function writeAll(ctx: Context): Promise<void> { | ||
const typegenDir = getTypesDir(ctx); | ||
|
||
fs.rmSync(typegenDir, { recursive: true, force: true }); | ||
Object.values(ctx.config.routes).forEach((route) => { | ||
// We only generate types for routes in the app directory | ||
if (!isRouteInAppDirectory(ctx, route)) { | ||
return; | ||
} | ||
|
||
const typesPath = getTypesPath(ctx, route); | ||
const content = generate(ctx, route); | ||
fs.mkdirSync(Path.dirname(typesPath), { recursive: true }); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function, and the logic in
createTestDirectory
below were pulled from other files and just refactored into this file for reuse.