import{context as m}from"esbuild";import{mkdirSync as w,writeFileSync as b,existsSync as j}from"node:fs";import*as a from"node:path";import{RUNTIME_RESOURCES_DIR as M}from"../constants/common.js";import{TELEMETRY_ENABLED as Y}from"../../constants/index.js";import{fromCurrentDir as t}from"../utils/paths.js";import{NodeBrowserPlugin as y}from"./plugins/node-browser.js";import{PortalImport as l}from"./plugins/portal-import.js";import{ThemesResolver as u}from"./plugins/themes-resolver.js";import{AssetsResolver as E}from"./plugins/assets-resolver.js";import{EsbuildErrorCollector as _}from"./plugins/esbuild-compile-resolver.js";import{OpenapiDocsModuleReplacer as f}from"./plugins/openapi-docs-module-replacer/index.js";import{ServerPropsResolver as R}from"./plugins/server-props-resolver.js";import{StyledComponentsSSR as v}from"./plugins/styled-components-ssr.js";import{DependencyResolver as d}from"./plugins/dependency-resolver.js";import{YamlLoader as L}from"./plugins/yaml-loader.js";import{ApiRequestHandlersResolver as A}from"./plugins/api-request-handlers-resolver.js";import{MiddlewareResolver as N}from"./plugins/middleware-resolver.js";import{OnRebuild as D}from"./plugins/on-rebuild.js";import{getPublicEnvVariables as V}from"../../shared/env.js";import{PORTAL_VERSION as g}from"../version.js";import{AsyncApiPatch as O}from"./plugins/async-api-patch.js";const C={bundle:!0,format:"esm",chunkNames:"chunks/[name]-[hash]"};function P(e){const n=a.join(e,"tsconfig.json");return j(n)?n:void 0}const c=new Set;async function se(){await Promise.all([...c].map(e=>e.dispose())),c.clear()}async function ie(e,n,r="production",o){const p=V(),i={};Object.entries(p).map(([I,T])=>i[`process.env.${I}`]=JSON.stringify(T));const s=[...o?[D(o)]:[],y({path:"path-browserify",fs:"{}",tty:"tty-browserify",os:"os-browserify",http:"stream-http",https:"stream-http",readline:"{}",crypto:"{}",stream:"{}",zlib:"{}","https-proxy-agent":"{}"}),u(e,e.contentDir),E(e,e.contentDir),_(e,e.contentDir),R(e),d(),l(),L(),O(),v(),f(),...e.esbuildPlugins],h=[t(import.meta.url,"../../client/browser-entry.js"),t(import.meta.url,"../../client/user-tags-entry.js")],S=await m({...C,entryPoints:h,outdir:a.join(n,M),sourcemap:r!=="production"&&process.env.ENABLE_SOURCE_MAPS==="true",plugins:s,tsconfig:P(e.contentDir),mainFields:["browser","module","main"],metafile:r!=="production",minify:r==="production",splitting:!0,external:["constants","zlib","stream","https","vm","module","worker_threads","child_process","@swc/core"],inject:[process.env.INSPECT_MODE==="true"?t(import.meta.url,"../../client/inspect-mode-hooks.js"):"",t(import.meta.url,"./web-shim.js")].filter(Boolean),define:{...i,"process.env.NODE_ENV":`"${r}"`,"process.env.REDOCLY_LOCAL_DEV":`"${process.env.REDOCLY_LOCAL_DEV}"`,"process.env.SERVER_EDITOR_APP_URL":`"${process.env.SERVER_EDITOR_APP_URL}"`,"process.env.ENABLE_COMMENTS":`"${process.env.ENABLE_COMMENTS}"`,"process.env.REDOCLY_TELEMETRY":Y?'""':'"off"',"process.env.REDOCLY_PORTAL_VERSION":`"${g}"`,"process.env":`{"NODE_ENV": "${r}"}`,"process.platform":'"browser"',"process.browser":"true","module.hot":"false",global:"{}",...process.env.REDOCLY_PREFIX_PATHS&&{"process.env.REDOCLY_PREFIX_PATHS":`"${process.env.REDOCLY_PREFIX_PATHS}"`},...process.env.INSPECT_MODE==="true"&&{"process.env.INSPECT_MODE":`${process.env.INSPECT_MODE}`},...process.env.MAIN_API_URL&&{"process.env.MAIN_API_URL":`"${process.env.MAIN_API_URL}"`}},logLevel:"silent"});return c.add(S),S}async function pe(e,n,r="development",o){w(n,{recursive:!0}),b(a.join(n,"package.json"),JSON.stringify({name:"@redocly/portal/server-cache",type:"module"}));const p=[...o?[D(o)]:[],u(e,e.contentDir),E(e,e.contentDir),_(e,e.contentDir),R(e),A(e),N(e),d(),l(),L(),O(),v(),f(),...e.esbuildPlugins],i=[t(import.meta.url,"../../client/server-entry.js"),t(import.meta.url,"../../client/user-tags-entry.js"),t(import.meta.url,"../../client/server-props-entry.js"),t(import.meta.url,"../../client/api-request-handlers-entry.js"),t(import.meta.url,"../../client/middleware-entry.js")],s=await m({...C,entryPoints:i,outdir:n,plugins:p,sourcemap:r!=="production"&&process.env.ENABLE_SOURCE_MAPS==="true",minify:r==="production",tsconfig:P(e.contentDir),mainFields:["module","main"],splitting:!0,external:["react","react-router-dom","@dr.pogodin/react-helmet","@swc/core","pnpapi","canvas"],banner:{js:`import { createRequire as topLevelCreateRequire } from 'module';
 const require = topLevelCreateRequire(import.meta.url);`},platform:"node",define:{"process.env.NODE_ENV":`"${r}"`,"process.env.REDOCLY_LOCAL_DEV":`"${process.env.REDOCLY_LOCAL_DEV}"`,"module.hot":"false"},logLevel:"silent"});return c.add(s),s}async function ce(e,n,r="production",o){const p=[...o?[D(o)]:[],y({"node-fetch":"{}",webpack:"{}",swagger2openapi:"{}"}),u(e,e.contentDir),E(e,e.contentDir),_(e,e.contentDir),R(e),A(e),N(e),d(),l(),L(),O(),v(),f(),...e.esbuildPlugins],i=[{in:t(import.meta.url,"../node-bundle-entry.js"),out:"index"},{in:t(import.meta.url,"../../client/user-tags-entry.js"),out:"user-tags-entry"},{in:t(import.meta.url,"../plugins/api-functions/api-functions-handler-worker.js"),out:"api-functions-handler-worker"}],s=await m({...C,entryPoints:i,outExtension:{".js":".mjs"},outdir:n,platform:"node",plugins:p,tsconfig:P(e.contentDir),mainFields:["module","main"],metafile:r!=="production",minify:r==="production",splitting:!1,define:{"process.env.NODE_ENV":`"${r}"`,"process.env.REDOCLY_LOCAL_DEV":`"${process.env.REDOCLY_LOCAL_DEV}"`,"process.env.REDOCLY_STATIC_BUNDLE":"true","process.env.REDOCLY_PORTAL_VERSION":`"${g}"`,"module.hot":"false",...process.env.REDOCLY_PREFIX_PATHS&&{"process.env.REDOCLY_PREFIX_PATHS":`"${process.env.REDOCLY_PREFIX_PATHS}"`}},banner:{js:`import { createRequire as topLevelCreateRequire } from 'module';
 const require = topLevelCreateRequire(import.meta.url);`},logLevel:"silent"});return c.add(s),s}export{ie as createClientCompiler,ce as createNodeBundleCompiler,pe as createServerCompiler,se as stopAllCompilers};
