Skip to content

Commit aed1eaf

Browse files
hoxyqAndyPengc12
authored andcommitted
refactor[react-devtools-extensions]: use globals to eliminate dead code (facebook#27516)
Small change to eliminate dead code in builds for different browsers. Tested by inspecting production sources.
1 parent 24e847c commit aed1eaf

File tree

9 files changed

+30
-32
lines changed

9 files changed

+30
-32
lines changed

.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,14 @@ module.exports = {
441441
TaskController: 'readonly',
442442
},
443443
},
444+
{
445+
files: ['packages/react-devtools-extensions/**/*.js'],
446+
globals: {
447+
__IS_CHROME__: 'readonly',
448+
__IS_FIREFOX__: 'readonly',
449+
__IS_EDGE__: 'readonly',
450+
},
451+
},
444452
],
445453

446454
env: {

packages/react-devtools-extensions/src/background/dynamicallyInjectContentScripts.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
/* global chrome */
22

3-
import {IS_FIREFOX} from '../utils';
4-
53
// Firefox doesn't support ExecutionWorld.MAIN yet
64
// equivalent logic for Firefox is in prepareInjection.js
7-
const contentScriptsToInject = IS_FIREFOX
5+
const contentScriptsToInject = __IS_FIREFOX__
86
? [
97
{
108
id: '@react-devtools/proxy',

packages/react-devtools-extensions/src/background/executeScript.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* global chrome */
22

3-
import {IS_FIREFOX} from '../utils';
4-
53
// Firefox doesn't support ExecutionWorld.MAIN yet
64
// https://bugzilla.mozilla.org/show_bug.cgi?id=1736575
75
function executeScriptForFirefoxInMainWorld({target, files}) {
@@ -46,7 +44,7 @@ export function executeScriptInIsolatedWorld({target, files}) {
4644
}
4745

4846
export function executeScriptInMainWorld({target, files}) {
49-
if (IS_FIREFOX) {
47+
if (__IS_FIREFOX__) {
5048
return executeScriptForFirefoxInMainWorld({target, files});
5149
}
5250

packages/react-devtools-extensions/src/background/setExtensionIconAndPopup.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
'use strict';
44

5-
import {IS_FIREFOX} from 'react-devtools-extensions/src/utils';
6-
75
function setExtensionIconAndPopup(reactBuildType, tabId) {
8-
const action = IS_FIREFOX ? chrome.browserAction : chrome.action;
6+
const action = __IS_FIREFOX__ ? chrome.browserAction : chrome.action;
97

108
action.setIcon({
119
tabId,

packages/react-devtools-extensions/src/background/tabsManager.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
'use strict';
44

5-
import {IS_FIREFOX} from 'react-devtools-extensions/src/utils';
6-
75
import setExtensionIconAndPopup from './setExtensionIconAndPopup';
86

97
function isRestrictedBrowserPage(url) {
@@ -20,7 +18,7 @@ function checkAndHandleRestrictedPageIfSo(tab) {
2018
// we can't update for any other types (prod,dev,outdated etc)
2119
// as the content script needs to be injected at document_start itself for those kinds of detection
2220
// TODO: Show a different popup page(to reload current page probably) for old tabs, opened before the extension is installed
23-
if (!IS_FIREFOX) {
21+
if (__IS_CHROME__ || __IS_EDGE__) {
2422
chrome.tabs.query({}, tabs => tabs.forEach(checkAndHandleRestrictedPageIfSo));
2523
chrome.tabs.onCreated.addListener((tabId, changeInfo, tab) =>
2624
checkAndHandleRestrictedPageIfSo(tab),
@@ -29,7 +27,7 @@ if (!IS_FIREFOX) {
2927

3028
// Listen to URL changes on the active tab and update the DevTools icon.
3129
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
32-
if (IS_FIREFOX) {
30+
if (__IS_FIREFOX__) {
3331
// We don't properly detect protected URLs in Firefox at the moment.
3432
// However, we can reset the DevTools icon to its loading state when the URL changes.
3533
// It will be updated to the correct icon by the onMessage callback below.

packages/react-devtools-extensions/src/contentScripts/prepareInjection.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* global chrome */
22

33
import nullthrows from 'nullthrows';
4-
import {IS_FIREFOX} from '../utils';
54

65
// We run scripts on the page via the service worker (background/index.js) for
76
// Manifest V3 extensions (Chrome & Edge).
@@ -62,7 +61,7 @@ window.addEventListener('pageshow', function ({target}) {
6261
chrome.runtime.sendMessage(lastSentDevToolsHookMessage);
6362
});
6463

65-
if (IS_FIREFOX) {
64+
if (__IS_FIREFOX__) {
6665
injectScriptSync(chrome.runtime.getURL('build/renderer.js'));
6766

6867
// Inject a __REACT_DEVTOOLS_GLOBAL_HOOK__ global for React to interact with.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {flushSync} from 'react-dom';
55
import {createRoot} from 'react-dom/client';
66
import Bridge from 'react-devtools-shared/src/bridge';
77
import Store from 'react-devtools-shared/src/devtools/store';
8-
import {IS_CHROME, IS_EDGE, getBrowserTheme, IS_FIREFOX} from '../utils';
8+
import {getBrowserTheme} from '../utils';
99
import {
1010
localStorageGetItem,
1111
localStorageSetItem,
@@ -93,10 +93,10 @@ function createBridgeAndStore() {
9393

9494
store = new Store(bridge, {
9595
isProfiling,
96-
supportsReloadAndProfile: IS_CHROME || IS_EDGE,
96+
supportsReloadAndProfile: __IS_CHROME__ || __IS_EDGE__,
9797
supportsProfiling,
9898
// At this time, the timeline can only parse Chrome performance profiles.
99-
supportsTimeline: IS_CHROME,
99+
supportsTimeline: __IS_CHROME__,
100100
supportsTraceUpdates: true,
101101
});
102102

@@ -218,8 +218,8 @@ function createComponentsPanel() {
218218
}
219219

220220
chrome.devtools.panels.create(
221-
IS_CHROME || IS_EDGE ? '⚛️ Components' : 'Components',
222-
IS_EDGE ? 'icons/production.svg' : '',
221+
__IS_CHROME__ || __IS_EDGE__ ? '⚛️ Components' : 'Components',
222+
__IS_EDGE__ ? 'icons/production.svg' : '',
223223
'panel.html',
224224
createdPanel => {
225225
componentsPanel = createdPanel;
@@ -257,8 +257,8 @@ function createProfilerPanel() {
257257
}
258258

259259
chrome.devtools.panels.create(
260-
IS_CHROME || IS_EDGE ? '⚛️ Profiler' : 'Profiler',
261-
IS_EDGE ? 'icons/production.svg' : '',
260+
__IS_CHROME__ || __IS_EDGE__ ? '⚛️ Profiler' : 'Profiler',
261+
__IS_EDGE__ ? 'icons/production.svg' : '',
262262
'panel.html',
263263
createdPanel => {
264264
profilerPanel = createdPanel;
@@ -444,7 +444,7 @@ const debouncedOnNavigatedListener = debounce(() => {
444444
chrome.devtools.network.onNavigated.addListener(debouncedOnNavigatedListener);
445445

446446
// Should be emitted when browser DevTools are closed
447-
if (IS_FIREFOX) {
447+
if (__IS_FIREFOX__) {
448448
// For some reason Firefox doesn't emit onBeforeUnload event
449449
window.addEventListener('unload', performFullCleanup);
450450
} else {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevTools';
44

5-
export const IS_EDGE: boolean = process.env.IS_EDGE;
6-
export const IS_FIREFOX: boolean = process.env.IS_FIREFOX;
7-
export const IS_CHROME: boolean = process.env.IS_CHROME;
8-
95
export function getBrowserTheme(): BrowserTheme {
10-
if (IS_CHROME) {
6+
if (__IS_CHROME__) {
117
// chrome.devtools.panels added in Chrome 18.
128
// chrome.devtools.panels.themeName added in Chrome 54.
139
return chrome.devtools.panels.themeName === 'dark' ? 'dark' : 'light';

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ module.exports = {
9090
minimizer: [
9191
new TerserPlugin({
9292
terserOptions: {
93-
compress: false,
93+
compress: {
94+
unused: true,
95+
dead_code: true,
96+
},
9497
mangle: {
9598
keep_fnames: true,
9699
},
@@ -113,6 +116,9 @@ module.exports = {
113116
__EXTENSION__: true,
114117
__PROFILE__: false,
115118
__TEST__: NODE_ENV === 'test',
119+
__IS_CHROME__: IS_CHROME,
120+
__IS_FIREFOX__: IS_FIREFOX,
121+
__IS_EDGE__: IS_EDGE,
116122
'process.env.DEVTOOLS_PACKAGE': `"react-devtools-extensions"`,
117123
'process.env.DEVTOOLS_VERSION': `"${DEVTOOLS_VERSION}"`,
118124
'process.env.EDITOR_URL': EDITOR_URL != null ? `"${EDITOR_URL}"` : null,
@@ -125,9 +131,6 @@ module.exports = {
125131
'process.env.LIGHT_MODE_DIMMED_WARNING_COLOR': `"${LIGHT_MODE_DIMMED_WARNING_COLOR}"`,
126132
'process.env.LIGHT_MODE_DIMMED_ERROR_COLOR': `"${LIGHT_MODE_DIMMED_ERROR_COLOR}"`,
127133
'process.env.LIGHT_MODE_DIMMED_LOG_COLOR': `"${LIGHT_MODE_DIMMED_LOG_COLOR}"`,
128-
'process.env.IS_CHROME': IS_CHROME,
129-
'process.env.IS_FIREFOX': IS_FIREFOX,
130-
'process.env.IS_EDGE': IS_EDGE,
131134
}),
132135
],
133136
module: {

0 commit comments

Comments
 (0)