unpeek 4.2.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +2 -2
  2. package/README.tr.md +2 -2
  3. package/dist/adapters/esbuild.cjs +5 -5
  4. package/dist/adapters/esbuild.js +3 -3
  5. package/dist/adapters/next/adapter.cjs +5 -5
  6. package/dist/adapters/next/adapter.js +4 -4
  7. package/dist/adapters/next/index.cjs +5 -5
  8. package/dist/adapters/next/index.js +4 -4
  9. package/dist/adapters/rollup.cjs +5 -5
  10. package/dist/adapters/rollup.js +3 -3
  11. package/dist/adapters/rspack.cjs +5 -5
  12. package/dist/adapters/rspack.js +3 -3
  13. package/dist/adapters/vite.cjs +5 -5
  14. package/dist/adapters/vite.js +3 -3
  15. package/dist/adapters/webpack.cjs +5 -5
  16. package/dist/adapters/webpack.js +3 -3
  17. package/dist/browser.cjs +2 -2
  18. package/dist/browser.js +1 -1
  19. package/dist/{chunk-UUJLTW7O.cjs → chunk-4IRIMLT3.cjs} +16 -16
  20. package/dist/{chunk-AD6UFBPA.cjs → chunk-4XSFPX2Z.cjs} +367 -8
  21. package/dist/{chunk-KIAYVIC5.cjs → chunk-7F75S3GQ.cjs} +60 -36
  22. package/dist/{chunk-K77WRITN.js → chunk-EASD7C2B.js} +3 -3
  23. package/dist/{chunk-APQOBFRO.js → chunk-GKWTP4Z5.js} +30 -6
  24. package/dist/{chunk-MSECP2P4.cjs → chunk-OR5M3SN2.cjs} +1199 -428
  25. package/dist/{chunk-32GLSJAW.js → chunk-RDP4MTHK.js} +359 -0
  26. package/dist/{chunk-BL2UXZGD.js → chunk-RM3V7KJA.js} +1040 -269
  27. package/dist/{chunk-SJK45WAU.cjs → chunk-SBRU7GSX.cjs} +8 -8
  28. package/dist/{chunk-GCWXTWTN.js → chunk-XEYMZUP5.js} +2 -2
  29. package/dist/cli/index.js +1425 -274
  30. package/dist/index.cjs +21 -21
  31. package/dist/index.js +3 -3
  32. package/dist/plugins/unplugin.cjs +4 -4
  33. package/dist/plugins/unplugin.js +3 -3
  34. package/package.json +1 -1
