|
2 years ago | |
---|---|---|
.. | ||
dist | 2 years ago | |
LICENSE | 2 years ago | |
README.md | 2 years ago | |
package.json | 2 years ago |
Unified plugin system for build tools.
Currently supports:
unplugin
extends the excellent Rollup plugin API as the unified plugin interface and provides a compatible layer base on the build tools used with.
Hook | Rollup | Vite | Webpack 4 | Webpack 5 | esbuild |
---|---|---|---|---|---|
enforce |
❌ 1 | ✅ | ✅ | ✅ | ❌ 1 |
buildStart |
✅ | ✅ | ✅ | ✅ | ✅ |
resolveId |
✅ | ✅ | ✅ | ✅ | ✅ |
loadInclude 2 |
✅ | ✅ | ✅ | ✅ | ✅ |
load |
✅ | ✅ | ✅ | ✅ | ✅ 3 |
transformInclude 2 |
✅ | ✅ | ✅ | ✅ | ✅ |
transform |
✅ | ✅ | ✅ | ✅ | ✅ 3 |
watchChange |
✅ | ✅ | ✅ | ✅ | ✅ |
buildEnd |
✅ | ✅ | ✅ | ✅ | ✅ |
enforce
to control the order of plugins. Users need to maintain the order manually.load
and transform
results.Hook | Rollup | Vite | Webpack 4 | Webpack 5 | esbuild |
---|---|---|---|---|---|
this.parse |
✅ | ✅ | ✅ | ✅ | ✅ |
this.addWatchFile |
✅ | ✅ | ✅ | ✅ | ✅ |
this.emitFile 4 |
✅ | ✅ | ✅ | ✅ | ✅ |
this.getWatchFiles |
✅ | ✅ | ✅ | ✅ | ✅ |
this.warn |
✅ | ✅ | ✅ | ✅ | ✅ |
this.error |
✅ | ✅ | ✅ | ✅ | ✅ |
this.emitFile
only supports the EmittedAsset
variant.import { createUnplugin } from 'unplugin'
export const unplugin = createUnplugin((options: UserOptions) => {
return {
name: 'unplugin-prefixed-name',
// 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
// vite.config.ts
import UnpluginFeature from './unplugin-feature'
export default {
plugins: [
UnpluginFeature.vite({ /* options */ })
]
}
// rollup.config.js
import UnpluginFeature from './unplugin-feature'
export default {
plugins: [
UnpluginFeature.rollup({ /* options */ })
]
}
// webpack.config.js
module.exports = {
plugins: [
require('./unplugin-feature').webpack({ /* options */ })
]
}
// esbuild.config.js
import { build } from 'esbuild'
build({
plugins: [
require('./unplugin-feature').esbuild({ /* options */ })
]
})
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: 'unplugin-prefixed-name',
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']
}
}
})
unplugin-
prefix.unplugin
keyword in package.json
.To provide better DX, packages could export 2 kinds of entry points:
Default export: the returned value of createUnplugin
function
import UnpluginFeature from 'unplugin-feature'
Subpath exports: properties of the returned value of createUnplugin
function for each framework users
import VitePlugin from 'unplugin-feature/vite'
Refer to unplugin-starter for more details about this setup.
MIT License © 2021 Nuxt Contrib