-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Serve index.html from custom host #341
Comments
I succcessfully setup Vite with a Symfony application but It would work with every backend. The goalWe want to use vite to serve assets, and the backend for the rest. So we will ignore vite ability to generate an HTML page. Steps to make the backend "vite friendly"If i'm in development mode I change assets path to point to the vite webserver. Replacing path to assets with the vite url <script src="http://localhost:3000/app.js" type="module" defer></script> Vite will complains about CORS so I have to edit the configuration adding // @ts-check
const prefresh = require('@prefresh/vite')
const cors = require('@koa/cors')
/**
* @type { import('vite').UserConfig }
*/
const config = {
jsx: 'preact', // Change this to adapt your situation
plugins: [prefresh()],
root: './assets', // You can change the root path as you wish
configureServer: function ({ app }) {
app.use(cors({ origin: '*' }))
}
}
module.exports = config But I won't have the hot reload at this point :(. <script type="module">
import "http://localhost:3000/vite/client"
window.process = { env: { NODE_ENV: "development" }}
</script> PHP SampleHere is a small sample to help bootstrap your configuration. /**
* $path contains the path of the assets, for instance /app.js
*/
function script($path) {
if ($this->isDev()) {
$prefix = "http://{$_SERVER['SERVER_NAME']}:3000/" // Vite
} else {
$prefix = "/assets/"; // Static path
}
$script = "<script src=\"{$prefix}{$name}\" type=\"module\" defer></script>";
if ($this->isDev()) {
// We need to add HMR ;)
$script = <<<HTML
<script type="module">
import "{$prefix}vite/client"
window.process = { env: { NODE_ENV: "development" }}
</script>
$script
HTML;
}
return $script;
} |
Hi, I'm using the same approach as @Grafikart for Django and this is working like a charm! Notice that in latest version of vite, the URL of the hmr has changed to
|
Sorry for the long delay here. Thank you very much @Grafikart for the detailed explanation. @mxmaxime Thanks too. Now, how are you handling the production build? How to find the hashed index.js and style.css? — The best solution would be to Rollup to export the filenames to a JSON file, right? I guess if Vite supports this natively it would be much cleaner. There are 3 areas we are hacking here which would be great experience if they "just work": What do you think? By the way, I am putting this all together here https://github.com/andrefelipe/vite-php-setup |
For the production build I use my own rollup configuration (it's not that hard to setup) since I'm not a huge fan of having a big configuration I don't understand and difficult to extend from vite. I keep vite for the dev, and a custom rollup setup for the build. Since I use my own rollup file I can put an extra logic that generates a JSON required for my backend framework (the logic could be extracted in a plugin).
It's a bit of boilerplate at the moment (it was the same nightmare with webpack anyway). @mxmaxime I fixed my code if someone finds it from a search ^^ |
Thanks for the feedback @Grafikart I will keep an eye on this and keep trying to come to nice setup. Hopefully Vite's core can help us in the future. |
@Grafikart Can you share your I need to generate a json with the I've found rollup-plugin-output-manifest But this plugins generate only a path for the |
@stephanedemotte It seem @Grafikart not using vite for building but directly rollup with a special config like here : https://github.com/Grafikart/Grafikart.fr/blob/master/rollup.config.js and here for the package.json It's a workaround but it could be cool if we can use vite in dev and build mode without plenty of The rollup-plugin-output-manifest plugin work well but vite is not using rollup for building css so that's why the plugin generate only the path for An enhancement could be to add a option in vite for generate a small manifest in outDir with all file generated in same way the Lines 454 to 489 in 1e375a4
My use-case is not so far from @Grafikart. I want to use vite inside a WordPress project and I would like to have a manifest or files without hash in names in generated files for a better handling in WordPress loading assets workflow. By the way thanks @Grafikart for your approach which works very well in dev mode ! |
fwiw: also mentioning the Vite dev server proxy (works for me) |
@Frulko Good news @underfin fix the issue with We could get js and css in the manifest.json soon, I use Twig in wordpress, so we can retrieve
|
Updated my setup code: https://github.com/andrefelipe/vite-php-setup With the manifest.json handled we have left:
Comments and ideas welcomed! Vite is too good for development, I look forward to use it everywhere. :) |
@andrefelipe thanks for sharing! This PR #918 is based on your work. |
Awesome! Thanks Vite team! With all recent features my example setup is very very clean now. Job is done! |
Is your feature request related to a problem? Please describe.
My final goal is to use Vite for traditional PHP based websites. Where HTML is rendered on PHP side and we'll be looking into a scenario where Vue components are used where necessary, mixed with regular HTML (not a SPA).
Describe the solution you'd like
For as far as I can go, the solution lies on allowing to customize Vite's index.html path, but not only customizing, but actually requesting it from an URL instead of reading from file.
For example, in vite.config have something like:
and also expect Vite to follow the paths, for example, by visiting http://localhost:3000/about it would request the HTML from http://mylocalsite.test/about — of course, it is of the server responsibility to output the <script type="module" src="/src/main.js"></script> within the body.
Describe alternatives you've considered
Tried working around with the proxy feature, but no success. For example, proxying "/" to my local server, and then proxying "/src" "/vite" and "/@modules" back to http://localhost:3000 which doesn't work.
Additional context
Here is the code of my trial run:
https://github.com/andrefelipe/vite-php-setup
Thank you very much! Would be fantastic to use Vite in different scenarios.
The text was updated successfully, but these errors were encountered: