This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fresh.config.ts
153 lines (136 loc) · 3.99 KB
/
fresh.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { defineConfig } from "$fresh/server.ts";
import { ensureDir } from "$std/fs/ensure_dir.ts";
import ImagesPlugin, {
extendKeyMap,
getParam,
transform,
} from "fresh_images/mod.ts";
import { crop, resize, rotate } from "fresh_images/transformer.ts";
import type { ImagesPluginOptions } from "fresh_images/src/types.ts";
import { decode, GIF, Image } from "imagescript/mod.ts";
import caption from "./transformers/canvasExample.ts";
import { basename, extname, join } from "$std/path/mod.ts";
// Website dependencies
import postcss from "https://deno.land/x/postcss@8.4.16/mod.js";
import autoprefixer from "npm:autoprefixer";
import tailwind from "npm:tailwindcss";
/**
* Process Tailwindcss stylesheets.
* (`.fresh-image` components do not have styles by default.)
*/
async function processTailwind() {
const plugins = [
autoprefixer,
tailwind({
content: [
"./routes/**/*.{tsx,jsx,ts,js}",
"./islands/**/*.{tsx,jsx,ts,js}",
"./components/**/*.{tsx,jsx,ts,js}",
],
}),
];
const stylesheet = await Deno.readTextFile("./style.css");
const processed = (await postcss(plugins).process(stylesheet, {
from: "./style.css",
to: "./static/style.css",
})).css;
await Deno.writeTextFile("./static/style.css", processed);
}
/**
* Custom transformer example.
* Rotate an image hue by a random number of degrees.\
* Optionally accept a query parameter to invert the hue. (Requires extending the key map.)
*/
const myTransformer = async (img: Image | GIF, req: Request) => {
const randomDegrees = Math.floor(Math.random() * 360);
const invert = getParam(req, "invert", extendKeyMap({ invert: "i" }));
if (invert) {
img = await transform(
img,
(img) => Promise.resolve((img as Image).invertHue()),
);
}
return transform(
img,
(img) => Promise.resolve((img as Image).hueShift(randomDegrees)),
);
};
/**
* Pre-optimize images before serving them.
*/
const myBuildFunction: ImagesPluginOptions["build"] = async ({
realPath,
}) => {
const targetDir = realPath ?? "./static";
const files = Deno.readDir("./static/image");
await ensureDir(targetDir);
for await (const file of files) {
if (!file.isFile) {
continue;
}
const input = await Deno.readFile(`./static/image/${file.name}`);
const output = await transform(
await decode(input),
(img) => Promise.resolve((img as Image).resize(Image.RESIZE_AUTO, 100)),
);
// Encode at lowest quality
const fileName = basename(file.name, extname(file.name));
if (output instanceof GIF) {
await Deno.writeFile(
join(targetDir, `${fileName}.gif`),
await output.encode(30),
);
continue;
}
await Deno.writeFile(
join(targetDir, `${fileName}.jpg`),
await output.encodeJPEG(1),
);
}
};
export default defineConfig({
plugins: [
ImagesPlugin({
// This creates a route, "/img", that will serve images from the "./static/image" directory.
route: "/img",
// Pass the transformers to "install"
transformers: {
resize,
rotate,
crop,
// Custom transformers
caption,
cool: myTransformer,
withRoute: {
path: "/desaturated",
handler: (img: Image | GIF) =>
transform(
img,
(img) => Promise.resolve((img as Image).saturation(0.25, true)),
),
},
},
middleware: {
rateLimit: 1000,
},
}),
ImagesPlugin({
// Create a different route for the placeholder images. (Nested directory routes are currently not supported.)
route: "/img/placeholder",
realPath: "./static/placeholders",
// Only specified transformers will be available on this route.
transformers: {
cool: myTransformer,
},
build: myBuildFunction,
middleware: {
rateLimit: 1000,
rateLimitDuration: 30,
},
}),
{
name: "tailwind",
buildStart: processTailwind,
},
],
});