import path from 'node:path';
import TEMPLATE_CLIENT_SCRIPT from '../template/client.js';
import { createDebugger } from '../util/create-debugger.js';

const CLIENT_SCRIPT_NAME = "vite-live-preview/client.ts";
const middlewareClientRoute = ({ base }) => {
  const debug = createDebugger("live-preview");
  const script = TEMPLATE_CLIENT_SCRIPT.replace(/(?<=const base *= *)'\/'/u, JSON.stringify(base));
  const length = Buffer.byteLength(script, "utf8");
  const route = path.posix.join(base, CLIENT_SCRIPT_NAME);
  return (req, res, next) => {
    if (req.url !== route)
      return next();
    res.setHeader("Content-Type", "text/javascript");
    res.setHeader("Content-Length", length);
    res.end(script);
    debug?.("served client script.");
  };
};

export { CLIENT_SCRIPT_NAME, middlewareClientRoute as default };
//# sourceMappingURL=client-route.js.map
