UNPKG

@pinegrow/piny-vite

Version:

A Vite plugin that implements Piny integration in dev mode.

41 lines (38 loc) 1.04 kB
import path from 'path' import fs from 'fs' export default function PinyVite(options = {}) { // Default to true if injectScript isn't provided const { injectScript = true } = options; const scriptPath = path.resolve(new URL(import.meta.url).pathname, '../piny.phone.js'); return { name: 'piny-vite-inject-dev-script', apply: 'serve', configureServer(server) { if (injectScript) { server.middlewares.use((req, res, next) => { if (req.url === '/_piny_vite/piny.phone.js') { res.setHeader('Content-Type', 'application/javascript'); fs.createReadStream(scriptPath).pipe(res); } else { next(); } }); } }, transformIndexHtml(html) { if (injectScript) { return { html, tags: [ { tag: 'script', attrs: { src: '/_piny_vite/piny.phone.js' }, injectTo: 'body' } ] }; } return html; } }; }