@meng-xi/vite-plugin 0.1.2 → 0.1.4
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 +86 -510
- package/README.md +83 -506
- package/dist/common/compress/index.cjs +1 -0
- package/dist/common/compress/index.d.cts +23 -0
- package/dist/common/compress/index.d.mts +23 -0
- package/dist/common/compress/index.d.ts +23 -0
- package/dist/common/compress/index.mjs +1 -0
- package/dist/common/format/index.cjs +1 -1
- package/dist/common/format/index.d.cts +33 -1
- package/dist/common/format/index.d.mts +33 -1
- package/dist/common/format/index.d.ts +33 -1
- package/dist/common/format/index.mjs +1 -1
- package/dist/common/fs/index.cjs +1 -1
- package/dist/common/fs/index.d.cts +70 -2
- package/dist/common/fs/index.d.mts +70 -2
- package/dist/common/fs/index.d.ts +70 -2
- package/dist/common/fs/index.mjs +1 -1
- package/dist/common/html/index.cjs +2 -2
- package/dist/common/html/index.d.cts +268 -19
- package/dist/common/html/index.d.mts +268 -19
- package/dist/common/html/index.d.ts +268 -19
- package/dist/common/html/index.mjs +2 -2
- package/dist/common/index.cjs +1 -1
- package/dist/common/index.d.cts +8 -3
- package/dist/common/index.d.mts +8 -3
- package/dist/common/index.d.ts +8 -3
- package/dist/common/index.mjs +1 -1
- package/dist/common/path/index.cjs +1 -0
- package/dist/common/path/index.d.cts +22 -0
- package/dist/common/path/index.d.mts +22 -0
- package/dist/common/path/index.d.ts +22 -0
- package/dist/common/path/index.mjs +1 -0
- package/dist/common/ui/index.cjs +1 -0
- package/dist/common/ui/index.d.cts +132 -0
- package/dist/common/ui/index.d.mts +132 -0
- package/dist/common/ui/index.d.ts +132 -0
- package/dist/common/ui/index.mjs +1 -0
- package/dist/common/validation/index.cjs +1 -1
- package/dist/common/validation/index.d.cts +1 -0
- package/dist/common/validation/index.d.mts +1 -0
- package/dist/common/validation/index.d.ts +1 -0
- package/dist/common/validation/index.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +11 -4
- package/dist/index.d.mts +11 -4
- package/dist/index.d.ts +11 -4
- package/dist/index.mjs +1 -1
- package/dist/plugins/buildProgress/index.cjs +2 -2
- package/dist/plugins/buildProgress/index.mjs +2 -2
- package/dist/plugins/bundleAnalyzer/index.cjs +235 -0
- package/dist/plugins/bundleAnalyzer/index.d.cts +215 -0
- package/dist/plugins/bundleAnalyzer/index.d.mts +215 -0
- package/dist/plugins/bundleAnalyzer/index.d.ts +215 -0
- package/dist/plugins/bundleAnalyzer/index.mjs +235 -0
- package/dist/plugins/compressAssets/index.cjs +1 -1
- package/dist/plugins/compressAssets/index.mjs +1 -1
- package/dist/plugins/copyFile/index.d.cts +20 -2
- package/dist/plugins/copyFile/index.d.mts +20 -2
- package/dist/plugins/copyFile/index.d.ts +20 -2
- package/dist/plugins/envGuard/index.cjs +67 -0
- package/dist/plugins/envGuard/index.d.cts +156 -0
- package/dist/plugins/envGuard/index.d.mts +156 -0
- package/dist/plugins/envGuard/index.d.ts +156 -0
- package/dist/plugins/envGuard/index.mjs +67 -0
- package/dist/plugins/faviconManager/index.cjs +1 -1
- package/dist/plugins/faviconManager/index.d.cts +43 -5
- package/dist/plugins/faviconManager/index.d.mts +43 -5
- package/dist/plugins/faviconManager/index.d.ts +43 -5
- package/dist/plugins/faviconManager/index.mjs +1 -1
- package/dist/plugins/generateRouter/index.cjs +4 -4
- package/dist/plugins/generateRouter/index.d.cts +61 -14
- package/dist/plugins/generateRouter/index.d.mts +61 -14
- package/dist/plugins/generateRouter/index.d.ts +61 -14
- package/dist/plugins/generateRouter/index.mjs +1 -1
- package/dist/plugins/generateVersion/index.cjs +1 -1
- package/dist/plugins/generateVersion/index.d.cts +12 -0
- package/dist/plugins/generateVersion/index.d.mts +12 -0
- package/dist/plugins/generateVersion/index.d.ts +12 -0
- package/dist/plugins/generateVersion/index.mjs +1 -1
- package/dist/plugins/htmlInject/index.cjs +1 -7
- package/dist/plugins/htmlInject/index.d.cts +49 -194
- package/dist/plugins/htmlInject/index.d.mts +49 -194
- package/dist/plugins/htmlInject/index.d.ts +49 -194
- package/dist/plugins/htmlInject/index.mjs +1 -7
- package/dist/plugins/index.cjs +1 -1
- package/dist/plugins/index.d.cts +5 -1
- package/dist/plugins/index.d.mts +5 -1
- package/dist/plugins/index.d.ts +5 -1
- package/dist/plugins/index.mjs +1 -1
- package/dist/plugins/loadingManager/index.cjs +2 -2
- package/dist/plugins/loadingManager/index.mjs +1 -1
- package/dist/plugins/versionUpdateChecker/index.cjs +2 -2
- package/dist/plugins/versionUpdateChecker/index.mjs +3 -3
- package/dist/shared/vite-plugin.BCuhU1au.mjs +7 -0
- package/dist/shared/vite-plugin.BrI73DHA.cjs +7 -0
- package/dist/shared/vite-plugin.CmtcnItg.d.cts +261 -0
- package/dist/shared/vite-plugin.CmtcnItg.d.mts +261 -0
- package/dist/shared/vite-plugin.CmtcnItg.d.ts +261 -0
- package/dist/shared/vite-plugin.DnFDPjNf.mjs +1 -0
- package/dist/shared/vite-plugin.Dumot0up.mjs +1 -0
- package/dist/shared/vite-plugin.FfJ-Wwfu.d.cts +143 -0
- package/dist/shared/vite-plugin.FfJ-Wwfu.d.mts +143 -0
- package/dist/shared/vite-plugin.FfJ-Wwfu.d.ts +143 -0
- package/dist/shared/vite-plugin.soT9a-KD.cjs +1 -0
- package/dist/shared/vite-plugin.vwox4bU0.cjs +1 -0
- package/package.json +26 -1
|
@@ -7,22 +7,36 @@ import '../../shared/vite-plugin.DRRlWY8P.mjs';
|
|
|
7
7
|
* 图标配置项接口
|
|
8
8
|
*
|
|
9
9
|
* @interface Icon
|
|
10
|
+
* @description 定义单个网站图标的属性,对应 HTML `<link>` 标签的各个属性。
|
|
10
11
|
*/
|
|
11
12
|
interface Icon {
|
|
12
13
|
/**
|
|
13
|
-
*
|
|
14
|
+
* 图标关系类型,对应 `<link>` 标签的 `rel` 属性
|
|
15
|
+
*
|
|
16
|
+
* @example 'icon'
|
|
17
|
+
* @example 'apple-touch-icon'
|
|
18
|
+
* @example 'manifest'
|
|
14
19
|
*/
|
|
15
20
|
rel: string;
|
|
16
21
|
/**
|
|
17
|
-
* 图标 URL
|
|
22
|
+
* 图标 URL,对应 `<link>` 标签的 `href` 属性
|
|
23
|
+
*
|
|
24
|
+
* @example '/favicon.ico'
|
|
25
|
+
* @example '/apple-touch-icon.png'
|
|
18
26
|
*/
|
|
19
27
|
href: string;
|
|
20
28
|
/**
|
|
21
|
-
*
|
|
29
|
+
* 图标尺寸,对应 `<link>` 标签的 `sizes` 属性
|
|
30
|
+
*
|
|
31
|
+
* @example '32x32'
|
|
32
|
+
* @example '180x180'
|
|
22
33
|
*/
|
|
23
34
|
sizes?: string;
|
|
24
35
|
/**
|
|
25
|
-
* 图标 MIME
|
|
36
|
+
* 图标 MIME 类型,对应 `<link>` 标签的 `type` 属性
|
|
37
|
+
*
|
|
38
|
+
* @example 'image/png'
|
|
39
|
+
* @example 'image/svg+xml'
|
|
26
40
|
*/
|
|
27
41
|
type?: string;
|
|
28
42
|
}
|
|
@@ -30,6 +44,8 @@ interface Icon {
|
|
|
30
44
|
* 图标文件复制配置选项接口
|
|
31
45
|
*
|
|
32
46
|
* @interface CopyOptions
|
|
47
|
+
* @description 配置图标文件从源目录到构建输出目录的复制行为,
|
|
48
|
+
* 仅当此对象存在时才开启图标文件复制功能。
|
|
33
49
|
*/
|
|
34
50
|
interface CopyOptions {
|
|
35
51
|
/**
|
|
@@ -51,7 +67,7 @@ interface CopyOptions {
|
|
|
51
67
|
*/
|
|
52
68
|
overwrite?: boolean;
|
|
53
69
|
/**
|
|
54
|
-
*
|
|
70
|
+
* 是否支持递归复制子目录
|
|
55
71
|
*
|
|
56
72
|
* @default true
|
|
57
73
|
*/
|
|
@@ -61,6 +77,28 @@ interface CopyOptions {
|
|
|
61
77
|
* 网站图标管理插件的配置选项接口
|
|
62
78
|
*
|
|
63
79
|
* @interface FaviconManagerOptions
|
|
80
|
+
* @extends {BasePluginOptions}
|
|
81
|
+
*
|
|
82
|
+
* @description 支持三种图标配置方式(优先级从高到低):
|
|
83
|
+
* 1. `link` - 自定义完整的 `<link>` 标签 HTML
|
|
84
|
+
* 2. `url` - 完整的图标 URL
|
|
85
|
+
* 3. `base` + 默认 favicon.ico - 基础路径拼接
|
|
86
|
+
* 此外还支持通过 `icons` 数组配置多个图标,以及通过 `copyOptions` 复制图标文件。
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* faviconManager({
|
|
91
|
+
* base: '/assets',
|
|
92
|
+
* icons: [
|
|
93
|
+
* { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' },
|
|
94
|
+
* { rel: 'icon', href: '/favicon-32x32.png', sizes: '32x32', type: 'image/png' }
|
|
95
|
+
* ],
|
|
96
|
+
* copyOptions: {
|
|
97
|
+
* sourceDir: 'src/assets/icons',
|
|
98
|
+
* targetDir: 'dist/assets/icons'
|
|
99
|
+
* }
|
|
100
|
+
* })
|
|
101
|
+
* ```
|
|
64
102
|
*/
|
|
65
103
|
interface FaviconManagerOptions extends BasePluginOptions {
|
|
66
104
|
/**
|
|
@@ -7,22 +7,36 @@ import '../../shared/vite-plugin.DRRlWY8P.js';
|
|
|
7
7
|
* 图标配置项接口
|
|
8
8
|
*
|
|
9
9
|
* @interface Icon
|
|
10
|
+
* @description 定义单个网站图标的属性,对应 HTML `<link>` 标签的各个属性。
|
|
10
11
|
*/
|
|
11
12
|
interface Icon {
|
|
12
13
|
/**
|
|
13
|
-
*
|
|
14
|
+
* 图标关系类型,对应 `<link>` 标签的 `rel` 属性
|
|
15
|
+
*
|
|
16
|
+
* @example 'icon'
|
|
17
|
+
* @example 'apple-touch-icon'
|
|
18
|
+
* @example 'manifest'
|
|
14
19
|
*/
|
|
15
20
|
rel: string;
|
|
16
21
|
/**
|
|
17
|
-
* 图标 URL
|
|
22
|
+
* 图标 URL,对应 `<link>` 标签的 `href` 属性
|
|
23
|
+
*
|
|
24
|
+
* @example '/favicon.ico'
|
|
25
|
+
* @example '/apple-touch-icon.png'
|
|
18
26
|
*/
|
|
19
27
|
href: string;
|
|
20
28
|
/**
|
|
21
|
-
*
|
|
29
|
+
* 图标尺寸,对应 `<link>` 标签的 `sizes` 属性
|
|
30
|
+
*
|
|
31
|
+
* @example '32x32'
|
|
32
|
+
* @example '180x180'
|
|
22
33
|
*/
|
|
23
34
|
sizes?: string;
|
|
24
35
|
/**
|
|
25
|
-
* 图标 MIME
|
|
36
|
+
* 图标 MIME 类型,对应 `<link>` 标签的 `type` 属性
|
|
37
|
+
*
|
|
38
|
+
* @example 'image/png'
|
|
39
|
+
* @example 'image/svg+xml'
|
|
26
40
|
*/
|
|
27
41
|
type?: string;
|
|
28
42
|
}
|
|
@@ -30,6 +44,8 @@ interface Icon {
|
|
|
30
44
|
* 图标文件复制配置选项接口
|
|
31
45
|
*
|
|
32
46
|
* @interface CopyOptions
|
|
47
|
+
* @description 配置图标文件从源目录到构建输出目录的复制行为,
|
|
48
|
+
* 仅当此对象存在时才开启图标文件复制功能。
|
|
33
49
|
*/
|
|
34
50
|
interface CopyOptions {
|
|
35
51
|
/**
|
|
@@ -51,7 +67,7 @@ interface CopyOptions {
|
|
|
51
67
|
*/
|
|
52
68
|
overwrite?: boolean;
|
|
53
69
|
/**
|
|
54
|
-
*
|
|
70
|
+
* 是否支持递归复制子目录
|
|
55
71
|
*
|
|
56
72
|
* @default true
|
|
57
73
|
*/
|
|
@@ -61,6 +77,28 @@ interface CopyOptions {
|
|
|
61
77
|
* 网站图标管理插件的配置选项接口
|
|
62
78
|
*
|
|
63
79
|
* @interface FaviconManagerOptions
|
|
80
|
+
* @extends {BasePluginOptions}
|
|
81
|
+
*
|
|
82
|
+
* @description 支持三种图标配置方式(优先级从高到低):
|
|
83
|
+
* 1. `link` - 自定义完整的 `<link>` 标签 HTML
|
|
84
|
+
* 2. `url` - 完整的图标 URL
|
|
85
|
+
* 3. `base` + 默认 favicon.ico - 基础路径拼接
|
|
86
|
+
* 此外还支持通过 `icons` 数组配置多个图标,以及通过 `copyOptions` 复制图标文件。
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* faviconManager({
|
|
91
|
+
* base: '/assets',
|
|
92
|
+
* icons: [
|
|
93
|
+
* { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' },
|
|
94
|
+
* { rel: 'icon', href: '/favicon-32x32.png', sizes: '32x32', type: 'image/png' }
|
|
95
|
+
* ],
|
|
96
|
+
* copyOptions: {
|
|
97
|
+
* sourceDir: 'src/assets/icons',
|
|
98
|
+
* targetDir: 'dist/assets/icons'
|
|
99
|
+
* }
|
|
100
|
+
* })
|
|
101
|
+
* ```
|
|
64
102
|
*/
|
|
65
103
|
interface FaviconManagerOptions extends BasePluginOptions {
|
|
66
104
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createPluginFactory as n,BasePlugin as a}from"../../factory/index.mjs";import{checkSourceExists as c,copySourceToTarget as l}from"../../common/fs/index.mjs";import{V as g}from"../../shared/vite-plugin.DcExl6jd.mjs";import{injectBeforeTag as p}from"../../common/html/index.mjs";import"../../logger/index.mjs";import"../../common/object/index.mjs";import"fs";import"path";function F(u){const i=[];if(u.link)return[];if(u.icons&&u.icons.length>0)for(const e of u.icons){const t={rel:e.rel,href:e.href};e.sizes&&(t.sizes=e.sizes),e.type&&(t.type=e.type),i.push({tag:"link",attrs:t,injectTo:"head"})}else if(u.url)i.push({tag:"link",attrs:{rel:"icon",href:u.url},injectTo:"head"});else{const e=u.base||"/",t=e.endsWith("/")?`${e}favicon.ico`:`${e}/favicon.ico`;i.push({tag:"link",attrs:{rel:"icon",href:t},injectTo:"head"})}return i}class h extends a{getDefaultOptions(){return{base:"/"}}validateOptions(){this.validator.field("base").string().field("url").string().field("link").string().field("icons").array(),this.options?.copyOptions&&(this.validator.field("copyOptions").object(),new g(this.options.copyOptions).field("sourceDir").required().string().field("targetDir").required().string().field("overwrite").boolean().field("recursive").boolean().validate()),this.validator.validate()}getPluginName(){return"favicon-manager"}getIconTagDescriptors(){if(!this.options.enabled)return this.logger.info("\u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u56FE\u6807\u6CE8\u5165"),[];const i=F(this.options);return i.length>0&&this.logger.success(`\u6210\u529F\u6CE8\u5165 ${i.length} \u4E2A\u56FE\u6807\u6807\u7B7E\u5230 HTML \u6587\u4EF6`),i}injectCustomLinkTag(i){if(!this.options.enabled||!this.options.link)return i;const e=this.options.link,t=p(i,"</head>",e);return t.injected?(this.logger.success("\u6210\u529F\u6CE8\u5165\u81EA\u5B9A\u4E49\u56FE\u6807\u6807\u7B7E\u5230 HTML \u6587\u4EF6"),this.logger.info(` - ${e}`),t.html):(this.logger.warn("\u672A\u627E\u5230 </head> \u6807\u7B7E\uFF0C\u8DF3\u8FC7\u56FE\u6807\u6CE8\u5165"),i)}async copyFiles(){if(!this.options.enabled){this.logger.info("\u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u6587\u4EF6\u590D\u5236");return}const{copyOptions:i}=this.options;if(!i)return;const{sourceDir:e,targetDir:t,overwrite:
|
|
1
|
+
import{createPluginFactory as n,BasePlugin as a}from"../../factory/index.mjs";import{checkSourceExists as c,copySourceToTarget as l}from"../../common/fs/index.mjs";import{V as g}from"../../shared/vite-plugin.DcExl6jd.mjs";import{injectBeforeTag as p}from"../../common/html/index.mjs";import"../../logger/index.mjs";import"../../common/object/index.mjs";import"fs";import"path";import"../../shared/vite-plugin.BCuhU1au.mjs";import"../../common/script/index.mjs";function F(u){const i=[];if(u.link)return[];if(u.icons&&u.icons.length>0)for(const e of u.icons){const t={rel:e.rel,href:e.href};e.sizes&&(t.sizes=e.sizes),e.type&&(t.type=e.type),i.push({tag:"link",attrs:t,injectTo:"head"})}else if(u.url)i.push({tag:"link",attrs:{rel:"icon",href:u.url},injectTo:"head"});else{const e=u.base||"/",t=e.endsWith("/")?`${e}favicon.ico`:`${e}/favicon.ico`;i.push({tag:"link",attrs:{rel:"icon",href:t},injectTo:"head"})}return i}class h extends a{getDefaultOptions(){return{base:"/"}}validateOptions(){this.validator.field("base").string().field("url").string().field("link").string().field("icons").array(),this.options?.copyOptions&&(this.validator.field("copyOptions").object(),new g(this.options.copyOptions).field("sourceDir").required().string().field("targetDir").required().string().field("overwrite").boolean().field("recursive").boolean().validate()),this.validator.validate()}getPluginName(){return"favicon-manager"}getIconTagDescriptors(){if(!this.options.enabled)return this.logger.info("\u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u56FE\u6807\u6CE8\u5165"),[];const i=F(this.options);return i.length>0&&this.logger.success(`\u6210\u529F\u6CE8\u5165 ${i.length} \u4E2A\u56FE\u6807\u6807\u7B7E\u5230 HTML \u6587\u4EF6`),i}injectCustomLinkTag(i){if(!this.options.enabled||!this.options.link)return i;const e=this.options.link,t=p(i,"</head>",e);return t.injected?(this.logger.success("\u6210\u529F\u6CE8\u5165\u81EA\u5B9A\u4E49\u56FE\u6807\u6807\u7B7E\u5230 HTML \u6587\u4EF6"),this.logger.info(` - ${e}`),t.html):(this.logger.warn("\u672A\u627E\u5230 </head> \u6807\u7B7E\uFF0C\u8DF3\u8FC7\u56FE\u6807\u6CE8\u5165"),i)}async copyFiles(){if(!this.options.enabled){this.logger.info("\u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u6587\u4EF6\u590D\u5236");return}const{copyOptions:i}=this.options;if(!i)return;const{sourceDir:e,targetDir:t,overwrite:o=!0,recursive:s=!0}=i;await c(e);const r=await l(e,t,{recursive:s,overwrite:o,incremental:!0});this.logger.success(`\u56FE\u6807\u6587\u4EF6\u590D\u5236\u6210\u529F\uFF1A\u4ECE ${e} \u5230 ${t}`,`\u590D\u5236\u4E86 ${r.copiedFiles} \u4E2A\u6587\u4EF6\uFF0C\u8DF3\u8FC7\u4E86 ${r.skippedFiles} \u4E2A\u6587\u4EF6\uFF0C\u8017\u65F6 ${r.executionTime}ms`)}addPluginHooks(i){i.transformIndexHtml={order:"pre",handler:e=>{if(this.options.link)return this.injectCustomLinkTag(e);const t=this.getIconTagDescriptors();return t.length>0?{html:e,tags:t}:e}},i.writeBundle=async()=>{await this.safeExecute(()=>this.copyFiles(),"\u56FE\u6807\u6587\u4EF6\u590D\u5236")}}}const f=n(h,u=>typeof u=="string"?{base:u}:u||{});export{f as faviconManager};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";const factory_index=require("../../factory/index.cjs"),common_fs_index=require("../../common/fs/index.cjs"),common_format_index=require("../../common/format/index.cjs"),
|
|
1
|
+
"use strict";const factory_index=require("../../factory/index.cjs"),common_fs_index=require("../../common/fs/index.cjs"),common_format_index=require("../../common/format/index.cjs"),f=require("path"),s=require("fs");require("../../logger/index.cjs"),require("../../common/object/index.cjs"),require("../../shared/vite-plugin.Bcg6RW2N.cjs"),require("crypto"),require("node:path");class R 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 common_format_index.toCamelCase(e);case"pascalCase":return common_format_index.toPascalCase(e);case"custom":return this.options.customNameGenerator(e);default:return common_format_index.toCamelCase(e)}}extractMeta(e,u){const t={},a=e.style||{},r=this.options.metaMapping||{};for(const[o,n]of Object.entries(r))a[o]!==void 0&&(t[n]=a[o]);return this.tabBarPages.has(u)&&(t.isTab=!0),t}parsePageToRoute(e,u=""){const t=u?`/${u}/${e.path}`:`/${e.path}`,a=this.generateRouteName(t),r=this.extractMeta(e,t.replace(/^\//,"")),o={path:t,name:a};return Object.keys(r).length>0&&(o.meta=r),o}parsePagesJson(e){const u=[];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"),u;if(this.tabBarPages.clear(),e.tabBar?.list)for(const t of e.tabBar.list)this.tabBarPages.add(t.pagePath);for(const t of e.pages)u.push(this.parsePageToRoute(t));if(this.options.includeSubPackages&&e.subPackages){for(const t of e.subPackages)if(t.pages&&Array.isArray(t.pages))for(const a of t.pages)u.push(this.parsePageToRoute(a,t.root))}return u}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 u=this.generateTypeDefinitions(),t=this.options.outputFormat==="ts",a=JSON.stringify(e,null," ").replace(/"(\w+)":/g,"$1:").replace(/: "([^"]+)"/g,": '$1'");return`${u}
|
|
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${t?": RouteConfig[]":""} = ${
|
|
32
|
+
export const routes${t?": RouteConfig[]":""} = ${a}
|
|
33
33
|
|
|
34
34
|
export default routes
|
|
35
|
-
`}async readPagesJson(){const e=
|
|
35
|
+
`}async readPagesJson(){const e=f.resolve(this.projectRoot,this.options.pagesJsonPath);if(!s.existsSync(e))return this.logger.warn(`pages.json \u6587\u4EF6\u4E0D\u5B58\u5728: ${e}`),null;try{const u=await common_fs_index.readFileContent(e),t=common_format_index.stripJsonComments(u);return JSON.parse(t)}catch(u){return this.logger.error(`\u89E3\u6790 pages.json \u5931\u8D25: ${u.message}`),null}}extractExistingRoutes(e){const u=new Map,t=e.match(/export const routes[^=]*=\s*(\[[\s\S]*?\](?=\s*\n|\s*$|\s*\/\/))/);if(!t)return u;try{let a=t[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&&u.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 u}mergeRoutes(e,u){return e.map(t=>{const a=u.get(t.path);if(!a)return t;const r={};return t.meta&&Object.assign(r,t.meta),a.meta&&Object.assign(r,a.meta),{...a,path:t.path,meta:Object.keys(r).length>0?r:void 0}})}async generateRouterConfig(){const e=await this.readPagesJson();if(!e)return;let u=this.parsePagesJson(e);const t=f.resolve(this.projectRoot,this.options.outputPath);if(this.options.preserveRouteChanges&&s.existsSync(t))try{const r=await common_fs_index.readFileContent(t),o=this.extractExistingRoutes(r);o.size>0&&(u=this.mergeRoutes(u,o),this.logger.info("\u5DF2\u5408\u5E76\u7528\u6237\u5BF9\u8DEF\u7531\u914D\u7F6E\u7684\u4FEE\u6539"))}catch{}const a=this.generateFileContent(u);await common_fs_index.writeFileContent(t,a),this.logger.success(`\u8DEF\u7531\u914D\u7F6E\u6587\u4EF6\u5DF2\u751F\u6210: ${t}`),this.logger.info(`\u5171\u751F\u6210 ${u.length} \u6761\u8DEF\u7531\u914D\u7F6E`)}startWatching(){if(!this.options.watch)return;const e=f.resolve(this.projectRoot,this.options.pagesJsonPath);s.existsSync(e)&&(this.watcher=s.watch(e,async u=>{u==="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 u=>{this.projectRoot=u.root,await this.safeExecute(()=>this.generateRouterConfig(),"\u751F\u6210\u8DEF\u7531\u914D\u7F6E"),u.command==="serve"&&this.startWatching()}}destroy(){super.destroy(),this.stopWatching()}}const generateRouter=factory_index.createPluginFactory(R);exports.generateRouter=generateRouter;
|
|
@@ -5,37 +5,47 @@ 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
|
/**
|
|
@@ -5,37 +5,47 @@ 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
|
/**
|