Skip to content

Commit

Permalink
Use AstroIntegrationLogger over custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sondr3 committed Nov 28, 2024
1 parent 4853f21 commit 8892cf1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 38 deletions.
11 changes: 7 additions & 4 deletions src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,6 +34,7 @@ const compress = async <T extends NodeJS.WritableStream>(
name: string,
compressedFileNames: string,
compressor: () => T,
logger: AstroIntegrationLogger,
{ dir, extensions, batchSize, enabled }: CompressionOptions,
): Promise<void> => {
if (!enabled) {
Expand All @@ -60,23 +61,25 @@ const compress = async <T extends NodeJS.WritableStream>(
}

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<string>,
enabled?: boolean,
batchSize = 10,
): Promise<void> => {
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<string>,
enabled?: boolean,
batchSize = 10,
): Promise<void> => {
await compress("brotli", "br", createBrotliCompress, { dir, extensions, enabled, batchSize });
await compress("brotli", "br", createBrotliCompress, logger, { dir, extensions, enabled, batchSize });
};
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down Expand Up @@ -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");
},
},
};
Expand Down
27 changes: 0 additions & 27 deletions src/logger.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 8892cf1

Please sign in to comment.