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

Fail to build routes in Windows #23

Closed KMilhan closed 3 years ago

KMilhan commented 3 years ago

Dear contributors,

Thanks for this awesome project! :)

After the recent update, I discovered voie is failing to build a route in my Windows machine. Quick jest run result is

C:\Users\10629\WebstormProjects\vite-plugin-voie\packages\vite-plugin-voie>npm test

> vite-plugin-voie@0.7.1 test
> jest

 FAIL  test/routes.spec.ts
  √ given basic routes it should return the correct structure (3 ms)
  √ given dynamic routes it should return the correct structure (1 ms)
  √ given nested routes it should return the correct structure
  √ given a catch-all route it should return the correct structure (1 ms)
  √ given a glob as pagesDir and extend route it should resolve to correct path
  × given a custom root, should return the correct component path (6 ms)

  ● given a custom root, should return the correct component path

    expect(received).toEqual(expected) // deep equality

    - Expected  - 9
    + Received  + 9

      Array [
        Object {
    -     "component": "/pages/index.vue",
    -     "name": "index",
    -     "path": "/",
    +     "component": "/\\pages\\index.vue",
    +     "name": "",
    +     "path": "",
        },
        Object {
    -     "component": "/pages/user/index.vue",
    -     "name": "user",
    -     "path": "/user",
    +     "component": "/\\pages\\user\\index.vue",
    +     "name": "",
    +     "path": "",
        },
        Object {
    -     "component": "/pages/user/one.vue",
    -     "name": "user-one",
    -     "path": "/user/one",
    +     "component": "/\\pages\\user\\one.vue",
    +     "name": "",
    +     "path": "",
        },
      ]

      233 |     root,
      234 |   });
    > 235 |   expect(actual).toEqual(expected);
          |                  ^
      236 | });
      237 | 

      at Object.<anonymous> (test/routes.spec.ts:235:18)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 5 passed, 6 total
Snapshots:   0 total
Time:        1.853 s, estimated 2 s
Ran all test suites.
npm ERR! code 1
npm ERR! path C:\Users\10629\WebstormProjects\vite-plugin-voie\packages\vite-plugin-voie
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c jest

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\10629\AppData\Local\npm-cache\_logs\2021-01-07T08_27_39_339Z-debug.log

I believe the problem occurs after the release 0.5 and using path.sep inside routes.ts and having actual set of Vue files as a test resource would be a remedy.

brattonross commented 3 years ago

Hi, thanks for the kind words, and for bringing this to my attention.

After looking into this a little, I'm pretty certain that most of the passing tests in routes.spec.ts are actually false positives on Windows because we are using paths that we'd never see in practice e.g. src/pages/index.vue.

To boost my confidence that we are supporting Windows correctly I'm going to update the tests to be platform agnostic, which should hopefully lead to a fix in the process 😀

greenskybluephish commented 3 years ago

Part of the issue is tied to fast-glob/micromatch library. Their pattern matching doesn't work with path.join() on Windows, so the buildRoutes method was getting an empty array of files. https://github.com/micromatch/micromatch#backslashes

greenskybluephish commented 3 years ago

FWIW, I was able to build successfully on Windows using the following.

async function generateRoutesCode(options) {
  const {root, pagesDir, exclude, extensions, extendRoute} = options;

 //const dir = _path2.default.join(root, pagesDir);

  const joinedPath = _path2.default.join(root, pagesDir);
  const dir = joinedPath.replace(/\\/g, '/');

  const files = await resolve({dir, extensions, exclude});
  const routes = buildRoutes({files, dir, extensions, root, extendRoute});
  return stringifyRoutes(routes, options);
}
lfhenrl commented 3 years ago

I got the same problem [Vue Router warn]: No match found for location with path "/" After I ugrade to vite 2 and vite-plugin-voie: 0.7.1

brattonross commented 3 years ago

Thanks @greenskybluephish for your input, I've come up with a fix based on your solution. I've tested this on Windows and it appears to be working just fine, going to test it on WSL and then hopefully I can release a fix 😀

brattonross commented 3 years ago

A fix has been released in v0.7.2, please let me know if you see any more issues

KMilhan commented 3 years ago

Problems are totally gone. Appreciate it a lot @brattonross .