posva / unplugin-vue-router

Next Generation file based typed routing for Vue Router
https://uvr.esm.is
MIT License
1.7k stars 82 forks source link

Export resolveOptions method #500

Closed tmg0 closed 2 months ago

tmg0 commented 2 months ago

Recently, I've been using unplugin-vue-router and rspack to build my application, but I've met some issues. So I'm trying to generating route records and inject to a virtual file manually.

I found that unplugin-vue-router exports a createRoutesContext function, but its parameters have already been processed by resolveOptions and undergone complex manipulations. This makes createRoutesContext virtually unusable directly.

I'm wondering if it would be possible to export the resolveOptions method, or move this into the createRoutesContext?

If it's possable, I'm glad to contribute a PR to improve this situation.

posva commented 2 months ago

Why can’t you use the exposed plugin directly? Rspack is supported

tmg0 commented 2 months ago

Thank you for your response.

I tried using unplugin-vue-router with rspack directly, but I encountered some issues. I've opened an issue in the rspack repo, and it seems the problem lies in unplugin rspack virtual.

unplugin-vue-router is a really awesome package, so I'd like to find some other ways to use it.

ERROR in ./node_modules/.pnpm/unplugin@1.12.3/node_modules/unplugin/dist/rspack/virtual.js?unplugin_rspack_virtual=%2F__vue-router%2Fauto-routes
  × Module parse failed:
  ╰─▶   × JavaScript parsing error: 'import', and 'export' cannot be used outside of module code
         ╭─[1:0]
       1 │ export const routes = [
         · ──────
       2 │   {
       3 │     path: '/',
         ╰────

  help:
        File was processed with these loaders:
         * commonjs|/nuxlite/node_modules/.pnpm/unplugin@1.12.3/node_modules/unplugin/dist/rspack/loaders/load.js??ruleSet[1].rules[2].use[0]
        You may need an additional loader to handle the result of these loaders.
posva commented 2 months ago

I see. The module should be usable. There is no getting around the virtual modules for it to work, so even if more parts of the code are exposed, it will still not work.

Note that even createRoutesContext is not documented, it's an internal feature too and could change in any version. I would suggest that you don't use it. You can copy the code of resolveOptions if you want to play around until virtual modules are supported but I think you will find other blockers.

Note that this library has many more improvements like HMR that only work with Vite. So I recommend you to use it with Vite if you can too

tmg0 commented 2 months ago

Thank you very much for your suggestion! I will still use Vite in production to avoid unknown issues.