compile-time eval for rollup
performant:
eval static expressions at compile time
allow shorter load times for client
readable:
avoid "magic numbers"
colocate "all the code" in one place
powerful:
use any node package on compile time
license = CC0-1.0 = public domain
warranty = none
npm install -D https://github.com/milahu/rollup-plugin-preval.gitmerge this with your rollup.config.js
import { preval } from 'rollup-plugin-preval';
export default {
plugins: [
preval({ basedir: __dirname }), // directory of rollup.config.js
],
};// preval object
let testPrevalObj = preval(() => ({
a: Math.PI * 0.5,
b: Math.sqrt(2),
}));
// result
let testPrevalObj = {
a: 1.5707963267948966,
b: 1.4142135623730951
};yes you can use modules inside the preval code
let testPreval = preval(() => {
const fs = require('fs');
return fs.readFileSync('input.txt').toString();
});require works relative to the directory of rollup-plugin-preval.js, so
let res = preval(() => require('./src/script.js').someProp);will try to require node_modules/rollup-plugin-preval/src/src/script.js
you could fix that with a relative path like ../../../src/script.js
but this is not portable
for example this breaks with pnpm package manager
cos symlinks are unidirectional and ../../../ is the pnpm global store
better solution: use absolute paths
in your rollup.config.js set
preval({
basedir: __dirname, // directory of rollup.config.js
}),and in your script use
let res = preval(({basedir}) => require(basedir+'/src/script.js').someProp);