farm-fe / farm

Extremely fast Vite-compatible web build tool written in Rust
https://farmfe.org
MIT License
5.05k stars 165 forks source link

[Bug Report]: Failed to start if one of route is removed #1975

Open fu050409 opened 4 days ago

fu050409 commented 4 days ago

Steps to reproduce

import {
  createRouter,
  createWebHashHistory,
  type RouteRecordRaw,
  type RouteComponent,
} from "vue-router";

const views: Record<string, { default: RouteComponent }> = import.meta.glob(
  ["./views/**/*.vue", "./views/**/index.vue", "./views/**/\\[*\\].vue"],
  {
    eager: true,
  }
);

const routes: RouteRecordRaw[] = Object.entries(views).map(
  ([filePath, component]) => {
    let path = filePath
      .replace(/^\.\/views\//, "")
      .replace(/\.vue$/, "")
      .replace(/^(.*)\/?index$/, "$1")
      .replace(/\[(\w+)\]$/, ":$1");
    path = "/" + path;

    return {
      path,
      name: filePath.replace(/^\.\/views\//, ""),
      component: component.default,
    } satisfies RouteRecordRaw;
  }
);

export default createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [...routes],
});

Code above which stayed at src/router.ts can automatically load all routes from views directory, consider remove a .vue file from src/views

Reproduce link

No response

What is actually happening?

[ Farm ] Error: Failed to start the server
Caused by: Error: Can not resolve `./views/account/pro.vue` from src/router.ts.
Original error: None.

Potential Causes:
1.The file that `./views/account/pro.vue` points to does not exist.
2.Install it first if `./views/account/pro.vue` is an dependency from node_modules, if you are using pnpm refer to [https://pnpm.io/faq#pnpm-does-not-work-with-your-project-here] for solutions.
3. If `./views/account/pro.vue` is a alias, make sure your alias config is correct.

System Info

any
fu050409 commented 4 days ago

A workaround is to run pnpm farm clean to remove all cached files and restart the dev server.

wre232114 commented 8 hours ago

It's a bug, the module that uses import.meta.glob should not be cached.