vite-plugin-performance 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 ice breaker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # `wrapPlugin` โ€“ Vite Plugin Performance Wrapper
2
+
3
+ `wrapPlugin` is a utility function designed to wrap Vite plugins and monitor the execution time of their lifecycle hooks. It helps developers gain insights into plugin performance and identify potential bottlenecks.
4
+
5
+ ---
6
+
7
+ ## โœจ Features
8
+
9
+ - Automatically wraps and times specified Vite plugin hooks
10
+ - Supports threshold-based logging (only logs slow hooks)
11
+ - Allows custom hook selection
12
+ - Provides a callback for custom performance tracking
13
+ - Array merge support with override using `defu`
14
+
15
+ ---
16
+
17
+ ## ๐Ÿ“ฆ Installation
18
+
19
+ ```sh
20
+ npm i -D vite-plugin-performance
21
+ ```
22
+
23
+ ---
24
+
25
+ ## ๐Ÿš€ Usage
26
+
27
+ ```ts
28
+ import someVitePlugin from 'some-vite-plugin'
29
+ import { defineConfig } from 'vite'
30
+ import { wrapPlugin } from 'vite-plugin-performance'
31
+
32
+ export default defineConfig({
33
+ plugins: [
34
+ wrapPlugin(
35
+ someVitePlugin(),
36
+ {
37
+ threshold: 50, // Log only hooks taking longer than 50ms
38
+ onHookExecution: ({ pluginName, hookName, duration }) => {
39
+ // Custom logic, e.g., send to monitoring service
40
+ reportToMonitoringSystem({ pluginName, hookName, duration })
41
+ },
42
+ }
43
+ ),
44
+ ],
45
+ })
46
+ ```
47
+
48
+ ---
49
+
50
+ ## ๐Ÿงฉ API
51
+
52
+ ### `wrapPlugin(plugin: Plugin, options?: Partial<WrapPluginOptions>): Plugin`
53
+
54
+ #### Parameters
55
+
56
+ - `plugin`: A Vite plugin object
57
+ - `options`: Optional configuration object
58
+
59
+ #### `WrapPluginOptions` Interface
60
+
61
+ | Option | Type | Default | Description |
62
+ | ----------------- | ----------------------------------------- | ---------------------------- | -------------------------------------- |
63
+ | `threshold` | `number` | `0` | Minimum duration (ms) to log a hook |
64
+ | `onHookExecution` | `(params: OnHookExecutionParams) => void` | `undefined` | Callback invoked when a hook completes |
65
+ | `hooks` | `PluginHooks[]` | Common hook list (see below) | List of Vite plugin hooks to wrap |
66
+ | `slient` | `boolean` | `false` | Suppress default console output |
67
+
68
+ #### Default Hook List:
69
+
70
+ ```ts
71
+ [
72
+ 'options',
73
+ 'buildStart',
74
+ 'resolveId',
75
+ 'load',
76
+ 'transform',
77
+ 'buildEnd',
78
+ 'generateBundle',
79
+ 'renderChunk',
80
+ 'writeBundle',
81
+ ]
82
+ ```
83
+
84
+ ---
85
+
86
+ ## ๐Ÿงช Example Output
87
+
88
+ When a hook's execution time exceeds the threshold, output will be shown in the console:
89
+
90
+ ```
91
+ [my-plugin] transform โฑ 132.57 ms
92
+ ```
93
+
94
+ ---
95
+
96
+ ## ๐Ÿ“˜ Types
97
+
98
+ ### `OnHookExecutionParams`
99
+
100
+ ```ts
101
+ interface OnHookExecutionParams {
102
+ pluginName: string
103
+ hookName: string
104
+ args: any[]
105
+ duration: number
106
+ }
107
+ ```
108
+
109
+ ---
110
+
111
+ ## โš ๏ธ Notes
112
+
113
+ - This utility only works with object-based plugin definitions. It does not support plugins returned as arrays.
114
+ - Async hooks are automatically awaited to ensure accurate timing.
115
+
116
+ ---
117
+
118
+ ## ๐Ÿ“„ License
119
+
120
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,122 @@
1
+ 'use strict';
2
+
3
+ function getDefaultOptions() {
4
+ return {
5
+ threshold: 0,
6
+ hooks: [
7
+ "options",
8
+ "buildStart",
9
+ "resolveId",
10
+ "load",
11
+ "transform",
12
+ "buildEnd",
13
+ "generateBundle",
14
+ "renderChunk",
15
+ "writeBundle"
16
+ ],
17
+ slient: false
18
+ };
19
+ }
20
+
21
+ function isPlainObject(value) {
22
+ if (value === null || typeof value !== "object") {
23
+ return false;
24
+ }
25
+ const prototype = Object.getPrototypeOf(value);
26
+ if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
27
+ return false;
28
+ }
29
+ if (Symbol.iterator in value) {
30
+ return false;
31
+ }
32
+ if (Symbol.toStringTag in value) {
33
+ return Object.prototype.toString.call(value) === "[object Module]";
34
+ }
35
+ return true;
36
+ }
37
+
38
+ function _defu(baseObject, defaults, namespace = ".", merger) {
39
+ if (!isPlainObject(defaults)) {
40
+ return _defu(baseObject, {}, namespace, merger);
41
+ }
42
+ const object = Object.assign({}, defaults);
43
+ for (const key in baseObject) {
44
+ if (key === "__proto__" || key === "constructor") {
45
+ continue;
46
+ }
47
+ const value = baseObject[key];
48
+ if (value === null || value === void 0) {
49
+ continue;
50
+ }
51
+ if (merger && merger(object, key, value, namespace)) {
52
+ continue;
53
+ }
54
+ if (Array.isArray(value) && Array.isArray(object[key])) {
55
+ object[key] = [...value, ...object[key]];
56
+ } else if (isPlainObject(value) && isPlainObject(object[key])) {
57
+ object[key] = _defu(
58
+ value,
59
+ object[key],
60
+ (namespace ? `${namespace}.` : "") + key.toString(),
61
+ merger
62
+ );
63
+ } else {
64
+ object[key] = value;
65
+ }
66
+ }
67
+ return object;
68
+ }
69
+ function createDefu(merger) {
70
+ return (...arguments_) => (
71
+ // eslint-disable-next-line unicorn/no-array-reduce
72
+ arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
73
+ );
74
+ }
75
+
76
+ const defuOverrideArray = createDefu((obj, key, value) => {
77
+ if (Array.isArray(obj[key]) && Array.isArray(value)) {
78
+ obj[key] = value;
79
+ return true;
80
+ }
81
+ });
82
+
83
+ function wrapPlugin(maybePlugin, options) {
84
+ const isArr = Array.isArray(maybePlugin);
85
+ const plugins = isArr ? maybePlugin : [maybePlugin];
86
+ const wrappedPlugins = plugins.map((plugin) => {
87
+ const wrapped = { ...plugin };
88
+ const { threshold = 0, onHookExecution, hooks, slient } = defuOverrideArray(
89
+ options,
90
+ getDefaultOptions()
91
+ );
92
+ if (Array.isArray(hooks)) {
93
+ for (const hook of hooks) {
94
+ if (typeof plugin[hook] === "function") {
95
+ wrapped[hook] = async function(...args) {
96
+ const start = performance.now();
97
+ const result = await plugin[hook]?.apply(this, args);
98
+ const end = performance.now();
99
+ const duration = Math.round(end - start);
100
+ if (duration >= threshold) {
101
+ const pluginName = plugin.name;
102
+ if (!slient) {
103
+ console.log(`[${pluginName}] ${hook.padEnd(20)} \u23F1 ${duration.toFixed(2).padStart(6)} ms`);
104
+ }
105
+ onHookExecution?.({
106
+ pluginName,
107
+ hookName: hook,
108
+ args,
109
+ duration
110
+ });
111
+ }
112
+ return result;
113
+ };
114
+ }
115
+ }
116
+ }
117
+ return wrapped;
118
+ });
119
+ return isArr ? wrappedPlugins : wrappedPlugins[0];
120
+ }
121
+
122
+ exports.wrapPlugin = wrapPlugin;
@@ -0,0 +1,20 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ type PluginHooks = keyof Plugin;
4
+ interface OnHookExecutionParams {
5
+ pluginName: string;
6
+ hookName: string;
7
+ args: any[];
8
+ duration: number;
9
+ }
10
+ interface WrapPluginOptions {
11
+ threshold?: number;
12
+ onHookExecution?: (params: OnHookExecutionParams) => void;
13
+ hooks?: PluginHooks[];
14
+ slient?: boolean;
15
+ }
16
+
17
+ declare function wrapPlugin(maybePlugin: Plugin, options?: Partial<WrapPluginOptions>): Plugin;
18
+ declare function wrapPlugin(maybePlugin: Plugin[], options?: Partial<WrapPluginOptions>): Plugin[];
19
+
20
+ export { type OnHookExecutionParams, type PluginHooks, type WrapPluginOptions, wrapPlugin };
@@ -0,0 +1,20 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ type PluginHooks = keyof Plugin;
4
+ interface OnHookExecutionParams {
5
+ pluginName: string;
6
+ hookName: string;
7
+ args: any[];
8
+ duration: number;
9
+ }
10
+ interface WrapPluginOptions {
11
+ threshold?: number;
12
+ onHookExecution?: (params: OnHookExecutionParams) => void;
13
+ hooks?: PluginHooks[];
14
+ slient?: boolean;
15
+ }
16
+
17
+ declare function wrapPlugin(maybePlugin: Plugin, options?: Partial<WrapPluginOptions>): Plugin;
18
+ declare function wrapPlugin(maybePlugin: Plugin[], options?: Partial<WrapPluginOptions>): Plugin[];
19
+
20
+ export { type OnHookExecutionParams, type PluginHooks, type WrapPluginOptions, wrapPlugin };
@@ -0,0 +1,20 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ type PluginHooks = keyof Plugin;
4
+ interface OnHookExecutionParams {
5
+ pluginName: string;
6
+ hookName: string;
7
+ args: any[];
8
+ duration: number;
9
+ }
10
+ interface WrapPluginOptions {
11
+ threshold?: number;
12
+ onHookExecution?: (params: OnHookExecutionParams) => void;
13
+ hooks?: PluginHooks[];
14
+ slient?: boolean;
15
+ }
16
+
17
+ declare function wrapPlugin(maybePlugin: Plugin, options?: Partial<WrapPluginOptions>): Plugin;
18
+ declare function wrapPlugin(maybePlugin: Plugin[], options?: Partial<WrapPluginOptions>): Plugin[];
19
+
20
+ export { type OnHookExecutionParams, type PluginHooks, type WrapPluginOptions, wrapPlugin };
package/dist/index.mjs ADDED
@@ -0,0 +1,120 @@
1
+ function getDefaultOptions() {
2
+ return {
3
+ threshold: 0,
4
+ hooks: [
5
+ "options",
6
+ "buildStart",
7
+ "resolveId",
8
+ "load",
9
+ "transform",
10
+ "buildEnd",
11
+ "generateBundle",
12
+ "renderChunk",
13
+ "writeBundle"
14
+ ],
15
+ slient: false
16
+ };
17
+ }
18
+
19
+ function isPlainObject(value) {
20
+ if (value === null || typeof value !== "object") {
21
+ return false;
22
+ }
23
+ const prototype = Object.getPrototypeOf(value);
24
+ if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) {
25
+ return false;
26
+ }
27
+ if (Symbol.iterator in value) {
28
+ return false;
29
+ }
30
+ if (Symbol.toStringTag in value) {
31
+ return Object.prototype.toString.call(value) === "[object Module]";
32
+ }
33
+ return true;
34
+ }
35
+
36
+ function _defu(baseObject, defaults, namespace = ".", merger) {
37
+ if (!isPlainObject(defaults)) {
38
+ return _defu(baseObject, {}, namespace, merger);
39
+ }
40
+ const object = Object.assign({}, defaults);
41
+ for (const key in baseObject) {
42
+ if (key === "__proto__" || key === "constructor") {
43
+ continue;
44
+ }
45
+ const value = baseObject[key];
46
+ if (value === null || value === void 0) {
47
+ continue;
48
+ }
49
+ if (merger && merger(object, key, value, namespace)) {
50
+ continue;
51
+ }
52
+ if (Array.isArray(value) && Array.isArray(object[key])) {
53
+ object[key] = [...value, ...object[key]];
54
+ } else if (isPlainObject(value) && isPlainObject(object[key])) {
55
+ object[key] = _defu(
56
+ value,
57
+ object[key],
58
+ (namespace ? `${namespace}.` : "") + key.toString(),
59
+ merger
60
+ );
61
+ } else {
62
+ object[key] = value;
63
+ }
64
+ }
65
+ return object;
66
+ }
67
+ function createDefu(merger) {
68
+ return (...arguments_) => (
69
+ // eslint-disable-next-line unicorn/no-array-reduce
70
+ arguments_.reduce((p, c) => _defu(p, c, "", merger), {})
71
+ );
72
+ }
73
+
74
+ const defuOverrideArray = createDefu((obj, key, value) => {
75
+ if (Array.isArray(obj[key]) && Array.isArray(value)) {
76
+ obj[key] = value;
77
+ return true;
78
+ }
79
+ });
80
+
81
+ function wrapPlugin(maybePlugin, options) {
82
+ const isArr = Array.isArray(maybePlugin);
83
+ const plugins = isArr ? maybePlugin : [maybePlugin];
84
+ const wrappedPlugins = plugins.map((plugin) => {
85
+ const wrapped = { ...plugin };
86
+ const { threshold = 0, onHookExecution, hooks, slient } = defuOverrideArray(
87
+ options,
88
+ getDefaultOptions()
89
+ );
90
+ if (Array.isArray(hooks)) {
91
+ for (const hook of hooks) {
92
+ if (typeof plugin[hook] === "function") {
93
+ wrapped[hook] = async function(...args) {
94
+ const start = performance.now();
95
+ const result = await plugin[hook]?.apply(this, args);
96
+ const end = performance.now();
97
+ const duration = Math.round(end - start);
98
+ if (duration >= threshold) {
99
+ const pluginName = plugin.name;
100
+ if (!slient) {
101
+ console.log(`[${pluginName}] ${hook.padEnd(20)} \u23F1 ${duration.toFixed(2).padStart(6)} ms`);
102
+ }
103
+ onHookExecution?.({
104
+ pluginName,
105
+ hookName: hook,
106
+ args,
107
+ duration
108
+ });
109
+ }
110
+ return result;
111
+ };
112
+ }
113
+ }
114
+ }
115
+ return wrapped;
116
+ });
117
+ return isArr ? wrappedPlugins : wrappedPlugins[0];
118
+ }
119
+
120
+ export { wrapPlugin };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "vite-plugin-performance",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "description": "vite-plugin-performance",
6
+ "author": "ice breaker <1324318532@qq.com>",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/weapp-vite/weapp-vite.git",
11
+ "directory": "packages/vite-plugin-performance"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/weapp-vite/weapp-vite/issues"
15
+ },
16
+ "keywords": [],
17
+ "sideEffects": false,
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.cjs"
23
+ }
24
+ },
25
+ "main": "./dist/index.cjs",
26
+ "module": "./dist/index.mjs",
27
+ "types": "./dist/index.d.ts",
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "scripts": {
32
+ "dev": "unbuild --stub",
33
+ "build:watch": "unbuild --watch --sourcemap",
34
+ "build": "unbuild",
35
+ "test": "vitest run",
36
+ "test:dev": "vitest",
37
+ "release": "pnpm publish",
38
+ "lint": "eslint .",
39
+ "lint:fix": "eslint . --fix"
40
+ }
41
+ }