Skip to content

Commit

Permalink
Merge pull request #18 from vio/fix-write-type
Browse files Browse the repository at this point in the history
Fix write option
  • Loading branch information
vio authored Dec 26, 2024
2 parents 1c4dc03 + 2404ad2 commit 6b8ec77
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
31 changes: 5 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import path from 'node:path';
import process from 'node:process';
import fs from 'node:fs/promises';
import type { Plugin } from 'rollup';

import extractRollupStats, { type Stats, type StatsOptions } from './extract';
import extractRollupStats, { type StatsOptions } from './extract';
import { type RollupStatsWrite, rollupStatsWrite } from './write';
import { formatFileSize } from './utils/format-file-size';

const PLUGIN_NAME = 'rollupStats';
const DEFAULT_FILE_NAME = 'stats.json';

export type RollupStatsWriteResponse = {
filepath: string;
content: string;
};

export type RollupStatsWrite = (filepath: string, stats: Stats) => RollupStatsWriteResponse;

export type RollupStatsOptions = {
/**
* Output filename relative to Rollup output directory or absolute
Expand All @@ -34,7 +27,7 @@ export type RollupStatsOptions = {
};

function rollupStats(options: RollupStatsOptions = {}): Plugin {
const { fileName, stats: statsOptions, writer = rollupStatsWrite } = options;
const { fileName, stats: statsOptions, write = rollupStatsWrite } = options;

return {
name: PLUGIN_NAME,
Expand All @@ -47,11 +40,11 @@ function rollupStats(options: RollupStatsOptions = {}): Plugin {
const stats = extractRollupStats(bundle, statsOptions);

try {
const res = await writer(filepath, stats);
const res = await write(filepath, stats);
const outputSize = Buffer.byteLength(res.content, 'utf-8');

this.info(`Stats saved to ${res.filepath} (${formatFileSize(outputSize)})`);
} catch (error) {
} catch (error: any) { // eslint-disable-line
// Log error, but do not throw to allow the compilation to continue
this.warn(error);
}
Expand All @@ -61,17 +54,3 @@ function rollupStats(options: RollupStatsOptions = {}): Plugin {

export default rollupStats;

async function rollupStatsWrite(filepath: string, stats: Stats): WriteInfo {
const content = JSON.stringify(stats, null, 2);

// Create base directory if it does not exist
await fs.mkdir(path.dirname(filepath), { recursive: true });

await fs.writeFile(filepath, content);

return {
filepath,
content,
};
}

28 changes: 28 additions & 0 deletions src/write.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from 'node:path';
import fs from 'node:fs/promises';

export type RollupStatsWriteResponse = {
filepath: string;
content: string;
};

export type RollupStatsWrite = (
filepath: string,
stats: Record<string, unknown>
) => RollupStatsWriteResponse;

export async function rollupStatsWrite<
T extends Record<string, unknown> = Record<string, unknown>,
>(filepath: string, stats: T): Promise<RollupStatsWriteResponse> {
const content = JSON.stringify(stats, null, 2);

// Create base directory if it does not exist
await fs.mkdir(path.dirname(filepath), { recursive: true });

await fs.writeFile(filepath, content);

return {
filepath,
content,
};
}

0 comments on commit 6b8ec77

Please sign in to comment.