How to add types for virtual modules? #11368
Answered
by
chenxch
Lisianthus-A
asked this question in
Q&A
-
I'm writing a library that generates a virtual module at runtime, which users can import it in JavaScript or TypeScript, just like: import { msg } from "virtual:my-plugin";
console.log(msg); But I get Here is the plugin code: import type { PluginOption } from "vite";
function MyPlugin(): PluginOption {
const virtualModuleId = "virtual:my-plugin";
const resolvedVirtualModuleId = "\0" + virtualModuleId;
return {
name: "my-plugin",
resolveId(id) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId;
}
},
load(id) {
if (id === resolvedVirtualModuleId) {
return `export const msg = "from virtual module"`;
}
},
};
}
export default MyPlugin; how can I include type definitions for this virtual module to fix it? |
Beta Was this translation helpful? Give feedback.
Answered by
chenxch
Dec 14, 2022
Replies: 2 comments 6 replies
-
You need to add the type declaration. // module.d.ts
declare module 'virtual:*' {
// eslint-disable-next-line
const component: any;
export default component;
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Lisianthus-A
-
Can a plugin generate the declaration files for you? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to add the type declaration.