vite-plugin-devtools-json
Version:
Vite plugin for generating `com.chrome.devtools.json` on the fly in the devserver.
62 lines (57 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var fs = require('fs');
var path = require('path');
var uuid = require('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 (uuid.validate(uuid2)) {
return uuid2;
}
}
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, { recursive: true });
}
const uuid$1 = uuid.v4();
fs.writeFileSync(uuidPath, uuid$1, { encoding: "utf-8" });
logger.info(`Generated UUID '${uuid$1}' for DevTools project settings.`);
return uuid$1;
};
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));
});
}
});
exports.default = plugin;