Minecraft wiki entry for Anvil_mechanics, associated text: Enchantments that are added to raw materials (e.g. an iron ingot with Sharpness III) are ignored when doing unit repair, not combined.
Because prior work penalty is charged for any rename, it is most economical to rename a weapon before repairing or enchanting it, minimizing the penalty the player must pay for the rename.