import*as g from"esbuild";import{logger as c}from"../tools/notifiers/logger.js";import{reporter as f}from"../tools/notifiers/reporter.js";import{isBuildMode as E}from"../utils/envs/is-build-mode.js";import{exit as M}from"../utils/process/exit.js";import{telemetry as u}from"../../cli/telemetry/index.js";import{stopAllCompilers as x}from"./esbuild.js";import{getCodeframe as y}from"../utils/codeframes/codeframes.js";class N extends Error{errors;warnings;constructor({errors:r=[],warnings:e=[]}){super(),this.errors=r,this.warnings=e}}async function S({errors:n,warnings:r}){const e=await m(n,"error"),s=await m(r,"warning");r.forEach((t,o)=>{c.warn(s[o])}),n.forEach((t,o)=>{const i=t.location?.file||"";i&&i.includes("node_modules/")?(c.error(e[o]),u.sendCliErrorCaughtMessage({message:JSON.stringify(t)})):(c.contentError(e[o]),u.sendCliErrorCaughtMessage({message:JSON.stringify(t),scope:"content"}))}),n.length&&E()&&(await x(),M(1))}async function m(n,r){switch(process.env.PORTAL_LOG_FORMAT){case"JSON":return n.map(e=>JSON.stringify(e));default:return await g.formatMessages(n,{kind:r,color:!1,terminalWidth:0})}}function L(n,r){if(!r?.lineText)return{line:1,column:1,lines:n.split(`
`),hasMatched:!1};const e=n.split(`
`),{lineText:s}=r,t=i=>{const a=i.indexOf(s);return a===-1?null:a};if(r.line&&r.line<=e.length&&t(e[r.line-1]))return{line:r.line,column:r.column||1,lines:e,hasMatched:!0};const o=e.findIndex(i=>t(i)!==null);if(o>=0){const i=t(e[o]);return{line:o+1,column:(i??0)+1,lines:e,hasMatched:!0}}return{line:1,column:1,lines:e,hasMatched:!1}}function _(n,r,e,s){const{line:t,column:o,lines:i,hasMatched:a}=L(e,s),{frame:d,start:p}=y({start:{line:t,character:o},end:{line:t,character:o+(s?.lineText?.length||0)},lines:i},!0),h=`${n}::${r}::${t}`,l={message:n,sourceFileRelativePath:r,sourceFileLocation:p,codeframe:d,hasMatched:a};return f.getCompilationProblem(h)||f.reportCompilationError({type:"ERROR",severity:"ERROR",...l}),l}export{N as EsbuildError,S as esbuildLogger,_ as reportEsbuildError};
