Last active
May 24, 2026 11:59
-
-
Save benhatsor/77f0267a9a0afe5a65ca8f4b76e34820 to your computer and use it in GitHub Desktop.
HTML Include Plugin for Vite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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