We present a new numerical framework to model the formation and evolution of giant planets. The code is based on the further development of the stellar evolution toolkit Modules for Experiments in Stellar Astrophysics. The model includes the dissolution of the accreted planetesimals/pebbles, which are assumed to be made of water ice, in the planetary gaseous envelope, and the effect of envelope enrichment on the planetary growth and internal structure is computed self-consistently. We apply our simulations to Jupiter and investigate the impact of different heavy-element and gas accretion rates on its formation history. We show that the assumed runaway gas accretion rate significantly affects the planetary radius and luminosity. It is confirmed that heavy-element enrichment leads to shorter formation timescales due to more efficient gas accretion. We find that with heavy-element enrichment Jupiter's formation timescale is compatible with typical disks' lifetimes even when assuming a low heavy-element accretion rate (oligarchic regime). Finally, we provide an approximation for the heavy-element profile in the innermost part of the planet, providing a link between the internal structure and the planetary growth history.