Experimental port of Bun macros into Rollup.js.
npm install @comandeer/rollup-plugin-macros --save-dev
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);
I've written a detailed article in Polish about inner workings of this package.
See LICENSE file for details.