Hello Evaa! This is a great question!! I don’t really know the answer. I’ve been trying to do some digging to look it up and its a hard one to find. I finally found a reasonable explanation on the Lepidoterists’ Society webpage (http://www.lepsoc.org/frequently_asked_questions.php#10). Lepidoptera are apparently an order of insects including butterflies and moths, who knew?! Turns out that throughout the caterpillar’s lifetime it is producing a hormone that keeps it in its juvenile form – the caterpillar. After a number of molts of its exoskeleton the caterpillar stops producing this hormone, so instead of making a new exoskeleton it makes a hard pupal surface and enters a period of metamorphosis. The caterpillar is ready though! It has earlier in its life prepared a bunch of clumps of cells throughout its body that in the pupa start to transition to pairs of eyes, legs, wings etc. In the caterpillar these cells played no role, they were freeloaders but here they start to really shine! This period of transformation is intense, nearly all cells not in these clumps die and are recycled to help the growth of these special regions (though not all do – the nervous system for example is apparently maintained!). So this is the basic process – you can check out more details at the link above. Thanks for asking such a great question – I had never thought of the ‘what does it do inside before?’ – that was pretty interesting to learn!
I think its AMAZING!! Basically its like Sara said the caterpillar sheds its exoskeleton to reveal a shell underneath which hardens into the chrysalis. The caterpillar then turns into a jelly-like blob which reforms into a butterfly before it emerges after a few weeks. This is caused by the release of hormones by the caterpillar causing internal organs to be disassembled and then remodelled.