0.1.0 • Published 11 months ago

@comandeer/rollup-plugin-macros v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@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.