Skip to content

Commit

Permalink
Merge pull request #681 from into-the-v0id/feature/plugin-brotli
Browse files Browse the repository at this point in the history
Add 'brotli' plugin
  • Loading branch information
oscarotero authored Oct 26, 2024
2 parents e851581 + a5e7076 commit 95b612b
Show file tree
Hide file tree
Showing 7 changed files with 1,339 additions and 0 deletions.
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: `brotli` to compress files. [#681]
- New plugin: `gzip` to compress files. [#680]

### Fixed
Expand Down Expand Up @@ -565,6 +566,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
[#681]: https://github.com/lumeland/lume/issues/681
[#680]: https://github.com/lumeland/lume/issues/680

[Unreleased]: https://github.com/lumeland/lume/compare/v2.3.3...HEAD
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 @@ -2,6 +2,7 @@
export const pluginNames = [
"attributes",
"base_path",
"brotli",
"check_urls",
"code_highlight",
"date",
Expand Down
1 change: 1 addition & 0 deletions deps/brotli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/x/[email protected]/mod.ts";
57 changes: 57 additions & 0 deletions plugins/brotli.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 { compress } from "../deps/brotli.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;

/**
* Quality param between 0 and 11 (11 is the smallest but takes the longest to encode)
*/
quality?: number;
}

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

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

return (site: Site) => {
site.process(options.extensions, (pages, allPages) => {
const textEncoder = new TextEncoder();

for (const page of pages) {
const content = page.content!;

const contentByteArray = typeof content === "string"
? textEncoder.encode(content)
: content;

const compressedContent = compress(
contentByteArray,
undefined,
options.quality,
);

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

export default brotli;
Loading

0 comments on commit 95b612b

Please sign in to comment.