bugwatch-cli 0.1.1 → 0.1.2

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.
package/README.md CHANGED
@@ -109,13 +109,28 @@ fix. The token is never printed.
109
109
  ## File names
110
110
 
111
111
  Each map is uploaded for the URL of the minified file it belongs to:
112
- the URL prefix + the map's path inside `<dir>`, without `.map`.
112
+ the URL prefix + that file's path inside `<dir>`.
113
+
114
+ - **The script's `sourceMappingURL` comment decides.** A JavaScript file
115
+ ending in `//# sourceMappingURL=559f….js.map` owns that map, so the map
116
+ is uploaded under the script's name, even when the names differ (Next.js
117
+ with Turbopack: `b55a….js` → `559f….js.map`). A map shared by several
118
+ scripts is uploaded once per script.
119
+ - **No comment** (e.g. Vite's `sourcemap: "hidden"`): the map's own path
120
+ without `.map` is used (`index-3f9a.js.map` → `index-3f9a.js`).
121
+ - **CSS maps** (`*.css.map`) are skipped: errors only come from scripts.
122
+ With `--delete-after-upload` they are deleted with the rest.
113
123
 
114
124
  | Map in the build | `--url-prefix` | Uploaded for |
115
125
  | ------------------------- | ------------------------------ | --- |
116
126
  | `dist/assets/index-3f9a.js.map` | `~/` (default) | `~/assets/index-3f9a.js` |
117
127
  | `dist/assets/index-3f9a.js.map` | `https://cdn.shop.com/static/` | `https://cdn.shop.com/static/assets/index-3f9a.js` |
118
128
 
129
+ For Next.js, upload `.next/static` with `--url-prefix "~/_next/static/"`
130
+ (files in `.next/static/chunks/` are served at `/_next/static/chunks/`), and
131
+ set `productionBrowserSourceMaps: true`. Next.js has no hidden mode, so always
132
+ use `--delete-after-upload` to keep the maps off your site.
133
+
119
134
  `~/` matches the path on any host, so it works for every domain and
120
135
  preview deployment. Use a full URL prefix when the files are served
121
136
  from a different path (e.g. a CDN folder) than their place in the
@@ -1,9 +1,9 @@
1
1
  // src/version.ts
2
- var VERSION = "0.1.1";
2
+ var VERSION = "0.1.2";
3
3
 
4
4
  // src/upload.ts
5
- import { readdir, readFile, stat, unlink } from "fs/promises";
6
- import { join, relative, resolve, sep } from "path";
5
+ import { open, readdir, readFile, stat, unlink } from "fs/promises";
6
+ import { join, posix, relative, resolve, sep } from "path";
7
7
  var DEFAULT_API_URL = "https://sentry-like.vercel.app";
8
8
  var DEFAULT_URL_PREFIX = "~/";
9
9
  var CONCURRENCY = 4;
@@ -34,7 +34,7 @@ function mapName(relativePath, urlPrefix = DEFAULT_URL_PREFIX) {
34
34
  const path = relativePath.split(sep).join("/").replace(/^\.?\//, "");
35
35
  return urlPrefix + path.replace(/\.map$/, "");
36
36
  }
37
- async function findSourceMaps(dir) {
37
+ async function findFiles(dir, test) {
38
38
  const root = resolve(dir);
39
39
  const found = [];
40
40
  async function walk(folder) {
@@ -43,12 +43,64 @@ async function findSourceMaps(dir) {
43
43
  if (entry.name === "node_modules") continue;
44
44
  const path = join(folder, entry.name);
45
45
  if (entry.isDirectory()) await walk(path);
46
- else if (entry.isFile() && entry.name.endsWith(".map")) found.push(relative(root, path).split(sep).join("/"));
46
+ else if (entry.isFile() && test(entry.name)) found.push(relative(root, path).split(sep).join("/"));
47
47
  }
48
48
  }
49
49
  await walk(root);
50
50
  return found.sort();
51
51
  }
52
+ function findSourceMaps(dir) {
53
+ return findFiles(dir, (name) => name.endsWith(".map"));
54
+ }
55
+ var COMMENT_TAIL_BYTES = 4096;
56
+ function sourceMappingUrlOf(scriptTail) {
57
+ const matches = [...scriptTail.matchAll(/\/\/[#@][ \t]*sourceMappingURL=([^\s'"]+)[ \t]*/g)];
58
+ const last = matches.at(-1);
59
+ if (!last || scriptTail.slice((last.index ?? 0) + last[0].length).trim() !== "") return null;
60
+ return last[1] ?? null;
61
+ }
62
+ async function readTail(path) {
63
+ const file = await open(path, "r");
64
+ try {
65
+ const { size } = await file.stat();
66
+ const length = Math.min(size, COMMENT_TAIL_BYTES);
67
+ const buffer = Buffer.alloc(length);
68
+ await file.read(buffer, 0, length, size - length);
69
+ return buffer.toString("utf8");
70
+ } finally {
71
+ await file.close();
72
+ }
73
+ }
74
+ async function findMapOwners(dir, maps) {
75
+ const root = resolve(dir);
76
+ const known = new Set(maps);
77
+ const owners = /* @__PURE__ */ new Map();
78
+ const scripts = await findFiles(root, (name) => /\.(m|c)?js$/.test(name));
79
+ for (const script of scripts) {
80
+ const url = sourceMappingUrlOf(await readTail(join(root, script)));
81
+ if (!url || /^[a-z][a-z0-9+.-]*:/i.test(url) || url.startsWith("/")) continue;
82
+ let decoded;
83
+ try {
84
+ decoded = decodeURIComponent(url.replace(/[?#].*$/, ""));
85
+ } catch {
86
+ continue;
87
+ }
88
+ const map = posix.normalize(posix.join(posix.dirname(script), decoded));
89
+ if (map.startsWith("../") || !known.has(map)) continue;
90
+ owners.set(map, [...owners.get(map) ?? [], script]);
91
+ }
92
+ return owners;
93
+ }
94
+ function isCssMap(map) {
95
+ return /\.css\.map$/i.test(map);
96
+ }
97
+ async function planUploads(dir, maps, urlPrefix = DEFAULT_URL_PREFIX) {
98
+ const owners = await findMapOwners(dir, maps);
99
+ return maps.filter((map) => !isCssMap(map)).flatMap((map) => {
100
+ const scripts = owners.get(map);
101
+ return scripts ? scripts.map((script) => ({ map, name: urlPrefix + script })) : [{ map, name: mapName(map, urlPrefix) }];
102
+ });
103
+ }
52
104
  function retryAfterMs(header, now = Date.now()) {
53
105
  if (!header) return null;
54
106
  const seconds = Number(header);
@@ -146,6 +198,8 @@ async function uploadSourceMaps(options) {
146
198
  }
147
199
  const files = await findSourceMaps(dir);
148
200
  if (files.length === 0) throw new UsageError(`No .map files found in ${options.dir}`);
201
+ const uploads = await planUploads(dir, files, urlPrefix);
202
+ if (uploads.length === 0) throw new UsageError(`No JavaScript source maps found in ${options.dir} (only CSS maps)`);
149
203
  const context = {
150
204
  apiUrl,
151
205
  release: options.release,
@@ -154,8 +208,7 @@ async function uploadSourceMaps(options) {
154
208
  sleep: options.sleep ?? defaultSleep,
155
209
  onRetry: options.onRetry
156
210
  };
157
- const results = await mapLimit(files, CONCURRENCY, async (file) => {
158
- const name = mapName(file, urlPrefix);
211
+ const results = await mapLimit(uploads, CONCURRENCY, async ({ map: file, name }) => {
159
212
  const path = join(dir, file);
160
213
  const { size } = await stat(path);
161
214
  let result;
@@ -175,6 +228,7 @@ async function uploadSourceMaps(options) {
175
228
  });
176
229
  const count = (outcome) => results.filter((r) => r.outcome === outcome).length;
177
230
  const summary = {
231
+ skipped: files.filter(isCssMap).length,
178
232
  uploaded: count("uploaded"),
179
233
  replaced: count("replaced"),
180
234
  unchanged: count("unchanged"),
@@ -200,5 +254,9 @@ export {
200
254
  urlPrefixProblem,
201
255
  mapName,
202
256
  findSourceMaps,
257
+ sourceMappingUrlOf,
258
+ findMapOwners,
259
+ isCssMap,
260
+ planUploads,
203
261
  uploadSourceMaps
204
262
  };
@@ -14,11 +14,12 @@ function formatResult(result) {
14
14
  return `${label} ${result.name} (${detail})`;
15
15
  }
16
16
  function formatSummary(summary, { dryRun, release, files }) {
17
+ const css = summary.skipped > 0 ? ` Skipped ${summary.skipped} CSS map${summary.skipped === 1 ? "" : "s"}.` : "";
17
18
  if (dryRun) {
18
19
  const skipped = summary.failed > 0 ? ` (${summary.failed} would fail)` : "";
19
- return `Dry run for release ${release}: ${files} source map${files === 1 ? "" : "s"} found${skipped}; nothing was uploaded.`;
20
+ return `Dry run for release ${release}: ${files} source map${files === 1 ? "" : "s"} found${skipped}; nothing was uploaded.${css}`;
20
21
  }
21
- const line = `Release ${release}: ${summary.uploaded} uploaded, ${summary.replaced} replaced, ${summary.unchanged} unchanged, ${summary.failed} failed.`;
22
+ const line = `Release ${release}: ${summary.uploaded} uploaded, ${summary.replaced} replaced, ${summary.unchanged} unchanged, ${summary.failed} failed.${css}`;
22
23
  return summary.deleted > 0 ? `${line} Deleted ${summary.deleted} local .map file${summary.deleted === 1 ? "" : "s"}.` : line;
23
24
  }
24
25
 
@@ -1,14 +1,14 @@
1
1
  import {
2
2
  formatResult,
3
3
  formatSummary
4
- } from "./chunk-QGLOKEF7.js";
4
+ } from "./chunk-C7AULTCO.js";
5
5
  import {
6
6
  DEFAULT_API_URL,
7
7
  DEFAULT_URL_PREFIX,
8
8
  UsageError,
9
9
  VERSION,
10
10
  uploadSourceMaps
11
- } from "./chunk-6YRBKOB7.js";
11
+ } from "./chunk-3TESXBNC.js";
12
12
 
13
13
  // src/main.ts
14
14
  import { parseArgs } from "util";
package/dist/cli.js CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  main
4
- } from "./chunk-NWO6RFO7.js";
5
- import "./chunk-QGLOKEF7.js";
6
- import "./chunk-6YRBKOB7.js";
4
+ } from "./chunk-RRAHAYFL.js";
5
+ import "./chunk-C7AULTCO.js";
6
+ import "./chunk-3TESXBNC.js";
7
7
 
8
8
  // src/cli.ts
9
9
  main(process.argv.slice(2)).then((code) => {
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ declare const MAX_RETRIES = 3;
8
8
  declare const MAX_MAP_BYTES: number;
9
9
  type Outcome = "uploaded" | "replaced" | "unchanged" | "failed" | "dry-run";
10
10
  interface FileResult {
11
- /** The map's path relative to `dir`, with `/` separators. */
11
+ /** The map's path relative to `dir`, with `/` separators (one map can be uploaded under several names). */
12
12
  file: string;
13
13
  /** The minified file's URL the map is uploaded for. */
14
14
  name: string;
@@ -20,6 +20,8 @@ interface FileResult {
20
20
  error?: string;
21
21
  }
22
22
  interface UploadSummary {
23
+ /** CSS maps found and not uploaded (errors only come from scripts). */
24
+ skipped: number;
23
25
  uploaded: number;
24
26
  replaced: number;
25
27
  unchanged: number;
@@ -70,6 +72,29 @@ declare function urlPrefixProblem(prefix: string): string | null;
70
72
  declare function mapName(relativePath: string, urlPrefix?: string): string;
71
73
  /** Every `*.map` file under `dir`, relative paths with `/`, sorted. Skips `node_modules`. */
72
74
  declare function findSourceMaps(dir: string): Promise<string[]>;
75
+ /** The `//# sourceMappingURL=` (or legacy `//@`) value at the end of a script, or null. */
76
+ declare function sourceMappingUrlOf(scriptTail: string): string | null;
77
+ /**
78
+ * Which script each map belongs to, from the scripts' `sourceMappingURL`
79
+ * comments: map path → script paths (relative to `dir`). Needed because some
80
+ * bundlers (e.g. Next.js with Turbopack) name a chunk's map differently from
81
+ * the chunk (`b55a.js` → `559f.js.map`). `data:` URLs, absolute URLs, and
82
+ * maps outside `dir` are ignored.
83
+ */
84
+ declare function findMapOwners(dir: string, maps: string[]): Promise<Map<string, string[]>>;
85
+ /** CSS maps are never needed for errors (and Turbopack writes them as index maps, which are rejected). */
86
+ declare function isCssMap(map: string): boolean;
87
+ /** One upload: a map file and the name (minified file URL) it is uploaded for. */
88
+ interface PlannedUpload {
89
+ map: string;
90
+ name: string;
91
+ }
92
+ /**
93
+ * The uploads for `maps`: a map referenced by scripts is uploaded once per
94
+ * script, named after the script; a map no script points to (e.g. Vite's
95
+ * `sourcemap: "hidden"`) is named after itself without `.map`.
96
+ */
97
+ declare function planUploads(dir: string, maps: string[], urlPrefix?: string): Promise<PlannedUpload[]>;
73
98
  /**
74
99
  * Uploads every source map under `dir` for `release` (4 at a time, with
75
100
  * retries). Throws `UsageError` for problems with the options; upload
@@ -78,6 +103,6 @@ declare function findSourceMaps(dir: string): Promise<string[]>;
78
103
  declare function uploadSourceMaps(options: UploadOptions): Promise<UploadReport>;
79
104
 
80
105
  /** Keep in sync with package.json (a test checks it). */
81
- declare const VERSION = "0.1.1";
106
+ declare const VERSION = "0.1.2";
82
107
 
83
- export { CONCURRENCY, DEFAULT_API_URL, DEFAULT_URL_PREFIX, type FileResult, MAX_MAP_BYTES, MAX_RETRIES, type Outcome, type UploadOptions, type UploadReport, type UploadSummary, UsageError, VERSION, findSourceMaps, mapName, releaseProblem, uploadSourceMaps, urlPrefixProblem };
108
+ export { CONCURRENCY, DEFAULT_API_URL, DEFAULT_URL_PREFIX, type FileResult, MAX_MAP_BYTES, MAX_RETRIES, type Outcome, type PlannedUpload, type UploadOptions, type UploadReport, type UploadSummary, UsageError, VERSION, findMapOwners, findSourceMaps, isCssMap, mapName, planUploads, releaseProblem, sourceMappingUrlOf, uploadSourceMaps, urlPrefixProblem };
package/dist/index.js CHANGED
@@ -6,12 +6,16 @@ import {
6
6
  MAX_RETRIES,
7
7
  UsageError,
8
8
  VERSION,
9
+ findMapOwners,
9
10
  findSourceMaps,
11
+ isCssMap,
10
12
  mapName,
13
+ planUploads,
11
14
  releaseProblem,
15
+ sourceMappingUrlOf,
12
16
  uploadSourceMaps,
13
17
  urlPrefixProblem
14
- } from "./chunk-6YRBKOB7.js";
18
+ } from "./chunk-3TESXBNC.js";
15
19
  export {
16
20
  CONCURRENCY,
17
21
  DEFAULT_API_URL,
@@ -20,9 +24,13 @@ export {
20
24
  MAX_RETRIES,
21
25
  UsageError,
22
26
  VERSION,
27
+ findMapOwners,
23
28
  findSourceMaps,
29
+ isCssMap,
24
30
  mapName,
31
+ planUploads,
25
32
  releaseProblem,
33
+ sourceMappingUrlOf,
26
34
  uploadSourceMaps,
27
35
  urlPrefixProblem
28
36
  };
package/dist/main.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  main
3
- } from "./chunk-NWO6RFO7.js";
4
- import "./chunk-QGLOKEF7.js";
5
- import "./chunk-6YRBKOB7.js";
3
+ } from "./chunk-RRAHAYFL.js";
4
+ import "./chunk-C7AULTCO.js";
5
+ import "./chunk-3TESXBNC.js";
6
6
  export {
7
7
  main
8
8
  };
package/dist/vite.js CHANGED
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  formatResult,
3
3
  formatSummary
4
- } from "./chunk-QGLOKEF7.js";
4
+ } from "./chunk-C7AULTCO.js";
5
5
  import {
6
6
  DEFAULT_URL_PREFIX,
7
7
  UsageError,
8
8
  uploadSourceMaps
9
- } from "./chunk-6YRBKOB7.js";
9
+ } from "./chunk-3TESXBNC.js";
10
10
 
11
11
  // src/vite.ts
12
12
  import { resolve } from "path";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bugwatch-cli",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Upload source maps to Bugwatch from CI (CLI and Vite plugin)",
5
5
  "type": "module",
6
6
  "bin": {