UNPKG

lazy-js-utils

Version:

A collection of lazy-loaded JavaScript utilities for efficient development

61 lines (59 loc) 1.48 kB
// src/vite/vitePluginCopyHtml.ts import fsp from "fs/promises"; import path from "path"; import process from "process"; async function vitePluginCopyHtml(template_url) { let content = await fsp.readFile( path.resolve(process.cwd(), template_url), "utf-8" ); return { name: "vite-plugin-copy-html", writeBundle(options, data) { const outDir = `${options.dir}/index.html`; const links = []; const scripts = []; for (const key in data) { if (key.endsWith(".css")) links.push(key); else if (key.endsWith(".js")) scripts.push(key); } if (links.length) { content = content.replace( /<\/head>/, () => `</head> ${links.map((link) => `<link rel="stylesheet" href="/${link}">`)}` ); } if (scripts.length) { content = content.replace( /<\/body>/, () => ` ${scripts.map( (script) => `<script src="/${script}" type="module"></script>` )} </body>` ); } fsp.writeFile(path.resolve(outDir), content, "utf-8"); } }; } // src/vite/vitePluginExport.ts function vitePluginExport(config) { return { name: "vite-plugin-export", transform(src, id) { if (id.endsWith(config)) { return { code: `export default ${JSON.stringify(src)}`, map: null }; } } }; } export { vitePluginCopyHtml, vitePluginExport };