vite-plugin-performance 1.0.0 → 2.0.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/dist/index.d.mts CHANGED
@@ -1,33 +1,51 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
+ /**
4
+ * @description Vite 插件 hook 名称
5
+ */
3
6
  type PluginHookName = Extract<keyof Plugin, string>;
7
+ /**
8
+ * @description hook 执行上下文
9
+ */
4
10
  interface HookExecutionContext {
5
11
  pluginName: string;
6
12
  hookName: PluginHookName;
7
13
  args: unknown[];
8
14
  duration: number;
9
15
  }
16
+ /**
17
+ * @description 日志输出函数
18
+ */
10
19
  type HookLogger = (message: string, context: HookExecutionContext) => void;
20
+ /**
21
+ * @description 日志格式化函数
22
+ */
11
23
  type HookFormatter = (context: HookExecutionContext) => string;
24
+ /**
25
+ * @description 自定义计时函数
26
+ */
12
27
  type HookClock = () => number;
13
28
  interface WrapPluginOptions {
14
- /** Wrap only these hooks. Use `all` to wrap every function hook on the plugin. */
29
+ /** 仅包裹这些 hooks;传入 `all` 表示包裹插件上的所有函数 hook。 */
15
30
  hooks?: PluginHookName[] | 'all';
16
- /** Minimum duration (ms) required to log or emit notifications. */
31
+ /** 触发日志/通知的最小耗时(毫秒)。 */
17
32
  threshold?: number;
18
- /** Disable the default logger output. */
33
+ /** 关闭默认日志输出。 */
19
34
  silent?: boolean;
20
- /** Custom logger called with the formatted message. Defaults to `console.log`. */
35
+ /** 自定义日志函数,默认使用 `console.log`。 */
21
36
  logger?: HookLogger;
22
- /** Custom formatter for the log message. */
37
+ /** 自定义日志格式化函数。 */
23
38
  formatter?: HookFormatter;
24
- /** Lifecycle callback invoked after a hook has completed. */
39
+ /** hook 执行完成后的回调。 */
25
40
  onHookExecution?: (context: HookExecutionContext) => void;
26
- /** Provide a custom high-resolution clock implementation. */
41
+ /** 自定义高精度计时函数。 */
27
42
  clock?: HookClock;
28
- /** @deprecated backwards compatibility typo */
43
+ /** @deprecated 兼容旧拼写 */
29
44
  slient?: boolean;
30
45
  }
