From 997b48e204676863d26c46608f01ffe42d7d92d0 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 00:29:29 -0400 Subject: [PATCH 01/12] tinkering --- packages/adapter-vercel/index.js | 28 +++++++++++-------- packages/adapter-vercel/package.json | 1 + packages/adapter-vercel/src/entry.js | 2 +- .../templates/default/svelte.config.cjs | 8 +----- pnpm-lock.yaml | 8 ++++++ 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 159c3fb98877..66972a72ccc4 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -8,32 +8,36 @@ module.exports = function () { async adapt(utils) { const vercel_output_directory = resolve('.vercel_build_output'); + utils.rimraf(vercel_output_directory); + 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'); + + utils.copy(join(__dirname, 'src/entry.js'), '.svelte/vercel/entry.js'); + + const esbuild = (await import('esbuild')).default; + + await esbuild.build({ + entryPoints: ['.svelte/vercel/entry.js'], + outfile: join(lambda_directory, 'index.js'), + bundle: true, + platform: 'node' + }); + + writeFileSync(join(lambda_directory, 'package.json'), JSON.stringify({ type: 'commonjs' })); utils.log.minor('Writing client application...'); utils.copy_static_files(static_directory); utils.copy_client_files(static_directory); - utils.log.minor('Building lambda...'); - utils.copy_server_files(server_directory); - renameSync(join(server_directory, 'app.js'), join(server_directory, 'app.mjs')); - - utils.copy(join(__dirname, 'files'), lambda_directory); - utils.log.minor('Prerendering static pages...'); await utils.prerender({ dest: static_directory }); utils.log.minor('Writing routes...'); - try { - mkdirSync(config_directory); - } catch { - // directory already exists - } + utils.mkdirp(config_directory); writeFileSync( join(config_directory, 'routes.json'), JSON.stringify([ diff --git a/packages/adapter-vercel/package.json b/packages/adapter-vercel/package.json index 7d4601a2df6d..f453be0960a3 100644 --- a/packages/adapter-vercel/package.json +++ b/packages/adapter-vercel/package.json @@ -15,6 +15,7 @@ }, "devDependencies": { "@sveltejs/kit": "workspace:*", + "esbuild": "^0.11.12", "rollup": "^2.41.1", "sirv": "^1.0.11", "typescript": "^4.2.3" diff --git a/packages/adapter-vercel/src/entry.js b/packages/adapter-vercel/src/entry.js index 72d4158dcf8a..ce67b568c5ce 100644 --- a/packages/adapter-vercel/src/entry.js +++ b/packages/adapter-vercel/src/entry.js @@ -12,7 +12,7 @@ globalThis.Headers = Headers; export default async (req, res) => { const { pathname, searchParams } = new URL(req.url || '', 'http://localhost'); - const { render } = await import('./server/app.mjs'); + const { render } = await import('../output/server/app.js'); const rendered = await render({ method: req.method, diff --git a/packages/create-svelte/templates/default/svelte.config.cjs b/packages/create-svelte/templates/default/svelte.config.cjs index 978f22817ead..375adf7c5a01 100644 --- a/packages/create-svelte/templates/default/svelte.config.cjs +++ b/packages/create-svelte/templates/default/svelte.config.cjs @@ -12,13 +12,7 @@ const config = { kit: { // hydrate the
element in src/app.html - target: '#svelte', - - vite: { - ssr: { - noExternal: Object.keys(pkg.dependencies || {}) - } - } + target: '#svelte' } }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fa407e1ce1f1..a962e02954fc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -136,11 +136,13 @@ importers: packages/adapter-vercel: specifiers: '@sveltejs/kit': workspace:* + esbuild: ^0.11.12 rollup: ^2.41.1 sirv: ^1.0.11 typescript: ^4.2.3 devDependencies: '@sveltejs/kit': link:../kit + esbuild: 0.11.12 rollup: 2.41.1 sirv: 1.0.11 typescript: 4.2.3 @@ -1513,6 +1515,12 @@ packages: is-symbol: 1.0.3 dev: true + /esbuild/0.11.12: + resolution: {integrity: sha512-c8cso/1RwVj+fbDvLtUgSG4ZJQ0y9Zdrl6Ot/GAjyy4pdMCHaFnDMts5gqFnWRPLajWtEnI+3hlET4R9fVoZng==} + hasBin: true + requiresBuild: true + dev: true + /esbuild/0.9.2: resolution: {integrity: sha512-xE3oOILjnmN8PSjkG3lT9NBbd1DbxNqolJ5qNyrLhDWsFef3yTp/KTQz1C/x7BYFKbtrr9foYtKA6KA1zuNAUQ==} hasBin: true From 9a86d48937393eb191ab57149068057a4f5a4317 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 00:30:29 -0400 Subject: [PATCH 02/12] lint --- packages/adapter-vercel/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index 66972a72ccc4..c4f5549bfab5 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -1,4 +1,4 @@ -const { writeFileSync, mkdirSync, renameSync } = require('fs'); +const { writeFileSync } = require('fs'); const { resolve, join } = require('path'); module.exports = function () { From 38d0243038eb9adf68d66ea5bd1695e0167c2085 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 09:54:24 -0400 Subject: [PATCH 03/12] simplify vercel adapter --- packages/adapter-vercel/.gitignore | 3 +- .../adapter-vercel/{src => files}/entry.js | 0 packages/adapter-vercel/files/routes.json | 9 +++ packages/adapter-vercel/index.js | 47 ++++++--------- packages/adapter-vercel/package.json | 7 +-- packages/adapter-vercel/src/index.cjs | 4 -- .../templates/default/package.json | 2 +- pnpm-lock.yaml | 58 ++++++++++++++++--- 8 files changed, 82 insertions(+), 48 deletions(-) rename packages/adapter-vercel/{src => files}/entry.js (100%) create mode 100644 packages/adapter-vercel/files/routes.json delete mode 100644 packages/adapter-vercel/src/index.cjs diff --git a/packages/adapter-vercel/.gitignore b/packages/adapter-vercel/.gitignore index 1f664acc2b82..91dfed8d4a8b 100644 --- a/packages/adapter-vercel/.gitignore +++ b/packages/adapter-vercel/.gitignore @@ -1,3 +1,2 @@ .DS_Store -node_modules -/files \ No newline at end of file +node_modules \ No newline at end of file diff --git a/packages/adapter-vercel/src/entry.js b/packages/adapter-vercel/files/entry.js similarity index 100% rename from packages/adapter-vercel/src/entry.js rename to packages/adapter-vercel/files/entry.js diff --git a/packages/adapter-vercel/files/routes.json b/packages/adapter-vercel/files/routes.json new file mode 100644 index 000000000000..ffaed1bca535 --- /dev/null +++ b/packages/adapter-vercel/files/routes.json @@ -0,0 +1,9 @@ +[ + { + "handle": "filesystem" + }, + { + "src": "/.*", + "dest": ".vercel/functions/render" + } +] diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index c4f5549bfab5..eb05a2a78d8b 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -1,5 +1,6 @@ const { writeFileSync } = require('fs'); -const { resolve, join } = require('path'); +const { join } = require('path'); +const esbuild = require('esbuild'); module.exports = function () { /** @type {import('@sveltejs/kit').Adapter} */ @@ -7,49 +8,39 @@ module.exports = function () { name: '@sveltejs/adapter-vercel', async adapt(utils) { - const vercel_output_directory = resolve('.vercel_build_output'); - utils.rimraf(vercel_output_directory); + const dir = '.vercel_build_output'; + utils.rimraf(dir); - 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 files = join(__dirname, 'files'); - utils.copy(join(__dirname, 'src/entry.js'), '.svelte/vercel/entry.js'); + const dirs = { + static: join(dir, 'static'), + lambda: join(dir, 'functions/node/render') + }; - const esbuild = (await import('esbuild')).default; + utils.log.minor('Generating serverless function...'); + utils.copy(join(files, 'entry.js'), '.svelte/vercel/entry.js'); await esbuild.build({ entryPoints: ['.svelte/vercel/entry.js'], - outfile: join(lambda_directory, 'index.js'), + outfile: join(dirs.lambda, 'index.js'), bundle: true, platform: 'node' }); - writeFileSync(join(lambda_directory, 'package.json'), JSON.stringify({ type: 'commonjs' })); - - utils.log.minor('Writing client application...'); - utils.copy_static_files(static_directory); - utils.copy_client_files(static_directory); + writeFileSync(join(dirs.lambda, 'package.json'), JSON.stringify({ type: 'commonjs' })); utils.log.minor('Prerendering static pages...'); await utils.prerender({ - dest: static_directory + dest: dirs.static }); + utils.log.minor('Copying assets...'); + utils.copy_static_files(dirs.static); + utils.copy_client_files(dirs.static); + utils.log.minor('Writing routes...'); - utils.mkdirp(config_directory); - writeFileSync( - join(config_directory, 'routes.json'), - JSON.stringify([ - { - handle: 'filesystem' - }, - { - src: '/.*', - dest: '.vercel/functions/render' - } - ]) - ); + utils.copy(join(files, 'routes.json'), join(dir, 'config/routes.json')); } }; diff --git a/packages/adapter-vercel/package.json b/packages/adapter-vercel/package.json index f453be0960a3..54dcc856552b 100644 --- a/packages/adapter-vercel/package.json +++ b/packages/adapter-vercel/package.json @@ -6,18 +6,13 @@ "files" ], "scripts": { - "dev": "rollup -cw", - "build": "rollup -c", "lint": "eslint --ignore-path .gitignore \"**/*.{ts,js,svelte}\" && npm run check-format", "format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore", - "check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore", - "prepublishOnly": "npm run build" + "check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore" }, "devDependencies": { "@sveltejs/kit": "workspace:*", "esbuild": "^0.11.12", - "rollup": "^2.41.1", - "sirv": "^1.0.11", "typescript": "^4.2.3" } } diff --git a/packages/adapter-vercel/src/index.cjs b/packages/adapter-vercel/src/index.cjs deleted file mode 100644 index cce6e18a8da7..000000000000 --- a/packages/adapter-vercel/src/index.cjs +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = async (res, req) => { - const { default: app } = await import('./entry.mjs'); - await app(res, req); -}; diff --git a/packages/create-svelte/templates/default/package.json b/packages/create-svelte/templates/default/package.json index 2b879dacb0fa..06c44ba5c642 100644 --- a/packages/create-svelte/templates/default/package.json +++ b/packages/create-svelte/templates/default/package.json @@ -3,7 +3,7 @@ "version": "0.0.1", "scripts": { "dev": "svelte-kit dev", - "build": "svelte-kit build", + "build": "svelte-kit build --verbose", "start": "svelte-kit start" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a962e02954fc..ec5377d4d4c6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -191,7 +191,7 @@ importers: devDependencies: '@lukeed/uuid': 2.0.0 '@sveltejs/adapter-vercel': link:../../../adapter-vercel - '@sveltejs/kit': link:../../../kit + '@sveltejs/kit': 1.0.0-next.79_svelte@3.35.0+vite@2.1.0 cookie: 0.4.1 dotenv: 8.2.0 svelte: 3.35.0 @@ -642,6 +642,16 @@ packages: rollup: 2.42.3 dev: true + /@rollup/pluginutils/4.1.0: + resolution: {integrity: sha512-TrBhfJkFxA+ER+ew2U2/fHbebhLT/l/2pRk0hfj9KusXUuRXd2v0R58AfaZK9VXDQ4TogOSEmICVrQAA3zFnHQ==} + engines: {node: '>= 8.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0 + dependencies: + estree-walker: 2.0.2 + picomatch: 2.2.3 + dev: true + /@rollup/pluginutils/4.1.0_rollup@2.41.1: resolution: {integrity: sha512-TrBhfJkFxA+ER+ew2U2/fHbebhLT/l/2pRk0hfj9KusXUuRXd2v0R58AfaZK9VXDQ4TogOSEmICVrQAA3zFnHQ==} engines: {node: '>= 8.0.0'} @@ -653,6 +663,24 @@ packages: rollup: 2.41.1 dev: false + /@sveltejs/kit/1.0.0-next.79_svelte@3.35.0+vite@2.1.0: + resolution: {integrity: sha512-0Ct/jaEHLweZ6lAbM/6hYIQQ4gUp0SSP0d/8v1Bwx2fYuBaHaraLvn7Q7oGsrjwiPL5V1jh5z0H2Wck8J4MWJg==} + engines: {node: '>= 12.17.0'} + hasBin: true + peerDependencies: + svelte: ^3.32.1 + vite: ^2.1.0 + dependencies: + '@sveltejs/vite-plugin-svelte': 1.0.0-next.7_svelte@3.35.0+vite@2.1.0 + cheap-watch: 1.0.3 + sade: 1.7.4 + svelte: 3.35.0 + vite: 2.1.0 + transitivePeerDependencies: + - rollup + - supports-color + dev: true + /@sveltejs/vite-plugin-svelte/1.0.0-next.7_93fef3a16c1b45ac67b14a795df10192: resolution: {integrity: sha512-ENvKYY36jrvFP7h1G87k5uOoEh5UM1m8n40J2duqV/R3wHnxfW81SCR1aXo+5CVU8Prm3/jtS4TWs8CUTqO1fw==} engines: {node: '>=12.0.0'} @@ -675,6 +703,28 @@ packages: - supports-color dev: false + /@sveltejs/vite-plugin-svelte/1.0.0-next.7_svelte@3.35.0+vite@2.1.0: + resolution: {integrity: sha512-ENvKYY36jrvFP7h1G87k5uOoEh5UM1m8n40J2duqV/R3wHnxfW81SCR1aXo+5CVU8Prm3/jtS4TWs8CUTqO1fw==} + engines: {node: '>=12.0.0'} + peerDependencies: + svelte: ^3.37.0 + vite: ^2.1.5 + dependencies: + '@rollup/pluginutils': 4.1.0 + chalk: 4.1.0 + debug: 4.3.2 + hash-sum: 2.0.0 + require-relative: 0.8.7 + slash: 3.0.0 + source-map: 0.7.3 + svelte: 3.35.0 + svelte-hmr: 0.14.0_svelte@3.35.0 + vite: 2.1.0 + transitivePeerDependencies: + - rollup + - supports-color + dev: true + /@types/amphtml-validator/1.0.1: resolution: {integrity: sha512-DWE7fy6KtC+Uw0KV/HAmjuH2GB/o8yskXlvmVWR7mOVsLDybp+XrwkzEeRFU9wGjWKeRMBNGsx+5DRq7sUsAwA==} dependencies: @@ -1188,7 +1238,6 @@ packages: /cheap-watch/1.0.3: resolution: {integrity: sha512-xC5CruMhLzjPwJ5ecUxGu1uGmwJQykUhqd2QrCrYbwvsFYdRyviu6jG9+pccwDXJR/OpmOTOJ9yLFunVgQu9wg==} engines: {node: '>=8'} - dev: false /ci-info/2.0.0: resolution: {integrity: sha512-5tK7EtrZ0N+OLFMthtqOj4fI2Jeb88C4CAZPu25LDVUgXJ0A3Js4PMGqrn0JU1W0Mh1/Z8wZzYPxqUrXeBboCQ==} @@ -1378,7 +1427,6 @@ packages: optional: true dependencies: ms: 2.1.2 - dev: false /decamelize-keys/1.1.0: resolution: {integrity: sha1-0XGoeTMlKAfrPLYdwcFEXQeN8tk=} @@ -2070,7 +2118,6 @@ packages: /hash-sum/2.0.0: resolution: {integrity: sha512-WdZTbAByD+pHfl/g9QSsBIIwy8IT+EsPiKDs0KNX+zSHhdDLFKdZu0BQHljvO+0QI/BasbMSUa8wYNCZTvhslg==} - dev: false /hosted-git-info/2.8.8: resolution: {integrity: sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==} @@ -2825,7 +2872,6 @@ packages: /picomatch/2.2.3: resolution: {integrity: sha512-KpELjfwcCDUb9PeigTs2mBJzXUPzAuP2oPcA989He8Rte0+YUAjw1JVedDhuTKPkHjSYzMN3npC9luThGYEKdg==} engines: {node: '>=8.6'} - dev: false /pify/2.3.0: resolution: {integrity: sha1-7RQaasBDqEnqWISY59yosVMw6Qw=} @@ -3085,7 +3131,6 @@ packages: /require-relative/0.8.7: resolution: {integrity: sha1-eZlTn8ngR6N5KPoZb44VY9q9Nt4=} - dev: false /resolve-from/4.0.0: resolution: {integrity: sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==} @@ -3417,7 +3462,6 @@ packages: svelte: '>=3.19.0' dependencies: svelte: 3.35.0 - dev: false /svelte-preprocess/4.7.0: resolution: {integrity: sha512-iNrY4YGqi0LD2e6oT9YbdSzOKntxk8gmzfqso1z/lUJOZh4o6fyIqkirmiZ8/dDJFqtIE1spVgDFWgkfhLEYlw==} From 3bef611750dd2247786359ba8b8fe4d57d083307 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 09:57:50 -0400 Subject: [PATCH 04/12] note to self --- packages/adapter-vercel/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index eb05a2a78d8b..e5e5380c2fc8 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -18,6 +18,11 @@ module.exports = function () { lambda: join(dir, 'functions/node/render') }; + // TODO ideally we'd have something like utils.tmpdir('vercel') + // rather than hardcoding '.svelte/vercel/entry.js', and the + // relative import from that file to output/server/app.js + // would be controlled. at the moment we're exposing + // implementation details that could change utils.log.minor('Generating serverless function...'); utils.copy(join(files, 'entry.js'), '.svelte/vercel/entry.js'); From 8cb3a29ab7330f101fbf5d447673edca4cbf367e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 10:00:25 -0400 Subject: [PATCH 05/12] expose @sveltejs/kit/install-fetch for adapters --- packages/kit/package.json | 3 +++ packages/kit/rollup.config.js | 3 ++- packages/kit/src/core/adapt/prerender.js | 2 +- packages/kit/src/core/dev/index.js | 2 +- packages/kit/src/core/start/index.js | 2 +- .../kit/src/{core/node-fetch-global.js => install-fetch.js} | 0 6 files changed, 8 insertions(+), 4 deletions(-) rename packages/kit/src/{core/node-fetch-global.js => install-fetch.js} (100%) diff --git a/packages/kit/package.json b/packages/kit/package.json index 6d11d996b223..aebc83e9a12c 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -67,6 +67,9 @@ "./http": { "import": "./dist/http.js" }, + "./install-fetch": { + "import": "./dist/install-fetch.js" + }, "./types": "./types/index.d.ts" }, "types": "types/index.d.ts", diff --git a/packages/kit/rollup.config.js b/packages/kit/rollup.config.js index 69ab9d4c145c..2762194130ad 100644 --- a/packages/kit/rollup.config.js +++ b/packages/kit/rollup.config.js @@ -42,7 +42,8 @@ export default [ input: { cli: 'src/cli.js', ssr: 'src/runtime/server/index.js', - http: 'src/core/http/index.js' + http: 'src/core/http/index.js', + 'install-fetch': 'src/install-fetch.js' }, output: { dir: 'dist', diff --git a/packages/kit/src/core/adapt/prerender.js b/packages/kit/src/core/adapt/prerender.js index 8a66dd4c4aff..3c51823e8175 100644 --- a/packages/kit/src/core/adapt/prerender.js +++ b/packages/kit/src/core/adapt/prerender.js @@ -2,7 +2,7 @@ import { readFileSync, writeFileSync } from 'fs'; import { dirname, join, resolve as resolve_path } from 'path'; import { parse, pathToFileURL, resolve } from 'url'; import { mkdirp } from '../filesystem/index.js'; -import '../node-fetch-global.js'; +import '../../install-fetch.js'; /** @param {string} html */ function clean_html(html) { diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js index bf91715cfe46..b9433474c967 100644 --- a/packages/kit/src/core/dev/index.js +++ b/packages/kit/src/core/dev/index.js @@ -14,7 +14,7 @@ import { get_body } from '../http/index.js'; import { copy_assets } from '../utils.js'; import svelte from '@sveltejs/vite-plugin-svelte'; import { get_server } from '../server/index.js'; -import '../node-fetch-global.js'; +import '../../install-fetch.js'; /** @typedef {{ cwd?: string, port: number, host: string, https: boolean, config: import('types/config').ValidatedConfig }} Options */ /** @typedef {import('types/internal').SSRComponent} SSRComponent */ diff --git a/packages/kit/src/core/start/index.js b/packages/kit/src/core/start/index.js index 4cc530f4cf8e..5fedb0f76cf1 100644 --- a/packages/kit/src/core/start/index.js +++ b/packages/kit/src/core/start/index.js @@ -4,7 +4,7 @@ import sirv from 'sirv'; import { get_body } from '../http/index.js'; import { join, resolve } from 'path'; import { get_server } from '../server/index.js'; -import '../node-fetch-global.js'; +import '../../install-fetch.js'; /** @param {string} dir */ const mutable = (dir) => diff --git a/packages/kit/src/core/node-fetch-global.js b/packages/kit/src/install-fetch.js similarity index 100% rename from packages/kit/src/core/node-fetch-global.js rename to packages/kit/src/install-fetch.js From 0a60cb3f163343c65c24549a5b0ef6691a6f5659 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 10:03:50 -0400 Subject: [PATCH 06/12] use install-fetch in vercel adapter --- packages/adapter-vercel/files/entry.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/adapter-vercel/files/entry.js b/packages/adapter-vercel/files/entry.js index ce67b568c5ce..75799d16b330 100644 --- a/packages/adapter-vercel/files/entry.js +++ b/packages/adapter-vercel/files/entry.js @@ -1,19 +1,12 @@ -import { URL } from 'url'; -// eslint-disable-next-line import/no-unresolved import { get_body } from '@sveltejs/kit/http'; -import fetch, { Response, Request, Headers } from 'node-fetch'; +import '@sveltejs/kit/install-fetch'; -// provide server-side fetch -globalThis.fetch = fetch; -globalThis.Response = Response; -globalThis.Request = Request; -globalThis.Headers = Headers; +// TODO hardcoding the relative location makes this brittle +import { render } from '../output/server/app.js'; export default async (req, res) => { const { pathname, searchParams } = new URL(req.url || '', 'http://localhost'); - const { render } = await import('../output/server/app.js'); - const rendered = await render({ method: req.method, headers: req.headers, From 63b795c258bd83273f77512b05080462fd395784 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 10:18:31 -0400 Subject: [PATCH 07/12] update netlify adapter --- packages/adapter-netlify/files/_redirects | 1 + .../files/{render.js => entry.js} | 13 +-- packages/adapter-netlify/files/index.cjs | 6 -- packages/adapter-netlify/index.js | 89 ++++++++++--------- .../templates/default/package.json | 1 + pnpm-lock.yaml | 6 +- 6 files changed, 55 insertions(+), 61 deletions(-) create mode 100644 packages/adapter-netlify/files/_redirects rename packages/adapter-netlify/files/{render.js => entry.js} (66%) delete mode 100644 packages/adapter-netlify/files/index.cjs diff --git a/packages/adapter-netlify/files/_redirects b/packages/adapter-netlify/files/_redirects new file mode 100644 index 000000000000..1083d2d09468 --- /dev/null +++ b/packages/adapter-netlify/files/_redirects @@ -0,0 +1 @@ +/* /.netlify/functions/render 200 \ No newline at end of file diff --git a/packages/adapter-netlify/files/render.js b/packages/adapter-netlify/files/entry.js similarity index 66% rename from packages/adapter-netlify/files/render.js rename to packages/adapter-netlify/files/entry.js index ccc2a98ea51f..9b88c72d3b82 100644 --- a/packages/adapter-netlify/files/render.js +++ b/packages/adapter-netlify/files/entry.js @@ -1,14 +1,7 @@ -'use strict'; +import '@sveltejs/kit/install-fetch'; -import { URLSearchParams } from 'url'; -import { render } from './app.mjs'; // eslint-disable-line import/no-unresolved -import fetch, { Response, Request, Headers } from 'node-fetch'; - -// provide server-side fetch -globalThis.fetch = fetch; -globalThis.Response = Response; -globalThis.Request = Request; -globalThis.Headers = Headers; +// TODO hardcoding the relative location makes this brittle +import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved export async function handler(event) { const { diff --git a/packages/adapter-netlify/files/index.cjs b/packages/adapter-netlify/files/index.cjs deleted file mode 100644 index df52d8e4876d..000000000000 --- a/packages/adapter-netlify/files/index.cjs +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - async handler(event) { - const { handler } = await import('./handler.mjs'); - return await handler(event); - } -}; diff --git a/packages/adapter-netlify/index.js b/packages/adapter-netlify/index.js index 787ac4cc95bb..de642d3786c1 100644 --- a/packages/adapter-netlify/index.js +++ b/packages/adapter-netlify/index.js @@ -1,5 +1,6 @@ const { existsSync, readFileSync, copyFileSync, writeFileSync, renameSync } = require('fs'); -const { resolve } = require('path'); +const { join } = require('path'); +const esbuild = require('esbuild'); const toml = require('toml'); module.exports = function () { @@ -8,58 +9,64 @@ module.exports = function () { name: '@sveltejs/adapter-netlify', async adapt(utils) { - 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); + const { publish, functions } = validate_config().build; - utils.copy_static_files(publish); - utils.copy_client_files(publish); - utils.copy_server_files(`${functions}/render`); + utils.rimraf(publish); + utils.rimraf(functions); - // rename app to .mjs - renameSync(`${functions}/render/app.js`, `${functions}/render/app.mjs`); + const files = join(__dirname, 'files'); - // copy the renderer - copyFileSync(resolve(__dirname, 'files/render.js'), `${functions}/render/handler.mjs`); + utils.log.minor('Generating serverless function...'); + utils.copy(join(files, 'entry.js'), '.svelte/netlify/entry.js'); - // copy the entry point - copyFileSync(resolve(__dirname, 'files/index.cjs'), `${functions}/render/index.js`); + await esbuild.build({ + entryPoints: ['.svelte/netlify/entry.js'], + outfile: join(functions, 'index.js'), + bundle: true, + platform: 'node' + }); - // create _redirects - writeFileSync(`${publish}/_redirects`, '/* /.netlify/functions/render 200'); + writeFileSync(join(functions, 'package.json'), JSON.stringify({ type: 'commonjs' })); - // prerender utils.log.info('Prerendering static pages...'); await utils.prerender({ dest: publish }); + + utils.log.minor('Copying assets...'); + utils.copy_static_files(publish); + utils.copy_client_files(publish); + + utils.log.minor('Writing redirects...'); + utils.copy(`${files}/_redirects`, `${publish}/_redirects`); } }; return adapter; }; + +function validate_config() { + if (existsSync('netlify.toml')) { + let netlify_config; + + try { + netlify_config = toml.parse(readFileSync('netlify.toml', 'utf-8')); + } catch (err) { + err.message = `Error parsing netlify.toml: ${err.message}`; + throw err; + } + + 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' + ); + } + + return netlify_config; + } + + // 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' + ); +} diff --git a/packages/create-svelte/templates/default/package.json b/packages/create-svelte/templates/default/package.json index 06c44ba5c642..818464739ea4 100644 --- a/packages/create-svelte/templates/default/package.json +++ b/packages/create-svelte/templates/default/package.json @@ -8,6 +8,7 @@ }, "devDependencies": { "@lukeed/uuid": "^2.0.0", + "@sveltejs/adapter-netlify": "next", "@sveltejs/adapter-vercel": "next", "@sveltejs/kit": "next", "cookie": "^0.4.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ec5377d4d4c6..d52540ffa5ac 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -137,14 +137,10 @@ importers: specifiers: '@sveltejs/kit': workspace:* esbuild: ^0.11.12 - rollup: ^2.41.1 - sirv: ^1.0.11 typescript: ^4.2.3 devDependencies: '@sveltejs/kit': link:../kit esbuild: 0.11.12 - rollup: 2.41.1 - sirv: 1.0.11 typescript: 4.2.3 packages/create-svelte: @@ -178,6 +174,7 @@ importers: specifiers: '@fontsource/fira-mono': ^4.2.2 '@lukeed/uuid': ^2.0.0 + '@sveltejs/adapter-netlify': next '@sveltejs/adapter-vercel': next '@sveltejs/kit': next cookie: ^0.4.1 @@ -190,6 +187,7 @@ importers: '@fontsource/fira-mono': 4.2.2 devDependencies: '@lukeed/uuid': 2.0.0 + '@sveltejs/adapter-netlify': link:../../../adapter-netlify '@sveltejs/adapter-vercel': link:../../../adapter-vercel '@sveltejs/kit': 1.0.0-next.79_svelte@3.35.0+vite@2.1.0 cookie: 0.4.1 From afc8cc59d648a165f322234e2135824c1ae33ee9 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 10:21:23 -0400 Subject: [PATCH 08/12] ignore netlify folders --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 01cb644ac036..3f8f49b69001 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ yarn.lock /packages/create-svelte/template/CHANGELOG.md .env .vercel_build_output +.netlify .svelte From a260792ec3e6c44717d6dca772da299d07a7314f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 10:23:14 -0400 Subject: [PATCH 09/12] lint --- packages/adapter-netlify/files/entry.js | 2 +- packages/adapter-netlify/index.js | 2 +- pnpm-lock.yaml | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/adapter-netlify/files/entry.js b/packages/adapter-netlify/files/entry.js index 9b88c72d3b82..09eaad2368c7 100644 --- a/packages/adapter-netlify/files/entry.js +++ b/packages/adapter-netlify/files/entry.js @@ -1,4 +1,4 @@ -import '@sveltejs/kit/install-fetch'; +import '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved // TODO hardcoding the relative location makes this brittle import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved diff --git a/packages/adapter-netlify/index.js b/packages/adapter-netlify/index.js index de642d3786c1..9d13ba173f94 100644 --- a/packages/adapter-netlify/index.js +++ b/packages/adapter-netlify/index.js @@ -1,4 +1,4 @@ -const { existsSync, readFileSync, copyFileSync, writeFileSync, renameSync } = require('fs'); +const { existsSync, readFileSync, writeFileSync } = require('fs'); const { join } = require('path'); const esbuild = require('esbuild'); const toml = require('toml'); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3dd1d3e4568a..3188f6545b11 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -498,6 +498,7 @@ packages: /@lukeed/csprng/1.0.0: resolution: {integrity: sha512-ruuGHsnabmObBdeMg3vKdGRmh06Oog3eFpf/Tk6X0kDSJDpJTDCj2dqdp1+0VjzIUgHlFF9GBm7uFqfYhhdX9g==} engines: {node: '>=8'} + dev: false /@lukeed/uuid/2.0.0: resolution: {integrity: sha512-dUz8OmYvlY5A9wXaroHIMSPASpSYRLCqbPvxGSyHguhtTQIy24lC+EGxQlwv71AhRCO55WOtgwhzQLpw27JaJQ==} From ca03377e7d9e1b0ded2e7e084cba5ce741af9d24 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 10:24:07 -0400 Subject: [PATCH 10/12] lint --- packages/adapter-vercel/files/entry.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/adapter-vercel/files/entry.js b/packages/adapter-vercel/files/entry.js index 75799d16b330..9d7a108dff0e 100644 --- a/packages/adapter-vercel/files/entry.js +++ b/packages/adapter-vercel/files/entry.js @@ -1,8 +1,8 @@ -import { get_body } from '@sveltejs/kit/http'; -import '@sveltejs/kit/install-fetch'; +import { get_body } from '@sveltejs/kit/http'; // eslint-disable-line import/no-unresolved +import '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved // TODO hardcoding the relative location makes this brittle -import { render } from '../output/server/app.js'; +import { render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved export default async (req, res) => { const { pathname, searchParams } = new URL(req.url || '', 'http://localhost'); From 176c1e8a3342f818e1ec44791073aa74f2a9ffee Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 10:30:16 -0400 Subject: [PATCH 11/12] use install-fetch in adapter-node --- packages/adapter-node/src/server.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/packages/adapter-node/src/server.js b/packages/adapter-node/src/server.js index e144d29d7eaf..ba43bf0c6c5c 100644 --- a/packages/adapter-node/src/server.js +++ b/packages/adapter-node/src/server.js @@ -1,18 +1,11 @@ -import compression from 'compression'; import fs from 'fs'; -import polka from 'polka'; import { dirname, join } from 'path'; +import { fileURLToPath } from 'url'; +import compression from 'compression'; +import polka from 'polka'; import sirv from 'sirv'; -import { URL, fileURLToPath } from 'url'; -// eslint-disable-next-line import/no-unresolved -import { get_body } from '@sveltejs/kit/http'; -import fetch, { Response, Request, Headers } from 'node-fetch'; - -// provide server-side fetch -globalThis.fetch = fetch; -globalThis.Response = Response; -globalThis.Request = Request; -globalThis.Headers = Headers; +import { get_body } from '@sveltejs/kit/http'; // eslint-disable-line import/no-unresolved +import '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved // App is a dynamic file built from the application layer. From 72d6b671dd5437770845d5f86610b2f9d0f4aa73 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Apr 2021 10:33:17 -0400 Subject: [PATCH 12/12] changesets --- .changeset/light-keys-mix.md | 5 +++++ .changeset/smart-mugs-chew.md | 5 +++++ .changeset/smooth-shoes-mix.md | 6 ++++++ 3 files changed, 16 insertions(+) create mode 100644 .changeset/light-keys-mix.md create mode 100644 .changeset/smart-mugs-chew.md create mode 100644 .changeset/smooth-shoes-mix.md diff --git a/.changeset/light-keys-mix.md b/.changeset/light-keys-mix.md new file mode 100644 index 000000000000..46032d9e3b17 --- /dev/null +++ b/.changeset/light-keys-mix.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-node': patch +--- + +Use install-fetch helper diff --git a/.changeset/smart-mugs-chew.md b/.changeset/smart-mugs-chew.md new file mode 100644 index 000000000000..5587f0cd6c06 --- /dev/null +++ b/.changeset/smart-mugs-chew.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Expose install-fetch subpackage for adapters to use diff --git a/.changeset/smooth-shoes-mix.md b/.changeset/smooth-shoes-mix.md new file mode 100644 index 000000000000..ce5f8c87c4ce --- /dev/null +++ b/.changeset/smooth-shoes-mix.md @@ -0,0 +1,6 @@ +--- +'@sveltejs/adapter-netlify': patch +'@sveltejs/adapter-vercel': patch +--- + +Bundle serverless functions with esbuild