vite-plugin-generoutes 0.2.8 → 0.2.10

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Ronnie Zhang(大脸怪)
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ronnie Zhang(大脸怪)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -10,25 +10,30 @@ import { Plugin } from 'vite';
10
10
 
11
11
  interface Options {
12
12
  /**
13
- * pages 文件夹
13
+ * pages folder
14
14
  *
15
15
  * default: `src/pages`
16
16
  */
17
17
  pagesFolder: string;
18
18
  /**
19
- * 忽略文件夹
19
+ * ignore folders
20
+ *
21
+ * ignore these folders when generating routes
20
22
  *
21
23
  * default: `['components']`
22
24
  */
23
25
  ignoreFolders: string[];
24
26
  /**
25
- * 路由文件路径
27
+ * routes file path
28
+ *
29
+ * default: `src/router/routes.js`
30
+ *
31
+ * It can also be a ts file,such as `src/router/routes.ts`
26
32
  *
27
- * default: `${pagesFolder}/generoutes.js`
28
33
  */
29
- routesPath?: string;
34
+ routesPath: string;
30
35
  /**
31
- * 是否嵌套
36
+ * nested routes
32
37
  *
33
38
  * default: false
34
39
  */
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ import { parse } from "@vue/compiler-sfc";
6
6
  import { debounce, slash } from "@antfu/utils";
7
7
  import chokidar from "chokidar";
8
8
  import prettier from "prettier";
9
+ import chalk from "chalk";
9
10
 
10
11
  // src/utils.ts
11
12
  function toPascalCase(str) {
@@ -59,19 +60,14 @@ function findDuplicateRoutes(routes) {
59
60
  }
60
61
 
61
62
  // src/index.ts
62
- var defaultOptions = {
63
- pagesFolder: "src/pages",
64
- ignoreFolders: ["components"],
65
- nested: false
66
- };
67
63
  function VitePluginGeneroutes(options = {}) {
68
64
  if (options.routesPath && ![".ts", ".js"].includes(path.extname(options.routesPath)))
69
65
  throw new Error("routesPath must be a js or ts file path, such as src/router/generoutes.js");
70
66
  let rootDir;
71
- const pagesFolder = options.pagesFolder || defaultOptions.pagesFolder;
72
- const routesPath = options.routesPath || path.join(pagesFolder, "generoutes.js");
73
- const nested = options.nested || defaultOptions.nested;
74
- const ignoreFolders = options.ignoreFolders || defaultOptions.ignoreFolders;
67
+ const pagesFolder = options.pagesFolder || "src/pages";
68
+ const routesPath = options.routesPath || "src/router/routes.js";
69
+ const ignoreFolders = options.ignoreFolders || ["components"];
70
+ const nested = options.nested || false;
75
71
  const defineOptionsCache = /* @__PURE__ */ new Map();
76
72
  function generateMenusAndRoutes() {
77
73
  const pages = globSync(`${pagesFolder}/**/*.vue`, { ignore: ignoreFolders.map((folder) => `${pagesFolder}/**/${folder}/**`) });
@@ -104,11 +100,10 @@ function VitePluginGeneroutes(options = {}) {
104
100
  routes: nested ? convertToTree(routes) : routes
105
101
  };
106
102
  }
107
- async function writerRoutesFile() {
103
+ async function writerRoutesFile(isInit = false) {
108
104
  const { routes } = generateMenusAndRoutes();
109
105
  let routesStr = `
110
- // ! \u6B64\u6587\u4EF6\u7531 vite-plugin-generoutes \u81EA\u52A8\u751F\u6210\uFF0C\u5EFA\u8BAE\u6DFB\u52A0\u5230 .gitignore\uFF0C\u8BF7\u52FF\u76F4\u63A5\u5728\u6B64\u6587\u4EF6\u4FEE\u6539!!!
111
- // ! This file is generated by vite-plugin-generoutes, it is recommended to add it to .gitignore, do not modify it directly in this file!!!
106
+ // Generated by vite-plugin-generoutes, don't modify it directly.
112
107
 
113
108
  export const routes = ${JSON.stringify(routes, null, 2)}
114
109
  `;
@@ -117,6 +112,7 @@ function VitePluginGeneroutes(options = {}) {
117
112
  const filePath = path.resolve(rootDir, routesPath);
118
113
  await fs.ensureDir(path.dirname(filePath));
119
114
  fs.writeFileSync(filePath, routesStr);
115
+ console.log(` \u2705 ${isInit ? "routes generated:" : "routes updated:"} ${chalk.cyan(routesPath)}`);
120
116
  }
121
117
  const debounceWriter = debounce(500, writerRoutesFile);
122
118
  let watcher;
@@ -138,9 +134,10 @@ function VitePluginGeneroutes(options = {}) {
138
134
  name: "vite-plugin-generoutes",
139
135
  async configResolved(config) {
140
136
  rootDir = config.root;
141
- await writerRoutesFile();
142
- if (config.command !== "build")
137
+ await writerRoutesFile(true);
138
+ if (config.command !== "build") {
143
139
  createWatcher();
140
+ }
144
141
  },
145
142
  async handleHotUpdate({ file, read }) {
146
143
  if (file.includes(pagesFolder) && !ignoreFolders.some((folder) => file.includes(`/${folder}/`)) && file.endsWith(".vue")) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-generoutes",
3
3
  "type": "module",
4
- "version": "0.2.8",
4
+ "version": "0.2.10",
5
5
  "packageManager": "pnpm@9.9.0",
6
6
  "description": "A Vite plugin that generate routes based on the file structure, supports dynamic routes, and supports custom meta data for each route.",
7
7
  "author": "Ronnie Zhang <zclzone@outlook.com>",
@@ -51,6 +51,7 @@
51
51
  "dependencies": {
52
52
  "@antfu/utils": "^0.7.10",
53
53
  "@vue/compiler-sfc": "^3.4.38",
54
+ "chalk": "^5.3.0",
54
55
  "chokidar": "^3.6.0",
55
56
  "fs-extra": "^11.2.0",
56
57
  "glob": "^10.4.5",