@@ -13,6 +13,9 @@ interface Options {
1313
1414 /** Directory for the output. Defaults to `.vite-source-map-visualizer` */
1515 outDir ?: string ;
16+
17+ /** Format name of the transformed file */
18+ formatName ?: ( filename : string ) => string ;
1619}
1720
1821interface Result {
@@ -33,6 +36,7 @@ export function sourcemapVisualizer(options?: Options): Plugin {
3336 ) ;
3437
3538 const reportName = options ?. filename || "report.html" ;
39+ const formatName = options ?. formatName || defaultFormatName ;
3640
3741 return {
3842 name : PLUGIN_NAME ,
@@ -64,7 +68,7 @@ export function sourcemapVisualizer(options?: Options): Plugin {
6468 transform ( code , id , options ) {
6569 const map = this . getCombinedSourcemap ( ) ;
6670 const hash = toVisualizer ( { code, map } ) ;
67- const filename = id ;
71+ const filename = formatName ( id ) ;
6872
6973 results . push ( { filename, hash, ssr : options ?. ssr || false } ) ;
7074 } ,
@@ -101,6 +105,10 @@ function generateHTML(results: Result[], root: string) {
101105 <a href="${ root } ">Vite Source Map Visualizer</a>
102106 </h1>
103107
108+ <button id="menu" title="Toggle file list">
109+ <svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g data-name="Layer 2"><g data-name="menu"><rect width="24" height="24" transform="rotate(180 12 12)" opacity="0"/><rect x="3" y="11" width="18" height="2" rx=".95" ry=".95"/><rect x="3" y="16" width="18" height="2" rx=".95" ry=".95"/><rect x="3" y="6" width="18" height="2" rx=".95" ry=".95"/></g></g></svg>
110+ </button>
111+
104112 <button id="theme-toggle" title="Toggle theme">
105113 <svg id="theme-icon-light" class="theme-light" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g data-name="Layer 2"><g data-name="sun"><rect width="24" height="24" transform="rotate(180 12 12)" opacity="0"/><path d="M12 6a1 1 0 0 0 1-1V3a1 1 0 0 0-2 0v2a1 1 0 0 0 1 1z"/><path d="M21 11h-2a1 1 0 0 0 0 2h2a1 1 0 0 0 0-2z"/><path d="M6 12a1 1 0 0 0-1-1H3a1 1 0 0 0 0 2h2a1 1 0 0 0 1-1z"/><path d="M6.22 5a1 1 0 0 0-1.39 1.47l1.44 1.39a1 1 0 0 0 .73.28 1 1 0 0 0 .72-.31 1 1 0 0 0 0-1.41z"/><path d="M17 8.14a1 1 0 0 0 .69-.28l1.44-1.39A1 1 0 0 0 17.78 5l-1.44 1.42a1 1 0 0 0 0 1.41 1 1 0 0 0 .66.31z"/><path d="M12 18a1 1 0 0 0-1 1v2a1 1 0 0 0 2 0v-2a1 1 0 0 0-1-1z"/><path d="M17.73 16.14a1 1 0 0 0-1.39 1.44L17.78 19a1 1 0 0 0 .69.28 1 1 0 0 0 .72-.3 1 1 0 0 0 0-1.42z"/><path d="M6.27 16.14l-1.44 1.39a1 1 0 0 0 0 1.42 1 1 0 0 0 .72.3 1 1 0 0 0 .67-.25l1.44-1.39a1 1 0 0 0-1.39-1.44z"/><path d="M12 8a4 4 0 1 0 4 4 4 4 0 0 0-4-4zm0 6a2 2 0 1 1 2-2 2 2 0 0 1-2 2z"/></g></g></svg>
106114 <svg id="theme-icon-dark" class="theme-dark" <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g data-name="Layer 2"><g data-name="moon"><rect width="24" height="24" opacity="0"/><path d="M12.3 22h-.1a10.31 10.31 0 0 1-7.34-3.15 10.46 10.46 0 0 1-.26-14 10.13 10.13 0 0 1 4-2.74 1 1 0 0 1 1.06.22 1 1 0 0 1 .24 1 8.4 8.4 0 0 0 1.94 8.81 8.47 8.47 0 0 0 8.83 1.94 1 1 0 0 1 1.27 1.29A10.16 10.16 0 0 1 19.6 19a10.28 10.28 0 0 1-7.3 3zM7.46 4.92a7.93 7.93 0 0 0-1.37 1.22 8.44 8.44 0 0 0 .2 11.32A8.29 8.29 0 0 0 12.22 20h.08a8.34 8.34 0 0 0 6.78-3.49A10.37 10.37 0 0 1 7.46 4.92z"/></g></g></svg>
@@ -123,7 +131,7 @@ function generateHTML(results: Result[], root: string) {
123131 <tr>
124132 <td>
125133 <a href="${ root } ?filename=${ result . filename } #${ result . hash } ">
126- ${ result . filename }
134+ ${ escapeHTML ( result . filename ) }
127135 </a>
128136 </td>
129137 <td>
@@ -150,3 +158,22 @@ function generateHTML(results: Result[], root: string) {
150158</html>
151159` . trim ( ) ;
152160}
161+
162+ function defaultFormatName ( filename : string ) {
163+ return decodeURI ( filename ) ;
164+ }
165+
166+ function escapeHTML ( str : string ) {
167+ return str . replace (
168+ / [ & < > ' " ] / g,
169+ ( tag ) =>
170+ // @ts -expect-error -- umm...
171+ ( {
172+ "&" : "&" ,
173+ "<" : "<" ,
174+ ">" : ">" ,
175+ "'" : "'" ,
176+ '"' : """ ,
177+ } [ tag ] )
178+ ) ;
179+ }
0 commit comments