Skip to content

hasIntegration

hasIntegration checks whether an integration has already been added to the Astro config. For example:

my-integration/index.ts
1
import {
2
defineIntegration,
3
hasIntegration
4
} from "astro-integration-kit";
5
6
export default defineIntegration({
7
// ...
8
setup() {
9
return {
10
hooks: {
11
"astro:config:setup": (params) => {
12
const { logger } = params
13
14
if (hasIntegration(params, { name: "@astrojs/tailwind" })) {
15
logger.info("Tailwind is installed!");
16
}
17
}
18
}
19
}
20
}
21
})

Relative position check

Sometimes two integrations must be installed in an specific order to work together correctly.

For that use-case, this utility accepts optional position and relativeTo parameters to check for the presence of one integration in relation to another.

Checking for the presence of an integration in relation to an uninstalled integration will result in an error.

my-integration/index.ts
1
import {
2
defineIntegration,
3
hasIntegration
4
} from "astro-integration-kit";
5
6
export default defineIntegration({
7
// ...
8
setup({ name }) {
9
return {
10
hooks: {
11
"astro:config:setup": (params) => {
12
const { logger } = params
13
14
if (hasIntegration(params, {
15
name: "@astrojs/tailwind",
16
position: "before",
17
relativeTo: name
18
})) {
19
logger.info("Tailwind is installed before my-integration");
20
}
21
22
if (hasIntegration(params, {
23
name: "astro-env",
24
position: "after",
25
relativeTo: name
26
})) {
27
logger.info("AstroEnv is installed after my-integration");
28
}
29
30
if (hasIntegration(params, {
31
name: "astro-expressive-code",
32
position: "before",
33
relativeTo: "@astrojs/mdx"
34
})) {
35
logger.info("Expressive Code is installed before MDX");
36
}
37
38
if (hasIntegration(params, {
39
name: "astro-expressive-code",
40
position: "after",
41
relativeTo: "@astrojs/tailwind"
42
})) {
43
logger.info("Expressive Code is installed after Tailwind");
44
}
45
}
46
}
47
}
48
}
49
})