milahu/rollup-plugin-preval(新しいタブで開きます)
主要言語: JavaScript
compile-time eval for rollup
- star 数
- 3
- watcher 数
- 1
- fork 数
- 1
- issue 数
- 0
- 最終更新
- 2020/11/23
主要言語: JavaScript
compile-time eval for rollup
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.git
merge 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);