Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'gzip' plugin #680

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
1 change: 1 addition & 0 deletions deps/streams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "jsr:@std/[email protected]";
53 changes: 53 additions & 0 deletions plugins/gzip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { merge } from "../core/utils/object.ts";
import { concurrent } from "../core/utils/concurrent.ts";
import { Page } from "../core/file.ts";
import { toArrayBuffer } from "../deps/streams.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;
}

// Default options
export const defaults: Options = {
extensions: [".html", ".css", ".js", ".mjs", ".svg", ".json", ".xml", ".txt"],
oscarotero marked this conversation as resolved.
Show resolved Hide resolved
};

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

return (site: Site) => {
const textEncoder = new TextEncoder();

site.process(
options.extensions,
(pages, allPages) =>
concurrent(pages, async (page: Page) => {
const content = page.content!;
const contentStream = ReadableStream.from([
typeof content === "string" ? textEncoder.encode(content) : content,
]);

const compressedStream = contentStream.pipeThrough(
new CompressionStream("gzip"),
);
const compressedArrayBuffer = await toArrayBuffer(compressedStream);
const compressedContent = new Uint8Array(compressedArrayBuffer);

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

export default gzip;
Loading