note
note copied to clipboard
介绍一下 rollup 插件,以及插件的开发规范?
https://rollupjs.org/guide/en/#plugins-overview
rollup 插件可以让我们自定义 rollup 的行为,它是一个函数,返回一个描述对象,这个对象可以包含:
-
properties
- name: 插件名,打印错误和警告信息时有用
-
build hooks
- #79
- output generation hooks
简单示例
// rollup-plugin-my-example.js
export default function myExample () {
return {
name: 'my-example', // this name will show up in warnings and errors
resolveId ( source ) {
if (source === 'virtual-module') {
return source; // this signals that rollup should not ask other plugins or check the file system to find this id
}
return null; // other ids should be handled as usually
},
load ( id ) {
if (id === 'virtual-module') {
return 'export default "This is virtual!"'; // the source code for "virtual-module"
}
return null; // other ids should be handled as usually
}
};
}
// rollup.config.js
import myExample from './rollup-plugin-my-example.js';
export default ({
input: 'virtual-module', // resolved by our plugin
plugins: [myExample()],
output: [{
file: 'bundle.js',
format: 'es'
}]
});