MCPcopy
hub / github.com/facebook/react / installHook

Function installHook

packages/react-devtools-shared/src/hook.js:58–702  ·  view source on GitHub ↗
(
  target: any,
  maybeSettingsOrSettingsPromise?:
    | DevToolsHookSettings
    | Promise<DevToolsHookSettings>,
  shouldStartProfilingNow: boolean = false,
  profilingSettings: ProfilingSettings = defaultProfilingSettings,
)

Source from the content-addressed store, hash-verified

56};
57
58export function installHook(
59 target: any,
60 maybeSettingsOrSettingsPromise?:
61 | DevToolsHookSettings
62 | Promise<DevToolsHookSettings>,
63 shouldStartProfilingNow: boolean = false,
64 profilingSettings: ProfilingSettings = defaultProfilingSettings,
65): DevToolsHook | null {
66 if (target.hasOwnProperty('__REACT_DEVTOOLS_GLOBAL_HOOK__')) {
67 return null;
68 }
69
70 function detectReactBuildType(renderer: ReactRenderer) {
71 try {
72 if (typeof renderer.version === 'string') {
73 // React DOM Fiber (16+)
74 if (renderer.bundleType > 0) {
75 // This is not a production build.
76 // We are currently only using 0 (PROD) and 1 (DEV)
77 // but might add 2 (PROFILE) in the future.
78 return 'development';
79 }
80
81 // React 16 uses flat bundles. If we report the bundle as production
82 // version, it means we also minified and envified it ourselves.
83 return 'production';
84 // Note: There is still a risk that the CommonJS entry point has not
85 // been envified or uglified. In this case the user would have *both*
86 // development and production bundle, but only the prod one would run.
87 // This would be really bad. We have a separate check for this because
88 // it happens *outside* of the renderer injection. See `checkDCE` below.
89 }
90
91 // $FlowFixMe[method-unbinding]
92 const toString = Function.prototype.toString;
93 if (renderer.Mount && renderer.Mount._renderNewRootComponent) {
94 // React DOM Stack
95 const renderRootCode = toString.call(
96 renderer.Mount._renderNewRootComponent,
97 );
98 // Filter out bad results (if that is even possible):
99 if (renderRootCode.indexOf('function') !== 0) {
100 // Hope for the best if we're not sure.
101 return 'production';
102 }
103 // Check for React DOM Stack < 15.1.0 in development.
104 // If it contains "storedMeasure" call, it's wrapped in ReactPerf (DEV only).
105 // This would be true even if it's minified, as method name still matches.
106 if (renderRootCode.indexOf('storedMeasure') !== -1) {
107 return 'development';
108 }
109 // For other versions (and configurations) it's not so easy.
110 // Let's quickly exclude proper production builds.
111 // If it contains a warning message, it's either a DEV build,
112 // or an PROD build without proper dead code elimination.
113 if (renderRootCode.indexOf('should be a pure function') !== -1) {
114 // Now how do we tell a DEV build from a bad PROD build?
115 // If we see NODE_ENV, we're going to assume this is a dev build

Callers 4

initializeFunction · 0.90
initializeFunction · 0.90
installHook.jsFile · 0.90
setupTests.jsFile · 0.85

Calls 4

thenMethod · 0.65
errorMethod · 0.65
emitMethod · 0.45

Tested by

no test coverage detected