Skip to content

Compiler

To provide these abstractions without sacrifising performance, Mapl uses a compiler to optimize the fetch handler before request time.

JIT compilation

Mapl exposes jitc() to compile an instance to a fetch handler with new Function():

import { jitc } from '@mapl/app';
const result = await jitc(app);
result.fetch; // The fetch function

AOT compilation

You can precompile the fetch handler content with aotfn():

import { aotfn } from '@mapl/app';
writeFileSync('fetch.mjs', `export default ${aotfn(app)};`);

Then load with necessary dependencies to create a fetch function:

import createFetch from './fetch.mjs';
import { aotdeps } from '@mapl/app';
const result = await createFetch(aotdeps(app));

This approach often results in 5x faster startup time than JIT compilation.

Since AOT compilation does not use new Function(), this unblocks Mapl on edge runtimes where eval() and new Function() are not allowed (Cloudflare Workers, Vercel Edge, …).

Compiler options

You can pass an option object to aotfn and jitc.

exposeStatic

If set to true, prebuilt routes will be exposed as the static property in the final compilation result instead of registered to the router.

Example usage with Bun:

import { router, jitc } from '@mapl/app';
// Build a static route
const app = router().build('/', () => 'Hi');
// Serve directly as Bun handles `fetch` and
// `static` property automatically
export default await jitc(app, {
exposeStatic: true
});