import{isMainThread as F}from"node:worker_threads";import{i as P,a as J,m as M}from"../node-features-Dcn4STxq.mjs";import{r as A}from"../register-DGMXNqQF.mjs";import{fileURLToPath as _,pathToFileURL as D}from"node:url";import{b,t as I}from"../index-BUWCPB1Z.mjs";import{i as T,a as v,r as $}from"../is-relative-path-pattern-DuKsZATx.mjs";import{p as E}from"../client-Cg7nS93t.mjs";import f from"node:path";import{parseTsconfig as L,getTsconfig as C,createFilesMatcher as W,createPathsMatcher as G}from"get-tsconfig";import k from"node:fs";import"node:module";import"esbuild";import"node:crypto";import"node:os";import"../temporary-directory-CwHp0_NW.mjs";import"node:net";import"../get-pipe-path-CmcG6VNd.mjs";const m={active:!0},H=async t=>{if(!t)throw new Error(`tsx must be loaded with --import instead of --loader
The --loader flag was deprecated in Node v20.6.0 and v18.19.0`);m.namespace=t.namespace,t.port&&(m.port=t.port,t.port.on("message",s=>{s==="deactivate"&&(m.active=!1,t.port.postMessage({type:"deactivated"}))}))},X=()=>"process.setSourceMapsEnabled(true);",d=new Map,q=async t=>{if(d.has(t))return d.get(t);if(!await k.promises.access(t).then(()=>!0,()=>!1)){d.set(t,void 0);return}const a=await k.promises.readFile(t,"utf8");try{const r=JSON.parse(a);return d.set(t,r),r}catch{throw new Error(`Error parsing: ${t}`)}},Q=async t=>{let s=new URL("package.json",t);for(;!s.pathname.endsWith("/node_modules/package.json");){const a=_(s),r=await q(a);if(r)return r;const c=s;if(s=new URL("../package.json",s),s.pathname===c.pathname)break}},K=async t=>(await Q(t))?.type??"commonjs",p=process.env.TSX_TSCONFIG_PATH?{path:f.resolve(process.env.TSX_TSCONFIG_PATH),config:L(process.env.TSX_TSCONFIG_PATH)}:C(),z=p&&W(p),O=p&&G(p),B=p?.config.compilerOptions?.allowJs??!1,R="file://",u=/\.([cm]?ts|[tj]sx)($|\?)/,x=/\.json(?:$|\?)/,V=t=>{const s=f.extname(t);if(s===".json")return"json";if(s===".mjs"||s===".mts")return"module";if(s===".cjs"||s===".cts")return"commonjs"},Y=t=>{const s=V(t);if(s)return s;if(u.test(t))return K(t)},h="tsx-namespace=",g=t=>{const s=t.indexOf(h);if(s===-1)return;const a=t[s-1];if(a!=="?"&&a!=="&")return;const r=s+h.length,c=t.indexOf("&",r);return c===-1?t.slice(r):t.slice(r,c)},l=P(J)?"importAttributes":"importAssertions",Z=async(t,s,a)=>{if(!m.active||m.namespace&&m.namespace!==g(t))return a(t,s);if(m.port){const e=new URL(t);e.searchParams.delete("tsx-namespace"),m.port.postMessage({type:"load",url:e.toString()})}E.send&&E.send({type:"dependency",path:t}),x.test(t)&&(s[l]||(s[l]={}),s[l].type="json");const r=await a(t,s);if(!r.source)return r;const c=t.startsWith("file://")?_(t):t,o=r.source.toString();if(r.format==="json"||u.test(t)){const e=await b(o,c,{tsconfigRaw:z?.(c)});return{format:"module",source:T(e)}}if(r.format==="module"){const e=I(c,o);e&&(r.source=T(e))}return r},N=/\/(?:$|\?)/,y=async(t,s,a)=>{const r=await t(s,a);return!r.format&&r.url.startsWith(R)&&(r.format=await Y(r.url)),r},tt=[".js",".json",".ts",".tsx",".jsx"],w=async(t,s,a)=>{const[r,c]=t.split("?");let o;for(const e of tt)try{return await y(a,r+e+(c?`?${c}`:""),s)}catch(n){if(o===void 0&&n instanceof Error){const{message:i}=n;n.message=n.message.replace(`${e}'`,"'"),n.stack=n.stack.replace(i,n.message),o=n}}throw o},U=async(t,s,a)=>{const r=N.test(t),c=r?"index":"/index",[o,e]=t.split("?");try{return await w(o+c+(e?`?${e}`:""),s,a)}catch(n){if(!r)try{return await w(t,s,a)}catch{}const i=n,{message:S}=i;throw i.message=i.message.replace(`${c.replace("/",f.sep)}'`,"'"),i.stack=i.stack.replace(S,i.message),i}},j=async(t,s,a,r)=>{if(!m.active)return a(t,s);if(t.startsWith(R)||f.isAbsolute(t)||v.test(t)){let o=g(t);if(s.parentURL){const e=g(s.parentURL);e&&!o&&(o=e,t+=`${t.includes("?")?"&":"?"}${h}${e}`)}if(m.namespace&&m.namespace!==o)return a(t,s);if(N.test(t))return await U(t,s,a)}else if(O&&!s.parentURL?.includes("/node_modules/")){const o=O(t);for(const e of o)try{return await j(D(e).toString(),s,a)}catch{}}if(u.test(s.parentURL)||B){const o=$(t);if(o)for(const e of o)try{return await y(a,e,s)}catch(n){const{code:i}=n;if(i!=="ERR_MODULE_NOT_FOUND"&&i!=="ERR_PACKAGE_PATH_NOT_EXPORTED")throw n}}try{return await y(a,t,s)}catch(o){if(o instanceof Error&&!r){const{code:e}=o;if(e==="ERR_UNSUPPORTED_DIR_IMPORT")try{return await U(t,s,a)}catch(n){if(n.code!=="ERR_PACKAGE_IMPORT_NOT_DEFINED")throw n}if(e==="ERR_MODULE_NOT_FOUND")try{return await w(t,s,a)}catch{}}throw o}};P(M)&&F&&A();export{X as globalPreload,H as initialize,Z as load,j as resolve};
