UNPKG

vite-plugin-devtools-json

Version:

Vite plugin for generating `com.chrome.devtools.json` on the fly in the devserver.

58 lines (55 loc) 1.67 kB
import fs from 'fs'; import path from 'path'; import { validate, v4 } from 'uuid'; const ENDPOINT = "/.well-known/appspecific/com.chrome.devtools.json"; const plugin = () => ({ name: "devtools-json", enforce: "post", configureServer(server) { const { config } = server; const { logger } = config; if (!config.env.DEV) { return; } const getOrCreateUUID = () => { let { cacheDir } = config; if (!path.isAbsolute(cacheDir)) { let { root } = config; if (!path.isAbsolute(root)) { root = path.resolve(process.cwd(), root); } cacheDir = path.resolve(root, cacheDir); } const uuidPath = path.resolve(cacheDir, "uuid.json"); if (fs.existsSync(uuidPath)) { const uuid2 = fs.readFileSync(uuidPath, { encoding: "utf-8" }); if (validate(uuid2)) { return uuid2; } } if (!fs.existsSync(cacheDir)) { fs.mkdirSync(cacheDir, { recursive: true }); } const uuid = v4(); fs.writeFileSync(uuidPath, uuid, { encoding: "utf-8" }); logger.info(`Generated UUID '${uuid}' for DevTools project settings.`); return uuid; }; server.middlewares.use(ENDPOINT, async (req, res) => { let { root } = config; if (!path.isAbsolute(root)) { root = path.resolve(process.cwd(), root); } const uuid = getOrCreateUUID(); const devtoolsJson = { workspace: { root, uuid } }; res.setHeader("Content-Type", "application/json"); res.end(JSON.stringify(devtoolsJson, null, 2)); }); } }); export { plugin as default };