Skip to content

Instantly share code, notes, and snippets.

@benhatsor
Last active May 24, 2026 11:59
Show Gist options
  • Select an option

  • Save benhatsor/77f0267a9a0afe5a65ca8f4b76e34820 to your computer and use it in GitHub Desktop.

Select an option

Save benhatsor/77f0267a9a0afe5a65ca8f4b76e34820 to your computer and use it in GitHub Desktop.
HTML Include Plugin for Vite
/*
* Tiny HTML include plugin for Vite.
* Usage: `<include src="./partials/header.html"></include>`
*/
import fs from 'fs'
import path from 'path'
function processIncludes(html, dir) {
return html.replaceAll(/<include src="(.+?)"><\/include>/g, (_match, src) => {
const filePath = path.resolve(dir, src)
const content = fs.readFileSync(filePath, 'utf-8')
return processIncludes(content, path.dirname(filePath))
})
}
export default {
name: 'html-include',
transformIndexHtml(html, ctx) {
return processIncludes(html, path.dirname(ctx.filename))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment