vite-plugin-performance 2.0.7 → 2.0.9
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/dist/index.d.ts +48 -47
- package/dist/index.mjs +122 -97
- package/package.json +4 -4
- package/dist/index.d.mts +0 -77
package/dist/index.d.ts
CHANGED
|
@@ -1,77 +1,78 @@
|
|
|
1
|
-
import { Plugin } from
|
|
2
|
-
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
//#region src/types.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* @description Vite 插件 hook 名称
|
|
5
5
|
*/
|
|
6
|
-
type PluginHookName = Extract<keyof Plugin, string>;
|
|
6
|
+
export type PluginHookName = Extract<keyof Plugin, string>;
|
|
7
7
|
/**
|
|
8
8
|
* @description hook 执行上下文
|
|
9
9
|
*/
|
|
10
|
-
interface HookExecutionContext {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
export interface HookExecutionContext {
|
|
11
|
+
pluginName: string;
|
|
12
|
+
hookName: PluginHookName;
|
|
13
|
+
args: unknown[];
|
|
14
|
+
duration: number;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
17
|
* @description 日志输出函数
|
|
18
18
|
*/
|
|
19
|
-
type HookLogger = (message: string, context: HookExecutionContext) => void;
|
|
19
|
+
export type HookLogger = (message: string, context: HookExecutionContext) => void;
|
|
20
20
|
/**
|
|
21
21
|
* @description 日志格式化函数
|
|
22
22
|
*/
|
|
23
|
-
type HookFormatter = (context: HookExecutionContext) => string;
|
|
23
|
+
export type HookFormatter = (context: HookExecutionContext) => string;
|
|
24
24
|
/**
|
|
25
25
|
* @description 自定义计时函数
|
|
26
26
|
*/
|
|
27
|
-
type HookClock = () => number;
|
|
28
|
-
interface WrapPluginOptions {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
27
|
+
export type HookClock = () => number;
|
|
28
|
+
export interface WrapPluginOptions {
|
|
29
|
+
/** 仅包裹这些 hooks;传入 `all` 表示包裹插件上的所有函数 hook。 */
|
|
30
|
+
hooks?: PluginHookName[] | 'all';
|
|
31
|
+
/** 触发日志/通知的最小耗时(毫秒)。 */
|
|
32
|
+
threshold?: number;
|
|
33
|
+
/** 关闭默认日志输出。 */
|
|
34
|
+
silent?: boolean;
|
|
35
|
+
/** 自定义日志函数,默认使用 `console.log`。 */
|
|
36
|
+
logger?: HookLogger;
|
|
37
|
+
/** 自定义日志格式化函数。 */
|
|
38
|
+
formatter?: HookFormatter;
|
|
39
|
+
/** hook 执行完成后的回调。 */
|
|
40
|
+
onHookExecution?: (context: HookExecutionContext) => void;
|
|
41
|
+
/** 自定义高精度计时函数。 */
|
|
42
|
+
clock?: HookClock;
|
|
43
|
+
/** @deprecated 兼容旧拼写 */
|
|
44
|
+
slient?: boolean;
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
47
47
|
* @description 解析后的插件配置
|
|
48
48
|
*/
|
|
49
|
-
interface ResolvedWrapPluginOptions {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
export interface ResolvedWrapPluginOptions {
|
|
50
|
+
hooks: PluginHookName[] | 'all';
|
|
51
|
+
threshold: number;
|
|
52
|
+
silent: boolean;
|
|
53
|
+
logger: HookLogger;
|
|
54
|
+
formatter: HookFormatter;
|
|
55
|
+
onHookExecution?: (context: HookExecutionContext) => void;
|
|
56
|
+
clock: HookClock;
|
|
57
57
|
}
|
|
58
|
-
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/constants.d.ts
|
|
59
60
|
/**
|
|
60
61
|
* @description 默认需要包裹的插件 hooks
|
|
61
62
|
*/
|
|
62
|
-
declare const DEFAULT_PLUGIN_HOOKS: PluginHookName[];
|
|
63
|
+
export declare const DEFAULT_PLUGIN_HOOKS: PluginHookName[];
|
|
63
64
|
/**
|
|
64
65
|
* @description 默认耗时阈值(毫秒)
|
|
65
66
|
*/
|
|
66
|
-
declare const DEFAULT_THRESHOLD = 0;
|
|
67
|
-
|
|
67
|
+
export declare const DEFAULT_THRESHOLD = 0;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/options.d.ts
|
|
68
70
|
/**
|
|
69
71
|
* @description 解析插件配置并补全默认值
|
|
70
72
|
*/
|
|
71
|
-
declare function resolveOptions(options?: Partial<WrapPluginOptions>): ResolvedWrapPluginOptions;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
declare function wrapPlugin(maybePlugin: Plugin
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
export type { HookClock, HookExecutionContext, HookFormatter, HookLogger, PluginHookName, ResolvedWrapPluginOptions, WrapPluginOptions };
|
|
73
|
+
export declare function resolveOptions(options?: Partial<WrapPluginOptions>): ResolvedWrapPluginOptions;
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/wrapPlugin.d.ts
|
|
76
|
+
export declare function wrapPlugin(maybePlugin: Plugin, options?: Partial<WrapPluginOptions>): Plugin;
|
|
77
|
+
export declare function wrapPlugin(maybePlugin: Plugin[], options?: Partial<WrapPluginOptions>): Plugin[];
|
|
78
|
+
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -1,122 +1,147 @@
|
|
|
1
|
+
//#region src/constants.ts
|
|
2
|
+
/**
|
|
3
|
+
* @description 默认需要包裹的插件 hooks
|
|
4
|
+
*/
|
|
1
5
|
const DEFAULT_PLUGIN_HOOKS = [
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
"options",
|
|
7
|
+
"config",
|
|
8
|
+
"configResolved",
|
|
9
|
+
"configureServer",
|
|
10
|
+
"buildStart",
|
|
11
|
+
"resolveId",
|
|
12
|
+
"load",
|
|
13
|
+
"transform",
|
|
14
|
+
"buildEnd",
|
|
15
|
+
"generateBundle",
|
|
16
|
+
"renderChunk",
|
|
17
|
+
"writeBundle"
|
|
14
18
|
];
|
|
19
|
+
/**
|
|
20
|
+
* @description 默认耗时阈值(毫秒)
|
|
21
|
+
*/
|
|
15
22
|
const DEFAULT_THRESHOLD = 0;
|
|
23
|
+
/**
|
|
24
|
+
* @description 未命名插件的默认名称
|
|
25
|
+
*/
|
|
16
26
|
const ANONYMOUS_PLUGIN_NAME = "anonymous-plugin";
|
|
27
|
+
/**
|
|
28
|
+
* @description 默认日志格式化器
|
|
29
|
+
*/
|
|
17
30
|
const DEFAULT_FORMATTER = ({ pluginName, hookName, duration }) => {
|
|
18
|
-
|
|
19
|
-
const formattedDuration = duration.toFixed(2).padStart(7);
|
|
20
|
-
return `[${pluginName}] ${paddedHook} \u23F1 ${formattedDuration} ms`;
|
|
31
|
+
return `[${pluginName}] ${hookName.padEnd(20)} ⏱ ${duration.toFixed(2).padStart(7)} ms`;
|
|
21
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* @description 默认日志输出
|
|
35
|
+
*/
|
|
22
36
|
const DEFAULT_LOGGER = (message) => {
|
|
23
|
-
|
|
37
|
+
console.log(message);
|
|
24
38
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const defaultClock =
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/options.ts
|
|
41
|
+
const defaultClock = typeof globalThis !== "undefined" && typeof globalThis.performance?.now === "function" ? () => globalThis.performance.now() : () => Date.now();
|
|
42
|
+
/**
|
|
43
|
+
* @description 解析插件配置并补全默认值
|
|
44
|
+
*/
|
|
28
45
|
function resolveOptions(options = {}) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
hooks,
|
|
40
|
-
threshold,
|
|
41
|
-
silent,
|
|
42
|
-
logger,
|
|
43
|
-
formatter,
|
|
44
|
-
onHookExecution,
|
|
45
|
-
clock
|
|
46
|
-
};
|
|
46
|
+
const { hooks = DEFAULT_PLUGIN_HOOKS, threshold = 0, silent = options.slient ?? false, logger = DEFAULT_LOGGER, formatter = DEFAULT_FORMATTER, onHookExecution, clock = defaultClock } = options;
|
|
47
|
+
return {
|
|
48
|
+
hooks,
|
|
49
|
+
threshold,
|
|
50
|
+
silent,
|
|
51
|
+
logger,
|
|
52
|
+
formatter,
|
|
53
|
+
onHookExecution,
|
|
54
|
+
clock
|
|
55
|
+
};
|
|
47
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* @description 解析插件名称(为空时回退默认值)
|
|
59
|
+
*/
|
|
48
60
|
function resolvePluginName(rawName) {
|
|
49
|
-
|
|
61
|
+
return rawName && rawName.length > 0 ? rawName : ANONYMOUS_PLUGIN_NAME;
|
|
50
62
|
}
|
|
51
|
-
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/utils.ts
|
|
65
|
+
/**
|
|
66
|
+
* @description 统一为数组
|
|
67
|
+
*/
|
|
52
68
|
function ensureArray(value) {
|
|
53
|
-
|
|
69
|
+
return Array.isArray(value) ? value : [value];
|
|
54
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* @description 判断是否为 Promise-like
|
|
73
|
+
*/
|
|
55
74
|
function isPromiseLike(value) {
|
|
56
|
-
|
|
75
|
+
return typeof value === "object" && value !== null && "then" in value;
|
|
57
76
|
}
|
|
58
|
-
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/wrapPlugin.ts
|
|
59
79
|
function resolveHooks(plugin, hooks) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
return [...hooks];
|
|
80
|
+
if (hooks === "all") return Object.keys(plugin).filter((key) => typeof plugin[key] === "function");
|
|
81
|
+
return [...hooks];
|
|
64
82
|
}
|
|
65
83
|
function reportExecution(params) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
84
|
+
const { pluginName, hookName, args, start, options } = params;
|
|
85
|
+
const duration = Math.max(0, options.clock() - start);
|
|
86
|
+
const context = {
|
|
87
|
+
pluginName,
|
|
88
|
+
hookName,
|
|
89
|
+
args,
|
|
90
|
+
duration
|
|
91
|
+
};
|
|
92
|
+
if (duration >= options.threshold) {
|
|
93
|
+
if (!options.silent) {
|
|
94
|
+
const message = options.formatter(context);
|
|
95
|
+
options.logger(message, context);
|
|
96
|
+
}
|
|
97
|
+
options.onHookExecution?.(context);
|
|
98
|
+
}
|
|
81
99
|
}
|
|
82
100
|
function wrapSinglePlugin(plugin, options) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
101
|
+
const wrapped = { ...plugin };
|
|
102
|
+
const pluginName = resolvePluginName(plugin.name);
|
|
103
|
+
const hooksToWrap = resolveHooks(plugin, options.hooks);
|
|
104
|
+
for (const hookName of hooksToWrap) {
|
|
105
|
+
const original = plugin[hookName];
|
|
106
|
+
if (typeof original !== "function") continue;
|
|
107
|
+
wrapped[hookName] = function performanceHook(...args) {
|
|
108
|
+
const start = options.clock();
|
|
109
|
+
const invoke = () => original.apply(this, args);
|
|
110
|
+
const finalize = (result) => {
|
|
111
|
+
reportExecution({
|
|
112
|
+
pluginName,
|
|
113
|
+
hookName,
|
|
114
|
+
args,
|
|
115
|
+
start,
|
|
116
|
+
options
|
|
117
|
+
});
|
|
118
|
+
return result;
|
|
119
|
+
};
|
|
120
|
+
const handleError = (error) => {
|
|
121
|
+
reportExecution({
|
|
122
|
+
pluginName,
|
|
123
|
+
hookName,
|
|
124
|
+
args,
|
|
125
|
+
start,
|
|
126
|
+
options
|
|
127
|
+
});
|
|
128
|
+
throw error;
|
|
129
|
+
};
|
|
130
|
+
try {
|
|
131
|
+
const result = invoke();
|
|
132
|
+
if (isPromiseLike(result)) return result.then(finalize, handleError);
|
|
133
|
+
return finalize(result);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
return handleError(error);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return wrapped;
|
|
114
140
|
}
|
|
115
141
|
function wrapPlugin(maybePlugin, options) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return Array.isArray(maybePlugin) ? wrapped : wrapped[0];
|
|
142
|
+
const resolvedOptions = resolveOptions(options);
|
|
143
|
+
const wrapped = ensureArray(maybePlugin).map((plugin) => wrapSinglePlugin(plugin, resolvedOptions));
|
|
144
|
+
return Array.isArray(maybePlugin) ? wrapped : wrapped[0];
|
|
120
145
|
}
|
|
121
|
-
|
|
146
|
+
//#endregion
|
|
122
147
|
export { DEFAULT_PLUGIN_HOOKS, DEFAULT_THRESHOLD, resolveOptions, wrapPlugin };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-performance",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.9",
|
|
5
5
|
"description": "vite-plugin-performance tools and runtime components for modern WeChat and multi-platform mini-program development.",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -44,9 +44,9 @@
|
|
|
44
44
|
"registry": "https://registry.npmjs.org"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
|
-
"dev": "
|
|
48
|
-
"build:watch": "
|
|
49
|
-
"build": "
|
|
47
|
+
"dev": "tsdown -w --sourcemap",
|
|
48
|
+
"build:watch": "tsdown -w --sourcemap",
|
|
49
|
+
"build": "tsdown",
|
|
50
50
|
"test": "vitest run",
|
|
51
51
|
"test:dev": "vitest",
|
|
52
52
|
"typecheck": "tsc --noEmit",
|
package/dist/index.d.mts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { Plugin } from 'vite';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @description Vite 插件 hook 名称
|
|
5
|
-
*/
|
|
6
|
-
type PluginHookName = Extract<keyof Plugin, string>;
|
|
7
|
-
/**
|
|
8
|
-
* @description hook 执行上下文
|
|
9
|
-
*/
|
|
10
|
-
interface HookExecutionContext {
|
|
11
|
-
pluginName: string;
|
|
12
|
-
hookName: PluginHookName;
|
|
13
|
-
args: unknown[];
|
|
14
|
-
duration: number;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* @description 日志输出函数
|
|
18
|
-
*/
|
|
19
|
-
type HookLogger = (message: string, context: HookExecutionContext) => void;
|
|
20
|
-
/**
|
|
21
|
-
* @description 日志格式化函数
|
|
22
|
-
*/
|
|
23
|
-
type HookFormatter = (context: HookExecutionContext) => string;
|
|
24
|
-
/**
|
|
25
|
-
* @description 自定义计时函数
|
|
26
|
-
*/
|
|
27
|
-
type HookClock = () => number;
|
|
28
|
-
interface WrapPluginOptions {
|
|
29
|
-
/** 仅包裹这些 hooks;传入 `all` 表示包裹插件上的所有函数 hook。 */
|
|
30
|
-
hooks?: PluginHookName[] | 'all';
|
|
31
|
-
/** 触发日志/通知的最小耗时(毫秒)。 */
|
|
32
|
-
threshold?: number;
|
|
33
|
-
/** 关闭默认日志输出。 */
|
|
34
|
-
silent?: boolean;
|
|
35
|
-
/** 自定义日志函数,默认使用 `console.log`。 */
|
|
36
|
-
logger?: HookLogger;
|
|
37
|
-
/** 自定义日志格式化函数。 */
|
|
38
|
-
formatter?: HookFormatter;
|
|
39
|
-
/** hook 执行完成后的回调。 */
|
|
40
|
-
onHookExecution?: (context: HookExecutionContext) => void;
|
|
41
|
-
/** 自定义高精度计时函数。 */
|
|
42
|
-
clock?: HookClock;
|
|
43
|
-
/** @deprecated 兼容旧拼写 */
|
|
44
|
-
slient?: boolean;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* @description 解析后的插件配置
|
|
48
|
-
*/
|
|
49
|
-
interface ResolvedWrapPluginOptions {
|
|
50
|
-
hooks: PluginHookName[] | 'all';
|
|
51
|
-
threshold: number;
|
|
52
|
-
silent: boolean;
|
|
53
|
-
logger: HookLogger;
|
|
54
|
-
formatter: HookFormatter;
|
|
55
|
-
onHookExecution?: (context: HookExecutionContext) => void;
|
|
56
|
-
clock: HookClock;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @description 默认需要包裹的插件 hooks
|
|
61
|
-
*/
|
|
62
|
-
declare const DEFAULT_PLUGIN_HOOKS: PluginHookName[];
|
|
63
|
-
/**
|
|
64
|
-
* @description 默认耗时阈值(毫秒)
|
|
65
|
-
*/
|
|
66
|
-
declare const DEFAULT_THRESHOLD = 0;
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @description 解析插件配置并补全默认值
|
|
70
|
-
*/
|
|
71
|
-
declare function resolveOptions(options?: Partial<WrapPluginOptions>): ResolvedWrapPluginOptions;
|
|
72
|
-
|
|
73
|
-
declare function wrapPlugin(maybePlugin: Plugin, options?: Partial<WrapPluginOptions>): Plugin;
|
|
74
|
-
declare function wrapPlugin(maybePlugin: Plugin[], options?: Partial<WrapPluginOptions>): Plugin[];
|
|
75
|
-
|
|
76
|
-
export { DEFAULT_PLUGIN_HOOKS, DEFAULT_THRESHOLD, resolveOptions, wrapPlugin };
|
|
77
|
-
export type { HookClock, HookExecutionContext, HookFormatter, HookLogger, PluginHookName, ResolvedWrapPluginOptions, WrapPluginOptions };
|