Back to articles
Unplugin Has a Free API That Most Developers Dont Know About

Unplugin Has a Free API That Most Developers Dont Know About

via Dev.to JavaScriptAlex Spinov

Unplugin lets you write bundler plugins once and they work across Vite, Webpack, Rollup, esbuild, and Rspack. Create a Universal Plugin import { createUnplugin } from " unplugin " ; const myPlugin = createUnplugin (( options ) => ({ name : " my-plugin " , transformInclude : ( id ) => id . endsWith ( " .vue " ) || id . endsWith ( " .tsx " ), transform ( code , id ) { // Transform code — works in ALL bundlers return code . replace ( /__DEV__/g , JSON . stringify ( options . dev ?? false )); }, buildStart () { console . log ( " Build started! " ); } })); // Use in any bundler: export const vite = myPlugin . vite ; export const webpack = myPlugin . webpack ; export const rollup = myPlugin . rollup ; export const esbuild = myPlugin . esbuild ; export const rspack = myPlugin . rspack ; Virtual Modules const virtualPlugin = createUnplugin (() => ({ name : " virtual-module " , resolveId ( id ) { if ( id === " virtual:config " ) return " \ 0virtual:config " ; }, load ( id ) { if ( id === " \ 0v

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
7 views

Related Articles