declarePlatformsInPubspec function

String declarePlatformsInPubspec(
  1. String pubspec,
  2. List<String> platforms
)

Declares platforms under a top-level platforms: key, which is how a plugin with a web target tells pub.dev what it supports.

Web has no web: plugin entry and no flutter_web_plugins dependency: the generated platform shim picks the web bridge with a conditional import, so nothing has to be registered, and flutter_web_plugins does not exist in SDKs without a web target (DartNative), where depending on it stops the plugin resolving. The bundled assets/web/ entry is written by linkWeb.

Implementation

String declarePlatformsInPubspec(String pubspec, List<String> platforms) {
  if (RegExp(r'^platforms:', multiLine: true).hasMatch(pubspec)) return pubspec;
  final declared = (platforms.toList()..sort()).map((name) => '  $name:\n').join();
  return pubspec.replaceFirst(RegExp(r'^environment:', multiLine: true), 'platforms:\n$declared\nenvironment:');
}