UNPKG

vite-plugin-html-injection

Version:

Vite plugin for injecting html, js, css code snippets into index.html

48 lines (47 loc) 1.15 kB
import l from "path"; import c from "fs"; function h(n) { let t; return { name: "html-injection", configResolved(a) { t = a; }, transformIndexHtml(a) { let e = a; for (let i = 0; i < n.injections.length; i++) { const r = n.injections[i], s = t.root, d = l.resolve(s, r.path); let o = c.readFileSync(d, "utf8"); if (!(r.buildModes && (r.buildModes === "dev" && !(t != null && t.env.DEV) || r.buildModes === "prod" && !(t != null && t.env.PROD)))) switch (r.type === "js" ? o = `<script> ${o} <\/script> ` : r.type === "css" && (o = `<style> ${o} </style> `), r.injectTo) { case "head": e = e.replace("</head>", `${o} </head>`); break; case "head-prepend": e = e.replace(/<head(.*)>/i, `$& ${o}`); break; case "body": e = e.replace("</body>", `${o} </body>`); break; case "body-prepend": e = e.replace(/<body(.*)>/i, `$& ${o}`); break; } } return e; } }; } export { h as htmlInjectionPlugin };