vite-plugin-generoutes 0.0.16 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +14 -18
  2. package/package.json +3 -2
package/dist/index.js CHANGED
@@ -1,29 +1,27 @@
1
1
  // src/index.ts
2
2
  import path from "node:path";
3
- import { exec } from "node:child_process";
4
3
  import fs from "fs-extra";
5
4
  import { globSync } from "glob";
6
5
  import { parse } from "@vue/compiler-sfc";
7
6
  import { debounce, slash } from "@antfu/utils";
8
7
  import chokidar from "chokidar";
8
+ import prettier from "prettier";
9
9
  function VitePluginGeneroutes(options = {}) {
10
10
  const defineOptionsCache = /* @__PURE__ */ new Map();
11
11
  let rootDir;
12
12
  let routesFolder = "src/router/generoutes/";
13
- const writerRoutesFile = debounce(500, async () => {
13
+ async function writerRoutesFile() {
14
14
  const { routes } = generateMenusAndRoutes();
15
- const routesStr = `
15
+ let routesStr = `
16
16
  // ! \u6B64\u6587\u4EF6\u7531 vite-plugin-generoutes \u81EA\u52A8\u751F\u6210\uFF0C\u8BF7\u52FF\u4FEE\u6539\uFF0C\u8BF7\u52FF\u4FEE\u6539\uFF0C\u8BF7\u52FF\u4FEE\u6539!!!
17
17
 
18
18
  export const routes = ${JSON.stringify(routes, null, 2)}
19
- `.replace(/"##(.*)##"/g, (_, p1) => `() => import('${p1}')`);
20
- const filePath = `${routesFolder}/index.js`;
21
- fs.writeFileSync(filePath, routesStr);
22
- try {
23
- exec(`npx eslint --fix ${filePath} --no-ignore`);
24
- } catch (error) {
25
- }
26
- });
19
+ `;
20
+ routesStr = routesStr.replace(/"##(.*)##"/g, (_, p1) => `() => import('${p1}')`);
21
+ routesStr = await prettier.format(routesStr, { parser: "babel", semi: false, singleQuote: true });
22
+ fs.writeFileSync(`${routesFolder}/index.js`, routesStr);
23
+ }
24
+ const debounceWriter = debounce(500, writerRoutesFile);
27
25
  function generateMenusAndRoutes() {
28
26
  const pages = globSync("src/pages/**/index.vue");
29
27
  const routes = pages.map((filePath) => {
@@ -63,7 +61,7 @@ function VitePluginGeneroutes(options = {}) {
63
61
  const watcher = chokidar.watch("src/pages/**/index.vue", { ignoreInitial: true });
64
62
  return watcher.on("all", async (event, path2) => {
65
63
  if (path2.endsWith("index.vue") && (event === "add" || event === "unlink")) {
66
- writerRoutesFile();
64
+ debounceWriter();
67
65
  await watcher.close();
68
66
  createWatcher();
69
67
  }
@@ -75,20 +73,18 @@ function VitePluginGeneroutes(options = {}) {
75
73
  rootDir = config.root;
76
74
  routesFolder = slash(path.resolve(rootDir, routesFolder));
77
75
  await fs.ensureDir(routesFolder);
78
- },
79
- buildStart() {
80
- writerRoutesFile();
76
+ await writerRoutesFile();
81
77
  !options.isBuild && createWatcher();
82
78
  },
83
79
  async handleHotUpdate({ file, read }) {
84
80
  if (file.includes("src/pages") && file.endsWith("index.vue")) {
85
81
  const prevDefineOptions = defineOptionsCache.get(slash(path.relative(rootDir, file)));
86
82
  if (!prevDefineOptions) {
87
- writerRoutesFile();
83
+ debounceWriter();
88
84
  } else {
89
85
  const defineOptions = JSON.stringify(parseDefineOptions(file, await read()));
90
- if (defineOptions && prevDefineOptions !== defineOptions) {
91
- writerRoutesFile();
86
+ if (prevDefineOptions !== defineOptions) {
87
+ debounceWriter();
92
88
  }
93
89
  }
94
90
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-generoutes",
3
3
  "type": "module",
4
- "version": "0.0.16",
4
+ "version": "0.0.18",
5
5
  "packageManager": "pnpm@9.1.1",
6
6
  "description": "_description_",
7
7
  "author": "Ronnie Zhang <zclzone@outlook.com>",
@@ -52,7 +52,8 @@
52
52
  "@vue/compiler-sfc": "^3.4.27",
53
53
  "chokidar": "^3.6.0",
54
54
  "fs-extra": "^11.2.0",
55
- "glob": "^10.4.1"
55
+ "glob": "^10.4.1",
56
+ "prettier": "^3.3.2"
56
57
  },
57
58
  "devDependencies": {
58
59
  "@antfu/eslint-config": "^2.18.1",