vite-plugin-html-injection
Version:
Vite plugin for injecting html, js, css code snippets into index.html
12 lines (11 loc) • 845 B
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("path"),d=require("fs");function p(s){let t;return{name:"html-injection",configResolved(r){t=r},transformIndexHtml(r){let e=r;for(let i=0;i<s.injections.length;i++){const o=s.injections[i],a=t.root,l=c.resolve(a,o.path);let n=d.readFileSync(l,"utf8");if(!(o.buildModes&&(o.buildModes==="dev"&&!(t!=null&&t.env.DEV)||o.buildModes==="prod"&&!(t!=null&&t.env.PROD))))switch(o.type==="js"?n=`<script>
${n}
<\/script>
`:o.type==="css"&&(n=`<style>
${n}
</style>
`),o.injectTo){case"head":e=e.replace("</head>",`${n}
</head>`);break;case"head-prepend":e=e.replace(/<head(.*)>/i,`$&
${n}`);break;case"body":e=e.replace("</body>",`${n}
</body>`);break;case"body-prepend":e=e.replace(/<body(.*)>/i,`$&
${n}`);break}}return e}}}exports.htmlInjectionPlugin=p;