Menu

1 / 2

Rollup Has a Free Plugin API That Powers Every Modern Bundler

DEV Community: rollup·Alex Spinov·5 months ago
#T9b2KSYr
#dev#bundle#return#rollup#article
Reading 0:00
15s threshold

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 ()], };

Enter fullscreen mode

Exit fullscreen mode

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.