Atmospheric mass loss during planet formation: The importance of planetesimal impacts