Created
April 28, 2022 20:05
-
-
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
This file contains 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
/* | |
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