ef603303962e2307f895c6140041d41eabe6590f2bcc36a02c4d1eaadac134fea792ba68d03367895490d4c2e4a6366c21f7d14de7e75978252b66d9c6678a 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  1. var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
  2. get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
  3. }) : x)(function(x) {
  4. if (typeof require !== "undefined") return require.apply(this, arguments);
  5. throw Error('Dynamic require of "' + x + '" is not supported');
  6. });
  7. // node_modules/.pnpm/tsup@8.3.5_jiti@2.3.3_typescript@5.6.3/node_modules/tsup/assets/esm_shims.js
  8. import { fileURLToPath } from "url";
  9. import path from "path";
  10. var getFilename = () => fileURLToPath(import.meta.url);
  11. var getDirname = () => path.dirname(getFilename());
  12. var __dirname = /* @__PURE__ */ getDirname();
  13. // src/esbuild/index.ts
  14. import fs2 from "fs";
  15. import path3 from "path";
  16. // src/esbuild/utils.ts
  17. import { Buffer as Buffer2 } from "buffer";
  18. import fs from "fs";
  19. import path2 from "path";
  20. // node_modules/.pnpm/@jridgewell+sourcemap-codec@1.5.0/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
  21. var comma = ",".charCodeAt(0);
  22. var semicolon = ";".charCodeAt(0);
  23. var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  24. var intToChar = new Uint8Array(64);
  25. var charToInt = new Uint8Array(128);
  26. for (let i = 0; i < chars.length; i++) {
  27. const c = chars.charCodeAt(i);
  28. intToChar[i] = c;
  29. charToInt[c] = i;
  30. }
  31. function decodeInteger(reader, relative) {
  32. let value = 0;
  33. let shift = 0;
  34. let integer = 0;
  35. do {
  36. const c = reader.next();
  37. integer = charToInt[c];
  38. value |= (integer & 31) << shift;
  39. shift += 5;
  40. } while (integer & 32);
  41. const shouldNegate = value & 1;
  42. value >>>= 1;
  43. if (shouldNegate) {
  44. value = -2147483648 | -value;
  45. }
  46. return relative + value;
  47. }
  48. function encodeInteger(builder, num, relative) {
  49. let delta = num - relative;
  50. delta = delta < 0 ? -delta << 1 | 1 : delta << 1;
  51. do {
  52. let clamped = delta & 31;
  53. delta >>>= 5;
  54. if (delta > 0)
  55. clamped |= 32;
  56. builder.write(intToChar[clamped]);
  57. } while (delta > 0);
  58. return num;
  59. }
  60. function hasMoreVlq(reader, max) {
  61. if (reader.pos >= max)
  62. return false;
  63. return reader.peek() !== comma;
  64. }
  65. var bufLength = 1024 * 16;
  66. var td = typeof TextDecoder !== "undefined" ? /* @__PURE__ */ new TextDecoder() : typeof Buffer !== "undefined" ? {
  67. decode(buf) {
  68. const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  69. return out.toString();
  70. }
  71. } : {
  72. decode(buf) {
  73. let out = "";
  74. for (let i = 0; i < buf.length; i++) {
  75. out += String.fromCharCode(buf[i]);
  76. }
  77. return out;
  78. }
  79. };
  80. var StringWriter = class {
  81. constructor() {
  82. this.pos = 0;
  83. this.out = "";
  84. this.buffer = new Uint8Array(bufLength);
  85. }
  86. write(v) {
  87. const { buffer } = this;
  88. buffer[this.pos++] = v;
  89. if (this.pos === bufLength) {
  90. this.out += td.decode(buffer);
  91. this.pos = 0;
  92. }
  93. }
  94. flush() {
  95. const { buffer, out, pos } = this;
  96. return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
  97. }
  98. };
  99. var StringReader = class {
  100. constructor(buffer) {
  101. this.pos = 0;
  102. this.buffer = buffer;
  103. }
  104. next() {
  105. return this.buffer.charCodeAt(this.pos++);
  106. }
  107. peek() {
  108. return this.buffer.charCodeAt(this.pos);
  109. }
  110. indexOf(char) {
  111. const { buffer, pos } = this;
  112. const idx = buffer.indexOf(char, pos);
  113. return idx === -1 ? buffer.length : idx;
  114. }
  115. };
  116. function decode(mappings) {
  117. const { length } = mappings;
  118. const reader = new StringReader(mappings);
  119. const decoded = [];
  120. let genColumn = 0;
  121. let sourcesIndex = 0;
  122. let sourceLine = 0;
  123. let sourceColumn = 0;
  124. let namesIndex = 0;
  125. do {
  126. const semi = reader.indexOf(";");
  127. const line = [];
  128. let sorted = true;
  129. let lastCol = 0;
  130. genColumn = 0;
  131. while (reader.pos < semi) {
  132. let seg;
  133. genColumn = decodeInteger(reader, genColumn);
  134. if (genColumn < lastCol)
  135. sorted = false;
  136. lastCol = genColumn;
  137. if (hasMoreVlq(reader, semi)) {
  138. sourcesIndex = decodeInteger(reader, sourcesIndex);
  139. sourceLine = decodeInteger(reader, sourceLine);
  140. sourceColumn = decodeInteger(reader, sourceColumn);
  141. if (hasMoreVlq(reader, semi)) {
  142. namesIndex = decodeInteger(reader, namesIndex);
  143. seg = [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex];
  144. } else {
  145. seg = [genColumn, sourcesIndex, sourceLine, sourceColumn];
  146. }
  147. } else {
  148. seg = [genColumn];
  149. }
  150. line.push(seg);
  151. reader.pos++;
  152. }
  153. if (!sorted)
  154. sort(line);
  155. decoded.push(line);
  156. reader.pos = semi + 1;
  157. } while (reader.pos <= length);
  158. return decoded;
  159. }
  160. function sort(line) {
  161. line.sort(sortComparator);
  162. }
  163. function sortComparator(a, b) {
  164. return a[0] - b[0];
  165. }
  166. function encode(decoded) {
  167. const writer = new StringWriter();
  168. let sourcesIndex = 0;
  169. let sourceLine = 0;
  170. let sourceColumn = 0;
  171. let namesIndex = 0;
  172. for (let i = 0; i < decoded.length; i++) {
  173. const line = decoded[i];
  174. if (i > 0)
  175. writer.write(semicolon);
  176. if (line.length === 0)
  177. continue;
  178. let genColumn = 0;
  179. for (let j = 0; j < line.length; j++) {
  180. const segment = line[j];
  181. if (j > 0)
  182. writer.write(comma);
  183. genColumn = encodeInteger(writer, segment[0], genColumn);
  184. if (segment.length === 1)
  185. continue;
  186. sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
  187. sourceLine = encodeInteger(writer, segment[2], sourceLine);
  188. sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
  189. if (segment.length === 4)
  190. continue;
  191. namesIndex = encodeInteger(writer, segment[4], namesIndex);
  192. }
  193. }
  194. return writer.flush();
  195. }
  196. // node_modules/.pnpm/@jridgewell+resolve-uri@3.1.2/node_modules/@jridgewell/resolve-uri/dist/resolve-uri.mjs
  197. var schemeRegex = /^[\w+.-]+:\/\//;
  198. var urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^#]*)?(#.*)?/;
  199. var fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i;
  200. function isAbsoluteUrl(input) {
  201. return schemeRegex.test(input);
  202. }
  203. function isSchemeRelativeUrl(input) {
  204. return input.startsWith("//");
  205. }
  206. function isAbsolutePath(input) {
  207. return input.startsWith("/");
  208. }
  209. function isFileUrl(input) {
  210. return input.startsWith("file:");
  211. }
  212. function isRelative(input) {
  213. return /^[.?#]/.test(input);
  214. }
  215. function parseAbsoluteUrl(input) {
  216. const match = urlRegex.exec(input);
  217. return makeUrl(match[1], match[2] || "", match[3], match[4] || "", match[5] || "/", match[6] || "", match[7] || "");
  218. }
  219. function parseFileUrl(input) {
  220. const match = fileRegex.exec(input);
  221. const path6 = match[2];
  222. return makeUrl("file:", "", match[1] || "", "", isAbsolutePath(path6) ? path6 : "/" + path6, match[3] || "", match[4] || "");
  223. }
  224. function makeUrl(scheme, user, host, port, path6, query, hash) {
  225. return {
  226. scheme,
  227. user,
  228. host,
  229. port,
  230. path: path6,
  231. query,
  232. hash,
  233. type: 7
  234. };
  235. }
  236. function parseUrl(input) {
  237. if (isSchemeRelativeUrl(input)) {
  238. const url2 = parseAbsoluteUrl("http:" + input);
  239. url2.scheme = "";
  240. url2.type = 6;
  241. return url2;
  242. }
  243. if (isAbsolutePath(input)) {
  244. const url2 = parseAbsoluteUrl("http://foo.com" + input);
  245. url2.scheme = "";
  246. url2.host = "";
  247. url2.type = 5;
  248. return url2;
  249. }
  250. if (isFileUrl(input))
  251. return parseFileUrl(input);
  252. if (isAbsoluteUrl(input))
  253. return parseAbsoluteUrl(input);
  254. const url = parseAbsoluteUrl("http://foo.com/" + input);
  255. url.scheme = "";
  256. url.host = "";
  257. url.type = input ? input.startsWith("?") ? 3 : input.startsWith("#") ? 2 : 4 : 1;
  258. return url;
  259. }
  260. function stripPathFilename(path6) {
  261. if (path6.endsWith("/.."))
  262. return path6;
  263. const index = path6.lastIndexOf("/");
  264. return path6.slice(0, index + 1);
  265. }
  266. function mergePaths(url, base) {
  267. normalizePath(base, base.type);
  268. if (url.path === "/") {
  269. url.path = base.path;
  270. } else {
  271. url.path = stripPathFilename(base.path) + url.path;
  272. }
  273. }
  274. function normalizePath(url, type) {
  275. const rel = type <= 4;
  276. const pieces = url.path.split("/");
  277. let pointer = 1;
  278. let positive = 0;
  279. let addTrailingSlash = false;
  280. for (let i = 1; i < pieces.length; i++) {
  281. const piece = pieces[i];
  282. if (!piece) {
  283. addTrailingSlash = true;
  284. continue;
  285. }
  286. addTrailingSlash = false;
  287. if (piece === ".")
  288. continue;
  289. if (piece === "..") {
  290. if (positive) {
  291. addTrailingSlash = true;
  292. positive--;
  293. pointer--;
  294. } else if (rel) {
  295. pieces[pointer++] = piece;
  296. }
  297. continue;
  298. }
  299. pieces[pointer++] = piece;
  300. positive++;
  301. }
  302. let path6 = "";
  303. for (let i = 1; i < pointer; i++) {
  304. path6 += "/" + pieces[i];
  305. }
  306. if (!path6 || addTrailingSlash && !path6.endsWith("/..")) {
  307. path6 += "/";
  308. }
  309. url.path = path6;
  310. }
  311. function resolve(input, base) {
  312. if (!input && !base)
  313. return "";
  314. const url = parseUrl(input);
  315. let inputType = url.type;
  316. if (base && inputType !== 7) {
  317. const baseUrl = parseUrl(base);
  318. const baseType = baseUrl.type;
  319. switch (inputType) {
  320. case 1:
  321. url.hash = baseUrl.hash;
  322. // fall through
  323. case 2:
  324. url.query = baseUrl.query;
  325. // fall through
  326. case 3:
  327. case 4:
  328. mergePaths(url, baseUrl);
  329. // fall through
  330. case 5:
  331. url.user = baseUrl.user;
  332. url.host = baseUrl.host;
  333. url.port = baseUrl.port;
  334. // fall through
  335. case 6:
  336. url.scheme = baseUrl.scheme;
  337. }
  338. if (baseType > inputType)
  339. inputType = baseType;
  340. }
  341. normalizePath(url, inputType);
  342. const queryHash = url.query + url.hash;
  343. switch (inputType) {
  344. // This is impossible, because of the empty checks at the start of the function.
  345. // case UrlType.Empty:
  346. case 2:
  347. case 3:
  348. return queryHash;
  349. case 4: {
  350. const path6 = url.path.slice(1);
  351. if (!path6)
  352. return queryHash || ".";
  353. if (isRelative(base || input) && !isRelative(path6)) {
  354. return "./" + path6 + queryHash;
  355. }
  356. return path6 + queryHash;
  357. }
  358. case 5:
  359. return url.path + queryHash;
  360. default:
  361. return url.scheme + "//" + url.user + url.host + url.port + url.path + queryHash;
  362. }
  363. }
  364. // node_modules/.pnpm/@jridgewell+trace-mapping@0.3.25/node_modules/@jridgewell/trace-mapping/dist/trace-mapping.mjs
  365. function resolve2(input, base) {
  366. if (base && !base.endsWith("/"))
  367. base += "/";
  368. return resolve(input, base);
  369. }
  370. function stripFilename(path6) {
  371. if (!path6)
  372. return "";
  373. const index = path6.lastIndexOf("/");
  374. return path6.slice(0, index + 1);
  375. }
  376. var COLUMN = 0;
  377. function maybeSort(mappings, owned) {
  378. const unsortedIndex = nextUnsortedSegmentLine(mappings, 0);
  379. if (unsortedIndex === mappings.length)
  380. return mappings;
  381. if (!owned)
  382. mappings = mappings.slice();
  383. for (let i = unsortedIndex; i < mappings.length; i = nextUnsortedSegmentLine(mappings, i + 1)) {
  384. mappings[i] = sortSegments(mappings[i], owned);
  385. }
  386. return mappings;
  387. }
  388. function nextUnsortedSegmentLine(mappings, start) {
  389. for (let i = start; i < mappings.length; i++) {
  390. if (!isSorted(mappings[i]))
  391. return i;
  392. }
  393. return mappings.length;
  394. }
  395. function isSorted(line) {
  396. for (let j = 1; j < line.length; j++) {
  397. if (line[j][COLUMN] < line[j - 1][COLUMN]) {
  398. return false;
  399. }
  400. }
  401. return true;
  402. }
  403. function sortSegments(line, owned) {
  404. if (!owned)
  405. line = line.slice();
  406. return line.sort(sortComparator2);
  407. }
  408. function sortComparator2(a, b) {
  409. return a[COLUMN] - b[COLUMN];
  410. }
  411. var found = false;
  412. function binarySearch(haystack, needle, low, high) {
  413. while (low <= high) {
  414. const mid = low + (high - low >> 1);
  415. const cmp = haystack[mid][COLUMN] - needle;
  416. if (cmp === 0) {
  417. found = true;
  418. return mid;
  419. }
  420. if (cmp < 0) {
  421. low = mid + 1;
  422. } else {
  423. high = mid - 1;
  424. }
  425. }
  426. found = false;
  427. return low - 1;
  428. }
  429. function upperBound(haystack, needle, index) {
  430. for (let i = index + 1; i < haystack.length; index = i++) {
  431. if (haystack[i][COLUMN] !== needle)
  432. break;
  433. }
  434. return index;
  435. }
  436. function lowerBound(haystack, needle, index) {
  437. for (let i = index - 1; i >= 0; index = i--) {
  438. if (haystack[i][COLUMN] !== needle)
  439. break;
  440. }
  441. return index;
  442. }
  443. function memoizedState() {
  444. return {
  445. lastKey: -1,
  446. lastNeedle: -1,
  447. lastIndex: -1
  448. };
  449. }
  450. function memoizedBinarySearch(haystack, needle, state, key) {
  451. const { lastKey, lastNeedle, lastIndex } = state;
  452. let low = 0;
  453. let high = haystack.length - 1;
  454. if (key === lastKey) {
  455. if (needle === lastNeedle) {
  456. found = lastIndex !== -1 && haystack[lastIndex][COLUMN] === needle;
  457. return lastIndex;
  458. }
  459. if (needle >= lastNeedle) {
  460. low = lastIndex === -1 ? 0 : lastIndex;
  461. } else {
  462. high = lastIndex;
  463. }
  464. }
  465. state.lastKey = key;
  466. state.lastNeedle = needle;
  467. return state.lastIndex = binarySearch(haystack, needle, low, high);
  468. }
  469. var LEAST_UPPER_BOUND = -1;
  470. var GREATEST_LOWER_BOUND = 1;
  471. var TraceMap = class {
  472. constructor(map, mapUrl) {
  473. const isString2 = typeof map === "string";
  474. if (!isString2 && map._decodedMemo)
  475. return map;
  476. const parsed = isString2 ? JSON.parse(map) : map;
  477. const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
  478. this.version = version;
  479. this.file = file;
  480. this.names = names || [];
  481. this.sourceRoot = sourceRoot;
  482. this.sources = sources;
  483. this.sourcesContent = sourcesContent;
  484. this.ignoreList = parsed.ignoreList || parsed.x_google_ignoreList || void 0;
  485. const from = resolve2(sourceRoot || "", stripFilename(mapUrl));
  486. this.resolvedSources = sources.map((s) => resolve2(s || "", from));
  487. const { mappings } = parsed;
  488. if (typeof mappings === "string") {
  489. this._encoded = mappings;
  490. this._decoded = void 0;
  491. } else {
  492. this._encoded = void 0;
  493. this._decoded = maybeSort(mappings, isString2);
  494. }
  495. this._decodedMemo = memoizedState();
  496. this._bySources = void 0;
  497. this._bySourceMemos = void 0;
  498. }
  499. };
  500. function cast(map) {
  501. return map;
  502. }
  503. function decodedMappings(map) {
  504. var _a;
  505. return (_a = cast(map))._decoded || (_a._decoded = decode(cast(map)._encoded));
  506. }
  507. function traceSegment(map, line, column) {
  508. const decoded = decodedMappings(map);
  509. if (line >= decoded.length)
  510. return null;
  511. const segments = decoded[line];
  512. const index = traceSegmentInternal(segments, cast(map)._decodedMemo, line, column, GREATEST_LOWER_BOUND);
  513. return index === -1 ? null : segments[index];
  514. }
  515. function traceSegmentInternal(segments, memo, line, column, bias) {
  516. let index = memoizedBinarySearch(segments, column, memo, line);
  517. if (found) {
  518. index = (bias === LEAST_UPPER_BOUND ? upperBound : lowerBound)(segments, column, index);
  519. } else if (bias === LEAST_UPPER_BOUND)
  520. index++;
  521. if (index === -1 || index === segments.length)
  522. return -1;
  523. return index;
  524. }
  525. // node_modules/.pnpm/@jridgewell+set-array@1.2.1/node_modules/@jridgewell/set-array/dist/set-array.mjs
  526. var SetArray = class {
  527. constructor() {
  528. this._indexes = { __proto__: null };
  529. this.array = [];
  530. }
  531. };
  532. function cast2(set) {
  533. return set;
  534. }
  535. function get(setarr, key) {
  536. return cast2(setarr)._indexes[key];
  537. }
  538. function put(setarr, key) {
  539. const index = get(setarr, key);
  540. if (index !== void 0)
  541. return index;
  542. const { array, _indexes: indexes } = cast2(setarr);
  543. const length = array.push(key);
  544. return indexes[key] = length - 1;
  545. }
  546. function remove(setarr, key) {
  547. const index = get(setarr, key);
  548. if (index === void 0)
  549. return;
  550. const { array, _indexes: indexes } = cast2(setarr);
  551. for (let i = index + 1; i < array.length; i++) {
  552. const k = array[i];
  553. array[i - 1] = k;
  554. indexes[k]--;
  555. }
  556. indexes[key] = void 0;
  557. array.pop();
  558. }
  559. // node_modules/.pnpm/@jridgewell+gen-mapping@0.3.5/node_modules/@jridgewell/gen-mapping/dist/gen-mapping.mjs
  560. var COLUMN2 = 0;
  561. var SOURCES_INDEX = 1;
  562. var SOURCE_LINE = 2;
  563. var SOURCE_COLUMN = 3;
  564. var NAMES_INDEX = 4;
  565. var NO_NAME = -1;
  566. var GenMapping = class {
  567. constructor({ file, sourceRoot } = {}) {
  568. this._names = new SetArray();
  569. this._sources = new SetArray();
  570. this._sourcesContent = [];
  571. this._mappings = [];
  572. this.file = file;
  573. this.sourceRoot = sourceRoot;
  574. this._ignoreList = new SetArray();
  575. }
  576. };
  577. function cast3(map) {
  578. return map;
  579. }
  580. var maybeAddSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
  581. return addSegmentInternal(true, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content);
  582. };
  583. function setSourceContent(map, source, content) {
  584. const { _sources: sources, _sourcesContent: sourcesContent } = cast3(map);
  585. const index = put(sources, source);
  586. sourcesContent[index] = content;
  587. }
  588. function setIgnore(map, source, ignore = true) {
  589. const { _sources: sources, _sourcesContent: sourcesContent, _ignoreList: ignoreList } = cast3(map);
  590. const index = put(sources, source);
  591. if (index === sourcesContent.length)
  592. sourcesContent[index] = null;
  593. if (ignore)
  594. put(ignoreList, index);
  595. else
  596. remove(ignoreList, index);
  597. }
  598. function toDecodedMap(map) {
  599. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, _ignoreList: ignoreList } = cast3(map);
  600. removeEmptyFinalLines(mappings);
  601. return {
  602. version: 3,
  603. file: map.file || void 0,
  604. names: names.array,
  605. sourceRoot: map.sourceRoot || void 0,
  606. sources: sources.array,
  607. sourcesContent,
  608. mappings,
  609. ignoreList: ignoreList.array
  610. };
  611. }
  612. function toEncodedMap(map) {
  613. const decoded = toDecodedMap(map);
  614. return Object.assign(Object.assign({}, decoded), { mappings: encode(decoded.mappings) });
  615. }
  616. function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
  617. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names } = cast3(map);
  618. const line = getLine(mappings, genLine);
  619. const index = getColumnIndex(line, genColumn);
  620. if (!source) {
  621. if (skipable && skipSourceless(line, index))
  622. return;
  623. return insert(line, index, [genColumn]);
  624. }
  625. const sourcesIndex = put(sources, source);
  626. const namesIndex = name ? put(names, name) : NO_NAME;
  627. if (sourcesIndex === sourcesContent.length)
  628. sourcesContent[sourcesIndex] = content !== null && content !== void 0 ? content : null;
  629. if (skipable && skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex)) {
  630. return;
  631. }
  632. return insert(line, index, name ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex] : [genColumn, sourcesIndex, sourceLine, sourceColumn]);
  633. }
  634. function getLine(mappings, index) {
  635. for (let i = mappings.length; i <= index; i++) {
  636. mappings[i] = [];
  637. }
  638. return mappings[index];
  639. }
  640. function getColumnIndex(line, genColumn) {
  641. let index = line.length;
  642. for (let i = index - 1; i >= 0; index = i--) {
  643. const current = line[i];
  644. if (genColumn >= current[COLUMN2])
  645. break;
  646. }
  647. return index;
  648. }
  649. function insert(array, index, value) {
  650. for (let i = array.length; i > index; i--) {
  651. array[i] = array[i - 1];
  652. }
  653. array[index] = value;
  654. }
  655. function removeEmptyFinalLines(mappings) {
  656. const { length } = mappings;
  657. let len = length;
  658. for (let i = len - 1; i >= 0; len = i, i--) {
  659. if (mappings[i].length > 0)
  660. break;
  661. }
  662. if (len < length)
  663. mappings.length = len;
  664. }
  665. function skipSourceless(line, index) {
  666. if (index === 0)
  667. return true;
  668. const prev = line[index - 1];
  669. return prev.length === 1;
  670. }
  671. function skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex) {
  672. if (index === 0)
  673. return false;
  674. const prev = line[index - 1];
  675. if (prev.length === 1)
  676. return false;
  677. return sourcesIndex === prev[SOURCES_INDEX] && sourceLine === prev[SOURCE_LINE] && sourceColumn === prev[SOURCE_COLUMN] && namesIndex === (prev.length === 5 ? prev[NAMES_INDEX] : NO_NAME);
  678. }
  679. // node_modules/.pnpm/@ampproject+remapping@2.3.0/node_modules/@ampproject/remapping/dist/remapping.mjs
  680. var SOURCELESS_MAPPING = /* @__PURE__ */ SegmentObject("", -1, -1, "", null, false);
  681. var EMPTY_SOURCES = [];
  682. function SegmentObject(source, line, column, name, content, ignore) {
  683. return { source, line, column, name, content, ignore };
  684. }
  685. function Source(map, sources, source, content, ignore) {
  686. return {
  687. map,
  688. sources,
  689. source,
  690. content,
  691. ignore
  692. };
  693. }
  694. function MapSource(map, sources) {
  695. return Source(map, sources, "", null, false);
  696. }
  697. function OriginalSource(source, content, ignore) {
  698. return Source(null, EMPTY_SOURCES, source, content, ignore);
  699. }
  700. function traceMappings(tree) {
  701. const gen = new GenMapping({ file: tree.map.file });
  702. const { sources: rootSources, map } = tree;
  703. const rootNames = map.names;
  704. const rootMappings = decodedMappings(map);
  705. for (let i = 0; i < rootMappings.length; i++) {
  706. const segments = rootMappings[i];
  707. for (let j = 0; j < segments.length; j++) {
  708. const segment = segments[j];
  709. const genCol = segment[0];
  710. let traced = SOURCELESS_MAPPING;
  711. if (segment.length !== 1) {
  712. const source2 = rootSources[segment[1]];
  713. traced = originalPositionFor(source2, segment[2], segment[3], segment.length === 5 ? rootNames[segment[4]] : "");
  714. if (traced == null)
  715. continue;
  716. }
  717. const { column, line, name, content, source, ignore } = traced;
  718. maybeAddSegment(gen, i, genCol, source, line, column, name);
  719. if (source && content != null)
  720. setSourceContent(gen, source, content);
  721. if (ignore)
  722. setIgnore(gen, source, true);
  723. }
  724. }
  725. return gen;
  726. }
  727. function originalPositionFor(source, line, column, name) {
  728. if (!source.map) {
  729. return SegmentObject(source.source, line, column, name, source.content, source.ignore);
  730. }
  731. const segment = traceSegment(source.map, line, column);
  732. if (segment == null)
  733. return null;
  734. if (segment.length === 1)
  735. return SOURCELESS_MAPPING;
  736. return originalPositionFor(source.sources[segment[1]], segment[2], segment[3], segment.length === 5 ? source.map.names[segment[4]] : name);
  737. }
  738. function asArray(value) {
  739. if (Array.isArray(value))
  740. return value;
  741. return [value];
  742. }
  743. function buildSourceMapTree(input, loader) {
  744. const maps = asArray(input).map((m) => new TraceMap(m, ""));
  745. const map = maps.pop();
  746. for (let i = 0; i < maps.length; i++) {
  747. if (maps[i].sources.length > 1) {
  748. throw new Error(`Transformation map ${i} must have exactly one source file.
  749. Did you specify these with the most recent transformation maps first?`);
  750. }
  751. }
  752. let tree = build(map, loader, "", 0);
  753. for (let i = maps.length - 1; i >= 0; i--) {
  754. tree = MapSource(maps[i], [tree]);
  755. }
  756. return tree;
  757. }
  758. function build(map, loader, importer, importerDepth) {
  759. const { resolvedSources, sourcesContent, ignoreList } = map;
  760. const depth = importerDepth + 1;
  761. const children = resolvedSources.map((sourceFile, i) => {
  762. const ctx = {
  763. importer,
  764. depth,
  765. source: sourceFile || "",
  766. content: void 0,
  767. ignore: void 0
  768. };
  769. const sourceMap = loader(ctx.source, ctx);
  770. const { source, content, ignore } = ctx;
  771. if (sourceMap)
  772. return build(new TraceMap(sourceMap, source), loader, source, depth);
  773. const sourceContent = content !== void 0 ? content : sourcesContent ? sourcesContent[i] : null;
  774. const ignored = ignore !== void 0 ? ignore : ignoreList ? ignoreList.includes(i) : false;
  775. return OriginalSource(source, sourceContent, ignored);
  776. });
  777. return MapSource(map, children);
  778. }
  779. var SourceMap = class {
  780. constructor(map, options) {
  781. const out = options.decodedMappings ? toDecodedMap(map) : toEncodedMap(map);
  782. this.version = out.version;
  783. this.file = out.file;
  784. this.mappings = out.mappings;
  785. this.names = out.names;
  786. this.ignoreList = out.ignoreList;
  787. this.sourceRoot = out.sourceRoot;
  788. this.sources = out.sources;
  789. if (!options.excludeContent) {
  790. this.sourcesContent = out.sourcesContent;
  791. }
  792. }
  793. toString() {
  794. return JSON.stringify(this);
  795. }
  796. };
  797. function remapping(input, loader, options) {
  798. const opts = typeof options === "object" ? options : { excludeContent: !!options, decodedMappings: false };
  799. const tree = buildSourceMapTree(input, loader);
  800. return new SourceMap(traceMappings(tree), opts);
  801. }
  802. // src/esbuild/utils.ts
  803. import { Parser } from "acorn";
  804. // src/utils.ts
  805. import { isAbsolute, normalize } from "path";
  806. function normalizeAbsolutePath(path6) {
  807. if (isAbsolute(path6))
  808. return normalize(path6);
  809. else
  810. return path6;
  811. }
  812. function toArray(array) {
  813. array = array || [];
  814. if (Array.isArray(array))
  815. return array;
  816. return [array];
  817. }
  818. function shouldLoad(id, plugin, externalModules) {
  819. if (id.startsWith(plugin.__virtualModulePrefix))
  820. id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
  821. if (plugin.loadInclude && !plugin.loadInclude(id))
  822. return false;
  823. return !externalModules.has(id);
  824. }
  825. function transformUse(data, plugin, transformLoader) {
  826. if (data.resource == null)
  827. return [];
  828. const id = normalizeAbsolutePath(data.resource + (data.resourceQuery || ""));
  829. if (!plugin.transformInclude || plugin.transformInclude(id)) {
  830. return [{
  831. loader: `${transformLoader}?unpluginName=${encodeURIComponent(plugin.name)}`
  832. }];
  833. }
  834. return [];
  835. }
  836. // src/esbuild/utils.ts
  837. var ExtToLoader = {
  838. ".js": "js",
  839. ".mjs": "js",
  840. ".cjs": "js",
  841. ".jsx": "jsx",
  842. ".ts": "ts",
  843. ".cts": "ts",
  844. ".mts": "ts",
  845. ".tsx": "tsx",
  846. ".css": "css",
  847. ".less": "css",
  848. ".stylus": "css",
  849. ".scss": "css",
  850. ".sass": "css",
  851. ".json": "json",
  852. ".txt": "text"
  853. };
  854. function guessLoader(code, id) {
  855. return ExtToLoader[path2.extname(id).toLowerCase()] || "js";
  856. }
  857. function unwrapLoader(loader, code, id) {
  858. if (typeof loader === "function")
  859. return loader(code, id);
  860. return loader;
  861. }
  862. function fixSourceMap(map) {
  863. if (!Object.prototype.hasOwnProperty.call(map, "toString")) {
  864. Object.defineProperty(map, "toString", {
  865. enumerable: false,
  866. value: function toString() {
  867. return JSON.stringify(this);
  868. }
  869. });
  870. }
  871. if (!Object.prototype.hasOwnProperty.call(map, "toUrl")) {
  872. Object.defineProperty(map, "toUrl", {
  873. enumerable: false,
  874. value: function toUrl() {
  875. return `data:application/json;charset=utf-8;base64,${Buffer2.from(this.toString()).toString("base64")}`;
  876. }
  877. });
  878. }
  879. return map;
  880. }
  881. var nullSourceMap = {
  882. names: [],
  883. sources: [],
  884. mappings: "",
  885. version: 3
  886. };
  887. function combineSourcemaps(filename, sourcemapList) {
  888. sourcemapList = sourcemapList.filter((m) => m.sources);
  889. if (sourcemapList.length === 0 || sourcemapList.every((m) => m.sources.length === 0)) {
  890. return { ...nullSourceMap };
  891. }
  892. let map;
  893. let mapIndex = 1;
  894. const useArrayInterface = sourcemapList.slice(0, -1).find((m) => m.sources.length !== 1) === void 0;
  895. if (useArrayInterface) {
  896. map = remapping(sourcemapList, () => null, true);
  897. } else {
  898. map = remapping(
  899. sourcemapList[0],
  900. (sourcefile) => {
  901. if (sourcefile === filename && sourcemapList[mapIndex])
  902. return sourcemapList[mapIndex++];
  903. else
  904. return { ...nullSourceMap };
  905. },
  906. true
  907. );
  908. }
  909. if (!map.file)
  910. delete map.file;
  911. return map;
  912. }
  913. function createBuildContext(build2) {
  914. const watchFiles = [];
  915. const { initialOptions } = build2;
  916. return {
  917. parse(code, opts = {}) {
  918. return Parser.parse(code, {
  919. sourceType: "module",
  920. ecmaVersion: "latest",
  921. locations: true,
  922. ...opts
  923. });
  924. },
  925. addWatchFile() {
  926. throw new Error("unplugin/esbuild: addWatchFile outside supported hooks (resolveId, load, transform)");
  927. },
  928. emitFile(emittedFile) {
  929. const outFileName = emittedFile.fileName || emittedFile.name;
  930. if (initialOptions.outdir && emittedFile.source && outFileName) {
  931. const outPath = path2.resolve(initialOptions.outdir, outFileName);
  932. const outDir = path2.dirname(outPath);
  933. if (!fs.existsSync(outDir))
  934. fs.mkdirSync(outDir, { recursive: true });
  935. fs.writeFileSync(outPath, emittedFile.source);
  936. }
  937. },
  938. getWatchFiles() {
  939. return watchFiles;
  940. },
  941. getNativeBuildContext() {
  942. return { framework: "esbuild", build: build2 };
  943. }
  944. };
  945. }
  946. function createPluginContext(context) {
  947. const errors = [];
  948. const warnings = [];
  949. const pluginContext = {
  950. error(message) {
  951. errors.push(normalizeMessage(message));
  952. },
  953. warn(message) {
  954. warnings.push(normalizeMessage(message));
  955. }
  956. };
  957. const mixedContext = {
  958. ...context,
  959. ...pluginContext,
  960. addWatchFile(id) {
  961. context.getWatchFiles().push(id);
  962. }
  963. };
  964. return {
  965. errors,
  966. warnings,
  967. mixedContext
  968. };
  969. }
  970. function normalizeMessage(message) {
  971. if (typeof message === "string")
  972. message = { message };
  973. return {
  974. id: message.id,
  975. pluginName: message.plugin,
  976. text: message.message,
  977. location: message.loc ? {
  978. file: message.loc.file,
  979. line: message.loc.line,
  980. column: message.loc.column
  981. } : null,
  982. detail: message.meta,
  983. notes: []
  984. };
  985. }
  986. function processCodeWithSourceMap(map, code) {
  987. if (map) {
  988. if (!map.sourcesContent || map.sourcesContent.length === 0)
  989. map.sourcesContent = [code];
  990. map = fixSourceMap(map);
  991. code += `
  992. //# sourceMappingURL=${map.toUrl()}`;
  993. }
  994. return code;
  995. }
  996. // src/esbuild/index.ts
  997. function getEsbuildPlugin(factory) {
  998. return (userOptions) => {
  999. const meta = {
  1000. framework: "esbuild"
  1001. };
  1002. const plugins = toArray(factory(userOptions, meta));
  1003. const setupPlugins = async (build2) => {
  1004. const setup = buildSetup(meta);
  1005. const loaders = [];
  1006. for (const plugin of plugins) {
  1007. const loader = {};
  1008. await setup(plugin)({
  1009. ...build2,
  1010. onLoad(_options, callback) {
  1011. loader.options = _options;
  1012. loader.onLoadCb = callback;
  1013. },
  1014. onTransform(_options, callback) {
  1015. loader.options || (loader.options = _options);
  1016. loader.onTransformCb = callback;
  1017. }
  1018. }, build2);
  1019. if (loader.onLoadCb || loader.onTransformCb)
  1020. loaders.push(loader);
  1021. }
  1022. if (loaders.length) {
  1023. build2.onLoad(loaders.length === 1 ? loaders[0].options : { filter: /.*/ }, async (args) => {
  1024. function checkFilter(options) {
  1025. return loaders.length === 1 || !(options == null ? void 0 : options.filter) || options.filter.test(args.path);
  1026. }
  1027. let result;
  1028. for (const { options, onLoadCb } of loaders) {
  1029. if (!checkFilter(options))
  1030. continue;
  1031. if (onLoadCb)
  1032. result = await onLoadCb(args);
  1033. if (result == null ? void 0 : result.contents)
  1034. break;
  1035. }
  1036. let fsContentsCache;
  1037. for (const { options, onTransformCb } of loaders) {
  1038. if (!checkFilter(options))
  1039. continue;
  1040. if (onTransformCb) {
  1041. const newArgs = {
  1042. ...result,
  1043. ...args,
  1044. async getContents() {
  1045. if (result == null ? void 0 : result.contents)
  1046. return result.contents;
  1047. if (fsContentsCache)
  1048. return fsContentsCache;
  1049. return fsContentsCache = await fs2.promises.readFile(args.path, "utf8");
  1050. }
  1051. };
  1052. const _result = await onTransformCb(newArgs);
  1053. if (_result == null ? void 0 : _result.contents)
  1054. result = _result;
  1055. }
  1056. }
  1057. if (result == null ? void 0 : result.contents)
  1058. return result;
  1059. });
  1060. }
  1061. };
  1062. return {
  1063. name: (plugins.length === 1 ? plugins[0].name : meta.esbuildHostName) ?? `unplugin-host:${plugins.map((p) => p.name).join(":")}`,
  1064. setup: setupPlugins
  1065. };
  1066. };
  1067. }
  1068. function buildSetup(meta) {
  1069. return (plugin) => {
  1070. return (build2, rawBuild) => {
  1071. var _a, _b, _c, _d, _e;
  1072. meta.build = build2;
  1073. const context = createBuildContext(build2);
  1074. const { onStart, onEnd, onResolve, onLoad, onTransform, initialOptions } = build2;
  1075. const onResolveFilter = ((_a = plugin.esbuild) == null ? void 0 : _a.onResolveFilter) ?? /.*/;
  1076. const onLoadFilter = ((_b = plugin.esbuild) == null ? void 0 : _b.onLoadFilter) ?? /.*/;
  1077. const loader = ((_c = plugin.esbuild) == null ? void 0 : _c.loader) ?? guessLoader;
  1078. if ((_d = plugin.esbuild) == null ? void 0 : _d.config)
  1079. plugin.esbuild.config.call(context, initialOptions);
  1080. if (plugin.buildStart)
  1081. onStart(() => plugin.buildStart.call(context));
  1082. if (plugin.buildEnd || plugin.writeBundle) {
  1083. onEnd(async () => {
  1084. if (plugin.buildEnd)
  1085. await plugin.buildEnd.call(context);
  1086. if (plugin.writeBundle)
  1087. await plugin.writeBundle();
  1088. });
  1089. }
  1090. if (plugin.resolveId) {
  1091. onResolve({ filter: onResolveFilter }, async (args) => {
  1092. var _a2;
  1093. if ((_a2 = initialOptions.external) == null ? void 0 : _a2.includes(args.path)) {
  1094. return void 0;
  1095. }
  1096. const { errors, warnings, mixedContext } = createPluginContext(context);
  1097. const isEntry = args.kind === "entry-point";
  1098. const result = await plugin.resolveId.call(
  1099. mixedContext,
  1100. args.path,
  1101. // We explicitly have this if statement here for consistency with
  1102. // the integration of other bundlers.
  1103. // Here, `args.importer` is just an empty string on entry files
  1104. // whereas the equivalent on other bundlers is`undefined.`
  1105. isEntry ? void 0 : args.importer,
  1106. { isEntry }
  1107. );
  1108. if (typeof result === "string") {
  1109. return {
  1110. path: result,
  1111. namespace: plugin.name,
  1112. errors,
  1113. warnings,
  1114. watchFiles: mixedContext.getWatchFiles()
  1115. };
  1116. } else if (typeof result === "object" && result !== null) {
  1117. return {
  1118. path: result.id,
  1119. external: result.external,
  1120. namespace: plugin.name,
  1121. errors,
  1122. warnings,
  1123. watchFiles: mixedContext.getWatchFiles()
  1124. };
  1125. }
  1126. });
  1127. }
  1128. if (plugin.load) {
  1129. onLoad({ filter: onLoadFilter }, async (args) => {
  1130. const id = args.path + args.suffix;
  1131. const { errors, warnings, mixedContext } = createPluginContext(context);
  1132. const resolveDir = path3.dirname(args.path);
  1133. let code, map;
  1134. if (plugin.load && (!plugin.loadInclude || plugin.loadInclude(id))) {
  1135. const result = await plugin.load.call(mixedContext, id);
  1136. if (typeof result === "string") {
  1137. code = result;
  1138. } else if (typeof result === "object" && result !== null) {
  1139. code = result.code;
  1140. map = result.map;
  1141. }
  1142. }
  1143. if (code === void 0)
  1144. return null;
  1145. if (map)
  1146. code = processCodeWithSourceMap(map, code);
  1147. return {
  1148. contents: code,
  1149. errors,
  1150. warnings,
  1151. watchFiles: mixedContext.getWatchFiles(),
  1152. loader: unwrapLoader(loader, code, args.path),
  1153. resolveDir
  1154. };
  1155. });
  1156. }
  1157. if (plugin.transform) {
  1158. onTransform({ filter: onLoadFilter }, async (args) => {
  1159. const id = args.path + args.suffix;
  1160. if (plugin.transformInclude && !plugin.transformInclude(id))
  1161. return;
  1162. const { mixedContext, errors, warnings } = createPluginContext(context);
  1163. const resolveDir = path3.dirname(args.path);
  1164. let code = await args.getContents();
  1165. let map;
  1166. const result = await plugin.transform.call(mixedContext, code, id);
  1167. if (typeof result === "string") {
  1168. code = result;
  1169. } else if (typeof result === "object" && result !== null) {
  1170. code = result.code;
  1171. if (map && result.map) {
  1172. map = combineSourcemaps(args.path, [
  1173. result.map === "string" ? JSON.parse(result.map) : result.map,
  1174. map
  1175. ]);
  1176. } else {
  1177. if (typeof result.map === "string") {
  1178. map = JSON.parse(result.map);
  1179. } else {
  1180. map = result.map;
  1181. }
  1182. }
  1183. }
  1184. if (code) {
  1185. if (map)
  1186. code = processCodeWithSourceMap(map, code);
  1187. return {
  1188. contents: code,
  1189. errors,
  1190. warnings,
  1191. watchFiles: mixedContext.getWatchFiles(),
  1192. loader: unwrapLoader(loader, code, args.path),
  1193. resolveDir
  1194. };
  1195. }
  1196. });
  1197. }
  1198. if ((_e = plugin.esbuild) == null ? void 0 : _e.setup)
  1199. return plugin.esbuild.setup(rawBuild);
  1200. };
  1201. };
  1202. }
  1203. // src/farm/index.ts
  1204. import path5 from "path";
  1205. // src/farm/context.ts
  1206. import { Buffer as Buffer3 } from "buffer";
  1207. import { extname } from "path";
  1208. import { Parser as Parser2 } from "acorn";
  1209. function createFarmContext(context, currentResolveId) {
  1210. return {
  1211. parse(code, opts = {}) {
  1212. return Parser2.parse(code, {
  1213. sourceType: "module",
  1214. ecmaVersion: "latest",
  1215. locations: true,
  1216. ...opts
  1217. });
  1218. },
  1219. addWatchFile(id) {
  1220. context.addWatchFile(currentResolveId || id, id);
  1221. },
  1222. emitFile(emittedFile) {
  1223. const outFileName = emittedFile.fileName || emittedFile.name;
  1224. if (emittedFile.source && outFileName) {
  1225. context.emitFile({
  1226. resolvedPath: outFileName,
  1227. name: outFileName,
  1228. content: [...Buffer3.from(emittedFile.source)],
  1229. resourceType: extname(outFileName)
  1230. });
  1231. }
  1232. },
  1233. getWatchFiles() {
  1234. return context.getWatchFiles();
  1235. },
  1236. getNativeBuildContext() {
  1237. return { framework: "farm", context };
  1238. }
  1239. };
  1240. }
  1241. function unpluginContext(context) {
  1242. return {
  1243. error: (error) => context.error(
  1244. typeof error === "string" ? new Error(error) : error
  1245. ),
  1246. warn: (error) => context.warn(typeof error === "string" ? new Error(error) : error)
  1247. };
  1248. }
  1249. // src/farm/utils.ts
  1250. import path4 from "path";
  1251. import * as querystring from "querystring";
  1252. var ExtToLoader2 = {
  1253. ".js": "js",
  1254. ".mjs": "js",
  1255. ".cjs": "js",
  1256. ".jsx": "jsx",
  1257. ".ts": "ts",
  1258. ".cts": "ts",
  1259. ".mts": "ts",
  1260. ".tsx": "tsx",
  1261. ".json": "json",
  1262. ".toml": "toml",
  1263. ".text": "text",
  1264. ".wasm": "wasm",
  1265. ".napi": "napi",
  1266. ".node": "napi"
  1267. };
  1268. function guessIdLoader(id) {
  1269. return ExtToLoader2[path4.extname(id).toLowerCase()] || "js";
  1270. }
  1271. function transformQuery(context) {
  1272. const queryParamsObject = {};
  1273. context.query.forEach(([param, value]) => {
  1274. queryParamsObject[param] = value;
  1275. });
  1276. const transformQuery2 = querystring.stringify(queryParamsObject);
  1277. context.resolvedPath = `${context.resolvedPath}?${transformQuery2}`;
  1278. }
  1279. function convertEnforceToPriority(value) {
  1280. const defaultPriority = 100;
  1281. const enforceToPriority = {
  1282. pre: 101,
  1283. post: 99
  1284. };
  1285. return enforceToPriority[value] !== void 0 ? enforceToPriority[value] : defaultPriority;
  1286. }
  1287. function convertWatchEventChange(value) {
  1288. const watchEventChange = {
  1289. Added: "create",
  1290. Updated: "update",
  1291. Removed: "delete"
  1292. };
  1293. return watchEventChange[value];
  1294. }
  1295. function getContentValue(content) {
  1296. return typeof content === "string" ? content : content.code;
  1297. }
  1298. function isString(variable) {
  1299. return typeof variable === "string";
  1300. }
  1301. function isObject(variable) {
  1302. return typeof variable === "object" && variable !== null;
  1303. }
  1304. function customParseQueryString(url) {
  1305. if (!url)
  1306. return [];
  1307. const queryString = url.split("?")[1];
  1308. const parsedParams = querystring.parse(queryString);
  1309. const paramsArray = [];
  1310. for (const key in parsedParams)
  1311. paramsArray.push([key, parsedParams[key]]);
  1312. return paramsArray;
  1313. }
  1314. // src/farm/index.ts
  1315. function getFarmPlugin(factory) {
  1316. return (userOptions) => {
  1317. const meta = {
  1318. framework: "farm"
  1319. };
  1320. const rawPlugins = toArray(factory(userOptions, meta));
  1321. const plugins = rawPlugins.map((rawPlugin) => {
  1322. const plugin = toFarmPlugin(rawPlugin, userOptions);
  1323. if (rawPlugin.farm)
  1324. Object.assign(plugin, rawPlugin.farm);
  1325. return plugin;
  1326. });
  1327. return plugins.length === 1 ? plugins[0] : plugins;
  1328. };
  1329. }
  1330. function toFarmPlugin(plugin, options) {
  1331. const farmPlugin = {
  1332. name: plugin.name,
  1333. priority: convertEnforceToPriority(plugin.enforce)
  1334. };
  1335. if (plugin.farm) {
  1336. Object.keys(plugin.farm).forEach((key) => {
  1337. const value = plugin.farm[key];
  1338. if (value)
  1339. Reflect.set(farmPlugin, key, value);
  1340. });
  1341. }
  1342. if (plugin.buildStart) {
  1343. const _buildStart = plugin.buildStart;
  1344. farmPlugin.buildStart = {
  1345. async executor(_, context) {
  1346. await _buildStart.call(createFarmContext(context));
  1347. }
  1348. };
  1349. }
  1350. if (plugin.resolveId) {
  1351. const _resolveId = plugin.resolveId;
  1352. let filters = [];
  1353. if (options)
  1354. filters = (options == null ? void 0 : options.filters) ?? [];
  1355. farmPlugin.resolve = {
  1356. filters: { sources: [".*", ...filters], importers: [".*"] },
  1357. async executor(params, context) {
  1358. const resolvedIdPath = path5.resolve(
  1359. process.cwd(),
  1360. params.importer ?? ""
  1361. );
  1362. let isEntry = false;
  1363. if (isObject(params.kind) && "entry" in params.kind) {
  1364. const kindWithEntry = params.kind;
  1365. isEntry = kindWithEntry.entry === "index";
  1366. }
  1367. const farmContext = createFarmContext(context, resolvedIdPath);
  1368. const resolveIdResult = await _resolveId.call(
  1369. Object.assign(unpluginContext(context), farmContext),
  1370. params.source,
  1371. resolvedIdPath ?? null,
  1372. { isEntry }
  1373. );
  1374. if (isString(resolveIdResult)) {
  1375. return {
  1376. resolvedPath: resolveIdResult,
  1377. query: customParseQueryString(resolveIdResult),
  1378. sideEffects: false,
  1379. external: false,
  1380. meta: {}
  1381. };
  1382. } else if (isObject(resolveIdResult)) {
  1383. return {
  1384. resolvedPath: resolveIdResult == null ? void 0 : resolveIdResult.id,
  1385. query: customParseQueryString(resolveIdResult.id),
  1386. sideEffects: false,
  1387. external: resolveIdResult == null ? void 0 : resolveIdResult.external,
  1388. meta: {}
  1389. };
  1390. }
  1391. return null;
  1392. }
  1393. };
  1394. }
  1395. if (plugin.load) {
  1396. const _load = plugin.load;
  1397. farmPlugin.load = {
  1398. filters: {
  1399. resolvedPaths: [".*"]
  1400. },
  1401. async executor(id, context) {
  1402. if (plugin.loadInclude && !plugin.loadInclude(id.resolvedPath))
  1403. return null;
  1404. const loader = guessIdLoader(id.resolvedPath);
  1405. const shouldLoadInclude = plugin.loadInclude && plugin.loadInclude(id.resolvedPath);
  1406. const farmContext = createFarmContext(context, id.resolvedPath);
  1407. const content = await _load.call(
  1408. Object.assign(unpluginContext(context), farmContext),
  1409. id.resolvedPath
  1410. );
  1411. const loadFarmResult = {
  1412. content: getContentValue(content),
  1413. moduleType: loader
  1414. };
  1415. if (shouldLoadInclude)
  1416. return loadFarmResult;
  1417. return null;
  1418. }
  1419. };
  1420. }
  1421. if (plugin.transform) {
  1422. const _transform = plugin.transform;
  1423. farmPlugin.transform = {
  1424. filters: { resolvedPaths: [".*"], moduleTypes: [".*"] },
  1425. async executor(params, context) {
  1426. if (params.query.length)
  1427. transformQuery(params);
  1428. if (plugin.transformInclude && !plugin.transformInclude(params.resolvedPath)) {
  1429. return null;
  1430. }
  1431. const loader = params.moduleType ?? guessIdLoader(params.resolvedPath);
  1432. const shouldTransformInclude = plugin.transformInclude && plugin.transformInclude(params.resolvedPath);
  1433. const farmContext = createFarmContext(context, params.resolvedPath);
  1434. const resource = await _transform.call(
  1435. Object.assign(unpluginContext(context), farmContext),
  1436. params.content,
  1437. params.resolvedPath
  1438. );
  1439. if (resource && typeof resource !== "string") {
  1440. const transformFarmResult = {
  1441. content: getContentValue(resource),
  1442. moduleType: loader,
  1443. sourceMap: JSON.stringify(resource.map)
  1444. };
  1445. if (shouldTransformInclude)
  1446. return transformFarmResult;
  1447. return transformFarmResult;
  1448. }
  1449. }
  1450. };
  1451. }
  1452. if (plugin.watchChange) {
  1453. const _watchChange = plugin.watchChange;
  1454. farmPlugin.updateModules = {
  1455. async executor(param, context) {
  1456. const updatePathContent = param.paths[0];
  1457. const ModifiedPath = updatePathContent[0];
  1458. const eventChange = convertWatchEventChange(
  1459. updatePathContent[1]
  1460. );
  1461. await _watchChange.call(createFarmContext(context), ModifiedPath, {
  1462. event: eventChange
  1463. });
  1464. }
  1465. };
  1466. }
  1467. if (plugin.buildEnd) {
  1468. const _buildEnd = plugin.buildEnd;
  1469. farmPlugin.buildEnd = {
  1470. async executor(_, context) {
  1471. await _buildEnd.call(createFarmContext(context));
  1472. }
  1473. };
  1474. }
  1475. if (plugin.writeBundle) {
  1476. const _writeBundle = plugin.writeBundle;
  1477. farmPlugin.finish = {
  1478. async executor() {
  1479. await _writeBundle();
  1480. }
  1481. };
  1482. }
  1483. return farmPlugin;
  1484. }
  1485. // src/rollup/index.ts
  1486. function getRollupPlugin(factory) {
  1487. return (userOptions) => {
  1488. const meta = {
  1489. framework: "rollup"
  1490. };
  1491. const rawPlugins = toArray(factory(userOptions, meta));
  1492. const plugins = rawPlugins.map((plugin) => toRollupPlugin(plugin));
  1493. return plugins.length === 1 ? plugins[0] : plugins;
  1494. };
  1495. }
  1496. function toRollupPlugin(plugin, containRollupOptions = true) {
  1497. if (plugin.transform && plugin.transformInclude) {
  1498. const _transform = plugin.transform;
  1499. plugin.transform = function(code, id) {
  1500. if (plugin.transformInclude && !plugin.transformInclude(id))
  1501. return null;
  1502. return _transform.call(this, code, id);
  1503. };
  1504. }
  1505. if (plugin.load && plugin.loadInclude) {
  1506. const _load = plugin.load;
  1507. plugin.load = function(id) {
  1508. if (plugin.loadInclude && !plugin.loadInclude(id))
  1509. return null;
  1510. return _load.call(this, id);
  1511. };
  1512. }
  1513. if (plugin.rollup && containRollupOptions)
  1514. Object.assign(plugin, plugin.rollup);
  1515. return plugin;
  1516. }
  1517. // src/rolldown/index.ts
  1518. function getRolldownPlugin(factory) {
  1519. return (userOptions) => {
  1520. const meta = {
  1521. framework: "rolldown"
  1522. };
  1523. const rawPlugins = toArray(factory(userOptions, meta));
  1524. const plugins = rawPlugins.map((rawPlugin) => {
  1525. const plugin = toRollupPlugin(rawPlugin, false);
  1526. if (rawPlugin.rolldown)
  1527. Object.assign(plugin, rawPlugin.rolldown);
  1528. return plugin;
  1529. });
  1530. return plugins.length === 1 ? plugins[0] : plugins;
  1531. };
  1532. }
  1533. // src/rspack/index.ts
  1534. import fs4 from "fs";
  1535. import { resolve as resolve5 } from "path";
  1536. // src/rspack/context.ts
  1537. import { Buffer as Buffer4 } from "buffer";
  1538. import { resolve as resolve3 } from "path";
  1539. import { Parser as Parser3 } from "acorn";
  1540. function createBuildContext2(compiler, compilation, loaderContext) {
  1541. return {
  1542. getNativeBuildContext() {
  1543. return {
  1544. framework: "rspack",
  1545. compiler,
  1546. compilation,
  1547. loaderContext
  1548. };
  1549. },
  1550. addWatchFile(file) {
  1551. compilation.fileDependencies.add(resolve3(process.cwd(), file));
  1552. },
  1553. getWatchFiles() {
  1554. return Array.from(compilation.fileDependencies);
  1555. },
  1556. parse(code, opts = {}) {
  1557. return Parser3.parse(code, {
  1558. sourceType: "module",
  1559. ecmaVersion: "latest",
  1560. locations: true,
  1561. ...opts
  1562. });
  1563. },
  1564. emitFile(emittedFile) {
  1565. const outFileName = emittedFile.fileName || emittedFile.name;
  1566. if (emittedFile.source && outFileName) {
  1567. const { sources } = compilation.compiler.webpack;
  1568. compilation.emitAsset(
  1569. outFileName,
  1570. new sources.RawSource(
  1571. typeof emittedFile.source === "string" ? emittedFile.source : Buffer4.from(emittedFile.source)
  1572. )
  1573. );
  1574. }
  1575. }
  1576. };
  1577. }
  1578. function normalizeMessage2(error) {
  1579. const err = new Error(typeof error === "string" ? error : error.message);
  1580. if (typeof error === "object") {
  1581. err.stack = error.stack;
  1582. err.cause = error.meta;
  1583. }
  1584. return err;
  1585. }
  1586. // src/rspack/utils.ts
  1587. import fs3 from "fs";
  1588. import { basename, dirname, resolve as resolve4 } from "path";
  1589. function encodeVirtualModuleId(id, plugin) {
  1590. return resolve4(plugin.__virtualModulePrefix, encodeURIComponent(id));
  1591. }
  1592. function decodeVirtualModuleId(encoded, _plugin) {
  1593. return decodeURIComponent(basename(encoded));
  1594. }
  1595. function isVirtualModuleId(encoded, plugin) {
  1596. return dirname(encoded) === plugin.__virtualModulePrefix;
  1597. }
  1598. var FakeVirtualModulesPlugin = class {
  1599. constructor(plugin) {
  1600. this.plugin = plugin;
  1601. }
  1602. name = "FakeVirtualModulesPlugin";
  1603. apply(compiler) {
  1604. const dir = this.plugin.__virtualModulePrefix;
  1605. if (!fs3.existsSync(dir)) {
  1606. fs3.mkdirSync(dir, { recursive: true });
  1607. }
  1608. compiler.hooks.shutdown.tap(this.name, () => {
  1609. if (fs3.existsSync(dir)) {
  1610. fs3.rmdirSync(dir, { recursive: true });
  1611. }
  1612. });
  1613. }
  1614. async writeModule(file) {
  1615. const path6 = encodeVirtualModuleId(file, this.plugin);
  1616. await fs3.promises.writeFile(path6, "");
  1617. return path6;
  1618. }
  1619. };
  1620. // src/rspack/index.ts
  1621. var TRANSFORM_LOADER = resolve5(
  1622. __dirname,
  1623. false ? "../../dist/rspack/loaders/transform.js" : "rspack/loaders/transform"
  1624. );
  1625. var LOAD_LOADER = resolve5(
  1626. __dirname,
  1627. false ? "../../dist/rspack/loaders/load.js" : "rspack/loaders/load"
  1628. );
  1629. function getRspackPlugin(factory) {
  1630. return (userOptions) => {
  1631. return {
  1632. apply(compiler) {
  1633. const VIRTUAL_MODULE_PREFIX = resolve5(compiler.options.context ?? process.cwd(), "node_modules/.virtual");
  1634. const injected = compiler.$unpluginContext || {};
  1635. compiler.$unpluginContext = injected;
  1636. const meta = {
  1637. framework: "rspack",
  1638. rspack: {
  1639. compiler
  1640. }
  1641. };
  1642. const rawPlugins = toArray(factory(userOptions, meta));
  1643. for (const rawPlugin of rawPlugins) {
  1644. const plugin = Object.assign(
  1645. rawPlugin,
  1646. {
  1647. __unpluginMeta: meta,
  1648. __virtualModulePrefix: VIRTUAL_MODULE_PREFIX
  1649. }
  1650. );
  1651. injected[plugin.name] = plugin;
  1652. compiler.hooks.thisCompilation.tap(plugin.name, (compilation) => {
  1653. if (typeof compilation.hooks.childCompiler === "undefined")
  1654. throw new Error("`compilation.hooks.childCompiler` only support by @rspack/core>=0.4.1");
  1655. compilation.hooks.childCompiler.tap(plugin.name, (childCompiler) => {
  1656. childCompiler.$unpluginContext = injected;
  1657. });
  1658. });
  1659. const externalModules = /* @__PURE__ */ new Set();
  1660. if (plugin.resolveId) {
  1661. const vfs = new FakeVirtualModulesPlugin(plugin);
  1662. vfs.apply(compiler);
  1663. plugin.__vfsModules = /* @__PURE__ */ new Set();
  1664. compiler.hooks.compilation.tap(plugin.name, (compilation, { normalModuleFactory }) => {
  1665. normalModuleFactory.hooks.resolve.tapPromise(plugin.name, async (resolveData) => {
  1666. const id = normalizeAbsolutePath(resolveData.request);
  1667. const requestContext = resolveData.contextInfo;
  1668. let importer = requestContext.issuer !== "" ? requestContext.issuer : void 0;
  1669. const isEntry = requestContext.issuer === "";
  1670. if (importer == null ? void 0 : importer.startsWith(plugin.__virtualModulePrefix))
  1671. importer = decodeURIComponent(importer.slice(plugin.__virtualModulePrefix.length));
  1672. const context = createBuildContext2(compiler, compilation);
  1673. let error;
  1674. const pluginContext = {
  1675. error(msg) {
  1676. if (error == null)
  1677. error = normalizeMessage2(msg);
  1678. else
  1679. console.error(`unplugin/rspack: multiple errors returned from resolveId hook: ${msg}`);
  1680. },
  1681. warn(msg) {
  1682. console.warn(`unplugin/rspack: warning from resolveId hook: ${msg}`);
  1683. }
  1684. };
  1685. const resolveIdResult = await plugin.resolveId.call({ ...context, ...pluginContext }, id, importer, { isEntry });
  1686. if (error != null)
  1687. throw error;
  1688. if (resolveIdResult == null)
  1689. return;
  1690. let resolved = typeof resolveIdResult === "string" ? resolveIdResult : resolveIdResult.id;
  1691. const isExternal = typeof resolveIdResult === "string" ? false : resolveIdResult.external === true;
  1692. if (isExternal)
  1693. externalModules.add(resolved);
  1694. if (!fs4.existsSync(resolved)) {
  1695. if (!plugin.__vfsModules.has(resolved)) {
  1696. plugin.__vfsModules.add(resolved);
  1697. await vfs.writeModule(resolved);
  1698. }
  1699. resolved = encodeVirtualModuleId(resolved, plugin);
  1700. }
  1701. resolveData.request = resolved;
  1702. });
  1703. });
  1704. }
  1705. if (plugin.load) {
  1706. compiler.options.module.rules.unshift({
  1707. enforce: plugin.enforce,
  1708. include(id) {
  1709. if (isVirtualModuleId(id, plugin))
  1710. id = decodeVirtualModuleId(id, plugin);
  1711. if (plugin.loadInclude && !plugin.loadInclude(id))
  1712. return false;
  1713. return !externalModules.has(id);
  1714. },
  1715. use: [{
  1716. loader: LOAD_LOADER,
  1717. options: {
  1718. unpluginName: plugin.name
  1719. }
  1720. }],
  1721. type: "javascript/auto"
  1722. });
  1723. }
  1724. if (plugin.transform) {
  1725. compiler.options.module.rules.unshift({
  1726. enforce: plugin.enforce,
  1727. use(data) {
  1728. return transformUse(data, plugin, TRANSFORM_LOADER);
  1729. }
  1730. });
  1731. }
  1732. if (plugin.rspack)
  1733. plugin.rspack(compiler);
  1734. if (plugin.watchChange || plugin.buildStart) {
  1735. compiler.hooks.make.tapPromise(plugin.name, async (compilation) => {
  1736. const context = createBuildContext2(compiler, compilation);
  1737. if (plugin.watchChange && (compiler.modifiedFiles || compiler.removedFiles)) {
  1738. const promises = [];
  1739. if (compiler.modifiedFiles) {
  1740. compiler.modifiedFiles.forEach(
  1741. (file) => promises.push(Promise.resolve(plugin.watchChange.call(context, file, { event: "update" })))
  1742. );
  1743. }
  1744. if (compiler.removedFiles) {
  1745. compiler.removedFiles.forEach(
  1746. (file) => promises.push(Promise.resolve(plugin.watchChange.call(context, file, { event: "delete" })))
  1747. );
  1748. }
  1749. await Promise.all(promises);
  1750. }
  1751. if (plugin.buildStart)
  1752. return await plugin.buildStart.call(context);
  1753. });
  1754. }
  1755. if (plugin.buildEnd) {
  1756. compiler.hooks.emit.tapPromise(plugin.name, async (compilation) => {
  1757. await plugin.buildEnd.call(createBuildContext2(compiler, compilation));
  1758. });
  1759. }
  1760. if (plugin.writeBundle) {
  1761. compiler.hooks.afterEmit.tapPromise(plugin.name, async () => {
  1762. await plugin.writeBundle();
  1763. });
  1764. }
  1765. }
  1766. }
  1767. };
  1768. };
  1769. }
  1770. // src/vite/index.ts
  1771. function getVitePlugin(factory) {
  1772. return (userOptions) => {
  1773. const meta = {
  1774. framework: "vite"
  1775. };
  1776. const rawPlugins = toArray(factory(userOptions, meta));
  1777. const plugins = rawPlugins.map((rawPlugin) => {
  1778. const plugin = toRollupPlugin(rawPlugin, false);
  1779. if (rawPlugin.vite)
  1780. Object.assign(plugin, rawPlugin.vite);
  1781. return plugin;
  1782. });
  1783. return plugins.length === 1 ? plugins[0] : plugins;
  1784. };
  1785. }
  1786. // src/webpack/index.ts
  1787. import fs5 from "fs";
  1788. import { resolve as resolve7 } from "path";
  1789. import process3 from "process";
  1790. import VirtualModulesPlugin from "webpack-virtual-modules";
  1791. // src/webpack/context.ts
  1792. import { Buffer as Buffer5 } from "buffer";
  1793. import { createRequire } from "module";
  1794. import { resolve as resolve6 } from "path";
  1795. import process2 from "process";
  1796. import { Parser as Parser4 } from "acorn";
  1797. function contextOptionsFromCompilation(compilation) {
  1798. return {
  1799. addWatchFile(file) {
  1800. (compilation.fileDependencies ?? compilation.compilationDependencies).add(file);
  1801. },
  1802. getWatchFiles() {
  1803. return Array.from(compilation.fileDependencies ?? compilation.compilationDependencies);
  1804. }
  1805. };
  1806. }
  1807. function getSource(fileSource) {
  1808. const webpackRequire = createRequire(__require.resolve("webpack"));
  1809. const RawSource = webpackRequire("webpack-sources").RawSource;
  1810. return new RawSource(
  1811. typeof fileSource === "string" ? fileSource : Buffer5.from(fileSource.buffer).toString("utf-8")
  1812. );
  1813. }
  1814. function createBuildContext3(options, compiler, compilation, loaderContext) {
  1815. return {
  1816. parse(code, opts = {}) {
  1817. return Parser4.parse(code, {
  1818. sourceType: "module",
  1819. ecmaVersion: "latest",
  1820. locations: true,
  1821. ...opts
  1822. });
  1823. },
  1824. addWatchFile(id) {
  1825. options.addWatchFile(resolve6(process2.cwd(), id));
  1826. },
  1827. emitFile(emittedFile) {
  1828. const outFileName = emittedFile.fileName || emittedFile.name;
  1829. if (emittedFile.source && outFileName) {
  1830. if (!compilation)
  1831. throw new Error("unplugin/webpack: emitFile outside supported hooks (buildStart, buildEnd, load, transform, watchChange)");
  1832. compilation.emitAsset(
  1833. outFileName,
  1834. getSource(emittedFile.source)
  1835. );
  1836. }
  1837. },
  1838. getWatchFiles() {
  1839. return options.getWatchFiles();
  1840. },
  1841. getNativeBuildContext() {
  1842. return { framework: "webpack", compiler, compilation, loaderContext };
  1843. }
  1844. };
  1845. }
  1846. function normalizeMessage3(error) {
  1847. const err = new Error(typeof error === "string" ? error : error.message);
  1848. if (typeof error === "object") {
  1849. err.stack = error.stack;
  1850. err.cause = error.meta;
  1851. }
  1852. return err;
  1853. }
  1854. // src/webpack/index.ts
  1855. var TRANSFORM_LOADER2 = resolve7(
  1856. __dirname,
  1857. false ? "../../dist/webpack/loaders/transform" : "webpack/loaders/transform"
  1858. );
  1859. var LOAD_LOADER2 = resolve7(
  1860. __dirname,
  1861. false ? "../../dist/webpack/loaders/load" : "webpack/loaders/load"
  1862. );
  1863. function getWebpackPlugin(factory) {
  1864. return (userOptions) => {
  1865. return {
  1866. apply(compiler) {
  1867. const VIRTUAL_MODULE_PREFIX = resolve7(compiler.options.context ?? process3.cwd(), "_virtual_");
  1868. const injected = compiler.$unpluginContext || {};
  1869. compiler.$unpluginContext = injected;
  1870. const meta = {
  1871. framework: "webpack",
  1872. webpack: {
  1873. compiler
  1874. }
  1875. };
  1876. const rawPlugins = toArray(factory(userOptions, meta));
  1877. for (const rawPlugin of rawPlugins) {
  1878. const plugin = Object.assign(
  1879. rawPlugin,
  1880. {
  1881. __unpluginMeta: meta,
  1882. __virtualModulePrefix: VIRTUAL_MODULE_PREFIX
  1883. }
  1884. );
  1885. injected[plugin.name] = plugin;
  1886. compiler.hooks.thisCompilation.tap(plugin.name, (compilation) => {
  1887. compilation.hooks.childCompiler.tap(plugin.name, (childCompiler) => {
  1888. childCompiler.$unpluginContext = injected;
  1889. });
  1890. });
  1891. const externalModules = /* @__PURE__ */ new Set();
  1892. if (plugin.resolveId) {
  1893. let vfs = compiler.options.plugins.find((i) => i instanceof VirtualModulesPlugin);
  1894. if (!vfs) {
  1895. vfs = new VirtualModulesPlugin();
  1896. compiler.options.plugins.push(vfs);
  1897. }
  1898. plugin.__vfsModules = /* @__PURE__ */ new Set();
  1899. plugin.__vfs = vfs;
  1900. const resolverPlugin = {
  1901. apply(resolver) {
  1902. const target = resolver.ensureHook("resolve");
  1903. resolver.getHook("resolve").tapAsync(plugin.name, async (request, resolveContext, callback) => {
  1904. if (!request.request)
  1905. return callback();
  1906. if (normalizeAbsolutePath(request.request).startsWith(plugin.__virtualModulePrefix))
  1907. return callback();
  1908. const id = normalizeAbsolutePath(request.request);
  1909. const requestContext = request.context;
  1910. let importer = requestContext.issuer !== "" ? requestContext.issuer : void 0;
  1911. const isEntry = requestContext.issuer === "";
  1912. if (importer == null ? void 0 : importer.startsWith(plugin.__virtualModulePrefix))
  1913. importer = decodeURIComponent(importer.slice(plugin.__virtualModulePrefix.length));
  1914. const fileDependencies = /* @__PURE__ */ new Set();
  1915. const context = createBuildContext3({
  1916. addWatchFile(file) {
  1917. var _a;
  1918. fileDependencies.add(file);
  1919. (_a = resolveContext.fileDependencies) == null ? void 0 : _a.add(file);
  1920. },
  1921. getWatchFiles() {
  1922. return Array.from(fileDependencies);
  1923. }
  1924. }, compiler);
  1925. let error;
  1926. const pluginContext = {
  1927. error(msg) {
  1928. if (error == null)
  1929. error = normalizeMessage3(msg);
  1930. else
  1931. console.error(`unplugin/webpack: multiple errors returned from resolveId hook: ${msg}`);
  1932. },
  1933. warn(msg) {
  1934. console.warn(`unplugin/webpack: warning from resolveId hook: ${msg}`);
  1935. }
  1936. };
  1937. const resolveIdResult = await plugin.resolveId.call({ ...context, ...pluginContext }, id, importer, { isEntry });
  1938. if (error != null)
  1939. return callback(error);
  1940. if (resolveIdResult == null)
  1941. return callback();
  1942. let resolved = typeof resolveIdResult === "string" ? resolveIdResult : resolveIdResult.id;
  1943. const isExternal = typeof resolveIdResult === "string" ? false : resolveIdResult.external === true;
  1944. if (isExternal)
  1945. externalModules.add(resolved);
  1946. if (!fs5.existsSync(resolved)) {
  1947. resolved = normalizeAbsolutePath(
  1948. plugin.__virtualModulePrefix + encodeURIComponent(resolved)
  1949. // URI encode id so webpack doesn't think it's part of the path
  1950. );
  1951. if (!plugin.__vfsModules.has(resolved)) {
  1952. plugin.__vfs.writeModule(resolved, "");
  1953. plugin.__vfsModules.add(resolved);
  1954. }
  1955. }
  1956. const newRequest = {
  1957. ...request,
  1958. request: resolved
  1959. };
  1960. resolver.doResolve(target, newRequest, null, resolveContext, callback);
  1961. });
  1962. }
  1963. };
  1964. compiler.options.resolve.plugins = compiler.options.resolve.plugins || [];
  1965. compiler.options.resolve.plugins.push(resolverPlugin);
  1966. }
  1967. if (plugin.load) {
  1968. compiler.options.module.rules.unshift({
  1969. include(id) {
  1970. return shouldLoad(id, plugin, externalModules);
  1971. },
  1972. enforce: plugin.enforce,
  1973. use: [{
  1974. loader: LOAD_LOADER2,
  1975. options: {
  1976. unpluginName: plugin.name
  1977. }
  1978. }],
  1979. type: "javascript/auto"
  1980. });
  1981. }
  1982. if (plugin.transform) {
  1983. compiler.options.module.rules.unshift({
  1984. enforce: plugin.enforce,
  1985. use(data) {
  1986. return transformUse(data, plugin, TRANSFORM_LOADER2);
  1987. }
  1988. });
  1989. }
  1990. if (plugin.webpack)
  1991. plugin.webpack(compiler);
  1992. if (plugin.watchChange || plugin.buildStart) {
  1993. compiler.hooks.make.tapPromise(plugin.name, async (compilation) => {
  1994. const context = createBuildContext3(contextOptionsFromCompilation(compilation), compiler, compilation);
  1995. if (plugin.watchChange && (compiler.modifiedFiles || compiler.removedFiles)) {
  1996. const promises = [];
  1997. if (compiler.modifiedFiles) {
  1998. compiler.modifiedFiles.forEach(
  1999. (file) => promises.push(Promise.resolve(plugin.watchChange.call(context, file, { event: "update" })))
  2000. );
  2001. }
  2002. if (compiler.removedFiles) {
  2003. compiler.removedFiles.forEach(
  2004. (file) => promises.push(Promise.resolve(plugin.watchChange.call(context, file, { event: "delete" })))
  2005. );
  2006. }
  2007. await Promise.all(promises);
  2008. }
  2009. if (plugin.buildStart)
  2010. return await plugin.buildStart.call(context);
  2011. });
  2012. }
  2013. if (plugin.buildEnd) {
  2014. compiler.hooks.emit.tapPromise(plugin.name, async (compilation) => {
  2015. await plugin.buildEnd.call(createBuildContext3(contextOptionsFromCompilation(compilation), compiler, compilation));
  2016. });
  2017. }
  2018. if (plugin.writeBundle) {
  2019. compiler.hooks.afterEmit.tapPromise(plugin.name, async () => {
  2020. await plugin.writeBundle();
  2021. });
  2022. }
  2023. }
  2024. }
  2025. };
  2026. };
  2027. }
  2028. // src/define.ts
  2029. function createUnplugin(factory) {
  2030. return {
  2031. get esbuild() {
  2032. return getEsbuildPlugin(factory);
  2033. },
  2034. get rollup() {
  2035. return getRollupPlugin(factory);
  2036. },
  2037. get vite() {
  2038. return getVitePlugin(factory);
  2039. },
  2040. /** @experimental do not use it in production */
  2041. get rolldown() {
  2042. return getRolldownPlugin(factory);
  2043. },
  2044. get webpack() {
  2045. return getWebpackPlugin(factory);
  2046. },
  2047. get rspack() {
  2048. return getRspackPlugin(factory);
  2049. },
  2050. get farm() {
  2051. return getFarmPlugin(factory);
  2052. },
  2053. get raw() {
  2054. return factory;
  2055. }
  2056. };
  2057. }
  2058. function createEsbuildPlugin(factory) {
  2059. return getEsbuildPlugin(factory);
  2060. }
  2061. function createRollupPlugin(factory) {
  2062. return getRollupPlugin(factory);
  2063. }
  2064. function createVitePlugin(factory) {
  2065. return getVitePlugin(factory);
  2066. }
  2067. function createRolldownPlugin(factory) {
  2068. return getRolldownPlugin(factory);
  2069. }
  2070. function createWebpackPlugin(factory) {
  2071. return getWebpackPlugin(factory);
  2072. }
  2073. function createRspackPlugin(factory) {
  2074. return getRspackPlugin(factory);
  2075. }
  2076. function createFarmPlugin(factory) {
  2077. return getFarmPlugin(factory);
  2078. }
  2079. export {
  2080. createEsbuildPlugin,
  2081. createFarmPlugin,
  2082. createRolldownPlugin,
  2083. createRollupPlugin,
  2084. createRspackPlugin,
  2085. createUnplugin,
  2086. createVitePlugin,
  2087. createWebpackPlugin
  2088. };