Experimental
Unified plugin system for build tools.
Current supports:
unplugin
extends the excellent Rollup plugin API as the unified plugin interface and provides a compatible layer base on the build tools using.
Hook | Rollup | Vite | Webpack |
---|---|---|---|
transformInclude |
✅ | ✅ | ✅ |
transform |
✅ | ✅ | ✅ |
enforce |
❌* | ✅ | ✅ |
resolveId |
✅ | ✅ | 🚧 Expiremental |
load |
✅ | ✅ | 🚧 Expiremental |
- *: Rollup does not support
enforce
to control the order of plugins. Users will need to maintain the order manually.
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 incoming
}
})
export const vitePlugin = plugin.vite
export const rollupPlugin = plugin.rollup
export const webpackPlugin = plugin.webpack
export const nuxtModule = plugin.nuxt
// vite.config.ts
import MyUnplugin from './my-unplugin'
export default {
plugins: [
MyUnplugin.vite({ /* options */ })
]
}
// rollup.config.js
import MyUnplugin from './my-unplugin'
export default {
plugins: [
MyUnplugin.rollup({ /* options */ })
]
}
// webpack.config.js
module.exports = {
plugins: [
require('./my-unplugin').webpack({ /* options */ })
]
}
Expose the Nuxt module in a submodule
// ./nuxt.js
import MyUnplugin from './my-unplugin'
export default MyUnplugin.nuxt
// nuxt.config.js
export default {
buildModules: [
['my-unplugin/nuxt', { /* options */ }]
]
}
MIT License © 2021 Nuxt Contrib