{"version":3,"sources":["../../src/plugin.ts"],"sourcesContent":["import type { Plugin } from \"vite\";\nimport {\n  startServer,\n  type CloseWebSocketServer,\n  type ServerOptions,\n} from \"@mocky-balboa/server\";\nimport mockyBalboaMiddleware from \"@mocky-balboa/server/middleware\";\nimport { logger } from \"./logger.js\";\n\n/**\n * Options for the Mocky Balboa Vite plugin.\n */\nexport interface MockyBalboaVitePluginOptions {\n  /**\n   * Controls whether the plugin is enabled.\n   *\n   * @default true\n   */\n  enabled?: boolean;\n  /**\n   * WebSocket server options\n   */\n  serverOptions?: ServerOptions;\n  /**\n   * Allows you disable overriding the Vite server configuration. Useful if you are\n   * needing to go against the behaviour of running as middleware in build mode,\n   * and running as a standalone server in dev mode.\n   *\n   * @default false\n   */\n  disableViteServerConfigOverride?: boolean;\n}\n\n/**\n * Creates a Vite plugin for dev servers to be used with Mocky Balboa.\n */\nconst mockyBalboaVitePlugin = ({\n  serverOptions,\n  enabled = true,\n  disableViteServerConfigOverride = false,\n}: MockyBalboaVitePluginOptions = {}): Plugin => ({\n  name: \"@mocky-balboa/vite\",\n  config: (initialConfig, configEnv) => {\n    if (!enabled || disableViteServerConfigOverride) {\n      logger.info(\"Skipping plugin configuration\");\n      return;\n    }\n\n    logger.info(\n      `Configuring Mocky Balboa Vite plugin for ${configEnv.command}`,\n    );\n    initialConfig.server = {\n      ...initialConfig.server,\n      middlewareMode: configEnv.command === \"build\",\n    };\n  },\n  /**\n   * Starts Mocky Balboa WebSocket server and registers server middleware\n   */\n  configureServer: (server) => {\n    if (!enabled) {\n      logger.info(\"Plugin disabled\");\n      return;\n    }\n\n    let closeWebSocketServer: CloseWebSocketServer | undefined = undefined;\n    server.httpServer?.on(\"listening\", async () => {\n      if (closeWebSocketServer) {\n        await closeWebSocketServer();\n      }\n\n      logger.info(\"Starting Mocky Balboa WebSocket server\");\n      closeWebSocketServer = await startServer(serverOptions);\n    });\n\n    server.httpServer?.on(\"close\", async () => {\n      if (closeWebSocketServer) {\n        await closeWebSocketServer();\n      }\n\n      closeWebSocketServer = undefined;\n    });\n\n    // Register the Mocky Balboa server middleware\n    server.middlewares.use(mockyBalboaMiddleware());\n  },\n});\n\nexport default mockyBalboaVitePlugin;\n"],"names":["mockyBalboaVitePlugin","serverOptions","enabled","disableViteServerConfigOverride","name","config","initialConfig","configEnv","logger","info","command","server","middlewareMode","configureServer","closeWebSocketServer","undefined","httpServer","on","startServer","middlewares","use","mockyBalboaMiddleware"],"mappings":";;;;+BAwFA;;;eAAA;;;wBAnFO;mEAC2B;wBACX;;;;;;AA0BvB;;CAEC,GACD,MAAMA,wBAAwB,CAAC,EAC7BC,aAAa,EACbC,UAAU,IAAI,EACdC,kCAAkC,KAAK,EACV,GAAG,CAAC,CAAC,GAAc,CAAA;QAChDC,MAAM;QACNC,QAAQ,CAACC,eAAeC;YACtB,IAAI,CAACL,WAAWC,iCAAiC;gBAC/CK,cAAM,CAACC,IAAI,CAAC;gBACZ;YACF;YAEAD,cAAM,CAACC,IAAI,CACT,CAAC,yCAAyC,EAAEF,UAAUG,OAAO,EAAE;YAEjEJ,cAAcK,MAAM,GAAG;gBACrB,GAAGL,cAAcK,MAAM;gBACvBC,gBAAgBL,UAAUG,OAAO,KAAK;YACxC;QACF;QACA;;GAEC,GACDG,iBAAiB,CAACF;YAChB,IAAI,CAACT,SAAS;gBACZM,cAAM,CAACC,IAAI,CAAC;gBACZ;YACF;YAEA,IAAIK,uBAAyDC;YAC7DJ,OAAOK,UAAU,EAAEC,GAAG,aAAa;gBACjC,IAAIH,sBAAsB;oBACxB,MAAMA;gBACR;gBAEAN,cAAM,CAACC,IAAI,CAAC;gBACZK,uBAAuB,MAAMI,IAAAA,mBAAW,EAACjB;YAC3C;YAEAU,OAAOK,UAAU,EAAEC,GAAG,SAAS;gBAC7B,IAAIH,sBAAsB;oBACxB,MAAMA;gBACR;gBAEAA,uBAAuBC;YACzB;YAEA,8CAA8C;YAC9CJ,OAAOQ,WAAW,CAACC,GAAG,CAACC,IAAAA,mBAAqB;QAC9C;IACF,CAAA;MAEA,WAAerB"}