Skip to content

Upgrade guide

Features get added and removed, and breaking changes are introduced! This documents how to migrate.


Previously the defineUtility signature would emit a type that required naming internal Astro types in some cases, which caused error TS2742 about non-portable types. This type was improved to emit small and portable types through a new public HookUtility type exported from astro-integration-kit.

The generic arguments of defineUtility haven’t changed, no action is necessary for you code to benefit from this change.

As an example, an utility defined with the following code:

export const utility = defineUtility("astro:config:setup")(
(params: HookParameters<"astro:config:setup">, options: { name: string }) => {
// do something
}
);

Was previously emitted as:

import * as astro from "astro";
import { AstroIntegrationLogger } from "astro";
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? DeepPartial<U>[]
: T[P] extends object | undefined
? DeepPartial<T[P]>
: T[P];
};
export const utility: (
params: {
config: astro.AstroConfig;
command: "dev" | "build" | "preview";
isRestart: boolean;
updateConfig: (
newConfig: DeepPartial<astro.AstroConfig>
) => astro.AstroConfig;
addRenderer: (renderer: astro.AstroRenderer) => void;
addWatchFile: (path: URL | string) => void;
injectScript: (stage: astro.InjectedScriptStage, content: string) => void;
injectRoute: (injectRoute: astro.InjectedRoute) => void;
addClientDirective: (directive: astro.ClientDirectiveConfig) => void;
addDevOverlayPlugin: (entrypoint: string) => void;
addDevToolbarApp: (entrypoint: astro.DevToolbarAppEntry | string) => void;
addMiddleware: (mid: astro.AstroIntegrationMiddleware) => void;
logger: AstroIntegrationLogger;
},
options: {
name: string;
}
) => void;

Now it is emitted as:

import * as astro_integration_kit from "astro-integration-kit";
export const utility: astro_integration_kit.HookUtility<
"astro:config:setup",
[
options: {
name: string;
}
],
void
>;

Improved emitted types for defineIntegration

Section titled “Improved emitted types for defineIntegration”

Previously the defineUtility signature would emit a type that required naming internal Astro types in some cases, which caused error TS2742 about non-portable types. This type was improved to not emit the portion of the type that would be discarded by its own construction.

To achieve this with newer versions of TS (>=5.5), the generic parameters had to be updated. If you were passing type parameters explicitly instead of relying on type inference you should either remove the parameters or adapt your types to the new parameter requirements.

As an example, an integration defined with the following code:

export const integration defineIntegration({
name: 'some-integration',
setup: () => ({
hooks: {
'astro:config:setup': (params) => {
// do something
},
},
something: (it: string): string => it,
}),
});

Was previously emitted as:

import * as astro from "astro";
import { AstroIntegrationLogger } from "astro";
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? DeepPartial<U>[]
: T[P] extends object | undefined
? DeepPartial<T[P]>
: T[P];
};
export const integration: () => astro.AstroIntegration &
Prettify<
Omit<
ReturnType<
() => {
hooks: {
"astro:config:setup": (params: {
config: astro.AstroConfig;
command: "dev" | "build" | "preview";
isRestart: boolean;
updateConfig: (
newConfig: DeepPartial<astro.AstroConfig>
) => astro.AstroConfig;
addRenderer: (renderer: astro.AstroRenderer) => void;
addWatchFile: (path: URL | string) => void;
injectScript: (
stage: astro.InjectedScriptStage,
content: string
) => void;
injectRoute: (injectRoute: astro.InjectedRoute) => void;
addClientDirective: (
directive: astro.ClientDirectiveConfig
) => void;
addDevOverlayPlugin: (entrypoint: string) => void;
addDevToolbarApp: (
entrypoint: astro.DevToolbarAppEntry | string
) => void;
addMiddleware: (mid: astro.AstroIntegrationMiddleware) => void;
logger: AstroIntegrationLogger;
}) => void;
};
something: (it: string) => string;
}
>,
keyof astro.AstroIntegration
>
>;

Now it is emitted as:

import * as astro from "astro";
export const integration: () => astro.AstroIntegration & {
something: (it: string) => string;
};

