forked from lumeland/lume
-
Notifications
You must be signed in to change notification settings - Fork 0
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 94017b7
Showing
8 changed files
with
1,966 additions
and
1 deletion.
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
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,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(); |
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 { 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; |
Oops, something went wrong.