Repository: remix-run/react-router
File: packages/react-router-dev/vite/plugin.ts (TypeScript)

Code before:
          // using the URL without ?inline query because the HMR code was
          // relying on the implicit SSR-client module graph relationship.
          injectQuery(dep.url, "inline")
        : dep.url;

    let cssMod: unknown;
    if (ctx.reactRouterConfig.future.unstable_viteEnvironmentApi) {
      const cssDevHelperEnvironment =
        viteDevServer.environments[CSS_DEV_HELPER_ENVIRONMENT_NAME];
      invariant(
        cssDevHelperEnvironment &&
          vite.isRunnableDevEnvironment(cssDevHelperEnvironment),
        "Missing CSS dev helper environment"
      );
      cssMod = await cssDevHelperEnvironment.runner.import(url);
    } else {
      cssMod = await viteDevServer.ssrLoadModule(url);
    }

    invariant(
      typeof cssMod === "object" &&
        cssMod !== null &&
        "default" in cssMod &&
        typeof cssMod.default === "string",
      `Failed to load CSS for ${dep.url}`
    );

    return cssMod.default;
  };

  return [
    {
      name: "react-router",
      config: async (_viteUserConfig, _viteConfigEnv) => {
        // Preload Vite's ESM build up-front as soon as we're in an async context
        await preloadVite();

        // Ensure sync import of Vite works after async preload
        let vite = getVite();

        viteUserConfig = _viteUserConfig;
        viteConfigEnv = _viteConfigEnv;
        viteCommand = viteConfigEnv.command;

        // This is a compatibility layer for Vite 5. Default conditions were
        // automatically added to any custom conditions in Vite 5, but Vite 6
        // removed this behavior. Instead, the default conditions are overridden
        // by any custom conditions. If we wish to retain the default
        // conditions, we need to manually merge them using the provided default
        // conditions arrays exported from Vite. In Vite 5, these default
        // conditions arrays do not exist.

Review comment (suggestion):
The only consumer of this code threw an error if the result was `undefined`, so I've inlined that logic into this function now instead.

Code after:
          // using the URL without ?inline query because the HMR code was
          // relying on the implicit SSR-client module graph relationship.
          injectQuery(dep.url, "inline")
        : dep.url;

    let cssMod: unknown;
    if (ctx.reactRouterConfig.future.unstable_viteEnvironmentApi) {
      const cssDevHelperEnvironment =
        viteDevServer.environments[CSS_DEV_HELPER_ENVIRONMENT_NAME];
      invariant(cssDevHelperEnvironment, "Missing CSS dev helper environment");
      invariant(vite.isRunnableDevEnvironment(cssDevHelperEnvironment));
      cssMod = await cssDevHelperEnvironment.runner.import(url);
    } else {
      cssMod = await viteDevServer.ssrLoadModule(url);
    }

    invariant(
      typeof cssMod === "object" &&
        cssMod !== null &&
        "default" in cssMod &&
        typeof cssMod.default === "string",
      `Failed to load CSS for ${dep.file ?? dep.url}`
    );

    return cssMod.default;
  };

  return [
    {
      name: "react-router",
      config: async (_viteUserConfig, _viteConfigEnv) => {
        // Preload Vite's ESM build up-front as soon as we're in an async context
        await preloadVite();

        // Ensure sync import of Vite works after async preload
        let vite = getVite();

        viteUserConfig = _viteUserConfig;
        viteConfigEnv = _viteConfigEnv;
        viteCommand = viteConfigEnv.command;

        // This is a compatibility layer for Vite 5. Default conditions were
        // automatically added to any custom conditions in Vite 5, but Vite 6
        // removed this behavior. Instead, the default conditions are overridden
        // by any custom conditions. If we wish to retain the default
        // conditions, we need to manually merge them using the provided default
        // conditions arrays exported from Vite. In Vite 5, these default
        // conditions arrays do not exist.
        // https://vite.dev/guide/migration.html#default-value-for-resolve-conditions
        let viteClientConditions: string[] = [
          ...(vite.defaultClientConditions ?? []),