@meng-xi/vite-plugin 0.0.2 → 0.0.3
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 +150 -150
- package/README.md +84 -84
- package/dist/common/index.cjs +1 -0
- package/dist/common/index.d.cts +109 -0
- package/dist/common/index.d.mts +109 -0
- package/dist/common/index.d.ts +109 -0
- package/dist/common/index.mjs +1 -0
- package/dist/factory/index.cjs +1 -0
- package/dist/factory/index.d.cts +260 -0
- package/dist/factory/index.d.mts +260 -0
- package/dist/factory/index.d.ts +260 -0
- package/dist/factory/index.mjs +1 -0
- package/dist/index.cjs +1 -5
- package/dist/index.d.cts +7 -241
- package/dist/index.d.mts +7 -241
- package/dist/index.d.ts +7 -241
- package/dist/index.mjs +1 -5
- package/dist/logger/index.cjs +1 -0
- package/dist/logger/index.d.cts +1 -0
- package/dist/logger/index.d.mts +1 -0
- package/dist/logger/index.d.ts +1 -0
- package/dist/logger/index.mjs +1 -0
- package/dist/plugins/index.cjs +1 -0
- package/dist/plugins/index.d.cts +210 -0
- package/dist/plugins/index.d.mts +210 -0
- package/dist/plugins/index.d.ts +210 -0
- package/dist/plugins/index.mjs +1 -0
- package/dist/shared/vite-plugin.B3PARlU9.d.cts +119 -0
- package/dist/shared/vite-plugin.B3PARlU9.d.mts +119 -0
- package/dist/shared/vite-plugin.B3PARlU9.d.ts +119 -0
- package/dist/shared/vite-plugin.BT1oHRKK.cjs +1 -0
- package/dist/shared/vite-plugin.BTKhc7n7.cjs +3 -0
- package/dist/shared/vite-plugin.BZqhBDYR.mjs +1 -0
- package/dist/shared/vite-plugin.Bn8mcCzy.cjs +3 -0
- package/dist/shared/vite-plugin.CY2ydccp.mjs +3 -0
- package/dist/shared/vite-plugin.CiHfwMiN.d.cts +91 -0
- package/dist/shared/vite-plugin.CiHfwMiN.d.mts +91 -0
- package/dist/shared/vite-plugin.CiHfwMiN.d.ts +91 -0
- package/dist/shared/vite-plugin.ClHiVXD6.mjs +1 -0
- package/dist/shared/vite-plugin.DSRKYuae.mjs +3 -0
- package/dist/shared/vite-plugin.DrSzERYS.cjs +1 -0
- package/dist/shared/vite-plugin.UkE7CdSe.d.cts +43 -0
- package/dist/shared/vite-plugin.UkE7CdSe.d.mts +43 -0
- package/dist/shared/vite-plugin.UkE7CdSe.d.ts +43 -0
- package/package.json +72 -53
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export { V as Validator } from '../shared/vite-plugin.CiHfwMiN.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 复制操作的选项接口
|
|
5
|
+
*/
|
|
6
|
+
interface CopyOptions {
|
|
7
|
+
/**
|
|
8
|
+
* 是否支持递归复制
|
|
9
|
+
*/
|
|
10
|
+
recursive: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* 是否覆盖同名文件
|
|
13
|
+
*/
|
|
14
|
+
overwrite: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 是否启用增量复制
|
|
17
|
+
*/
|
|
18
|
+
incremental?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* 并行处理的最大文件数
|
|
21
|
+
*/
|
|
22
|
+
parallelLimit?: number;
|
|
23
|
+
/**
|
|
24
|
+
* 是否跳过空目录
|
|
25
|
+
*/
|
|
26
|
+
skipEmptyDirs?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 复制结果接口
|
|
30
|
+
*/
|
|
31
|
+
interface CopyResult {
|
|
32
|
+
/**
|
|
33
|
+
* 复制的文件数量
|
|
34
|
+
*/
|
|
35
|
+
copiedFiles: number;
|
|
36
|
+
/**
|
|
37
|
+
* 跳过的文件数量
|
|
38
|
+
*/
|
|
39
|
+
skippedFiles: number;
|
|
40
|
+
/**
|
|
41
|
+
* 复制的目录数量
|
|
42
|
+
*/
|
|
43
|
+
copiedDirs: number;
|
|
44
|
+
/**
|
|
45
|
+
* 总执行时间(毫秒)
|
|
46
|
+
*/
|
|
47
|
+
executionTime: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 检查源文件是否存在
|
|
52
|
+
* @param sourcePath 源文件路径
|
|
53
|
+
* @throws 当源文件不存在或无法访问时抛出异常
|
|
54
|
+
*/
|
|
55
|
+
declare function checkSourceExists(sourcePath: string): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* 创建目标目录
|
|
58
|
+
* @param targetPath 目标目录路径
|
|
59
|
+
* @throws 当无法创建目标目录时抛出异常
|
|
60
|
+
*/
|
|
61
|
+
declare function ensureTargetDir(targetPath: string): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* 读取目录内容
|
|
64
|
+
* @param dirPath 目录路径
|
|
65
|
+
* @param recursive 是否递归读取
|
|
66
|
+
* @returns 文件和目录列表
|
|
67
|
+
*/
|
|
68
|
+
declare function readDirRecursive(dirPath: string, recursive: boolean): Promise<string[]>;
|
|
69
|
+
/**
|
|
70
|
+
* 检查文件是否需要更新
|
|
71
|
+
* @param sourceFile 源文件路径
|
|
72
|
+
* @param targetFile 目标文件路径
|
|
73
|
+
* @returns 是否需要更新
|
|
74
|
+
*/
|
|
75
|
+
declare function shouldUpdateFile(sourceFile: string, targetFile: string): Promise<boolean>;
|
|
76
|
+
/**
|
|
77
|
+
* 执行文件复制操作
|
|
78
|
+
* @param sourcePath 源文件或目录路径
|
|
79
|
+
* @param targetPath 目标文件或目录路径
|
|
80
|
+
* @param options 复制选项
|
|
81
|
+
* @returns 复制结果
|
|
82
|
+
* @throws 当复制过程中出现错误时抛出异常
|
|
83
|
+
*/
|
|
84
|
+
declare function copySourceToTarget(sourcePath: string, targetPath: string, options: CopyOptions): Promise<CopyResult>;
|
|
85
|
+
/**
|
|
86
|
+
* 写入文件内容
|
|
87
|
+
* @param filePath 文件路径
|
|
88
|
+
* @param content 文件内容
|
|
89
|
+
* @throws 当写入过程中出现错误时抛出异常
|
|
90
|
+
*/
|
|
91
|
+
declare function writeFileContent(filePath: string, content: string): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* 同步读取文件内容
|
|
94
|
+
* @param filePath 文件路径
|
|
95
|
+
* @returns 文件内容字符串
|
|
96
|
+
* @throws 当读取过程中出现错误时抛出异常
|
|
97
|
+
*/
|
|
98
|
+
declare function readFileSync(filePath: string): string;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 深度合并对象
|
|
102
|
+
*
|
|
103
|
+
* @param target 目标对象
|
|
104
|
+
* @param sources 源对象列表
|
|
105
|
+
* @returns 合并后的对象
|
|
106
|
+
*/
|
|
107
|
+
declare function deepMerge<T extends Record<string, any>>(...sources: Partial<T>[]): T;
|
|
108
|
+
|
|
109
|
+
export { checkSourceExists, copySourceToTarget, deepMerge, ensureTargetDir, readDirRecursive, readFileSync, shouldUpdateFile, writeFileContent };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export { V as Validator } from '../shared/vite-plugin.CiHfwMiN.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 复制操作的选项接口
|
|
5
|
+
*/
|
|
6
|
+
interface CopyOptions {
|
|
7
|
+
/**
|
|
8
|
+
* 是否支持递归复制
|
|
9
|
+
*/
|
|
10
|
+
recursive: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* 是否覆盖同名文件
|
|
13
|
+
*/
|
|
14
|
+
overwrite: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 是否启用增量复制
|
|
17
|
+
*/
|
|
18
|
+
incremental?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* 并行处理的最大文件数
|
|
21
|
+
*/
|
|
22
|
+
parallelLimit?: number;
|
|
23
|
+
/**
|
|
24
|
+
* 是否跳过空目录
|
|
25
|
+
*/
|
|
26
|
+
skipEmptyDirs?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 复制结果接口
|
|
30
|
+
*/
|
|
31
|
+
interface CopyResult {
|
|
32
|
+
/**
|
|
33
|
+
* 复制的文件数量
|
|
34
|
+
*/
|
|
35
|
+
copiedFiles: number;
|
|
36
|
+
/**
|
|
37
|
+
* 跳过的文件数量
|
|
38
|
+
*/
|
|
39
|
+
skippedFiles: number;
|
|
40
|
+
/**
|
|
41
|
+
* 复制的目录数量
|
|
42
|
+
*/
|
|
43
|
+
copiedDirs: number;
|
|
44
|
+
/**
|
|
45
|
+
* 总执行时间(毫秒)
|
|
46
|
+
*/
|
|
47
|
+
executionTime: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 检查源文件是否存在
|
|
52
|
+
* @param sourcePath 源文件路径
|
|
53
|
+
* @throws 当源文件不存在或无法访问时抛出异常
|
|
54
|
+
*/
|
|
55
|
+
declare function checkSourceExists(sourcePath: string): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* 创建目标目录
|
|
58
|
+
* @param targetPath 目标目录路径
|
|
59
|
+
* @throws 当无法创建目标目录时抛出异常
|
|
60
|
+
*/
|
|
61
|
+
declare function ensureTargetDir(targetPath: string): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* 读取目录内容
|
|
64
|
+
* @param dirPath 目录路径
|
|
65
|
+
* @param recursive 是否递归读取
|
|
66
|
+
* @returns 文件和目录列表
|
|
67
|
+
*/
|
|
68
|
+
declare function readDirRecursive(dirPath: string, recursive: boolean): Promise<string[]>;
|
|
69
|
+
/**
|
|
70
|
+
* 检查文件是否需要更新
|
|
71
|
+
* @param sourceFile 源文件路径
|
|
72
|
+
* @param targetFile 目标文件路径
|
|
73
|
+
* @returns 是否需要更新
|
|
74
|
+
*/
|
|
75
|
+
declare function shouldUpdateFile(sourceFile: string, targetFile: string): Promise<boolean>;
|
|
76
|
+
/**
|
|
77
|
+
* 执行文件复制操作
|
|
78
|
+
* @param sourcePath 源文件或目录路径
|
|
79
|
+
* @param targetPath 目标文件或目录路径
|
|
80
|
+
* @param options 复制选项
|
|
81
|
+
* @returns 复制结果
|
|
82
|
+
* @throws 当复制过程中出现错误时抛出异常
|
|
83
|
+
*/
|
|
84
|
+
declare function copySourceToTarget(sourcePath: string, targetPath: string, options: CopyOptions): Promise<CopyResult>;
|
|
85
|
+
/**
|
|
86
|
+
* 写入文件内容
|
|
87
|
+
* @param filePath 文件路径
|
|
88
|
+
* @param content 文件内容
|
|
89
|
+
* @throws 当写入过程中出现错误时抛出异常
|
|
90
|
+
*/
|
|
91
|
+
declare function writeFileContent(filePath: string, content: string): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* 同步读取文件内容
|
|
94
|
+
* @param filePath 文件路径
|
|
95
|
+
* @returns 文件内容字符串
|
|
96
|
+
* @throws 当读取过程中出现错误时抛出异常
|
|
97
|
+
*/
|
|
98
|
+
declare function readFileSync(filePath: string): string;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 深度合并对象
|
|
102
|
+
*
|
|
103
|
+
* @param target 目标对象
|
|
104
|
+
* @param sources 源对象列表
|
|
105
|
+
* @returns 合并后的对象
|
|
106
|
+
*/
|
|
107
|
+
declare function deepMerge<T extends Record<string, any>>(...sources: Partial<T>[]): T;
|
|
108
|
+
|
|
109
|
+
export { checkSourceExists, copySourceToTarget, deepMerge, ensureTargetDir, readDirRecursive, readFileSync, shouldUpdateFile, writeFileContent };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{c as checkSourceExists,a as copySourceToTarget,e as ensureTargetDir,r as readDirRecursive,b as readFileSync,s as shouldUpdateFile,w as writeFileContent}from"../shared/vite-plugin.BZqhBDYR.mjs";export{V as Validator,d as deepMerge}from"../shared/vite-plugin.DSRKYuae.mjs";import"fs";import"path";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const index=require("../shared/vite-plugin.BT1oHRKK.cjs");require("../logger/index.cjs"),require("fs"),require("path"),require("../shared/vite-plugin.BTKhc7n7.cjs"),exports.BasePlugin=index.BasePlugin,exports.createPluginFactory=index.createPluginFactory;
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { ResolvedConfig, Plugin } from 'vite';
|
|
2
|
+
import { B as BasePluginOptions, O as OptionsNormalizer, P as PluginFactory } from '../shared/vite-plugin.UkE7CdSe.cjs';
|
|
3
|
+
import { P as PluginLogger, a as LoggerOptions } from '../shared/vite-plugin.B3PARlU9.cjs';
|
|
4
|
+
import { V as Validator } from '../shared/vite-plugin.CiHfwMiN.cjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 基础插件抽象类,提供插件开发的核心功能和生命周期管理
|
|
8
|
+
*
|
|
9
|
+
* @class BasePlugin
|
|
10
|
+
* @template T - 插件配置类型,必须继承自 BasePluginOptions
|
|
11
|
+
* @abstract
|
|
12
|
+
* @description 该类是所有插件的基类,提供了插件配置管理、日志记录、生命周期管理等核心功能
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* class MyPlugin extends BasePlugin<MyPluginOptions> {
|
|
16
|
+
* protected getPluginName() {
|
|
17
|
+
* return 'my-plugin'
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* protected addPluginHooks(plugin: Plugin) {
|
|
21
|
+
* // 添加插件钩子
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare abstract class BasePlugin<T extends BasePluginOptions = BasePluginOptions> {
|
|
27
|
+
/**
|
|
28
|
+
* 插件配置
|
|
29
|
+
*
|
|
30
|
+
* @protected
|
|
31
|
+
* @description 插件配置,包含插件的运行参数和选项
|
|
32
|
+
*/
|
|
33
|
+
protected options: Required<T>;
|
|
34
|
+
/**
|
|
35
|
+
* 插件日志记录器
|
|
36
|
+
*
|
|
37
|
+
* @protected
|
|
38
|
+
* @description 插件日志记录器,用于记录插件运行时的日志信息
|
|
39
|
+
*/
|
|
40
|
+
protected logger: PluginLogger;
|
|
41
|
+
/**
|
|
42
|
+
* 插件配置验证器
|
|
43
|
+
*
|
|
44
|
+
* @protected
|
|
45
|
+
* @description 插件配置验证器,用于验证插件配置参数是否符合要求
|
|
46
|
+
*/
|
|
47
|
+
protected validator: Validator<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Vite 配置
|
|
50
|
+
*
|
|
51
|
+
* @protected
|
|
52
|
+
* @description Vite 配置,包含 Vite 构建的运行参数和选项
|
|
53
|
+
*/
|
|
54
|
+
protected viteConfig: ResolvedConfig | null;
|
|
55
|
+
/**
|
|
56
|
+
* 插件构造函数
|
|
57
|
+
*
|
|
58
|
+
* @param options 插件配置
|
|
59
|
+
* @param loggerConfig 日志配置,可选
|
|
60
|
+
*
|
|
61
|
+
* @protected
|
|
62
|
+
* @description 插件构造函数,初始化插件配置、日志记录器和验证插件参数
|
|
63
|
+
*/
|
|
64
|
+
constructor(options: T, loggerConfig?: LoggerOptions);
|
|
65
|
+
/**
|
|
66
|
+
* 获取插件的默认配置选项
|
|
67
|
+
*
|
|
68
|
+
* @protected
|
|
69
|
+
* @abstract
|
|
70
|
+
* @returns {Partial<T>} 插件特定的默认配置
|
|
71
|
+
* @description 子类必须实现此方法,以提供插件特定的默认配置值
|
|
72
|
+
*/
|
|
73
|
+
protected abstract getDefaultOptions(): Partial<T>;
|
|
74
|
+
/**
|
|
75
|
+
* 合并插件配置,将用户提供的配置与默认配置合并
|
|
76
|
+
*
|
|
77
|
+
* @protected
|
|
78
|
+
* @template T - 插件配置类型,必须继承自 BasePluginOptions
|
|
79
|
+
* @param {T} options - 用户提供的插件配置
|
|
80
|
+
* @returns {Required<T>} 合并后的完整插件配置,包含所有必填字段
|
|
81
|
+
* @description 将用户提供的配置与基础默认值、插件特定默认值进行深度合并,确保所有必填字段都有值
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const userOptions = { enabled: false }
|
|
85
|
+
* const mergedOptions = this.mergeOptions(userOptions)
|
|
86
|
+
* // mergedOptions 将包含基础默认值和插件特定默认值
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
protected mergeOptions(options: T): Required<T>;
|
|
90
|
+
/**
|
|
91
|
+
* 初始化日志记录器
|
|
92
|
+
*
|
|
93
|
+
* @private
|
|
94
|
+
* @param {LoggerOptions} loggerConfig - 日志配置对象,可选
|
|
95
|
+
* @returns {PluginLogger} 插件日志代理对象,用于记录插件日志
|
|
96
|
+
* @description 使用单例 Logger 创建插件特定的日志代理对象
|
|
97
|
+
*/
|
|
98
|
+
private initLogger;
|
|
99
|
+
/**
|
|
100
|
+
* 验证插件配置参数,确保配置符合要求
|
|
101
|
+
*
|
|
102
|
+
* @protected
|
|
103
|
+
* @virtual
|
|
104
|
+
* @returns {void} 无返回值
|
|
105
|
+
* @throws {Error} 如果配置参数无效,抛出包含错误信息的异常
|
|
106
|
+
* @description 该方法在插件初始化时被调用,用于验证插件配置的有效性。子类可以重写此方法以添加自定义验证逻辑
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* protected validateOptions(): void {
|
|
110
|
+
* if (!this.options.sourceDir) {
|
|
111
|
+
* throw new Error('sourceDir 是必填项')
|
|
112
|
+
* }
|
|
113
|
+
*
|
|
114
|
+
* if (!this.options.targetDir) {
|
|
115
|
+
* throw new Error('targetDir 是必填项')
|
|
116
|
+
* }
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
protected validateOptions(): void;
|
|
121
|
+
/**
|
|
122
|
+
* 获取插件名称
|
|
123
|
+
*
|
|
124
|
+
* @protected
|
|
125
|
+
* @returns {string} 插件的名称,用于 Vite 插件系统识别
|
|
126
|
+
*/
|
|
127
|
+
protected abstract getPluginName(): string;
|
|
128
|
+
/**
|
|
129
|
+
* 获取插件执行时机
|
|
130
|
+
*
|
|
131
|
+
* @protected
|
|
132
|
+
* @returns {Plugin['enforce']} 插件的执行时机,可选值为 'pre'、'post' 或 undefined
|
|
133
|
+
* @description 'post' 表示插件在 Vite 构建后期执行
|
|
134
|
+
*/
|
|
135
|
+
protected getEnforce(): Plugin['enforce'];
|
|
136
|
+
/**
|
|
137
|
+
* 处理配置解析完成事件
|
|
138
|
+
*
|
|
139
|
+
* @param config 解析后的 Vite 配置
|
|
140
|
+
*
|
|
141
|
+
* @protected
|
|
142
|
+
* @description 处理 Vite 配置解析完成事件,将解析后的配置存储到插件实例中
|
|
143
|
+
*/
|
|
144
|
+
protected onConfigResolved(config: ResolvedConfig): void;
|
|
145
|
+
/**
|
|
146
|
+
* 添加插件钩子到 Vite 插件对象
|
|
147
|
+
*
|
|
148
|
+
* @protected
|
|
149
|
+
* @abstract
|
|
150
|
+
* @param {Plugin} plugin - Vite 插件对象,用于添加钩子
|
|
151
|
+
* @returns {void} 无返回值
|
|
152
|
+
* @description 添加插件钩子到 Vite 插件对象,用于在构建过程中执行插件逻辑
|
|
153
|
+
*/
|
|
154
|
+
protected abstract addPluginHooks(plugin: Plugin): void;
|
|
155
|
+
/**
|
|
156
|
+
* 安全执行同步函数,自动处理执行过程中可能出现的错误
|
|
157
|
+
*
|
|
158
|
+
* @protected
|
|
159
|
+
* @template T - 函数的返回值类型
|
|
160
|
+
* @param {() => T} fn - 要执行的同步函数
|
|
161
|
+
* @param {string} context - 执行上下文描述,用于错误日志记录
|
|
162
|
+
* @returns {T | undefined} 函数的执行结果,如果执行过程中发生错误,根据错误策略返回 undefined 或抛出错误
|
|
163
|
+
* @description 该方法封装了同步函数的执行,自动处理可能出现的错误,根据插件配置的 errorStrategy 决定如何处理错误
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* // 安全执行同步操作
|
|
167
|
+
* const result = this.safeExecuteSync(() => {
|
|
168
|
+
* return someSyncOperation()
|
|
169
|
+
* }, '执行同步操作')
|
|
170
|
+
*
|
|
171
|
+
* // 如果 someSyncOperation() 抛出错误,会根据 errorStrategy 处理
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
protected safeExecuteSync<T>(fn: () => T, context: string): T | undefined;
|
|
175
|
+
/**
|
|
176
|
+
* 安全执行异步函数,自动处理执行过程中可能出现的错误
|
|
177
|
+
*
|
|
178
|
+
* @protected
|
|
179
|
+
* @async
|
|
180
|
+
* @template T - 异步函数的返回值类型
|
|
181
|
+
* @param {() => Promise<T>} fn - 要执行的异步函数
|
|
182
|
+
* @param {string} context - 执行上下文描述,用于错误日志记录
|
|
183
|
+
* @returns {Promise<T | undefined>} 异步函数的执行结果,如果执行过程中发生错误,根据错误策略返回 undefined 或抛出错误
|
|
184
|
+
* @description 该方法封装了异步函数的执行,自动处理可能出现的错误,根据插件配置的 errorStrategy 决定如何处理错误
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* // 安全执行异步操作
|
|
188
|
+
* const result = await this.safeExecute(async () => {
|
|
189
|
+
* return await someAsyncOperation()
|
|
190
|
+
* }, '执行异步操作')
|
|
191
|
+
*
|
|
192
|
+
* // 如果 someAsyncOperation() 抛出错误,会根据 errorStrategy 处理
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
protected safeExecute<T>(fn: () => Promise<T>, context: string): Promise<T | undefined>;
|
|
196
|
+
/**
|
|
197
|
+
* 处理插件执行过程中出现的错误,根据配置的错误策略决定如何处理
|
|
198
|
+
*
|
|
199
|
+
* @protected
|
|
200
|
+
* @template T - 返回值类型,仅当错误策略为 'log' 或 'ignore' 时返回 undefined
|
|
201
|
+
* @param {unknown} error - 捕获到的错误对象
|
|
202
|
+
* @param {string} context - 错误发生的上下文描述,用于错误日志记录
|
|
203
|
+
* @returns {T | undefined} 当错误策略为 'log' 或 'ignore' 时返回 undefined,否则抛出错误
|
|
204
|
+
* @description 根据插件配置的 errorStrategy 处理错误:
|
|
205
|
+
* - 'throw': 记录错误日志并抛出错误,中断执行
|
|
206
|
+
* - 'log': 记录错误日志但不抛出错误,继续执行
|
|
207
|
+
* - 'ignore': 记录错误日志但不抛出错误,继续执行
|
|
208
|
+
* - 默认:记录错误日志并抛出错误
|
|
209
|
+
* @example
|
|
210
|
+
* ```typescript
|
|
211
|
+
* // 当 errorStrategy 为 'throw' 时
|
|
212
|
+
* this.handleError(new Error('测试错误'), '测试上下文') // 记录错误日志并抛出错误
|
|
213
|
+
*
|
|
214
|
+
* // 当 errorStrategy 为 'log' 时
|
|
215
|
+
* this.handleError(new Error('测试错误'), '测试上下文') // 记录错误日志并返回 undefined
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
protected handleError<T>(error: unknown, context: string): T | undefined;
|
|
219
|
+
/**
|
|
220
|
+
* 将插件实例转换为 Vite 插件对象,用于 Vite 构建系统
|
|
221
|
+
*
|
|
222
|
+
* @public
|
|
223
|
+
* @returns {Plugin} Vite 插件对象,包含插件名称、执行时机和各种钩子函数
|
|
224
|
+
* @description 该方法创建并返回一个符合 Vite 插件规范的对象,设置了插件的基本信息和 configResolved 钩子,然后调用 addPluginHooks 方法添加插件特定的钩子
|
|
225
|
+
* @example
|
|
226
|
+
* ```typescript
|
|
227
|
+
* // 创建插件实例
|
|
228
|
+
* const pluginInstance = new MyPlugin(options)
|
|
229
|
+
*
|
|
230
|
+
* // 转换为 Vite 插件
|
|
231
|
+
* const vitePlugin = pluginInstance.toPlugin()
|
|
232
|
+
*
|
|
233
|
+
* // 导出 Vite 插件
|
|
234
|
+
* export const myPlugin = vitePlugin
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
toPlugin(): Plugin;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* 创建插件工厂函数,用于生成 Vite 插件实例
|
|
241
|
+
*
|
|
242
|
+
* @template T - 插件配置类型,必须继承自 BasePluginOptions
|
|
243
|
+
* @template P - 插件实例类型,必须继承自 BasePlugin<T>
|
|
244
|
+
* @template R - 原始配置类型
|
|
245
|
+
* @param {new (options: T, loggerConfig?: LoggerOptions) => P} PluginClass - 插件类构造函数
|
|
246
|
+
* @param {OptionsNormalizer<T, R>} [normalizer] - 选项标准化器,可选
|
|
247
|
+
* @returns {PluginFactory<T, R>} 插件工厂函数,接收插件配置并返回 Vite 插件实例
|
|
248
|
+
* @description 该函数创建一个插件工厂,用于生成 Vite 插件实例。工厂函数接收插件配置,支持可选的标准化器,创建插件实例,转换为 Vite 插件对象,并在插件对象上添加对原始插件实例的引用
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* // 基本使用
|
|
252
|
+
* const myPluginFactory = createPluginFactory(MyPlugin)
|
|
253
|
+
*
|
|
254
|
+
* // 带标准化器的使用(支持字符串或对象)
|
|
255
|
+
* const myPluginWithNormalizer = createPluginFactory(MyPlugin, (opt) => typeof opt === 'string' ? { path: opt } : opt)
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
declare function createPluginFactory<T extends BasePluginOptions, P extends BasePlugin<T>, R = T>(PluginClass: new (options: T, loggerConfig?: LoggerOptions) => P, normalizer?: OptionsNormalizer<T, R>): PluginFactory<T, R>;
|
|
259
|
+
|
|
260
|
+
export { BasePlugin, createPluginFactory };
|