Skip to content
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

Loading vite config when it is not in the current working directory #92

Closed
peterlama opened this issue Nov 18, 2023 · 5 comments · Fixed by #102
Closed

Loading vite config when it is not in the current working directory #92

peterlama opened this issue Nov 18, 2023 · 5 comments · Fixed by #102

Comments

@peterlama
Copy link
Contributor

First of all, I want to thank you! I was able to accomplish exactly what I wanted using this module.
With that said, I have a unique requirement and I had to make a couple changes to get it to work.

The thing is, my project is in a monorepo and I have the Express server and vite app in two different packages. This means the vite config is not in the current working directory and vite can't find it. There is the vite-express config option inlineViteConfig, but it seemed like vite plugins couldn't be used with that.

My solution was to pass the absolute path of vite.config.ts to vite.resolveConfig and the root path to vite.createServer.
Essentially:

@@ -85,7 +87,10 @@ async function resolveConfig(): Promise<ViteConfig> {
   try {
     const { resolveConfig } = await import("vite");
     try {
-      const config = await resolveConfig({}, "build");
+
+      const config = await resolveConfig({
+        configFile: Config.viteConfigFile,
+      }, "build");
       info(
         `Using ${pc.yellow("Vite")} to resolve the ${pc.yellow("config file")}`
       );
@@ -252,7 +257,9 @@ async function startServer(server: http.Server | https.Server) {
   const isUsingViteResolvedConfig = Object.entries(config).length > 3;
 
   const vite = await createServer(
-    mergeConfig(isUsingViteResolvedConfig ? {} : config, {
+    mergeConfig(isUsingViteResolvedConfig ? {
+      root: config.root,
+    } : config, {

Would you be open to a PR for that?

@szymmis
Copy link
Owner

szymmis commented Nov 20, 2023

Hi @peterlama, thanks for raising that issue.
That's the case I've been thinking about and now that you need that feature is the best time to implement it. I could do that, but I strongly encourage you to try implementing that yourself as every contribution is welcome. Do you want to try? If not, then no problem, but if you do, I'll be happy to help and guide you.

When it comes to the implementation, I think that creating a config option for custom config path and passing it down as you did is the best way.

@griimick
Copy link

I stumbled upon the same problem statement and ended up setting up Vite SSR myself. I can give it a go if this hasn't been picked up yet. But what I did was similar to what OP has mentioned but in my case as the server was behind a reverse proxy, I did not want the server to use config.root.

@peterlama
Copy link
Contributor Author

I forgot I had not followed up on this! I ended up pulling this package into my project's monorepo to make some other customizations, but I have now created a PR with the above mentioned config change.

@szymmis szymmis linked a pull request Dec 30, 2023 that will close this issue
@griimick
Copy link

griimick commented Jan 2, 2024

Thanks for working on this @peterlama and @szymmis 🚀

@szymmis
Copy link
Owner

szymmis commented Jan 2, 2024

These changes together with #103 were published in v0.13.0! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants