-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathcompile-images.ts
More file actions
89 lines (77 loc) · 3.25 KB
/
compile-images.ts
File metadata and controls
89 lines (77 loc) · 3.25 KB
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
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
import { build } from "esbuild";
/**
* Compiles the initialization code for the workerd runtime
*/
export async function compileImages(options: BuildOptions): Promise<void> {
const currentDir = path.join(path.dirname(fileURLToPath(import.meta.url)));
const templatesDir = path.join(currentDir, "../../templates");
const imagesPath = path.join(templatesDir, "images.js");
const imagesManifestPath = path.join(options.appBuildOutputPath, ".next/images-manifest.json");
const imagesManifest = fs.existsSync(imagesManifestPath)
? JSON.parse(fs.readFileSync(imagesManifestPath, { encoding: "utf-8" }))
: {};
const __IMAGES_REMOTE_PATTERNS__ = JSON.stringify(imagesManifest?.images?.remotePatterns ?? []);
const __IMAGES_LOCAL_PATTERNS__ = JSON.stringify(
imagesManifest?.images?.localPatterns ?? defaultLocalPatterns
);
const __IMAGES_DEVICE_SIZES__ = JSON.stringify(imagesManifest?.images?.deviceSizes ?? defaultDeviceSizes);
const __IMAGES_IMAGE_SIZES__ = JSON.stringify(imagesManifest?.images?.imageSizes ?? defaultImageSizes);
const __IMAGES_QUALITIES__ = JSON.stringify(imagesManifest?.images?.qualities ?? defaultQualities);
const __IMAGES_FORMATS__ = JSON.stringify(imagesManifest?.images?.formats ?? defaultFormats);
const __IMAGES_MINIMUM_CACHE_TTL_SEC__ = JSON.stringify(
imagesManifest?.images?.minimumCacheTTL ?? defaultMinimumCacheTTLSec
);
const __IMAGES_ALLOW_SVG__ = JSON.stringify(Boolean(imagesManifest?.images?.dangerouslyAllowSVG));
const __IMAGES_CONTENT_SECURITY_POLICY__ = JSON.stringify(
imagesManifest?.images?.contentSecurityPolicy ?? "script-src 'none'; frame-src 'none'; sandbox;"
);
const __IMAGES_CONTENT_DISPOSITION__ = JSON.stringify(
imagesManifest?.images?.contentDispositionType ?? "attachment"
);
const __IMAGES_MAX_REDIRECTS__ = JSON.stringify(
imagesManifest?.images?.maximumRedirects ?? defaultMaxRedirects
);
await build({
entryPoints: [imagesPath],
outdir: path.join(options.outputDir, "cloudflare"),
bundle: true,
minify: false,
format: "esm",
target: "esnext",
platform: "node",
define: {
__IMAGES_REMOTE_PATTERNS__,
__IMAGES_LOCAL_PATTERNS__,
__IMAGES_DEVICE_SIZES__,
__IMAGES_IMAGE_SIZES__,
__IMAGES_QUALITIES__,
__IMAGES_FORMATS__,
__IMAGES_MINIMUM_CACHE_TTL_SEC__,
__IMAGES_ALLOW_SVG__,
__IMAGES_CONTENT_SECURITY_POLICY__,
__IMAGES_CONTENT_DISPOSITION__,
__IMAGES_MAX_REDIRECTS__,
},
});
}
const defaultDeviceSizes = [640, 750, 828, 1080, 1200, 1920, 2048, 3840];
// 16 was included in Next.js 15
const defaultImageSizes = [32, 48, 64, 96, 128, 256, 384];
// All values between 1-100 were allowed in Next.js 15
const defaultQualities = [75];
// Was unlimited in Next.js 15
const defaultMaxRedirects = 3;
const defaultFormats = ["image/webp"];
const defaultMinimumCacheTTLSec = 14400;
// Allow any local image when no localPatterns are specified.
// This regexp was generated using the image manifest generated by setting the local patterns
// to [{ pathname: "/**" }] in the next config.
const defaultLocalPatterns = [
{
pathname: "^(?:\\/(?!\\.{1,2}(?:\\/|$))(?:(?:(?!(?:^|\\/)\\.{1,2}(?:\\/|$)).)*?))$",
},
];