This is used to aggregate all the plugins and expose them to the bundler.
Note
We use unplugin to support many different bundlers.
These are the plugins that are used internally by the factory. Most of the time they will interact via the global context.
Send some analytics data to Datadog internally.
It gives you acces to the
context.sendLog()
function.
This will populate
context.build
with a bunch of data coming from the build.
A very basic report on the currently used bundler.
It is useful to unify some configurations.
Adds repository data to the global context from the
buildStart
hook.
This is used to inject some code to the produced bundle.
Particularly useful :
- to share some global context.
- to automatically inject some SDK.
- to initialise some global dependencies.
- ...
If you need to log anything into the console you'll have to use the global Logger.
You can get a logger by calling context.getLogger(PLUGIN_NAME);
.
// ./packages/plugins/my-plugin/index.ts
[...]
export const getMyPlugins = (context: GlobalContext) => {
const log = context.getLogger(PLUGIN_NAME);
log.debug('Welcome to my plugin');
[...]
};
Then you can either use one of the level logger methods:
logger.warn('This is also a warning');
logger.error('This is an error');
logger.info('This is an info');
logger.debug('This is a debug message');
You can also create a "sub-logger" when you want to individually identify logs from a specific part of your plugin.
Simply use log.getLogger('my-plugin')
for this:
export const getMyPlugins = (context: GlobalContext) =>
const log = context.getLogger(PLUGIN_NAME);
log.debug('Welcome to the root of my plugin');
return [
{
name: 'my-plugin',
setup: (context: PluginContext) => {
const subLog = log.getLogger('my-plugin');
subLog.info('This is a debug message from one of my plugins.');
},
},
];
};
A global, shared context within the build plugins ecosystem.
It is passed to your plugin's initialization, and is mutated during the build process.
type GlobalContext = { // Mirror of the user's config. auth?: { apiKey?: string; }; // More details on the currently running bundler. bundler: BundlerReport // Added in `writeBundle`. build: BuildReport cwd: string; getLogger: (name: string) => Logger // Added in `buildStart`. git?: Git inject: Injection start: number; version: string; }
Note
Some parts of the context are only available after certain hooks:
context.bundler.rawConfig
is added in thebuildStart
hook.context.build.*
is populated in thewriteBundle
hook.context.git.*
is populated in thebuildStart
hook.
Your function will need to return an array of Unplugin Plugins definitions.