A compactness result in the gradient theory of phase transitions