one
Version:
One is a new React Framework that makes Vite serve both native and web.
49 lines (48 loc) • 1.82 kB
JavaScript
import { readFileSync } from "node:fs";
import path from "node:path";
import { resolvePath } from "@vxrn/resolve";
const DEVTOOLS_VIRTUAL_ID = "/@one/dev.js";
function createDevtoolsPlugin(options = {}) {
const {
includeUI = true
} = options;
return {
name: "one-devtools",
apply: "serve",
// only in dev
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url === DEVTOOLS_VIRTUAL_ID) {
try {
const devEntryPath = resolvePath("one/devtools/dev.mjs");
const devEntry = readFileSync(devEntryPath, "utf-8");
let code = devEntry.replace("import './devtools.mjs'", "").replace("import './source-inspector.mjs'", "");
if (includeUI) {
const devtoolsPath = resolvePath("one/devtools/devtools.mjs");
const sourceInspectorPath = resolvePath("one/devtools/source-inspector.mjs");
const devtools = readFileSync(devtoolsPath, "utf-8");
const sourceInspector = readFileSync(sourceInspectorPath, "utf-8");
const projectName = path.basename(process.cwd());
code = `window.__oneProjectRoot="${projectName}";
${code}
${devtools}
${sourceInspector}`;
}
res.setHeader("Content-Type", "application/javascript");
res.setHeader("Cache-Control", "no-store");
res.end(code);
return;
} catch (e) {
console.error("[one] Failed to load devtools script:", e);
}
res.setHeader("Content-Type", "application/javascript");
res.end("// devtools failed to load");
return;
}
next();
});
}
};
}
export { DEVTOOLS_VIRTUAL_ID, createDevtoolsPlugin };
//# sourceMappingURL=devtoolsPlugin.mjs.map