From beae720a359ded66e176f6bd5ba0e893aae3c8d2 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Thu, 30 May 2024 13:58:34 -0700 Subject: [PATCH 1/6] fix: add images to edge config types --- .changeset/stupid-lies-remain.md | 5 +++++ packages/adapter-vercel/index.d.ts | 4 ++++ packages/adapter-vercel/index.js | 27 ++++++++++++++------------- packages/adapter-vercel/tsconfig.json | 3 +-- 4 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 .changeset/stupid-lies-remain.md diff --git a/.changeset/stupid-lies-remain.md b/.changeset/stupid-lies-remain.md new file mode 100644 index 000000000000..c3cf452ae00a --- /dev/null +++ b/.changeset/stupid-lies-remain.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/adapter-vercel": patch +--- + +fix: add images to edge config types diff --git a/packages/adapter-vercel/index.d.ts b/packages/adapter-vercel/index.d.ts index 6d9f8d359891..76a710d11bba 100644 --- a/packages/adapter-vercel/index.d.ts +++ b/packages/adapter-vercel/index.d.ts @@ -98,6 +98,10 @@ export interface EdgeConfig { * If `true`, this route will always be deployed as its own separate function */ split?: boolean; + /** + * https://vercel.com/docs/build-output-api/v3/configuration#images + */ + images?: ImagesConfig; } export type Config = EdgeConfig | ServerlessConfig; diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 7506a90d782d..3ce53a1176bf 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -22,7 +22,7 @@ const get_default_runtime = () => { // https://vercel.com/docs/functions/edge-functions/edge-runtime#compatible-node.js-modules const compatible_node_modules = ['async_hooks', 'events', 'buffer', 'assert', 'util']; -/** @type {import('.').default} **/ +/** @type {import('index.js').default} **/ const plugin = function (defaults = {}) { if ('edge' in defaults) { throw new Error("{ edge: true } has been removed in favour of { runtime: 'edge' }"); @@ -69,8 +69,8 @@ const plugin = function (defaults = {}) { /** * @param {string} name - * @param {import('.').ServerlessConfig} config - * @param {import('@sveltejs/kit').RouteDefinition[]} routes + * @param {import('index.js').ServerlessConfig} config + * @param {import('@sveltejs/kit').RouteDefinition[]} routes */ async function generate_serverless_function(name, config, routes) { const dir = `${dirs.functions}/${name}.func`; @@ -99,8 +99,8 @@ const plugin = function (defaults = {}) { /** * @param {string} name - * @param {import('.').EdgeConfig} config - * @param {import('@sveltejs/kit').RouteDefinition[]} routes + * @param {import('index.js').EdgeConfig} config + * @param {import('@sveltejs/kit').RouteDefinition[]} routes */ async function generate_edge_function(name, config, routes) { const tmp = builder.getBuildDirectory(`vercel-tmp/${name}`); @@ -146,7 +146,8 @@ const plugin = function (defaults = {}) { console.error(formatted.join('\n')); } - } catch (error) { + } catch (err) { + const error = /** @type {import('esbuild').BuildFailure} */ (err); for (const e of error.errors) { for (const node of e.notes) { const match = @@ -192,7 +193,7 @@ const plugin = function (defaults = {}) { ); } - /** @type {Map[] }>} */ + /** @type {Map[] }>} */ const groups = new Map(); /** @type {Map} */ @@ -201,7 +202,7 @@ const plugin = function (defaults = {}) { /** @type {Map} */ const functions = new Map(); - /** @type {Map, { expiration: number | false, bypassToken: string | undefined, allowQuery: string[], group: number, passQuery: true }>} */ + /** @type {Map, { expiration: number | false, bypassToken: string | undefined, allowQuery: string[], group: number, passQuery: true }>} */ const isr_config = new Map(); /** @type {Set} */ @@ -220,7 +221,7 @@ const plugin = function (defaults = {}) { } const node_runtime = /nodejs([0-9]+)\.x/.exec(runtime); - if (runtime !== 'edge' && (!node_runtime || node_runtime[1] < 18)) { + if (runtime !== 'edge' && (!node_runtime || parseInt(node_runtime[1]) < 18)) { throw new Error( `Invalid runtime '${runtime}' for route ${route.id}. Valid runtimes are 'edge' and 'nodejs18.x' or higher ` + '(see the Node.js Version section in your Vercel project settings for info on the currently supported versions).' @@ -395,7 +396,7 @@ const plugin = function (defaults = {}) { }; }; -/** @param {import('.').EdgeConfig & import('.').ServerlessConfig} config */ +/** @param {import('index.js').EdgeConfig & import('index.js').ServerlessConfig} config */ function hash_config(config) { return [ config.runtime ?? '', @@ -424,7 +425,7 @@ function write(file, data) { // This function is duplicated in adapter-static /** * @param {import('@sveltejs/kit').Builder} builder - * @param {import('.').Config} config + * @param {import('index.js').Config} config * @param {string} dir */ function static_vercel_config(builder, config, dir) { @@ -434,7 +435,7 @@ function static_vercel_config(builder, config, dir) { /** @type {Record} */ const overrides = {}; - /** @type {import('./index').ImagesConfig} */ + /** @type {import('./index.js').ImagesConfig | undefined} */ const images = config.images; for (const [src, redirect] of builder.prerendered.redirects) { @@ -531,7 +532,7 @@ function static_vercel_config(builder, config, dir) { * @param {import('@sveltejs/kit').Builder} builder * @param {string} entry * @param {string} dir - * @param {import('.').ServerlessConfig} config + * @param {import('index.js').ServerlessConfig} config */ async function create_function_bundle(builder, entry, dir, config) { fs.rmSync(dir, { force: true, recursive: true }); diff --git a/packages/adapter-vercel/tsconfig.json b/packages/adapter-vercel/tsconfig.json index 8aa63e3d2a76..3d157ebc29e5 100644 --- a/packages/adapter-vercel/tsconfig.json +++ b/packages/adapter-vercel/tsconfig.json @@ -13,6 +13,5 @@ "paths": { "@sveltejs/kit": ["../kit/types/index"] } - }, - "include": ["**/*.js", "index.d.ts", "internal.d.ts"] + } } From 8bd53cd0905a41c020a64936a3322aa76caa33f7 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 5 Jun 2024 07:39:41 -0400 Subject: [PATCH 2/6] relative paths --- packages/adapter-vercel/index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 3ce53a1176bf..11d7223dfa0f 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -22,7 +22,7 @@ const get_default_runtime = () => { // https://vercel.com/docs/functions/edge-functions/edge-runtime#compatible-node.js-modules const compatible_node_modules = ['async_hooks', 'events', 'buffer', 'assert', 'util']; -/** @type {import('index.js').default} **/ +/** @type {import('./index.js').default} **/ const plugin = function (defaults = {}) { if ('edge' in defaults) { throw new Error("{ edge: true } has been removed in favour of { runtime: 'edge' }"); @@ -69,8 +69,8 @@ const plugin = function (defaults = {}) { /** * @param {string} name - * @param {import('index.js').ServerlessConfig} config - * @param {import('@sveltejs/kit').RouteDefinition[]} routes + * @param {import('./index.js').ServerlessConfig} config + * @param {import('@sveltejs/kit').RouteDefinition[]} routes */ async function generate_serverless_function(name, config, routes) { const dir = `${dirs.functions}/${name}.func`; @@ -99,8 +99,8 @@ const plugin = function (defaults = {}) { /** * @param {string} name - * @param {import('index.js').EdgeConfig} config - * @param {import('@sveltejs/kit').RouteDefinition[]} routes + * @param {import('./index.js').EdgeConfig} config + * @param {import('@sveltejs/kit').RouteDefinition[]} routes */ async function generate_edge_function(name, config, routes) { const tmp = builder.getBuildDirectory(`vercel-tmp/${name}`); @@ -193,7 +193,7 @@ const plugin = function (defaults = {}) { ); } - /** @type {Map[] }>} */ + /** @type {Map[] }>} */ const groups = new Map(); /** @type {Map} */ @@ -202,7 +202,7 @@ const plugin = function (defaults = {}) { /** @type {Map} */ const functions = new Map(); - /** @type {Map, { expiration: number | false, bypassToken: string | undefined, allowQuery: string[], group: number, passQuery: true }>} */ + /** @type {Map, { expiration: number | false, bypassToken: string | undefined, allowQuery: string[], group: number, passQuery: true }>} */ const isr_config = new Map(); /** @type {Set} */ @@ -396,7 +396,7 @@ const plugin = function (defaults = {}) { }; }; -/** @param {import('index.js').EdgeConfig & import('index.js').ServerlessConfig} config */ +/** @param {import('./index.js').EdgeConfig & import('./index.js').ServerlessConfig} config */ function hash_config(config) { return [ config.runtime ?? '', @@ -425,7 +425,7 @@ function write(file, data) { // This function is duplicated in adapter-static /** * @param {import('@sveltejs/kit').Builder} builder - * @param {import('index.js').Config} config + * @param {import('./index.js').Config} config * @param {string} dir */ function static_vercel_config(builder, config, dir) { @@ -532,7 +532,7 @@ function static_vercel_config(builder, config, dir) { * @param {import('@sveltejs/kit').Builder} builder * @param {string} entry * @param {string} dir - * @param {import('index.js').ServerlessConfig} config + * @param {import('./index.js').ServerlessConfig} config */ async function create_function_bundle(builder, entry, dir, config) { fs.rmSync(dir, { force: true, recursive: true }); From 6d2fb21cf76ee6afd739664d18953223bc159283 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 5 Jun 2024 07:52:07 -0400 Subject: [PATCH 3/6] images config can only be specified at the project level --- packages/adapter-vercel/index.d.ts | 12 ++++-------- packages/adapter-vercel/index.js | 8 ++++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/adapter-vercel/index.d.ts b/packages/adapter-vercel/index.d.ts index 76a710d11bba..74aaed8cd620 100644 --- a/packages/adapter-vercel/index.d.ts +++ b/packages/adapter-vercel/index.d.ts @@ -29,11 +29,6 @@ export interface ServerlessConfig { */ split?: boolean; - /** - * https://vercel.com/docs/build-output-api/v3/configuration#images - */ - images?: ImagesConfig; - /** * [Incremental Static Regeneration](https://vercel.com/docs/concepts/incremental-static-regeneration/overview) configuration. * Serverless only. @@ -98,13 +93,14 @@ export interface EdgeConfig { * If `true`, this route will always be deployed as its own separate function */ split?: boolean; +} + +export type Config = (EdgeConfig | ServerlessConfig) & { /** * https://vercel.com/docs/build-output-api/v3/configuration#images */ images?: ImagesConfig; -} - -export type Config = EdgeConfig | ServerlessConfig; +}; // we copy the RequestContext interface from `@vercel/edge` because that package can't co-exist with `@types/node`. // see https://github.com/sveltejs/kit/pull/9280#issuecomment-1452110035 diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 11d7223dfa0f..cdf3eec2e9fe 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -210,6 +210,14 @@ const plugin = function (defaults = {}) { // group routes by config for (const route of builder.routes) { + if (route.config?.images) { + // we need this because previous versions incorrectly put `images` on `ServerlessConfig` — + // it's unlikely anyone actually used it that way, but belt and braces + throw new Error( + `\`images\` configuration can only be specified in your adapter options, not in individual routes (${route.id})` + ); + } + const runtime = route.config?.runtime ?? defaults?.runtime ?? get_default_runtime(); const config = { runtime, ...defaults, ...route.config }; From c051da98b6a00507af0aa3de1ff3e50f7726df6c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 5 Jun 2024 07:52:46 -0400 Subject: [PATCH 4/6] update changeset --- .changeset/stupid-lies-remain.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/stupid-lies-remain.md b/.changeset/stupid-lies-remain.md index c3cf452ae00a..aabe7d4d412f 100644 --- a/.changeset/stupid-lies-remain.md +++ b/.changeset/stupid-lies-remain.md @@ -1,5 +1,5 @@ --- -"@sveltejs/adapter-vercel": patch +'@sveltejs/adapter-vercel': major --- -fix: add images to edge config types +breaking: remove `images` from route-level config From 3a944c0d969d683c8f7754bda639756afe958e16 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 5 Jun 2024 12:07:25 -0400 Subject: [PATCH 5/6] live dangerously --- .changeset/stupid-lies-remain.md | 2 +- packages/adapter-vercel/index.js | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/.changeset/stupid-lies-remain.md b/.changeset/stupid-lies-remain.md index aabe7d4d412f..9c4e85ea97bf 100644 --- a/.changeset/stupid-lies-remain.md +++ b/.changeset/stupid-lies-remain.md @@ -2,4 +2,4 @@ '@sveltejs/adapter-vercel': major --- -breaking: remove `images` from route-level config +fix: remove `images` from route-level config diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index cdf3eec2e9fe..11d7223dfa0f 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -210,14 +210,6 @@ const plugin = function (defaults = {}) { // group routes by config for (const route of builder.routes) { - if (route.config?.images) { - // we need this because previous versions incorrectly put `images` on `ServerlessConfig` — - // it's unlikely anyone actually used it that way, but belt and braces - throw new Error( - `\`images\` configuration can only be specified in your adapter options, not in individual routes (${route.id})` - ); - } - const runtime = route.config?.runtime ?? defaults?.runtime ?? get_default_runtime(); const config = { runtime, ...defaults, ...route.config }; From ad7bc0075dabf7c5d1374fe7c76d05a2f868b2ee Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 5 Jun 2024 12:08:09 -0400 Subject: [PATCH 6/6] downgrade changeset --- .changeset/stupid-lies-remain.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/stupid-lies-remain.md b/.changeset/stupid-lies-remain.md index 9c4e85ea97bf..83a7bec30d45 100644 --- a/.changeset/stupid-lies-remain.md +++ b/.changeset/stupid-lies-remain.md @@ -1,5 +1,5 @@ --- -'@sveltejs/adapter-vercel': major +'@sveltejs/adapter-vercel': patch --- fix: remove `images` from route-level config