[Native] Refactor dependency tracking into collectAndMaybeSerializeDependencies utility function