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