1
1
import fs from "node:fs/promises" ;
2
2
import { join } from "node:path" ;
3
- import type { Plugin } from "vite" ;
3
+ import type { Logger , Plugin } from "vite" ;
4
4
5
5
import { toVisualizer } from "./generate-link.js" ;
6
6
import { script , style } from "./report.js" ;
@@ -16,6 +16,9 @@ interface Options {
16
16
17
17
/** Format name of the transformed file */
18
18
formatName ?: ( filename : string ) => string ;
19
+
20
+ /** Silence verbose logging. */
21
+ silent ?: boolean ;
19
22
}
20
23
21
24
interface Result {
@@ -28,6 +31,7 @@ interface Result {
28
31
* Generate HTML report for inspecting the transformed files in https://evanw.github.io/source-map-visualization/
29
32
*/
30
33
export function sourcemapVisualizer ( options ?: Options ) : Plugin {
34
+ let logger : Logger ;
31
35
const results : Result [ ] = [ ] ;
32
36
33
37
const outDir = join (
@@ -37,6 +41,7 @@ export function sourcemapVisualizer(options?: Options): Plugin {
37
41
38
42
const reportName = options ?. filename || "report.html" ;
39
43
const formatName = options ?. formatName || defaultFormatName ;
44
+ const silent = options ?. silent || false ;
40
45
41
46
return {
42
47
name : PLUGIN_NAME ,
@@ -48,6 +53,8 @@ export function sourcemapVisualizer(options?: Options): Plugin {
48
53
} ,
49
54
50
55
configResolved ( config ) {
56
+ logger = config . logger ;
57
+
51
58
try {
52
59
const index = config . plugins . findIndex (
53
60
( plugin ) => plugin . name === PLUGIN_NAME
@@ -78,6 +85,10 @@ export function sourcemapVisualizer(options?: Options): Plugin {
78
85
const filename = `${ outDir } /${ reportName } ` ;
79
86
const html = generateHTML ( results ) ;
80
87
await fs . writeFile ( filename , html , "utf8" ) ;
88
+
89
+ if ( ! silent ) {
90
+ logger . info ( `Report written to ${ filename } ` ) ;
91
+ }
81
92
} catch ( error ) {
82
93
console . error ( error ) ;
83
94
throw error ;
0 commit comments