---
title: Plugin API
sidebar_position: 6
---

# Plugin API

The `setup` function of the plugin will receive an `api` imported parameter, and you can call some methods provided on the api to obtain information such as configuration and application context.

```ts
import type { CliPlugin } from '@modern-js/core';

export default (): CliPlugin => ({
  name: 'my-plugin',

  setup(api) {
    // get user config
    const config = api.useConfigContext();
    // get plugin context
    const appContext = api.useAppContext();
    // get resolved config
    const resolvedConfig = api.useResolvedConfigContext();
  },
});
```

## API

### useConfigContext

Used to retrieve the original configuration of the application.

```ts
const useConfigContext: () => UserConfig;

interface UserConfig {
  source?: SourceConfig;
  output?: OutputConfig;
  server?: ServerConfig;
  deploy?: DeployConfig;
  // ...other fields
}
```

Please refer to [Configuration](/configure/app/usage) for the specific meanings of configuration fields.

:::tip
This method returns a read-only configuration and cannot be modified. If you need to modify the configuration, please use [config hook](/guides/topic-detail/framework-plugin/hook-list.html#config).
:::

### useResolvedConfigContext

Used to retrieve the final configuration after parsing.

```ts
const useResolvedConfigContext: () => NormalizedConfig;

interface NormalizedConfig {
  source: NormalizedSourceConfig;
  output: NormalizedOutputConfig;
  server: NormalizedServerConfig;
  deploy: NormalizedDeployConfig;
  _raw: UserConfig; // the original user config
  // ...other fields
}
```

Please refer to [Configuration](/configure/app/usage) for the specific meanings of configuration fields.

:::tip
This method returns a read-only configuration and cannot be modified. If you need to modify the configuration, please use [config hook](/guides/topic-detail/framework-plugin/hook-list.html#config).
:::

### useAppContext

Used to retrieve the runtime context of the application.

```ts
const useAppContext: () => IAppContext;

interface IAppContext {
  appDirectory: string;
  configFile: string | false;
  ip?: string;
  port?: number;
  distDirectory: string;
  packageName: string;
  srcDirectory: string;
  sharedDirectory: string;
  nodeModulesDirectory: string;
  internalDirectory: string;
  plugins: {
    cli?: any;
    server?: any;
  }[];
  entrypoints: Entrypoint[];
  serverRoutes: ServerRoute[];
  htmlTemplates: HtmlTemplates;
}
```

### useHookRunners

Used to retrieve the executor of Hooks and trigger the execution of specific Hooks.

```ts
import type { CliPlugin } from '@modern-js/core';

export default (): CliPlugin => ({
  name: 'my-plugin',

  async setup(api) {
    const hookRunners = api.useHookRunners();
    // invoke afterBuild Hook
    await hookRunners.afterBuild();
  },
});
```
