@traceletdev/vite 0.5.0

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.
Files changed (3) hide show
  1. package/README.md +41 -0
  2. package/index.cjs +57 -0
  3. package/package.json +31 -0
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # @traceletdev/vite
2
+
3
+ Tracelet plugin for Vite projects.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -D @traceletdev/cli @traceletdev/vite
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Add the plugin to your `vite.config.js`:
14
+
15
+ ```js
16
+ import { defineConfig } from 'vite';
17
+ import tracelet from '@traceletdev/vite';
18
+
19
+ export default defineConfig({
20
+ plugins: [
21
+ // your other plugins
22
+ tracelet(),
23
+ ],
24
+ });
25
+ ```
26
+
27
+ The plugin automatically runs during builds and generates `.tracelet/stats.json` which is used by `tracelet lint` to check performance budgets.
28
+
29
+ ## How it works
30
+
31
+ The plugin hooks into Vite's build process and:
32
+
33
+ - Analyzes entry chunks during bundle generation
34
+ - Calculates gzip-compressed JavaScript sizes
35
+ - Identifies third-party JavaScript bundles
36
+ - Generates statistics for tracelet linting
37
+
38
+ ## Configuration
39
+
40
+ Ensure you have a `tracelet.config.json` file in your project root. See the [main tracelet documentation](../../README.md) for configuration options.
41
+
package/index.cjs ADDED
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const zlib = require('zlib');
6
+
7
+ function writeStats(routes, _outDir) {
8
+ const outPath = path.join(process.cwd(), '.tracelet', 'stats.json');
9
+ try {
10
+ fs.mkdirSync(path.dirname(outPath), { recursive: true });
11
+ fs.writeFileSync(outPath, JSON.stringify({ routes }, null, 2));
12
+
13
+ console.log(`[tracelet] wrote stats to ${path.relative(process.cwd(), outPath)}`);
14
+ } catch (e) {
15
+ console.warn('[tracelet] failed to write stats:', e.message);
16
+ }
17
+ }
18
+
19
+ module.exports = function traceletPlugin() {
20
+ let resolvedOutDir = '';
21
+ return {
22
+ name: 'tracelet-plugin',
23
+ apply: 'build',
24
+ configResolved(cfg) {
25
+ resolvedOutDir = (cfg.build && cfg.build.outDir) || 'dist';
26
+ },
27
+ generateBundle(_, bundle) {
28
+ // Find entry chunks and sum sizes as a homepage surrogate
29
+ const routes = [];
30
+ let homeBytes = 0;
31
+ let thirdPartyBytes = 0;
32
+ for (const [fileName, chunk] of Object.entries(bundle)) {
33
+ if (chunk && chunk.type === 'chunk' && chunk.isEntry) {
34
+ // Compress chunk code directly (files not written yet in generateBundle)
35
+ const code = chunk.code || '';
36
+ const compressed = zlib.gzipSync(Buffer.from(code), { level: 6 });
37
+ const size = compressed.length;
38
+ homeBytes += size;
39
+ // Detect third-party: Vite often names vendor chunks as "vendor-*.js" or includes node_modules imports
40
+ if (
41
+ fileName.includes('vendor') ||
42
+ fileName.includes('chunk-vendor') ||
43
+ (chunk.imports && chunk.imports.some(imp => imp && imp.includes('node_modules')))
44
+ ) {
45
+ thirdPartyBytes += size;
46
+ }
47
+ }
48
+ }
49
+ routes.push({
50
+ path: '/',
51
+ jsGzipBytes: homeBytes,
52
+ thirdPartyJsBytes: thirdPartyBytes,
53
+ });
54
+ writeStats(routes, resolvedOutDir);
55
+ },
56
+ };
57
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@traceletdev/vite",
3
+ "version": "0.5.0",
4
+ "description": "Tracelet plugin for Vite - automatically collects route statistics during builds",
5
+ "type": "commonjs",
6
+ "main": "index.cjs",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "peerDependencies": {
11
+ "@traceletdev/cli": "^0.5.0",
12
+ "vite": ">=4.0.0"
13
+ },
14
+ "keywords": [
15
+ "tracelet",
16
+ "vite",
17
+ "performance",
18
+ "plugin"
19
+ ],
20
+ "author": "Moiz Imran <moizwasti@gmail.com>",
21
+ "license": "MIT",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/traceletdev/tracelet.git",
25
+ "directory": "packages/tracelet-vite"
26
+ },
27
+ "files": [
28
+ "index.cjs",
29
+ "README.md"
30
+ ]
31
+ }