Socket
Socket
Sign inDemoInstall

unplugin

Package Overview
Dependencies
241
Maintainers
1
Versions
76
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    unplugin

Unified plugin system for build tools


Version published
Maintainers
1
Install size
1.06 MB
Created

Package description

What is unplugin?

The unplugin npm package is a framework-agnostic plugin system designed to create plugins that work across different build tools like Webpack, Vite, Rollup, and more. It allows developers to write a plugin once and have it compatible with multiple build systems without the need to rewrite it for each one.

What are unplugin's main functionalities?

Virtual Modules

Create virtual modules that can be imported by your code. These modules don't exist on the filesystem but are created on the fly by the plugin.

unplugin.createPlugin({
  resolveId(id) {
    if (id === 'virtual-module') return id;
  },
  load(id) {
    if (id === 'virtual-module') return 'export default "This is virtual!"';
  }
});

Transformations

Apply custom transformations to your code. This can be used to modify the source code of files before they are bundled.

unplugin.createPlugin({
  transform(code, id) {
    if (id.endsWith('.custom')) {
      return code.replace(/find/g, 'replace');
    }
  }
});

Custom Build Steps

Execute custom code at different stages of the build process. This can be used to perform tasks before or after the build.

unplugin.createPlugin({
  buildStart() {
    console.log('Build started!');
  },
  buildEnd() {
    console.log('Build finished!');
  }
});

Other packages similar to unplugin

Changelog

Source

0.8.1 (2022-08-04)

Bug Fixes

  • fix webpack 4 by not using loader options (#149) (b68fbc4)

Readme

Source

unplugin

NPM version

Unified plugin system for build tools.

Currently supports:

Hooks

unplugin extends the excellent Rollup plugin API as the unified plugin interface and provides a compatible layer base on the build tools used with.

Supported
HookRollupViteWebpack 4Webpack 5esbuild
buildStart
buildEnd
transformInclude1
transform3
enforce22
resolveId
load3
watchChange
  1. Webpack's id filter is outside of loader logic; an additional hook is needed for better perf on Webpack. In Rollup and Vite, this hook has been polyfilled to match the behaviors. See for following usage examples.
  2. Rollup and esbuild do not support using enforce to control the order of plugins. Users need to maintain the order manually.
  3. Although esbuild can handle both JavaScript and CSS and many other file formats, you can only return JavaScript in load and transform results.

Hook Context

Supported
HookRollupViteWebpack 4Webpack 5esbuild
this.parse
this.addWatchFile
this.emitFile4
this.getWatchFiles
this.warn
this.errror
  1. Currently, this.emitFile only supports the EmittedAsset variant.

Usage

import { createUnplugin } from 'unplugin'

export const unplugin = createUnplugin((options: UserOptions) => {
  return {
    name: 'my-first-unplugin',
    // webpack's id filter is outside of loader logic,
    // an additional hook is needed for better perf on webpack
    transformInclude (id) {
      return id.endsWith('.vue')
    },
    // just like rollup transform
    transform (code) {
      return code.replace(/<template>/, `<template><div>Injected</div>`)
    },
    // more hooks coming
  }
})

export const vitePlugin = unplugin.vite
export const rollupPlugin = unplugin.rollup
export const webpackPlugin = unplugin.webpack
export const esbuildPlugin = unplugin.esbuild

Plugin Installation

Vite
// vite.config.ts
import MyUnplugin from './my-unplugin'

export default {
  plugins: [
    MyUnplugin.vite({ /* options */ })
  ]
}
Rollup
// rollup.config.js
import MyUnplugin from './my-unplugin'

export default {
  plugins: [
    MyUnplugin.rollup({ /* options */ })
  ]
}
Webpack
// webpack.config.js
module.exports = {
  plugins: [
    require('./my-unplugin').webpack({ /* options */ })
  ]
}
esbuild
// esbuild.config.js
import { build } from 'esbuild'

build({
  plugins: [
    require('./my-unplugin').esbuild({ /* options */ })
  ]
})

Framework-specific Logic

While unplugin provides compatible layers for some hooks, the functionality of it is limited to the common subset of the build's plugins capability. For more advanced framework-specific usages, unplugin provides an escape hatch for that.

export const unplugin = createUnplugin((options: UserOptions, meta) => {

  console.log(meta.framework) // 'vite' | 'rollup' | 'webpack' | 'esbuild'

  return {
    // common unplugin hooks
    name: 'my-first-unplugin',
    transformInclude (id) { /* ... */ },
    transform (code) { /* ... */  },
    
    // framework specific hooks
    vite: {
      // Vite config
      configureServer(server) {
        // configure Vite server
      }
    },
    rollup: {
      // Rollup config
    },
    webpack(compiler) {
      // configure Webpack compiler
    },
    esbuild: {
      // change the filter of onResolve and onLoad
      onResolveFilter?: RegExp
      onLoadFilter?: RegExp
      // or you can completely replace the setup logic
      setup?: EsbuildPlugin['setup']
    }
  }
})

Starter Templates

Examples

License

MIT License © 2021 Nuxt Contrib

FAQs

Last updated on 04 Aug 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc