@rollup/plugin-image导入JPG、PNG、GIF、SVG和WebP文件的rollup插件。图像使用base64编码,这意味着它们将比磁盘上的大小大33%。因此,您应该只对启动时可用的小图像(例如,在不协调异步加载多个图像的情况下立即渲染到画布)的便利性大于成本的情况下使用此选项。
安装:
npm install @rollup/plugin-image --save-dev
rollup.config.js配置:
import image from '@rollup/plugin-image';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [image()]
};
使用:
import logo from './rollup.png';
console.log(logo);
但如果图片太多太大不建议用rollup打包,因为要装的各种插件实在太多了,rollup主要针对纯js的模块化打包解决方案。