@@ -6,10 +6,18 @@ import {
6
6
  import fs from "fs";
7
7
  import path from "path";
8
8
  var SUPPORTED_EXTENSIONS = /* @__PURE__ */ new Set([".js", ".mjs", ".cjs"]);
9
+ var MAX_HTML_FILES = 64;
10
+ var MAX_SCAN_BYTES = 512 * 1024;
11
+ var MAX_JS_GRAPH_FILES = 1024;
9
12
  var EXCLUDED_DIR_NAMES = /* @__PURE__ */ new Set([
10
13
  "node_modules",
11
14
  ".unpeek-maps"
12
15
  ]);
16
+ var HTML_WALK_EXCLUDED_DIR_NAMES = /* @__PURE__ */ new Set([
17
+ "node_modules",
18
+ ".unpeek-maps",
19
+ ".git"
20
+ ]);
13
21
  var WORKER_OR_SW_BASENAME_REGEX = /^(?:sw|service-worker|workbox-[^/]+)\.(?:js|mjs|cjs)$|\.worker(?:[.-][^/]+)*\.(?:js|mjs|cjs)$/i;
14
22
  function isWorkerOrServiceWorkerFile(filePath, rootDir) {
15
23
  const normalized = filePath.replace(/\\/g, "/").split("?")[0];
@@ -372,6 +380,356 @@ function scanClientBundles(outDir, config) {
372
380
  walk(resolvedOutDir);
373
381
  return results.sort();
374
382
  }
383
+ var SCRIPT_TAG_REGEX = /<script\b([^>]*)>([\s\S]*?)<\/script>/gi;
384
+ var LINK_TAG_REGEX = /<link\b([^>]*)>/gi;
385
+ var HTML_COMMENT_REGEX = /<!--[\s\S]*?-->/g;
386
+ var ATTR_SRC_REGEX = /\bsrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
387
+ var ATTR_HREF_REGEX = /\bhref\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
388
+ var ATTR_REL_REGEX = /\brel\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
389
+ var ATTR_AS_REGEX = /\bas\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
390
+ var ATTR_TYPE_REGEX = /\btype\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
391
+ var STATIC_FROM_IMPORT_REGEX = /\bfrom\s*(['"`])([^'"`\r\n${}]+)\1/g;
392
+ var SIDE_EFFECT_IMPORT_REGEX = /(?:^|[;}\n])\s*import\s*(['"`])([^'"`\r\n${}]+)\1/g;
393
+ var DYNAMIC_IMPORT_REGEX = /\bimport\s*\(\s*(?:\/\*[^*]{0,120}\*\/\s*)?(['"`])([^'"`\r\n${}]+)\1/g;
394
+ var WORKER_META_URL_REGEX = /\bnew\s+(?:(?:window|self|globalThis)\s*\.\s*)?(?:Shared)?Worker\s*\(\s*new\s+URL\s*\(\s*(['"`])([^'"`\r\n${}]+)\1\s*,\s*import\.meta\.url\s*\)/g;
395
+ var WORKER_DIRECT_URL_REGEX = /\bnew\s+(?:(?:window|self|globalThis)\s*\.\s*)?(?:Shared)?Worker\s*\(\s*(['"`])([^'"`\r\n${}]+)\1/g;
396
+ function getHtmlAttr(attrs, regex) {
397
+ const match = attrs.match(regex);
398
+ if (!match) return void 0;
399
+ return (match[1] ?? match[2] ?? match[3] ?? "").trim();
400
+ }
401
+ function readBoundedText(filePath, maxBytes) {
402
+ let fd;
403
+ try {
404
+ fd = fs.openSync(filePath, "r");
405
+ const stat = fs.fstatSync(fd);
406
+ const toRead = Math.min(stat.size, maxBytes);
407
+ if (toRead <= 0) return "";
408
+ const buf = Buffer.allocUnsafe(toRead);
409
+ let offset = 0;
410
+ while (offset < toRead) {
411
+ const bytesRead = fs.readSync(fd, buf, offset, toRead - offset, offset);
412
+ if (bytesRead <= 0) break;
413
+ offset += bytesRead;
414
+ }
415
+ return buf.subarray(0, offset).toString("utf8");
416
+ } catch {
417
+ return "";
418
+ } finally {
419
+ if (fd !== void 0) {
420
+ try {
421
+ fs.closeSync(fd);
422
+ } catch {
423
+ }
424
+ }
425
+ }
426
+ }
427
+ function buildServerDirPrefixes(config) {
428
+ const prefixes = [];
429
+ if (config?.next?.distDir) {
430
+ prefixes.push(
431
+ path.join(config.next.distDir, "server"),
432
+ path.join(config.next.distDir, "ssr")
433
+ );
434
+ }
435
+ if (config?.vite?.outDir) {
436
+ prefixes.push(path.join(config.vite.outDir, "server"));
437
+ }
438
+ return prefixes;
439
+ }
440
+ function getAllowedExtensions(config) {
441
+ return config?.extensions && config.extensions.length > 0 ? new Set(
442
+ config.extensions.map((e) => e.startsWith(".") ? e.toLowerCase() : `.${e.toLowerCase()}`)
443
+ ) : SUPPORTED_EXTENSIONS;
444
+ }
445
+ function isExcludedPathSegment(fullPath) {
446
+ const normalized = fullPath.replace(/\\/g, "/");
447
+ return normalized.includes("/.git/") || normalized.endsWith("/.git") || normalized.includes("/node_modules/") || normalized.endsWith("/node_modules") || normalized.includes("/.unpeek-maps/") || normalized.endsWith("/.unpeek-maps");
448
+ }
449
+ function resolveOutDirJsTarget(rawSpecifier, baseDir, resolvedOutDir, allowedExtensions, serverDirPrefixes, fallbackRootDir) {
450
+ const trimmed = rawSpecifier.trim();
451
+ if (!trimmed || trimmed.startsWith("#") || /^(?:https?:|data:|blob:|javascript:|mailto:|\/\/)/i.test(trimmed)) {
452
+ return void 0;
453
+ }
454
+ const withoutQuery = trimmed.split("?")[0].split("#")[0];
455
+ let cleanSpec = withoutQuery;
456
+ try {
457
+ cleanSpec = decodeURIComponent(withoutQuery);
458
+ } catch {
459
+ }
460
+ if (!cleanSpec) return void 0;
461
+ const candidates = [];
462
+ if (cleanSpec.startsWith("/")) {
463
+ const stripped = cleanSpec.replace(/^\/+/, "");
464
+ candidates.push(path.resolve(resolvedOutDir, stripped));
465
+ if (fallbackRootDir && fallbackRootDir !== resolvedOutDir) {
466
+ candidates.push(path.resolve(fallbackRootDir, stripped));
467
+ }
468
+ } else {
469
+ candidates.push(path.resolve(baseDir, cleanSpec));
470
+ }
471
+ for (const baseCandidate of candidates) {
472
+ const ext = path.extname(baseCandidate).toLowerCase();
473
+ const withExts = ext ? [baseCandidate] : Array.from(allowedExtensions).map((e) => `${baseCandidate}${e}`);
474
+ for (const candidate of withExts) {
475
+ const candExt = path.extname(candidate).toLowerCase();
476
+ if (!allowedExtensions.has(candExt)) continue;
477
+ if (isExcludedPathSegment(candidate)) continue;
478
+ if (hasNextServerSegment(candidate, resolvedOutDir, serverDirPrefixes)) continue;
479
+ const rel = path.relative(resolvedOutDir, candidate).replace(/\\/g, "/");
480
+ if (!rel || rel.startsWith("..") || path.isAbsolute(rel)) continue;
481
+ try {
482
+ if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
483
+ return { absPath: candidate, relPath: rel };
484
+ }
485
+ } catch {
486
+ }
487
+ }
488
+ }
489
+ return void 0;
490
+ }
491
+ function extractRelativeSpecifiers(jsText, allowBareRelativeFromRoot) {
492
+ const imports = [];
493
+ const workers = [];
494
+ const collectMatches = (regex, target, requireDotPrefix) => {
495
+ regex.lastIndex = 0;
496
+ let m;
497
+ while ((m = regex.exec(jsText)) !== null) {
498
+ const spec = m[2];
499
+ if (!spec) continue;
500
+ if (spec.startsWith("./") || spec.startsWith("../") || !requireDotPrefix && !/^(?:https?:|data:|blob:)/i.test(spec)) {
501
+ target.push(spec);
502
+ }
503
+ }
504
+ };
505
+ collectMatches(STATIC_FROM_IMPORT_REGEX, imports, true);
506
+ collectMatches(SIDE_EFFECT_IMPORT_REGEX, imports, true);
507
+ collectMatches(DYNAMIC_IMPORT_REGEX, imports, true);
508
+ collectMatches(WORKER_META_URL_REGEX, workers, !allowBareRelativeFromRoot);
509
+ collectMatches(WORKER_DIRECT_URL_REGEX, workers, true);
510
+ return { imports, workers };
511
+ }
512
+ function discoverHtmlEntryGraph(outDir, config, extraHtmlDirs = []) {
513
+ const htmlEntryFiles = /* @__PURE__ */ new Set();
514
+ const preloadedFiles = /* @__PURE__ */ new Set();
515
+ const workerEntryFiles = /* @__PURE__ */ new Set();
516
+ const reachableFiles = /* @__PURE__ */ new Set();
517
+ const emptyResult = () => ({
518
+ htmlEntryFiles,
519
+ preloadedFiles,
520
+ workerEntryFiles,
521
+ reachableFiles,
522
+ entryFiles: /* @__PURE__ */ new Set(),
523
+ scannedHtmlFiles: 0,
524
+ scannedJsFiles: 0
525
+ });
526
+ const resolvedOutDir = path.resolve(outDir);
527
+ if (!fs.existsSync(resolvedOutDir)) {
528
+ return emptyResult();
529
+ }
530
+ const serverDirPrefixes = buildServerDirPrefixes(config);
531
+ const allowedExtensions = getAllowedExtensions(config);
532
+ const htmlFiles = [];
533
+ function walkHtml(currentDir) {
534
+ if (htmlFiles.length >= MAX_HTML_FILES) return;
535
+ if (hasNextServerSegment(currentDir, resolvedOutDir, serverDirPrefixes)) return;
536
+ let entries;
537
+ try {
538
+ entries = fs.readdirSync(currentDir, { withFileTypes: true });
539
+ } catch {
540
+ return;
541
+ }
542
+ entries.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
543
+ for (const entry of entries) {
544
+ if (htmlFiles.length >= MAX_HTML_FILES) break;
545
+ const fullPath = path.join(currentDir, entry.name);
546
+ if (entry.isDirectory()) {
547
+ if (HTML_WALK_EXCLUDED_DIR_NAMES.has(entry.name.toLowerCase())) continue;
548
+ if (hasNextServerSegment(fullPath, resolvedOutDir, serverDirPrefixes)) continue;
549
+ walkHtml(fullPath);
550
+ } else if (entry.isFile()) {
551
+ if (hasNextServerSegment(fullPath, resolvedOutDir, serverDirPrefixes)) continue;
552
+ if (path.extname(entry.name).toLowerCase() === ".html") {
553
+ htmlFiles.push({ filePath: fullPath, rootDir: resolvedOutDir });
554
+ }
555
+ }
556
+ }
557
+ }
558
+ walkHtml(resolvedOutDir);
559
+ if (htmlFiles.length === 0 && extraHtmlDirs.length > 0) {
560
+ for (const extraDir of extraHtmlDirs) {
561
+ if (htmlFiles.length >= MAX_HTML_FILES) break;
562
+ const resolvedExtra = path.resolve(extraDir);
563
+ if (!fs.existsSync(resolvedExtra) || isExcludedPathSegment(resolvedExtra) || hasNextServerSegment(resolvedExtra, resolvedExtra, serverDirPrefixes)) {
564
+ continue;
565
+ }
566
+ let entries;
567
+ try {
568
+ entries = fs.readdirSync(resolvedExtra, { withFileTypes: true });
569
+ } catch {
570
+ continue;
571
+ }
572
+ entries.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
573
+ for (const entry of entries) {
574
+ if (htmlFiles.length >= MAX_HTML_FILES) break;
575
+ if (entry.isFile() && path.extname(entry.name).toLowerCase() === ".html") {
576
+ htmlFiles.push({ filePath: path.join(resolvedExtra, entry.name), rootDir: resolvedExtra });
577
+ }
578
+ }
579
+ }
580
+ }
581
+ const jsQueue = [];
582
+ const queuedOrVisitedJs = /* @__PURE__ */ new Set();
583
+ const enqueueJs = (resolved) => {
584
+ if (!resolved) return;
585
+ reachableFiles.add(resolved.relPath);
586
+ if (!queuedOrVisitedJs.has(resolved.relPath) && queuedOrVisitedJs.size < MAX_JS_GRAPH_FILES) {
587
+ queuedOrVisitedJs.add(resolved.relPath);
588
+ jsQueue.push(resolved);
589
+ }
590
+ };
591
+ for (const { filePath: htmlPath, rootDir: htmlRootDir } of htmlFiles) {
592
+ const rawHtml = readBoundedText(htmlPath, MAX_SCAN_BYTES);
593
+ if (!rawHtml) continue;
594
+ const htmlText = rawHtml.replace(HTML_COMMENT_REGEX, "");
595
+ const htmlDir = path.dirname(htmlPath);
596
+ SCRIPT_TAG_REGEX.lastIndex = 0;
597
+ let scriptMatch;
598
+ while ((scriptMatch = SCRIPT_TAG_REGEX.exec(htmlText)) !== null) {
599
+ const attrs = scriptMatch[1] ?? "";
600
+ const body = scriptMatch[2] ?? "";
601
+ const scriptType = (getHtmlAttr(attrs, ATTR_TYPE_REGEX) ?? "").toLowerCase();
602
+ const isJsScript = !scriptType || scriptType === "module" || scriptType.includes("javascript") || scriptType.includes("ecmascript");
603
+ if (!isJsScript) continue;
604
+ const src = getHtmlAttr(attrs, ATTR_SRC_REGEX);
605
+ if (src) {
606
+ const resolved = resolveOutDirJsTarget(
607
+ src,
608
+ htmlDir,
609
+ resolvedOutDir,
610
+ allowedExtensions,
611
+ serverDirPrefixes,
612
+ htmlRootDir
613
+ );
614
+ if (resolved) {
615
+ htmlEntryFiles.add(resolved.relPath);
616
+ enqueueJs(resolved);
617
+ }
618
+ } else if (body.trim()) {
619
+ const { imports, workers } = extractRelativeSpecifiers(body, false);
620
+ for (const imp of imports) {
621
+ const resolved = resolveOutDirJsTarget(
622
+ imp,
623
+ htmlDir,
624
+ resolvedOutDir,
625
+ allowedExtensions,
626
+ serverDirPrefixes,
627
+ htmlRootDir
628
+ );
629
+ if (resolved) {
630
+ htmlEntryFiles.add(resolved.relPath);
631
+ enqueueJs(resolved);
632
+ }
633
+ }
634
+ for (const workerSpec of workers) {
635
+ const resolved = resolveOutDirJsTarget(
636
+ workerSpec,
637
+ htmlDir,
638
+ resolvedOutDir,
639
+ allowedExtensions,
640
+ serverDirPrefixes,
641
+ htmlRootDir
642
+ );
643
+ if (resolved) {
644
+ workerEntryFiles.add(resolved.relPath);
645
+ enqueueJs(resolved);
646
+ }
647
+ }
648
+ }
649
+ }
650
+ LINK_TAG_REGEX.lastIndex = 0;
651
+ let linkMatch;
652
+ while ((linkMatch = LINK_TAG_REGEX.exec(htmlText)) !== null) {
653
+ const attrs = linkMatch[1] ?? "";
654
+ const relAttr = (getHtmlAttr(attrs, ATTR_REL_REGEX) ?? "").toLowerCase();
655
+ if (!relAttr) continue;
656
+ const relTokens = relAttr.split(/\s+/);
657
+ const isModulePreload = relTokens.includes("modulepreload");
658
+ const isPreload = relTokens.includes("preload");
659
+ if (!isModulePreload && !isPreload) continue;
660
+ if (isPreload && !isModulePreload) {
661
+ const asAttr = (getHtmlAttr(attrs, ATTR_AS_REGEX) ?? "").toLowerCase();
662
+ if (asAttr && asAttr !== "script" && asAttr !== "worker") {
663
+ continue;
664
+ }
665
+ }
666
+ const href = getHtmlAttr(attrs, ATTR_HREF_REGEX);
667
+ if (!href) continue;
668
+ const resolved = resolveOutDirJsTarget(
669
+ href,
670
+ htmlDir,
671
+ resolvedOutDir,
672
+ allowedExtensions,
673
+ serverDirPrefixes,
674
+ htmlRootDir
675
+ );
676
+ if (resolved) {
677
+ preloadedFiles.add(resolved.relPath);
678
+ enqueueJs(resolved);
679
+ }
680
+ }
681
+ }
682
+ let scannedJsFiles = 0;
683
+ let head = 0;
684
+ while (head < jsQueue.length && scannedJsFiles < MAX_JS_GRAPH_FILES) {
685
+ const current = jsQueue[head++];
686
+ const jsText = readBoundedText(current.absPath, MAX_SCAN_BYTES);
687
+ scannedJsFiles++;
688
+ if (!jsText) continue;
689
+ const currentDir = path.dirname(current.absPath);
690
+ const { imports, workers } = extractRelativeSpecifiers(jsText, true);
691
+ for (const imp of imports) {
692
+ const resolved = resolveOutDirJsTarget(
693
+ imp,
694
+ currentDir,
695
+ resolvedOutDir,
696
+ allowedExtensions,
697
+ serverDirPrefixes
698
+ );
699
+ if (resolved) {
700
+ enqueueJs(resolved);
701
+ }
702
+ }
703
+ for (const workerSpec of workers) {
704
+ const resolved = resolveOutDirJsTarget(
705
+ workerSpec,
706
+ currentDir,
707
+ resolvedOutDir,
708
+ allowedExtensions,
709
+ serverDirPrefixes
710
+ );
711
+ if (resolved) {
712
+ workerEntryFiles.add(resolved.relPath);
713
+ enqueueJs(resolved);
714
+ }
715
+ }
716
+ }
717
+ const entryFiles = /* @__PURE__ */ new Set([...htmlEntryFiles, ...workerEntryFiles]);
718
+ if (htmlEntryFiles.size === 0 && preloadedFiles.size > 0) {
719
+ for (const preloaded of preloadedFiles) {
720
+ entryFiles.add(preloaded);
721
+ }
722
+ }
723
+ return {
724
+ htmlEntryFiles,
725
+ preloadedFiles,
726
+ workerEntryFiles,
727
+ reachableFiles,
728
+ entryFiles,
729
+ scannedHtmlFiles: htmlFiles.length,
730
+ scannedJsFiles
731
+ };
732
+ }
375
733
 
376
734
  // src/core/sourcemap-vault.ts
377
735
  import fs2 from "fs";
@@ -604,6 +962,7 @@ export {
604
962
  detectTargetFramework,
605
963
  matchesGlobPattern,
606
964
  scanClientBundles,
965
+ discoverHtmlEntryGraph,
607
966
  SOURCE_MAP_COMMENT_REGEX,
608
967
  extractDebugId,
609
968
  stripDebugIdComments,