With the new minimal version of Astro, there is no longer any need for the previous workaround to support non-native hooks, like those from @astrojs/db. The following changes are needed to replace the previous Astro Integration Kit types with the native types:

  • Hook type extensions
    namespace AstroIntegrationKit {
    interface ExtraHooks {
    namespace Astro {
    interface IntegrationHooks {
    'myLib:myHook': (params: { foo: string }) => Promise<void>;
    }
    }
  • Hook parameters
    import type { HookParameters } from 'astro-integration-kit';
    import type { HookParameters } from 'astro';
  • @astrojs/db hooks:
    import "astro-integration-kit/types/db";
    /// <reference types="@astrojs/db" />
    If you are using @astrojs/db values and types in your project, you can just remove import. Importing anything from the library also loads the types.
    import "astro-integration-kit/types/db";
    import {...} from "@astrojs/db";

The new Plugin signature added back in 0.9.0 returned the need for emitted declarations to include the full type for hook parameters from Astro (which was removed on 0.7.0). This was added along with support for a plugin to define new utilities for multiple hooks at once.

Now, the Plugin generics have been simplified again to allow for such support without having to replicate Astro’s hook parameters types. Just like in 0.7.0, this should be non-breaking for any plugin relying on type inference but plugins with explicitly declared signatures need to update the following:

type SomePlugin = Plugin<
"utilityName",
"astro:config:setup",
{
"astro:config:setup": (HookParameters<"astro:config:setup">) => {
"astro:config:setup": {
utilityName: (params: UtilityParams) => UtilityOutput
}
}
>;

The addDevToolbarFrameworkApp has been removed in favour of the Astro defineToolbarApp utility added in Astro v4.7.0.

To migrate your Dev Toolbar App to the new Astro utility, please see their documentation here.


defineIntegration setup return type updated

Section titled “defineIntegration setup return type updated”

Previously the return of the setup function passed to defineIntegration was the Astro hooks defined by the integration, and would be set as the hooks property in the final integration object.

Now, the expected return of setup is the properties of the integration object itself:

my-integration.ts
import { defineIntegration } from "astro-integration-kit";
export default defineIntegration({
name: "my-integration",
setup({ name }) {
return {
hooks: {
"astro:config:setup": () => {
// ...
},
},
};
},
});

watchIntegration renamed to watchDirectory

Section titled “watchIntegration renamed to watchDirectory”

Since watchIntegration is not recommended for dev HMR anymore, it has been renamed according to what it really does, watching a directory:

import { watchIntegration } from "astro-integration-kit";
watchIntegration(params, resolve());
import { watchDirectory } from "astro-integration-kit";
watchDirectory(params, resolve());

New withPlugins utility, plugins removed from defineIntegration

Section titled “New withPlugins utility, plugins removed from defineIntegration”

Plugins are no longer passed to defineIntegration. Instead, there’s a new withPlugins utility.

my-integration/index.ts
import { defineIntegration, withPlugins } from "astro-integration-kit";
import { hasVitePluginPlugin } from "astro-integration-kit/plugins";
export default defineIntegration({
name: "my-integration",
plugins: [hasVitePluginPlugin],
setup({ name }) {
return {
"astro:config:setup": ({ hasVitePlugin }) => {},
};
return withPlugins({
name,
plugins: [hasVitePluginPlugin],
hooks: {
"astro:config:setup": ({ hasVitePlugin }) => {},
},
});
},
});

You should not use the Plugin type manually but if you do, you’ll need to update it:

type SomePlugin = Plugin<
"utilityName",
"astro:config:setup",
(params: UtilityParams) => UtilityOutput,
{
"astro:config:setup": (HookParameters<"astro:config:setup">) => {
utilityName: (params: UtilityParams) => UtilityOutput
}
}
>;

The definePlugin signature has been updated as well:

import type { AstroConfig } from "astro";
import type { Plugin, PluginOption } from "vite";
import { definePlugin } from "astro-integration-kit";
import { hasVitePlugin } from "../utilities/has-vite-plugin.js";
export const hasVitePluginPlugin = definePlugin({
name: "hasVitePlugin",
hook: "astro:config:setup",
implementation: (params) => (plugin: string | PluginOption) =>
hasVitePlugin(params, { plugin }),
setup() {
return {
"astro:config:setup": (params) => ({
hasVitePlugin: (plugin: string | PluginOption) =>
hasVitePlugin(params, { plugin }),
}),
};
},
});

The following plugins have been removed in favor of their standalone utility version:

  • addDevToolbarFrameworkAppPlugin
  • addDtsPlugin
  • addIntegrationPlugin
  • addVirtualImportsPlugin
  • addVitePluginPlugin
  • hasIntegrationPlugin
  • injectDevRoutePlugin
  • watchIntegrationPlugin

Only hasVitePluginPlugin remains.

corePlugins is no longer exported from astro-integration-kit. Import the plugin you want directly:

import { defineIntegration } from "astro-integration-kit";
import {
corePlugins,
hasVitePluginPlugin,
} from "astro-integration-kit/plugins";
export default defineIntegration({
// ...
plugins: [...corePlugins],
plugins: [hasVitePluginPlugin],
});
const { resolve } = createResolver(import.meta.url);
addDevToolbarFrameworkApp({
framework: "vue",
name: "Test Vue Plugin",
id: "my-vue-plugin",
icon: `<svg version="1.1" viewBox="0 0 261.76 226.69" xmlns="http://www.w3.org/2000/svg"><g transform="matrix(1.3333 0 0 -1.3333 -76.311 313.34)"><g transform="translate(178.06 235.01)"><path d="m0 0-22.669-39.264-22.669 39.264h-75.491l98.16-170.02 98.16 170.02z" fill="#41b883"/></g><g transform="translate(178.06 235.01)"><path d="m0 0-22.669-39.264-22.669 39.264h-36.227l58.896-102.01 58.896 102.01z" fill="#34495e"/></g></g></svg>`,
src: resolve("./my-plugin.vue"),
style: `
h1 {
font-family: Inter;
}
`,
config,
updateConfig,
addDevToolbarApp,
injectScript,
});
addDevToolbarFrameworkApp(params, {
framework: "vue",
name: "Test Vue Plugin",
id: "my-vue-plugin",
icon: `<svg version="1.1" viewBox="0 0 261.76 226.69" xmlns="http://www.w3.org/2000/svg"><g transform="matrix(1.3333 0 0 -1.3333 -76.311 313.34)"><g transform="translate(178.06 235.01)"><path d="m0 0-22.669-39.264-22.669 39.264h-75.491l98.16-170.02 98.16 170.02z" fill="#41b883"/></g><g transform="translate(178.06 235.01)"><path d="m0 0-22.669-39.264-22.669 39.264h-36.227l58.896-102.01 58.896 102.01z" fill="#34495e"/></g></g></svg>`,
src: resolve("./my-plugin.vue"),
style: `
h1 {
font-family: Inter;
}
`,
});
addDts({
name: "my-integration",
content: `declare module "virtual:my-integration" {}`,
root: config.root,
srcDir: config.srcDir,
logger,
});
addDts(params, {
name: "my-integration",
content: `declare module "virtual:my-integration" {}`,
});
addIntegration({
integration: Vue(),
updateConfig,
config,
logger,
});
addIntegration(params, {
integration: Vue(),
});
addVirtualImports({
name: "my-integration",
imports: {
"virtual:my-integration/config": `export default ${JSON.stringify({
foo: "bar",
})}`,
},
updateConfig,
config,
});
addVirtualImports(params, {
name: "my-integration",
imports: {
"virtual:my-integration/config": `export default ${JSON.stringify({
foo: "bar",
})}`,
},
});
addVitePlugin({
plugin: VitePWA({ registerType: "autoUpdate" }),
config,
logger,
updateConfig,
});
addVitePlugin(params, {
plugin: VitePWA({ registerType: "autoUpdate" }),
});
hasVitePlugin({
plugin: "vite-plugin-my-integration",
config,
});
hasVitePlugin(params, {
plugin: "vite-plugin-my-integration",
});
hasIntegration({
name: "@astrojs/tailwind",
position: "before",
relativeTo: "my-integration",
config,
});
hasIntegration(params, {
name: "@astrojs/tailwind",
position: "before",
relativeTo: "my-integration",
});
const { resolve } = createResolver(import.meta.url);
injectDevRoute({
command,
injectRoute,
injectedRoute: {
pattern: "/foo",
entrypoint: resolve("./pages/foo.astro"),
},
});
injectDevRoute(params, {
pattern: "/foo",
entrypoint: resolve("./pages/foo.astro"),
});
const { resolve } = createResolver(import.meta.url);
watchIntegration({
addWatchFile,
command,
dir: resolve(),
updateConfig,
});
watchIntegration(params, resolve());

They’re not optional by default, you need to manually add .optional() at the end of your zod schema. If it’s optional, users can still pass nothing or undefined.

defineIntegration({
// ...
optionsSchema: z.object({ foo: z.string() }),
optionsSchema: z.object({ foo: z.string() }).optional(),
});

Plugin generics have been simplified, allowing simpler plugin builds. This should be non-breaking for plugin relying on type inference, plugins with explicitly declared signature should update the following:

type SomePlugin = Plugin<
"utilityName",
"astro:config:setup",
(p: HookParams) => (params: UtilityParams) => UtilityOutput
(params: UtilityParams) => UtilityOutput
>;
export const somePlugin: SomePlugin = definePlugin();

The addVitePlugin utility now requires a config and logger parameter to log warnings for duplicate plugins

"astro:config:setup": ({ config, updateConfig }) => {
addVitePlugin({
plugin,
config,
logger
updateConfig
})
}

Or you can turn off warnings for duplicate plugins using warnDuplicate: false

addVitePlugin({
warnDuplicate: false,
plugin,
updateConfig,
});

The addVirtualImports utility now requires a config parameter

"astro:config:setup": ({ config, updateConfig }) => {
addVirtualImports({
updateConfig,
config
name: 'my-integration',
imports: {
'virtual:my-integration/config': `export default ${JSON.stringify({ foo: "bar" })}`,
},
})
}

The addDevToolbarFrameworkApp utility now requires a config parameter

"astro:config:setup": ({ config, updateConfig }) => {
addDevToolbarFrameworkApp({
config,
framework: "vue",
name: "Test Vue Plugin",
id: "my-vue-plugin",
icon: `<svg>...</svg>`,
src: resolve("./my-plugin.vue"),
style: `
h1 {
font-family: Inter;
}
`,
})
}

addVirtualImport was removed in 0.5.0. Here is how to migrate:

my-integration/index.ts
import { defineIntegration } from "astro-integration-kit";
import { addVirtualImportPlugin } from "astro-integration-kit/plugins";
import { addVirtualImportsPlugin } from "astro-integration-kit/plugins";
export default defineIntegration({
name: "my-integration",
plugins: [addVirtualImportPlugin],
plugins: [addVirtualImportsPlugin],
setup() {
return {
"astro:config:setup": ({ addVirtualImport }) => {
"astro:config:setup": ({ addVirtualImports }) => {
addVirtualImport({
name: 'virtual:my-integration/config',
content: `export default ${JSON.stringify({ foo: "bar" })}`,
})
addVirtualImport({
name: 'virtual:my-integration/context',
content: `export default ${JSON.stringify({ entrypoint: import.meta.url })}`,
})
addVirtualImports({
'virtual:my-integration/config': `export default ${JSON.stringify({ foo: "bar" })}`,
'virtual:my-integration/context': `export default ${JSON.stringify({ entrypoint: import.meta.url })}`
})
}
}
}
})

defineOptions has been removed in 0.2.0. Here is how to migrate:

my-integration/index.ts
import { defineIntegration, defineOptions } from "astro-integration-kit";
import { defineIntegration } from "astro-integration-kit";
import { z } from "astro/zod";
type Options = {
/**
* A comment
*
* @default `"bar"`
*/
foo?: string | undefined;
};
export default defineIntegration({
// ...
options: defineOptions<Options>({ foo: "bar" }),
optionsSchema: z.object({
/**
* A comment
*
* @default `"bar"`
*/
foo: z.string().optional().default("bar"),
}),
});