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
"astro:config:setup": (params) => {
11
const { logger } = params
12
13
if (hasIntegration(params, { name: "@astrojs/tailwind" })) {
14
logger.info("Tailwind is installed!");
15
}
16
}
17
}
18
}
19
})

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
"astro:config:setup": (params) => {
11
const { logger } = params
12
13
if (hasIntegration(params, {
14
name: "@astrojs/tailwind",
15
position: "before",
16
relativeTo: name
17
})) {
18
logger.info("Tailwind is installed before my-integration");
19
}
20
21
if (hasIntegration(params, {
22
name: "astro-env",
23
position: "after",
24
relativeTo: name
25
})) {
26
logger.info("AstroEnv is installed after my-integration");
27
}
28
29
if (hasIntegration(params, {
30
name: "astro-expressive-code",
31
position: "before",
32
relativeTo: "@astrojs/mdx"
33
})) {
34
logger.info("Expressive Code is installed before MDX");
35
}
36
37
if (hasIntegration(params, {
38
name: "astro-expressive-code",
39
position: "after",
40
relativeTo: "@astrojs/tailwind"
41
})) {
42
logger.info("Expressive Code is installed after Tailwind");
43
}
44
}
45
}
46
}
47
})