Skip to content

createResolver

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

integration/index.ts
import type { AstroIntegration } from "astro";
import { createResolver } from "astro-integration-kit";
export default function myIntegration(): AstroIntegration {
const { resolve } = createResolver(import.meta.url);
return {
name: "my-integration",
hooks: {
"astro:config:setup": ({ addDevToolbarApp }) => {
addDevToolbarApp(resolve("./plugin.ts"));
}
}
}
}

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
{
"name": "package-name",
"exports": {
"pages/my-route.astro": "./src/pages/my-route.astro",
"plugin.ts": "./src/plugin.ts"
}
}
integration/index.ts
import type { AstroIntegration } from "astro";
import { createResolver } from "astro-integration-kit";
export default function myIntegration(): AstroIntegration {
const { resolve } = createResolver(import.meta.url);
return {
name: "my-integration",
hooks: {
"astro:config:setup": ({ injectRoute, addDevToolbarApp }) => {
injectRoute({
pattern: "/my-route",
entrypoint: "package-name/pages/my-route.astro"
entrypoint: resolve("./pages/my-route.astro")
})
addDevToolbarApp(
"package-name/plugin.ts"
resolve("./plugin.ts")
)
}
}
}
}

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.