Oftentimes it's handy to compute some data at build time, like querying an API endpoint, so that it's faster for the client. This plugin makes it really simple.
yarn add -D rollup-plugin-computed
# or using npm
npm i -D rollup-plugin-computed
// rollup.config.js or vite.config.js
import computed from 'rollup-plugin-computed';
const computers = {
test() {
return { hello: 'world' };
},
image: {
// bundle file, even though nothing in the app imports it
alwaysBuild: true,
type: 'asset', // chunk by default
fileExt: 'svg', // required for assets
// will be saved to dist/image.svg (rollup applies image-hash.svg by default)
fileName: 'image.svg',
fn() {
return '<svg>...</svg>';
}
}
};
export default {
plugins: [computed({ computers })]
};
You can access the computed data like this in your app:
import data from 'test.computed';
console.log(data);
// { hello: 'world' }
More more info, head over to the documentation.