Skip to content

Comandeer/rollup-plugin-macros

Repository files navigation

@comandeer/rollup-plugin-macros

Build Status codecov npm (scoped)

Experimental port of Bun macros into Rollup.js.

Installation

npm install @comandeer/rollup-plugin-macros --save-dev

Usage

Add it to your Rollup.js configuration:

import macros from '@comandeer/rollup-plugin-macros';

export default {
    input: './src/index.js',
    output: [
		{ file: './dist/bundle.js', format: 'es' }
	],
    plugins: [
		macros()
	],
};

export default config;

Now you can create a module with a macro, e.g. random.js:

function random() {
	return Math.random();
}

export { random };

And then import it in your code with appropriate import attribute:

import { random } from './random.js' with { type: 'macro' };
// or using the older syntax
// import { random } from './random.js' assert { type: 'macro' };

console.log( random() );

After bundling the code with Rollup the import will be removed and the random() call replaced with an actual value returned by the macro:

console.log(0.7507075485199182);

How does it work?

I've written a detailed article in Polish about inner workings of this package.

License

See LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published