vite-plugin-vue-block-routes 1.0.0

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/client.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ declare module 'virtual:generated-routes' {
2
+ import type { RouteRecordRaw } from 'vue-router';
3
+ const routes: RouteRecordRaw[];
4
+ export default routes;
5
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ default: () => vueBlockRoutes
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_compiler_sfc = require("@vue/compiler-sfc");
37
+ var import_fast_glob = __toESM(require("fast-glob"), 1);
38
+ var import_fs = __toESM(require("fs"), 1);
39
+ var import_path = __toESM(require("path"), 1);
40
+ function vueBlockRoutes(options = {}) {
41
+ const virtualModuleId = "virtual:generated-routes";
42
+ const resolvedVirtualModuleId = "\0" + virtualModuleId;
43
+ const pagesDir = options.pagesDir || "src/pages";
44
+ return {
45
+ name: "vite-plugin-vue-block-routes",
46
+ // 1. 拦截虚拟模块导入
47
+ resolveId(id) {
48
+ if (id === virtualModuleId) {
49
+ return resolvedVirtualModuleId;
50
+ }
51
+ },
52
+ // 2. 加载虚拟模块内容
53
+ async load(id) {
54
+ if (id === resolvedVirtualModuleId) {
55
+ const files = await (0, import_fast_glob.default)(`${pagesDir}/**/*.vue`, { cwd: process.cwd() });
56
+ const routes = [];
57
+ for (const file of files) {
58
+ const filePath = import_path.default.resolve(process.cwd(), file);
59
+ const content = import_fs.default.readFileSync(filePath, "utf-8");
60
+ const { descriptor } = (0, import_compiler_sfc.parse)(content);
61
+ const routeBlock = descriptor.customBlocks.find((b) => b.type === "route");
62
+ if (routeBlock) {
63
+ try {
64
+ const routeData = JSON.parse(routeBlock.content.trim());
65
+ const componentPath = `/${file}`;
66
+ const routeString = `{
67
+ path: '${routeData.path || "/" + import_path.default.basename(file, ".vue")}',
68
+ name: '${routeData.name || import_path.default.basename(file, ".vue")}',
69
+ component: () => import('${componentPath}'),
70
+ meta: ${JSON.stringify(routeData.meta || {})}
71
+ }`;
72
+ routes.push(routeString);
73
+ } catch (e) {
74
+ console.error(`[vite-plugin] Error parsing route block in ${file}:`, e);
75
+ }
76
+ }
77
+ }
78
+ return `export default [${routes.join(",\n")}]`;
79
+ }
80
+ },
81
+ // 3. 处理 HMR (热更新)
82
+ // 当 Vue 文件变化时,如果 route 块变了,需要通知 Vite 重新加载虚拟模块
83
+ handleHotUpdate({ file, server }) {
84
+ if (file.includes(pagesDir) && file.endsWith(".vue")) {
85
+ const mod = server.moduleGraph.getModuleById(resolvedVirtualModuleId);
86
+ if (mod) {
87
+ server.moduleGraph.invalidateModule(mod);
88
+ server.ws.send({
89
+ type: "full-reload",
90
+ path: "*"
91
+ });
92
+ }
93
+ }
94
+ }
95
+ };
96
+ }
@@ -0,0 +1,8 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface PluginOptions {
4
+ pagesDir?: string;
5
+ }
6
+ declare function vueBlockRoutes(options?: PluginOptions): Plugin;
7
+
8
+ export { vueBlockRoutes as default };
@@ -0,0 +1,8 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface PluginOptions {
4
+ pagesDir?: string;
5
+ }
6
+ declare function vueBlockRoutes(options?: PluginOptions): Plugin;
7
+
8
+ export { vueBlockRoutes as default };
package/dist/index.js ADDED
@@ -0,0 +1,65 @@
1
+ // src/index.ts
2
+ import { parse } from "@vue/compiler-sfc";
3
+ import glob from "fast-glob";
4
+ import fs from "fs";
5
+ import path from "path";
6
+ function vueBlockRoutes(options = {}) {
7
+ const virtualModuleId = "virtual:generated-routes";
8
+ const resolvedVirtualModuleId = "\0" + virtualModuleId;
9
+ const pagesDir = options.pagesDir || "src/pages";
10
+ return {
11
+ name: "vite-plugin-vue-block-routes",
12
+ // 1. 拦截虚拟模块导入
13
+ resolveId(id) {
14
+ if (id === virtualModuleId) {
15
+ return resolvedVirtualModuleId;
16
+ }
17
+ },
18
+ // 2. 加载虚拟模块内容
19
+ async load(id) {
20
+ if (id === resolvedVirtualModuleId) {
21
+ const files = await glob(`${pagesDir}/**/*.vue`, { cwd: process.cwd() });
22
+ const routes = [];
23
+ for (const file of files) {
24
+ const filePath = path.resolve(process.cwd(), file);
25
+ const content = fs.readFileSync(filePath, "utf-8");
26
+ const { descriptor } = parse(content);
27
+ const routeBlock = descriptor.customBlocks.find((b) => b.type === "route");
28
+ if (routeBlock) {
29
+ try {
30
+ const routeData = JSON.parse(routeBlock.content.trim());
31
+ const componentPath = `/${file}`;
32
+ const routeString = `{
33
+ path: '${routeData.path || "/" + path.basename(file, ".vue")}',
34
+ name: '${routeData.name || path.basename(file, ".vue")}',
35
+ component: () => import('${componentPath}'),
36
+ meta: ${JSON.stringify(routeData.meta || {})}
37
+ }`;
38
+ routes.push(routeString);
39
+ } catch (e) {
40
+ console.error(`[vite-plugin] Error parsing route block in ${file}:`, e);
41
+ }
42
+ }
43
+ }
44
+ return `export default [${routes.join(",\n")}]`;
45
+ }
46
+ },
47
+ // 3. 处理 HMR (热更新)
48
+ // 当 Vue 文件变化时,如果 route 块变了,需要通知 Vite 重新加载虚拟模块
49
+ handleHotUpdate({ file, server }) {
50
+ if (file.includes(pagesDir) && file.endsWith(".vue")) {
51
+ const mod = server.moduleGraph.getModuleById(resolvedVirtualModuleId);
52
+ if (mod) {
53
+ server.moduleGraph.invalidateModule(mod);
54
+ server.ws.send({
55
+ type: "full-reload",
56
+ path: "*"
57
+ });
58
+ }
59
+ }
60
+ }
61
+ };
62
+ }
63
+ export {
64
+ vueBlockRoutes as default
65
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "vite-plugin-vue-block-routes",
3
+ "version": "1.0.0",
4
+ "description": "Generate Vue Router config from SFC custom blocks",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "type": "module",
9
+ "files": [
10
+ "dist",
11
+ "client.d.ts"
12
+ ],
13
+ "scripts": {
14
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean",
15
+ "prepublishOnly": "npm run build"
16
+ },
17
+ "keywords": [
18
+ "vite",
19
+ "vue",
20
+ "router",
21
+ "plugin"
22
+ ],
23
+ "peerDependencies": {
24
+ "vite": "^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0"
25
+ },
26
+ "dependencies": {
27
+ "@vue/compiler-sfc": "^3.0.0",
28
+ "fast-glob": "^3.0.0"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^25.0.3",
32
+ "tsup": "^8.5.1",
33
+ "typescript": "^5.9.3"
34
+ }
35
+ }