@meng-xi/vite-plugin 0.1.3 → 0.1.5
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-en.md +49 -775
- package/README.md +46 -770
- package/dist/common/format/index.cjs +1 -1
- package/dist/common/format/index.d.cts +1 -130
- package/dist/common/format/index.d.mts +1 -130
- package/dist/common/format/index.d.ts +1 -130
- package/dist/common/format/index.mjs +1 -1
- package/dist/common/fs/index.cjs +1 -1
- package/dist/common/fs/index.d.cts +1 -89
- package/dist/common/fs/index.d.mts +1 -89
- package/dist/common/fs/index.d.ts +1 -89
- package/dist/common/fs/index.mjs +1 -1
- package/dist/common/html/index.cjs +2 -2
- package/dist/common/html/index.d.cts +19 -81
- package/dist/common/html/index.d.mts +19 -81
- package/dist/common/html/index.d.ts +19 -81
- package/dist/common/html/index.mjs +2 -2
- package/dist/common/index.cjs +1 -1
- package/dist/common/index.d.cts +7 -8
- package/dist/common/index.d.mts +7 -8
- package/dist/common/index.d.ts +7 -8
- package/dist/common/index.mjs +1 -1
- package/dist/common/script/index.cjs +1 -1
- package/dist/common/script/index.d.cts +1 -44
- package/dist/common/script/index.d.mts +1 -44
- package/dist/common/script/index.d.ts +1 -44
- package/dist/common/script/index.mjs +1 -1
- package/dist/common/ui/index.cjs +1 -0
- package/dist/common/ui/index.d.cts +22 -0
- package/dist/common/ui/index.d.mts +22 -0
- package/dist/common/ui/index.d.ts +22 -0
- package/dist/common/ui/index.mjs +1 -0
- package/dist/common/validation/index.cjs +1 -1
- package/dist/common/validation/index.d.cts +2 -79
- package/dist/common/validation/index.d.mts +2 -79
- package/dist/common/validation/index.d.ts +2 -79
- package/dist/common/validation/index.mjs +1 -1
- package/dist/factory/index.cjs +1 -1
- package/dist/factory/index.d.cts +1 -1
- package/dist/factory/index.d.mts +1 -1
- package/dist/factory/index.d.ts +1 -1
- package/dist/factory/index.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +12 -10
- package/dist/index.d.mts +12 -10
- package/dist/index.d.ts +12 -10
- package/dist/index.mjs +1 -1
- package/dist/logger/index.d.cts +92 -1
- package/dist/logger/index.d.mts +92 -1
- package/dist/logger/index.d.ts +92 -1
- package/dist/plugins/autoImport/index.cjs +17 -0
- package/dist/plugins/autoImport/index.d.cts +291 -0
- package/dist/plugins/autoImport/index.d.mts +291 -0
- package/dist/plugins/autoImport/index.d.ts +291 -0
- package/dist/plugins/autoImport/index.mjs +17 -0
- package/dist/plugins/buildProgress/index.cjs +2 -2
- package/dist/plugins/buildProgress/index.d.cts +1 -1
- package/dist/plugins/buildProgress/index.d.mts +1 -1
- package/dist/plugins/buildProgress/index.d.ts +1 -1
- package/dist/plugins/buildProgress/index.mjs +2 -2
- package/dist/plugins/bundleAnalyzer/index.cjs +11 -11
- package/dist/plugins/bundleAnalyzer/index.d.cts +1 -1
- package/dist/plugins/bundleAnalyzer/index.d.mts +1 -1
- package/dist/plugins/bundleAnalyzer/index.d.ts +1 -1
- package/dist/plugins/bundleAnalyzer/index.mjs +18 -18
- package/dist/plugins/compressAssets/index.cjs +1 -1
- package/dist/plugins/compressAssets/index.d.cts +1 -1
- package/dist/plugins/compressAssets/index.d.mts +1 -1
- package/dist/plugins/compressAssets/index.d.ts +1 -1
- package/dist/plugins/compressAssets/index.mjs +1 -1
- package/dist/plugins/copyFile/index.cjs +1 -1
- package/dist/plugins/copyFile/index.d.cts +21 -3
- package/dist/plugins/copyFile/index.d.mts +21 -3
- package/dist/plugins/copyFile/index.d.ts +21 -3
- package/dist/plugins/copyFile/index.mjs +1 -1
- package/dist/plugins/envGuard/index.cjs +67 -0
- package/dist/plugins/envGuard/index.d.cts +255 -0
- package/dist/plugins/envGuard/index.d.mts +255 -0
- package/dist/plugins/envGuard/index.d.ts +255 -0
- package/dist/plugins/envGuard/index.mjs +67 -0
- package/dist/plugins/faviconManager/index.cjs +1 -1
- package/dist/plugins/faviconManager/index.d.cts +44 -6
- package/dist/plugins/faviconManager/index.d.mts +44 -6
- package/dist/plugins/faviconManager/index.d.ts +44 -6
- package/dist/plugins/faviconManager/index.mjs +1 -1
- package/dist/plugins/generateRouter/index.cjs +4 -4
- package/dist/plugins/generateRouter/index.d.cts +62 -15
- package/dist/plugins/generateRouter/index.d.mts +62 -15
- package/dist/plugins/generateRouter/index.d.ts +62 -15
- package/dist/plugins/generateRouter/index.mjs +4 -4
- package/dist/plugins/generateVersion/index.cjs +1 -1
- package/dist/plugins/generateVersion/index.d.cts +13 -1
- package/dist/plugins/generateVersion/index.d.mts +13 -1
- package/dist/plugins/generateVersion/index.d.ts +13 -1
- package/dist/plugins/generateVersion/index.mjs +1 -1
- package/dist/plugins/htmlInject/index.cjs +7 -7
- package/dist/plugins/htmlInject/index.d.cts +50 -195
- package/dist/plugins/htmlInject/index.d.mts +50 -195
- package/dist/plugins/htmlInject/index.d.ts +50 -195
- package/dist/plugins/htmlInject/index.mjs +7 -7
- package/dist/plugins/index.cjs +1 -1
- package/dist/plugins/index.d.cts +5 -2
- package/dist/plugins/index.d.mts +5 -2
- package/dist/plugins/index.d.ts +5 -2
- package/dist/plugins/index.mjs +1 -1
- package/dist/plugins/loadingManager/index.cjs +26 -26
- package/dist/plugins/loadingManager/index.d.cts +1 -1
- package/dist/plugins/loadingManager/index.d.mts +1 -1
- package/dist/plugins/loadingManager/index.d.ts +1 -1
- package/dist/plugins/loadingManager/index.mjs +11 -11
- package/dist/plugins/versionUpdateChecker/index.cjs +1 -1
- package/dist/plugins/versionUpdateChecker/index.d.cts +1 -1
- package/dist/plugins/versionUpdateChecker/index.d.mts +1 -1
- package/dist/plugins/versionUpdateChecker/index.d.ts +1 -1
- package/dist/plugins/versionUpdateChecker/index.mjs +1 -1
- package/dist/shared/vite-plugin.B8FuZce1.d.cts +45 -0
- package/dist/shared/vite-plugin.B8FuZce1.d.mts +45 -0
- package/dist/shared/vite-plugin.B8FuZce1.d.ts +45 -0
- package/dist/shared/vite-plugin.BI9taN75.d.cts +122 -0
- package/dist/shared/vite-plugin.BI9taN75.d.mts +122 -0
- package/dist/shared/vite-plugin.BI9taN75.d.ts +122 -0
- package/dist/shared/vite-plugin.BPFqtmWa.mjs +1 -0
- package/dist/shared/vite-plugin.CnOy46d3.cjs +1 -0
- package/package.json +16 -16
- package/dist/common/compress/index.cjs +0 -1
- package/dist/common/compress/index.d.cts +0 -23
- package/dist/common/compress/index.d.mts +0 -23
- package/dist/common/compress/index.d.ts +0 -23
- package/dist/common/compress/index.mjs +0 -1
- package/dist/common/object/index.cjs +0 -1
- package/dist/common/object/index.d.cts +0 -30
- package/dist/common/object/index.d.mts +0 -30
- package/dist/common/object/index.d.ts +0 -30
- package/dist/common/object/index.mjs +0 -1
- package/dist/common/path/index.cjs +0 -1
- package/dist/common/path/index.d.cts +0 -22
- package/dist/common/path/index.d.mts +0 -22
- package/dist/common/path/index.d.ts +0 -22
- package/dist/common/path/index.mjs +0 -1
- package/dist/shared/vite-plugin.CLr0ttuO.d.cts +0 -135
- package/dist/shared/vite-plugin.CLr0ttuO.d.mts +0 -135
- package/dist/shared/vite-plugin.CLr0ttuO.d.ts +0 -135
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";const factory_index=require("../../factory/index.cjs"),common_fs_index=require("../../common/fs/index.cjs"),
|
|
1
|
+
"use strict";const factory_index=require("../../factory/index.cjs"),common_fs_index=require("../../common/fs/index.cjs"),m=require("path"),n=require("fs");require("../../logger/index.cjs"),require("../../shared/vite-plugin.Bcg6RW2N.cjs");function u(i,e=/[/-]/){return i.replace(/^\/+/,"").split(e).filter(Boolean).map((t,s)=>s===0?t.toLowerCase():t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join("")}function R(i,e=/[/-]/){return i.replace(/^\/+/,"").split(e).filter(Boolean).map(t=>t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()).join("")}function P(i){return i.replace(/\/\/.*$/gm,"").replace(/\/\*[\s\S]*?\*\//g,"")}class d extends factory_index.BasePlugin{projectRoot=process.cwd();tabBarPages=new Set;watcher=null;getDefaultOptions(){return{pagesJsonPath:"src/pages.json",outputPath:"src/router.config.ts",outputFormat:"ts",nameStrategy:"camelCase",includeSubPackages:!0,watch:!0,exportTypes:!0,preserveRouteChanges:!0,metaMapping:{navigationBarTitleText:"title",requireAuth:"requireAuth"}}}validateOptions(){if(this.validator.field("pagesJsonPath").string().field("outputPath").string().field("outputFormat").enum(["ts","js"]).field("nameStrategy").enum(["path","camelCase","pascalCase","custom"]).validate(),this.options.nameStrategy==="custom"&&!this.options.customNameGenerator)throw new Error("\u5F53 nameStrategy \u4E3A custom \u65F6\uFF0C\u5FC5\u987B\u63D0\u4F9B customNameGenerator")}getPluginName(){return"generate-router"}generateRouteName(e){switch(this.options.nameStrategy){case"path":return e.replace(/\//g,"_").replace(/^_/,"");case"camelCase":return u(e);case"pascalCase":return R(e);case"custom":return this.options.customNameGenerator(e);default:return u(e)}}extractMeta(e,t){const s={},a=e.style||{},r=this.options.metaMapping||{};for(const[o,c]of Object.entries(r))a[o]!==void 0&&(s[c]=a[o]);return this.tabBarPages.has(t)&&(s.isTab=!0),s}parsePageToRoute(e,t=""){const s=t?`/${t}/${e.path}`:`/${e.path}`,a=this.generateRouteName(s),r=this.extractMeta(e,s.replace(/^\//,"")),o={path:s,name:a};return Object.keys(r).length>0&&(o.meta=r),o}parsePagesJson(e){const t=[];if(!e.pages||!Array.isArray(e.pages)||e.pages.length===0)return this.logger.warn("pages.json \u4E2D\u6CA1\u6709\u6709\u6548\u7684\u9875\u9762\u914D\u7F6E"),t;if(this.tabBarPages.clear(),e.tabBar?.list)for(const s of e.tabBar.list)this.tabBarPages.add(s.pagePath);for(const s of e.pages)t.push(this.parsePageToRoute(s));if(this.options.includeSubPackages&&e.subPackages){for(const s of e.subPackages)if(s.pages&&Array.isArray(s.pages))for(const a of s.pages)t.push(this.parsePageToRoute(a,s.root))}return t}generateTypeDefinitions(){return!this.options.exportTypes||this.options.outputFormat==="js"?"":`
|
|
2
2
|
/**
|
|
3
3
|
* \u8DEF\u7531\u5143\u4FE1\u606F
|
|
4
4
|
*/
|
|
@@ -24,12 +24,12 @@ export interface RouteConfig {
|
|
|
24
24
|
/** \u8DEF\u7531\u5143\u4FE1\u606F */
|
|
25
25
|
meta?: RouteMeta
|
|
26
26
|
}
|
|
27
|
-
`}generateFileContent(e){const
|
|
27
|
+
`}generateFileContent(e){const t=this.generateTypeDefinitions(),s=this.options.outputFormat==="ts",a=JSON.stringify(e,null," ").replace(/"(\w+)":/g,"$1:").replace(/: "([^"]+)"/g,": '$1'");return`${t}
|
|
28
28
|
/**
|
|
29
29
|
* \u8DEF\u7531\u914D\u7F6E\u5217\u8868
|
|
30
30
|
* @description \u7531 pages.json \u81EA\u52A8\u751F\u6210
|
|
31
31
|
*/
|
|
32
|
-
export const routes${
|
|
32
|
+
export const routes${s?": RouteConfig[]":""} = ${a}
|
|
33
33
|
|
|
34
34
|
export default routes
|
|
35
|
-
`}async readPagesJson(){const e=
|
|
35
|
+
`}async readPagesJson(){const e=m.resolve(this.projectRoot,this.options.pagesJsonPath);if(!n.existsSync(e))return this.logger.warn(`pages.json \u6587\u4EF6\u4E0D\u5B58\u5728: ${e}`),null;try{const t=await n.promises.readFile(e,"utf-8"),s=P(t);return JSON.parse(s)}catch(t){return this.logger.error(`\u89E3\u6790 pages.json \u5931\u8D25: ${t.message}`),null}}extractExistingRoutes(e){const t=new Map,s=e.match(/export const routes[^=]*=\s*(\[[\s\S]*?\](?=\s*\n|\s*$|\s*\/\/))/);if(!s)return t;try{let a=s[1].replace(/(\w+)(?=\s*:)/g,'"$1"').replace(/'([^']*)'/g,'"$1"').replace(/,\s*([\]\}])/g,"$1");const r=JSON.parse(a);for(const o of r)o.path&&t.set(o.path,o)}catch{this.logger.warn("\u89E3\u6790\u73B0\u6709 routes \u914D\u7F6E\u5931\u8D25\uFF0C\u5C06\u5B8C\u5168\u91CD\u65B0\u751F\u6210")}return t}mergeRoutes(e,t){return e.map(s=>{const a=t.get(s.path);if(!a)return s;const r={};return s.meta&&Object.assign(r,s.meta),a.meta&&Object.assign(r,a.meta),{...a,path:s.path,meta:Object.keys(r).length>0?r:void 0}})}async generateRouterConfig(){const e=await this.readPagesJson();if(!e)return;let t=this.parsePagesJson(e);const s=m.resolve(this.projectRoot,this.options.outputPath);if(this.options.preserveRouteChanges&&n.existsSync(s))try{const r=await n.promises.readFile(s,"utf-8"),o=this.extractExistingRoutes(r);o.size>0&&(t=this.mergeRoutes(t,o),this.logger.info("\u5DF2\u5408\u5E76\u7528\u6237\u5BF9\u8DEF\u7531\u914D\u7F6E\u7684\u4FEE\u6539"))}catch{}const a=this.generateFileContent(t);await common_fs_index.writeFileContent(s,a),this.logger.success(`\u8DEF\u7531\u914D\u7F6E\u6587\u4EF6\u5DF2\u751F\u6210: ${s}`),this.logger.info(`\u5171\u751F\u6210 ${t.length} \u6761\u8DEF\u7531\u914D\u7F6E`)}startWatching(){if(!this.options.watch)return;const e=m.resolve(this.projectRoot,this.options.pagesJsonPath);n.existsSync(e)&&(this.watcher=n.watch(e,async t=>{t==="change"&&(this.logger.info("\u68C0\u6D4B\u5230 pages.json \u53D8\u5316\uFF0C\u91CD\u65B0\u751F\u6210\u8DEF\u7531\u914D\u7F6E..."),await this.safeExecute(()=>this.generateRouterConfig(),"\u91CD\u65B0\u751F\u6210\u8DEF\u7531\u914D\u7F6E"))}),this.logger.info(`\u6B63\u5728\u76D1\u542C pages.json \u53D8\u5316: ${e}`))}stopWatching(){this.watcher&&(this.watcher.close(),this.watcher=null)}addPluginHooks(e){e.configResolved=async t=>{this.projectRoot=t.root,await this.safeExecute(()=>this.generateRouterConfig(),"\u751F\u6210\u8DEF\u7531\u914D\u7F6E"),t.command==="serve"&&this.startWatching()}}destroy(){super.destroy(),this.stopWatching()}}const generateRouter=factory_index.createPluginFactory(d);exports.generateRouter=generateRouter;
|
|
@@ -1,41 +1,51 @@
|
|
|
1
1
|
import { BasePluginOptions, PluginFactory } from '../../factory/index.cjs';
|
|
2
2
|
import 'vite';
|
|
3
|
-
import '../../shared/vite-plugin.
|
|
3
|
+
import '../../shared/vite-plugin.B8FuZce1.cjs';
|
|
4
4
|
import '../../shared/vite-plugin.DRRlWY8P.cjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* 路由元信息
|
|
8
|
+
*
|
|
9
|
+
* @interface RouteMeta
|
|
10
|
+
* @description 路由附加的元数据,用于页面导航守卫、标题设置等场景。
|
|
11
|
+
* 支持通过索引签名扩展自定义字段。
|
|
8
12
|
*/
|
|
9
13
|
interface RouteMeta {
|
|
10
|
-
/**
|
|
14
|
+
/** 页面标题,对应 pages.json 中的 navigationBarTitleText */
|
|
11
15
|
title?: string;
|
|
12
|
-
/** 是否为TabBar
|
|
16
|
+
/** 是否为 TabBar 页面,由插件自动从 tabBar 配置推断 */
|
|
13
17
|
isTab?: boolean;
|
|
14
|
-
/**
|
|
18
|
+
/** 是否需要登录才能访问 */
|
|
15
19
|
requireAuth?: boolean;
|
|
16
20
|
/** 自定义扩展字段 */
|
|
17
21
|
[key: string]: unknown;
|
|
18
22
|
}
|
|
19
23
|
/**
|
|
20
24
|
* 路由配置项
|
|
25
|
+
*
|
|
26
|
+
* @interface RouteConfig
|
|
27
|
+
* @description 单条路由的完整配置,包含路径、名称和元信息。
|
|
21
28
|
*/
|
|
22
29
|
interface RouteConfig {
|
|
23
|
-
/**
|
|
30
|
+
/** 路由路径,以 `/` 开头 */
|
|
24
31
|
path: string;
|
|
25
|
-
/**
|
|
32
|
+
/** 路由名称(用于命名路由导航),根据 nameStrategy 自动生成 */
|
|
26
33
|
name?: string;
|
|
27
34
|
/** 路由元信息 */
|
|
28
35
|
meta?: RouteMeta;
|
|
29
36
|
}
|
|
30
37
|
/**
|
|
31
38
|
* uni-app pages.json 中的页面配置项
|
|
39
|
+
*
|
|
40
|
+
* @interface UniAppPageConfig
|
|
41
|
+
* @description 对应 pages.json 中 `pages` 数组里的单个页面对象。
|
|
32
42
|
*/
|
|
33
43
|
interface UniAppPageConfig {
|
|
34
|
-
/**
|
|
44
|
+
/** 页面路径,相对于 pages.json 所在目录 */
|
|
35
45
|
path: string;
|
|
36
|
-
/**
|
|
46
|
+
/** 页面样式配置 */
|
|
37
47
|
style?: {
|
|
38
|
-
/**
|
|
48
|
+
/** 导航栏标题文本 */
|
|
39
49
|
navigationBarTitleText?: string;
|
|
40
50
|
/** 是否需要登录 */
|
|
41
51
|
requireAuth?: boolean;
|
|
@@ -47,15 +57,18 @@ interface UniAppPageConfig {
|
|
|
47
57
|
}
|
|
48
58
|
/**
|
|
49
59
|
* uni-app pages.json 中的 tabBar 配置
|
|
60
|
+
*
|
|
61
|
+
* @interface UniAppTabBarConfig
|
|
62
|
+
* @description 对应 pages.json 中的 `tabBar` 字段,定义底部标签栏的页面列表和图标。
|
|
50
63
|
*/
|
|
51
64
|
interface UniAppTabBarConfig {
|
|
52
|
-
/** tabBar
|
|
65
|
+
/** tabBar 页面列表 */
|
|
53
66
|
list?: Array<{
|
|
54
67
|
/** 页面路径 */
|
|
55
68
|
pagePath: string;
|
|
56
|
-
/**
|
|
69
|
+
/** 标签文字 */
|
|
57
70
|
text?: string;
|
|
58
|
-
/**
|
|
71
|
+
/** 未选中图标路径 */
|
|
59
72
|
iconPath?: string;
|
|
60
73
|
/** 选中图标路径 */
|
|
61
74
|
selectedIconPath?: string;
|
|
@@ -65,32 +78,66 @@ interface UniAppTabBarConfig {
|
|
|
65
78
|
}
|
|
66
79
|
/**
|
|
67
80
|
* uni-app pages.json 结构
|
|
81
|
+
*
|
|
82
|
+
* @interface UniAppPagesJson
|
|
83
|
+
* @description 完整的 pages.json 文件结构,包含主包页面、子包、tabBar 和全局样式。
|
|
68
84
|
*/
|
|
69
85
|
interface UniAppPagesJson {
|
|
70
|
-
/**
|
|
86
|
+
/** 主包页面列表 */
|
|
71
87
|
pages: UniAppPageConfig[];
|
|
72
|
-
/**
|
|
88
|
+
/** 子包(分包)配置列表 */
|
|
73
89
|
subPackages?: Array<{
|
|
90
|
+
/** 子包根路径 */
|
|
74
91
|
root: string;
|
|
92
|
+
/** 子包页面列表 */
|
|
75
93
|
pages: UniAppPageConfig[];
|
|
76
94
|
}>;
|
|
77
95
|
/** tabBar 配置 */
|
|
78
96
|
tabBar?: UniAppTabBarConfig;
|
|
79
|
-
/**
|
|
97
|
+
/** 全局样式配置 */
|
|
80
98
|
globalStyle?: Record<string, unknown>;
|
|
81
99
|
/** 其他属性 */
|
|
82
100
|
[key: string]: unknown;
|
|
83
101
|
}
|
|
84
102
|
/**
|
|
85
103
|
* 输出文件格式类型
|
|
104
|
+
*
|
|
105
|
+
* @description 支持的输出文件格式:
|
|
106
|
+
* - `ts`: TypeScript 文件
|
|
107
|
+
* - `js`: JavaScript 文件
|
|
86
108
|
*/
|
|
87
109
|
type OutputFormat = 'ts' | 'js';
|
|
88
110
|
/**
|
|
89
111
|
* 路由名称生成策略
|
|
112
|
+
*
|
|
113
|
+
* @description 定义路由名称的生成方式:
|
|
114
|
+
* - `path`: 使用原始路径(斜杠替换为下划线)
|
|
115
|
+
* - `camelCase`: 驼峰命名(默认)
|
|
116
|
+
* - `pascalCase`: 帕斯卡命名
|
|
117
|
+
* - `custom`: 自定义生成函数
|
|
90
118
|
*/
|
|
91
119
|
type NameStrategy = 'path' | 'camelCase' | 'pascalCase' | 'custom';
|
|
92
120
|
/**
|
|
93
121
|
* 生成路由配置插件选项
|
|
122
|
+
*
|
|
123
|
+
* @interface GenerateRouterOptions
|
|
124
|
+
* @extends {BasePluginOptions}
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* generateRouter({
|
|
129
|
+
* pagesJsonPath: 'src/pages.json',
|
|
130
|
+
* outputPath: 'src/router.config.ts',
|
|
131
|
+
* outputFormat: 'ts',
|
|
132
|
+
* nameStrategy: 'camelCase',
|
|
133
|
+
* includeSubPackages: true,
|
|
134
|
+
* watch: true,
|
|
135
|
+
* metaMapping: {
|
|
136
|
+
* navigationBarTitleText: 'title',
|
|
137
|
+
* requireAuth: 'requireAuth'
|
|
138
|
+
* }
|
|
139
|
+
* })
|
|
140
|
+
* ```
|
|
94
141
|
*/
|
|
95
142
|
interface GenerateRouterOptions extends BasePluginOptions {
|
|
96
143
|
/**
|
|
@@ -1,41 +1,51 @@
|
|
|
1
1
|
import { BasePluginOptions, PluginFactory } from '../../factory/index.mjs';
|
|
2
2
|
import 'vite';
|
|
3
|
-
import '../../shared/vite-plugin.
|
|
3
|
+
import '../../shared/vite-plugin.B8FuZce1.mjs';
|
|
4
4
|
import '../../shared/vite-plugin.DRRlWY8P.mjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* 路由元信息
|
|
8
|
+
*
|
|
9
|
+
* @interface RouteMeta
|
|
10
|
+
* @description 路由附加的元数据,用于页面导航守卫、标题设置等场景。
|
|
11
|
+
* 支持通过索引签名扩展自定义字段。
|
|
8
12
|
*/
|
|
9
13
|
interface RouteMeta {
|
|
10
|
-
/**
|
|
14
|
+
/** 页面标题,对应 pages.json 中的 navigationBarTitleText */
|
|
11
15
|
title?: string;
|
|
12
|
-
/** 是否为TabBar
|
|
16
|
+
/** 是否为 TabBar 页面,由插件自动从 tabBar 配置推断 */
|
|
13
17
|
isTab?: boolean;
|
|
14
|
-
/**
|
|
18
|
+
/** 是否需要登录才能访问 */
|
|
15
19
|
requireAuth?: boolean;
|
|
16
20
|
/** 自定义扩展字段 */
|
|
17
21
|
[key: string]: unknown;
|
|
18
22
|
}
|
|
19
23
|
/**
|
|
20
24
|
* 路由配置项
|
|
25
|
+
*
|
|
26
|
+
* @interface RouteConfig
|
|
27
|
+
* @description 单条路由的完整配置,包含路径、名称和元信息。
|
|
21
28
|
*/
|
|
22
29
|
interface RouteConfig {
|
|
23
|
-
/**
|
|
30
|
+
/** 路由路径,以 `/` 开头 */
|
|
24
31
|
path: string;
|
|
25
|
-
/**
|
|
32
|
+
/** 路由名称(用于命名路由导航),根据 nameStrategy 自动生成 */
|
|
26
33
|
name?: string;
|
|
27
34
|
/** 路由元信息 */
|
|
28
35
|
meta?: RouteMeta;
|
|
29
36
|
}
|
|
30
37
|
/**
|
|
31
38
|
* uni-app pages.json 中的页面配置项
|
|
39
|
+
*
|
|
40
|
+
* @interface UniAppPageConfig
|
|
41
|
+
* @description 对应 pages.json 中 `pages` 数组里的单个页面对象。
|
|
32
42
|
*/
|
|
33
43
|
interface UniAppPageConfig {
|
|
34
|
-
/**
|
|
44
|
+
/** 页面路径,相对于 pages.json 所在目录 */
|
|
35
45
|
path: string;
|
|
36
|
-
/**
|
|
46
|
+
/** 页面样式配置 */
|
|
37
47
|
style?: {
|
|
38
|
-
/**
|
|
48
|
+
/** 导航栏标题文本 */
|
|
39
49
|
navigationBarTitleText?: string;
|
|
40
50
|
/** 是否需要登录 */
|
|
41
51
|
requireAuth?: boolean;
|
|
@@ -47,15 +57,18 @@ interface UniAppPageConfig {
|
|
|
47
57
|
}
|
|
48
58
|
/**
|
|
49
59
|
* uni-app pages.json 中的 tabBar 配置
|
|
60
|
+
*
|
|
61
|
+
* @interface UniAppTabBarConfig
|
|
62
|
+
* @description 对应 pages.json 中的 `tabBar` 字段,定义底部标签栏的页面列表和图标。
|
|
50
63
|
*/
|
|
51
64
|
interface UniAppTabBarConfig {
|
|
52
|
-
/** tabBar
|
|
65
|
+
/** tabBar 页面列表 */
|
|
53
66
|
list?: Array<{
|
|
54
67
|
/** 页面路径 */
|
|
55
68
|
pagePath: string;
|
|
56
|
-
/**
|
|
69
|
+
/** 标签文字 */
|
|
57
70
|
text?: string;
|
|
58
|
-
/**
|
|
71
|
+
/** 未选中图标路径 */
|
|
59
72
|
iconPath?: string;
|
|
60
73
|
/** 选中图标路径 */
|
|
61
74
|
selectedIconPath?: string;
|
|
@@ -65,32 +78,66 @@ interface UniAppTabBarConfig {
|
|
|
65
78
|
}
|
|
66
79
|
/**
|
|
67
80
|
* uni-app pages.json 结构
|
|
81
|
+
*
|
|
82
|
+
* @interface UniAppPagesJson
|
|
83
|
+
* @description 完整的 pages.json 文件结构,包含主包页面、子包、tabBar 和全局样式。
|
|
68
84
|
*/
|
|
69
85
|
interface UniAppPagesJson {
|
|
70
|
-
/**
|
|
86
|
+
/** 主包页面列表 */
|
|
71
87
|
pages: UniAppPageConfig[];
|
|
72
|
-
/**
|
|
88
|
+
/** 子包(分包)配置列表 */
|
|
73
89
|
subPackages?: Array<{
|
|
90
|
+
/** 子包根路径 */
|
|
74
91
|
root: string;
|
|
92
|
+
/** 子包页面列表 */
|
|
75
93
|
pages: UniAppPageConfig[];
|
|
76
94
|
}>;
|
|
77
95
|
/** tabBar 配置 */
|
|
78
96
|
tabBar?: UniAppTabBarConfig;
|
|
79
|
-
/**
|
|
97
|
+
/** 全局样式配置 */
|
|
80
98
|
globalStyle?: Record<string, unknown>;
|
|
81
99
|
/** 其他属性 */
|
|
82
100
|
[key: string]: unknown;
|
|
83
101
|
}
|
|
84
102
|
/**
|
|
85
103
|
* 输出文件格式类型
|
|
104
|
+
*
|
|
105
|
+
* @description 支持的输出文件格式:
|
|
106
|
+
* - `ts`: TypeScript 文件
|
|
107
|
+
* - `js`: JavaScript 文件
|
|
86
108
|
*/
|
|
87
109
|
type OutputFormat = 'ts' | 'js';
|
|
88
110
|
/**
|
|
89
111
|
* 路由名称生成策略
|
|
112
|
+
*
|
|
113
|
+
* @description 定义路由名称的生成方式:
|
|
114
|
+
* - `path`: 使用原始路径(斜杠替换为下划线)
|
|
115
|
+
* - `camelCase`: 驼峰命名(默认)
|
|
116
|
+
* - `pascalCase`: 帕斯卡命名
|
|
117
|
+
* - `custom`: 自定义生成函数
|
|
90
118
|
*/
|
|
91
119
|
type NameStrategy = 'path' | 'camelCase' | 'pascalCase' | 'custom';
|
|
92
120
|
/**
|
|
93
121
|
* 生成路由配置插件选项
|
|
122
|
+
*
|
|
123
|
+
* @interface GenerateRouterOptions
|
|
124
|
+
* @extends {BasePluginOptions}
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* generateRouter({
|
|
129
|
+
* pagesJsonPath: 'src/pages.json',
|
|
130
|
+
* outputPath: 'src/router.config.ts',
|
|
131
|
+
* outputFormat: 'ts',
|
|
132
|
+
* nameStrategy: 'camelCase',
|
|
133
|
+
* includeSubPackages: true,
|
|
134
|
+
* watch: true,
|
|
135
|
+
* metaMapping: {
|
|
136
|
+
* navigationBarTitleText: 'title',
|
|
137
|
+
* requireAuth: 'requireAuth'
|
|
138
|
+
* }
|
|
139
|
+
* })
|
|
140
|
+
* ```
|
|
94
141
|
*/
|
|
95
142
|
interface GenerateRouterOptions extends BasePluginOptions {
|
|
96
143
|
/**
|
|
@@ -1,41 +1,51 @@
|
|
|
1
1
|
import { BasePluginOptions, PluginFactory } from '../../factory/index.js';
|
|
2
2
|
import 'vite';
|
|
3
|
-
import '../../shared/vite-plugin.
|
|
3
|
+
import '../../shared/vite-plugin.B8FuZce1.js';
|
|
4
4
|
import '../../shared/vite-plugin.DRRlWY8P.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* 路由元信息
|
|
8
|
+
*
|
|
9
|
+
* @interface RouteMeta
|
|
10
|
+
* @description 路由附加的元数据,用于页面导航守卫、标题设置等场景。
|
|
11
|
+
* 支持通过索引签名扩展自定义字段。
|
|
8
12
|
*/
|
|
9
13
|
interface RouteMeta {
|
|
10
|
-
/**
|
|
14
|
+
/** 页面标题,对应 pages.json 中的 navigationBarTitleText */
|
|
11
15
|
title?: string;
|
|
12
|
-
/** 是否为TabBar
|
|
16
|
+
/** 是否为 TabBar 页面,由插件自动从 tabBar 配置推断 */
|
|
13
17
|
isTab?: boolean;
|
|
14
|
-
/**
|
|
18
|
+
/** 是否需要登录才能访问 */
|
|
15
19
|
requireAuth?: boolean;
|
|
16
20
|
/** 自定义扩展字段 */
|
|
17
21
|
[key: string]: unknown;
|
|
18
22
|
}
|
|
19
23
|
/**
|
|
20
24
|
* 路由配置项
|
|
25
|
+
*
|
|
26
|
+
* @interface RouteConfig
|
|
27
|
+
* @description 单条路由的完整配置,包含路径、名称和元信息。
|
|
21
28
|
*/
|
|
22
29
|
interface RouteConfig {
|
|
23
|
-
/**
|
|
30
|
+
/** 路由路径,以 `/` 开头 */
|
|
24
31
|
path: string;
|
|
25
|
-
/**
|
|
32
|
+
/** 路由名称(用于命名路由导航),根据 nameStrategy 自动生成 */
|
|
26
33
|
name?: string;
|
|
27
34
|
/** 路由元信息 */
|
|
28
35
|
meta?: RouteMeta;
|
|
29
36
|
}
|
|
30
37
|
/**
|
|
31
38
|
* uni-app pages.json 中的页面配置项
|
|
39
|
+
*
|
|
40
|
+
* @interface UniAppPageConfig
|
|
41
|
+
* @description 对应 pages.json 中 `pages` 数组里的单个页面对象。
|
|
32
42
|
*/
|
|
33
43
|
interface UniAppPageConfig {
|
|
34
|
-
/**
|
|
44
|
+
/** 页面路径,相对于 pages.json 所在目录 */
|
|
35
45
|
path: string;
|
|
36
|
-
/**
|
|
46
|
+
/** 页面样式配置 */
|
|
37
47
|
style?: {
|
|
38
|
-
/**
|
|
48
|
+
/** 导航栏标题文本 */
|
|
39
49
|
navigationBarTitleText?: string;
|
|
40
50
|
/** 是否需要登录 */
|
|
41
51
|
requireAuth?: boolean;
|
|
@@ -47,15 +57,18 @@ interface UniAppPageConfig {
|
|
|
47
57
|
}
|
|
48
58
|
/**
|
|
49
59
|
* uni-app pages.json 中的 tabBar 配置
|
|
60
|
+
*
|
|
61
|
+
* @interface UniAppTabBarConfig
|
|
62
|
+
* @description 对应 pages.json 中的 `tabBar` 字段,定义底部标签栏的页面列表和图标。
|
|
50
63
|
*/
|
|
51
64
|
interface UniAppTabBarConfig {
|
|
52
|
-
/** tabBar
|
|
65
|
+
/** tabBar 页面列表 */
|
|
53
66
|
list?: Array<{
|
|
54
67
|
/** 页面路径 */
|
|
55
68
|
pagePath: string;
|
|
56
|
-
/**
|
|
69
|
+
/** 标签文字 */
|
|
57
70
|
text?: string;
|
|
58
|
-
/**
|
|
71
|
+
/** 未选中图标路径 */
|
|
59
72
|
iconPath?: string;
|
|
60
73
|
/** 选中图标路径 */
|
|
61
74
|
selectedIconPath?: string;
|
|
@@ -65,32 +78,66 @@ interface UniAppTabBarConfig {
|
|
|
65
78
|
}
|
|
66
79
|
/**
|
|
67
80
|
* uni-app pages.json 结构
|
|
81
|
+
*
|
|
82
|
+
* @interface UniAppPagesJson
|
|
83
|
+
* @description 完整的 pages.json 文件结构,包含主包页面、子包、tabBar 和全局样式。
|
|
68
84
|
*/
|
|
69
85
|
interface UniAppPagesJson {
|
|
70
|
-
/**
|
|
86
|
+
/** 主包页面列表 */
|
|
71
87
|
pages: UniAppPageConfig[];
|
|
72
|
-
/**
|
|
88
|
+
/** 子包(分包)配置列表 */
|
|
73
89
|
subPackages?: Array<{
|
|
90
|
+
/** 子包根路径 */
|
|
74
91
|
root: string;
|
|
92
|
+
/** 子包页面列表 */
|
|
75
93
|
pages: UniAppPageConfig[];
|
|
76
94
|
}>;
|
|
77
95
|
/** tabBar 配置 */
|
|
78
96
|
tabBar?: UniAppTabBarConfig;
|
|
79
|
-
/**
|
|
97
|
+
/** 全局样式配置 */
|
|
80
98
|
globalStyle?: Record<string, unknown>;
|
|
81
99
|
/** 其他属性 */
|
|
82
100
|
[key: string]: unknown;
|
|
83
101
|
}
|
|
84
102
|
/**
|
|
85
103
|
* 输出文件格式类型
|
|
104
|
+
*
|
|
105
|
+
* @description 支持的输出文件格式:
|
|
106
|
+
* - `ts`: TypeScript 文件
|
|
107
|
+
* - `js`: JavaScript 文件
|
|
86
108
|
*/
|
|
87
109
|
type OutputFormat = 'ts' | 'js';
|
|
88
110
|
/**
|
|
89
111
|
* 路由名称生成策略
|
|
112
|
+
*
|
|
113
|
+
* @description 定义路由名称的生成方式:
|
|
114
|
+
* - `path`: 使用原始路径(斜杠替换为下划线)
|
|
115
|
+
* - `camelCase`: 驼峰命名(默认)
|
|
116
|
+
* - `pascalCase`: 帕斯卡命名
|
|
117
|
+
* - `custom`: 自定义生成函数
|
|
90
118
|
*/
|
|
91
119
|
type NameStrategy = 'path' | 'camelCase' | 'pascalCase' | 'custom';
|
|
92
120
|
/**
|
|
93
121
|
* 生成路由配置插件选项
|
|
122
|
+
*
|
|
123
|
+
* @interface GenerateRouterOptions
|
|
124
|
+
* @extends {BasePluginOptions}
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* generateRouter({
|
|
129
|
+
* pagesJsonPath: 'src/pages.json',
|
|
130
|
+
* outputPath: 'src/router.config.ts',
|
|
131
|
+
* outputFormat: 'ts',
|
|
132
|
+
* nameStrategy: 'camelCase',
|
|
133
|
+
* includeSubPackages: true,
|
|
134
|
+
* watch: true,
|
|
135
|
+
* metaMapping: {
|
|
136
|
+
* navigationBarTitleText: 'title',
|
|
137
|
+
* requireAuth: 'requireAuth'
|
|
138
|
+
* }
|
|
139
|
+
* })
|
|
140
|
+
* ```
|
|
94
141
|
*/
|
|
95
142
|
interface GenerateRouterOptions extends BasePluginOptions {
|
|
96
143
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{createPluginFactory as
|
|
1
|
+
import{createPluginFactory as h,BasePlugin as l}from"../../factory/index.mjs";import{writeFileContent as F}from"../../common/fs/index.mjs";import{resolve as n}from"path";import{existsSync as i,promises as c,watch as f}from"fs";import"../../logger/index.mjs";import"../../shared/vite-plugin.DcExl6jd.mjs";function p(o,t=/[/-]/){return o.replace(/^\/+/,"").split(t).filter(Boolean).map((e,u)=>u===0?e.toLowerCase():e.charAt(0).toUpperCase()+e.slice(1).toLowerCase()).join("")}function m(o,t=/[/-]/){return o.replace(/^\/+/,"").split(t).filter(Boolean).map(e=>e.charAt(0).toUpperCase()+e.slice(1).toLowerCase()).join("")}function E(o){return o.replace(/\/\/.*$/gm,"").replace(/\/\*[\s\S]*?\*\//g,"")}class D extends l{projectRoot=process.cwd();tabBarPages=new Set;watcher=null;getDefaultOptions(){return{pagesJsonPath:"src/pages.json",outputPath:"src/router.config.ts",outputFormat:"ts",nameStrategy:"camelCase",includeSubPackages:!0,watch:!0,exportTypes:!0,preserveRouteChanges:!0,metaMapping:{navigationBarTitleText:"title",requireAuth:"requireAuth"}}}validateOptions(){if(this.validator.field("pagesJsonPath").string().field("outputPath").string().field("outputFormat").enum(["ts","js"]).field("nameStrategy").enum(["path","camelCase","pascalCase","custom"]).validate(),this.options.nameStrategy==="custom"&&!this.options.customNameGenerator)throw new Error("\u5F53 nameStrategy \u4E3A custom \u65F6\uFF0C\u5FC5\u987B\u63D0\u4F9B customNameGenerator")}getPluginName(){return"generate-router"}generateRouteName(t){switch(this.options.nameStrategy){case"path":return t.replace(/\//g,"_").replace(/^_/,"");case"camelCase":return p(t);case"pascalCase":return m(t);case"custom":return this.options.customNameGenerator(t);default:return p(t)}}extractMeta(t,e){const u={},s=t.style||{},a=this.options.metaMapping||{};for(const[r,g]of Object.entries(a))s[r]!==void 0&&(u[g]=s[r]);return this.tabBarPages.has(e)&&(u.isTab=!0),u}parsePageToRoute(t,e=""){const u=e?`/${e}/${t.path}`:`/${t.path}`,s=this.generateRouteName(u),a=this.extractMeta(t,u.replace(/^\//,"")),r={path:u,name:s};return Object.keys(a).length>0&&(r.meta=a),r}parsePagesJson(t){const e=[];if(!t.pages||!Array.isArray(t.pages)||t.pages.length===0)return this.logger.warn("pages.json \u4E2D\u6CA1\u6709\u6709\u6548\u7684\u9875\u9762\u914D\u7F6E"),e;if(this.tabBarPages.clear(),t.tabBar?.list)for(const u of t.tabBar.list)this.tabBarPages.add(u.pagePath);for(const u of t.pages)e.push(this.parsePageToRoute(u));if(this.options.includeSubPackages&&t.subPackages){for(const u of t.subPackages)if(u.pages&&Array.isArray(u.pages))for(const s of u.pages)e.push(this.parsePageToRoute(s,u.root))}return e}generateTypeDefinitions(){return!this.options.exportTypes||this.options.outputFormat==="js"?"":`
|
|
2
2
|
/**
|
|
3
3
|
* \u8DEF\u7531\u5143\u4FE1\u606F
|
|
4
4
|
*/
|
|
@@ -24,12 +24,12 @@ export interface RouteConfig {
|
|
|
24
24
|
/** \u8DEF\u7531\u5143\u4FE1\u606F */
|
|
25
25
|
meta?: RouteMeta
|
|
26
26
|
}
|
|
27
|
-
`}generateFileContent(t){const
|
|
27
|
+
`}generateFileContent(t){const e=this.generateTypeDefinitions(),u=this.options.outputFormat==="ts",s=JSON.stringify(t,null," ").replace(/"(\w+)":/g,"$1:").replace(/: "([^"]+)"/g,": '$1'");return`${e}
|
|
28
28
|
/**
|
|
29
29
|
* \u8DEF\u7531\u914D\u7F6E\u5217\u8868
|
|
30
30
|
* @description \u7531 pages.json \u81EA\u52A8\u751F\u6210
|
|
31
31
|
*/
|
|
32
|
-
export const routes${
|
|
32
|
+
export const routes${u?": RouteConfig[]":""} = ${s}
|
|
33
33
|
|
|
34
34
|
export default routes
|
|
35
|
-
`}async readPagesJson(){const t=
|
|
35
|
+
`}async readPagesJson(){const t=n(this.projectRoot,this.options.pagesJsonPath);if(!i(t))return this.logger.warn(`pages.json \u6587\u4EF6\u4E0D\u5B58\u5728: ${t}`),null;try{const e=await c.readFile(t,"utf-8"),u=E(e);return JSON.parse(u)}catch(e){return this.logger.error(`\u89E3\u6790 pages.json \u5931\u8D25: ${e.message}`),null}}extractExistingRoutes(t){const e=new Map,u=t.match(/export const routes[^=]*=\s*(\[[\s\S]*?\](?=\s*\n|\s*$|\s*\/\/))/);if(!u)return e;try{let s=u[1].replace(/(\w+)(?=\s*:)/g,'"$1"').replace(/'([^']*)'/g,'"$1"').replace(/,\s*([\]\}])/g,"$1");const a=JSON.parse(s);for(const r of a)r.path&&e.set(r.path,r)}catch{this.logger.warn("\u89E3\u6790\u73B0\u6709 routes \u914D\u7F6E\u5931\u8D25\uFF0C\u5C06\u5B8C\u5168\u91CD\u65B0\u751F\u6210")}return e}mergeRoutes(t,e){return t.map(u=>{const s=e.get(u.path);if(!s)return u;const a={};return u.meta&&Object.assign(a,u.meta),s.meta&&Object.assign(a,s.meta),{...s,path:u.path,meta:Object.keys(a).length>0?a:void 0}})}async generateRouterConfig(){const t=await this.readPagesJson();if(!t)return;let e=this.parsePagesJson(t);const u=n(this.projectRoot,this.options.outputPath);if(this.options.preserveRouteChanges&&i(u))try{const a=await c.readFile(u,"utf-8"),r=this.extractExistingRoutes(a);r.size>0&&(e=this.mergeRoutes(e,r),this.logger.info("\u5DF2\u5408\u5E76\u7528\u6237\u5BF9\u8DEF\u7531\u914D\u7F6E\u7684\u4FEE\u6539"))}catch{}const s=this.generateFileContent(e);await F(u,s),this.logger.success(`\u8DEF\u7531\u914D\u7F6E\u6587\u4EF6\u5DF2\u751F\u6210: ${u}`),this.logger.info(`\u5171\u751F\u6210 ${e.length} \u6761\u8DEF\u7531\u914D\u7F6E`)}startWatching(){if(!this.options.watch)return;const t=n(this.projectRoot,this.options.pagesJsonPath);i(t)&&(this.watcher=f(t,async e=>{e==="change"&&(this.logger.info("\u68C0\u6D4B\u5230 pages.json \u53D8\u5316\uFF0C\u91CD\u65B0\u751F\u6210\u8DEF\u7531\u914D\u7F6E..."),await this.safeExecute(()=>this.generateRouterConfig(),"\u91CD\u65B0\u751F\u6210\u8DEF\u7531\u914D\u7F6E"))}),this.logger.info(`\u6B63\u5728\u76D1\u542C pages.json \u53D8\u5316: ${t}`))}stopWatching(){this.watcher&&(this.watcher.close(),this.watcher=null)}addPluginHooks(t){t.configResolved=async e=>{this.projectRoot=e.root,await this.safeExecute(()=>this.generateRouterConfig(),"\u751F\u6210\u8DEF\u7531\u914D\u7F6E"),e.command==="serve"&&this.startWatching()}}destroy(){super.destroy(),this.stopWatching()}}const C=h(D);export{C as generateRouter};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const factory_index=require("../../factory/index.cjs"),common_format_index=require("../../common/format/index.cjs"),common_fs_index=require("../../common/fs/index.cjs"),
|
|
1
|
+
"use strict";const factory_index=require("../../factory/index.cjs"),common_format_index=require("../../common/format/index.cjs"),common_fs_index=require("../../common/fs/index.cjs"),m=require("path"),crypto=require("crypto");require("../../logger/index.cjs"),require("../../shared/vite-plugin.Bcg6RW2N.cjs"),require("fs");function f(o=8){const e=Math.max(1,Math.min(64,o));return crypto.randomBytes(Math.ceil(e/2)).toString("hex").slice(0,e)}function c(o,e){let t=o;for(const[i,s]of Object.entries(e))t=t.replace(new RegExp(`\\{${i}\\}`,"g"),s);return t}class g extends factory_index.BasePlugin{version="";buildTime=new Date;getDefaultOptions(){return{format:"timestamp",semverBase:"1.0.0",outputType:"file",outputFile:"version.json",defineName:"__APP_VERSION__",hashLength:8,prefix:"",suffix:""}}validateOptions(){if(this.validator.field("format").enum(["timestamp","date","datetime","semver","hash","custom"]).field("outputType").enum(["file","define","both"]).field("hashLength").number().minValue(1).maxValue(32).validate(),this.options.format==="custom"&&!this.options.customFormat)throw new Error("\u5F53 format \u4E3A custom \u65F6\uFF0C\u5FC5\u987B\u63D0\u4F9B customFormat")}getPluginName(){return"generate-version"}generateVersion(){const e=common_format_index.getDateFormatParams(this.buildTime),t=f(this.options.hashLength);let i;switch(this.options.format){case"timestamp":i=`${e.YYYY}${e.MM}${e.DD}${e.HH}${e.mm}${e.ss}`;break;case"date":i=`${e.YYYY}.${e.MM}.${e.DD}`;break;case"datetime":i=`${e.YYYY}.${e.MM}.${e.DD}.${e.HH}${e.mm}${e.ss}`;break;case"semver":i=this.options.semverBase||"1.0.0";break;case"hash":i=t;break;case"custom":i=this.parseCustomFormat({...e,hash:t});break;default:i=e.timestamp}const s=this.options.prefix||"",n=this.options.suffix||"";return`${s}${i}${n}`}parseCustomFormat(e){const t={...e};if(this.options.semverBase){const[i,s,n]=this.options.semverBase.split(".");t.major=i||"1",t.minor=s||"0",t.patch=n||"0"}return c(this.options.customFormat||"",t)}generateVersionInfo(){return{version:this.version,buildTime:this.buildTime.toISOString(),timestamp:this.buildTime.getTime(),format:this.options.format,...this.options.extra}}async writeVersionFile(e){const t=m.join(e,this.options.outputFile||"version.json"),i=this.generateVersionInfo();await common_fs_index.writeFileContent(t,JSON.stringify(i,null,2)),this.logger.success(`\u7248\u672C\u6587\u4EF6\u5DF2\u751F\u6210: ${t}`)}addPluginHooks(e){e.configResolved=()=>{this.buildTime=new Date,this.version=this.generateVersion(),this.logger.info(`\u751F\u6210\u7248\u672C\u53F7: ${this.version}`)},(this.options.outputType==="define"||this.options.outputType==="both")&&(e.config=()=>{this.version||(this.buildTime=new Date,this.version=this.generateVersion());const t=this.options.defineName||"__APP_VERSION__";return this.logger.info(`\u6CE8\u5165\u5168\u5C40\u53D8\u91CF: ${t} = "${this.version}"`),{define:{[t]:JSON.stringify(this.version),[`${t}_INFO`]:JSON.stringify(this.generateVersionInfo())}}}),(this.options.outputType==="file"||this.options.outputType==="both")&&(e.writeBundle=async()=>{this.viteConfig&&await this.safeExecute(async()=>{const t=this.viteConfig.build.outDir;await this.writeVersionFile(t)},"\u5199\u5165\u7248\u672C\u6587\u4EF6")})}}const generateVersion=factory_index.createPluginFactory(g);exports.generateVersion=generateVersion;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BasePluginOptions, PluginFactory } from '../../factory/index.cjs';
|
|
2
2
|
import 'vite';
|
|
3
|
-
import '../../shared/vite-plugin.
|
|
3
|
+
import '../../shared/vite-plugin.B8FuZce1.cjs';
|
|
4
4
|
import '../../shared/vite-plugin.DRRlWY8P.cjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -46,6 +46,18 @@ interface VersionInfo {
|
|
|
46
46
|
* 自动生成版本号插件的配置选项接口
|
|
47
47
|
*
|
|
48
48
|
* @interface GenerateVersionOptions
|
|
49
|
+
* @extends {BasePluginOptions}
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* generateVersion({
|
|
54
|
+
* format: 'semver',
|
|
55
|
+
* semverBase: '2.0.0',
|
|
56
|
+
* outputType: 'both',
|
|
57
|
+
* prefix: 'v',
|
|
58
|
+
* extra: { environment: 'production' }
|
|
59
|
+
* })
|
|
60
|
+
* ```
|
|
49
61
|
*/
|
|
50
62
|
interface GenerateVersionOptions extends BasePluginOptions {
|
|
51
63
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BasePluginOptions, PluginFactory } from '../../factory/index.mjs';
|
|
2
2
|
import 'vite';
|
|
3
|
-
import '../../shared/vite-plugin.
|
|
3
|
+
import '../../shared/vite-plugin.B8FuZce1.mjs';
|
|
4
4
|
import '../../shared/vite-plugin.DRRlWY8P.mjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -46,6 +46,18 @@ interface VersionInfo {
|
|
|
46
46
|
* 自动生成版本号插件的配置选项接口
|
|
47
47
|
*
|
|
48
48
|
* @interface GenerateVersionOptions
|
|
49
|
+
* @extends {BasePluginOptions}
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* generateVersion({
|
|
54
|
+
* format: 'semver',
|
|
55
|
+
* semverBase: '2.0.0',
|
|
56
|
+
* outputType: 'both',
|
|
57
|
+
* prefix: 'v',
|
|
58
|
+
* extra: { environment: 'production' }
|
|
59
|
+
* })
|
|
60
|
+
* ```
|
|
49
61
|
*/
|
|
50
62
|
interface GenerateVersionOptions extends BasePluginOptions {
|
|
51
63
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BasePluginOptions, PluginFactory } from '../../factory/index.js';
|
|
2
2
|
import 'vite';
|
|
3
|
-
import '../../shared/vite-plugin.
|
|
3
|
+
import '../../shared/vite-plugin.B8FuZce1.js';
|
|
4
4
|
import '../../shared/vite-plugin.DRRlWY8P.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -46,6 +46,18 @@ interface VersionInfo {
|
|
|
46
46
|
* 自动生成版本号插件的配置选项接口
|
|
47
47
|
*
|
|
48
48
|
* @interface GenerateVersionOptions
|
|
49
|
+
* @extends {BasePluginOptions}
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* generateVersion({
|
|
54
|
+
* format: 'semver',
|
|
55
|
+
* semverBase: '2.0.0',
|
|
56
|
+
* outputType: 'both',
|
|
57
|
+
* prefix: 'v',
|
|
58
|
+
* extra: { environment: 'production' }
|
|
59
|
+
* })
|
|
60
|
+
* ```
|
|
49
61
|
*/
|
|
50
62
|
interface GenerateVersionOptions extends BasePluginOptions {
|
|
51
63
|
/**
|