Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.2 KB
Raw Blame
import { existsSync } from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";

import { packInstallDir } from "./optionalPacks.ts";

type PlaywrightCore = typeof import("playwright-core");

/** Resolve the optional browser pack explicitly: native ESM ignores NODE_PATH. */
export async function loadPlaywrightCore(): Promise<PlaywrightCore> {
  const packRoot = packInstallDir("browser-runtime");
  const member = path.join(packRoot, "node_modules", "playwright-core", "package.json");
  if (existsSync(member)) {
    // Installed packs take precedence over the bundled dependency, as in Electron.
    return createRequire(path.join(packRoot, "package.json"))("playwright-core") as PlaywrightCore;
  }
  try {
    return await import("playwright-core");
  } catch (error) {
    const missing = error as NodeJS.ErrnoException;
    if (
      missing.message?.includes("Cannot find package 'playwright-core'") &&
      (missing.code === "ERR_MODULE_NOT_FOUND" ||
        missing.message.startsWith("Failed to load external module playwright-core:"))
    ) {
      throw new Error(
        "The browser-runtime optional pack is missing. Install it to use browser features."
      );
    }
    throw error;
  }
}