-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Decouple CLI arg parsing/execution for NODE_ENV initialization #12505
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f62c224
Decouple CLI arg parsing/execution for NODE_ENV initialization
brophdawg11 c01b823
Properly set node env
brophdawg11 f920e54
Updates
brophdawg11 44c4e31
Add changeset
brophdawg11 4095308
fix(react-router-serve): ensure node_env set before react loaded
jrestall c9d314d
chore: add jrestall to contributors.yml as CLA signature
jrestall 12cf86a
Add entry point for run.ts
brophdawg11 9b1e38b
Revert "Add entry point for run.ts"
brophdawg11 5446875
Change approach to extract plugin context
brophdawg11 3ebe742
Merge branch 'dev' into brophdawg11/node-env
brophdawg11 2fe399b
Update .changeset/default-node-env.md
brophdawg11 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,6 @@ | ||
--- | ||
"@react-router/serve": patch | ||
"@react-router/dev": patch | ||
--- | ||
|
||
Properly initialize `NODE_ENV` if not already set for compatibility with React 19 |
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 |
---|---|---|
|
@@ -141,6 +141,7 @@ | |
- johnpangalos | ||
- jonkoops | ||
- jrakotoharisoa | ||
- jrestall | ||
- juanpprieto | ||
- jungwoo3490 | ||
- kachun333 | ||
|
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
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,85 @@ | ||
import arg from "arg"; | ||
import semver from "semver"; | ||
|
||
/** | ||
* Parse command line arguments for `react-router` dev CLI | ||
*/ | ||
export function parseArgs(argv: string[] = process.argv.slice(2)) { | ||
// Check the node version | ||
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. No changes to this logic, just extracted out from |
||
let versions = process.versions; | ||
let MINIMUM_NODE_VERSION = 20; | ||
if ( | ||
versions && | ||
versions.node && | ||
semver.major(versions.node) < MINIMUM_NODE_VERSION | ||
) { | ||
console.warn( | ||
`️⚠️ Oops, Node v${versions.node} detected. react-router requires ` + | ||
`a Node version greater than ${MINIMUM_NODE_VERSION}.` | ||
); | ||
} | ||
|
||
let isBooleanFlag = (arg: string) => { | ||
let index = argv.indexOf(arg); | ||
let nextArg = argv[index + 1]; | ||
return !nextArg || nextArg.startsWith("-"); | ||
}; | ||
|
||
let args = arg( | ||
{ | ||
"--force": Boolean, | ||
"--help": Boolean, | ||
"-h": "--help", | ||
"--json": Boolean, | ||
"--token": String, | ||
"--typescript": Boolean, | ||
"--no-typescript": Boolean, | ||
"--version": Boolean, | ||
"-v": "--version", | ||
"--port": Number, | ||
"-p": "--port", | ||
"--config": String, | ||
"-c": "--config", | ||
"--assetsInlineLimit": Number, | ||
"--clearScreen": Boolean, | ||
"--cors": Boolean, | ||
"--emptyOutDir": Boolean, | ||
"--host": isBooleanFlag("--host") ? Boolean : String, | ||
"--logLevel": String, | ||
"-l": "--logLevel", | ||
"--minify": String, | ||
"--mode": String, | ||
"-m": "--mode", | ||
"--open": isBooleanFlag("--open") ? Boolean : String, | ||
"--strictPort": Boolean, | ||
"--profile": Boolean, | ||
"--sourcemapClient": isBooleanFlag("--sourcemapClient") | ||
? Boolean | ||
: String, | ||
"--sourcemapServer": isBooleanFlag("--sourcemapServer") | ||
? Boolean | ||
: String, | ||
"--watch": Boolean, | ||
}, | ||
{ | ||
argv, | ||
} | ||
); | ||
|
||
let flags: any = Object.entries(args).reduce((acc, [key, value]) => { | ||
key = key.replace(/^--/, ""); | ||
acc[key] = value; | ||
return acc; | ||
}, {} as any); | ||
|
||
flags.interactive = flags.interactive ?? require.main === module; | ||
if (args["--no-typescript"]) { | ||
flags.typescript = false; | ||
} | ||
|
||
return { | ||
input: args._, | ||
command: args._[0], | ||
flags, | ||
}; | ||
} |
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
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,120 @@ | ||
// We can only import types from Vite at the top level since we're in a CJS | ||
// context but want to use Vite's ESM build to avoid deprecation warnings | ||
import * as fse from "fs-extra"; | ||
import * as path from "node:path"; | ||
import colors from "picocolors"; | ||
import type * as Vite from "vite"; | ||
import type { ResolvedReactRouterConfig } from "../config/config"; | ||
import type { RouteManifest } from "../config/routes"; | ||
import type { Manifest as ReactRouterManifest } from "../manifest"; | ||
|
||
export type ServerBundleBuildConfig = { | ||
routes: RouteManifest; | ||
serverBundleId: string; | ||
}; | ||
|
||
export type ReactRouterPluginSsrBuildContext = | ||
| { | ||
isSsrBuild: false; | ||
getReactRouterServerManifest?: never; | ||
serverBundleBuildConfig?: never; | ||
} | ||
| { | ||
isSsrBuild: true; | ||
getReactRouterServerManifest: () => Promise<ReactRouterManifest>; | ||
serverBundleBuildConfig: ServerBundleBuildConfig | null; | ||
}; | ||
|
||
export type ReactRouterPluginContext = ReactRouterPluginSsrBuildContext & { | ||
rootDirectory: string; | ||
entryClientFilePath: string; | ||
entryServerFilePath: string; | ||
publicPath: string; | ||
reactRouterConfig: ResolvedReactRouterConfig; | ||
viteManifestEnabled: boolean; | ||
}; | ||
|
||
export function findConfig( | ||
dir: string, | ||
basename: string, | ||
extensions: string[] | ||
): string | undefined { | ||
for (let ext of extensions) { | ||
let name = basename + ext; | ||
let file = path.join(dir, name); | ||
if (fse.existsSync(file)) return file; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
export async function resolveViteConfig({ | ||
configFile, | ||
mode, | ||
root, | ||
}: { | ||
configFile?: string; | ||
mode?: string; | ||
root: string; | ||
}) { | ||
let vite = await import("vite"); | ||
|
||
let viteConfig = await vite.resolveConfig( | ||
{ mode, configFile, root }, | ||
"build", // command | ||
"production", // default mode | ||
"production" // default NODE_ENV | ||
); | ||
|
||
if (typeof viteConfig.build.manifest === "string") { | ||
throw new Error("Custom Vite manifest paths are not supported"); | ||
} | ||
|
||
return viteConfig; | ||
} | ||
|
||
export async function extractPluginContext(viteConfig: Vite.ResolvedConfig) { | ||
return viteConfig["__reactRouterPluginContext" as keyof typeof viteConfig] as | ||
| ReactRouterPluginContext | ||
| undefined; | ||
} | ||
|
||
export async function loadPluginContext({ | ||
configFile, | ||
root, | ||
}: { | ||
configFile?: string; | ||
root?: string; | ||
}) { | ||
if (!root) { | ||
root = process.env.REACT_ROUTER_ROOT || process.cwd(); | ||
} | ||
|
||
configFile = | ||
configFile ?? | ||
findConfig(root, "vite.config", [ | ||
".ts", | ||
".cts", | ||
".mts", | ||
".js", | ||
".cjs", | ||
".mjs", | ||
]); | ||
|
||
if (!configFile) { | ||
console.error(colors.red("Vite config file not found")); | ||
process.exit(1); | ||
} | ||
|
||
let viteConfig = await resolveViteConfig({ configFile, root }); | ||
let ctx = await extractPluginContext(viteConfig); | ||
|
||
if (!ctx) { | ||
console.error( | ||
colors.red("React Router Vite plugin not found in Vite config") | ||
); | ||
process.exit(1); | ||
} | ||
|
||
return ctx; | ||
} |
Oops, something went wrong.
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.
Parse args and set
NODE_ENV
first