brattonross / vite-plugin-voie

File system based routing plugin for Vite
https://www.npmjs.com/package/vite-plugin-voie
MIT License
230 stars 9 forks source link

TypeScript support #2

Closed antfu closed 4 years ago

antfu commented 4 years ago

Thanks for this lib, it's great and helped me a lot.

But the one small problem is that the /@voie/pages does not have types.

image

I have made a vite plugin inspired by your implementation. To fix the same problem, I have published a package with the same name and install as a dependency to make the types inferring work.

image

Hope it would be helpful, and looking forward to seeing this project grows.

brattonross commented 4 years ago

Hey @antfu, I'm glad you're finding it useful. It's especially great to hear it from a developer that I am inspired by!

Thanks for the suggestion, I'll definitely look at this.

d0whc3r commented 4 years ago

I use https://github.com/timocov/dts-bundle-generator in my typescript projects, for types generator, you should try it

antfu commented 4 years ago

@d0whc3r it's not about bundling. /@voie/pages is a virtual path that linked to a generator, there is no actual path/module for TS to infer the types.

d0whc3r commented 4 years ago

ah ok hehe

brattonross commented 4 years ago

This is turning out to be an interesting issue... since the module name /@voie/pages is not a valid package name, I'm not sure how I could go about using a package to declare types. TypeScript also isn't happy about using an ambient module declaration to declare /@voie/pages

antfu commented 4 years ago

This is turning out to be an interesting issue... since the module name /@voie/pages is not a valid package name, I'm not sure how I could go about using a package to declare types. TypeScript also isn't happy about using an ambient module declaration to declare /@voie/pages

Is that possible for you to rename it to something like @voie/pages? I treat using absolute path a dangerous smell (though in this case it's actually a virtual path, my linter still complain about it 😅)

brattonross commented 4 years ago

I've done some more work towards fixing this issue. I've created a PR but haven't merged it because I think it might still need to change.

One problem that I've found with creating a package with the name @voie/pages for type definitions is that vite would complain that there was no entry point in the package, as it only contained a .d.ts file. This problem can be resolved by publishing a @types package for the generated routes, i.e. a @types/voie__pages package.

What I need to understand now is do I need to create a PR in the DefinitelyTyped repo in order to publish the @types/voie__pages package, or can I just publish it from this repo?

antfu commented 4 years ago

Sorry I missed the notification. I don't think you can publish @types/voie__pages without merging into DefinitelyTyped. And that would make the maintenance hard I think.

Guess you can create an empty entry for @voie/pages as something like

module.exports = []

And hijack the module resolution in the plugin. Like

https://github.com/antfu/purge-icons/blob/cd87acfd9bffddb72c2e01cb79ad4ae53cf65c4e/packages/rollup-plugin-purge-icons/src/index.ts#L8-L16

https://github.com/antfu/purge-icons/blob/cd87acfd9bffddb72c2e01cb79ad4ae53cf65c4e/packages/vite-plugin-purge-icons/src/server.ts#L7-L9

Good luck :)

brattonross commented 4 years ago

I agree that adding to DefinitelyTyped would not be ideal. I've done as you've suggested at it is working nicely! :)