Skip to content

Commit 529a533

Browse files
authored
Verify metro.config.js when start debugging (microsoft#2023)
1 parent a1824f6 commit 529a533

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/common/reactNativeProjectHelper.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import * as fs from "fs";
55
import * as path from "path";
66
import { ProjectVersionHelper } from "./projectVersionHelper";
7+
import { OutputChannelLogger } from "../extension/log/OutputChannelLogger";
78

89
export interface ParsedPackage {
910
packageName: string;
@@ -96,4 +97,22 @@ export class ReactNativeProjectHelper {
9697
);
9798
return hermesEnabled;
9899
}
100+
101+
public static async verifyMetroConfigFile(projectRoot: string) {
102+
const logger = OutputChannelLogger.getChannel(OutputChannelLogger.MAIN_CHANNEL_NAME, true);
103+
const version = await ProjectVersionHelper.getReactNativeVersions(projectRoot);
104+
const metroConfigPath = path.join(projectRoot, "metro.config.js");
105+
const content = fs.readFileSync(metroConfigPath, "utf-8");
106+
const isNewMetroConfig = content.includes("getDefaultConfig");
107+
if (parseInt(version.reactNativeVersion.substring(2, 4)) <= 72 && !isNewMetroConfig) {
108+
logger.warning(
109+
'The version of "metro.config.js" in current project will be deprecated from rn 0.73, please update your "metro.config.js" file according to template: https://github.com/facebook/react-native/blob/main/packages/react-native/template/metro.config.js',
110+
);
111+
} else if (parseInt(version.reactNativeVersion.substring(2, 4)) > 72 && !isNewMetroConfig) {
112+
// As official mentioned, the new version of metro config will be required from 0.73, will enable this once the old version of config is totally disabled.
113+
// throw new Error(
114+
// 'The version of "metro.config.js" in current project is deprecated, please update your "metro.config.js" file according to template: https://github.com/facebook/react-native/blob/main/packages/react-native/template/metro.config.js',
115+
// );
116+
}
117+
}
99118
}

src/debugger/direct/directDebugSession.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { BaseCDPMessageHandler } from "../../cdp-proxy/CDPMessageHandlers/baseCD
2424
import { TipNotificationService } from "../../extension/services/tipsNotificationsService/tipsNotificationService";
2525
import { RNSession } from "../debugSessionWrapper";
2626
import { SettingsHelper } from "../../extension/settingsHelper";
27+
import { ReactNativeProjectHelper } from "../../common/reactNativeProjectHelper";
2728
import { IWDPHelper } from "./IWDPHelper";
2829

2930
nls.config({
@@ -78,6 +79,7 @@ export class DirectDebugSession extends DebugSessionBase {
7879

7980
try {
8081
try {
82+
await ReactNativeProjectHelper.verifyMetroConfigFile(launchArgs.cwd);
8183
await this.initializeSettings(launchArgs);
8284
logger.log("Launching the application");
8385
logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`);
@@ -139,6 +141,9 @@ export class DirectDebugSession extends DebugSessionBase {
139141
this.previousAttachArgs = attachArgs;
140142

141143
try {
144+
if (attachArgs.request === "attach") {
145+
await ReactNativeProjectHelper.verifyMetroConfigFile(attachArgs.cwd);
146+
}
142147
await this.initializeSettings(attachArgs);
143148

144149
const packager = this.appLauncher.getPackager();

src/debugger/rnDebugSession.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from "./debugSessionBase";
2222
import { JsDebugConfigAdapter } from "./jsDebugConfigAdapter";
2323
import { RNSession } from "./debugSessionWrapper";
24+
import { ReactNativeProjectHelper } from "../common/reactNativeProjectHelper";
2425

2526
nls.config({
2627
messageFormat: nls.MessageFormat.bundle,
@@ -56,6 +57,7 @@ export class RNDebugSession extends DebugSessionBase {
5657
): Promise<void> {
5758
try {
5859
try {
60+
await ReactNativeProjectHelper.verifyMetroConfigFile(launchArgs.cwd);
5961
await this.initializeSettings(launchArgs);
6062
logger.log("Launching the application");
6163
logger.verbose(`Launching the application: ${JSON.stringify(launchArgs, null, 2)}`);
@@ -98,6 +100,9 @@ export class RNDebugSession extends DebugSessionBase {
98100

99101
return new Promise<void>(async (resolve, reject) => {
100102
try {
103+
if (attachArgs.request === "attach") {
104+
await ReactNativeProjectHelper.verifyMetroConfigFile(attachArgs.cwd);
105+
}
101106
await this.initializeSettings(attachArgs);
102107
logger.log("Attaching to the application");
103108
logger.verbose(

0 commit comments

Comments
 (0)