Backend Integration
If you want to serve the HTML using a traditional backend (e.g. Rails, Laravel) but use Vite for serving assets, here’s what you can do:
In your Vite config, configure the entry and enable build manifest:
// vite.config.js
export default {
build: {
manifest: true,
rollupOptions: {
// overwrite default .html entry
input: '/path/to/main.js'
}
}
}
Also remember to add the dynamic import polyfill to your entry, since it will no longer be auto-injected:
// add the beginning of your app entry
import 'vite/dynamic-import-polyfill'
For development, inject the following in your server’s HTML template (substitute
http://localhost:3000
with the local URL Vite is running at):<!-- if development -->
<script type="module" src="http://localhost:3000/@vite/client"></script>
<script type="module" src="http://localhost:3000/main.js"></script>
Also make sure the server is configured to serve static assets in the Vite working directory, otherwise assets such as images won’t be loaded properly.
For production: after running
vite build
, amanifest.json
file will be generated alongside other asset files. You can use this file to render links with hashed filenames (note: the syntax here is for explanation only, substitute with your server templating language):<!-- if production -->
<link rel="stylesheet" href="/assets/{{ manifest['index.css'].file }}" />
<script type="module" src="/assets/{{ manifest['index.js'].file }}"></script>