-
-
Notifications
You must be signed in to change notification settings - Fork 809
/
utils.js
110 lines (100 loc) · 3.45 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* eslint-disable no-underscore-dangle */
// Avoid warning of use metro require on dev mode
// it actually unnecessary for RN >= 0.56, so it is backward compatibility
const avoidWarnForRequire = (moduleNames) => {
if (!moduleNames.length) moduleNames.push('NativeModules')
return new Promise((resolve) => {
setTimeout(() => {
// It's replaced console.warn of react-native
const originalWarn = console.warn
console.warn = (...args) => {
if (
args[0]
&& moduleNames.some(
(name) => args[0].indexOf(`Requiring module '${name}' by name`) > -1,
)
) {
return
}
return originalWarn(...args)
}
resolve(() => {
console.warn = originalWarn
})
})
})
}
let reactNative
const getRequireMethod = () => {
// RN >= 0.57
if (typeof window.__r === 'function') return window.__r
// RN < 0.57
if (typeof window.require === 'function') return window.require
}
const lookupForRNModules = (size = 999) => {
const metroRequire = getRequireMethod()
let actualSize = size
let getModule = metroRequire
if (metroRequire.getModules) {
const mods = metroRequire.getModules()
actualSize = Object.keys(mods).length
getModule = (moduleId) => {
const mod = mods && mods[moduleId]
return (mod && mod.publicModule && mod.publicModule.exports) || null
}
} else {
getModule = (moduleId) => metroRequire(moduleId)
}
for (let moduleId = 0; moduleId <= actualSize - 1; moduleId += 1) {
const rn = getModule(moduleId)
if (rn && rn.requireNativeComponent && rn.NativeModules) {
return rn
}
}
return null
}
const getModule = (name, size) => {
let result
try {
const metroRequire = getRequireMethod()
// RN >= 0.56
if (metroRequire.name === 'metroRequire') {
const rn = reactNative || lookupForRNModules(size)
reactNative = rn
global.$reactNative = rn
result = reactNative && reactNative[name]
} else if (metroRequire.name === '_require') {
result = metroRequire(name)
}
} catch (e) {} // eslint-disable-line
return result || { __empty: true }
}
const requiredModules = {
MessageQueue: (size) => (self.__fbBatchedBridge
&& Object.getPrototypeOf(self.__fbBatchedBridge).constructor)
|| getModule('MessageQueue', size),
NativeModules: (size) => getModule('NativeModules', size),
Platform: (size) => getModule('Platform', size),
setupDevtools: (size) => getModule('setupDevtools', size),
}
export const getRequiredModules = async (size) => {
if (!window.__DEV__ || !getRequireMethod()) return
const done = await avoidWarnForRequire(Object.keys(requiredModules))
const modules = {}
Object.keys(requiredModules).forEach((name) => {
modules[name] = requiredModules[name](size)
})
done()
return modules
}
const RN_DEBUGGER_URL_PART = 'RNDebuggerWorker.js'
const BUNDLE_URL_REGEXP = /(http[\S]*?index\.bundle\?[\S]*?)(:\d+:?\d?)/
const addInlineSourceMap = (_, urlGroup1, urlGroup2) => `${urlGroup1}&inlineSourceMap=true${urlGroup2}`
const mapStackLines = (line) => line.replace(BUNDLE_URL_REGEXP, addInlineSourceMap)
const filterRnDebuggerLines = (line) => !line.includes(RN_DEBUGGER_URL_PART)
export function updateStackWithSourceMap(stack) {
const lines = stack.split('\n')
const linesWithoutRNDebugger = lines.filter(filterRnDebuggerLines)
const lineWithSourceMap = linesWithoutRNDebugger.map(mapStackLines)
return lineWithSourceMap.join('\n')
}