From 1ae7cd905edf8fdb6ad9a0e12f227f0259c73643 Mon Sep 17 00:00:00 2001 From: Grant Montgomery Date: Sun, 22 Jan 2023 18:51:24 -0600 Subject: [PATCH 1/5] initial commit --- packages/adapter-vercel/index.d.ts | 24 ++++++++++++++++++++++++ packages/adapter-vercel/index.js | 9 +++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/adapter-vercel/index.d.ts b/packages/adapter-vercel/index.d.ts index 49e4f9661985..d31b0c7420ac 100644 --- a/packages/adapter-vercel/index.d.ts +++ b/packages/adapter-vercel/index.d.ts @@ -4,6 +4,30 @@ type Options = { edge?: boolean; external?: string[]; split?: boolean; + platform?: VercelConfig; }; +type ImageFormat = 'image/avif' | 'image/webp'; + +type RemotePattern = { + protocol?: 'http' | 'https'; + hostname: string; + port?: string; + pathname?: string; +}; + +type ImagesConfig = { + sizes: number[]; + domains: string[]; + remotePatterns?: RemotePattern[]; + minimumCacheTTL?: number; // seconds + formats?: ImageFormat[]; + dangerouslyAllowSVG?: boolean; + contentSecurityPolicy?: string; +}; + +type VercelConfig = { + images?: ImagesConfig, +} + export default function plugin(options?: Options): Adapter; diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 529225d7cf28..843cffc49549 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -5,7 +5,7 @@ import { nodeFileTrace } from '@vercel/nft'; import esbuild from 'esbuild'; /** @type {import('.').default} **/ -const plugin = function ({ external = [], edge, split } = {}) { +const plugin = function ({ external = [], edge, split, platform } = {}) { return { name: '@sveltejs/adapter-vercel', @@ -134,6 +134,10 @@ const plugin = function ({ external = [], edge, split } = {}) { await generate_function('render', '/.*', builder.generateManifest); } + if (platform && platform?.images) { + config.images = platform.images; + } + builder.log.minor('Copying assets...'); builder.writeClient(dirs.static); @@ -219,7 +223,8 @@ function static_vercel_config(builder) { handle: 'filesystem' } ], - overrides + overrides, + images: {}, }; } From 55a2467f97575bd8000cc94e202892a8a999b30a Mon Sep 17 00:00:00 2001 From: Grant Montgomery Date: Sun, 22 Jan 2023 22:53:15 -0600 Subject: [PATCH 2/5] feat: expose image optimization config in adapter-vercel --- .changeset/flat-clocks-listen.md | 5 +++++ packages/adapter-vercel/index.d.ts | 26 +++++++++++++------------- packages/adapter-vercel/index.js | 7 +++++-- 3 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 .changeset/flat-clocks-listen.md diff --git a/.changeset/flat-clocks-listen.md b/.changeset/flat-clocks-listen.md new file mode 100644 index 000000000000..c7fe30360fa7 --- /dev/null +++ b/.changeset/flat-clocks-listen.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-vercel': minor +--- + +feat: expose vercel image optimization config in adapter config diff --git a/packages/adapter-vercel/index.d.ts b/packages/adapter-vercel/index.d.ts index d31b0c7420ac..435da2376716 100644 --- a/packages/adapter-vercel/index.d.ts +++ b/packages/adapter-vercel/index.d.ts @@ -10,24 +10,24 @@ type Options = { type ImageFormat = 'image/avif' | 'image/webp'; type RemotePattern = { - protocol?: 'http' | 'https'; - hostname: string; - port?: string; - pathname?: string; + protocol?: 'http' | 'https'; + hostname: string; + port?: string; + pathname?: string; }; type ImagesConfig = { - sizes: number[]; - domains: string[]; - remotePatterns?: RemotePattern[]; - minimumCacheTTL?: number; // seconds - formats?: ImageFormat[]; - dangerouslyAllowSVG?: boolean; - contentSecurityPolicy?: string; + sizes: number[]; + domains: string[]; + remotePatterns?: RemotePattern[]; + minimumCacheTTL?: number; // seconds + formats?: ImageFormat[]; + dangerouslyAllowSVG?: boolean; + contentSecurityPolicy?: string; }; type VercelConfig = { - images?: ImagesConfig, -} + images: ImagesConfig; +}; export default function plugin(options?: Options): Adapter; diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 843cffc49549..063fb5bc9815 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -5,7 +5,7 @@ import { nodeFileTrace } from '@vercel/nft'; import esbuild from 'esbuild'; /** @type {import('.').default} **/ -const plugin = function ({ external = [], edge, split, platform } = {}) { +const plugin = function ({ external = [], edge, split, platform = {} } = {}) { return { name: '@sveltejs/adapter-vercel', @@ -186,6 +186,9 @@ function static_vercel_config(builder) { /** @type {Record} */ const overrides = {}; + /** @type {import('./index').ImagesConfig} */ + const images = undefined; + for (const [src, redirect] of builder.prerendered.redirects) { prerendered_redirects.push({ src, @@ -224,7 +227,7 @@ function static_vercel_config(builder) { } ], overrides, - images: {}, + images }; } From 295ae4caaa0881f22a2c2a0dbeb27ad8938cdf21 Mon Sep 17 00:00:00 2001 From: Grant Montgomery Date: Mon, 23 Jan 2023 10:16:12 -0600 Subject: [PATCH 3/5] add image optimization to adapter-vercel documentation --- .../docs/25-build-and-deploy/90-adapter-vercel.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/documentation/docs/25-build-and-deploy/90-adapter-vercel.md b/documentation/docs/25-build-and-deploy/90-adapter-vercel.md index d0161e1d5e14..235b98afe9e4 100644 --- a/documentation/docs/25-build-and-deploy/90-adapter-vercel.md +++ b/documentation/docs/25-build-and-deploy/90-adapter-vercel.md @@ -30,7 +30,12 @@ export default { // if true, will split your app into multiple functions // instead of creating a single one for the entire app - split: false + split: false, + + // vercel build output config. At this time only the images + // property is supported. + // (https://vercel.com/docs/build-output-api/v3#build-output-configuration) + platform: {} }) } }; From f96a3f5db937f65fe93e430e2e5d017d96dabd84 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 9 Oct 2023 10:12:10 -0700 Subject: [PATCH 4/5] add new option --- packages/adapter-vercel/index.d.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/adapter-vercel/index.d.ts b/packages/adapter-vercel/index.d.ts index f8cfb21d31a5..d3cfe25a9396 100644 --- a/packages/adapter-vercel/index.d.ts +++ b/packages/adapter-vercel/index.d.ts @@ -76,10 +76,7 @@ type ImagesConfig = { formats?: ImageFormat[]; dangerouslyAllowSVG?: boolean; contentSecurityPolicy?: string; -}; - -type VercelConfig = { - images: ImagesConfig; + contentDispositionType?: string; }; export interface EdgeConfig { From 885ba69eb4dc71bc4c6788b6eb4daa4499ba8ed2 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 9 Oct 2023 10:17:17 -0700 Subject: [PATCH 5/5] initialize variable --- packages/adapter-vercel/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 24fad1082b7e..6b38530a6370 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -55,7 +55,7 @@ const plugin = function (defaults = {}) { functions: `${dir}/functions` }; - const static_config = static_vercel_config(builder); + const static_config = static_vercel_config(builder, defaults); builder.log.minor('Generating serverless function...'); @@ -368,8 +368,11 @@ function write(file, data) { } // This function is duplicated in adapter-static -/** @param {import('@sveltejs/kit').Builder} builder */ -function static_vercel_config(builder) { +/** + * @param {import('@sveltejs/kit').Builder} builder + * @param {import('.').Config} config + */ +function static_vercel_config(builder, config) { /** @type {any[]} */ const prerendered_redirects = []; @@ -377,7 +380,7 @@ function static_vercel_config(builder) { const overrides = {}; /** @type {import('./index').ImagesConfig} */ - const images = undefined; + const images = config.images; for (const [src, redirect] of builder.prerendered.redirects) { prerendered_redirects.push({