vite-plugin-generoutes 0.0.14 → 0.0.16
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.
- package/dist/index.d.ts +4 -1
- package/dist/index.js +17 -10
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,9 @@ import { Plugin } from 'vite';
|
|
|
8
8
|
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
|
|
9
9
|
**********************************/
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
interface Options {
|
|
12
|
+
isBuild?: boolean;
|
|
13
|
+
}
|
|
14
|
+
declare function VitePluginGeneroutes(options?: Options): Plugin<any>;
|
|
12
15
|
|
|
13
16
|
export { VitePluginGeneroutes as default };
|
package/dist/index.js
CHANGED
|
@@ -6,19 +6,21 @@ import { globSync } from "glob";
|
|
|
6
6
|
import { parse } from "@vue/compiler-sfc";
|
|
7
7
|
import { debounce, slash } from "@antfu/utils";
|
|
8
8
|
import chokidar from "chokidar";
|
|
9
|
-
function VitePluginGeneroutes() {
|
|
9
|
+
function VitePluginGeneroutes(options = {}) {
|
|
10
10
|
const defineOptionsCache = /* @__PURE__ */ new Map();
|
|
11
11
|
let rootDir;
|
|
12
12
|
let routesFolder = "src/router/generoutes/";
|
|
13
13
|
const writerRoutesFile = debounce(500, async () => {
|
|
14
14
|
const { routes } = generateMenusAndRoutes();
|
|
15
|
-
const routesStr =
|
|
15
|
+
const routesStr = `
|
|
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
|
+
|
|
16
18
|
export const routes = ${JSON.stringify(routes, null, 2)}
|
|
17
19
|
`.replace(/"##(.*)##"/g, (_, p1) => `() => import('${p1}')`);
|
|
18
20
|
const filePath = `${routesFolder}/index.js`;
|
|
19
21
|
fs.writeFileSync(filePath, routesStr);
|
|
20
22
|
try {
|
|
21
|
-
exec(`npx eslint --fix ${filePath}`);
|
|
23
|
+
exec(`npx eslint --fix ${filePath} --no-ignore`);
|
|
22
24
|
} catch (error) {
|
|
23
25
|
}
|
|
24
26
|
});
|
|
@@ -35,8 +37,7 @@ function VitePluginGeneroutes() {
|
|
|
35
37
|
const name = defineOptions?.name || meta.id;
|
|
36
38
|
meta.parentId = pageFolders.slice(0, -1).join("_") || null;
|
|
37
39
|
const component = `##/${filePath}##`;
|
|
38
|
-
|
|
39
|
-
let routePath = folderPath;
|
|
40
|
+
let routePath = `/${pageFolders.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
|
|
40
41
|
if (meta.isNotFound) {
|
|
41
42
|
routePath = "/:pathMatch(.*)*";
|
|
42
43
|
}
|
|
@@ -58,20 +59,26 @@ function VitePluginGeneroutes() {
|
|
|
58
59
|
routes
|
|
59
60
|
};
|
|
60
61
|
}
|
|
62
|
+
function createWatcher() {
|
|
63
|
+
const watcher = chokidar.watch("src/pages/**/index.vue", { ignoreInitial: true });
|
|
64
|
+
return watcher.on("all", async (event, path2) => {
|
|
65
|
+
if (path2.endsWith("index.vue") && (event === "add" || event === "unlink")) {
|
|
66
|
+
writerRoutesFile();
|
|
67
|
+
await watcher.close();
|
|
68
|
+
createWatcher();
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
61
72
|
return {
|
|
62
73
|
name: "vite-plugin-generoutes",
|
|
63
74
|
async configResolved(config) {
|
|
64
75
|
rootDir = config.root;
|
|
65
76
|
routesFolder = slash(path.resolve(rootDir, routesFolder));
|
|
66
77
|
await fs.ensureDir(routesFolder);
|
|
67
|
-
chokidar.watch("src/pages/**/index.vue", { ignoreInitial: true }).on("all", (event, path2) => {
|
|
68
|
-
if (path2.endsWith("index.vue") && (event === "add" || event === "unlink")) {
|
|
69
|
-
writerRoutesFile();
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
78
|
},
|
|
73
79
|
buildStart() {
|
|
74
80
|
writerRoutesFile();
|
|
81
|
+
!options.isBuild && createWatcher();
|
|
75
82
|
},
|
|
76
83
|
async handleHotUpdate({ file, read }) {
|
|
77
84
|
if (file.includes("src/pages") && file.endsWith("index.vue")) {
|