Repository: expo/expo
File: packages/@expo/cli/src/start/server/metro/instantiateMetro.ts (TypeScript)

Code before:
      metroMiddleware: any,
      server: MetroServer
    ) => {
      if (customEnhanceMiddleware) {
        metroMiddleware = customEnhanceMiddleware(metroMiddleware, server);
      }
      return middleware.use(metroMiddleware);
    };

    const devtoolsWebsocketEndpoints = createDevToolsPluginWebsocketEndpoint();
    Object.assign(websocketEndpoints, devtoolsWebsocketEndpoints);
  }

  // Attach Expo Atlas if enabled
  await attachAtlasAsync({
    isExporting,
    exp,
    projectRoot,
    middleware,
    metroConfig,
    // NOTE(cedric): reset the Atlas file once, and reuse it for static exports
    resetAtlasFile: isExporting,
  });

  // Support HTTPS based on the metro's tls server config
  // @ts-ignore- the tls option will be added in the latest metro config
  const { tls } = metroConfig.server;
  const secureServerOptions = !tls
    ? undefined
    : {
        key: tls.key,
        cert: tls.cert,
        ca: tls.ca,
        requestCert: tls.requestCert,
      };

  const { server, hmrServer, metro } = await runServer(
    metroBundler,
    metroConfig,
    {
      host: options.host,
      websocketEndpoints,
      watch: !isExporting && isWatchEnabled(),
      secureServerOptions,
    },
    {
      mockServer: isExporting,
    }
  );

  // Patch transform file to remove inconvenient customTransformOptions which are only used in single well-known files.

Review comment (suggestion):
Let's use a cast here please. We've had issues with excessive `ts-ignore` comments, since if they shift lines, or an unrelated error occurs (rather than what's expected), they still mute the error. We've been trying to reduce them over time since last year.

I'd also still be more comfortable if Metro's configuration added this first. We can speculatively add it. But generally this seems a bit suspect.

also nit: inverted `tls` check. Usually we write `tls ? { ... } : undefined` and such

Code after:
}> {
  const projectRoot = metroBundler.projectRoot;

  const {
    config: metroConfig,
    setEventReporter,
    reporter,
  } = await loadMetroConfigAsync(projectRoot, options, {
    exp,
    isExporting,
    getMetroBundler() {
      return metro.getBundler().getBundler();
    },
  });

  // Create the core middleware stack for Metro, including websocket listeners
  const { middleware, messagesSocket, eventsSocket, websocketEndpoints } =
    createMetroMiddleware(metroConfig);

  // Get local URL to Metro bundler server (typically configured as 127.0.0.1:8081)
  const serverBaseUrl = metroBundler
    .getUrlCreator()
    .constructUrl({ scheme: 'http', hostType: 'localhost' });

  if (!isExporting) {
    // Enable correct CORS headers for Expo Router features
    prependMiddleware(middleware, createCorsMiddleware(exp));

    // Enable debug middleware for CDP-related debugging
    const { debugMiddleware, debugWebsocketEndpoints } = createDebugMiddleware({
      serverBaseUrl,
      reporter,
    });
    Object.assign(websocketEndpoints, debugWebsocketEndpoints);
    middleware.use(debugMiddleware);
    middleware.use('/_expo/debugger', createJsInspectorMiddleware());

    // TODO(cedric): `enhanceMiddleware` is deprecated, but is currently used to unify the middleware stacks
    // See: https://github.com/facebook/metro/commit/22e85fde85ec454792a1b70eba4253747a2587a9
    // See: https://github.com/facebook/metro/commit/d0d554381f119bb80ab09dbd6a1d310b54737e52
    const customEnhanceMiddleware = metroConfig.server.enhanceMiddleware;
    asWritable(metroConfig.server).enhanceMiddleware = (
      metroMiddleware: any,
      server: MetroServer
    ) => {
      if (customEnhanceMiddleware) {
        metroMiddleware = customEnhanceMiddleware(metroMiddleware, server);
      }
      return middleware.use(metroMiddleware);
    };