var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
    if (typeof path === "string" && /^\.\.?\//.test(path)) {
        return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
            return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
        });
    }
    return path;
};
import path from "pathe";
import { getPackageManagerRunner } from "../../util/detect-package-manager.js";
import { withSkipPathValidation } from "../miniflare/paths.js";
import { spreadBuildProps, spreadDevProps, Website, } from "../website.js";
export async function Vite(id, props) {
    const runner = await getPackageManagerRunner();
    let dev = spreadDevProps(props, `${runner} vite dev`);
    let domain = typeof dev === "object" ? dev.domain : undefined;
    const command = typeof dev === "object" ? dev.command : dev;
    if (!domain) {
        let port;
        const args = command.split(" ");
        if (args.find((arg) => arg.startsWith("--port="))) {
            port = args.find((arg) => arg.startsWith("--port="))?.split("=")[1];
        }
        else if (args.includes("--port")) {
            const index = args.indexOf("--port");
            port = args[index + 1];
        }
        else {
            try {
                const config = await withSkipPathValidation(() => import(__rewriteRelativeImportExtension(path.resolve(props.cwd ?? process.cwd(), "vite.config.ts"))));
                port = config.default?.server?.port ?? 5173;
            }
            catch { }
        }
        if (port) {
            domain = `localhost:${port}`;
        }
    }
    return await Website(id, {
        spa: true,
        ...props,
        assets: typeof props.assets === "string"
            ? { directory: props.assets }
            : {
                ...(props.assets ?? {}),
                directory: props.assets?.directory ??
                    (props.entrypoint || props.script ? "dist/client" : "dist"),
            },
        build: spreadBuildProps(props, `${runner} vite build`),
        dev: domain
            ? typeof dev === "string"
                ? { command: dev, domain }
                : { ...dev, domain }
            : dev,
    });
}
//# sourceMappingURL=vite.js.map