Why is buying a bike as a whole cheaper than buying separate parts and building it? At least in Indonesia it is.

Know why?
Economics of scale. 

For instance, when a bike manufacturer buys thousands of groupsets directly from a component manufacturer, they get a significantly better per unit price than a retailer can. 

The retailer not only must necessarily buy in lower volume, but also typically buys through one or more layers of middlemen, each layer adding their cut to the wholesale price.  

Thus, the typical replacement price for a single part is often double or more than the original cost to install it at the manufacturer of the bike.