import { Credential } from "./types"; import { livereloadPlugin } from "./livereload"; import type { IsomorphicRollupFs } from "../fs-iface"; /** * Creates dev server plugins for Rollup with live reload and optional proxy support. * Returns an array of [devServer, livereload] plugins, or undefined if no watch paths are provided. * @param config Configuration object for the dev server * @param config.watchPaths Array of directories to serve and watch for live reload * @param config.proxyPaths Array of paths to proxy to the ServiceNow instance * @param config.credential ServiceNow authentication credentials, or undefined * @param config.httpProxy Optional HTTP proxy server URL for tunnelling upstream requests (e.g. `http://proxy.corp:8080`) * @returns Array of Rollup plugins [devServer, livereload], or undefined if watchPaths is empty * * Note: auth headers are fetched once via `credential.getHeaders()` at call time (before * the dev server starts). They are reused for the lifetime of the session. If the credential * token expires during a long dev session, proxied requests will silently fail with auth * errors from the instance. Restart the dev server to refresh credentials. */ export async function devServerPlugins(config: { watchPaths: string[]; proxyPaths: string[]; credential: Credential | undefined; httpProxy?: string; fs?: IsomorphicRollupFs; }) { const { credential, watchPaths, proxyPaths, httpProxy, fs } = config; if (watchPaths.length === 0) { console.warn( "No watch paths provided for dev server. Please provide at least one path to watch for changes.", ); return; } const proxy: Array<{ from: string; to: string; opts: { replyOptions: { rewriteRequestHeaders: ( _req: Request, headers: Record, ) => Record; }; websocket?: boolean; }; }> = []; if ( credential && typeof credential.getUrl === "function" && typeof credential.getHeaders === "function" ) { const authHeaders = await credential.getHeaders(); const proxyTarget = credential.getUrl().href.replace(/\/$/, ""); const rewriteRequestHeaders = ( _req: Request, headers: Record, ) => ({ ...headers, ...authHeaders, }); proxy.push( ...proxyPaths.map((proxyPath) => ({ from: proxyPath, to: `${proxyTarget}${proxyPath}`, opts: { replyOptions: { rewriteRequestHeaders, }, ...(proxyPath === "/amb" ? { websocket: true } : {}), }, })), ); } else if (credential) { console.warn( "credential is invalid. It must have getUrl and getHeaders methods. Dev server will start without proxying to ServiceNow instance.", ); } else if (proxyPaths.length > 0) { console.warn( "credential not provided. Dev server will start without proxying to ServiceNow instance.", ); } let devServer: any; try { const imported = await import("../dev-server"); devServer = imported.default || imported; } catch (error: any) { throw new Error( `dev-server failed to load. Please ensure all optional dev-server dependencies (fastify, @fastify/static, @fastify/http-proxy) are installed.${error.message ? ` Original error: ${error.message}` : ""}`, ); } let createLivereloadServer: any; try { const imported: any = await import("livereload"); createLivereloadServer = imported.createServer ?? imported.default?.createServer; } catch (error: any) { throw new Error( `livereload is not installed or failed to load. Please install it to enable the dev server.${error.message ? ` Original error: ${error.message}` : ""}`, ); } return [ devServer({ dirs: watchPaths, port: 3000, proxy, ...(httpProxy ? { httpProxy } : {}), }), await livereloadPlugin(createLivereloadServer, { watch: watchPaths, fs, }), ]; }