@pinegrow/piny-vite
Version:
A Vite plugin that implements Piny integration in dev mode.
42 lines (39 loc) • 1.08 kB
JavaScript
import path from 'path'
import fs from 'fs'
import { fileURLToPath } from 'url'
export default function PinyVite(options = {}) {
// Default to true if injectScript isn't provided
const { injectScript = true } = options;
const scriptPath = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '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;
}
};
}