I deployed a project bundled by Vite to CDN recently. It's surprising that there isn't a clear solution available for such a common requirement. It's even more surprising that there is little discussion around this topic on the web, I only found a single related Github discussion which created about a year ago.
I didn't find a solution after searching via Google, so I visited the official website with the hope of discovering some magic configurations that would make CDN deployment possible. Fortunately, I stumbled upon the experimental renderBuiltUrl configuration in the advanced base options section, which appeared to support CDN usage based on the sample code provided.
According to the sample code, the simplified final configuration in the project is as follows, I added additional comments for better understanding. The real configuration is more complicated because it depends on a package which is based on web worker architecture, some additional resources must be copied and hashed manually.
Do a full function check if you want to upgrade Vite. My colleague upgrade Vite from 4.2.1 to 4.3.4 recently, caused dynamic import and manually chunk not work as expected!