Replies: 4 comments 1 reply
-
@dburian I am stumbling over the same problem, did you find a solution in the meantime? |
Beta Was this translation helpful? Give feedback.
-
Same here, I'm struggling to get this working. I think I'm missing the appropriate typescript knowledge, but I'm really lost. |
Beta Was this translation helpful? Give feedback.
-
@ramiel In the meantime, I managed to find a solution ... or workaround: The Vite-Plugin
// vite-plugin-example/src/virtual.d.ts
declare module "virtual:example" {
export interface ExampleData { ... }
const data: ExampleData
export default data
} // vite-plugin-example/src/main.ts
import type { ExampleData } from "virtual:example"; // vite-plugin-example/package.json
{
"name": "vite-plugin-example",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./virtual": {
"types": "./dist/virtual.d.ts"
}
},
"scripts": {
"build": "npx --yes rimraf dist && tsc && npx --yes cpy-cli --flat src/virtual.d.ts dist/"
}
} Consumer Project// example-project/tsconfig.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"types": [
"vite/client",
"vite-plugin-example/virtual"
]
}
} // example-project/src/main.ts
import type { ExampleData } from "virtual:example"
import exampleData from "virtual:example" |
Beta Was this translation helpful? Give feedback.
-
Hey @danielrentz , I found a different solution just after posting my message. I'll share it soon but in short is similar to yours except there's no need to change tsconfig since you can update vite.d.ts that is meant for that |
Beta Was this translation helpful? Give feedback.
-
Hi everybody,
I'm building a plugin (say with a name
plugin-name
), which intercepts imports of a virtual module and creates it dynamically (withresolveId
andload
hooks). The problem I am having is how to declare types for the virtual module or rather how to include them in my package.So let's say I have a declaration of a virtual module:
The problem is, how to package this. I compile my code with Rollup with typescript plugin, but it gives me errors saying it cannot find the module
plugin-name/types
, where the definition ofGraph
lies in. This is intuitive as this module does not yet exist (the bundling creates this module). Similar exception occurs if I rewrite that import into a relative one - "Import or export declaration in an ambient module declaration cannot reference module through relative module name.".Obviously one solution would be to let the user of the plugin define such declaration of manually include it in my
dist/
folder. But I consider these a hacks since this declaration is part of my package and therefore should be bundled as such.Do you have any advice on how to resolve this?
Thanks :).
Beta Was this translation helpful? Give feedback.
All reactions