-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Fix root directory resolution logic #13472
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@react-router/dev": patch | ||
--- | ||
|
||
Support project root directories without a `package.json` if it exists in a parent directory |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@react-router/dev": patch | ||
--- | ||
|
||
When providing a custom Vite config path via the CLI `--config`/`-c` flag, default the project root directory to the directory containing the Vite config when not explicitly provided |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@react-router/dev": patch | ||
--- | ||
|
||
Ensure consistent project root directory resolution logic in CLI commands |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,13 @@ import * as Typegen from "../typegen"; | |
import { preloadVite, getVite } from "../vite/vite"; | ||
|
||
export async function routes( | ||
reactRouterRoot?: string, | ||
rootDirectory?: string, | ||
flags: { | ||
config?: string; | ||
json?: boolean; | ||
} = {} | ||
): Promise<void> { | ||
let rootDirectory = reactRouterRoot ?? process.cwd(); | ||
rootDirectory = resolveRootDirectory(rootDirectory, flags); | ||
let configResult = await loadConfig({ rootDirectory }); | ||
|
||
if (!configResult.ok) { | ||
|
@@ -39,9 +39,7 @@ export async function build( | |
root?: string, | ||
options: ViteBuildOptions = {} | ||
): Promise<void> { | ||
if (!root) { | ||
root = process.env.REACT_ROUTER_ROOT || process.cwd(); | ||
} | ||
root = resolveRootDirectory(root, options); | ||
|
||
let { build } = await import("../vite/build"); | ||
if (options.profile) { | ||
|
@@ -54,12 +52,14 @@ export async function build( | |
} | ||
} | ||
|
||
export async function dev(root: string, options: ViteDevOptions = {}) { | ||
export async function dev(root?: string, options: ViteDevOptions = {}) { | ||
let { dev } = await import("../vite/dev"); | ||
if (options.profile) { | ||
await profiler.start(); | ||
} | ||
exitHook(() => profiler.stop(console.info)); | ||
|
||
root = resolveRootDirectory(root, options); | ||
await dev(root, options); | ||
|
||
// keep `react-router dev` alive by waiting indefinitely | ||
|
@@ -77,20 +77,20 @@ let conjunctionListFormat = new Intl.ListFormat("en", { | |
|
||
export async function generateEntry( | ||
entry?: string, | ||
reactRouterRoot?: string, | ||
rootDirectory?: string, | ||
flags: { | ||
typescript?: boolean; | ||
config?: string; | ||
} = {} | ||
) { | ||
// if no entry passed, attempt to create both | ||
if (!entry) { | ||
await generateEntry("entry.client", reactRouterRoot, flags); | ||
await generateEntry("entry.server", reactRouterRoot, flags); | ||
await generateEntry("entry.client", rootDirectory, flags); | ||
await generateEntry("entry.server", rootDirectory, flags); | ||
return; | ||
} | ||
|
||
let rootDirectory = reactRouterRoot ?? process.cwd(); | ||
rootDirectory = resolveRootDirectory(rootDirectory, flags); | ||
let configResult = await loadConfig({ rootDirectory }); | ||
|
||
if (!configResult.ok) { | ||
|
@@ -162,6 +162,17 @@ export async function generateEntry( | |
); | ||
} | ||
|
||
function resolveRootDirectory(root?: string, flags?: { config?: string }) { | ||
if (root) { | ||
return path.resolve(root); | ||
} | ||
|
||
return ( | ||
process.env.REACT_ROUTER_ROOT || | ||
(flags?.config ? path.dirname(path.resolve(flags.config)) : process.cwd()) | ||
); | ||
} | ||
|
||
async function checkForEntry( | ||
rootDirectory: string, | ||
appDirectory: string, | ||
|
@@ -198,8 +209,14 @@ async function createClientEntry( | |
return contents; | ||
} | ||
|
||
export async function typegen(root: string, flags: { watch: boolean }) { | ||
root ??= process.cwd(); | ||
export async function typegen( | ||
root: string, | ||
flags: { | ||
watch?: boolean; | ||
config?: string; | ||
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. @pcattori Just calling out that since the 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. Side note, but I think this raises something I might have missed when we introduced This is part of my rationale for this change. If our |
||
} | ||
) { | ||
root = resolveRootDirectory(root, flags); | ||
|
||
if (flags.watch) { | ||
await preloadVite(); | ||
|
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.
Note that if a root directory was explicitly provided (e.g.
react-router build my/app/dir
) this will still take precedence. We only fall back to inferring the root when this isn't provided, so anyone relying on this should maintain existing behaviour.