Skip to content

Commit 373421a

Browse files
Brian Vaughnzhengjitf
authored andcommitted
Add named hooks support to react-devtools-inline (facebook#22263)
This commit builds on PR facebook#22260 and makes the following changes: * Adds a DevTools feature flag for named hooks support. (This allows us to disable it entirely for a build via feature flag.) * Adds a new Suspense cache for dynamically imported modules. (This allows a component to suspend while importing an external code chunk– like the hook names parsing code). * DevTools supports a hookNamesModuleLoaderFunction param to import the hook names module. I wish this could be handles as part of the react-devtools-shared package, but I'm not sure how to configure Webpack (4) to serve the chunk from react-devtools-inline. This seemed like a reasonable workaround. The PR also contains an additional unrelated change: * Removes pre-fetch optimization (added in DevTools: Improve named hooks network caching facebook#22198). This optimization was mostly only important for cases where sources needed to be re-downloaded, something which we can now avoid in most cases¹ thanks to using cached responses already loaded by the page. (I tested this locally on Facebook and this change has no negative performance impact. There is still some overhead from serializing the JS through the Bridge but that's constant between the two approaches.) ¹ The case where we don't benefit from cached responses is when DevTools are opened after the page has already loaded certain scripts. This seems uncommon enough that I don't think it justified the added complexity of prefetching.
1 parent 6385f4d commit 373421a

24 files changed

+415
-278
lines changed

packages/react-devtools-extensions/src/main.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -287,32 +287,30 @@ function createPanelIfReactLoaded() {
287287
};
288288
}
289289

290+
// TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration.
291+
const hookNamesModuleLoaderFunction = () =>
292+
import('react-devtools-inline/hookNames');
293+
290294
root = createRoot(document.createElement('div'));
291295

292296
render = (overrideTab = mostRecentOverrideTab) => {
293297
mostRecentOverrideTab = overrideTab;
294-
import('react-devtools-shared/src/hooks/parseHookNames').then(
295-
({parseHookNames, prefetchSourceFiles, purgeCachedMetadata}) => {
296-
root.render(
297-
createElement(DevTools, {
298-
bridge,
299-
browserTheme: getBrowserTheme(),
300-
componentsPortalContainer,
301-
enabledInspectedElementContextMenu: true,
302-
fetchFileWithCaching,
303-
loadHookNames: parseHookNames,
304-
overrideTab,
305-
prefetchSourceFiles,
306-
profilerPortalContainer,
307-
purgeCachedHookNamesMetadata: purgeCachedMetadata,
308-
showTabBar: false,
309-
store,
310-
warnIfUnsupportedVersionDetected: true,
311-
viewAttributeSourceFunction,
312-
viewElementSourceFunction,
313-
}),
314-
);
315-
},
298+
root.render(
299+
createElement(DevTools, {
300+
bridge,
301+
browserTheme: getBrowserTheme(),
302+
componentsPortalContainer,
303+
enabledInspectedElementContextMenu: true,
304+
fetchFileWithCaching,
305+
hookNamesModuleLoaderFunction,
306+
overrideTab,
307+
profilerPortalContainer,
308+
showTabBar: false,
309+
store,
310+
warnIfUnsupportedVersionDetected: true,
311+
viewAttributeSourceFunction,
312+
viewElementSourceFunction,
313+
}),
316314
);
317315
};
318316

packages/react-devtools-extensions/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = {
5252
path: __dirname + '/build',
5353
publicPath: '/build/',
5454
filename: '[name].js',
55+
chunkFilename: '[name].chunk.js',
5556
},
5657
node: {
5758
// Don't define a polyfill on window.setImmediate

packages/react-devtools-inline/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ const DevTools = initialize(contentWindow);
6464

6565
## Examples
6666

67+
### Supporting named hooks
68+
69+
DevTools can display hook "names" for an inspected component, although determining the "names" requires loading the source (and source-maps), parsing the code, and infering the names based on which variables hook values get assigned to. Because the code for this is non-trivial, it's lazy-loaded only if the feature is enabled.
70+
71+
To configure this package to support this functionality, you'll need to provide a prop that dynamically imports the extra functionality:
72+
```js
73+
// Follow code examples above to configure the backend and frontend.
74+
// When rendering DevTools, the important part is to pass a 'hookNamesModuleLoaderFunction' prop.
75+
const hookNamesModuleLoaderFunction = () => import('react-devtools-inline/hookNames');
76+
77+
// Render:
78+
<DevTools
79+
hookNamesModuleLoaderFunction={hookNamesModuleLoaderFunction}
80+
{...otherProps}
81+
/>;
82+
```
83+
6784
### Configuring a same-origin `iframe`
6885

6986
The simplest way to use this package is to install the hook from the parent `window`. This is possible if the `iframe` is not sandboxed and there are no cross-origin restrictions.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/hookNames');

packages/react-devtools-inline/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
"prepublish": "yarn run build",
2121
"start": "cross-env NODE_ENV=development webpack --config webpack.config.js --watch"
2222
},
23-
"dependencies": {},
23+
"dependencies": {
24+
"source-map-js": "^0.6.2",
25+
"sourcemap-codec": "^1.4.8"
26+
},
2427
"devDependencies": {
2528
"@babel/core": "^7.11.1",
2629
"@babel/plugin-proposal-class-properties": "^7.10.4",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/** @flow */
2+
3+
import {
4+
parseHookNames,
5+
parseSourceAndMetadata,
6+
purgeCachedMetadata,
7+
} from 'react-devtools-shared/src/hooks/parseHookNames';
8+
9+
export {parseHookNames, parseSourceAndMetadata, purgeCachedMetadata};

packages/react-devtools-inline/webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ module.exports = {
3737
entry: {
3838
backend: './src/backend.js',
3939
frontend: './src/frontend.js',
40+
hookNames: './src/hookNames.js',
4041
},
4142
output: {
4243
path: __dirname + '/dist',
4344
filename: '[name].js',
45+
chunkFilename: '[name].chunk.js',
4446
library: '[name]',
4547
libraryTarget: 'commonjs2',
4648
},

packages/react-devtools-shared/src/config/DevToolsFeatureFlags.core-fb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
export const enableProfilerChangedHookIndices = true;
1717
export const isInternalFacebookBuild = true;
18-
18+
export const enableNamedHooksFeature = false;
1919
export const consoleManagedByDevToolsDuringStrictMode = false;
2020

2121
/************************************************************************

packages/react-devtools-shared/src/config/DevToolsFeatureFlags.core-oss.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
export const enableProfilerChangedHookIndices = false;
1717
export const isInternalFacebookBuild = false;
18-
18+
export const enableNamedHooksFeature = false;
1919
export const consoleManagedByDevToolsDuringStrictMode = false;
2020

2121
/************************************************************************

packages/react-devtools-shared/src/config/DevToolsFeatureFlags.default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
export const enableProfilerChangedHookIndices = false;
1717
export const isInternalFacebookBuild = false;
18-
18+
export const enableNamedHooksFeature = true;
1919
export const consoleManagedByDevToolsDuringStrictMode = true;

0 commit comments

Comments
 (0)