-
-
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 ecff249
Showing
7 changed files
with
1,958 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 "jsr:@std/[email protected]"; |
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,54 @@ | ||
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 { Content } from "../core/file.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"], | ||
}; | ||
|
||
/** | ||
* 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 as 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; |
Oops, something went wrong.