Skip to content

Commit

Permalink
Add 'gzip' plugin lumeland#680
Browse files Browse the repository at this point in the history
  • Loading branch information
into-the-v0id committed Oct 24, 2024
1 parent a8f4ce5 commit 94017b7
Show file tree
Hide file tree
Showing 8 changed files with 1,966 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
- New plugin: `check_urls` to detect broken links [#675].
- New plugin: `icons` to load automatically icons from popular icon catalogs.
- New plugin: `google_fonts` to download and self-host automatically fonts from Google Fonts.
- New plugin: `gzip` to compress files. [#680]

### Fixed
- Nav plugin: Breadcrumb with urls with CJK characters.
Expand Down Expand Up @@ -564,6 +565,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
[#677]: https://github.com/lumeland/lume/issues/677
[#678]: https://github.com/lumeland/lume/issues/678
[#679]: https://github.com/lumeland/lume/issues/679
[#680]: https://github.com/lumeland/lume/issues/680

[Unreleased]: https://github.com/lumeland/lume/compare/v2.3.3...HEAD
[2.3.3]: https://github.com/lumeland/lume/compare/v2.3.2...v2.3.3
Expand Down
2 changes: 1 addition & 1 deletion core/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type Processor = (
allPages: Page[],
) => void | false | Promise<void | false>;

function pageMatches(exts: Extensions, page: Page): boolean {
export function pageMatches(exts: Extensions, page: Page): boolean {
if (exts === "*") {
return true;
}
Expand Down
1 change: 1 addition & 0 deletions core/utils/lume_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const pluginNames = [
"fff",
"filter_pages",
"google_fonts",
"gzip",
"inline",
"jsx",
"jsx_preact",
Expand Down
4 changes: 4 additions & 0 deletions deps/foras.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Foras } from "https://deno.land/x/[email protected]/src/deno/mod.ts";
export * from "https://deno.land/x/[email protected]/src/deno/mod.ts";

await Foras.initBundledOnce();
57 changes: 57 additions & 0 deletions plugins/gzip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { merge } from "../core/utils/object.ts";
import { Page } from "../core/file.ts";
import { pageMatches } from "../core/processors.ts";
import { gzip as compress, Memory } from "../deps/foras.ts";

import type { Extensions } from "../core/utils/path.ts";
import type Site from "../core/site.ts";

export interface Options {
/** The list of extensions this plugin applies to */
extensions?: Extensions;

/**
* Level param between 0 and 9 (9 is the smallest but takes the longest to encode)
*/
level?: number;
}

// Default options
export const defaults: Options = {
extensions: [".html", ".css", ".js", ".mjs", ".svg", ".json", ".xml", ".txt"],
level: 6,
};

/**
* A plugin to compress files with gzip
*/
export function gzip(userOptions?: Options) {
const options = merge(defaults, userOptions);

return (site: Site) => {
site.addEventListener("beforeSave", () => {
const textEncoder = new TextEncoder();

for (const page of site.pages) {
if (!pageMatches(options.extensions, page)) {
continue;
}

const content = page.content as string;
const compressedContent = compress(
new Memory(textEncoder.encode(content)),
options.level,
).copyAndDispose();

const compressedPage = Page.create({
url: page.outputPath + ".gz",
content: compressedContent,
});

site.pages.push(compressedPage);
}
});
};
}

export default gzip;
Loading

0 comments on commit 94017b7

Please sign in to comment.