hannoeru / vite-plugin-pages

File system based route generator for ⚡️Vite
MIT License
1.84k stars 127 forks source link

extendRoute does not support JS #338

Closed blowsie closed 1 year ago

blowsie commented 1 year ago

Describe the bug

Since yml and json5 don't allow us to write JS functions, I tried to extend a route using extendRoute, however that fails when writing functions too.

Config

  extendRoute(route) {
          if (route.meta && route.meta.props) {
            route.props = function () {
              return route.meta.props
            }
          }
          return route
        },

Output

const __pages_import_0__ = ()=>import("/src/pages/test.vue");

const routes = [{
    "name": "test",
    "path": "/test",
    "component": __pages_import_0__,
    "props": function() {
        returnroute.meta.props;
    },
    "meta": {
        "title": "Item",
        "props": ["item", "delivery", "timeline"]
    }
}];

export default routes;

Reproduction

https://stackblitz.com/edit/vitejs-vite-zwtdaf?file=vite.config.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm

Used Package Manager

npm

Validations

blowsie commented 1 year ago

also fails on onRoutesGenerated

   onRoutesGenerated(routes) {
          return routes.map((route) => {
            return {
              ...route,
              props: (to) => ({
                test: 'it',
              }),
            }
          })
        },
hannoeru commented 1 year ago

Because routes need to be serialize during build time then pass to frontend, so if you want to include some logic in props, try to extends routes in your entry point. ex. main.ts