Skip to content

fix: use URL hash for local links, SPA mode #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function sourcemapVisualizer(options?: Options): Plugin {
async buildEnd() {
try {
const filename = `${outDir}/${reportName}`;
const html = generateHTML(results, filename);
const html = generateHTML(results);
await fs.writeFile(filename, html, "utf8");
} catch (error) {
console.error(error);
Expand All @@ -86,7 +86,7 @@ export function sourcemapVisualizer(options?: Options): Plugin {
};
}

function generateHTML(results: Result[], root: string) {
function generateHTML(results: Result[]) {
// prettier-ignore
return `
<!DOCTYPE html>
Expand All @@ -102,7 +102,7 @@ function generateHTML(results: Result[], root: string) {
<body>
<main>
<h1>
<a href="${root}">Vite Source Map Visualizer</a>
<a href="#">Vite Source Map Visualizer</a>
</h1>

<button id="menu" title="Toggle file list">
Expand Down Expand Up @@ -130,7 +130,7 @@ function generateHTML(results: Result[], root: string) {
${results.map((result) => `
<tr>
<td>
<a href="${root}?filename=${result.filename}#${result.hash}">
<a href="#${result.hash}">
${escapeHTML(result.filename)}
</a>
</td>
Expand Down
20 changes: 13 additions & 7 deletions src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@ export function script() {
fileList.open = !fileList.open;
});

const url = new URL(window.location.href);
const filename = url.searchParams.get("filename");
initializePage();
addEventListener("hashchange", initializePage);

if (filename) {
function initializePage() {
const fileList = document.querySelector("details#files" as "details")!;
const iframe = document.querySelector(
"iframe#source-map-visualizer" as "iframe"
)!;
iframe.src = `https://evanw.github.io/source-map-visualization${url.hash}`;
iframe.style.display = "block";

const fileList = document.querySelector("details#files" as "details")!;
fileList.open = false;
if (window.location.hash) {
iframe.src = `https://evanw.github.io/source-map-visualization${window.location.hash}`;
iframe.style.display = "block";
fileList.open = false;
} else {
iframe.src = "";
iframe.style.display = "none";
fileList.open = true;
}
}
}
/* v8 ignore stop */
Expand Down