using PrecompileTools

macro compile(block)
    return quote
        let
            figlike = $(esc(block))
            # We don't do something like colorbuffer(fig)
            # since we can't guarantee that the user has a browser setup
            # while precompiling
            # So we just do all parts of the stack we can do without browser
            session = Session()
            app = App(() -> DOM.div(figlike))
            dom = Bonito.session_dom(session, app)
            show(IOBuffer(), dom)
            Makie.second_resolve(figlike, :wgl_renderobject)
            close(session)
            yield()
            return nothing
        end
    end
end

let
    @compile_workload begin
        WGLMakie.activate!()
        include(Makie.SHARED_PRECOMPILE_PATH)
        # Cleanup globals to avoid serializing stale state (servers, sessions, fonts, figures, tasks)
        # Note: __init__ doesn't run during precompilation, so we must always clean up here
        Bonito.cleanup_globals()
        Makie.cleanup_globals()
        nothing
    end
end
