The actual process of metamorphosis is quite intense. The “skin” underneath the caterpillar’s final moult is what becomes the chrysalis, and everything else inside completely dissolves - leaving a slurry of goopy biomaterial in its wake.
Bodily function all but ceases for many caterpillar species at this stage. There are, however, select groups of cells that are designed to survive the carnage.
Caterpillars are all born with imaginal discs - small clusters of cells that will eventually develop into an adult butterfly that lay dormant until the rest of the caterpillar breaks down. Using the matter around them, the imaginal discs divide, specialise, and eventually turn into a brand new winged insect.
After the adult butterfly has completely developed, it breaks out of its chrysalis, reborn.