vite-plugin-generate-route 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/README.md ADDED
@@ -0,0 +1,15 @@
1
+ ### VitePluginGenerateRoute
2
+
3
+ ```javaScript
4
+ // vue.config.js
5
+ import VitePluginGenerateRoute from 'vite-plugin-generate-route'
6
+
7
+ export default defineConfig({
8
+ plugins: [
9
+ VitePluginGenerateRoute({
10
+ pageDir = 'src/views',
11
+ routesFile = 'src/routes/generateRoutes.js'
12
+ })
13
+ ]
14
+ })
15
+ ```
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),r=require("path"),t=require("vite");function n(i,o){return e.readdirSync(i).flatMap(s=>{const l=r.resolve(i,s),u=r.relative(o,l);if(e.statSync(l).isDirectory())return n(l,o);if(s.endsWith(".vue")){const r=u.replace(/\\/g,"/").replace(/\.vue$/,"").replace(/\/index$/,"").replace(/\/_/g,"").split("/").filter(Boolean);r.length>1&&r[r.length-1]===r[r.length-2]&&r.pop();const n=`/${r.join("/").toLowerCase()}`,i=l.replace(".vue",".page.js"),o=e.existsSync(i)?require(i):{};return[{path:n,component:t.normalizePath(l),...o}]}return[]})}exports.default=function(t={}){const{pageDir:i="src/views",routesFile:o="src/routes/generateRoutes.js"}=t;return{name:"vite-plugin-generate-routes",config(t,{command:s}){const l=r.resolve(t.root||process.cwd(),i),u=n(l,l),c=`export default ${JSON.stringify(u,null,2)}`,a=r.resolve(t.root||process.cwd(),o),p=r.dirname(a);return e.existsSync(p)||e.mkdirSync(p,{recursive:!0}),e.writeFileSync(a,c),{build:{rollupOptions:{input:t.build?.rollupOptions?.input||"index.html"}}}}}};
2
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/index.js"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport { normalizePath } from 'vite';\n\nfunction generateRoutes(dir, basePath) {\n const files = fs.readdirSync(dir);\n return files.flatMap((file) => {\n const fullPath = path.resolve(dir, file);\n const relativePath = path.relative(basePath, fullPath);\n if (fs.statSync(fullPath).isDirectory()) {\n return generateRoutes(fullPath, basePath);\n }\n if (file.endsWith('.vue')) {\n const routePath = relativePath\n .replace(/\\\\/g, '/')\n .replace(/\\.vue$/, '')\n .replace(/\\/index$/, '')\n .replace(/\\/_/g, '');\n\n const parts = routePath.split('/').filter(Boolean);\n\n if (parts.length > 1 && parts[parts.length - 1] === parts[parts.length - 2]) {\n parts.pop();\n }\n\n const finalPath = `/${parts.join('/').toLowerCase()}`;\n\n const metaFilePath = fullPath.replace('.vue', '.page.js');\n const meta = fs.existsSync(metaFilePath) ? require(metaFilePath) : {};\n\n return [\n {\n path: finalPath,\n component: normalizePath(fullPath),\n ...meta,\n },\n ];\n }\n\n return [];\n });\n}\n\nexport default function VitePluginGenerateRoutes(options = {}) {\n const { pageDir = 'src/views', routesFile = 'src/routes/generateRoutes.js' } = options;\n return {\n name: 'vite-plugin-generate-routes',\n config(config, { command }) {\n const basePath = path.resolve(config.root || process.cwd(), pageDir);\n const routes = generateRoutes(basePath, basePath);\n const routesContent = `export default ${JSON.stringify(routes, null, 2)}`;\n const outputPath = path.resolve(config.root || process.cwd(), routesFile);\n\n // 确保输出目录存在\n const outputDir = path.dirname(outputPath);\n if (!fs.existsSync(outputDir)) {\n fs.mkdirSync(outputDir, { recursive: true });\n }\n\n fs.writeFileSync(outputPath, routesContent);\n\n return {\n build: {\n rollupOptions: {\n input: config.build?.rollupOptions?.input || 'index.html',\n },\n },\n };\n },\n };\n}\n"],"names":["generateRoutes","dir","basePath","fs","readdirSync","flatMap","file","fullPath","path","resolve","relativePath","relative","statSync","isDirectory","endsWith","parts","replace","split","filter","Boolean","length","pop","finalPath","join","toLowerCase","metaFilePath","meta","existsSync","require","component","normalizePath","options","pageDir","routesFile","name","config","command","root","process","cwd","routes","routesContent","JSON","stringify","outputPath","outputDir","dir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YH,GAEtB,CACLS,MAAO,CACLC,cAAe,CACbC,MAAOjB,EAAOe,OAAOC,eAAeC,OAAS,eAIrD,EAEJ"}
@@ -0,0 +1,2 @@
1
+ import e from"fs";import r from"path";import{normalizePath as t}from"vite";function n(o,i){return e.readdirSync(o).flatMap(s=>{const l=r.resolve(o,s),c=r.relative(i,l);if(e.statSync(l).isDirectory())return n(l,i);if(s.endsWith(".vue")){const r=c.replace(/\\/g,"/").replace(/\.vue$/,"").replace(/\/index$/,"").replace(/\/_/g,"").split("/").filter(Boolean);r.length>1&&r[r.length-1]===r[r.length-2]&&r.pop();const n=`/${r.join("/").toLowerCase()}`,o=l.replace(".vue",".page.js"),i=e.existsSync(o)?require(o):{};return[{path:n,component:t(l),...i}]}return[]})}function o(t={}){const{pageDir:o="src/views",routesFile:i="src/routes/generateRoutes.js"}=t;return{name:"vite-plugin-generate-routes",config(t,{command:s}){const l=r.resolve(t.root||process.cwd(),o),c=n(l,l),p=`export default ${JSON.stringify(c,null,2)}`,u=r.resolve(t.root||process.cwd(),i),a=r.dirname(u);return e.existsSync(a)||e.mkdirSync(a,{recursive:!0}),e.writeFileSync(u,p),{build:{rollupOptions:{input:t.build?.rollupOptions?.input||"index.html"}}}}}}export{o as default};
2
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":["../src/index.js"],"sourcesContent":["import fs from 'fs';\nimport path from 'path';\nimport { normalizePath } from 'vite';\n\nfunction generateRoutes(dir, basePath) {\n const files = fs.readdirSync(dir);\n return files.flatMap((file) => {\n const fullPath = path.resolve(dir, file);\n const relativePath = path.relative(basePath, fullPath);\n if (fs.statSync(fullPath).isDirectory()) {\n return generateRoutes(fullPath, basePath);\n }\n if (file.endsWith('.vue')) {\n const routePath = relativePath\n .replace(/\\\\/g, '/')\n .replace(/\\.vue$/, '')\n .replace(/\\/index$/, '')\n .replace(/\\/_/g, '');\n\n const parts = routePath.split('/').filter(Boolean);\n\n if (parts.length > 1 && parts[parts.length - 1] === parts[parts.length - 2]) {\n parts.pop();\n }\n\n const finalPath = `/${parts.join('/').toLowerCase()}`;\n\n const metaFilePath = fullPath.replace('.vue', '.page.js');\n const meta = fs.existsSync(metaFilePath) ? require(metaFilePath) : {};\n\n return [\n {\n path: finalPath,\n component: normalizePath(fullPath),\n ...meta,\n },\n ];\n }\n\n return [];\n });\n}\n\nexport default function VitePluginGenerateRoutes(options = {}) {\n const { pageDir = 'src/views', routesFile = 'src/routes/generateRoutes.js' } = options;\n return {\n name: 'vite-plugin-generate-routes',\n config(config, { command }) {\n const basePath = path.resolve(config.root || process.cwd(), pageDir);\n const routes = generateRoutes(basePath, basePath);\n const routesContent = `export default ${JSON.stringify(routes, null, 2)}`;\n const outputPath = path.resolve(config.root || process.cwd(), routesFile);\n\n // 确保输出目录存在\n const outputDir = path.dirname(outputPath);\n if (!fs.existsSync(outputDir)) {\n fs.mkdirSync(outputDir, { recursive: true });\n }\n\n fs.writeFileSync(outputPath, routesContent);\n\n return {\n build: {\n rollupOptions: {\n input: config.build?.rollupOptions?.input || 'index.html',\n },\n },\n };\n },\n };\n}\n"],"names":["generateRoutes","dir","basePath","fs","readdirSync","flatMap","file","fullPath","path","resolve","relativePath","relative","statSync","isDirectory","endsWith","parts","replace","split","filter","Boolean","length","pop","finalPath","join","toLowerCase","metaFilePath","meta","existsSync","require","component","normalizePath","VitePluginGenerateRoutes","options","pageDir","routesFile","name","config","command","root","process","cwd","routes","routesContent","JSON","stringify","outputPath","outputDir","dirname","mkdirSync","recursive","writeFileSync","build","rollupOptions","input"],"mappings":"2EAIA,SAASA,EAAeC,EAAKC,GAE3B,OADcC,EAAGC,YAAYH,GAChBI,QAASC,IACpB,MAAMC,EAAWC,EAAKC,QAAQR,EAAKK,GAC7BI,EAAeF,EAAKG,SAAST,EAAUK,GAC7C,GAAIJ,EAAGS,SAASL,GAAUM,cACxB,OAAOb,EAAeO,EAAUL,GAElC,GAAII,EAAKQ,SAAS,QAAS,CACzB,MAMMC,EANYL,EACfM,QAAQ,MAAO,KACfA,QAAQ,SAAU,IAClBA,QAAQ,WAAY,IACpBA,QAAQ,OAAQ,IAEKC,MAAM,KAAKC,OAAOC,SAEtCJ,EAAMK,OAAS,GAAKL,EAAMA,EAAMK,OAAS,KAAOL,EAAMA,EAAMK,OAAS,IACvEL,EAAMM,MAGR,MAAMC,EAAY,IAAIP,EAAMQ,KAAK,KAAKC,gBAEhCC,EAAelB,EAASS,QAAQ,OAAQ,YACxCU,EAAOvB,EAAGwB,WAAWF,GAAgBG,QAAQH,GAAgB,CAAA,EAEnE,MAAO,CACL,CACEjB,KAAMc,EACNO,UAAWC,EAAcvB,MACtBmB,GAGT,CAEA,MAAO,IAEX,CAEe,SAASK,EAAyBC,EAAU,IACzD,MAAMC,QAAEA,EAAU,YAAWC,WAAEA,EAAa,gCAAmCF,EAC/E,MAAO,CACLG,KAAM,8BACN,MAAAC,CAAOA,GAAQC,QAAEA,IACf,MAAMnC,EAAWM,EAAKC,QAAQ2B,EAAOE,MAAQC,QAAQC,MAAOP,GACtDQ,EAASzC,EAAeE,EAAUA,GAClCwC,EAAgB,kBAAkBC,KAAKC,UAAUH,EAAQ,KAAM,KAC/DI,EAAarC,EAAKC,QAAQ2B,EAAOE,MAAQC,QAAQC,MAAON,GAGxDY,EAAYtC,EAAKuC,QAAQF,GAO/B,OANK1C,EAAGwB,WAAWmB,IACjB3C,EAAG6C,UAAUF,EAAW,CAAEG,WAAW,IAGvC9C,EAAG+C,cAAcL,EAAYH,GAEtB,CACLS,MAAO,CACLC,cAAe,CACbC,MAAOjB,EAAOe,OAAOC,eAAeC,OAAS,eAIrD,EAEJ"}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "vite-plugin-generate-route",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "dist/index.cjs.js",
6
+ "module": "dist/index.esm.js",
7
+ "license": "MIT",
8
+ "author": "jianying_wj",
9
+ "scripts": {
10
+ "init": "pnpm install",
11
+ "build": "rollup -c",
12
+ "clean": "rm -rf dist node_modules pnpm-lock.yaml",
13
+ "release": "npm publish"
14
+ },
15
+ "keywords": [
16
+ "vite",
17
+ "plugin",
18
+ "generate",
19
+ "routes",
20
+ "vue-router"
21
+ ],
22
+ "files": [
23
+ "dist",
24
+ "README.md"
25
+ ],
26
+ "devDependencies": {
27
+ "@rollup/plugin-commonjs": "^29.0.2",
28
+ "@rollup/plugin-json": "^6.1.0",
29
+ "@rollup/plugin-node-resolve": "^16.0.3",
30
+ "@rollup/plugin-terser": "^1.0.0",
31
+ "rollup": "^4.60.0",
32
+ "vite": "^8.0.1"
33
+ },
34
+ "volta": {
35
+ "node": "20.19.0"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "registry": "https://registry.npmjs.org/"
40
+ }
41
+ }