addVirtualImports
addVirtualImports
allows you to create virtual modules and share data from your integration. For example:
1import {2 defineIntegration,3 addVirtualImports4} from "astro-integration-kit";5
6export default defineIntegration({7 // ...8 setup() {9 return {10 "astro:config:setup": (params) => {11 addVirtualImports(params, {12 imports: {13 'virtual:my-integration/config': `export default ${JSON.stringify({ foo: "bar" })}`,14 }15 })16
17 addVirtualImports(params, {18 imports: [19 {20 id: "virtual:my-integration/advanced",21 content: "export const foo = 'server'",22 context: "server"23 },24 {25 id: "virtual:my-integration/advanced",26 content: "export const foo = 'client'",27 context: "client"28 },29 ]30 })31 }32 }33 }34})
You can now access the config somewhere else, for example inside a page injected by your integration:
1---2import config from "virtual:my-integration/config",3
4console.log(config.foo) // "bar"5---
Typing your virtual module
In order to get the types for your virtual modules while developing your integration, you’ll need to manually declare a virtual module.
First create a virtual.d.ts
file in your integration root. Then you can define your virtual modules as global TypeScript modules:
1declare module "virtual:my-integration/config" {2 export default { foo: string };3}