0.0.3 • Published 1 year ago

rollup-plugin-dynamic-macros v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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
0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago