diff --git a/src/compress.ts b/src/compress.ts index 5e7acfb..9d5a2a2 100644 --- a/src/compress.ts +++ b/src/compress.ts @@ -5,7 +5,7 @@ import { hrtime } from "node:process"; import { promises as stream } from "node:stream"; import { createBrotliCompress, createGzip } from "node:zlib"; -import * as logger from "./logger.js"; +import type { AstroIntegrationLogger } from "astro"; interface CompressionOptions { dir: string; @@ -34,6 +34,7 @@ const compress = async ( name: string, compressedFileNames: string, compressor: () => T, + logger: AstroIntegrationLogger, { dir, extensions, batchSize, enabled }: CompressionOptions, ): Promise => { if (!enabled) { @@ -60,23 +61,25 @@ const compress = async ( } const end = hrtime.bigint(); - logger.success(`finished ${name} of ${files.length} files in ${(end - start) / BigInt(1000000)}ms`); + logger.info(`${name.padEnd(8, " ")} compressed ${files.length} files in ${(end - start) / BigInt(1000000)}ms`); }; export const gzip = async ( dir: string, + logger: AstroIntegrationLogger, extensions: Array, enabled?: boolean, batchSize = 10, ): Promise => { - await compress("gzip", "gz", createGzip.bind({ level: 9 }), { dir, extensions, enabled, batchSize }); + await compress("gzip", "gz", createGzip.bind({ level: 9 }), logger, { dir, extensions, enabled, batchSize }); }; export const brotli = async ( dir: string, + logger: AstroIntegrationLogger, extensions: Array, enabled?: boolean, batchSize = 10, ): Promise => { - await compress("brotli", "br", createBrotliCompress, { dir, extensions, enabled, batchSize }); + await compress("brotli", "br", createBrotliCompress, logger, { dir, extensions, enabled, batchSize }); }; diff --git a/src/index.ts b/src/index.ts index eb93df8..5d40422 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,6 @@ import { fileURLToPath } from "node:url"; import type { AstroIntegration } from "astro"; import { brotli, gzip } from "./compress.js"; -import * as logger from "./logger.js"; const defaultFileExtensions = [".css", ".js", ".html", ".xml", ".cjs", ".mjs", ".svg", ".txt"]; @@ -31,13 +30,13 @@ export default function (opts: Options = defaultOptions): AstroIntegration { return { name: "astro-compressor", hooks: { - "astro:build:done": async ({ dir }) => { + "astro:build:done": async ({ dir, logger }) => { const path = fileURLToPath(dir); await Promise.allSettled([ - gzip(path, options.fileExtensions, options.gzip, options.batchSize), - brotli(path, options.fileExtensions, options.brotli, options.batchSize), + gzip(path, logger, options.fileExtensions, options.gzip, options.batchSize), + brotli(path, logger, options.fileExtensions, options.brotli, options.batchSize), ]); - logger.success("Compression finished\n"); + logger.info("Compression finished\n"); }, }, }; diff --git a/src/logger.ts b/src/logger.ts deleted file mode 100644 index 576da7f..0000000 --- a/src/logger.ts +++ /dev/null @@ -1,27 +0,0 @@ -const COLORS = { - reset: "\x1b[0m", - red: "\x1b[31m", - green: "\x1b[32m", - yellow: "\x1b[33m", -} as const; - -const format = (msg: string, prefix = ""): string => { - const end = prefix ? COLORS.reset : ""; - return `${prefix}astro-compressor:${end} ${msg}`; -}; - -export const info = (msg: string) => { - console.info(format(msg)); -}; - -export const success = (msg: string) => { - console.log(format(msg, COLORS.green)); -}; - -export const warn = (msg: string) => { - console.warn(format(msg, COLORS.yellow)); -}; - -export const error = (msg: string) => { - console.error(format(msg, COLORS.red)); -}; diff --git a/src/plugin.test.ts b/src/plugin.test.ts index 332d9d6..05cbd58 100644 --- a/src/plugin.test.ts +++ b/src/plugin.test.ts @@ -9,8 +9,8 @@ test("astro build outputs expected log", () => { cwd: path.join(process.cwd(), "test"), }); - expect(build.stdout).toContain("finished gzip of 2 files"); - expect(build.stdout).toContain("finished brotli of 2 files"); + expect(build.stdout).toContain("[astro-compressor] gzip compressed 2 files"); + expect(build.stdout).toContain("[astro-compressor] brotli compressed 2 files"); expect(build.stderr).toBeFalsy(); expect(build.status).toBe(0); });