-
-
Notifications
You must be signed in to change notification settings - Fork 65
example of setup with vite istead of vuecli? #93
Comments
I think the only change would be to the regex for whatever the export of Vite shows on the console when the web app is ready. Also, be sure to replace the contents of https://github.com/EEParker/aspnetcore-vueclimiddleware/blob/master/samples/Vue3/Startup.cs#L67 endpoints.MapToVueCliProxy(
"{*path}",
new SpaOptions { SourcePath = "ClientApp" },
npmScript: (System.Diagnostics.Debugger.IsAttached) ? "serve" : null, // make sure `serve` is the correct command
regex: "Compiled successfully", // change this to vite output
forceKill: true
); |
At the moment without these two tickets I'm not sure how far you can get with this.
The HMR requires websockets to be proxied. You can use something like AspNetCore.Proxy to forward the websockets but there is no way to tell Vite the port to actually run the HMR (runtimePort) and what to use in the HTML (hmr port - need to be the proxy server port). An alterative approach however is to use Vite's proxy feature and skip this plugin and continue to use AddSpaStaticFiles/UseSpaStaticFiles for production builds. You would need to start the SPA project separately and use those endpoints instead. This approach does require the proxy url to be different than the vite project. Run this command or use alternative methods for generating a SSL certificate. I used the following msbuild target. <Target Name="GetHttpsForLocal" BeforeTargets="Build">
<Exec WorkingDirectory="$(SpaRoot)" Command="dotnet dev-certs https -ep ./localhost-key.pfx -p dev-passphrase" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
<Error Condition="'$(ErrorCode)' != '0'" Text="Failed to get localhost key" />
</Target> vite.config.ts import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import { readFileSync } from 'fs'
const key = readFileSync('localhost-key.pfx');
export default defineConfig({
plugins: [vue()],
server: {
https: {
pfx: key,
passphrase: 'dev-passphrase' // required for some reason -- maybe easier way?
},
proxy: {
'^/api/.*': {
target: 'https://localhost:5001',
rewrite: (path) => path.replace(/^\/api/, ''),
changeOrigin: true,
secure: false
},
'^/swagger': {
target: 'https://localhost:5001',
changeOrigin: true,
secure: false
}
}
}
}) |
So does this mean that all traffic goes through vite first, which forwards specific routes to dotnet while serving the vue routes directly? Are swagger supposed to go to dotnet? |
This tool is meant for development only and to facility HMR and other features. Production should serve the static files directly using standard AspNet static file methods. |
I gave it a try and eventually hit a wall.
When using
To accommodate I guess one of two things need to happen:
|
I've got this mostly working now (albeit with Vue 2) due to the following 2 fixes in Vite:
In vite.config.js:
package.json:
Program.cs (or Startup.cs):
However, quite often on the first run, a few of the requests from vite for certain components are failing. The requests make it to the vite proxy, but are failing with:
A refresh of the page and all works fine. I can't work out what the issue is. |
I have it working just fine and I'm using vite3 with vuejs3 in my .NET 6 project. What I have: vite.config.ts - I had to set server.host to true because of some VueCliMiddelware was pushing for 127.0.0.1 while vite was expecting localhost and I needed to set server.hmr.protocol to 'ws' for websocket:
Program.cs (or Startup.cs) - like @yitzolsberg said, vite3 prints out ready in [x]ms so you gotta make VueCliMiddleware look for that:
Hope this helps! |
I think it would be great if there was an example of how to setup with vite instead of vue-cli. it seems that the middleware is general enough that this should be possible.
The text was updated successfully, but these errors were encountered: