Skip to content

Latest commit

 

History

History

factory

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Factory

This is used to aggregate all the plugins and expose them to the bundler.

Note

We use unplugin to support many different bundlers.

Internal Plugins

These are the plugins that are used internally by the factory. Most of the time they will interact via the global context.

Analytics

Send some analytics data to Datadog internally.

It gives you acces to the context.sendLog() function.

Build Report

This will populate context.build with a bunch of data coming from the build.

Bundler Report

A very basic report on the currently used bundler.
It is useful to unify some configurations.

Git

Adds repository data to the global context from the buildStart hook.

Injection

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.
  • ...

Logger

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.');
            },
        },
    ];
};

Global Context

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 the buildStart hook.
  • context.build.* is populated in the writeBundle hook.
  • context.git.* is populated in the buildStart hook.

Your function will need to return an array of Unplugin Plugins definitions.