Skip to content

Commit b5c6631

Browse files
authored
feat: add formatName option (#5)
1 parent 47f7d1a commit b5c6631

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ Filename for the report. Defaults to `report.html`.
6161

6262
Directory for the output. Defaults to `.vite-source-map-visualizer`.
6363

64+
### `formatName`
65+
66+
Format name of the files. For example to remove `process.cwd()`:
67+
68+
```ts
69+
export default defineConfig({
70+
plugins: [
71+
sourcemapVisualizer({
72+
formatName: (filename) => filename.replace(process.cwd(), "/<root>"),
73+
}),
74+
],
75+
});
76+
```
77+
6478
## Credits
6579

6680
Special thanks to:

src/plugin.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1821
interface 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+
"&": "&amp;",
173+
"<": "&lt;",
174+
">": "&gt;",
175+
"'": "&#39;",
176+
'"': "&quot;",
177+
}[tag])
178+
);
179+
}

src/report.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ export function script() {
1717
localStorage.setItem("theme", next);
1818
});
1919

20+
const menu = document.querySelector("button#menu")!;
21+
menu.addEventListener("click", () => {
22+
const fileList = document.querySelector("details#files" as "details")!;
23+
fileList.open = !fileList.open;
24+
});
25+
2026
const url = new URL(window.location.href);
2127
const filename = url.searchParams.get("filename");
2228

src/styles.css

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
:root[data-theme="light"] {
22
--text: rgba(60, 60, 67);
3-
--text-accaccent: #3451b2;
3+
--text-accent: #3451b2;
44
--bg: #ffffff;
5+
--bg-invert: #1b1b1f;
56
--border: #c2c2c4;
67
}
78

89
:root[data-theme="dark"] {
9-
--text-accaccent: #a8b1ff;
10+
--text-accent: #a8b1ff;
1011
--text: rgba(255, 255, 245, 0.86);
1112
--bg: #1b1b1f;
13+
--bg-invert: #ffffff;
1214
--border: #3c3f44;
1315
}
1416

@@ -46,7 +48,8 @@ h1 {
4648
margin: 2rem;
4749
}
4850

49-
button#theme-toggle {
51+
button#theme-toggle,
52+
button#menu {
5053
width: 3rem;
5154
height: 3rem;
5255
border: 0;
@@ -55,33 +58,28 @@ button#theme-toggle {
5558
background-color: transparent;
5659
position: absolute;
5760
top: 1rem;
58-
right: 1rem;
5961
cursor: pointer;
62+
fill: var(--bg-invert);
6063
}
6164

62-
#theme-icon-light {
63-
fill: #ffffff;
65+
button#theme-toggle {
66+
right: 1rem;
6467
}
6568

66-
#theme-icon-dark {
67-
fill: #1b1b1f;
69+
button#menu {
70+
right: 5rem;
6871
}
6972

7073
a {
71-
color: var(--text-accaccent);
74+
color: var(--text-accent);
7275
}
7376

7477
details#files {
7578
margin: 2rem;
7679
}
7780

78-
summary {
79-
display: inline-block;
80-
list-style: none;
81-
color: var(--text-accaccent);
82-
text-decoration: underline;
83-
cursor: pointer;
84-
margin-bottom: 1rem;
81+
details#files summary {
82+
display: none;
8583
}
8684

8785
table {

0 commit comments

Comments
 (0)