Skip to content

definePlugin

Allows defining a type-safe plugin that can be used in defineIntegration.

package/plugins/add-vite-plugin.ts
1
import { definePlugin } from "../core/define-plugin.js";
2
import { addVitePlugin } from "../utilities/add-vite-plugin.js";
3
import type { Plugin as VitePlugin } from "vite"
4
5
export const addVitePluginPlugin = definePlugin({
6
name: "addVitePlugin",
7
hook: "astro:config:setup",
8
implementation:
9
({ updateConfig }) =>
10
(plugin: VitePlugin) =>
11
addVitePlugin({ plugin, updateConfig }),
12
});

You can then use it in defineIntegration:

my-integration/index.ts
1
import { defineIntegration } from "astro-integration-kit";
2
import { addVitePluginPlugin } from "../package/plugins/add-vite-plugin.js";
3
4
export default defineIntegration({
5
name: "my-integration",
6
plugins: [addVitePluginPlugin],
7
setup() {
8
return {
9
"astro:config:setup": ({ addVitePlugin }) => {}
10
}
11
}
12
})

Limitations

  1. A plugin only defines a utility that will be injected for a given hook
  2. A plugin can only use the built-in utilities (ie. the ones from the official Integration API)
  3. Plugins support overrides. That means that if 2 plugins declare the same name and hook, the latest will be kept.

Practical examples

Astro Integration Kit uses definePlugin for its core plugins under the hood, have a look at our source for practical examples!