From 93e366bfa368ca9f7a442f64d09749427b833b44 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 12 Mar 2021 11:06:08 -0500 Subject: [PATCH 1/3] future-proof adapters --- packages/adapter-begin/index.js | 52 +++++++------- packages/adapter-netlify/index.js | 102 ++++++++++++++------------- packages/adapter-node/index.js | 32 +++++---- packages/adapter-static/index.js | 20 +++--- packages/adapter-vercel/index.js | 72 ++++++++++--------- packages/kit/package.json | 1 + packages/kit/src/core/adapt/index.js | 12 ++-- 7 files changed, 156 insertions(+), 135 deletions(-) diff --git a/packages/adapter-begin/index.js b/packages/adapter-begin/index.js index 2bafa9684003..adb9b80b5ba3 100644 --- a/packages/adapter-begin/index.js +++ b/packages/adapter-begin/index.js @@ -1,5 +1,3 @@ -'use strict'; - import { readFileSync, existsSync } from 'fs'; import { copy } from '@sveltejs/app-utils/files'; import { resolve, join } from 'path'; @@ -24,27 +22,31 @@ function parse_arc(arcPath) { } } -export default async function adapter(builder) { - builder.log.minor('Parsing app.arc file'); - const { static: static_mount_point } = parse_arc('app.arc'); - - const lambda_directory = resolve(join('src', 'http', 'get-index')); - const static_directory = resolve(static_mount_point); - const server_directory = resolve(join('src', 'shared')); - - builder.log.minor('Writing client application...'); - builder.copy_static_files(static_directory); - builder.copy_client_files(static_directory); - - builder.log.minor('Building lambda...'); - const local_lambda_dir = join(__dirname, 'files'); - copy(local_lambda_dir, lambda_directory); - - builder.log.minor('Writing server application...'); - builder.copy_server_files(server_directory); - - builder.log.minor('Prerendering static pages...'); - await builder.prerender({ - dest: static_directory - }); +export default function adapter() { + return { + async adapt(builder) { + builder.log.minor('Parsing app.arc file'); + const { static: static_mount_point } = parse_arc('app.arc'); + + const lambda_directory = resolve(join('src', 'http', 'get-index')); + const static_directory = resolve(static_mount_point); + const server_directory = resolve(join('src', 'shared')); + + builder.log.minor('Writing client application...'); + builder.copy_static_files(static_directory); + builder.copy_client_files(static_directory); + + builder.log.minor('Building lambda...'); + const local_lambda_dir = join(__dirname, 'files'); + copy(local_lambda_dir, lambda_directory); + + builder.log.minor('Writing server application...'); + builder.copy_server_files(server_directory); + + builder.log.minor('Prerendering static pages...'); + await builder.prerender({ + dest: static_directory + }); + } + }; } diff --git a/packages/adapter-netlify/index.js b/packages/adapter-netlify/index.js index 36a381091d76..d93a80e1b384 100644 --- a/packages/adapter-netlify/index.js +++ b/packages/adapter-netlify/index.js @@ -1,5 +1,3 @@ -'use strict'; - import { existsSync, readFileSync, copyFileSync, writeFileSync, renameSync } from 'fs'; import { dirname, resolve } from 'path'; import toml from 'toml'; @@ -7,51 +5,59 @@ import { fileURLToPath } from 'url'; const __dirname = dirname(fileURLToPath(import.meta.url)); -export default async function adapter(builder) { - let netlify_config; - - if (existsSync('netlify.toml')) { - try { - netlify_config = toml.parse(readFileSync('netlify.toml', 'utf-8')); - } catch (err) { - err.message = `Error parsing netlify.toml: ${err.message}`; - throw err; +export default function () { + return { + async adapt(builder) { + let netlify_config; + + if (existsSync('netlify.toml')) { + try { + netlify_config = toml.parse(readFileSync('netlify.toml', 'utf-8')); + } catch (err) { + err.message = `Error parsing netlify.toml: ${err.message}`; + throw err; + } + } else { + // TODO offer to create one? + throw new Error( + 'Missing a netlify.toml file. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration' + ); + } + + if ( + !netlify_config.build || + !netlify_config.build.publish || + !netlify_config.build.functions + ) { + throw new Error( + 'You must specify build.publish and build.functions in netlify.toml. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration' + ); + } + + const publish = resolve(netlify_config.build.publish); + const functions = resolve(netlify_config.build.functions); + + builder.copy_static_files(publish); + builder.copy_client_files(publish); + builder.copy_server_files(`${functions}/render`); + + // rename app to .mjs + renameSync(`${functions}/render/app.js`, `${functions}/render/app.mjs`); + + // copy the renderer + copyFileSync(resolve(__dirname, 'files/render.js'), `${functions}/render/handler.mjs`); + + // copy the entry point + copyFileSync(resolve(__dirname, 'files/index.cjs'), `${functions}/render/index.js`); + + // create _redirects + writeFileSync(`${publish}/_redirects`, '/* /.netlify/functions/render 200'); + + // prerender + builder.log.info('Prerendering static pages...'); + await builder.prerender({ + dest: publish + }); } - } else { - // TODO offer to create one? - throw new Error( - 'Missing a netlify.toml file. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration' - ); - } - - if (!netlify_config.build || !netlify_config.build.publish || !netlify_config.build.functions) { - throw new Error( - 'You must specify build.publish and build.functions in netlify.toml. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration' - ); - } - - const publish = resolve(netlify_config.build.publish); - const functions = resolve(netlify_config.build.functions); - - builder.copy_static_files(publish); - builder.copy_client_files(publish); - builder.copy_server_files(`${functions}/render`); - - // rename app to .mjs - renameSync(`${functions}/render/app.js`, `${functions}/render/app.mjs`); - - // copy the renderer - copyFileSync(resolve(__dirname, 'files/render.js'), `${functions}/render/handler.mjs`); - - // copy the entry point - copyFileSync(resolve(__dirname, 'files/index.cjs'), `${functions}/render/index.js`); - - // create _redirects - writeFileSync(`${publish}/_redirects`, '/* /.netlify/functions/render 200'); - - // prerender - builder.log.info('Prerendering static pages...'); - await builder.prerender({ - dest: publish - }); + }; } diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index e562d4dca90c..fdb918c2806f 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -2,22 +2,26 @@ import { copyFileSync } from 'fs'; import { dirname, join } from 'path'; import { fileURLToPath } from 'url'; -export default async function adapter(builder) { - const dir = dirname(fileURLToPath(import.meta.url)); - const out = 'build'; // TODO implement adapter options +export default function () { + return { + async adapt(builder) { + const dir = dirname(fileURLToPath(import.meta.url)); + const out = 'build'; // TODO implement adapter options - builder.log.minor('Writing client application...'); - const static_directory = join(out, 'assets'); - builder.copy_client_files(static_directory); - builder.copy_static_files(static_directory); + builder.log.minor('Writing client application...'); + const static_directory = join(out, 'assets'); + builder.copy_client_files(static_directory); + builder.copy_static_files(static_directory); - builder.log.minor('Building server'); - builder.copy_server_files(out); + builder.log.minor('Building server'); + builder.copy_server_files(out); - copyFileSync(`${dir}/files/server.js`, `${out}/index.js`); + copyFileSync(`${dir}/files/server.js`, `${out}/index.js`); - builder.log.minor('Prerendering static pages...'); - await builder.prerender({ - dest: `${out}/prerendered` - }); + builder.log.minor('Prerendering static pages...'); + await builder.prerender({ + dest: `${out}/prerendered` + }); + } + }; } diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index b1a7a507920a..2526b7f821c1 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -1,11 +1,13 @@ -'use strict'; +export default function ({ pages = 'build', assets = 'build' } = {}) { + return { + async adapt(builder) { + builder.copy_static_files(assets); + builder.copy_client_files(assets); -export default async function adapter(builder, { pages = 'build', assets = 'build' } = {}) { - builder.copy_static_files(assets); - builder.copy_client_files(assets); - - await builder.prerender({ - force: true, - dest: pages - }); + await builder.prerender({ + force: true, + dest: pages + }); + } + }; } diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 12edd9073908..ad5454565718 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -5,44 +5,48 @@ import { copy } from '@sveltejs/app-utils/files'; const __dirname = dirname(fileURLToPath(import.meta.url)); -export default async function adapter(builder) { - const vercel_output_directory = resolve('.vercel_build_output'); - const config_directory = join(vercel_output_directory, 'config'); - const static_directory = join(vercel_output_directory, 'static'); - const lambda_directory = join(vercel_output_directory, 'functions', 'node', 'render'); - const server_directory = join(lambda_directory, 'server'); +export default function () { + return { + async adapt(builder) { + const vercel_output_directory = resolve('.vercel_build_output'); + const config_directory = join(vercel_output_directory, 'config'); + const static_directory = join(vercel_output_directory, 'static'); + const lambda_directory = join(vercel_output_directory, 'functions', 'node', 'render'); + const server_directory = join(lambda_directory, 'server'); - builder.log.minor('Writing client application...'); - builder.copy_static_files(static_directory); - builder.copy_client_files(static_directory); + builder.log.minor('Writing client application...'); + builder.copy_static_files(static_directory); + builder.copy_client_files(static_directory); - builder.log.minor('Building lambda...'); - builder.copy_server_files(server_directory); - renameSync(join(server_directory, 'app.js'), join(server_directory, 'app.mjs')); + builder.log.minor('Building lambda...'); + builder.copy_server_files(server_directory); + renameSync(join(server_directory, 'app.js'), join(server_directory, 'app.mjs')); - copy(join(__dirname, 'files'), lambda_directory); + copy(join(__dirname, 'files'), lambda_directory); - builder.log.minor('Prerendering static pages...'); - await builder.prerender({ - dest: static_directory - }); + builder.log.minor('Prerendering static pages...'); + await builder.prerender({ + dest: static_directory + }); - builder.log.minor('Writing routes...'); - try { - mkdirSync(config_directory); - } catch { - // directory already exists - } - writeFileSync( - join(config_directory, 'routes.json'), - JSON.stringify([ - { - handle: 'filesystem' - }, - { - src: '/.*', - dest: '.vercel/functions/render' + builder.log.minor('Writing routes...'); + try { + mkdirSync(config_directory); + } catch { + // directory already exists } - ]) - ); + writeFileSync( + join(config_directory, 'routes.json'), + JSON.stringify([ + { + handle: 'filesystem' + }, + { + src: '/.*', + dest: '.vercel/functions/render' + } + ]) + ); + } + }; } diff --git a/packages/kit/package.json b/packages/kit/package.json index 87558da0fdbe..8c8c187c643c 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -56,6 +56,7 @@ "test:integration": "uvu test test.js" }, "exports": { + "./package.json": "./package.json", "./ssr": { "import": "./dist/ssr.js" } diff --git a/packages/kit/src/core/adapt/index.js b/packages/kit/src/core/adapt/index.js index f04dff7d17b3..3382b6cc4de5 100644 --- a/packages/kit/src/core/adapt/index.js +++ b/packages/kit/src/core/adapt/index.js @@ -13,18 +13,20 @@ export async function adapt(config, { cwd = process.cwd(), verbose }) { throw new Error('No adapter specified'); } - const [adapter, options] = config.kit.adapter; + const [name, options] = config.kit.adapter; const log = logger({ verbose }); - console.log(colors.bold().cyan(`\n> Using ${adapter}`)); + console.log(colors.bold().cyan(`\n> Using ${name}`)); const builder = new Builder({ cwd, config, log }); const require = createRequire(import.meta.url); - const resolved = require.resolve(adapter, { paths: [pathToFileURL(process.cwd()).href] }); - const fn = (await import(pathToFileURL(resolved).href)).default; - await fn(builder, options); + const resolved = require.resolve(name, { paths: [pathToFileURL(process.cwd()).href] }); + const mod = await import(pathToFileURL(resolved).href); + const adapter = mod.default(options); + + await adapter.adapt(builder); log.success('done'); } From fdefb660fb8059acada0bd65986300bf46e03b40 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 12 Mar 2021 11:06:46 -0500 Subject: [PATCH 2/3] changeset --- .changeset/sharp-mangos-tickle.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .changeset/sharp-mangos-tickle.md diff --git a/.changeset/sharp-mangos-tickle.md b/.changeset/sharp-mangos-tickle.md new file mode 100644 index 000000000000..77a127490c24 --- /dev/null +++ b/.changeset/sharp-mangos-tickle.md @@ -0,0 +1,10 @@ +--- +'@sveltejs/adapter-begin': patch +'@sveltejs/adapter-netlify': patch +'@sveltejs/adapter-node': patch +'@sveltejs/adapter-static': patch +'@sveltejs/adapter-vercel': patch +'@sveltejs/kit': patch +--- + +Change adapter signature From 199534348313fa30bd0eef123c5e5bdb858de4f3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 12 Mar 2021 12:58:39 -0500 Subject: [PATCH 3/3] make function anonymous --- packages/adapter-begin/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/adapter-begin/index.js b/packages/adapter-begin/index.js index adb9b80b5ba3..85ff5c15c2e5 100644 --- a/packages/adapter-begin/index.js +++ b/packages/adapter-begin/index.js @@ -22,7 +22,7 @@ function parse_arc(arcPath) { } } -export default function adapter() { +export default function () { return { async adapt(builder) { builder.log.minor('Parsing app.arc file');