-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
49 lines (44 loc) · 1.86 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { Plugin } from 'vite';
import * as path from 'path';
import { BundleFs } from './modules/fs/implementations/bundle-fs';
import { BundleParser } from './modules/parser/implementations/bundle-parser';
import { BundleCompare } from './modules/compare/implementations/bundle-compare';
import {
ConsoleBundleCompareView,
} from './modules/view/implementations/console-bundle-compare-view';
export type VitePluginBundleMonitoringOptions = {
compareFileDir?: string;
compareFileName?: string;
}
export default function (options: VitePluginBundleMonitoringOptions): Plugin {
const {
compareFileDir = __dirname,
compareFileName = 'vite-bundle-monitoring.compare.json',
} = options;
const filesPaths: string[] = [];
return {
name : 'vite-plugin-bundle-monitoring',
apply : 'build',
generateBundle: (_, bundle) => {
if (_.dir) {
Object.keys(bundle).forEach((bundleFilePath) => {
filesPaths.push(path.join(_.dir!, bundleFilePath));
});
}
},
closeBundle : () => {
const bundleFs = new BundleFs(compareFileDir, compareFileName);
const bundleParser = new BundleParser();
const bundleCompare = new BundleCompare();
const bundleView = new ConsoleBundleCompareView();
const previousBundleInfo = bundleFs.read();
const currentBundleInfo = bundleParser.parse(filesPaths);
const previousValueExist = previousBundleInfo !== null;
if (!previousValueExist) {
bundleFs.write(currentBundleInfo);
}
const compareResult = bundleCompare.compare(previousBundleInfo ?? {}, currentBundleInfo);
bundleView.render(compareResult);
},
};
};