Skip to content

createResolver

createResolver allows you to resolve paths relative to the integration folder easily.

integration/index.ts
1
import type { AstroIntegration } from "astro";
2
import { createResolver } from "astro-integration-kit";
3
4
export default function myIntegration(): AstroIntegration {
5
const { resolve } = createResolver(import.meta.url);
6
7
return {
8
name: "my-integration",
9
hooks: {
10
"astro:config:setup": ({ addDevToolbarApp }) => {
11
addDevToolbarApp(resolve("./plugin.ts"));
12
}
13
}
14
}
15
}

Why should you use it?

We think it provides a better DX. Instead of having to do some back and forth between your code and your package.json exports fields, you can just use some intuitive relative paths!

package.json
1
{
2
"name": "package-name",
3
"exports": {
4
"pages/my-route.astro": "./src/pages/my-route.astro",
5
"plugin.ts": "./src/plugin.ts"
6
}
7
}
integration/index.ts
1
import type { AstroIntegration } from "astro";
2
import { createResolver } from "astro-integration-kit";
3
4
export default function myIntegration(): AstroIntegration {
5
const { resolve } = createResolver(import.meta.url);
6
return {
7
name: "my-integration",
8
hooks: {
9
"astro:config:setup": ({ injectRoute, addDevToolbarApp }) => {
10
injectRoute({
11
pattern: "/my-route",
12
entrypoint: "package-name/pages/my-route.astro"
13
entrypoint: resolve("./pages/my-route.astro")
14
})
15
addDevToolbarApp(
16
"package-name/plugin.ts"
17
resolve("./plugin.ts")
18
)
19
}
20
}
21
}
22
}

Usage

Always pass import.meta.url to createResolver! That’s the equivalent of the old __filename. This way, resolve will always create a valid path relatively, no matter its location in node_modules, package managers madness etc

We recommend calling it in setup to easily access resolve in any hook.