Skip to content

Commit 8df87a1

Browse files
committed
fix: add error patch
1 parent 455aca6 commit 8df87a1

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"scripts": {
88
"dev": "wxt",
99
"dev:firefox": "wxt -b firefox",
10-
"build": "wxt build && pnpm run build:rewriter && pnpm run build:plugins && pnpm run build:readme",
10+
"build": "wxt build && pnpm run build:rewrite && pnpm run build:plugins && pnpm run build:readme",
1111
"build:firefox": "wxt build -b firefox",
12-
"build:rewrite": "esbuild ./rewrite/figma.ts --outfile=./dist/figma.js --bundle --format=esm",
12+
"build:rewrite": "esbuild ./rewrite/figma.ts --outfile=./dist/figma.js --bundle --format=iife",
1313
"build:plugins": "tsc -p ./plugins/tsconfig.json",
1414
"build:readme": "tsx ./build/readme.ts",
1515
"npm:plugins": "pnpm build:plugins && cd plugins && pnpm publish --access public && cd ..",

rewrite/figma.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { matchFile, REWRITE_PATTERN, REWRITE_REPLACER } from './config'
2+
import { patchErrorStack } from './patch'
3+
4+
patchErrorStack()
25

36
async function rewriteScript() {
47
const current = document.currentScript as HTMLScriptElement

rewrite/patch.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const EXT_SCHEMES = /(?:chrome|moz)-extension:\/\//g
2+
const PATCHED = Symbol()
3+
4+
function sanitizeStack(text: string = '') {
5+
return text.replace(EXT_SCHEMES, '')
6+
}
7+
8+
export function patchErrorStack() {
9+
if ((globalThis as any)[PATCHED]) return
10+
11+
const NativeError = globalThis.Error
12+
13+
function Error(...args: any[]) {
14+
const error = new NativeError(...args)
15+
16+
if (typeof NativeError.captureStackTrace === 'function') {
17+
NativeError.captureStackTrace(error, Error)
18+
}
19+
20+
let rawStack
21+
try {
22+
rawStack = error.stack
23+
} catch {}
24+
25+
if (typeof rawStack === 'string') {
26+
let stored = rawStack
27+
Object.defineProperty(error, 'stack', {
28+
configurable: true,
29+
enumerable: false,
30+
get() {
31+
return sanitizeStack(stored)
32+
},
33+
set(v) {
34+
stored = v
35+
}
36+
})
37+
}
38+
39+
return error
40+
}
41+
42+
Object.setPrototypeOf(Error, NativeError)
43+
Error.prototype = NativeError.prototype
44+
45+
globalThis.Error = Error as ErrorConstructor
46+
47+
Object.defineProperty(globalThis, PATCHED, {
48+
value: true,
49+
writable: false,
50+
enumerable: false,
51+
configurable: false
52+
})
53+
}

0 commit comments

Comments
 (0)