As a cleaner, WD-40 is just very expensive kerosene in a handy spray container. You can use it as a cleaner/degreaser but wipe off all of the excess or, as noted, it will be a dirt magnet in the future.
To clean the pulleys of a rear derailleur, all you have to do is take out the two axle bolts and separate the cage. Wipe off the cage plates and scrub the pulleys clean with a solvent dampened cloth. You can remove the dust covers and bushings to get the interior clean. Add a few drops of oil and reassemble your now sparkling clean derailleur.