Skip to content

Instantly share code, notes, and snippets.

@drodsou
Created April 28, 2022 20:05
Show Gist options
  • Save drodsou/ad35512de9953936f248495b567c3589 to your computer and use it in GitHub Desktop.
Save drodsou/ad35512de9953936f248495b567c3589 to your computer and use it in GitHub Desktop.
Vite plugin to use index.md, or other custom extension, as entry point instead of index.html
/*
Vite hardcodes .html as entry points, but you can trick it to use custom extensions, eg .md, this way:
(thanks to marko-vite plugin source code for hints)
*/
import fs from "fs";
export default function myPlugin() {
return {
name: 'myplugin',
enforce: 'pre',
async resolveId(importee, importer, importOpts) {
if (importee.endsWith('.html')) {
return importee; // tell Vite index.html exists when it looks for it
}
},
async load(id) {
if (id.endsWith('.html')) {
// read .md contents instead
let content = await fs.promises.readFile(id.replace('.html','.md'), "utf-8")
// do your custom trasnformation if needed, eg md => html
content += '<!-- processed -->'
return content;
}
}
async transform(src, ctx) {
// this is if you need to transform custom 'import' as well
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment