@@ -14,6 +14,8 @@ import { InternalErrorCode } from "../common/error/internalErrorCode";
1414import { ReactNativeCDPProxy } from "../cdp-proxy/reactNativeCDPProxy" ;
1515import { Request } from "../common/node/request" ;
1616import { PromiseUtil } from "../common/node/promise" ;
17+ import { FileSystem } from "../common/node/fileSystem" ;
18+ import { stripJsonTrailingComma } from "../common/utils" ;
1719import { MultipleLifetimesAppWorker } from "./appWorker" ;
1820import {
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 ;
0 commit comments