46
+ /**
47
+ * @description 解析后的插件配置
48
+ */
31
49
  interface ResolvedWrapPluginOptions {
32
50
  hooks: PluginHookName[] | 'all';
33
51
  threshold: number;
@@ -38,9 +56,18 @@ interface ResolvedWrapPluginOptions {
38
56
  clock: HookClock;
39
57
  }
40
58
 
59
+ /**
60
+ * @description 默认需要包裹的插件 hooks
61
+ */
41
62
  declare const DEFAULT_PLUGIN_HOOKS: PluginHookName[];
63
+ /**
64
+ * @description 默认耗时阈值(毫秒)
65
+ */
42
66
  declare const DEFAULT_THRESHOLD = 0;
43
67
 
68
+ /**
69
+ * @description 解析插件配置并补全默认值
70
+ */
44
71
  declare function resolveOptions(options?: Partial<WrapPluginOptions>): ResolvedWrapPluginOptions;
45
72
 
46
73
  declare function wrapPlugin(maybePlugin: Plugin, options?: Partial<WrapPluginOptions>): Plugin;
package/dist/index.d.ts CHANGED
@@ -1,33 +1,51 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
+ /**
4
+ * @description Vite 插件 hook 名称
5
+ */
3
6
  type PluginHookName = Extract<keyof Plugin, string>;
7
+ /**
8
+ * @description hook 执行上下文
9
+ */
4
10
  interface HookExecutionContext {
5
11
  pluginName: string;
6
12
  hookName: PluginHookName;
7
13
  args: unknown[];
8
14
  duration: number;
9
15
  }
16
+ /**
17
+ * @description 日志输出函数
18
+ */
10
19
  type HookLogger = (message: string, context: HookExecutionContext) => void;
20
+ /**
21
+ * @description 日志格式化函数
22
+ */
11
23
  type HookFormatter = (context: HookExecutionContext) => string;
24
+ /**
25
+ * @description 自定义计时函数
26
+ */
12
27
  type HookClock = () => number;
13
28
  interface WrapPluginOptions {
14
- /** Wrap only these hooks. Use `all` to wrap every function hook on the plugin. */
29
+ /** 仅包裹这些 hooks;传入 `all` 表示包裹插件上的所有函数 hook。 */
15
30
  hooks?: PluginHookName[] | 'all';
16
- /** Minimum duration (ms) required to log or emit notifications. */
31
+ /** 触发日志/通知的最小耗时(毫秒)。 */
17
32
  threshold?: number;
18
- /** Disable the default logger output. */
33
+ /** 关闭默认日志输出。 */
19
34
  silent?: boolean;
20
- /** Custom logger called with the formatted message. Defaults to `console.log`. */
35
+ /** 自定义日志函数,默认使用 `console.log`。 */
21
36
  logger?: HookLogger;
22
- /** Custom formatter for the log message. */
37
+ /** 自定义日志格式化函数。 */
23
38
  formatter?: HookFormatter;
24
- /** Lifecycle callback invoked after a hook has completed. */
39
+ /** hook 执行完成后的回调。 */
25
40
  onHookExecution?: (context: HookExecutionContext) => void;
26
- /** Provide a custom high-resolution clock implementation. */
41
+ /** 自定义高精度计时函数。 */
27
42
  clock?: HookClock;
28
- /** @deprecated backwards compatibility typo */
43
+ /** @deprecated 兼容旧拼写 */
29
44
  slient?: boolean;
30
45
  }
46
+ /**
47
+ * @description 解析后的插件配置
48
+ */
31
49
  interface ResolvedWrapPluginOptions {
32
50
  hooks: PluginHookName[] | 'all';
33
51
  threshold: number;
@@ -38,9 +56,18 @@ interface ResolvedWrapPluginOptions {
38
56
  clock: HookClock;
39
57
  }
40
58
 
59
+ /**
60
+ * @description 默认需要包裹的插件 hooks
61
+ */
41
62
  declare const DEFAULT_PLUGIN_HOOKS: PluginHookName[];
63
+ /**
64
+ * @description 默认耗时阈值(毫秒)
65
+ */
42
66
  declare const DEFAULT_THRESHOLD = 0;
43
67
 
68
+ /**
69
+ * @description 解析插件配置并补全默认值
70
+ */
44
71
  declare function resolveOptions(options?: Partial<WrapPluginOptions>): ResolvedWrapPluginOptions;
45
72
 
46
73
  declare function wrapPlugin(maybePlugin: Plugin, options?: Partial<WrapPluginOptions>): Plugin;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-performance",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "2.0.1",
5
5
  "description": "vite-plugin-performance",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -18,16 +18,18 @@
18
18
  "exports": {
19
19
  ".": {
20
20
  "types": "./dist/index.d.ts",
21
- "import": "./dist/index.mjs",
22
- "require": "./dist/index.cjs"
21
+ "import": "./dist/index.mjs"
23
22
  }
24
23
  },
25
- "main": "./dist/index.cjs",
24
+ "main": "./dist/index.mjs",
26
25
  "module": "./dist/index.mjs",
27
26
  "types": "./dist/index.d.ts",
28
27
  "files": [
29
28
  "dist"
30
29
  ],
30
+ "engines": {
31
+ "node": "^20.19.0 || >=22.12.0"
32
+ },
31
33
  "scripts": {
32
34
  "dev": "unbuild --stub",
33
35
  "build:watch": "unbuild --watch --sourcemap",
package/dist/index.cjs DELETED
@@ -1,127 +0,0 @@
1
- 'use strict';
2
-
3
- const DEFAULT_PLUGIN_HOOKS = [
4
- "options",
5
- "config",
6
- "configResolved",
7
- "configureServer",
8
- "buildStart",
9
- "resolveId",
10
- "load",
11
- "transform",
12
- "buildEnd",
13
- "generateBundle",
14
- "renderChunk",
15
- "writeBundle"
16
- ];
17
- const DEFAULT_THRESHOLD = 0;
18
- const ANONYMOUS_PLUGIN_NAME = "anonymous-plugin";
19
- const DEFAULT_FORMATTER = ({ pluginName, hookName, duration }) => {
20
- const paddedHook = hookName.padEnd(20);
21
- const formattedDuration = duration.toFixed(2).padStart(7);
22
- return `[${pluginName}] ${paddedHook} \u23F1 ${formattedDuration} ms`;
23
- };
24
- const DEFAULT_LOGGER = (message) => {
25
- console.log(message);
26
- };
27
-
28
- const hasPerformanceNow = typeof globalThis !== "undefined" && typeof globalThis.performance?.now === "function";
29
- const defaultClock = hasPerformanceNow ? () => globalThis.performance.now() : () => Date.now();
30
- function resolveOptions(options = {}) {
31
- const {
32
- hooks = DEFAULT_PLUGIN_HOOKS,
33
- threshold = DEFAULT_THRESHOLD,
34
- silent = options.slient ?? false,
35
- logger = DEFAULT_LOGGER,
36
- formatter = DEFAULT_FORMATTER,
37
- onHookExecution,
38
- clock = defaultClock
39
- } = options;
40
- return {
41
- hooks,
42
- threshold,
43
- silent,
44
- logger,
45
- formatter,
46
- onHookExecution,
47
- clock
48
- };
49
- }
50
- function resolvePluginName(rawName) {
51
- return rawName && rawName.length > 0 ? rawName : ANONYMOUS_PLUGIN_NAME;
52
- }
53
-
54
- function ensureArray(value) {
55
- return Array.isArray(value) ? value : [value];
56
- }
57
- function isPromiseLike(value) {
58
- return typeof value === "object" && value !== null && "then" in value;
59
- }
60
-
61
- function wrapPlugin(maybePlugin, options) {
62
- const resolvedOptions = resolveOptions(options);
63
- const plugins = ensureArray(maybePlugin);
64
- const wrapped = plugins.map((plugin) => wrapSinglePlugin(plugin, resolvedOptions));
65
- return Array.isArray(maybePlugin) ? wrapped : wrapped[0];
66
- }
67
- function wrapSinglePlugin(plugin, options) {
68
- const wrapped = { ...plugin };
69
- const pluginName = resolvePluginName(plugin.name);
70
- const hooksToWrap = resolveHooks(plugin, options.hooks);
71
- for (const hookName of hooksToWrap) {
72
- const original = plugin[hookName];
73
- if (typeof original !== "function") {
74
- continue;
75
- }
76
- wrapped[hookName] = function performanceHook(...args) {
77
- const start = options.clock();
78
- const invoke = () => original.apply(this, args);
79
- const finalize = (result) => {
80
- reportExecution({ pluginName, hookName, args, start, options });
81
- return result;
82
- };
83
- const handleError = (error) => {
84
- reportExecution({ pluginName, hookName, args, start, options });
85
- throw error;
86
- };
87
- try {
88
- const result = invoke();
89
- if (isPromiseLike(result)) {
90
- return result.then(finalize, handleError);
91
- }
92
- return finalize(result);
93
- } catch (error) {
94
- return handleError(error);
95
- }
96
- };
97
- }
98
- return wrapped;
99
- }
100
- function resolveHooks(plugin, hooks) {
101
- if (hooks === "all") {
102
- return Object.keys(plugin).filter((key) => typeof plugin[key] === "function");
103
- }
104
- return [...hooks];
105
- }
106
- function reportExecution(params) {
107
- const { pluginName, hookName, args, start, options } = params;
108
- const duration = Math.max(0, options.clock() - start);
109
- const context = {
110
- pluginName,
111
- hookName,
112
- args,
113
- duration
114
- };
115
- if (duration >= options.threshold) {
116
- if (!options.silent) {
117
- const message = options.formatter(context);
118
- options.logger(message, context);
119
- }
120
- options.onHookExecution?.(context);
121
- }
122
- }
123
-
124
- exports.DEFAULT_PLUGIN_HOOKS = DEFAULT_PLUGIN_HOOKS;
125
- exports.DEFAULT_THRESHOLD = DEFAULT_THRESHOLD;
126
- exports.resolveOptions = resolveOptions;
127
- exports.wrapPlugin = wrapPlugin;
package/dist/index.d.cts DELETED
@@ -1,50 +0,0 @@
1
- import { Plugin } from 'vite';
2
-
3
- type PluginHookName = Extract<keyof Plugin, string>;
4
- interface HookExecutionContext {
5
- pluginName: string;
6
- hookName: PluginHookName;
7
- args: unknown[];
8
- duration: number;
9
- }
10
- type HookLogger = (message: string, context: HookExecutionContext) => void;
11
- type HookFormatter = (context: HookExecutionContext) => string;
12
- type HookClock = () => number;
13
- interface WrapPluginOptions {
14
- /** Wrap only these hooks. Use `all` to wrap every function hook on the plugin. */
15
- hooks?: PluginHookName[] | 'all';
16
- /** Minimum duration (ms) required to log or emit notifications. */
17
- threshold?: number;
18
- /** Disable the default logger output. */
19
- silent?: boolean;
20
- /** Custom logger called with the formatted message. Defaults to `console.log`. */
21
- logger?: HookLogger;
22
- /** Custom formatter for the log message. */
23
- formatter?: HookFormatter;
24
- /** Lifecycle callback invoked after a hook has completed. */
25
- onHookExecution?: (context: HookExecutionContext) => void;
26
- /** Provide a custom high-resolution clock implementation. */
27
- clock?: HookClock;
28
- /** @deprecated backwards compatibility typo */
29
- slient?: boolean;
30
- }
31
- interface ResolvedWrapPluginOptions {
32
- hooks: PluginHookName[] | 'all';
33
- threshold: number;
34
- silent: boolean;
35
- logger: HookLogger;
36
- formatter: HookFormatter;
37
- onHookExecution?: (context: HookExecutionContext) => void;
38
- clock: HookClock;
39
- }
40
-
41
- declare const DEFAULT_PLUGIN_HOOKS: PluginHookName[];
42
- declare const DEFAULT_THRESHOLD = 0;
43
-
44
- declare function resolveOptions(options?: Partial<WrapPluginOptions>): ResolvedWrapPluginOptions;
45
-
46
- declare function wrapPlugin(maybePlugin: Plugin, options?: Partial<WrapPluginOptions>): Plugin;
47
- declare function wrapPlugin(maybePlugin: Plugin[], options?: Partial<WrapPluginOptions>): Plugin[];
48
-
49
- export { DEFAULT_PLUGIN_HOOKS, DEFAULT_THRESHOLD, resolveOptions, wrapPlugin };
50
- export type { HookClock, HookExecutionContext, HookFormatter, HookLogger, PluginHookName, ResolvedWrapPluginOptions, WrapPluginOptions };