@denis
Vite is a build tool that is designed for modern JavaScript development. It does not have built-in support for including HTML partials like some other build tools do. However, you can achieve this by using a JavaScript function to load and insert the partials into your HTML files.
One approach is to create a JavaScript function that fetches the HTML partials using the Fetch API or a similar method, and then inserts the partials into a designated DOM element on your page.
Here's an example of how you could accomplish this in a Vite project:
1 2 |
<!-- partial.html --> <div>This is a partial HTML file</div> |
1 2 3 4 5 6 7 |
// utils.js export async function loadHtmlPartial(url, targetElement) { const response = await fetch(url); const html = await response.text(); targetElement.innerHTML = html; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!-- index.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vite HTML Partials Example</title> </head> <body> <h1>Main Content</h1> <div id="partialContent"></div> <script type="module"> import { loadHtmlPartial } from './utils.js'; loadHtmlPartial('partial.html', document.getElementById('partialContent')); </script> </body> </html> |
When you run your Vite project, the partial HTML file will be included in the designated element on your page using the loadHtmlPartial function. You can use this approach to include multiple HTML partials in your Vite project as needed.