Skip to content

Commit f1178de

Browse files
committed
fix: fix rewrite script evaluate timing
1 parent dad15a1 commit f1178de

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

rewrite/figma.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
import waitFor from 'p-wait-for'
2+
13
import { GROUPS } from './config'
24
import { applyGroups } from './shared'
35

6+
async function waitRuntimeReady() {
7+
const getQueue = () => (window.webpackChunk_figma_web_bundler ||= [])
8+
9+
await waitFor(() => {
10+
const queue = getQueue()
11+
return typeof queue.push === 'function' && queue.push !== Array.prototype.push
12+
})
13+
}
14+
415
async function rewriteScript() {
516
const current = document.currentScript as HTMLScriptElement
617
const src = current?.src
@@ -29,6 +40,8 @@ async function rewriteScript() {
2940

3041
const content = afterRules.replaceAll('delete window.figma', 'window.figma = undefined')
3142

43+
await waitRuntimeReady()
44+
3245
Object.defineProperty(document, 'currentScript', {
3346
configurable: true,
3447
get() {
@@ -41,7 +54,11 @@ async function rewriteScript() {
4154
console.error(e)
4255
replaceScript(`${src}?fallback`)
4356
} finally {
44-
Object.defineProperty(document, 'currentScript', desc as PropertyDescriptor)
57+
if (desc) {
58+
Object.defineProperty(document, 'currentScript', desc)
59+
} else {
60+
delete (document as any).currentScript
61+
}
4562
}
4663
}
4764

types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
interface Window {
22
figma: PluginAPI
3+
webpackChunk_figma_web_bundler: any[] & { push: (...args: any[]) => any }
34
}

0 commit comments

Comments
 (0)