@meng-xi/vite-plugin 0.1.0 → 0.1.1
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 +170 -49
- package/README.md +149 -29
- package/dist/common/format/index.cjs +1 -0
- package/dist/common/format/index.d.cts +156 -0
- package/dist/common/format/index.d.mts +156 -0
- package/dist/common/format/index.d.ts +156 -0
- package/dist/common/format/index.mjs +1 -0
- package/dist/common/fs/index.cjs +1 -0
- package/dist/common/fs/index.d.cts +150 -0
- package/dist/common/fs/index.d.mts +150 -0
- package/dist/common/fs/index.d.ts +150 -0
- package/dist/common/fs/index.mjs +1 -0
- package/dist/common/html/index.cjs +2 -0
- package/dist/common/html/index.d.cts +109 -0
- package/dist/common/html/index.d.mts +109 -0
- package/dist/common/html/index.d.ts +109 -0
- package/dist/common/html/index.mjs +2 -0
- package/dist/common/index.cjs +1 -1
- package/dist/common/index.d.cts +7 -417
- package/dist/common/index.d.mts +7 -417
- package/dist/common/index.d.ts +7 -417
- package/dist/common/index.mjs +1 -1
- package/dist/common/object/index.cjs +1 -0
- package/dist/common/object/index.d.cts +30 -0
- package/dist/common/object/index.d.mts +30 -0
- package/dist/common/object/index.d.ts +30 -0
- package/dist/common/object/index.mjs +1 -0
- package/dist/common/script/index.cjs +1 -0
- package/dist/common/script/index.d.cts +54 -0
- package/dist/common/script/index.d.mts +54 -0
- package/dist/common/script/index.d.ts +54 -0
- package/dist/common/script/index.mjs +1 -0
- package/dist/common/validation/index.cjs +1 -0
- package/dist/common/validation/index.d.cts +93 -0
- package/dist/common/validation/index.d.mts +93 -0
- package/dist/common/validation/index.d.ts +93 -0
- package/dist/common/validation/index.mjs +1 -0
- 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 +15 -3
- package/dist/index.d.mts +15 -3
- package/dist/index.d.ts +15 -3
- package/dist/index.mjs +1 -1
- package/dist/plugins/buildProgress/index.cjs +2 -0
- package/dist/plugins/buildProgress/index.d.cts +187 -0
- package/dist/plugins/buildProgress/index.d.mts +187 -0
- package/dist/plugins/buildProgress/index.d.ts +187 -0
- package/dist/plugins/buildProgress/index.mjs +2 -0
- package/dist/plugins/copyFile/index.cjs +1 -0
- package/dist/plugins/copyFile/index.d.cts +78 -0
- package/dist/plugins/copyFile/index.d.mts +78 -0
- package/dist/plugins/copyFile/index.d.ts +78 -0
- package/dist/plugins/copyFile/index.mjs +1 -0
- package/dist/plugins/faviconManager/index.cjs +1 -0
- package/dist/plugins/faviconManager/index.d.cts +143 -0
- package/dist/plugins/faviconManager/index.d.mts +143 -0
- package/dist/plugins/faviconManager/index.d.ts +143 -0
- package/dist/plugins/faviconManager/index.mjs +1 -0
- package/dist/plugins/generateRouter/index.cjs +35 -0
- package/dist/plugins/generateRouter/index.d.cts +215 -0
- package/dist/plugins/generateRouter/index.d.mts +215 -0
- package/dist/plugins/generateRouter/index.d.ts +215 -0
- package/dist/plugins/generateRouter/index.mjs +35 -0
- package/dist/plugins/generateVersion/index.cjs +1 -0
- package/dist/plugins/generateVersion/index.d.cts +184 -0
- package/dist/plugins/generateVersion/index.d.mts +184 -0
- package/dist/plugins/generateVersion/index.d.ts +184 -0
- package/dist/plugins/generateVersion/index.mjs +1 -0
- package/dist/plugins/htmlInject/index.cjs +7 -0
- package/dist/plugins/htmlInject/index.d.cts +278 -0
- package/dist/plugins/htmlInject/index.d.mts +278 -0
- package/dist/plugins/htmlInject/index.d.ts +278 -0
- package/dist/plugins/htmlInject/index.mjs +7 -0
- package/dist/plugins/index.cjs +1 -1
- package/dist/plugins/index.d.cts +10 -1714
- package/dist/plugins/index.d.mts +10 -1714
- package/dist/plugins/index.d.ts +10 -1714
- package/dist/plugins/index.mjs +1 -1
- package/dist/plugins/loadingManager/index.cjs +487 -0
- package/dist/plugins/loadingManager/index.d.cts +769 -0
- package/dist/plugins/loadingManager/index.d.mts +769 -0
- package/dist/plugins/loadingManager/index.d.ts +769 -0
- package/dist/plugins/loadingManager/index.mjs +487 -0
- package/dist/plugins/versionUpdateChecker/index.cjs +185 -0
- package/dist/plugins/versionUpdateChecker/index.d.cts +200 -0
- package/dist/plugins/versionUpdateChecker/index.d.mts +200 -0
- package/dist/plugins/versionUpdateChecker/index.d.ts +200 -0
- package/dist/plugins/versionUpdateChecker/index.mjs +185 -0
- package/dist/shared/vite-plugin.Bcg6RW2N.cjs +3 -0
- package/dist/shared/{vite-plugin.CiHfwMiN.d.ts → vite-plugin.DRRlWY8P.d.cts} +50 -0
- package/dist/shared/{vite-plugin.CiHfwMiN.d.cts → vite-plugin.DRRlWY8P.d.mts} +50 -0
- package/dist/shared/{vite-plugin.CiHfwMiN.d.mts → vite-plugin.DRRlWY8P.d.ts} +50 -0
- package/dist/shared/{vite-plugin.B88RyRN8.mjs → vite-plugin.DcExl6jd.mjs} +2 -2
- package/package.json +72 -2
- package/dist/shared/vite-plugin.CawoITTT.cjs +0 -1
- package/dist/shared/vite-plugin.D6Law9Ke.mjs +0 -706
- package/dist/shared/vite-plugin.D8L9KzuW.cjs +0 -706
- package/dist/shared/vite-plugin.DFjf9wFM.mjs +0 -2
- package/dist/shared/vite-plugin.DSb6XzBn.mjs +0 -1
- package/dist/shared/vite-plugin.IGZeStMa.cjs +0 -3
- package/dist/shared/vite-plugin.Tab4qcIM.cjs +0 -2
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { BasePluginOptions, PluginFactory } from '../../factory/index.mjs';
|
|
2
|
+
import 'vite';
|
|
3
|
+
import '../../shared/vite-plugin.CLr0ttuO.mjs';
|
|
4
|
+
import '../../shared/vite-plugin.DRRlWY8P.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 复制文件插件的配置选项接口
|
|
8
|
+
*
|
|
9
|
+
* @interface CopyFileOptions
|
|
10
|
+
*/
|
|
11
|
+
interface CopyFileOptions extends BasePluginOptions {
|
|
12
|
+
/**
|
|
13
|
+
* 源文件目录的路径
|
|
14
|
+
*
|
|
15
|
+
* @example 'src/assets'
|
|
16
|
+
*/
|
|
17
|
+
sourceDir: string;
|
|
18
|
+
/**
|
|
19
|
+
* 目标文件目录的路径
|
|
20
|
+
*
|
|
21
|
+
* @example 'dist/assets'
|
|
22
|
+
*/
|
|
23
|
+
targetDir: string;
|
|
24
|
+
/**
|
|
25
|
+
* 是否覆盖同名文件
|
|
26
|
+
*
|
|
27
|
+
* @default true
|
|
28
|
+
*/
|
|
29
|
+
overwrite?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* 是否支持递归复制
|
|
32
|
+
*
|
|
33
|
+
* @default true
|
|
34
|
+
*/
|
|
35
|
+
recursive?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* 是否启用增量复制
|
|
38
|
+
*
|
|
39
|
+
* @default true
|
|
40
|
+
*/
|
|
41
|
+
incremental?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 复制文件插件
|
|
46
|
+
*
|
|
47
|
+
* @param {CopyFileOptions} options - 插件配置选项
|
|
48
|
+
* @returns {Plugin} 一个 Vite 插件实例
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* // 基本使用
|
|
53
|
+
* copyFile({
|
|
54
|
+
* sourceDir: 'src/assets',
|
|
55
|
+
* targetDir: 'dist/assets'
|
|
56
|
+
* })
|
|
57
|
+
*
|
|
58
|
+
* // 高级配置
|
|
59
|
+
* copyFile({
|
|
60
|
+
* sourceDir: 'src/static',
|
|
61
|
+
* targetDir: 'dist/static',
|
|
62
|
+
* overwrite: false,
|
|
63
|
+
* recursive: true,
|
|
64
|
+
* incremental: true,
|
|
65
|
+
* enabled: true,
|
|
66
|
+
* verbose: true,
|
|
67
|
+
* errorStrategy: 'throw'
|
|
68
|
+
* })
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* 该插件会在 Vite 构建完成后执行,将指定源目录的所有文件和子目录复制到目标目录。
|
|
73
|
+
* 支持增量复制、递归复制和覆盖控制等功能。
|
|
74
|
+
*/
|
|
75
|
+
declare const copyFile: PluginFactory<CopyFileOptions, CopyFileOptions>;
|
|
76
|
+
|
|
77
|
+
export { copyFile };
|
|
78
|
+
export type { CopyFileOptions };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { BasePluginOptions, PluginFactory } from '../../factory/index.js';
|
|
2
|
+
import 'vite';
|
|
3
|
+
import '../../shared/vite-plugin.CLr0ttuO.js';
|
|
4
|
+
import '../../shared/vite-plugin.DRRlWY8P.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 复制文件插件的配置选项接口
|
|
8
|
+
*
|
|
9
|
+
* @interface CopyFileOptions
|
|
10
|
+
*/
|
|
11
|
+
interface CopyFileOptions extends BasePluginOptions {
|
|
12
|
+
/**
|
|
13
|
+
* 源文件目录的路径
|
|
14
|
+
*
|
|
15
|
+
* @example 'src/assets'
|
|
16
|
+
*/
|
|
17
|
+
sourceDir: string;
|
|
18
|
+
/**
|
|
19
|
+
* 目标文件目录的路径
|
|
20
|
+
*
|
|
21
|
+
* @example 'dist/assets'
|
|
22
|
+
*/
|
|
23
|
+
targetDir: string;
|
|
24
|
+
/**
|
|
25
|
+
* 是否覆盖同名文件
|
|
26
|
+
*
|
|
27
|
+
* @default true
|
|
28
|
+
*/
|
|
29
|
+
overwrite?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* 是否支持递归复制
|
|
32
|
+
*
|
|
33
|
+
* @default true
|
|
34
|
+
*/
|
|
35
|
+
recursive?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* 是否启用增量复制
|
|
38
|
+
*
|
|
39
|
+
* @default true
|
|
40
|
+
*/
|
|
41
|
+
incremental?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 复制文件插件
|
|
46
|
+
*
|
|
47
|
+
* @param {CopyFileOptions} options - 插件配置选项
|
|
48
|
+
* @returns {Plugin} 一个 Vite 插件实例
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* // 基本使用
|
|
53
|
+
* copyFile({
|
|
54
|
+
* sourceDir: 'src/assets',
|
|
55
|
+
* targetDir: 'dist/assets'
|
|
56
|
+
* })
|
|
57
|
+
*
|
|
58
|
+
* // 高级配置
|
|
59
|
+
* copyFile({
|
|
60
|
+
* sourceDir: 'src/static',
|
|
61
|
+
* targetDir: 'dist/static',
|
|
62
|
+
* overwrite: false,
|
|
63
|
+
* recursive: true,
|
|
64
|
+
* incremental: true,
|
|
65
|
+
* enabled: true,
|
|
66
|
+
* verbose: true,
|
|
67
|
+
* errorStrategy: 'throw'
|
|
68
|
+
* })
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* 该插件会在 Vite 构建完成后执行,将指定源目录的所有文件和子目录复制到目标目录。
|
|
73
|
+
* 支持增量复制、递归复制和覆盖控制等功能。
|
|
74
|
+
*/
|
|
75
|
+
declare const copyFile: PluginFactory<CopyFileOptions, CopyFileOptions>;
|
|
76
|
+
|
|
77
|
+
export { copyFile };
|
|
78
|
+
export type { CopyFileOptions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createPluginFactory as c,BasePlugin as n}from"../../factory/index.mjs";import{checkSourceExists as a,copySourceToTarget as l}from"../../common/fs/index.mjs";import"../../logger/index.mjs";import"../../common/object/index.mjs";import"../../shared/vite-plugin.DcExl6jd.mjs";import"fs";import"path";class F extends n{getDefaultOptions(){return{overwrite:!0,recursive:!0,incremental:!0}}validateOptions(){this.validator.field("sourceDir").required().string().custom(e=>e.trim()!=="","sourceDir \u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32").field("targetDir").required().string().custom(e=>e.trim()!=="","targetDir \u4E0D\u80FD\u4E3A\u7A7A\u5B57\u7B26\u4E32").field("overwrite").boolean().field("recursive").boolean().field("incremental").boolean().validate()}getPluginName(){return"copy-file"}getEnforce(){return"post"}async copyFiles(){const{sourceDir:e,targetDir:u,overwrite:t=!0,recursive:i=!0,incremental:o=!0,enabled:s=!0}=this.options;if(!s){this.logger.info(`\u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u6267\u884C\uFF1A\u4ECE ${e} \u590D\u5236\u5230 ${u}`);return}await a(e);const r=await l(e,u,{recursive:i,overwrite:t,incremental:o});this.logger.success(`\u590D\u5236\u6587\u4EF6\u6210\u529F\uFF1A\u4ECE ${e} \u5230 ${u}`,`\u590D\u5236\u4E86 ${r.copiedFiles} \u4E2A\u6587\u4EF6\uFF0C\u8DF3\u8FC7\u4E86 ${r.skippedFiles} \u4E2A\u6587\u4EF6\uFF0C\u8017\u65F6 ${r.executionTime}ms`)}addPluginHooks(e){e.writeBundle=async()=>{await this.safeExecute(()=>this.copyFiles(),"\u590D\u5236\u6587\u4EF6")}}}const E=c(F);export{E as copyFile};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const factory_index=require("../../factory/index.cjs"),common_fs_index=require("../../common/fs/index.cjs"),validator=require("../../shared/vite-plugin.Bcg6RW2N.cjs"),common_html_index=require("../../common/html/index.cjs");require("../../logger/index.cjs"),require("../../common/object/index.cjs"),require("fs"),require("path");function generateIconTagDescriptors(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 f extends factory_index.BasePlugin{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 validator.Validator(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=generateIconTagDescriptors(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=common_html_index.injectBeforeTag(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:r=!0,recursive:n=!0}=i;await common_fs_index.checkSourceExists(e);const s=await common_fs_index.copySourceToTarget(e,t,{recursive:n,overwrite:r,incremental:!0});this.logger.success(`\u56FE\u6807\u6587\u4EF6\u590D\u5236\u6210\u529F\uFF1A\u4ECE ${e} \u5230 ${t}`,`\u590D\u5236\u4E86 ${s.copiedFiles} \u4E2A\u6587\u4EF6\uFF0C\u8DF3\u8FC7\u4E86 ${s.skippedFiles} \u4E2A\u6587\u4EF6\uFF0C\u8017\u65F6 ${s.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 faviconManager=factory_index.createPluginFactory(f,u=>typeof u=="string"?{base:u}:u||{});exports.faviconManager=faviconManager;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { BasePluginOptions, PluginFactory } from '../../factory/index.cjs';
|
|
2
|
+
import 'vite';
|
|
3
|
+
import '../../shared/vite-plugin.CLr0ttuO.cjs';
|
|
4
|
+
import '../../shared/vite-plugin.DRRlWY8P.cjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 图标配置项接口
|
|
8
|
+
*
|
|
9
|
+
* @interface Icon
|
|
10
|
+
*/
|
|
11
|
+
interface Icon {
|
|
12
|
+
/**
|
|
13
|
+
* 图标关系类型
|
|
14
|
+
*/
|
|
15
|
+
rel: string;
|
|
16
|
+
/**
|
|
17
|
+
* 图标 URL
|
|
18
|
+
*/
|
|
19
|
+
href: string;
|
|
20
|
+
/**
|
|
21
|
+
* 图标尺寸
|
|
22
|
+
*/
|
|
23
|
+
sizes?: string;
|
|
24
|
+
/**
|
|
25
|
+
* 图标 MIME 类型
|
|
26
|
+
*/
|
|
27
|
+
type?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 图标文件复制配置选项接口
|
|
31
|
+
*
|
|
32
|
+
* @interface CopyOptions
|
|
33
|
+
*/
|
|
34
|
+
interface CopyOptions {
|
|
35
|
+
/**
|
|
36
|
+
* 图标源文件目录,用于复制图标到打包目录
|
|
37
|
+
*
|
|
38
|
+
* @example 'src/assets/icons'
|
|
39
|
+
*/
|
|
40
|
+
sourceDir: string;
|
|
41
|
+
/**
|
|
42
|
+
* 图标目标目录(打包目录),用于复制图标到打包目录
|
|
43
|
+
*
|
|
44
|
+
* @example 'dist/assets/icons'
|
|
45
|
+
*/
|
|
46
|
+
targetDir: string;
|
|
47
|
+
/**
|
|
48
|
+
* 是否覆盖同名文件
|
|
49
|
+
*
|
|
50
|
+
* @default true
|
|
51
|
+
*/
|
|
52
|
+
overwrite?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* 是否支持递归复制
|
|
55
|
+
*
|
|
56
|
+
* @default true
|
|
57
|
+
*/
|
|
58
|
+
recursive?: boolean;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 网站图标管理插件的配置选项接口
|
|
62
|
+
*
|
|
63
|
+
* @interface FaviconManagerOptions
|
|
64
|
+
*/
|
|
65
|
+
interface FaviconManagerOptions extends BasePluginOptions {
|
|
66
|
+
/**
|
|
67
|
+
* 图标文件的基础路径,默认为根路径 '/'
|
|
68
|
+
*
|
|
69
|
+
* @default '/'
|
|
70
|
+
* @example '/assets'
|
|
71
|
+
*/
|
|
72
|
+
base?: string;
|
|
73
|
+
/**
|
|
74
|
+
* 图标的完整 URL,如果提供则优先使用(覆盖 base + favicon.ico)
|
|
75
|
+
*
|
|
76
|
+
* @example 'https://example.com/favicon.ico'
|
|
77
|
+
*/
|
|
78
|
+
url?: string;
|
|
79
|
+
/**
|
|
80
|
+
* 自定义的完整 link 标签 HTML,如果提供则优先使用(覆盖 url 和 base)
|
|
81
|
+
*
|
|
82
|
+
* @example '<link rel="icon" href="/favicon.svg" type="image/svg+xml" />'
|
|
83
|
+
*/
|
|
84
|
+
link?: string;
|
|
85
|
+
/**
|
|
86
|
+
* 自定义图标数组,支持多种图标格式和尺寸
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* [
|
|
90
|
+
* { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' },
|
|
91
|
+
* { rel: 'icon', href: '/favicon-32x32.png', sizes: '32x32', type: 'image/png' },
|
|
92
|
+
* { rel: 'icon', href: '/favicon-16x16.png', sizes: '16x16', type: 'image/png' }
|
|
93
|
+
* ]
|
|
94
|
+
*/
|
|
95
|
+
icons?: Icon[];
|
|
96
|
+
/**
|
|
97
|
+
* 图标文件复制配置选项
|
|
98
|
+
*
|
|
99
|
+
* @remarks
|
|
100
|
+
* 当此对象存在时,才会开启图标文件复制功能
|
|
101
|
+
*/
|
|
102
|
+
copyOptions?: CopyOptions;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 网站图标管理插件
|
|
107
|
+
*
|
|
108
|
+
* @param options - 插件配置选项,可以是字符串形式的 base 路径或完整的配置对象
|
|
109
|
+
* @returns Vite 插件实例
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* // 基本使用
|
|
114
|
+
* faviconManager() // 使用默认配置
|
|
115
|
+
*
|
|
116
|
+
* // 使用字符串配置 base 路径
|
|
117
|
+
* faviconManager('/assets')
|
|
118
|
+
*
|
|
119
|
+
* // 使用完整配置
|
|
120
|
+
* faviconManager({
|
|
121
|
+
* base: '/assets',
|
|
122
|
+
* icons: [
|
|
123
|
+
* { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' },
|
|
124
|
+
* { rel: 'icon', href: '/favicon-32x32.png', sizes: '32x32', type: 'image/png' }
|
|
125
|
+
* ],
|
|
126
|
+
* copyOptions: {
|
|
127
|
+
* sourceDir: 'src/assets/icons',
|
|
128
|
+
* targetDir: 'dist/assets/icons'
|
|
129
|
+
* }
|
|
130
|
+
* })
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @remarks
|
|
134
|
+
* 该插件在构建过程中:
|
|
135
|
+
* 1. 将网站图标(favicon)的 link 标签注入到 HTML 文件的 `<head>` 中
|
|
136
|
+
* 2. 如果配置了 copyOptions,将图标文件复制到目标目录
|
|
137
|
+
*
|
|
138
|
+
* 支持自定义图标链接、图标数组配置以及图标文件复制功能。
|
|
139
|
+
*/
|
|
140
|
+
declare const faviconManager: PluginFactory<FaviconManagerOptions, string | FaviconManagerOptions>;
|
|
141
|
+
|
|
142
|
+
export { faviconManager };
|
|
143
|
+
export type { FaviconManagerOptions, Icon };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { BasePluginOptions, PluginFactory } from '../../factory/index.mjs';
|
|
2
|
+
import 'vite';
|
|
3
|
+
import '../../shared/vite-plugin.CLr0ttuO.mjs';
|
|
4
|
+
import '../../shared/vite-plugin.DRRlWY8P.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 图标配置项接口
|
|
8
|
+
*
|
|
9
|
+
* @interface Icon
|
|
10
|
+
*/
|
|
11
|
+
interface Icon {
|
|
12
|
+
/**
|
|
13
|
+
* 图标关系类型
|
|
14
|
+
*/
|
|
15
|
+
rel: string;
|
|
16
|
+
/**
|
|
17
|
+
* 图标 URL
|
|
18
|
+
*/
|
|
19
|
+
href: string;
|
|
20
|
+
/**
|
|
21
|
+
* 图标尺寸
|
|
22
|
+
*/
|
|
23
|
+
sizes?: string;
|
|
24
|
+
/**
|
|
25
|
+
* 图标 MIME 类型
|
|
26
|
+
*/
|
|
27
|
+
type?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 图标文件复制配置选项接口
|
|
31
|
+
*
|
|
32
|
+
* @interface CopyOptions
|
|
33
|
+
*/
|
|
34
|
+
interface CopyOptions {
|
|
35
|
+
/**
|
|
36
|
+
* 图标源文件目录,用于复制图标到打包目录
|
|
37
|
+
*
|
|
38
|
+
* @example 'src/assets/icons'
|
|
39
|
+
*/
|
|
40
|
+
sourceDir: string;
|
|
41
|
+
/**
|
|
42
|
+
* 图标目标目录(打包目录),用于复制图标到打包目录
|
|
43
|
+
*
|
|
44
|
+
* @example 'dist/assets/icons'
|
|
45
|
+
*/
|
|
46
|
+
targetDir: string;
|
|
47
|
+
/**
|
|
48
|
+
* 是否覆盖同名文件
|
|
49
|
+
*
|
|
50
|
+
* @default true
|
|
51
|
+
*/
|
|
52
|
+
overwrite?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* 是否支持递归复制
|
|
55
|
+
*
|
|
56
|
+
* @default true
|
|
57
|
+
*/
|
|
58
|
+
recursive?: boolean;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 网站图标管理插件的配置选项接口
|
|
62
|
+
*
|
|
63
|
+
* @interface FaviconManagerOptions
|
|
64
|
+
*/
|
|
65
|
+
interface FaviconManagerOptions extends BasePluginOptions {
|
|
66
|
+
/**
|
|
67
|
+
* 图标文件的基础路径,默认为根路径 '/'
|
|
68
|
+
*
|
|
69
|
+
* @default '/'
|
|
70
|
+
* @example '/assets'
|
|
71
|
+
*/
|
|
72
|
+
base?: string;
|
|
73
|
+
/**
|
|
74
|
+
* 图标的完整 URL,如果提供则优先使用(覆盖 base + favicon.ico)
|
|
75
|
+
*
|
|
76
|
+
* @example 'https://example.com/favicon.ico'
|
|
77
|
+
*/
|
|
78
|
+
url?: string;
|
|
79
|
+
/**
|
|
80
|
+
* 自定义的完整 link 标签 HTML,如果提供则优先使用(覆盖 url 和 base)
|
|
81
|
+
*
|
|
82
|
+
* @example '<link rel="icon" href="/favicon.svg" type="image/svg+xml" />'
|
|
83
|
+
*/
|
|
84
|
+
link?: string;
|
|
85
|
+
/**
|
|
86
|
+
* 自定义图标数组,支持多种图标格式和尺寸
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* [
|
|
90
|
+
* { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' },
|
|
91
|
+
* { rel: 'icon', href: '/favicon-32x32.png', sizes: '32x32', type: 'image/png' },
|
|
92
|
+
* { rel: 'icon', href: '/favicon-16x16.png', sizes: '16x16', type: 'image/png' }
|
|
93
|
+
* ]
|
|
94
|
+
*/
|
|
95
|
+
icons?: Icon[];
|
|
96
|
+
/**
|
|
97
|
+
* 图标文件复制配置选项
|
|
98
|
+
*
|
|
99
|
+
* @remarks
|
|
100
|
+
* 当此对象存在时,才会开启图标文件复制功能
|
|
101
|
+
*/
|
|
102
|
+
copyOptions?: CopyOptions;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 网站图标管理插件
|
|
107
|
+
*
|
|
108
|
+
* @param options - 插件配置选项,可以是字符串形式的 base 路径或完整的配置对象
|
|
109
|
+
* @returns Vite 插件实例
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* // 基本使用
|
|
114
|
+
* faviconManager() // 使用默认配置
|
|
115
|
+
*
|
|
116
|
+
* // 使用字符串配置 base 路径
|
|
117
|
+
* faviconManager('/assets')
|
|
118
|
+
*
|
|
119
|
+
* // 使用完整配置
|
|
120
|
+
* faviconManager({
|
|
121
|
+
* base: '/assets',
|
|
122
|
+
* icons: [
|
|
123
|
+
* { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' },
|
|
124
|
+
* { rel: 'icon', href: '/favicon-32x32.png', sizes: '32x32', type: 'image/png' }
|
|
125
|
+
* ],
|
|
126
|
+
* copyOptions: {
|
|
127
|
+
* sourceDir: 'src/assets/icons',
|
|
128
|
+
* targetDir: 'dist/assets/icons'
|
|
129
|
+
* }
|
|
130
|
+
* })
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @remarks
|
|
134
|
+
* 该插件在构建过程中:
|
|
135
|
+
* 1. 将网站图标(favicon)的 link 标签注入到 HTML 文件的 `<head>` 中
|
|
136
|
+
* 2. 如果配置了 copyOptions,将图标文件复制到目标目录
|
|
137
|
+
*
|
|
138
|
+
* 支持自定义图标链接、图标数组配置以及图标文件复制功能。
|
|
139
|
+
*/
|
|
140
|
+
declare const faviconManager: PluginFactory<FaviconManagerOptions, string | FaviconManagerOptions>;
|
|
141
|
+
|
|
142
|
+
export { faviconManager };
|
|
143
|
+
export type { FaviconManagerOptions, Icon };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { BasePluginOptions, PluginFactory } from '../../factory/index.js';
|
|
2
|
+
import 'vite';
|
|
3
|
+
import '../../shared/vite-plugin.CLr0ttuO.js';
|
|
4
|
+
import '../../shared/vite-plugin.DRRlWY8P.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 图标配置项接口
|
|
8
|
+
*
|
|
9
|
+
* @interface Icon
|
|
10
|
+
*/
|
|
11
|
+
interface Icon {
|
|
12
|
+
/**
|
|
13
|
+
* 图标关系类型
|
|
14
|
+
*/
|
|
15
|
+
rel: string;
|
|
16
|
+
/**
|
|
17
|
+
* 图标 URL
|
|
18
|
+
*/
|
|
19
|
+
href: string;
|
|
20
|
+
/**
|
|
21
|
+
* 图标尺寸
|
|
22
|
+
*/
|
|
23
|
+
sizes?: string;
|
|
24
|
+
/**
|
|
25
|
+
* 图标 MIME 类型
|
|
26
|
+
*/
|
|
27
|
+
type?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 图标文件复制配置选项接口
|
|
31
|
+
*
|
|
32
|
+
* @interface CopyOptions
|
|
33
|
+
*/
|
|
34
|
+
interface CopyOptions {
|
|
35
|
+
/**
|
|
36
|
+
* 图标源文件目录,用于复制图标到打包目录
|
|
37
|
+
*
|
|
38
|
+
* @example 'src/assets/icons'
|
|
39
|
+
*/
|
|
40
|
+
sourceDir: string;
|
|
41
|
+
/**
|
|
42
|
+
* 图标目标目录(打包目录),用于复制图标到打包目录
|
|
43
|
+
*
|
|
44
|
+
* @example 'dist/assets/icons'
|
|
45
|
+
*/
|
|
46
|
+
targetDir: string;
|
|
47
|
+
/**
|
|
48
|
+
* 是否覆盖同名文件
|
|
49
|
+
*
|
|
50
|
+
* @default true
|
|
51
|
+
*/
|
|
52
|
+
overwrite?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* 是否支持递归复制
|
|
55
|
+
*
|
|
56
|
+
* @default true
|
|
57
|
+
*/
|
|
58
|
+
recursive?: boolean;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 网站图标管理插件的配置选项接口
|
|
62
|
+
*
|
|
63
|
+
* @interface FaviconManagerOptions
|
|
64
|
+
*/
|
|
65
|
+
interface FaviconManagerOptions extends BasePluginOptions {
|
|
66
|
+
/**
|
|
67
|
+
* 图标文件的基础路径,默认为根路径 '/'
|
|
68
|
+
*
|
|
69
|
+
* @default '/'
|
|
70
|
+
* @example '/assets'
|
|
71
|
+
*/
|
|
72
|
+
base?: string;
|
|
73
|
+
/**
|
|
74
|
+
* 图标的完整 URL,如果提供则优先使用(覆盖 base + favicon.ico)
|
|
75
|
+
*
|
|
76
|
+
* @example 'https://example.com/favicon.ico'
|
|
77
|
+
*/
|
|
78
|
+
url?: string;
|
|
79
|
+
/**
|
|
80
|
+
* 自定义的完整 link 标签 HTML,如果提供则优先使用(覆盖 url 和 base)
|
|
81
|
+
*
|
|
82
|
+
* @example '<link rel="icon" href="/favicon.svg" type="image/svg+xml" />'
|
|
83
|
+
*/
|
|
84
|
+
link?: string;
|
|
85
|
+
/**
|
|
86
|
+
* 自定义图标数组,支持多种图标格式和尺寸
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* [
|
|
90
|
+
* { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' },
|
|
91
|
+
* { rel: 'icon', href: '/favicon-32x32.png', sizes: '32x32', type: 'image/png' },
|
|
92
|
+
* { rel: 'icon', href: '/favicon-16x16.png', sizes: '16x16', type: 'image/png' }
|
|
93
|
+
* ]
|
|
94
|
+
*/
|
|
95
|
+
icons?: Icon[];
|
|
96
|
+
/**
|
|
97
|
+
* 图标文件复制配置选项
|
|
98
|
+
*
|
|
99
|
+
* @remarks
|
|
100
|
+
* 当此对象存在时,才会开启图标文件复制功能
|
|
101
|
+
*/
|
|
102
|
+
copyOptions?: CopyOptions;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 网站图标管理插件
|
|
107
|
+
*
|
|
108
|
+
* @param options - 插件配置选项,可以是字符串形式的 base 路径或完整的配置对象
|
|
109
|
+
* @returns Vite 插件实例
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* // 基本使用
|
|
114
|
+
* faviconManager() // 使用默认配置
|
|
115
|
+
*
|
|
116
|
+
* // 使用字符串配置 base 路径
|
|
117
|
+
* faviconManager('/assets')
|
|
118
|
+
*
|
|
119
|
+
* // 使用完整配置
|
|
120
|
+
* faviconManager({
|
|
121
|
+
* base: '/assets',
|
|
122
|
+
* icons: [
|
|
123
|
+
* { rel: 'icon', href: '/favicon.svg', type: 'image/svg+xml' },
|
|
124
|
+
* { rel: 'icon', href: '/favicon-32x32.png', sizes: '32x32', type: 'image/png' }
|
|
125
|
+
* ],
|
|
126
|
+
* copyOptions: {
|
|
127
|
+
* sourceDir: 'src/assets/icons',
|
|
128
|
+
* targetDir: 'dist/assets/icons'
|
|
129
|
+
* }
|
|
130
|
+
* })
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @remarks
|
|
134
|
+
* 该插件在构建过程中:
|
|
135
|
+
* 1. 将网站图标(favicon)的 link 标签注入到 HTML 文件的 `<head>` 中
|
|
136
|
+
* 2. 如果配置了 copyOptions,将图标文件复制到目标目录
|
|
137
|
+
*
|
|
138
|
+
* 支持自定义图标链接、图标数组配置以及图标文件复制功能。
|
|
139
|
+
*/
|
|
140
|
+
declare const faviconManager: PluginFactory<FaviconManagerOptions, string | FaviconManagerOptions>;
|
|
141
|
+
|
|
142
|
+
export { faviconManager };
|
|
143
|
+
export type { FaviconManagerOptions, Icon };
|
|
@@ -0,0 +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:s=!0,recursive:o=!0}=i;await c(e);const r=await l(e,t,{recursive:o,overwrite:s,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};
|
|
@@ -0,0 +1,35 @@
|
|
|
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"),u=require("path"),s=require("fs");require("../../logger/index.cjs"),require("../../common/object/index.cjs"),require("../../shared/vite-plugin.Bcg6RW2N.cjs"),require("crypto");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,a){const t={},r=e.style||{},o=this.options.metaMapping||{};for(const[n,i]of Object.entries(o))r[n]!==void 0&&(t[i]=r[n]);return this.tabBarPages.has(a)&&(t.isTab=!0),t}parsePageToRoute(e,a=""){const t=a?`/${a}/${e.path}`:`/${e.path}`,r=this.generateRouteName(t),o=this.extractMeta(e,t.replace(/^\//,"")),n={path:t,name:r};return Object.keys(o).length>0&&(n.meta=o),n}parsePagesJson(e){const a=[];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"),a;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)a.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 r of t.pages)a.push(this.parsePageToRoute(r,t.root))}return a}generateTypeDefinitions(){return!this.options.exportTypes||this.options.outputFormat==="js"?"":`
|
|
2
|
+
/**
|
|
3
|
+
* \u8DEF\u7531\u5143\u4FE1\u606F
|
|
4
|
+
*/
|
|
5
|
+
export interface RouteMeta {
|
|
6
|
+
/** \u9875\u9762\u6807\u9898 */
|
|
7
|
+
title?: string
|
|
8
|
+
/** \u662F\u5426\u4E3ATabBar\u9875\u9762 */
|
|
9
|
+
isTab?: boolean
|
|
10
|
+
/** \u662F\u5426\u9700\u8981\u767B\u5F55 */
|
|
11
|
+
requireAuth?: boolean
|
|
12
|
+
/** \u81EA\u5B9A\u4E49\u6269\u5C55\u5B57\u6BB5 */
|
|
13
|
+
[key: string]: unknown
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* \u8DEF\u7531\u914D\u7F6E\u9879
|
|
18
|
+
*/
|
|
19
|
+
export interface RouteConfig {
|
|
20
|
+
/** \u8DEF\u7531\u8DEF\u5F84 */
|
|
21
|
+
path: string
|
|
22
|
+
/** \u8DEF\u7531\u540D\u79F0\uFF08\u7528\u4E8E\u547D\u540D\u8DEF\u7531\u5BFC\u822A\uFF09 */
|
|
23
|
+
name?: string
|
|
24
|
+
/** \u8DEF\u7531\u5143\u4FE1\u606F */
|
|
25
|
+
meta?: RouteMeta
|
|
26
|
+
}
|
|
27
|
+
`}generateFileContent(e){const a=this.generateTypeDefinitions(),t=this.options.outputFormat==="ts",r=JSON.stringify(e,null," ").replace(/"(\w+)":/g,"$1:").replace(/: "([^"]+)"/g,": '$1'");return`${a}
|
|
28
|
+
/**
|
|
29
|
+
* \u8DEF\u7531\u914D\u7F6E\u5217\u8868
|
|
30
|
+
* @description \u7531 pages.json \u81EA\u52A8\u751F\u6210
|
|
31
|
+
*/
|
|
32
|
+
export const routes${t?": RouteConfig[]":""} = ${r}
|
|
33
|
+
|
|
34
|
+
export default routes
|
|
35
|
+
`}async readPagesJson(){const e=u.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 a=await common_fs_index.readFileContent(e),t=common_format_index.stripJsonComments(a);return JSON.parse(t)}catch(a){return this.logger.error(`\u89E3\u6790 pages.json \u5931\u8D25: ${a.message}`),null}}extractExistingRoutes(e){const a=new Map,t=e.match(/export const routes[^=]*=\s*(\[[\s\S]*?\](?=\s*\n|\s*$|\s*\/\/))/);if(!t)return a;try{let r=t[1].replace(/(\w+)(?=\s*:)/g,'"$1"').replace(/'([^']*)'/g,'"$1"').replace(/,\s*([\]\}])/g,"$1");const o=JSON.parse(r);for(const n of o)n.path&&a.set(n.path,n)}catch{this.logger.warn("\u89E3\u6790\u73B0\u6709 routes \u914D\u7F6E\u5931\u8D25\uFF0C\u5C06\u5B8C\u5168\u91CD\u65B0\u751F\u6210")}return a}mergeRoutes(e,a){return e.map(t=>{const r=a.get(t.path);if(!r)return t;const o={};return t.meta&&Object.assign(o,t.meta),r.meta&&Object.assign(o,r.meta),{...r,path:t.path,meta:Object.keys(o).length>0?o:void 0}})}async generateRouterConfig(){const e=await this.readPagesJson();if(!e)return;let a=this.parsePagesJson(e);const t=u.resolve(this.projectRoot,this.options.outputPath);if(this.options.preserveRouteChanges&&s.existsSync(t))try{const o=await common_fs_index.readFileContent(t),n=this.extractExistingRoutes(o);n.size>0&&(a=this.mergeRoutes(a,n),this.logger.info("\u5DF2\u5408\u5E76\u7528\u6237\u5BF9\u8DEF\u7531\u914D\u7F6E\u7684\u4FEE\u6539"))}catch{}const r=this.generateFileContent(a);await common_fs_index.writeFileContent(t,r),this.logger.success(`\u8DEF\u7531\u914D\u7F6E\u6587\u4EF6\u5DF2\u751F\u6210: ${t}`),this.logger.info(`\u5171\u751F\u6210 ${a.length} \u6761\u8DEF\u7531\u914D\u7F6E`)}startWatching(){if(!this.options.watch)return;const e=u.resolve(this.projectRoot,this.options.pagesJsonPath);s.existsSync(e)&&(this.watcher=s.watch(e,async a=>{a==="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 a=>{this.projectRoot=a.root,await this.safeExecute(()=>this.generateRouterConfig(),"\u751F\u6210\u8DEF\u7531\u914D\u7F6E"),a.command==="serve"&&this.startWatching()}}destroy(){super.destroy(),this.stopWatching()}}const generateRouter=factory_index.createPluginFactory(R);exports.generateRouter=generateRouter;
|