vite-bundle-explorer 0.3.1 → 0.3.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.md +15 -12
- package/{plugin → dist-plugin}/stats-plugin.d.ts +8 -5
- package/dist-plugin/stats-plugin.js +174 -0
- package/dist-ui/assets/index-DHfe3O-k.js +63 -0
- package/{dist → dist-ui}/index.html +1 -1
- package/package.json +17 -16
- package/dist/assets/index-C2mzRsne.js +0 -63
- package/plugin/stats-plugin.js +0 -141
- /package/{dist → dist-ui}/assets/index-D-ZNVfN7.css +0 -0
package/plugin/stats-plugin.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { join } from 'node:path';
|
|
2
|
-
import { gzip } from 'node:zlib';
|
|
3
|
-
import { promisify } from 'node:util';
|
|
4
|
-
import fs from 'node:fs/promises';
|
|
5
|
-
const compress = promisify(gzip);
|
|
6
|
-
const REPORT_DIR_NAME = 'bundle-report';
|
|
7
|
-
function upsertNodeIndex(nodes, item) {
|
|
8
|
-
const index = nodes.indexOf(item);
|
|
9
|
-
return index === -1 ? nodes.push(item) - 1 : index;
|
|
10
|
-
}
|
|
11
|
-
export function statsPlugin(options) {
|
|
12
|
-
const emitHtml = options?.emitHtml ?? true;
|
|
13
|
-
const emitJson = options?.emitJson ?? false;
|
|
14
|
-
let root = process.cwd();
|
|
15
|
-
let outDir = '';
|
|
16
|
-
let enabled = options?.enabled ?? true;
|
|
17
|
-
let reportCompressed = true;
|
|
18
|
-
function truncatePath(filePath) {
|
|
19
|
-
let index = 0;
|
|
20
|
-
const normalizedPath = filePath.replaceAll('\u0000', '');
|
|
21
|
-
if (outDir && normalizedPath.startsWith(outDir)) {
|
|
22
|
-
index = outDir.length;
|
|
23
|
-
}
|
|
24
|
-
else if (root && normalizedPath.startsWith(root)) {
|
|
25
|
-
index = root.length;
|
|
26
|
-
}
|
|
27
|
-
return index > 0 ? normalizedPath.substring(index + 1) : normalizedPath;
|
|
28
|
-
}
|
|
29
|
-
const edges = new Set();
|
|
30
|
-
const stats = { chunks: [], importGraph: { edges: [] }, moduleFileNames: [] };
|
|
31
|
-
const plugin = {
|
|
32
|
-
name: 'stats-plugin',
|
|
33
|
-
// vite specific hook
|
|
34
|
-
configResolved(config) {
|
|
35
|
-
// disable during dev mode
|
|
36
|
-
enabled = config.env.PROD;
|
|
37
|
-
},
|
|
38
|
-
outputOptions(opts) {
|
|
39
|
-
// workaround for libraries with multiple output formats
|
|
40
|
-
enabled = opts.format === 'es';
|
|
41
|
-
},
|
|
42
|
-
resolveId: {
|
|
43
|
-
order: 'pre',
|
|
44
|
-
async handler(source, importer, options) {
|
|
45
|
-
if (!enabled) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
// skip bundle root element
|
|
49
|
-
if (!importer) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
const result = await this.resolve(source, importer, { ...options, skipSelf: true });
|
|
53
|
-
if (!result) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
const parent = upsertNodeIndex(stats.moduleFileNames, truncatePath(importer));
|
|
57
|
-
const child = upsertNodeIndex(stats.moduleFileNames, truncatePath(result.id));
|
|
58
|
-
edges.add(`${parent},${child}`);
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
generateBundle: {
|
|
62
|
-
async handler(_options, bundle) {
|
|
63
|
-
if (!enabled) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
for (const [_name, chunk] of Object.entries(bundle)) {
|
|
67
|
-
if (chunk.type !== 'chunk') {
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
const currentChunk = {
|
|
71
|
-
fileName: chunk.fileName,
|
|
72
|
-
modules: [],
|
|
73
|
-
minifiedLength: -1,
|
|
74
|
-
compressedLength: -1,
|
|
75
|
-
};
|
|
76
|
-
stats.chunks.push(currentChunk);
|
|
77
|
-
for (const [moduleName, mod] of Object.entries(chunk.modules)) {
|
|
78
|
-
const fileName = truncatePath(moduleName);
|
|
79
|
-
currentChunk.modules.push({
|
|
80
|
-
fileNameIndex: upsertNodeIndex(stats.moduleFileNames, fileName),
|
|
81
|
-
renderedLength: mod.renderedLength,
|
|
82
|
-
virtual: moduleName.startsWith('\u0000') ? true : undefined,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
stats.importGraph.edges = Array.from(edges).map((edge) => {
|
|
87
|
-
const pair = edge.split(',');
|
|
88
|
-
return [Number.parseInt(pair[0], 10), Number.parseInt(pair[1], 10)];
|
|
89
|
-
});
|
|
90
|
-
},
|
|
91
|
-
},
|
|
92
|
-
async writeBundle(_options, bundle) {
|
|
93
|
-
for (const [_name, chunk] of Object.entries(bundle)) {
|
|
94
|
-
if (chunk.type !== 'chunk') {
|
|
95
|
-
continue;
|
|
96
|
-
}
|
|
97
|
-
const c = stats.chunks.find((c) => c.fileName === chunk.fileName);
|
|
98
|
-
if (!c) {
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
c.minifiedLength = chunk.code.length;
|
|
102
|
-
if (reportCompressed) {
|
|
103
|
-
c.compressedLength = (await compress(chunk.code)).length;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
async closeBundle(error) {
|
|
108
|
-
if (!enabled || error) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
// create a target directory
|
|
112
|
-
const target = join(root, options?.reportDirectoryName ?? REPORT_DIR_NAME);
|
|
113
|
-
try {
|
|
114
|
-
const stat = await fs.stat(target);
|
|
115
|
-
if (stat.isDirectory()) {
|
|
116
|
-
await fs.rm(target, { recursive: true });
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
catch (e) { }
|
|
120
|
-
await fs.mkdir(target);
|
|
121
|
-
if (emitHtml) {
|
|
122
|
-
const source = new URL('../dist', import.meta.url);
|
|
123
|
-
const names = await fs.readdir(source);
|
|
124
|
-
await Promise.all(names.map((name) => {
|
|
125
|
-
return fs.cp(join(source.pathname, name), join(target, name), { recursive: true });
|
|
126
|
-
}));
|
|
127
|
-
let html = await fs.readFile(join(target, 'index.html'), 'utf-8');
|
|
128
|
-
html = html.replace('%BUNDLE_STATS%', JSON.stringify(stats).replaceAll("'", "\\'"));
|
|
129
|
-
fs.writeFile(join(target, 'index.html'), html);
|
|
130
|
-
}
|
|
131
|
-
if (emitJson) {
|
|
132
|
-
fs.writeFile(join(target, 'stats.json'), JSON.stringify(stats, null, 2));
|
|
133
|
-
}
|
|
134
|
-
console.log(`Bundle stats saved to ${target}`);
|
|
135
|
-
if (emitHtml) {
|
|
136
|
-
console.log(`Run "npx vite-bundle-explorer ${target}" to view the stats`);
|
|
137
|
-
}
|
|
138
|
-
},
|
|
139
|
-
};
|
|
140
|
-
return plugin;
|
|
141
|
-
}
|
|
File without changes
|