Skip to content

Commit

Permalink
feat: add silent option
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Aug 31, 2024
1 parent a32c510 commit fcc5f4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Filename for the report. Defaults to `report.html`.

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

### `silent`

Silence plugin's verbose logging.

### `formatName`

Format name of the files:
Expand Down
13 changes: 12 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "node:fs/promises";
import { join } from "node:path";
import type { Plugin } from "vite";
import type { Logger, Plugin } from "vite";

import { toVisualizer } from "./generate-link.js";
import { script, style } from "./report.js";
Expand All @@ -16,6 +16,9 @@ interface Options {

/** Format name of the transformed file */
formatName?: (filename: string) => string;

/** Silence verbose logging. */
silent?: boolean;
}

interface Result {
Expand All @@ -28,6 +31,7 @@ interface Result {
* Generate HTML report for inspecting the transformed files in https://evanw.github.io/source-map-visualization/
*/
export function sourcemapVisualizer(options?: Options): Plugin {
let logger: Logger;
const results: Result[] = [];

const outDir = join(
Expand All @@ -37,6 +41,7 @@ export function sourcemapVisualizer(options?: Options): Plugin {

const reportName = options?.filename || "report.html";
const formatName = options?.formatName || defaultFormatName;
const silent = options?.silent || false;

return {
name: PLUGIN_NAME,
Expand All @@ -48,6 +53,8 @@ export function sourcemapVisualizer(options?: Options): Plugin {
},

configResolved(config) {
logger = config.logger;

try {
const index = config.plugins.findIndex(
(plugin) => plugin.name === PLUGIN_NAME
Expand Down Expand Up @@ -78,6 +85,10 @@ export function sourcemapVisualizer(options?: Options): Plugin {
const filename = `${outDir}/${reportName}`;
const html = generateHTML(results);
await fs.writeFile(filename, html, "utf8");

if (!silent) {
logger.info(`Report written to ${filename}`);
}
} catch (error) {
console.error(error);
throw error;
Expand Down

0 comments on commit fcc5f4a

Please sign in to comment.