[Refactoring] Add Module::rootImports() and use it in TemplateInstance::needsCodegen()

it would make compilation a little faster so `rootImports` cache the result.