Skip to content

Commit a1824f6

Browse files
authored
Add metro bundler field in app.json (microsoft#2021)
1 parent 907c0e3 commit a1824f6

6 files changed

Lines changed: 48 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ Expo support open application in browser, the expo web application is generated
383383
"cwd": "${workspaceFolder}",
384384
"platform": "expoweb",
385385
"browserTarget": "chrome",
386-
"url": "http://localhost:19006"
386+
"url": "http://localhost:8081"
387387
}
388388
]
389389
```

src/debugger/direct/directDebugSession.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import { PlatformType } from "../../extension/launchArgs";
2323
import { BaseCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/baseCDPMessageHandler";
2424
import { TipNotificationService } from "../../extension/services/tipsNotificationsService/tipsNotificationService";
2525
import { RNSession } from "../debugSessionWrapper";
26-
import { IWDPHelper } from "./IWDPHelper";
2726
import { SettingsHelper } from "../../extension/settingsHelper";
27+
import { IWDPHelper } from "./IWDPHelper";
2828

2929
nls.config({
3030
messageFormat: nls.MessageFormat.bundle,

src/debugger/webDebugSession.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { InternalErrorCode } from "../common/error/internalErrorCode";
1414
import { ReactNativeCDPProxy } from "../cdp-proxy/reactNativeCDPProxy";
1515
import { Request } from "../common/node/request";
1616
import { PromiseUtil } from "../common/node/promise";
17+
import { FileSystem } from "../common/node/fileSystem";
18+
import { stripJsonTrailingComma } from "../common/utils";
1719
import { MultipleLifetimesAppWorker } from "./appWorker";
1820
import {
1921
DebugSessionBase,
@@ -66,6 +68,7 @@ export class WebDebugSession extends DebugSessionBase {
6668
logger.log("Launching the application");
6769
logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`);
6870

71+
await this.updateWebpackMetroConfig(launchArgs);
6972
await this.verifyExpoWebRequiredDependencies(launchArgs);
7073
await this.appLauncher.launchExpoWeb(launchArgs);
7174
await this.waitExpoWebIsRunning(launchArgs);
@@ -215,24 +218,48 @@ export class WebDebugSession extends DebugSessionBase {
215218
}
216219
}
217220

221+
private async updateWebpackMetroConfig(launchArgs: any): Promise<void> {
222+
logger.log("Checking expo webpack-config for project.");
223+
224+
const exponentHelper = this.appLauncher.getPackager().getExponentHelper();
225+
const sdkVersion = await exponentHelper.exponentSdk(true);
226+
if (parseInt(sdkVersion.substring(0, 2)) >= 49) {
227+
// If Expo SDK >= 49, add web metro bundler in app.json for expo web debugging
228+
const appJsonPath = path.join(launchArgs.cwd, "app.json");
229+
const fs = new FileSystem();
230+
const appJson = await fs.readFile(appJsonPath).then(content => {
231+
return stripJsonTrailingComma(content.toString());
232+
});
233+
234+
if (!appJson.expo.web.bundler) {
235+
logger.log("Add metro bundler field to app.json.");
236+
appJson.expo.web.bundler = "metro";
237+
await fs.writeFile(appJsonPath, JSON.stringify(appJson, null, 2));
238+
}
239+
} else {
240+
// If Expo SDK < 49, using @expo/webpack-config for expo web debugging
241+
const nodeModulePath = path.join(launchArgs.cwd, "node_modules");
242+
const expoWebpackConfigPath = path.join(nodeModulePath, "@expo", "webpack-config");
243+
if (!fs.existsSync(expoWebpackConfigPath)) {
244+
logger.log("@expo/webpack-config is not found in current project.");
245+
throw new Error(
246+
"Required dependencies not found: Please check and install @expo/webpack-config by running: npx expo install @expo/webpack-config.",
247+
);
248+
}
249+
}
250+
}
251+
218252
private verifyExpoWebRequiredDependencies(launchArgs: any): void {
219253
logger.log("Checking expo web required dependencies");
220254
const nodeModulePath = path.join(launchArgs.cwd, "node_modules");
221-
const expoMetroConfigPath = path.join(nodeModulePath, "@expo", "metro-config");
222255
const reactDomPath = path.join(nodeModulePath, "react-dom");
223256
const reactNativeWebPath = path.join(nodeModulePath, "react-native-web");
224-
if (
225-
fs.existsSync(expoMetroConfigPath) &&
226-
fs.existsSync(reactDomPath) &&
227-
fs.existsSync(reactNativeWebPath)
228-
) {
257+
if (fs.existsSync(reactDomPath) && fs.existsSync(reactNativeWebPath)) {
229258
logger.log("All required dependencies installed");
230259
} else {
231-
logger.log(
232-
"Please install react-native-web@~0.19.6, react-dom@18.2.0, @expo/webpack-config by running: npx expo install react-native-web@~0.19.6 react-dom@18.2.0 @expo/webpack-config",
233-
);
260+
logger.log("react-native-web, react-dom is not found in current project.");
234261
throw new Error(
235-
"Required dependencies not found: Please check and install react-native-web, react-dom, @expo/webpack-config by running: npx expo install react-native-web react-dom @expo/webpack-config",
262+
"Required dependencies not found: Please check and install react-native-web, react-dom by running: npx expo install react-native-web react-dom",
236263
);
237264
}
238265
}
@@ -257,7 +284,10 @@ export class WebDebugSession extends DebugSessionBase {
257284
running => running,
258285
retryCount,
259286
delay,
260-
localize("ExpoWebIsNotRunning", "Expo web is not running"),
287+
localize(
288+
"ExpoWebIsNotRunning",
289+
"Expo web is not running, please check metro status and browser launching url.",
290+
),
261291
);
262292
} catch (error) {
263293
throw error;

src/extension/debuggingConfiguration/configurationProviders/debugConfigProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class DebugConfigProvider extends BaseConfigProvider {
7070
return async () => {
7171
await this.configureBrowserTarget(input, state.config);
7272
await this.configureApplicationType(input, state.config);
73-
state.config.url = "http://localhost:19006";
73+
state.config.url = "http://localhost:8081";
7474
};
7575
}
7676
return;

src/extension/exponent/exponentHelper.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ declare interface ExpConfig {
1414
public version?: string;
1515
public entryPoint?: string;
1616
public packagerOpts?: ExpConfigPackager;
17+
public android?: [];
18+
public ios?: [];
19+
public web?:[];
1720
}
1821

1922
declare interface ExpMetroConfig {

src/extension/exponent/exponentHelper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ require('${entryPoint}');`;
364364
* Exponent sdk version that maps to the current react-native version
365365
* If react native version is not supported it returns null.
366366
*/
367-
private async exponentSdk(showProgress: boolean = false): Promise<string> {
367+
public async exponentSdk(showProgress: boolean = false): Promise<string> {
368368
if (showProgress) {
369369
this.logger.logStream("...");
370370
}

0 commit comments

Comments
 (0)