-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DevTools] remove backend dependency from the global hook #26563
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/react-devtools-extensions/src/contentScripts/renderer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* In order to support reload-and-profile functionality, the renderer needs to be injected before any other scripts. | ||
* Since it is a complex file (with imports) we can't just toString() it like we do with the hook itself, | ||
* So this entry point (one of the web_accessible_resources) provides a way to eagerly inject it. | ||
* The hook will look for the presence of a global __REACT_DEVTOOLS_ATTACH__ and attach an injected renderer early. | ||
* The normal case (not a reload-and-profile) will not make use of this entry point though. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import {attach} from 'react-devtools-shared/src/backend/renderer'; | ||
import {SESSION_STORAGE_RELOAD_AND_PROFILE_KEY} from 'react-devtools-shared/src/constants'; | ||
import {sessionStorageGetItem} from 'react-devtools-shared/src/storage'; | ||
|
||
if ( | ||
sessionStorageGetItem(SESSION_STORAGE_RELOAD_AND_PROFILE_KEY) === 'true' && | ||
!window.hasOwnProperty('__REACT_DEVTOOLS_ATTACH__') | ||
) { | ||
Object.defineProperty( | ||
window, | ||
'__REACT_DEVTOOLS_ATTACH__', | ||
({ | ||
enumerable: false, | ||
// This property needs to be configurable to allow third-party integrations | ||
// to attach their own renderer. Note that using third-party integrations | ||
// is not officially supported. Use at your own risk. | ||
configurable: true, | ||
get() { | ||
return attach; | ||
}, | ||
}: Object), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What could error before and why are we removing the catch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great question! In the original implementation, the hook is injected into the page via an inline script tag with
installHook.toString()
as the content. For example:react/packages/react-devtools-extensions/src/injectGlobalHook.js
Lines 92 to 98 in 2b903da
Because webpack will add some prefix (like
_backend_console__WEBPACK_IMPORTED_MODULE_0__
) to the modules imported in hook.js during compiling, it would actually always cause an error in the extension. Thistry-catch
is for that purpose.The comments on the top of hook.js says "we have to inline the whole ... implementation here" for the same reason.
Now we are using a much more elegant way to inject the hook, this is no longer necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People probably wanted to patch as early as possible for all platforms. But they didn't find a way to make it work for the extension. Then they noticed that the DevTools backend is initialized automatically by the extension, so it was accepted to patch it a bit later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This patched console depends on
WorkTagMap
, which is defined in the reconciler and can be changed in different versions. Going forward I think it's the best to keep it with the devtools backend. On dev and profiling builds we will be able to patch it early even on the web. On prod builds it should be OK to not have this additional information before the DevTools backend is initialized.I'd like to hear thoughts from @gaearon and @acdlite, if any