Skip to content

Commit a1042f3

Browse files
Brian Vaughnbvaughnsaphal1998vibhorgupta-gh
authored andcommitted
DevTools: Show hook names based on variable usage (facebook#21641)
Co-authored-by: Brian Vaughn <brian.david.vaughn@gmail.com> Co-authored-by: Saphal Patro <saphal1998@gmail.com> Co-authored-by: VibhorCodecianGupta <vibhordelgupta@gmail.com>
1 parent 9d95af7 commit a1042f3

File tree

84 files changed

+2966
-74
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+2966
-74
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ packages/react-devtools-core/dist
2020
packages/react-devtools-extensions/chrome/build
2121
packages/react-devtools-extensions/firefox/build
2222
packages/react-devtools-extensions/shared/build
23+
packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/
24+
packages/react-devtools-extensions/src/ErrorTesterCompiled.js
2325
packages/react-devtools-inline/dist
2426
packages/react-devtools-shell/dist
2527
packages/react-devtools-scheduling-profiler/dist

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ packages/react-devtools-core/dist
22
packages/react-devtools-extensions/chrome/build
33
packages/react-devtools-extensions/firefox/build
44
packages/react-devtools-extensions/shared/build
5+
packages/react-devtools-extensions/src/__tests__/__source__/__compiled__/
6+
packages/react-devtools-extensions/src/ErrorTesterCompiled.js
57
packages/react-devtools-inline/dist
68
packages/react-devtools-shell/dist
79
packages/react-devtools-scheduling-profiler/dist

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
"scripts": {
108108
"build": "node ./scripts/rollup/build.js",
109109
"build-combined": "node ./scripts/rollup/build-all-release-channels.js",
110-
"build-for-devtools": "cross-env RELEASE_CHANNEL=experimental yarn build react/index,react-dom,react-is,react-debug-tools,scheduler,react-test-renderer,react-refresh --type=NODE && rm -rf build2 && mkdir build2 && cp -r ./build/node_modules build2/oss-experimental/",
110+
"build-for-devtools": "cross-env RELEASE_CHANNEL=experimental yarn build react/index,react/jsx,react-dom,react-is,react-debug-tools,scheduler,react-test-renderer,react-refresh --type=NODE && rm -rf build2 && mkdir build2 && cp -r ./build/node_modules build2/oss-experimental/",
111111
"build-for-devtools-dev": "yarn build-for-devtools --type=NODE_DEV",
112112
"build-for-devtools-prod": "yarn build-for-devtools --type=NODE_PROD",
113113
"linc": "node ./scripts/tasks/linc.js",

packages/react-devtools-core/webpack.backend.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ module.exports = {
4747
scheduler: resolve(builtModulesDir, 'scheduler'),
4848
},
4949
},
50+
node: {
51+
fs: 'empty',
52+
},
5053
plugins: [
5154
new DefinePlugin({
5255
__DEV__,

packages/react-devtools-extensions/build.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ const build = async (tempPath, manifestPath) => {
9393
STATIC_FILES.map(file => copy(join(__dirname, file), join(zipPath, file))),
9494
);
9595

96+
// The "source-map" library requires this chunk of WASM to be bundled at runtime.
97+
await copy(
98+
join(__dirname, 'node_modules', 'source-map', 'lib', 'mappings.wasm'),
99+
join(zipPath, 'mappings.wasm'),
100+
);
101+
96102
const commit = getGitCommit();
97103
const dateString = new Date().toLocaleDateString();
98104
const manifest = JSON.parse(readFileSync(copiedManifestPath).toString());

packages/react-devtools-extensions/chrome/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"main.html",
3333
"panel.html",
3434
"build/react_devtools_backend.js",
35-
"build/renderer.js"
35+
"build/renderer.js",
36+
"mappings.wasm"
3637
],
3738

3839
"background": {

packages/react-devtools-extensions/edge/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"main.html",
3333
"panel.html",
3434
"build/react_devtools_backend.js",
35-
"build/renderer.js"
35+
"build/renderer.js",
36+
"mappings.wasm"
3637
],
3738

3839
"background": {

packages/react-devtools-extensions/firefox/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"main.html",
3838
"panel.html",
3939
"build/react_devtools_backend.js",
40-
"build/renderer.js"
40+
"build/renderer.js",
41+
"mappings.wasm"
4142
],
4243

4344
"background": {

packages/react-devtools-extensions/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
"build:edge:dev": "cross-env NODE_ENV=development node ./edge/build",
1616
"test:chrome": "node ./chrome/test",
1717
"test:firefox": "node ./firefox/test",
18-
"test:edge": "node ./edge/test"
18+
"test:edge": "node ./edge/test",
19+
"update-mock-source-maps": "node ./src/__tests__/updateMockSourceMaps.js"
1920
},
2021
"devDependencies": {
2122
"@babel/core": "^7.11.1",
2223
"@babel/plugin-proposal-class-properties": "^7.10.4",
2324
"@babel/plugin-transform-flow-strip-types": "^7.10.4",
25+
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
2426
"@babel/plugin-transform-react-jsx-source": "^7.10.5",
2527
"@babel/preset-env": "^7.11.0",
2628
"@babel/preset-flow": "^7.10.4",
@@ -29,16 +31,20 @@
2931
"babel-core": "^7.0.0-bridge",
3032
"babel-eslint": "^9.0.0",
3133
"babel-loader": "^8.0.4",
34+
"babel-preset-minify": "^0.5.1",
3235
"child-process-promise": "^2.2.1",
3336
"chrome-launch": "^1.1.4",
3437
"crx": "^5.0.0",
3538
"css-loader": "^1.0.1",
3639
"firefox-profile": "^1.0.2",
40+
"fs-extra": "^4.0.2",
41+
"jest-fetch-mock": "^3.0.3",
3742
"node-libs-browser": "0.5.3",
3843
"nullthrows": "^1.0.0",
3944
"open": "^7.0.2",
4045
"os-name": "^3.1.0",
4146
"raw-loader": "^3.1.0",
47+
"source-map": "^0.8.0-beta.0",
4248
"style-loader": "^0.23.1",
4349
"web-ext": "^3.0.0",
4450
"webpack": "^4.43.0",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import ErrorStackParser from 'error-stack-parser';
11+
import testErrorStack, {
12+
SOURCE_STACK_FRAME_LINE_NUMBER,
13+
} from './ErrorTesterCompiled';
14+
15+
let sourceMapsAreAppliedToErrors: boolean | null = null;
16+
17+
// Source maps are automatically applied to Error stack frames.
18+
export function areSourceMapsAppliedToErrors(): boolean {
19+
if (sourceMapsAreAppliedToErrors === null) {
20+
try {
21+
testErrorStack();
22+
sourceMapsAreAppliedToErrors = false;
23+
} catch (error) {
24+
const parsed = ErrorStackParser.parse(error);
25+
const topStackFrame = parsed[0];
26+
const lineNumber = topStackFrame.lineNumber;
27+
if (lineNumber === SOURCE_STACK_FRAME_LINE_NUMBER) {
28+
sourceMapsAreAppliedToErrors = true;
29+
}
30+
}
31+
}
32+
33+
return sourceMapsAreAppliedToErrors === true;
34+
}

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

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The JavaScript files and source maps in this directory are used to test DevTools hook name parsing code. The files here use source maps in different formats to mirror real world applications. The source for the files is located in the parnet `__tests__` folder. To regenerate these compiled files, run `yarn update-mock-source-maps` in the `react-devtools-extensions` directory.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import React, {useEffect, useState} from 'react';
11+
12+
export function Component() {
13+
const [count, setCount] = useState(0);
14+
const isDarkMode = useIsDarkMode();
15+
16+
useEffect(() => {
17+
// ...
18+
}, []);
19+
20+
const handleClick = () => setCount(count + 1);
21+
22+
return (
23+
<>
24+
<div>Dark mode? {isDarkMode}</div>
25+
<div>Count: {count}</div>
26+
<button onClick={handleClick}>Update count</button>
27+
</>
28+
);
29+
}
30+
31+
function useIsDarkMode() {
32+
const [isDarkMode] = useState(false);
33+
34+
useEffect(function useEffectCreate() {
35+
// Here is where we may listen to a "theme" event...
36+
}, []);
37+
38+
return isDarkMode;
39+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import React from 'react';
11+
import useTheme from './useTheme';
12+
13+
export function Component() {
14+
const theme = useTheme();
15+
16+
return <div>theme: {theme}</div>;
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import React, {useState} from 'react';
11+
12+
export function Component() {
13+
const [count, setCount] = useState(0);
14+
15+
return (
16+
<div>
17+
<p>You clicked {count} times</p>
18+
<button onClick={() => setCount(count + 1)}>Click me</button>
19+
</div>
20+
);
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
export function Component() {
11+
const [count] = require('react').useState(0);
12+
13+
return count;
14+
}

0 commit comments

Comments
 (0)