Keep a plugin encapsulated using the encapsulate option
mainBy default, fastify-plugin breaks encapsulation so the plugin is accessible in the parent context. If you want to set metadata (like name or dependencies) but keep the plugin's decorators and state private to its own scope, set encapsulate: true in the options object.
const fp = require('fastify-plugin')
function plugin (fastify, opts, done) {
// the decorator is not accessible outside this plugin
fastify.decorate('util', function() {})
done()
}
module.exports = fp(plugin, {
name: 'my-encapsulated-plugin',
fastify: '5.x',
decorators: {
fastify: ['plugin1', 'plugin2'],
reply: ['compress']
},
dependencies: ['plugin1-name', 'plugin2-name'],
encapsulate: true
})