
Rollup Has a Free Plugin API That Powers Every Modern Bundler
Rollup is the bundler behind Vite, and its plugin API is the de facto standard. If you've written a Vite plugin, you've used Rollup's API. The Rollup Plugin Interface // rollup.config.js export default { input : " src/index.js " , output : [ { file : " dist/bundle.cjs.js " , format : " cjs " }, { file : " dist/bundle.esm.js " , format : " es " }, { file : " dist/bundle.umd.js " , format : " umd " , name : " MyLib " }, ], plugins : [ myPlugin ()], }; Writing a Plugin: Full Lifecycle function jsonPlugin () { return { name : " json " , // Resolve custom module IDs resolveId ( source ) { if ( source === " virtual:config " ) return source ; return null ; // Defer to other plugins }, // Load content for resolved IDs load ( id ) { if ( id === " virtual:config " ) { return `export default ${ JSON . stringify ({ version : " 1.0 " })} ` ; } return null ; }, // Transform file contents transform ( code , id ) { if ( ! id . endsWith ( " .json " )) return null ; const parsed = JSON . parse ( code );
Continue reading on Dev.to Webdev
Opens in a new tab


