A code generation system.
import{IS_AOT,IS_BUILD}from'runtime-compiler/env';import{evaluate}from'runtime-compiler';// Only log when buildingIS_BUILD&&console.log('Building...');// Only emit code if not in AOT modeconstfn=evaluate<()=>void>(IS_AOT||`return()=>console.log("Hi")`);// Run only when not in build modeIS_BUILD||fn();Build with rolldown:
...
importrtcfrom'runtime-compiler/rolldown';exportdefaultdefineConfig({plugins: [rtc()]});Or Vite:
importrtcfrom'runtime-compiler/rolldown';exportdefaultdefineConfig({// Only run this plugin on buildbuild: {rolldownOptions: {plugins: [rtc()]}}});If you have dependencies marked as external that uses runtime-compiler:
// package: external-pkgimport{IS_AOT}from'runtime-compiler/env';import{evaluate}from'runtime-compiler';exportconstfn=evaluate(IS_AOT||'()=>console.log("Hi")');// index.tsimport{fn}from'external-pkg';fn();// buildexportdefault{
...,external: ['external-pkg', ...],plugins: [rtc({// Chunks that import 'external-pkg' will output loader insteaduseLoader: (pkg)=>pkg==='external-pkg';});]}Library code and startup code that uses runtime-compiler cannot be mixed together in the same chunk.
import{IS_AOT}from'runtime-compiler/env';import{evaluate}from'runtime-compiler';constfn=evaluate<typeofconsole.log>(IS_AOT||`return console.log`)('Hi');// AOT build will break this exportexportconstcreateFn=()=>evaluate<typeofconsole.log>(IS_AOT||`return console.log`);