-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8f4ce5
commit 0197108
Showing
7 changed files
with
1,339 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "https://deno.land/x/[email protected]/mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.