Skip to content

cawa-93/rollup-plugin-dynamic-macros

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dynamic macros

A minimalist Rollup and Vite plugin for creating compile-time macros.

How it works

The work of the plugin is very similar to define. You define a pure function that returns some value. At compile time, each call to your function in the code will be evaluated and the inline value will be added to the assembly.

Usage

// source.js
const randomNum1 = randomNumMacro()
const randomNum2 = randomNumMacro()
// vite.config.js
import DynamicMacros from 'vite-plugin-dynamic-macros'

defineConfig({
    plugins: [
        DynamicMacros({
            macros: {
                randomNumMacro: () => Math.random()
            }
        })
    ]
})
// compiled.js
const randomNum1 = 0.051910135154368486
const randomNum2 = 0.3576022563333674

You also can pass arguments into macro function.

TypeScript

For TypeScript support you can easily define global functions.

// globals.d.ts
declare function randomNumMacro() : number