-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Read Vite config in reveal
/routes
commands
#8916
Conversation
🦋 Changeset detectedLatest commit: 116672f The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
async function resolveViteConfig({ | ||
configFile, | ||
mode, | ||
root, | ||
}: { | ||
configFile?: string; | ||
mode?: string; | ||
root: string; | ||
}) { | ||
let vite = await import("vite"); | ||
|
||
// Leverage the Vite config as a way to configure the entire multi-step build | ||
// process so we don't need to have a separate Remix config | ||
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; | ||
} | ||
|
||
async function extractRemixPluginContext(viteConfig: Vite.ResolvedConfig) { | ||
let ctx = viteConfig["__remixPluginContext" as keyof typeof viteConfig] as | ||
| RemixPluginContext | ||
| undefined; | ||
|
||
if (!ctx) { | ||
console.error(colors.red("Remix Vite plugin not found in Vite config")); | ||
process.exit(1); | ||
} | ||
|
||
return ctx; | ||
} | ||
|
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 has moved to vite/plugin.ts
since the logic is now shared and not specific to the vite:build
command.
if (!ctx) { | ||
console.error(colors.red("Remix Vite plugin not found in Vite config")); | ||
process.exit(1); | ||
} |
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 error used to be in extractRemixPluginContext
but since it's now used in contexts where it's not guaranteed that the consumer is using Vite, extractRemixPluginContext
now returns undefined
if Vite and/or the Remix Vite plugin aren't found, so we handle the error here now instead.
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 extractRemixPluginContext( | ||
viteConfig: Vite.ResolvedConfig | ||
) { | ||
return viteConfig["__remixPluginContext" as keyof typeof viteConfig] as | ||
| RemixPluginContext | ||
| undefined; | ||
} |
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.
As noted above, these have moved from vite/build.ts
.
export async function loadVitePluginContext({ | ||
configFile, | ||
root, | ||
}: { | ||
configFile?: string; | ||
root?: string; | ||
}) { | ||
if (!root) { | ||
root = process.env.REMIX_ROOT || process.cwd(); | ||
} | ||
|
||
configFile = | ||
configFile ?? | ||
findConfig(root, "vite.config", [ | ||
".ts", | ||
".cts", | ||
".mts", | ||
".js", | ||
".cjs", | ||
".mjs", | ||
]); | ||
|
||
// V3 TODO: Vite config should not be optional | ||
if (!configFile) { | ||
return; | ||
} | ||
|
||
let viteConfig = await resolveViteConfig({ configFile, root }); | ||
return await extractRemixPluginContext(viteConfig); | ||
} |
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 is the logic used by the reveal
and routes
commands. Since it's not guaranteed that consumers are using Vite, this function returns undefined
if a Vite config doesn't exist or if the Remix Vite plugin isn't present. This allows us to fall back to the old Remix config.
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.
Nice work! Gave it a quick test on my vite app and npx remix routes
picked up custom routes. I also pushed a commit to this branch for npx remix reveal
to reveal the simpler SPA mode server entry.
packages/remix-dev/cli/commands.ts
Outdated
let formatArg = flags.json ? "json" : "jsx"; | ||
let format = isRoutesFormat(formatArg) ? formatArg : RoutesFormat.jsx; |
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.
I think we can streamline this due to the boolean and remove the isRoutesFormat
utility?
let formatArg = flags.json ? "json" : "jsx"; | |
let format = isRoutesFormat(formatArg) ? formatArg : RoutesFormat.jsx; | |
let format = flags.json ? RoutesFormat.json : RoutesFormat.jsx; |
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.
{ | ||
"--config": String, | ||
"-c": "--config", | ||
} | ||
: { | ||
// Handle non Vite config commands | ||
"-c": "--command", | ||
}), |
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 feels slightly odd but looks like it was already there before - npx remix -c
means something different depending on if you're using vite?
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.
The issue was that remix vite:
commands are designed to mirror the Vite CLI where -c
is short for --config
. This config flag now needs to work with reveal
and routes
too. Passing -c
to these commands currently has no effect.
@@ -120,14 +120,25 @@ export async function run(argv: string[] = process.argv.slice(2)) { | |||
"--tls-key": String, | |||
"--tls-cert": String, | |||
|
|||
...(argv[0].startsWith("vite:") || |
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.
Not related to this PR but I noticed we don't mention anything about the vite:
commands in the npx remix --help
output - might be worth a follow up to add some info there?
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.
Updated the --help
output here: #8939
🤖 Hello there, We just published version Thanks! |
🤖 Hello there, We just published version Thanks! |
Fixes #8845
Fixes #8881