index.mjs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. // src/esbuild/index.ts
  2. import fs from "fs";
  3. import path from "path";
  4. import chokidar from "chokidar";
  5. import { Parser } from "acorn";
  6. // src/esbuild/utils.ts
  7. import { extname } from "path";
  8. // node_modules/.pnpm/@jridgewell+sourcemap-codec@1.4.11/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
  9. var comma = ",".charCodeAt(0);
  10. var semicolon = ";".charCodeAt(0);
  11. var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  12. var intToChar = new Uint8Array(64);
  13. var charToInteger = new Uint8Array(128);
  14. for (let i = 0; i < chars.length; i++) {
  15. const c = chars.charCodeAt(i);
  16. charToInteger[c] = i;
  17. intToChar[i] = c;
  18. }
  19. var td = typeof TextDecoder !== "undefined" ? new TextDecoder() : typeof Buffer !== "undefined" ? {
  20. decode(buf) {
  21. const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  22. return out.toString();
  23. }
  24. } : {
  25. decode(buf) {
  26. let out = "";
  27. for (let i = 0; i < buf.length; i++) {
  28. out += String.fromCharCode(buf[i]);
  29. }
  30. return out;
  31. }
  32. };
  33. function decode(mappings) {
  34. const state = new Int32Array(5);
  35. const decoded = [];
  36. let line = [];
  37. let sorted = true;
  38. let lastCol = 0;
  39. for (let i = 0; i < mappings.length; ) {
  40. const c = mappings.charCodeAt(i);
  41. if (c === comma) {
  42. i++;
  43. } else if (c === semicolon) {
  44. state[0] = lastCol = 0;
  45. if (!sorted)
  46. sort(line);
  47. sorted = true;
  48. decoded.push(line);
  49. line = [];
  50. i++;
  51. } else {
  52. i = decodeInteger(mappings, i, state, 0);
  53. const col = state[0];
  54. if (col < lastCol)
  55. sorted = false;
  56. lastCol = col;
  57. if (!hasMoreSegments(mappings, i)) {
  58. line.push([col]);
  59. continue;
  60. }
  61. i = decodeInteger(mappings, i, state, 1);
  62. i = decodeInteger(mappings, i, state, 2);
  63. i = decodeInteger(mappings, i, state, 3);
  64. if (!hasMoreSegments(mappings, i)) {
  65. line.push([col, state[1], state[2], state[3]]);
  66. continue;
  67. }
  68. i = decodeInteger(mappings, i, state, 4);
  69. line.push([col, state[1], state[2], state[3], state[4]]);
  70. }
  71. }
  72. if (!sorted)
  73. sort(line);
  74. decoded.push(line);
  75. return decoded;
  76. }
  77. function decodeInteger(mappings, pos, state, j) {
  78. let value = 0;
  79. let shift = 0;
  80. let integer = 0;
  81. do {
  82. const c = mappings.charCodeAt(pos++);
  83. integer = charToInteger[c];
  84. value |= (integer & 31) << shift;
  85. shift += 5;
  86. } while (integer & 32);
  87. const shouldNegate = value & 1;
  88. value >>>= 1;
  89. if (shouldNegate) {
  90. value = -2147483648 | -value;
  91. }
  92. state[j] += value;
  93. return pos;
  94. }
  95. function hasMoreSegments(mappings, i) {
  96. if (i >= mappings.length)
  97. return false;
  98. const c = mappings.charCodeAt(i);
  99. if (c === comma || c === semicolon)
  100. return false;
  101. return true;
  102. }
  103. function sort(line) {
  104. line.sort(sortComparator);
  105. }
  106. function sortComparator(a, b) {
  107. return a[0] - b[0];
  108. }
  109. function encode(decoded) {
  110. const state = new Int32Array(5);
  111. let buf = new Uint8Array(1024);
  112. let pos = 0;
  113. for (let i = 0; i < decoded.length; i++) {
  114. const line = decoded[i];
  115. if (i > 0) {
  116. buf = reserve(buf, pos, 1);
  117. buf[pos++] = semicolon;
  118. }
  119. if (line.length === 0)
  120. continue;
  121. state[0] = 0;
  122. for (let j = 0; j < line.length; j++) {
  123. const segment = line[j];
  124. buf = reserve(buf, pos, 36);
  125. if (j > 0)
  126. buf[pos++] = comma;
  127. pos = encodeInteger(buf, pos, state, segment, 0);
  128. if (segment.length === 1)
  129. continue;
  130. pos = encodeInteger(buf, pos, state, segment, 1);
  131. pos = encodeInteger(buf, pos, state, segment, 2);
  132. pos = encodeInteger(buf, pos, state, segment, 3);
  133. if (segment.length === 4)
  134. continue;
  135. pos = encodeInteger(buf, pos, state, segment, 4);
  136. }
  137. }
  138. return td.decode(buf.subarray(0, pos));
  139. }
  140. function reserve(buf, pos, count) {
  141. if (buf.length > pos + count)
  142. return buf;
  143. const swap = new Uint8Array(buf.length * 2);
  144. swap.set(buf);
  145. return swap;
  146. }
  147. function encodeInteger(buf, pos, state, segment, j) {
  148. const next = segment[j];
  149. let num = next - state[j];
  150. state[j] = next;
  151. num = num < 0 ? -num << 1 | 1 : num << 1;
  152. do {
  153. let clamped = num & 31;
  154. num >>>= 5;
  155. if (num > 0)
  156. clamped |= 32;
  157. buf[pos++] = intToChar[clamped];
  158. } while (num > 0);
  159. return pos;
  160. }
  161. // node_modules/.pnpm/@jridgewell+resolve-uri@3.0.5/node_modules/@jridgewell/resolve-uri/dist/resolve-uri.mjs
  162. var schemeRegex = /^[\w+.-]+:\/\//;
  163. var urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?/;
  164. function isAbsoluteUrl(input) {
  165. return schemeRegex.test(input);
  166. }
  167. function isSchemeRelativeUrl(input) {
  168. return input.startsWith("//");
  169. }
  170. function isAbsolutePath(input) {
  171. return input.startsWith("/");
  172. }
  173. function parseAbsoluteUrl(input) {
  174. const match = urlRegex.exec(input);
  175. return {
  176. scheme: match[1],
  177. user: match[2] || "",
  178. host: match[3],
  179. port: match[4] || "",
  180. path: match[5] || "/",
  181. relativePath: false
  182. };
  183. }
  184. function parseUrl(input) {
  185. if (isSchemeRelativeUrl(input)) {
  186. const url = parseAbsoluteUrl("http:" + input);
  187. url.scheme = "";
  188. return url;
  189. }
  190. if (isAbsolutePath(input)) {
  191. const url = parseAbsoluteUrl("http://foo.com" + input);
  192. url.scheme = "";
  193. url.host = "";
  194. return url;
  195. }
  196. if (!isAbsoluteUrl(input)) {
  197. const url = parseAbsoluteUrl("http://foo.com/" + input);
  198. url.scheme = "";
  199. url.host = "";
  200. url.relativePath = true;
  201. return url;
  202. }
  203. return parseAbsoluteUrl(input);
  204. }
  205. function stripPathFilename(path2) {
  206. if (path2.endsWith("/.."))
  207. return path2;
  208. const index = path2.lastIndexOf("/");
  209. return path2.slice(0, index + 1);
  210. }
  211. function mergePaths(url, base) {
  212. if (!url.relativePath)
  213. return;
  214. normalizePath(base);
  215. if (url.path === "/") {
  216. url.path = base.path;
  217. } else {
  218. url.path = stripPathFilename(base.path) + url.path;
  219. }
  220. url.relativePath = base.relativePath;
  221. }
  222. function normalizePath(url) {
  223. const { relativePath } = url;
  224. const pieces = url.path.split("/");
  225. let pointer = 1;
  226. let positive = 0;
  227. let addTrailingSlash = false;
  228. for (let i = 1; i < pieces.length; i++) {
  229. const piece = pieces[i];
  230. if (!piece) {
  231. addTrailingSlash = true;
  232. continue;
  233. }
  234. addTrailingSlash = false;
  235. if (piece === ".")
  236. continue;
  237. if (piece === "..") {
  238. if (positive) {
  239. addTrailingSlash = true;
  240. positive--;
  241. pointer--;
  242. } else if (relativePath) {
  243. pieces[pointer++] = piece;
  244. }
  245. continue;
  246. }
  247. pieces[pointer++] = piece;
  248. positive++;
  249. }
  250. let path2 = "";
  251. for (let i = 1; i < pointer; i++) {
  252. path2 += "/" + pieces[i];
  253. }
  254. if (!path2 || addTrailingSlash && !path2.endsWith("/..")) {
  255. path2 += "/";
  256. }
  257. url.path = path2;
  258. }
  259. function resolve(input, base) {
  260. if (!input && !base)
  261. return "";
  262. const url = parseUrl(input);
  263. if (base && !url.scheme) {
  264. const baseUrl = parseUrl(base);
  265. url.scheme = baseUrl.scheme;
  266. if (!url.host || baseUrl.scheme === "file:") {
  267. url.user = baseUrl.user;
  268. url.host = baseUrl.host;
  269. url.port = baseUrl.port;
  270. }
  271. mergePaths(url, baseUrl);
  272. }
  273. normalizePath(url);
  274. if (url.relativePath) {
  275. const path2 = url.path.slice(1);
  276. if (!path2)
  277. return ".";
  278. const keepRelative = (base || input).startsWith(".");
  279. return !keepRelative || path2.startsWith(".") ? path2 : "./" + path2;
  280. }
  281. if (!url.scheme && !url.host)
  282. return url.path;
  283. return `${url.scheme}//${url.user}${url.host}${url.port}${url.path}`;
  284. }
  285. // node_modules/.pnpm/@jridgewell+trace-mapping@0.3.9/node_modules/@jridgewell/trace-mapping/dist/trace-mapping.mjs
  286. function resolve2(input, base) {
  287. if (base && !base.endsWith("/"))
  288. base += "/";
  289. return resolve(input, base);
  290. }
  291. function stripFilename(path2) {
  292. if (!path2)
  293. return "";
  294. const index = path2.lastIndexOf("/");
  295. return path2.slice(0, index + 1);
  296. }
  297. var COLUMN = 0;
  298. var SOURCES_INDEX = 1;
  299. var SOURCE_LINE = 2;
  300. var SOURCE_COLUMN = 3;
  301. var NAMES_INDEX = 4;
  302. var REV_GENERATED_LINE = 1;
  303. var REV_GENERATED_COLUMN = 2;
  304. function maybeSort(mappings, owned) {
  305. const unsortedIndex = nextUnsortedSegmentLine(mappings, 0);
  306. if (unsortedIndex === mappings.length)
  307. return mappings;
  308. if (!owned)
  309. mappings = mappings.slice();
  310. for (let i = unsortedIndex; i < mappings.length; i = nextUnsortedSegmentLine(mappings, i + 1)) {
  311. mappings[i] = sortSegments(mappings[i], owned);
  312. }
  313. return mappings;
  314. }
  315. function nextUnsortedSegmentLine(mappings, start) {
  316. for (let i = start; i < mappings.length; i++) {
  317. if (!isSorted(mappings[i]))
  318. return i;
  319. }
  320. return mappings.length;
  321. }
  322. function isSorted(line) {
  323. for (let j = 1; j < line.length; j++) {
  324. if (line[j][COLUMN] < line[j - 1][COLUMN]) {
  325. return false;
  326. }
  327. }
  328. return true;
  329. }
  330. function sortSegments(line, owned) {
  331. if (!owned)
  332. line = line.slice();
  333. return line.sort(sortComparator2);
  334. }
  335. function sortComparator2(a, b) {
  336. return a[COLUMN] - b[COLUMN];
  337. }
  338. var found = false;
  339. function binarySearch(haystack, needle, low, high) {
  340. while (low <= high) {
  341. const mid = low + (high - low >> 1);
  342. const cmp = haystack[mid][COLUMN] - needle;
  343. if (cmp === 0) {
  344. found = true;
  345. return mid;
  346. }
  347. if (cmp < 0) {
  348. low = mid + 1;
  349. } else {
  350. high = mid - 1;
  351. }
  352. }
  353. found = false;
  354. return low - 1;
  355. }
  356. function upperBound(haystack, needle, index) {
  357. for (let i = index + 1; i < haystack.length; i++, index++) {
  358. if (haystack[i][COLUMN] !== needle)
  359. break;
  360. }
  361. return index;
  362. }
  363. function lowerBound(haystack, needle, index) {
  364. for (let i = index - 1; i >= 0; i--, index--) {
  365. if (haystack[i][COLUMN] !== needle)
  366. break;
  367. }
  368. return index;
  369. }
  370. function memoizedState() {
  371. return {
  372. lastKey: -1,
  373. lastNeedle: -1,
  374. lastIndex: -1
  375. };
  376. }
  377. function memoizedBinarySearch(haystack, needle, state, key) {
  378. const { lastKey, lastNeedle, lastIndex } = state;
  379. let low = 0;
  380. let high = haystack.length - 1;
  381. if (key === lastKey) {
  382. if (needle === lastNeedle) {
  383. found = lastIndex !== -1 && haystack[lastIndex][COLUMN] === needle;
  384. return lastIndex;
  385. }
  386. if (needle >= lastNeedle) {
  387. low = lastIndex === -1 ? 0 : lastIndex;
  388. } else {
  389. high = lastIndex;
  390. }
  391. }
  392. state.lastKey = key;
  393. state.lastNeedle = needle;
  394. return state.lastIndex = binarySearch(haystack, needle, low, high);
  395. }
  396. function buildBySources(decoded, memos) {
  397. const sources2 = memos.map(buildNullArray);
  398. for (let i = 0; i < decoded.length; i++) {
  399. const line = decoded[i];
  400. for (let j = 0; j < line.length; j++) {
  401. const seg = line[j];
  402. if (seg.length === 1)
  403. continue;
  404. const sourceIndex = seg[SOURCES_INDEX];
  405. const sourceLine = seg[SOURCE_LINE];
  406. const sourceColumn = seg[SOURCE_COLUMN];
  407. const originalSource = sources2[sourceIndex];
  408. const originalLine = originalSource[sourceLine] || (originalSource[sourceLine] = []);
  409. const memo = memos[sourceIndex];
  410. const index = upperBound(originalLine, sourceColumn, memoizedBinarySearch(originalLine, sourceColumn, memo, sourceLine));
  411. insert(originalLine, memo.lastIndex = index + 1, [sourceColumn, i, seg[COLUMN]]);
  412. }
  413. }
  414. return sources2;
  415. }
  416. function insert(array, index, value) {
  417. for (let i = array.length; i > index; i--) {
  418. array[i] = array[i - 1];
  419. }
  420. array[index] = value;
  421. }
  422. function buildNullArray() {
  423. return { __proto__: null };
  424. }
  425. var INVALID_ORIGINAL_MAPPING = Object.freeze({
  426. source: null,
  427. line: null,
  428. column: null,
  429. name: null
  430. });
  431. var INVALID_GENERATED_MAPPING = Object.freeze({
  432. line: null,
  433. column: null
  434. });
  435. var LINE_GTR_ZERO = "`line` must be greater than 0 (lines start at line 1)";
  436. var COL_GTR_EQ_ZERO = "`column` must be greater than or equal to 0 (columns start at column 0)";
  437. var LEAST_UPPER_BOUND = -1;
  438. var GREATEST_LOWER_BOUND = 1;
  439. var encodedMappings;
  440. var decodedMappings;
  441. var traceSegment;
  442. var originalPositionFor;
  443. var generatedPositionFor;
  444. var eachMapping;
  445. var presortedDecodedMap;
  446. var decodedMap;
  447. var encodedMap;
  448. var TraceMap = class {
  449. constructor(map, mapUrl) {
  450. this._decodedMemo = memoizedState();
  451. this._bySources = void 0;
  452. this._bySourceMemos = void 0;
  453. const isString = typeof map === "string";
  454. if (!isString && map.constructor === TraceMap)
  455. return map;
  456. const parsed = isString ? JSON.parse(map) : map;
  457. const { version, file, names, sourceRoot, sources: sources2, sourcesContent } = parsed;
  458. this.version = version;
  459. this.file = file;
  460. this.names = names;
  461. this.sourceRoot = sourceRoot;
  462. this.sources = sources2;
  463. this.sourcesContent = sourcesContent;
  464. if (sourceRoot || mapUrl) {
  465. const from = resolve2(sourceRoot || "", stripFilename(mapUrl));
  466. this.resolvedSources = sources2.map((s) => resolve2(s || "", from));
  467. } else {
  468. this.resolvedSources = sources2.map((s) => s || "");
  469. }
  470. const { mappings } = parsed;
  471. if (typeof mappings === "string") {
  472. this._encoded = mappings;
  473. this._decoded = void 0;
  474. } else {
  475. this._encoded = void 0;
  476. this._decoded = maybeSort(mappings, isString);
  477. }
  478. }
  479. };
  480. (() => {
  481. encodedMappings = (map) => {
  482. var _a;
  483. return (_a = map._encoded) !== null && _a !== void 0 ? _a : map._encoded = encode(map._decoded);
  484. };
  485. decodedMappings = (map) => {
  486. return map._decoded || (map._decoded = decode(map._encoded));
  487. };
  488. traceSegment = (map, line, column) => {
  489. const decoded = decodedMappings(map);
  490. if (line >= decoded.length)
  491. return null;
  492. return traceSegmentInternal(decoded[line], map._decodedMemo, line, column, GREATEST_LOWER_BOUND);
  493. };
  494. originalPositionFor = (map, { line, column, bias }) => {
  495. line--;
  496. if (line < 0)
  497. throw new Error(LINE_GTR_ZERO);
  498. if (column < 0)
  499. throw new Error(COL_GTR_EQ_ZERO);
  500. const decoded = decodedMappings(map);
  501. if (line >= decoded.length)
  502. return INVALID_ORIGINAL_MAPPING;
  503. const segment = traceSegmentInternal(decoded[line], map._decodedMemo, line, column, bias || GREATEST_LOWER_BOUND);
  504. if (segment == null)
  505. return INVALID_ORIGINAL_MAPPING;
  506. if (segment.length == 1)
  507. return INVALID_ORIGINAL_MAPPING;
  508. const { names, resolvedSources } = map;
  509. return {
  510. source: resolvedSources[segment[SOURCES_INDEX]],
  511. line: segment[SOURCE_LINE] + 1,
  512. column: segment[SOURCE_COLUMN],
  513. name: segment.length === 5 ? names[segment[NAMES_INDEX]] : null
  514. };
  515. };
  516. generatedPositionFor = (map, { source, line, column, bias }) => {
  517. line--;
  518. if (line < 0)
  519. throw new Error(LINE_GTR_ZERO);
  520. if (column < 0)
  521. throw new Error(COL_GTR_EQ_ZERO);
  522. const { sources: sources2, resolvedSources } = map;
  523. let sourceIndex = sources2.indexOf(source);
  524. if (sourceIndex === -1)
  525. sourceIndex = resolvedSources.indexOf(source);
  526. if (sourceIndex === -1)
  527. return INVALID_GENERATED_MAPPING;
  528. const generated = map._bySources || (map._bySources = buildBySources(decodedMappings(map), map._bySourceMemos = sources2.map(memoizedState)));
  529. const memos = map._bySourceMemos;
  530. const segments = generated[sourceIndex][line];
  531. if (segments == null)
  532. return INVALID_GENERATED_MAPPING;
  533. const segment = traceSegmentInternal(segments, memos[sourceIndex], line, column, bias || GREATEST_LOWER_BOUND);
  534. if (segment == null)
  535. return INVALID_GENERATED_MAPPING;
  536. return {
  537. line: segment[REV_GENERATED_LINE] + 1,
  538. column: segment[REV_GENERATED_COLUMN]
  539. };
  540. };
  541. eachMapping = (map, cb) => {
  542. const decoded = decodedMappings(map);
  543. const { names, resolvedSources } = map;
  544. for (let i = 0; i < decoded.length; i++) {
  545. const line = decoded[i];
  546. for (let j = 0; j < line.length; j++) {
  547. const seg = line[j];
  548. const generatedLine = i + 1;
  549. const generatedColumn = seg[0];
  550. let source = null;
  551. let originalLine = null;
  552. let originalColumn = null;
  553. let name = null;
  554. if (seg.length !== 1) {
  555. source = resolvedSources[seg[1]];
  556. originalLine = seg[2] + 1;
  557. originalColumn = seg[3];
  558. }
  559. if (seg.length === 5)
  560. name = names[seg[4]];
  561. cb({
  562. generatedLine,
  563. generatedColumn,
  564. source,
  565. originalLine,
  566. originalColumn,
  567. name
  568. });
  569. }
  570. }
  571. };
  572. presortedDecodedMap = (map, mapUrl) => {
  573. const clone = Object.assign({}, map);
  574. clone.mappings = [];
  575. const tracer = new TraceMap(clone, mapUrl);
  576. tracer._decoded = map.mappings;
  577. return tracer;
  578. };
  579. decodedMap = (map) => {
  580. return {
  581. version: 3,
  582. file: map.file,
  583. names: map.names,
  584. sourceRoot: map.sourceRoot,
  585. sources: map.sources,
  586. sourcesContent: map.sourcesContent,
  587. mappings: decodedMappings(map)
  588. };
  589. };
  590. encodedMap = (map) => {
  591. return {
  592. version: 3,
  593. file: map.file,
  594. names: map.names,
  595. sourceRoot: map.sourceRoot,
  596. sources: map.sources,
  597. sourcesContent: map.sourcesContent,
  598. mappings: encodedMappings(map)
  599. };
  600. };
  601. })();
  602. function traceSegmentInternal(segments, memo, line, column, bias) {
  603. let index = memoizedBinarySearch(segments, column, memo, line);
  604. if (found) {
  605. index = (bias === LEAST_UPPER_BOUND ? upperBound : lowerBound)(segments, column, index);
  606. } else if (bias === LEAST_UPPER_BOUND)
  607. index++;
  608. if (index === -1 || index === segments.length)
  609. return null;
  610. return segments[index];
  611. }
  612. // node_modules/.pnpm/@jridgewell+set-array@1.1.0/node_modules/@jridgewell/set-array/dist/set-array.mjs
  613. var get;
  614. var put;
  615. var pop;
  616. var SetArray = class {
  617. constructor() {
  618. this._indexes = { __proto__: null };
  619. this.array = [];
  620. }
  621. };
  622. (() => {
  623. get = (strarr, key) => strarr._indexes[key];
  624. put = (strarr, key) => {
  625. const index = get(strarr, key);
  626. if (index !== void 0)
  627. return index;
  628. const { array, _indexes: indexes } = strarr;
  629. return indexes[key] = array.push(key) - 1;
  630. };
  631. pop = (strarr) => {
  632. const { array, _indexes: indexes } = strarr;
  633. if (array.length === 0)
  634. return;
  635. const last = array.pop();
  636. indexes[last] = void 0;
  637. };
  638. })();
  639. // node_modules/.pnpm/@jridgewell+gen-mapping@0.1.1/node_modules/@jridgewell/gen-mapping/dist/gen-mapping.mjs
  640. var addSegment;
  641. var addMapping;
  642. var setSourceContent;
  643. var decodedMap2;
  644. var encodedMap2;
  645. var allMappings;
  646. var GenMapping = class {
  647. constructor({ file, sourceRoot } = {}) {
  648. this._names = new SetArray();
  649. this._sources = new SetArray();
  650. this._sourcesContent = [];
  651. this._mappings = [];
  652. this.file = file;
  653. this.sourceRoot = sourceRoot;
  654. }
  655. };
  656. (() => {
  657. addSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name) => {
  658. const { _mappings: mappings, _sources: sources2, _sourcesContent: sourcesContent, _names: names } = map;
  659. const line = getLine(mappings, genLine);
  660. if (source == null) {
  661. const seg2 = [genColumn];
  662. const index2 = getColumnIndex(line, genColumn, seg2);
  663. return insert2(line, index2, seg2);
  664. }
  665. const sourcesIndex = put(sources2, source);
  666. const seg = name ? [genColumn, sourcesIndex, sourceLine, sourceColumn, put(names, name)] : [genColumn, sourcesIndex, sourceLine, sourceColumn];
  667. const index = getColumnIndex(line, genColumn, seg);
  668. if (sourcesIndex === sourcesContent.length)
  669. sourcesContent[sourcesIndex] = null;
  670. insert2(line, index, seg);
  671. };
  672. addMapping = (map, mapping) => {
  673. const { generated, source, original, name } = mapping;
  674. return addSegment(map, generated.line - 1, generated.column, source, original == null ? void 0 : original.line - 1, original === null || original === void 0 ? void 0 : original.column, name);
  675. };
  676. setSourceContent = (map, source, content) => {
  677. const { _sources: sources2, _sourcesContent: sourcesContent } = map;
  678. sourcesContent[put(sources2, source)] = content;
  679. };
  680. decodedMap2 = (map) => {
  681. const { file, sourceRoot, _mappings: mappings, _sources: sources2, _sourcesContent: sourcesContent, _names: names } = map;
  682. return {
  683. version: 3,
  684. file,
  685. names: names.array,
  686. sourceRoot: sourceRoot || void 0,
  687. sources: sources2.array,
  688. sourcesContent,
  689. mappings
  690. };
  691. };
  692. encodedMap2 = (map) => {
  693. const decoded = decodedMap2(map);
  694. return Object.assign(Object.assign({}, decoded), { mappings: encode(decoded.mappings) });
  695. };
  696. allMappings = (map) => {
  697. const out = [];
  698. const { _mappings: mappings, _sources: sources2, _names: names } = map;
  699. for (let i = 0; i < mappings.length; i++) {
  700. const line = mappings[i];
  701. for (let j = 0; j < line.length; j++) {
  702. const seg = line[j];
  703. const generated = { line: i + 1, column: seg[0] };
  704. let source = void 0;
  705. let original = void 0;
  706. let name = void 0;
  707. if (seg.length !== 1) {
  708. source = sources2.array[seg[1]];
  709. original = { line: seg[2] + 1, column: seg[3] };
  710. if (seg.length === 5)
  711. name = names.array[seg[4]];
  712. }
  713. out.push({ generated, source, original, name });
  714. }
  715. }
  716. return out;
  717. };
  718. })();
  719. function getLine(mappings, index) {
  720. for (let i = mappings.length; i <= index; i++) {
  721. mappings[i] = [];
  722. }
  723. return mappings[index];
  724. }
  725. function getColumnIndex(line, column, seg) {
  726. let index = line.length;
  727. for (let i = index - 1; i >= 0; i--, index--) {
  728. const current = line[i];
  729. const col = current[0];
  730. if (col > column)
  731. continue;
  732. if (col < column)
  733. break;
  734. const cmp = compare(current, seg);
  735. if (cmp === 0)
  736. return index;
  737. if (cmp < 0)
  738. break;
  739. }
  740. return index;
  741. }
  742. function compare(a, b) {
  743. let cmp = compareNum(a.length, b.length);
  744. if (cmp !== 0)
  745. return cmp;
  746. if (a.length === 1)
  747. return 0;
  748. cmp = compareNum(a[1], b[1]);
  749. if (cmp !== 0)
  750. return cmp;
  751. cmp = compareNum(a[2], b[2]);
  752. if (cmp !== 0)
  753. return cmp;
  754. cmp = compareNum(a[3], b[3]);
  755. if (cmp !== 0)
  756. return cmp;
  757. if (a.length === 4)
  758. return 0;
  759. return compareNum(a[4], b[4]);
  760. }
  761. function compareNum(a, b) {
  762. return a - b;
  763. }
  764. function insert2(array, index, value) {
  765. if (index === -1)
  766. return;
  767. for (let i = array.length; i > index; i--) {
  768. array[i] = array[i - 1];
  769. }
  770. array[index] = value;
  771. }
  772. // node_modules/.pnpm/@ampproject+remapping@2.2.0/node_modules/@ampproject/remapping/dist/remapping.mjs
  773. var SOURCELESS_MAPPING = {
  774. source: null,
  775. column: null,
  776. line: null,
  777. name: null,
  778. content: null
  779. };
  780. var EMPTY_SOURCES = [];
  781. function Source(map, sources2, source, content) {
  782. return {
  783. map,
  784. sources: sources2,
  785. source,
  786. content
  787. };
  788. }
  789. function MapSource(map, sources2) {
  790. return Source(map, sources2, "", null);
  791. }
  792. function OriginalSource(source, content) {
  793. return Source(null, EMPTY_SOURCES, source, content);
  794. }
  795. function traceMappings(tree) {
  796. const gen = new GenMapping({ file: tree.map.file });
  797. const { sources: rootSources, map } = tree;
  798. const rootNames = map.names;
  799. const rootMappings = decodedMappings(map);
  800. for (let i = 0; i < rootMappings.length; i++) {
  801. const segments = rootMappings[i];
  802. let lastSource = null;
  803. let lastSourceLine = null;
  804. let lastSourceColumn = null;
  805. for (let j = 0; j < segments.length; j++) {
  806. const segment = segments[j];
  807. const genCol = segment[0];
  808. let traced = SOURCELESS_MAPPING;
  809. if (segment.length !== 1) {
  810. const source2 = rootSources[segment[1]];
  811. traced = originalPositionFor2(source2, segment[2], segment[3], segment.length === 5 ? rootNames[segment[4]] : "");
  812. if (traced == null)
  813. continue;
  814. }
  815. const { column, line, name, content, source } = traced;
  816. if (line === lastSourceLine && column === lastSourceColumn && source === lastSource) {
  817. continue;
  818. }
  819. lastSourceLine = line;
  820. lastSourceColumn = column;
  821. lastSource = source;
  822. addSegment(gen, i, genCol, source, line, column, name);
  823. if (content != null)
  824. setSourceContent(gen, source, content);
  825. }
  826. }
  827. return gen;
  828. }
  829. function originalPositionFor2(source, line, column, name) {
  830. if (!source.map) {
  831. return { column, line, name, source: source.source, content: source.content };
  832. }
  833. const segment = traceSegment(source.map, line, column);
  834. if (segment == null)
  835. return null;
  836. if (segment.length === 1)
  837. return SOURCELESS_MAPPING;
  838. return originalPositionFor2(source.sources[segment[1]], segment[2], segment[3], segment.length === 5 ? source.map.names[segment[4]] : name);
  839. }
  840. function asArray(value) {
  841. if (Array.isArray(value))
  842. return value;
  843. return [value];
  844. }
  845. function buildSourceMapTree(input, loader) {
  846. const maps = asArray(input).map((m) => new TraceMap(m, ""));
  847. const map = maps.pop();
  848. for (let i = 0; i < maps.length; i++) {
  849. if (maps[i].sources.length > 1) {
  850. throw new Error(`Transformation map ${i} must have exactly one source file.
  851. Did you specify these with the most recent transformation maps first?`);
  852. }
  853. }
  854. let tree = build(map, loader, "", 0);
  855. for (let i = maps.length - 1; i >= 0; i--) {
  856. tree = MapSource(maps[i], [tree]);
  857. }
  858. return tree;
  859. }
  860. function build(map, loader, importer, importerDepth) {
  861. const { resolvedSources, sourcesContent } = map;
  862. const depth = importerDepth + 1;
  863. const children = resolvedSources.map((sourceFile, i) => {
  864. const ctx = {
  865. importer,
  866. depth,
  867. source: sourceFile || "",
  868. content: void 0
  869. };
  870. const sourceMap = loader(ctx.source, ctx);
  871. const { source, content } = ctx;
  872. if (sourceMap)
  873. return build(new TraceMap(sourceMap, source), loader, source, depth);
  874. const sourceContent = content !== void 0 ? content : sourcesContent ? sourcesContent[i] : null;
  875. return OriginalSource(source, sourceContent);
  876. });
  877. return MapSource(map, children);
  878. }
  879. var SourceMap = class {
  880. constructor(map, options) {
  881. const out = options.decodedMappings ? decodedMap2(map) : encodedMap2(map);
  882. this.version = out.version;
  883. this.file = out.file;
  884. this.mappings = out.mappings;
  885. this.names = out.names;
  886. this.sourceRoot = out.sourceRoot;
  887. this.sources = out.sources;
  888. if (!options.excludeContent) {
  889. this.sourcesContent = out.sourcesContent;
  890. }
  891. }
  892. toString() {
  893. return JSON.stringify(this);
  894. }
  895. };
  896. function remapping(input, loader, options) {
  897. const opts = typeof options === "object" ? options : { excludeContent: !!options, decodedMappings: false };
  898. const tree = buildSourceMapTree(input, loader);
  899. return new SourceMap(traceMappings(tree), opts);
  900. }
  901. // src/esbuild/utils.ts
  902. var ExtToLoader = {
  903. ".js": "js",
  904. ".mjs": "js",
  905. ".cjs": "js",
  906. ".jsx": "jsx",
  907. ".ts": "ts",
  908. ".cts": "ts",
  909. ".mts": "ts",
  910. ".tsx": "tsx",
  911. ".css": "css",
  912. ".less": "css",
  913. ".stylus": "css",
  914. ".scss": "css",
  915. ".sass": "css",
  916. ".json": "json",
  917. ".txt": "text"
  918. };
  919. function guessLoader(id) {
  920. return ExtToLoader[extname(id).toLowerCase()] || "js";
  921. }
  922. function fixSourceMap(map) {
  923. if (!("toString" in map)) {
  924. Object.defineProperty(map, "toString", {
  925. enumerable: false,
  926. value: function toString() {
  927. return JSON.stringify(this);
  928. }
  929. });
  930. }
  931. if (!("toUrl" in map)) {
  932. Object.defineProperty(map, "toUrl", {
  933. enumerable: false,
  934. value: function toUrl() {
  935. return "data:application/json;charset=utf-8;base64," + Buffer.from(this.toString()).toString("base64");
  936. }
  937. });
  938. }
  939. return map;
  940. }
  941. var nullSourceMap = {
  942. names: [],
  943. sources: [],
  944. mappings: "",
  945. version: 3
  946. };
  947. function combineSourcemaps(filename, sourcemapList) {
  948. sourcemapList = sourcemapList.filter((m) => m.sources);
  949. if (sourcemapList.length === 0 || sourcemapList.every((m) => m.sources.length === 0)) {
  950. return { ...nullSourceMap };
  951. }
  952. let map;
  953. let mapIndex = 1;
  954. const useArrayInterface = sourcemapList.slice(0, -1).find((m) => m.sources.length !== 1) === void 0;
  955. if (useArrayInterface) {
  956. map = remapping(sourcemapList, () => null, true);
  957. } else {
  958. map = remapping(
  959. sourcemapList[0],
  960. function loader(sourcefile) {
  961. if (sourcefile === filename && sourcemapList[mapIndex]) {
  962. return sourcemapList[mapIndex++];
  963. } else {
  964. return { ...nullSourceMap };
  965. }
  966. },
  967. true
  968. );
  969. }
  970. if (!map.file) {
  971. delete map.file;
  972. }
  973. return map;
  974. }
  975. // src/esbuild/index.ts
  976. var watchListRecord = {};
  977. var watchList = /* @__PURE__ */ new Set();
  978. function getEsbuildPlugin(factory) {
  979. return (userOptions) => {
  980. var _a;
  981. const meta = {
  982. framework: "esbuild"
  983. };
  984. const plugin = factory(userOptions, meta);
  985. return {
  986. name: plugin.name,
  987. setup: ((_a = plugin.esbuild) == null ? void 0 : _a.setup) ?? function setup({ onStart, onEnd, onResolve, onLoad, initialOptions, esbuild: { build: build2 } }) {
  988. var _a2, _b;
  989. const onResolveFilter = ((_a2 = plugin.esbuild) == null ? void 0 : _a2.onResolveFilter) ?? /.*/;
  990. const onLoadFilter = ((_b = plugin.esbuild) == null ? void 0 : _b.onLoadFilter) ?? /.*/;
  991. const context = {
  992. parse(code, opts = {}) {
  993. return Parser.parse(code, {
  994. sourceType: "module",
  995. ecmaVersion: "latest",
  996. locations: true,
  997. ...opts
  998. });
  999. },
  1000. addWatchFile(id) {
  1001. watchList.add(path.resolve(id));
  1002. },
  1003. emitFile(emittedFile) {
  1004. const outFileName = emittedFile.fileName || emittedFile.name;
  1005. if (initialOptions.outdir && emittedFile.source && outFileName) {
  1006. fs.writeFileSync(path.resolve(initialOptions.outdir, outFileName), emittedFile.source);
  1007. }
  1008. },
  1009. getWatchFiles() {
  1010. return Array.from(watchList);
  1011. }
  1012. };
  1013. if (initialOptions.outdir && !fs.existsSync(initialOptions.outdir)) {
  1014. fs.mkdirSync(initialOptions.outdir, { recursive: true });
  1015. }
  1016. if (plugin.buildStart) {
  1017. onStart(() => plugin.buildStart.call(context));
  1018. }
  1019. if (plugin.buildEnd || initialOptions.watch) {
  1020. const rebuild = () => build2({
  1021. ...initialOptions,
  1022. watch: false
  1023. });
  1024. onEnd(async () => {
  1025. await plugin.buildEnd.call(context);
  1026. if (initialOptions.watch) {
  1027. Object.keys(watchListRecord).forEach((id) => {
  1028. if (!watchList.has(id)) {
  1029. watchListRecord[id].close();
  1030. delete watchListRecord[id];
  1031. }
  1032. });
  1033. watchList.forEach((id) => {
  1034. if (!Object.keys(watchListRecord).includes(id)) {
  1035. watchListRecord[id] = chokidar.watch(id);
  1036. watchListRecord[id].on("change", async () => {
  1037. var _a3;
  1038. await ((_a3 = plugin.watchChange) == null ? void 0 : _a3.call(context, id, { event: "update" }));
  1039. rebuild();
  1040. });
  1041. watchListRecord[id].on("unlink", async () => {
  1042. var _a3;
  1043. await ((_a3 = plugin.watchChange) == null ? void 0 : _a3.call(context, id, { event: "delete" }));
  1044. rebuild();
  1045. });
  1046. }
  1047. });
  1048. }
  1049. });
  1050. }
  1051. if (plugin.resolveId) {
  1052. onResolve({ filter: onResolveFilter }, async (args) => {
  1053. var _a3;
  1054. if ((_a3 = initialOptions.external) == null ? void 0 : _a3.includes(args.path)) {
  1055. return void 0;
  1056. }
  1057. const isEntry = args.kind === "entry-point";
  1058. const result = await plugin.resolveId(
  1059. args.path,
  1060. isEntry ? void 0 : args.importer,
  1061. { isEntry }
  1062. );
  1063. if (typeof result === "string") {
  1064. return { path: result, namespace: plugin.name };
  1065. } else if (typeof result === "object" && result !== null) {
  1066. return { path: result.id, external: result.external, namespace: plugin.name };
  1067. }
  1068. });
  1069. }
  1070. if (plugin.load || plugin.transform) {
  1071. onLoad({ filter: onLoadFilter }, async (args) => {
  1072. const id = args.path + args.suffix;
  1073. const errors = [];
  1074. const warnings = [];
  1075. const pluginContext = {
  1076. error(message) {
  1077. errors.push({ text: String(message) });
  1078. },
  1079. warn(message) {
  1080. warnings.push({ text: String(message) });
  1081. }
  1082. };
  1083. const resolveDir = path.dirname(args.path);
  1084. let code, map;
  1085. if (plugin.load && (!plugin.loadInclude || plugin.loadInclude(id))) {
  1086. const result = await plugin.load.call(Object.assign(context, pluginContext), id);
  1087. if (typeof result === "string") {
  1088. code = result;
  1089. } else if (typeof result === "object" && result !== null) {
  1090. code = result.code;
  1091. map = result.map;
  1092. }
  1093. }
  1094. if (!plugin.transform) {
  1095. if (code === void 0) {
  1096. return null;
  1097. }
  1098. if (map) {
  1099. if (!map.sourcesContent || map.sourcesContent.length === 0) {
  1100. map.sourcesContent = [code];
  1101. }
  1102. map = fixSourceMap(map);
  1103. code += `
  1104. //# sourceMappingURL=${map.toUrl()}`;
  1105. }
  1106. return { contents: code, errors, warnings, loader: guessLoader(args.path), resolveDir };
  1107. }
  1108. if (!plugin.transformInclude || plugin.transformInclude(id)) {
  1109. if (!code) {
  1110. code = await fs.promises.readFile(args.path, "utf8");
  1111. }
  1112. const result = await plugin.transform.call(Object.assign(context, pluginContext), code, id);
  1113. if (typeof result === "string") {
  1114. code = result;
  1115. } else if (typeof result === "object" && result !== null) {
  1116. code = result.code;
  1117. if (map && result.map) {
  1118. map = combineSourcemaps(args.path, [
  1119. result.map,
  1120. map
  1121. ]);
  1122. } else {
  1123. map = result.map;
  1124. }
  1125. }
  1126. }
  1127. if (code) {
  1128. if (map) {
  1129. if (!map.sourcesContent || map.sourcesContent.length === 0) {
  1130. map.sourcesContent = [code];
  1131. }
  1132. map = fixSourceMap(map);
  1133. code += `
  1134. //# sourceMappingURL=${map.toUrl()}`;
  1135. }
  1136. return { contents: code, errors, warnings, loader: guessLoader(args.path), resolveDir };
  1137. }
  1138. });
  1139. }
  1140. }
  1141. };
  1142. };
  1143. }
  1144. // src/rollup/index.ts
  1145. function getRollupPlugin(factory) {
  1146. return (userOptions) => {
  1147. const meta = {
  1148. framework: "rollup"
  1149. };
  1150. const rawPlugin = factory(userOptions, meta);
  1151. return toRollupPlugin(rawPlugin);
  1152. };
  1153. }
  1154. function toRollupPlugin(plugin, containRollupOptions = true) {
  1155. if (plugin.transform && plugin.transformInclude) {
  1156. const _transform = plugin.transform;
  1157. plugin.transform = function(code, id) {
  1158. if (plugin.transformInclude && !plugin.transformInclude(id)) {
  1159. return null;
  1160. }
  1161. return _transform.call(this, code, id);
  1162. };
  1163. }
  1164. if (plugin.load && plugin.loadInclude) {
  1165. const _load = plugin.load;
  1166. plugin.load = function(id) {
  1167. if (plugin.loadInclude && !plugin.loadInclude(id)) {
  1168. return null;
  1169. }
  1170. return _load.call(this, id);
  1171. };
  1172. }
  1173. if (plugin.rollup && containRollupOptions) {
  1174. Object.assign(plugin, plugin.rollup);
  1175. }
  1176. return plugin;
  1177. }
  1178. // src/vite/index.ts
  1179. function getVitePlugin(factory) {
  1180. return (userOptions) => {
  1181. const meta = {
  1182. framework: "vite"
  1183. };
  1184. const rawPlugin = factory(userOptions, meta);
  1185. const plugin = toRollupPlugin(rawPlugin, false);
  1186. if (rawPlugin.vite) {
  1187. Object.assign(plugin, rawPlugin.vite);
  1188. }
  1189. return plugin;
  1190. };
  1191. }
  1192. // src/webpack/index.ts
  1193. import fs2 from "fs";
  1194. import { fileURLToPath } from "url";
  1195. import { resolve as resolve4, dirname } from "path";
  1196. import VirtualModulesPlugin from "webpack-virtual-modules";
  1197. // src/utils.ts
  1198. import { isAbsolute, normalize } from "path";
  1199. function normalizeAbsolutePath(path2) {
  1200. if (isAbsolute(path2)) {
  1201. return normalize(path2);
  1202. } else {
  1203. return path2;
  1204. }
  1205. }
  1206. // src/webpack/context.ts
  1207. import { resolve as resolve3 } from "path";
  1208. import sources from "webpack-sources";
  1209. import { Parser as Parser2 } from "acorn";
  1210. function createContext(compilation) {
  1211. return {
  1212. parse(code, opts = {}) {
  1213. return Parser2.parse(code, {
  1214. sourceType: "module",
  1215. ecmaVersion: "latest",
  1216. locations: true,
  1217. ...opts
  1218. });
  1219. },
  1220. addWatchFile(id) {
  1221. (compilation.fileDependencies ?? compilation.compilationDependencies).add(
  1222. resolve3(process.cwd(), id)
  1223. );
  1224. },
  1225. emitFile(emittedFile) {
  1226. const outFileName = emittedFile.fileName || emittedFile.name;
  1227. if (emittedFile.source && outFileName) {
  1228. compilation.emitAsset(
  1229. outFileName,
  1230. sources ? new sources.RawSource(
  1231. typeof emittedFile.source === "string" ? emittedFile.source : Buffer.from(emittedFile.source)
  1232. ) : {
  1233. source: () => emittedFile.source,
  1234. size: () => emittedFile.source.length
  1235. }
  1236. );
  1237. }
  1238. },
  1239. getWatchFiles() {
  1240. return Array.from(
  1241. compilation.fileDependencies ?? compilation.compilationDependencies
  1242. );
  1243. }
  1244. };
  1245. }
  1246. // src/webpack/index.ts
  1247. var _dirname = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
  1248. var TRANSFORM_LOADER = resolve4(
  1249. _dirname,
  1250. false ? "../../dist/webpack/loaders/transform" : "webpack/loaders/transform"
  1251. );
  1252. var LOAD_LOADER = resolve4(
  1253. _dirname,
  1254. false ? "../../dist/webpack/loaders/load" : "webpack/loaders/load"
  1255. );
  1256. var VIRTUAL_MODULE_PREFIX = resolve4(process.cwd(), "_virtual_");
  1257. function getWebpackPlugin(factory) {
  1258. return (userOptions) => {
  1259. return {
  1260. apply(compiler) {
  1261. const meta = {
  1262. framework: "webpack",
  1263. webpack: {
  1264. compiler
  1265. }
  1266. };
  1267. const rawPlugin = factory(userOptions, meta);
  1268. const plugin = Object.assign(
  1269. rawPlugin,
  1270. {
  1271. __unpluginMeta: meta,
  1272. __virtualModulePrefix: VIRTUAL_MODULE_PREFIX
  1273. }
  1274. );
  1275. const injected = compiler.$unpluginContext || {};
  1276. compiler.$unpluginContext = injected;
  1277. injected[plugin.name] = plugin;
  1278. compiler.hooks.thisCompilation.tap(plugin.name, (compilation) => {
  1279. compilation.hooks.childCompiler.tap(plugin.name, (childCompiler) => {
  1280. childCompiler.$unpluginContext = injected;
  1281. });
  1282. });
  1283. const externalModules = /* @__PURE__ */ new Set();
  1284. if (plugin.transform) {
  1285. const useLoader = [{
  1286. loader: `${TRANSFORM_LOADER}?unpluginName=${encodeURIComponent(plugin.name)}`
  1287. }];
  1288. const useNone = [];
  1289. compiler.options.module.rules.push({
  1290. enforce: plugin.enforce,
  1291. use: (data) => {
  1292. if (data.resource == null) {
  1293. return useNone;
  1294. }
  1295. const id = normalizeAbsolutePath(data.resource + (data.resourceQuery || ""));
  1296. if (!plugin.transformInclude || plugin.transformInclude(id)) {
  1297. return useLoader;
  1298. }
  1299. return useNone;
  1300. }
  1301. });
  1302. }
  1303. if (plugin.resolveId) {
  1304. let vfs = compiler.options.plugins.find((i) => i instanceof VirtualModulesPlugin);
  1305. if (!vfs) {
  1306. vfs = new VirtualModulesPlugin();
  1307. compiler.options.plugins.push(vfs);
  1308. }
  1309. plugin.__vfsModules = /* @__PURE__ */ new Set();
  1310. plugin.__vfs = vfs;
  1311. const resolverPlugin = {
  1312. apply(resolver) {
  1313. const target = resolver.ensureHook("resolve");
  1314. resolver.getHook("resolve").tapAsync(plugin.name, async (request, resolveContext, callback) => {
  1315. if (!request.request) {
  1316. return callback();
  1317. }
  1318. if (normalizeAbsolutePath(request.request).startsWith(plugin.__virtualModulePrefix)) {
  1319. return callback();
  1320. }
  1321. const id = normalizeAbsolutePath(request.request);
  1322. const requestContext = request.context;
  1323. const importer = requestContext.issuer !== "" ? requestContext.issuer : void 0;
  1324. const isEntry = requestContext.issuer === "";
  1325. const resolveIdResult = await plugin.resolveId(id, importer, { isEntry });
  1326. if (resolveIdResult == null) {
  1327. return callback();
  1328. }
  1329. let resolved = typeof resolveIdResult === "string" ? resolveIdResult : resolveIdResult.id;
  1330. const isExternal = typeof resolveIdResult === "string" ? false : resolveIdResult.external === true;
  1331. if (isExternal) {
  1332. externalModules.add(resolved);
  1333. }
  1334. if (!fs2.existsSync(resolved)) {
  1335. resolved = normalizeAbsolutePath(
  1336. plugin.__virtualModulePrefix + encodeURIComponent(resolved)
  1337. );
  1338. if (!plugin.__vfsModules.has(resolved)) {
  1339. plugin.__vfs.writeModule(resolved, "");
  1340. plugin.__vfsModules.add(resolved);
  1341. }
  1342. }
  1343. const newRequest = {
  1344. ...request,
  1345. request: resolved
  1346. };
  1347. resolver.doResolve(target, newRequest, null, resolveContext, callback);
  1348. });
  1349. }
  1350. };
  1351. compiler.options.resolve.plugins = compiler.options.resolve.plugins || [];
  1352. compiler.options.resolve.plugins.push(resolverPlugin);
  1353. }
  1354. if (plugin.load) {
  1355. compiler.options.module.rules.push({
  1356. include(id) {
  1357. if (id.startsWith(plugin.__virtualModulePrefix)) {
  1358. id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
  1359. }
  1360. if (plugin.loadInclude && !plugin.loadInclude(id)) {
  1361. return false;
  1362. }
  1363. return !externalModules.has(id);
  1364. },
  1365. enforce: plugin.enforce,
  1366. use: [{
  1367. loader: LOAD_LOADER,
  1368. options: {
  1369. unpluginName: plugin.name
  1370. }
  1371. }]
  1372. });
  1373. }
  1374. if (plugin.webpack) {
  1375. plugin.webpack(compiler);
  1376. }
  1377. if (plugin.watchChange || plugin.buildStart) {
  1378. compiler.hooks.make.tapPromise(plugin.name, async (compilation) => {
  1379. const context = createContext(compilation);
  1380. if (plugin.watchChange && (compiler.modifiedFiles || compiler.removedFiles)) {
  1381. const promises = [];
  1382. if (compiler.modifiedFiles) {
  1383. compiler.modifiedFiles.forEach(
  1384. (file) => promises.push(Promise.resolve(plugin.watchChange.call(context, file, { event: "update" })))
  1385. );
  1386. }
  1387. if (compiler.removedFiles) {
  1388. compiler.removedFiles.forEach(
  1389. (file) => promises.push(Promise.resolve(plugin.watchChange.call(context, file, { event: "delete" })))
  1390. );
  1391. }
  1392. await Promise.all(promises);
  1393. }
  1394. if (plugin.buildStart) {
  1395. return await plugin.buildStart.call(context);
  1396. }
  1397. });
  1398. }
  1399. if (plugin.buildEnd) {
  1400. compiler.hooks.emit.tapPromise(plugin.name, async (compilation) => {
  1401. await plugin.buildEnd.call(createContext(compilation));
  1402. });
  1403. }
  1404. }
  1405. };
  1406. };
  1407. }
  1408. // src/define.ts
  1409. function createUnplugin(factory) {
  1410. return {
  1411. get esbuild() {
  1412. return getEsbuildPlugin(factory);
  1413. },
  1414. get rollup() {
  1415. return getRollupPlugin(factory);
  1416. },
  1417. get vite() {
  1418. return getVitePlugin(factory);
  1419. },
  1420. get webpack() {
  1421. return getWebpackPlugin(factory);
  1422. },
  1423. get raw() {
  1424. return factory;
  1425. }
  1426. };
  1427. }
  1428. export {
  1429. createUnplugin
  1430. };