emosheeep / vite-plugin-virtual-mpa

Out-of-box MPA plugin for Vite, generate multiple entries using only one template.
https://stackblitz.com/~/github.com/emosheeep/vite-plugin-virtual-mpa
MIT License
116 stars 15 forks source link

scanPages 最好能够扫描目录内的模板文件 #38

Closed brookqin closed 1 year ago

brookqin commented 1 year ago

https://github.com/emosheeep/vite-plugin-virtual-mpa/blob/d70771090351bfa2a1f40abb39e4f6c9b8e9363b/workspaces/plugin/src/plugin.ts#L307-L329

function scanPages(scanOptions?: ScanOptions, defaultTemplate: string) {
  const { filename, entryFile, scanDirs } = scanOptions || {} as ScanOptions;
  const pages: Page[] = [];

  for (const entryDir of [scanDirs].flat().filter(Boolean)) {
    for (const name of fs.readdirSync(entryDir)) {
      const dir = path.join(entryDir, name); // dir path
      if (!fs.statSync(dir).isDirectory()) continue;

      const templateFile = path.join(dir, defaultTemplate);

      pages.push({
        name,
        filename: typeof filename === 'function'
          ? filename(name) as Page['filename']
          : undefined,
        entry: entryFile
          ? path.join('/', dir, entryFile) as Page['entry']
          : undefined,
        template: fs.existsSync(templateFile) ? templateFile : undefined
      });
    }
  }

  return pages;
}
emosheeep commented 1 year ago

这个可以考虑