"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.clientInjectionsPlugin = void 0;
const constants_1 = require("../constants");
const utils_1 = require("../utils");
// ids in transform are normalized to unix style
const normalizedClientEntry = utils_1.normalizePath(constants_1.CLIENT_ENTRY);
/**
 * some values used by the client needs to be dynamically injected by the server
 * @server-only
 */
function clientInjectionsPlugin(config) {
    return {
        name: 'vite:client-inject',
        transform(code, id) {
            if (id === normalizedClientEntry) {
                let options = config.server.hmr;
                options = options && typeof options !== 'boolean' ? options : {};
                const host = options.host || null;
                const protocol = options.protocol || null;
                const timeout = options.timeout || 30000;
                const overlay = options.overlay !== false;
                let port = String(options.port || config.server.port);
                if (options.path) {
                    port = `${port}/${options.path}`;
                }
                return code
                    .replace(`__MODE__`, JSON.stringify(config.mode))
                    .replace(`__ROOT__`, JSON.stringify(config.root))
                    .replace(`__DEFINES__`, JSON.stringify(config.define || {}))
                    .replace(`__HMR_PROTOCOL__`, JSON.stringify(protocol))
                    .replace(`__HMR_HOSTNAME__`, JSON.stringify(host))
                    .replace(`__HMR_PORT__`, JSON.stringify(port))
                    .replace(`__HMR_TIMEOUT__`, JSON.stringify(timeout))
                    .replace(`__HMR_ENABLE_OVERLAY__`, JSON.stringify(overlay));
            }
        }
    };
}
exports.clientInjectionsPlugin = clientInjectionsPlugin;
//# sourceMappingURL=clientInjections.js.map