Skip to content

Commit 2c4c847

Browse files
authored
refactor: resolve browser via env variables based on build rather than user agent (#27179)
Fixes #26911, #26860. Currently, we are parsing user agent string to determine which browser is running the extension. This doesn't work well with custom user agents, and sometimes when user turns on mobile dev mode in Firefox, we stop resolving that this is a Firefox browser, extension starts to use Chrome API's and fails to inject. Changes: Since we are building different extensions for all supported browsers (Chrome, Firefox, Edge), we predefine env variables for browser resolution, which are populated in a build step.
1 parent 456d153 commit 2c4c847

File tree

5 files changed

+40
-33
lines changed

5 files changed

+40
-33
lines changed

packages/react-devtools-extensions/build.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,25 @@ const preProcess = async (destinationPath, tempPath) => {
5252
await ensureDir(tempPath); // Create temp dir for this new build
5353
};
5454

55-
const build = async (tempPath, manifestPath) => {
55+
const build = async (tempPath, manifestPath, envExtension = {}) => {
5656
const binPath = join(tempPath, 'bin');
5757
const zipPath = join(tempPath, 'zip');
58+
const mergedEnv = {...process.env, ...envExtension};
5859

5960
const webpackPath = join(__dirname, 'node_modules', '.bin', 'webpack');
6061
execSync(
6162
`${webpackPath} --config webpack.config.js --output-path ${binPath}`,
6263
{
6364
cwd: __dirname,
64-
env: process.env,
65+
env: mergedEnv,
6566
stdio: 'inherit',
6667
},
6768
);
6869
execSync(
6970
`${webpackPath} --config webpack.backend.js --output-path ${binPath}`,
7071
{
7172
cwd: __dirname,
72-
env: process.env,
73+
env: mergedEnv,
7374
stdio: 'inherit',
7475
},
7576
);
@@ -132,16 +133,32 @@ const postProcess = async (tempPath, destinationPath) => {
132133
await remove(tempPath); // Clean up temp directory and files
133134
};
134135

136+
const SUPPORTED_BUILDS = ['chrome', 'firefox', 'edge'];
137+
135138
const main = async buildId => {
139+
if (!SUPPORTED_BUILDS.includes(buildId)) {
140+
throw new Error(
141+
`Unexpected build id - "${buildId}". Use one of ${JSON.stringify(
142+
SUPPORTED_BUILDS,
143+
)}.`,
144+
);
145+
}
146+
136147
const root = join(__dirname, buildId);
137148
const manifestPath = join(root, 'manifest.json');
138149
const destinationPath = join(root, 'build');
139150

151+
const envExtension = {
152+
IS_CHROME: buildId === 'chrome',
153+
IS_FIREFOX: buildId === 'firefox',
154+
IS_EDGE: buildId === 'edge',
155+
};
156+
140157
try {
141158
const tempPath = join(__dirname, 'build', buildId);
142159
await ensureLocalBuild();
143160
await preProcess(destinationPath, tempPath);
144-
await build(tempPath, manifestPath);
161+
await build(tempPath, manifestPath, envExtension);
145162

146163
const builtUnpackedPath = join(destinationPath, 'unpacked');
147164
await postProcess(tempPath, destinationPath);

packages/react-devtools-extensions/edge/build.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const chalk = require('chalk');
66
const {execSync} = require('child_process');
77
const {join} = require('path');
88
const {argv} = require('yargs');
9+
910
const build = require('../build');
1011

1112
const main = async () => {
@@ -15,15 +16,7 @@ const main = async () => {
1516

1617
const cwd = join(__dirname, 'build');
1718
if (crx) {
18-
const crxPath = join(
19-
__dirname,
20-
'..',
21-
'..',
22-
'..',
23-
'node_modules',
24-
'.bin',
25-
'crx'
26-
);
19+
const crxPath = join(__dirname, '..', 'node_modules', '.bin', 'crx');
2720

2821
execSync(`${crxPath} pack ./unpacked -o ReactDevTools.crx`, {
2922
cwd,

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,9 @@
22

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

5-
export const IS_EDGE = navigator.userAgent.indexOf('Edg') >= 0;
6-
export const IS_FIREFOX = navigator.userAgent.indexOf('Firefox') >= 0;
7-
export const IS_CHROME = IS_EDGE === false && IS_FIREFOX === false;
8-
9-
export type BrowserName = 'Chrome' | 'Firefox' | 'Edge';
10-
11-
export function getBrowserName(): BrowserName {
12-
if (IS_EDGE) {
13-
return 'Edge';
14-
}
15-
if (IS_FIREFOX) {
16-
return 'Firefox';
17-
}
18-
if (IS_CHROME) {
19-
return 'Chrome';
20-
}
21-
throw new Error(
22-
'Expected browser name to be one of Chrome, Edge or Firefox.',
23-
);
24-
}
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;
258

269
export function getBrowserTheme(): BrowserTheme {
2710
if (IS_CHROME) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ const __DEV__ = NODE_ENV === 'development';
3434

3535
const DEVTOOLS_VERSION = getVersionString(process.env.DEVTOOLS_VERSION);
3636

37+
const IS_CHROME = process.env.IS_CHROME === 'true';
38+
const IS_FIREFOX = process.env.IS_FIREFOX === 'true';
39+
const IS_EDGE = process.env.IS_EDGE === 'true';
40+
3741
const featureFlagTarget = process.env.FEATURE_FLAG_TARGET || 'extension-oss';
3842

3943
module.exports = {
@@ -79,6 +83,9 @@ module.exports = {
7983
'process.env.LIGHT_MODE_DIMMED_WARNING_COLOR': `"${LIGHT_MODE_DIMMED_WARNING_COLOR}"`,
8084
'process.env.LIGHT_MODE_DIMMED_ERROR_COLOR': `"${LIGHT_MODE_DIMMED_ERROR_COLOR}"`,
8185
'process.env.LIGHT_MODE_DIMMED_LOG_COLOR': `"${LIGHT_MODE_DIMMED_LOG_COLOR}"`,
86+
'process.env.IS_CHROME': IS_CHROME,
87+
'process.env.IS_FIREFOX': IS_FIREFOX,
88+
'process.env.IS_EDGE': IS_EDGE,
8289
}),
8390
new DevToolsIgnorePlugin({
8491
shouldIgnorePath: function (path) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ const DEVTOOLS_VERSION = getVersionString(process.env.DEVTOOLS_VERSION);
3535
const EDITOR_URL = process.env.EDITOR_URL || null;
3636
const LOGGING_URL = process.env.LOGGING_URL || null;
3737

38+
const IS_CHROME = process.env.IS_CHROME === 'true';
39+
const IS_FIREFOX = process.env.IS_FIREFOX === 'true';
40+
const IS_EDGE = process.env.IS_EDGE === 'true';
41+
3842
const featureFlagTarget = process.env.FEATURE_FLAG_TARGET || 'extension-oss';
3943

4044
const babelOptions = {
@@ -105,6 +109,9 @@ module.exports = {
105109
'process.env.LIGHT_MODE_DIMMED_WARNING_COLOR': `"${LIGHT_MODE_DIMMED_WARNING_COLOR}"`,
106110
'process.env.LIGHT_MODE_DIMMED_ERROR_COLOR': `"${LIGHT_MODE_DIMMED_ERROR_COLOR}"`,
107111
'process.env.LIGHT_MODE_DIMMED_LOG_COLOR': `"${LIGHT_MODE_DIMMED_LOG_COLOR}"`,
112+
'process.env.IS_CHROME': IS_CHROME,
113+
'process.env.IS_FIREFOX': IS_FIREFOX,
114+
'process.env.IS_EDGE': IS_EDGE,
108115
}),
109116
],
110117
module: {

0 commit comments

Comments
 (0)