Repository: medusajs/medusa
File: packages/admin/admin-bundler/src/utils/config.ts (TypeScript)

Code before:
        entry: root,
        sources: options.sources,
        plugins: options.plugins,
      }),
      react(),
      medusa({
        sources: options.sources,
      }),
    ],
  }

  // Inject plugin environment variables as vite defines
  for (const plugin of options.plugins ?? []) {
    const normalizedName = plugin.replace(/\/admin$/, "")
      .replace(/^@/, "")
      .replace(/[^a-zA-Z0-9]/g, "_")
      .toUpperCase()
    const pluginEnv: Record<string, string | undefined> = {}
    for (const envVar in process.env) {
      if (envVar.startsWith(`PLUGIN_${normalizedName}_`)) {
        pluginEnv[envVar] = process.env[envVar];
      }
    }
    baseConfig.define!["process.env"] = JSON.stringify(pluginEnv);
  }

  if (options.vite) {
    const customConfig = options.vite(baseConfig)
    return mergeConfig(baseConfig, customConfig)
  }

  return baseConfig
}

function getHmrConfig(hmrPort: number): HmrOptions | boolean {
  const options: HmrOptions = {
    port: hmrPort,
  }

  if (process.env.HMR_PROTOCOL) {
    options.protocol = process.env.HMR_PROTOCOL
  }
  if (process.env.HMR_HOST) {
    options.host = process.env.HMR_HOST
  }
  if (process.env.HMR_CLIENT_PORT) {
    options.clientPort = parseInt(process.env.HMR_CLIENT_PORT)
  }

  return options
}

Review comment (suggestion):
suggestion: Here it seems that for every plugin loop we are overriding the `process.env` in define. Also, I propose a little change to this, let me know what you think
```suggestion
const pluginEnv = Object.entries(process.env).filter(([key]) =>
    key.startsWith("PLUGIN_")
  )
  const pluginEnvMap: Record<string, string | undefined> = {}
  if (pluginEnv.length) {
    for (const plugin of options.plugins ?? []) {
      const normalizedName = plugin
        .replace(/\/admin$/, "")
        .replace(/^@/, "")
        .replace(/[^a-zA-Z0-9]/g, "_")
        .toUpperCase()
      for (const [env, value] of pluginEnv) {
        if (env.startsWith(`PLUGIN_${normalizedName}_`)) {
          pluginEnvMap[env] = value
        }
      }
    }
    baseConfig.define!["process.env"] = JSON.stringify(pluginEnvMap)
  }
```

Code after:
        entry: root,
        sources: options.sources,
        plugins: options.plugins,
      }),
      react(),
      medusa({
        sources: options.sources,
      }),
    ],
  }

  // Inject plugin environment variables with vite define
  const pluginEnv: Record<string, string | undefined> = { BACKEND_URL: backendUrl }
  for (const [key, value] of Object.entries(process.env)) {
    if (key.startsWith("PLUGIN_")) {
      pluginEnv[key.replace(/^PLUGIN_/, "")] = value
    }
  }
  baseConfig.define!["process.env"] = JSON.stringify(pluginEnv)

  if (options.vite) {
    const customConfig = options.vite(baseConfig)
    return mergeConfig(baseConfig, customConfig)
  }

  return baseConfig
}

function getHmrConfig(hmrPort: number): HmrOptions | boolean {
  const options: HmrOptions = {
    port: hmrPort,
  }

  if (process.env.HMR_PROTOCOL) {
    options.protocol = process.env.HMR_PROTOCOL
  }
  if (process.env.HMR_HOST) {
    options.host = process.env.HMR_HOST
  }
  if (process.env.HMR_CLIENT_PORT) {
    options.clientPort = parseInt(process.env.HMR_CLIENT_PORT)
  }

  return options
}