From cbfd2cf4867c5cbf27349426f46366c7e21c6b2c Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 25 Aug 2021 09:16:38 +0700 Subject: [PATCH 01/23] create proxy for adapters to import from --- packages/kit/rollup.config.js | 1 + packages/kit/src/core/adapt/proxy.js | 10 ++++++++++ packages/kit/src/core/constants.js | 3 +++ 3 files changed, 14 insertions(+) create mode 100644 packages/kit/src/core/adapt/proxy.js diff --git a/packages/kit/rollup.config.js b/packages/kit/rollup.config.js index cc44571ab66b..b7dd5431fade 100644 --- a/packages/kit/rollup.config.js +++ b/packages/kit/rollup.config.js @@ -46,6 +46,7 @@ export default [ { input: { cli: 'src/cli.js', + adapter: 'src/core/adapt/proxy.js', ssr: 'src/runtime/server/index.js', node: 'src/core/node/index.js', hooks: 'src/runtime/hooks.js', diff --git a/packages/kit/src/core/adapt/proxy.js b/packages/kit/src/core/adapt/proxy.js new file mode 100644 index 000000000000..f7d9c6c50a89 --- /dev/null +++ b/packages/kit/src/core/adapt/proxy.js @@ -0,0 +1,10 @@ +import { resolve } from 'path'; +import { SERVER_OUTPUT } from '../constants'; + +const cwd = resolve(process.cwd(), `${SERVER_OUTPUT}/app.js`); + +export const init = async () => (await import(cwd)).init; + +export const render = async () => (await import(cwd)).render; + +export default { init, render }; diff --git a/packages/kit/src/core/constants.js b/packages/kit/src/core/constants.js index 565b216bfa66..f8bbbe027387 100644 --- a/packages/kit/src/core/constants.js +++ b/packages/kit/src/core/constants.js @@ -4,3 +4,6 @@ export const SVELTE_KIT = '.svelte-kit'; // asset path so that we can serve local assets while still // verifying that requests are correctly prefixed export const SVELTE_KIT_ASSETS = '/_svelte_kit_assets'; + +export const CLIENT_OUTPUT = `${SVELTE_KIT}/output/client`; +export const SERVER_OUTPUT = `${SVELTE_KIT}/output/server`; From f94fbe8b221a399f64165410c00f65e366a5069c Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 25 Aug 2021 09:17:16 +0700 Subject: [PATCH 02/23] adapter ambient module --- packages/kit/types/ambient-modules.d.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/kit/types/ambient-modules.d.ts b/packages/kit/types/ambient-modules.d.ts index 6e4f9b5de04d..0261a0623080 100644 --- a/packages/kit/types/ambient-modules.d.ts +++ b/packages/kit/types/ambient-modules.d.ts @@ -132,6 +132,15 @@ declare module '$service-worker' { export const timestamp: number; } +declare module '@sveltejs/kit/adapter' { + import { App } from '@sveltejs/kit'; + + export const init: App['init']; + export const render: App['render']; + const app: App; + export default app; +} + declare module '@sveltejs/kit/hooks' { import { Handle } from '@sveltejs/kit'; From 11b99fc37a21bd4e9c9a38f5d86aecaa15183360 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 25 Aug 2021 09:17:50 +0700 Subject: [PATCH 03/23] adapter node sync --- packages/adapter-node/index.js | 2 +- packages/adapter-node/src/env.d.ts | 3 +++ packages/adapter-node/src/index.js | 7 ++----- packages/adapter-node/src/server.js | 3 +-- 4 files changed, 7 insertions(+), 8 deletions(-) create mode 100644 packages/adapter-node/src/env.d.ts diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index ae7cad98ab59..b86e9574a223 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -45,7 +45,7 @@ export default function ({ const files = fileURLToPath(new URL('./files', import.meta.url)); utils.copy(files, '.svelte-kit/node'); writeFileSync( - '.svelte-kit/node/env.js', + '.svelte-kit/node/env.js', // types pointed in src/env.d.ts `export const path = process.env[${JSON.stringify( path_env )}] || false;\nexport const host = process.env[${JSON.stringify( diff --git a/packages/adapter-node/src/env.d.ts b/packages/adapter-node/src/env.d.ts new file mode 100644 index 000000000000..8987d5f2becc --- /dev/null +++ b/packages/adapter-node/src/env.d.ts @@ -0,0 +1,3 @@ +export const host: string; +export const path: string; +export const port: number; diff --git a/packages/adapter-node/src/index.js b/packages/adapter-node/src/index.js index 4db8b8be6723..f823fb19677b 100644 --- a/packages/adapter-node/src/index.js +++ b/packages/adapter-node/src/index.js @@ -1,8 +1,5 @@ -// TODO hardcoding the relative location makes this brittle -// @ts-ignore -import { init, render } from '../output/server/app.js'; -// @ts-ignore -import { path, host, port } from './env.js'; +import { init, render } from '@sveltejs/kit/adapter'; +import { host, path, port } from './env.js'; import { createServer } from './server'; init(); diff --git a/packages/adapter-node/src/server.js b/packages/adapter-node/src/server.js index 9fb12ceb0f04..28a309f1ee32 100644 --- a/packages/adapter-node/src/server.js +++ b/packages/adapter-node/src/server.js @@ -16,8 +16,7 @@ const paths = { prerendered: join(__dirname, '/prerendered') }; -// TODO: type render function from @sveltejs/kit/adapter -// @ts-ignore +/** @param {{ render: import('@sveltejs/kit').App['render'] }} app */ export function createServer({ render }) { const prerendered_handler = fs.existsSync(paths.prerendered) ? sirv(paths.prerendered, { From 8fde2191fd51f52b4ced21153dcb5ffe3e5a9159 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 25 Aug 2021 09:19:03 +0700 Subject: [PATCH 04/23] update constants --- packages/kit/src/core/adapt/prerender.js | 8 +++----- packages/kit/src/core/adapt/test/index.js | 12 +++--------- packages/kit/src/core/adapt/utils.js | 6 +++--- packages/kit/src/core/build/index.js | 12 +++++------- packages/kit/src/core/preview/index.js | 6 +++--- 5 files changed, 17 insertions(+), 27 deletions(-) diff --git a/packages/kit/src/core/adapt/prerender.js b/packages/kit/src/core/adapt/prerender.js index 86903461abfb..70f14226abfb 100644 --- a/packages/kit/src/core/adapt/prerender.js +++ b/packages/kit/src/core/adapt/prerender.js @@ -3,7 +3,7 @@ import { dirname, join, resolve as resolve_path } from 'path'; import { pathToFileURL, resolve, URL } from 'url'; import { mkdirp } from '../../utils/filesystem.js'; import { __fetch_polyfill } from '../../install-fetch.js'; -import { SVELTE_KIT } from '../constants.js'; +import { SERVER_OUTPUT } from '../constants.js'; /** * @typedef {import('types/config').PrerenderErrorHandler} PrerenderErrorHandler @@ -99,14 +99,12 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a __fetch_polyfill(); - const dir = resolve_path(cwd, `${SVELTE_KIT}/output`); - const seen = new Set(); - const server_root = resolve_path(dir); + const server_root = resolve_path(cwd, SERVER_OUTPUT); /** @type {import('types/app').App} */ - const app = await import(pathToFileURL(`${server_root}/server/app.js`).href); + const app = await import(pathToFileURL(`${server_root}/app.js`).href); app.init({ paths: config.kit.paths, diff --git a/packages/kit/src/core/adapt/test/index.js b/packages/kit/src/core/adapt/test/index.js index d1b3cd94093a..b8b915580b9f 100644 --- a/packages/kit/src/core/adapt/test/index.js +++ b/packages/kit/src/core/adapt/test/index.js @@ -5,7 +5,7 @@ import rimraf from 'rimraf'; import glob from 'tiny-glob/sync.js'; import { get_utils } from '../utils.js'; import { fileURLToPath } from 'url'; -import { SVELTE_KIT } from '../../constants.js'; +import { CLIENT_OUTPUT, SERVER_OUTPUT } from '../../constants.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = join(__filename, '..'); @@ -63,18 +63,12 @@ suite('copy files', () => { rimraf.sync(dest); utils.copy_client_files(dest); - assert.equal( - glob('**', { cwd: `${cwd}/${SVELTE_KIT}/output/client` }), - glob('**', { cwd: dest }) - ); + assert.equal(glob('**', { cwd: `${cwd}/${CLIENT_OUTPUT}` }), glob('**', { cwd: dest })); rimraf.sync(dest); utils.copy_server_files(dest); - assert.equal( - glob('**', { cwd: `${cwd}/${SVELTE_KIT}/output/server` }), - glob('**', { cwd: dest }) - ); + assert.equal(glob('**', { cwd: `${cwd}/${SERVER_OUTPUT}` }), glob('**', { cwd: dest })); }); suite('prerender', async () => { diff --git a/packages/kit/src/core/adapt/utils.js b/packages/kit/src/core/adapt/utils.js index 12623819b611..69cf30b737b3 100644 --- a/packages/kit/src/core/adapt/utils.js +++ b/packages/kit/src/core/adapt/utils.js @@ -1,4 +1,4 @@ -import { SVELTE_KIT } from '../constants.js'; +import { CLIENT_OUTPUT, SERVER_OUTPUT } from '../constants.js'; import { copy, rimraf, mkdirp } from '../../utils/filesystem.js'; import { prerender } from './prerender.js'; @@ -19,11 +19,11 @@ export function get_utils({ cwd, config, build_data, log }) { copy, copy_client_files(dest) { - copy(`${cwd}/${SVELTE_KIT}/output/client`, dest, (file) => file[0] !== '.'); + copy(`${cwd}/${CLIENT_OUTPUT}`, dest, (file) => file[0] !== '.'); }, copy_server_files(dest) { - copy(`${cwd}/${SVELTE_KIT}/output/server`, dest, (file) => file[0] !== '.'); + copy(`${cwd}/${SERVER_OUTPUT}`, dest, (file) => file[0] !== '.'); }, copy_static_files(dest) { diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index 75f91ac5808b..5ca7aeb8264f 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -8,7 +8,7 @@ import { create_app } from '../../core/create_app/index.js'; import vite from 'vite'; import { svelte } from '@sveltejs/vite-plugin-svelte'; import glob from 'tiny-glob/sync.js'; -import { SVELTE_KIT } from '../constants.js'; +import { CLIENT_OUTPUT, SERVER_OUTPUT, SVELTE_KIT } from '../constants.js'; /** @param {any} value */ const s = (value) => JSON.stringify(value); @@ -66,8 +66,8 @@ export async function build(config, { cwd = process.cwd(), runtime = '@sveltejs/ await build_service_worker(options, client_manifest); } - const client = glob('**', { cwd: `${output_dir}/client`, filesOnly: true }).map(posixify); - const server = glob('**', { cwd: `${output_dir}/server`, filesOnly: true }).map(posixify); + const client = glob('**', { cwd: CLIENT_OUTPUT, filesOnly: true }).map(posixify); + const server = glob('**', { cwd: SERVER_OUTPUT, filesOnly: true }).map(posixify); return { client, @@ -88,7 +88,6 @@ export async function build(config, { cwd = process.cwd(), runtime = '@sveltejs/ * build_dir: string; * output_dir: string; * client_entry_file: string; - * service_worker_entry_file: string | null; * }} options */ async function build_client({ @@ -98,8 +97,7 @@ async function build_client({ manifest, build_dir, output_dir, - client_entry_file, - service_worker_entry_file + client_entry_file }) { create_app({ manifest_data: manifest, @@ -111,7 +109,7 @@ async function build_client({ process.env.VITE_SVELTEKIT_AMP = config.kit.amp ? 'true' : ''; - const client_out_dir = `${output_dir}/client/${config.kit.appDir}`; + const client_out_dir = `${CLIENT_OUTPUT}/${config.kit.appDir}`; const client_manifest_file = `${client_out_dir}/manifest.json`; /** @type {Record} */ diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index 2ec1778a4865..e006819077be 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -5,7 +5,7 @@ import { getRawBody } from '../node/index.js'; import { join, resolve } from 'path'; import { get_server } from '../server/index.js'; import { __fetch_polyfill } from '../../install-fetch.js'; -import { SVELTE_KIT, SVELTE_KIT_ASSETS } from '../constants.js'; +import { CLIENT_OUTPUT, SERVER_OUTPUT, SVELTE_KIT_ASSETS } from '../constants.js'; /** @param {string} dir */ const mutable = (dir) => @@ -32,7 +32,7 @@ export async function preview({ }) { __fetch_polyfill(); - const app_file = resolve(cwd, `${SVELTE_KIT}/output/server/app.js`); + const app_file = resolve(cwd, `${SERVER_OUTPUT}/app.js`); /** @type {import('types/app').App} */ const app = await import(pathToFileURL(app_file).href); @@ -45,7 +45,7 @@ export async function preview({ return next(); }; - const assets_handler = sirv(resolve(cwd, `${SVELTE_KIT}/output/client`), { + const assets_handler = sirv(resolve(cwd, CLIENT_OUTPUT), { maxAge: 31536000, immutable: true }); From f9bff84174719831e82a68824b492e36ba010ca8 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 25 Aug 2021 10:14:02 +0700 Subject: [PATCH 05/23] add to exports --- packages/kit/package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/kit/package.json b/packages/kit/package.json index f3ebd9e95082..aca0437cfb70 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -66,6 +66,9 @@ }, "exports": { "./package.json": "./package.json", + "./adapter": { + "import": "./dist/adapter.js" + }, "./ssr": { "import": "./dist/ssr.js" }, From 09d007cd70f724843810326b477aa5ef35545903 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 25 Aug 2021 10:20:25 +0700 Subject: [PATCH 06/23] promisify module export types --- packages/kit/types/ambient-modules.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/types/ambient-modules.d.ts b/packages/kit/types/ambient-modules.d.ts index 0261a0623080..3e67fb709038 100644 --- a/packages/kit/types/ambient-modules.d.ts +++ b/packages/kit/types/ambient-modules.d.ts @@ -135,8 +135,8 @@ declare module '$service-worker' { declare module '@sveltejs/kit/adapter' { import { App } from '@sveltejs/kit'; - export const init: App['init']; - export const render: App['render']; + export const init: () => Promise; + export const render: () => Promise; const app: App; export default app; } From a76784b4f0331e348b54e86643c76addb40f3d46 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 25 Aug 2021 10:20:31 +0700 Subject: [PATCH 07/23] properly type default export --- packages/kit/types/ambient-modules.d.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/kit/types/ambient-modules.d.ts b/packages/kit/types/ambient-modules.d.ts index 3e67fb709038..9b8a8c3d5930 100644 --- a/packages/kit/types/ambient-modules.d.ts +++ b/packages/kit/types/ambient-modules.d.ts @@ -135,9 +135,12 @@ declare module '$service-worker' { declare module '@sveltejs/kit/adapter' { import { App } from '@sveltejs/kit'; - export const init: () => Promise; - export const render: () => Promise; - const app: App; + export function init(): Promise; + export function render(): Promise; + const app: { + init: typeof init; + render: typeof render; + }; export default app; } From 17a9bbe08f1231007045a45d658e7c6860a5deae Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 25 Aug 2021 10:22:10 +0700 Subject: [PATCH 08/23] shorter namings --- packages/adapter-node/src/index.js | 6 +++--- packages/adapter-node/src/server.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/adapter-node/src/index.js b/packages/adapter-node/src/index.js index f823fb19677b..0a72c6da0f4c 100644 --- a/packages/adapter-node/src/index.js +++ b/packages/adapter-node/src/index.js @@ -1,10 +1,10 @@ -import { init, render } from '@sveltejs/kit/adapter'; +import app from '@sveltejs/kit/adapter'; import { host, path, port } from './env.js'; import { createServer } from './server'; -init(); +app.init(); -const instance = createServer({ render }); +const instance = createServer(app); const listenOpts = { path, host, port }; instance.listen(listenOpts, () => { diff --git a/packages/adapter-node/src/server.js b/packages/adapter-node/src/server.js index 28a309f1ee32..224e07ce8284 100644 --- a/packages/adapter-node/src/server.js +++ b/packages/adapter-node/src/server.js @@ -16,7 +16,7 @@ const paths = { prerendered: join(__dirname, '/prerendered') }; -/** @param {{ render: import('@sveltejs/kit').App['render'] }} app */ +/** @param {import('@sveltejs/kit').App} app */ export function createServer({ render }) { const prerendered_handler = fs.existsSync(paths.prerendered) ? sirv(paths.prerendered, { From 409b239ae180f185260b0cd514a2a708c1bed61b Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 25 Aug 2021 10:30:38 +0700 Subject: [PATCH 09/23] revert some paths --- packages/kit/src/core/build/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index 5ca7aeb8264f..e70b3bd11608 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -8,7 +8,7 @@ import { create_app } from '../../core/create_app/index.js'; import vite from 'vite'; import { svelte } from '@sveltejs/vite-plugin-svelte'; import glob from 'tiny-glob/sync.js'; -import { CLIENT_OUTPUT, SERVER_OUTPUT, SVELTE_KIT } from '../constants.js'; +import { SVELTE_KIT } from '../constants.js'; /** @param {any} value */ const s = (value) => JSON.stringify(value); @@ -66,8 +66,8 @@ export async function build(config, { cwd = process.cwd(), runtime = '@sveltejs/ await build_service_worker(options, client_manifest); } - const client = glob('**', { cwd: CLIENT_OUTPUT, filesOnly: true }).map(posixify); - const server = glob('**', { cwd: SERVER_OUTPUT, filesOnly: true }).map(posixify); + const client = glob('**', { cwd: `${output_dir}/client`, filesOnly: true }).map(posixify); + const server = glob('**', { cwd: `${output_dir}/server`, filesOnly: true }).map(posixify); return { client, @@ -109,7 +109,7 @@ async function build_client({ process.env.VITE_SVELTEKIT_AMP = config.kit.amp ? 'true' : ''; - const client_out_dir = `${CLIENT_OUTPUT}/${config.kit.appDir}`; + const client_out_dir = `${output_dir}/client/${config.kit.appDir}`; const client_manifest_file = `${client_out_dir}/manifest.json`; /** @type {Record} */ From 6f50f5b6d0042956ad45d75b19144959838a1034 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 12:56:51 +0700 Subject: [PATCH 10/23] tidy up imports --- packages/adapter-node/index.js | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index b86e9574a223..71855418b890 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -1,17 +1,11 @@ -import esbuild from 'esbuild'; -import { - createReadStream, - createWriteStream, - existsSync, - readFileSync, - statSync, - writeFileSync -} from 'fs'; +import fs from 'fs'; import { join } from 'path'; import { pipeline } from 'stream'; -import glob from 'tiny-glob'; import { fileURLToPath } from 'url'; import { promisify } from 'util'; + +import esbuild from 'esbuild'; +import glob from 'tiny-glob'; import zlib from 'zlib'; const pipe = promisify(pipeline); @@ -25,7 +19,7 @@ export default function ({ out = 'build', precompress, env: { path: path_env = 'SOCKET_PATH', host: host_env = 'HOST', port: port_env = 'PORT' } = {}, - esbuild: esbuildConfig + esbuild: esbuild_config } = {}) { return { name: '@sveltejs/adapter-node', @@ -44,7 +38,7 @@ export default function ({ utils.log.minor('Building server'); const files = fileURLToPath(new URL('./files', import.meta.url)); utils.copy(files, '.svelte-kit/node'); - writeFileSync( + fs.writeFileSync( '.svelte-kit/node/env.js', // types pointed in src/env.d.ts `export const path = process.env[${JSON.stringify( path_env @@ -55,11 +49,13 @@ export default function ({ )}] || (!path && 3000);` ); /** @type {BuildOptions} */ - const defaultOptions = { + const default_options = { entryPoints: ['.svelte-kit/node/index.js'], outfile: join(out, 'index.js'), bundle: true, - external: Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}), + external: Object.keys( + JSON.parse(fs.readFileSync('package.json', 'utf8')).dependencies || {} + ), format: 'esm', platform: 'node', target: 'node12', @@ -68,14 +64,14 @@ export default function ({ APP_DIR: `"/${config.kit.appDir}/"` } }; - const buildOptions = esbuildConfig ? await esbuildConfig(defaultOptions) : defaultOptions; + const buildOptions = esbuild_config ? await esbuild_config(default_options) : default_options; await esbuild.build(buildOptions); utils.log.minor('Prerendering static pages'); await utils.prerender({ dest: `${out}/prerendered` }); - if (precompress && existsSync(`${out}/prerendered`)) { + if (precompress && fs.existsSync(`${out}/prerendered`)) { utils.log.minor('Compressing prerendered pages'); await compress(`${out}/prerendered`); } @@ -110,13 +106,13 @@ async function compress_file(file, format = 'gz') { params: { [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT, [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY, - [zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size + [zlib.constants.BROTLI_PARAM_SIZE_HINT]: fs.statSync(file).size } }) : zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION }); - const source = createReadStream(file); - const destination = createWriteStream(`${file}.${format}`); + const source = fs.createReadStream(file); + const destination = fs.createWriteStream(`${file}.${format}`); await pipe(source, compress, destination); } From b07e67750c0d23420b352304a09cae832f7df638 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 13:03:53 +0700 Subject: [PATCH 11/23] @sveltejs/esbuild-plugin-app-resolver --- packages/kit/package.json | 1 + packages/kit/rollup.config.js | 2 +- packages/kit/src/core/adapt/plugins.js | 13 ++++++ packages/kit/src/core/adapt/proxy.js | 10 ----- packages/kit/types/ambient-modules.d.ts | 9 +++++ pnpm-lock.yaml | 54 +++++++++++++++++++------ 6 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 packages/kit/src/core/adapt/plugins.js delete mode 100644 packages/kit/src/core/adapt/proxy.js diff --git a/packages/kit/package.json b/packages/kit/package.json index aca0437cfb70..4531ab0d9a0e 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -21,6 +21,7 @@ "amphtml-validator": "^1.0.34", "cookie": "^0.4.1", "devalue": "^2.0.1", + "esbuild": "^0.12.5", "eslint": "^7.25.0", "kleur": "^4.1.4", "locate-character": "^2.0.5", diff --git a/packages/kit/rollup.config.js b/packages/kit/rollup.config.js index b7dd5431fade..5b033430ac47 100644 --- a/packages/kit/rollup.config.js +++ b/packages/kit/rollup.config.js @@ -46,7 +46,7 @@ export default [ { input: { cli: 'src/cli.js', - adapter: 'src/core/adapt/proxy.js', + adapter: 'src/core/adapt/plugins.js', ssr: 'src/runtime/server/index.js', node: 'src/core/node/index.js', hooks: 'src/runtime/hooks.js', diff --git a/packages/kit/src/core/adapt/plugins.js b/packages/kit/src/core/adapt/plugins.js new file mode 100644 index 000000000000..ff0bb1bbbe94 --- /dev/null +++ b/packages/kit/src/core/adapt/plugins.js @@ -0,0 +1,13 @@ +import { join } from 'path'; + +/** @type {import('@sveltejs/kit/adapter').AppResolver} */ +export function appResolver() { + return { + name: '@sveltejs/esbuild-plugin-app-resolver', + setup(build) { + build.onResolve({ filter: /@sveltejs\/kit\/app/ }, (args) => ({ + path: join(args.resolveDir, '../output/server/app.js') + })); + } + }; +} diff --git a/packages/kit/src/core/adapt/proxy.js b/packages/kit/src/core/adapt/proxy.js deleted file mode 100644 index f7d9c6c50a89..000000000000 --- a/packages/kit/src/core/adapt/proxy.js +++ /dev/null @@ -1,10 +0,0 @@ -import { resolve } from 'path'; -import { SERVER_OUTPUT } from '../constants'; - -const cwd = resolve(process.cwd(), `${SERVER_OUTPUT}/app.js`); - -export const init = async () => (await import(cwd)).init; - -export const render = async () => (await import(cwd)).render; - -export default { init, render }; diff --git a/packages/kit/types/ambient-modules.d.ts b/packages/kit/types/ambient-modules.d.ts index 9b8a8c3d5930..ef1191f6ee0b 100644 --- a/packages/kit/types/ambient-modules.d.ts +++ b/packages/kit/types/ambient-modules.d.ts @@ -133,6 +133,15 @@ declare module '$service-worker' { } declare module '@sveltejs/kit/adapter' { + import { Plugin } from 'esbuild'; + + export interface AppResolver { + (): Plugin; + } + export const appResolver: AppResolver; +} + +declare module '@sveltejs/kit/app' { import { App } from '@sveltejs/kit'; export function init(): Promise; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dbc2bd803fa0..01733919b2c3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -30,7 +30,7 @@ importers: '@sveltejs/eslint-config': github.com/sveltejs/eslint-config/885b062904591606c030b4e8eb9160c63f16b322_9d3ce2148653bb6cbbfcd9b88d4d1961 '@typescript-eslint/eslint-plugin': 4.28.4_b1648df9f9ba40bdeef3710a5a5af353 '@typescript-eslint/parser': 4.28.4_eslint@7.31.0+typescript@4.3.5 - action-deploy-docs: github.com/sveltejs/action-deploy-docs/09c92b2b876365be1567b05a30a5778755b42c0d + action-deploy-docs: github.com/sveltejs/action-deploy-docs/321fe3aec0f4484eaa42e32283025106161cc296 dotenv: 10.0.0 eslint: 7.31.0 eslint-plugin-import: 2.23.4_eslint@7.31.0 @@ -214,6 +214,7 @@ importers: cheap-watch: ^1.0.3 cookie: ^0.4.1 devalue: ^2.0.1 + esbuild: ^0.12.5 eslint: ^7.25.0 kleur: ^4.1.4 locate-character: ^2.0.5 @@ -251,6 +252,7 @@ importers: amphtml-validator: 1.0.34 cookie: 0.4.1 devalue: 2.0.1 + esbuild: 0.12.20 eslint: 7.25.0 kleur: 4.1.4 locate-character: 2.0.5 @@ -556,7 +558,6 @@ packages: /@iarna/toml/2.2.5: resolution: {integrity: sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==} - dev: false /@istanbuljs/schema/0.1.3: resolution: {integrity: sha512-ZXRY4jNvVgSVQ8DL3LTcakaAtXwTVUxE81hslsyD2AtoXW/wVob10HkOJ1X/pAlcI7D+2YoZKg5do8G/w6RYgA==} @@ -778,7 +779,6 @@ packages: dependencies: estree-walker: 2.0.2 picomatch: 2.2.3 - dev: false /@sveltejs/vite-plugin-svelte/1.0.0-next.16_svelte@3.40.0+vite@2.5.0: resolution: {integrity: sha512-Zzw5vWJ2PjeAFG04/HQrTHvTd4B+v/pz5hgod7fPMOMXu289ZuHs2DPj68xBmOGlmTq0JyrMfBl+NDGCgaSxzA==} @@ -803,6 +803,29 @@ packages: - supports-color dev: false + /@sveltejs/vite-plugin-svelte/1.0.0-next.21_svelte@3.40.0+vite@2.5.3: + resolution: {integrity: sha512-JjPnoQoI1+VXHjguE1bWyFYdZvPN6VTZ5RsFRpju45zICas1N5jAuOqKTbhQ1G+eEMCbhhB6zjZgQQRtaXPhNQ==} + engines: {node: ^12.20 || ^14.13.1 || >= 16} + peerDependencies: + diff-match-patch: ^1.0.5 + svelte: ^3.34.0 + vite: ^2.5.3 + peerDependenciesMeta: + diff-match-patch: + optional: true + dependencies: + '@rollup/pluginutils': 4.1.1 + debug: 4.3.2 + kleur: 4.1.4 + magic-string: 0.25.7 + require-relative: 0.8.7 + svelte: 3.40.0 + svelte-hmr: 0.14.7_svelte@3.40.0 + vite: 2.5.3 + transitivePeerDependencies: + - supports-color + dev: true + /@types/amphtml-validator/1.0.1: resolution: {integrity: sha512-DWE7fy6KtC+Uw0KV/HAmjuH2GB/o8yskXlvmVWR7mOVsLDybp+XrwkzEeRFU9wGjWKeRMBNGsx+5DRq7sUsAwA==} dependencies: @@ -1399,7 +1422,6 @@ packages: /cheap-watch/1.0.3: resolution: {integrity: sha512-xC5CruMhLzjPwJ5ecUxGu1uGmwJQykUhqd2QrCrYbwvsFYdRyviu6jG9+pccwDXJR/OpmOTOJ9yLFunVgQu9wg==} engines: {node: '>=8'} - dev: false /chokidar/3.5.1: resolution: {integrity: sha512-9+s+Od+W0VJJzawDma/gvBNQqkTiqYTWLuZoyAsivsI4AaWTCzHG06/TMjsf1cYe9Cb97UCEhjz7HvnPk2p/tw==} @@ -1469,7 +1491,6 @@ packages: /colorette/1.2.2: resolution: {integrity: sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==} - dev: false /colors/1.4.0: resolution: {integrity: sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==} @@ -1757,7 +1778,6 @@ packages: resolution: {integrity: sha512-u7+0qTo9Z64MD9PhooEngCmzyEYJ6ovFhPp8PLNh3UasR5Ihjv6HWVXqm8uHmasdQlpsAf0IsY4U0YVUfCpt4Q==} hasBin: true requiresBuild: true - dev: false /esbuild/0.12.5: resolution: {integrity: sha512-vcuP53pA5XiwUU4FnlXM+2PnVjTfHGthM7uP1gtp+9yfheGvFFbq/KyuESThmtoHPUrfZH5JpxGVJIFDVD1Egw==} @@ -2855,7 +2875,6 @@ packages: resolution: {integrity: sha512-FiB0kzdP0FFVGDKlRLEQ1BgDzU87dy5NnzjeW9YZNt+/c3+q82EQDUwniSAUxp/F0gFNI1ZhKU1FqYsMuqZVnw==} engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} hasBin: true - dev: false /natural-compare/1.4.0: resolution: {integrity: sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=} @@ -3221,7 +3240,6 @@ packages: colorette: 1.2.2 nanoid: 3.1.23 source-map-js: 0.6.2 - dev: false /preferred-pm/3.0.3: resolution: {integrity: sha512-+wZgbxNES/KlJs9q40F/1sfOd/j7f1O9JaHcW5Dsn3aUUOZg3L2bjpVUcKV2jvtElYfoTuQiNeMfQJ4kwUAhCQ==} @@ -3407,7 +3425,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==} @@ -3576,7 +3593,6 @@ packages: /source-map-js/0.6.2: resolution: {integrity: sha512-/3GptzWzu0+0MBQFrDKzw/DvvMTUORvgY6k6jd/VS6iCR4RDTKWH6v6WPwQoUO8667uQEf9Oe38DxAYWY5F/Ug==} engines: {node: '>=0.10.0'} - dev: false /source-map/0.7.3: resolution: {integrity: sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==} @@ -3761,7 +3777,6 @@ packages: svelte: '>=3.19.0' dependencies: svelte: 3.40.0 - dev: false /svelte-preprocess/4.7.3_svelte@3.40.0+typescript@4.3.5: resolution: {integrity: sha512-Zx1/xLeGOIBlZMGPRCaXtlMe4ZA0faato5Dc3CosEqwu75MIEPuOstdkH6cy+RYTUYynoxzNaDxkPX4DbrPwRA==} @@ -4086,6 +4101,19 @@ packages: fsevents: 2.3.2 dev: false + /vite/2.5.3: + resolution: {integrity: sha512-1wMDnjflvtTTkMov8O/Xb5+w1/VW/Gw8oCf8f6dqgHn8lMOEqq0SaPtFEQeikFcOKCfSbiU0nEi0LDIx6DNsaQ==} + engines: {node: '>=12.2.0'} + hasBin: true + dependencies: + esbuild: 0.12.20 + postcss: 8.3.6 + resolve: 1.20.0 + rollup: 2.55.0 + optionalDependencies: + fsevents: 2.3.2 + dev: true + /wcwidth/1.0.1: resolution: {integrity: sha1-8LDc+RW8X/FSivrbLA4XtTLaL+g=} dependencies: @@ -4248,8 +4276,8 @@ packages: engines: {node: '>=10'} dev: true - github.com/sveltejs/action-deploy-docs/09c92b2b876365be1567b05a30a5778755b42c0d: - resolution: {tarball: https://codeload.github.com/sveltejs/action-deploy-docs/tar.gz/09c92b2b876365be1567b05a30a5778755b42c0d} + github.com/sveltejs/action-deploy-docs/321fe3aec0f4484eaa42e32283025106161cc296: + resolution: {tarball: https://codeload.github.com/sveltejs/action-deploy-docs/tar.gz/321fe3aec0f4484eaa42e32283025106161cc296} name: action-deploy-docs version: 1.0.0 hasBin: true From f0695399e7998cab319f6469b007f535cf83e859 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 13:04:41 +0700 Subject: [PATCH 12/23] allow importing app output from @sveltejs/kit/app --- packages/adapter-node/rollup.config.js | 2 +- packages/adapter-node/src/index.js | 2 +- packages/kit/src/core/build/index.js | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/adapter-node/rollup.config.js b/packages/adapter-node/rollup.config.js index 51c98aa0f083..7d6a1c21dbab 100644 --- a/packages/adapter-node/rollup.config.js +++ b/packages/adapter-node/rollup.config.js @@ -11,7 +11,7 @@ export default [ sourcemap: true }, plugins: [nodeResolve(), commonjs(), json()], - external: ['../output/server/app.js', './env.js', ...require('module').builtinModules] + external: ['@sveltejs/kit/app', './env.js', ...require('module').builtinModules] }, { input: 'src/shims.js', diff --git a/packages/adapter-node/src/index.js b/packages/adapter-node/src/index.js index 0a72c6da0f4c..45a85f1bd1c9 100644 --- a/packages/adapter-node/src/index.js +++ b/packages/adapter-node/src/index.js @@ -1,4 +1,4 @@ -import app from '@sveltejs/kit/adapter'; +import app from '@sveltejs/kit/app'; import { host, path, port } from './env.js'; import { createServer } from './server'; diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index e70b3bd11608..f11a178a6e4b 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -420,6 +420,9 @@ async function build_server( const host = ${config.kit.host ? s(config.kit.host) : `request.headers[${s(config.kit.hostHeader || 'host')}]`}; return respond({ ...request, host }, options, { prerender }); } + + const app = { init, render }; + export default app; ` .replace(/^\t{3}/gm, '') .trim() From 4abc0a2b93ff3264066ae2e3d9d6eeca83f49e17 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 13:14:04 +0700 Subject: [PATCH 13/23] resolve merges --- packages/adapter-node/src/kit-middleware.js | 6 +++--- packages/adapter-node/src/middlewares.js | 13 ++----------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/packages/adapter-node/src/kit-middleware.js b/packages/adapter-node/src/kit-middleware.js index 5807b3d587fb..82fa0898fd8d 100644 --- a/packages/adapter-node/src/kit-middleware.js +++ b/packages/adapter-node/src/kit-middleware.js @@ -1,11 +1,11 @@ import { getRawBody } from '@sveltejs/kit/node'; /** + * @param {import('@sveltejs/kit').App} app * @return {import('polka').Middleware} */ -// TODO: type render function from @sveltejs/kit/adapter -// @ts-ignore -export function create_kit_middleware({ render }) { +export function create_kit_middleware({ init, render }) { + init(); return async (req, res) => { const parsed = new URL(req.url || '', 'http://localhost'); diff --git a/packages/adapter-node/src/middlewares.js b/packages/adapter-node/src/middlewares.js index ba4ee030244d..c21a53c14f6c 100644 --- a/packages/adapter-node/src/middlewares.js +++ b/packages/adapter-node/src/middlewares.js @@ -1,8 +1,4 @@ -// TODO hardcoding the relative location makes this brittle -// Also, we need most of the logic in another file for testing because -// ../output/server/app.js doesn't exist when we run the tests -// @ts-ignore -import { init, render } from '../output/server/app.js'; +import app from '@sveltejs/kit/app'; import { create_kit_middleware } from './kit-middleware.js'; import fs from 'fs'; @@ -10,8 +6,6 @@ import { dirname, join } from 'path'; import sirv from 'sirv'; import { fileURLToPath } from 'url'; -// App is a dynamic file built from the application layer. - const __dirname = dirname(fileURLToPath(import.meta.url)); /** @type {import('polka').Middleware} */ const noop_handler = (_req, _res, next) => next(); @@ -42,7 +36,4 @@ export const assetsMiddleware = fs.existsSync(paths.assets) }) : noop_handler; -export const kitMiddleware = (function () { - init(); - return create_kit_middleware({ render }); -})(); +export const kitMiddleware = create_kit_middleware(app); From 6cb5be2f8d1f4e008a4c4212847480e93229c968 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 13:22:02 +0700 Subject: [PATCH 14/23] use appResolver for adapter-node --- packages/adapter-node/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index 5db71949c080..9ae41ea7ef9c 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -4,6 +4,7 @@ import { pipeline } from 'stream'; import { fileURLToPath } from 'url'; import { promisify } from 'util'; +import { appResolver } from '@sveltejs/kit/adapter'; import esbuild from 'esbuild'; import glob from 'tiny-glob'; import zlib from 'zlib'; @@ -61,6 +62,7 @@ export default function ({ platform: 'node', target: 'node12', inject: [join(files, 'shims.js')], + plugins: [appResolver()], define: { APP_DIR: `"/${config.kit.appDir}/"` } From 8fd0c4c8956377007135ea9d370776d5a5e50cc0 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 13:28:51 +0700 Subject: [PATCH 15/23] update adapter api docs --- documentation/docs/80-adapter-api.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/documentation/docs/80-adapter-api.md b/documentation/docs/80-adapter-api.md index ef008e7b622a..c9b33ecadf6f 100644 --- a/documentation/docs/80-adapter-api.md +++ b/documentation/docs/80-adapter-api.md @@ -23,15 +23,17 @@ export default function (options) { The types for `Adapter` and its parameters are available in [types/config.d.ts](https://github.com/sveltejs/kit/blob/master/packages/kit/types/config.d.ts). -Within the `adapt` method, there are a number of things that an adapter should do: +Within the `adapt` method, an adapter should - Clear out the build directory -- Output code that: - - Calls `init` - - Converts from the platform's request to a [SvelteKit request](#hooks-handle), calls `render`, and converts from a [SvelteKit response](#hooks-handle) to the platform's - - Globally shims `fetch` to work on the target platform. SvelteKit provides a `@sveltejs/kit/install-fetch` helper for platforms that can use `node-fetch` +- Import `{ appResolver }` from `@sveltejs/kit/adapter` +- Pass in `appResolver()` to esbuild plugins for build options +- Import `app` from `@sveltejs/kit/app` +- Calls `app.init()` +- Converts from the platform's request to a [SvelteKit request](#hooks-handle), calls `app.render()`, and converts from a [SvelteKit response](#hooks-handle) to the platform's +- Globally shims `fetch` to work on the target platform. SvelteKit provides a `@sveltejs/kit/install-fetch` helper for platforms that can use `node-fetch` - Bundle the output to avoid needing to install dependencies on the target platform, if desired -- Call `utils.prerender` +- Call `utils.prerender()` - Put the user's static files and the generated JS/CSS in the correct location for the target platform If possible, we recommend putting the adapter output under the `build/` directory with any intermediate output placed under `'.svelte-kit/' + adapterName`. From e0602fcea92b1fa965d0920165e7855621a5f3c0 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 13:32:15 +0700 Subject: [PATCH 16/23] lockfile --- pnpm-lock.yaml | 88 +++++++++++++++++--------------------------------- 1 file changed, 30 insertions(+), 58 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 54ede1a71902..95aa693f6f53 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -282,7 +282,7 @@ packages: dependencies: '@actions/http-client': 1.0.11 '@octokit/core': 3.5.1 - '@octokit/plugin-paginate-rest': 2.16.0_@octokit+core@3.5.1 + '@octokit/plugin-paginate-rest': 2.15.1_@octokit+core@3.5.1 '@octokit/plugin-rest-endpoint-methods': 4.15.1_@octokit+core@3.5.1 dev: true @@ -558,6 +558,7 @@ packages: /@iarna/toml/2.2.5: resolution: {integrity: sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==} + dev: false /@istanbuljs/schema/0.1.3: resolution: {integrity: sha512-ZXRY4jNvVgSVQ8DL3LTcakaAtXwTVUxE81hslsyD2AtoXW/wVob10HkOJ1X/pAlcI7D+2YoZKg5do8G/w6RYgA==} @@ -619,17 +620,17 @@ packages: /@octokit/auth-token/2.4.5: resolution: {integrity: sha512-BpGYsPgJt05M7/L/5FoE1PiAbdxXFZkX/3kDYcsvd1v6UhlnE5e96dTDr0ezX/EFwciQxf3cNV0loipsURU+WA==} dependencies: - '@octokit/types': 6.26.0 + '@octokit/types': 6.25.0 dev: true /@octokit/core/3.5.1: resolution: {integrity: sha512-omncwpLVxMP+GLpLPgeGJBF6IWJFjXDS5flY5VbppePYX9XehevbDykRH9PdCdvqt9TS5AOTiDide7h0qrkHjw==} dependencies: '@octokit/auth-token': 2.4.5 - '@octokit/graphql': 4.8.0 + '@octokit/graphql': 4.6.4 '@octokit/request': 5.6.1 '@octokit/request-error': 2.1.0 - '@octokit/types': 6.26.0 + '@octokit/types': 6.25.0 before-after-hook: 2.2.2 universal-user-agent: 6.0.0 dev: true @@ -637,30 +638,30 @@ packages: /@octokit/endpoint/6.0.12: resolution: {integrity: sha512-lF3puPwkQWGfkMClXb4k/eUT/nZKQfxinRWJrdZaJO85Dqwo/G0yOC434Jr2ojwafWJMYqFGFa5ms4jJUgujdA==} dependencies: - '@octokit/types': 6.26.0 + '@octokit/types': 6.25.0 is-plain-object: 5.0.0 universal-user-agent: 6.0.0 dev: true - /@octokit/graphql/4.8.0: - resolution: {integrity: sha512-0gv+qLSBLKF0z8TKaSKTsS39scVKF9dbMxJpj3U0vC7wjNWFuIpL/z76Qe2fiuCbDRcJSavkXsVtMS6/dtQQsg==} + /@octokit/graphql/4.6.4: + resolution: {integrity: sha512-SWTdXsVheRmlotWNjKzPOb6Js6tjSqA2a8z9+glDJng0Aqjzti8MEWOtuT8ZSu6wHnci7LZNuarE87+WJBG4vg==} dependencies: '@octokit/request': 5.6.1 - '@octokit/types': 6.26.0 + '@octokit/types': 6.25.0 universal-user-agent: 6.0.0 dev: true - /@octokit/openapi-types/10.0.0: - resolution: {integrity: sha512-k1iO2zKuEjjRS1EJb4FwSLk+iF6EGp+ZV0OMRViQoWhQ1fZTk9hg1xccZII5uyYoiqcbC73MRBmT45y1vp2PPg==} + /@octokit/openapi-types/9.7.0: + resolution: {integrity: sha512-TUJ16DJU8mekne6+KVcMV5g6g/rJlrnIKn7aALG9QrNpnEipFc1xjoarh0PKaAWf2Hf+HwthRKYt+9mCm5RsRg==} dev: true - /@octokit/plugin-paginate-rest/2.16.0_@octokit+core@3.5.1: - resolution: {integrity: sha512-8YYzALPMvEZ35kgy5pdYvQ22Roz+BIuEaedO575GwE2vb/ACDqQn0xQrTJR4tnZCJn7pi8+AWPVjrFDaERIyXQ==} + /@octokit/plugin-paginate-rest/2.15.1_@octokit+core@3.5.1: + resolution: {integrity: sha512-47r52KkhQDkmvUKZqXzA1lKvcyJEfYh3TKAIe5+EzMeyDM3d+/s5v11i2gTk8/n6No6DPi3k5Ind6wtDbo/AEg==} peerDependencies: '@octokit/core': '>=2' dependencies: '@octokit/core': 3.5.1 - '@octokit/types': 6.26.0 + '@octokit/types': 6.25.0 dev: true /@octokit/plugin-rest-endpoint-methods/4.15.1_@octokit+core@3.5.1: @@ -669,14 +670,14 @@ packages: '@octokit/core': '>=3' dependencies: '@octokit/core': 3.5.1 - '@octokit/types': 6.26.0 + '@octokit/types': 6.25.0 deprecation: 2.3.1 dev: true /@octokit/request-error/2.1.0: resolution: {integrity: sha512-1VIvgXxs9WHSjicsRwq8PlR2LR2x6DwsJAaFgzdi0JfJoGSO8mYI/cHJQ+9FbN21aa+DrgNLnwObmyeSC8Rmpg==} dependencies: - '@octokit/types': 6.26.0 + '@octokit/types': 6.25.0 deprecation: 2.3.1 once: 1.4.0 dev: true @@ -686,16 +687,16 @@ packages: dependencies: '@octokit/endpoint': 6.0.12 '@octokit/request-error': 2.1.0 - '@octokit/types': 6.26.0 + '@octokit/types': 6.25.0 is-plain-object: 5.0.0 node-fetch: 2.6.1 universal-user-agent: 6.0.0 dev: true - /@octokit/types/6.26.0: - resolution: {integrity: sha512-RDxZBAFMtqs1ZPnbUu1e7ohPNfoNhTiep4fErY7tZs995BeHu369Vsh5woMIaFbllRWEZBfvTCS4hvDnMPiHrA==} + /@octokit/types/6.25.0: + resolution: {integrity: sha512-bNvyQKfngvAd/08COlYIN54nRgxskmejgywodizQNyiKoXmWRAjKup2/LYwm+T9V0gsKH6tuld1gM0PzmOiB4Q==} dependencies: - '@octokit/openapi-types': 10.0.0 + '@octokit/openapi-types': 9.7.0 dev: true /@polka/send/1.0.0-next.15: @@ -779,6 +780,7 @@ packages: dependencies: estree-walker: 2.0.2 picomatch: 2.2.3 + dev: false /@sveltejs/vite-plugin-svelte/1.0.0-next.21_svelte@3.42.4+vite@2.5.3: resolution: {integrity: sha512-JjPnoQoI1+VXHjguE1bWyFYdZvPN6VTZ5RsFRpju45zICas1N5jAuOqKTbhQ1G+eEMCbhhB6zjZgQQRtaXPhNQ==} @@ -803,29 +805,6 @@ packages: - supports-color dev: false - /@sveltejs/vite-plugin-svelte/1.0.0-next.21_svelte@3.40.0+vite@2.5.3: - resolution: {integrity: sha512-JjPnoQoI1+VXHjguE1bWyFYdZvPN6VTZ5RsFRpju45zICas1N5jAuOqKTbhQ1G+eEMCbhhB6zjZgQQRtaXPhNQ==} - engines: {node: ^12.20 || ^14.13.1 || >= 16} - peerDependencies: - diff-match-patch: ^1.0.5 - svelte: ^3.34.0 - vite: ^2.5.3 - peerDependenciesMeta: - diff-match-patch: - optional: true - dependencies: - '@rollup/pluginutils': 4.1.1 - debug: 4.3.2 - kleur: 4.1.4 - magic-string: 0.25.7 - require-relative: 0.8.7 - svelte: 3.40.0 - svelte-hmr: 0.14.7_svelte@3.40.0 - vite: 2.5.3 - transitivePeerDependencies: - - supports-color - dev: true - /@types/amphtml-validator/1.0.1: resolution: {integrity: sha512-DWE7fy6KtC+Uw0KV/HAmjuH2GB/o8yskXlvmVWR7mOVsLDybp+XrwkzEeRFU9wGjWKeRMBNGsx+5DRq7sUsAwA==} dependencies: @@ -1011,8 +990,8 @@ packages: '@types/node': 15.0.1 dev: true - /@types/yauzl/2.9.2: - resolution: {integrity: sha512-8uALY5LTvSuHgloDVUvWP3pIauILm+8/0pDMokuDYIoNsOkSwd5AiHBTSEJjKTDcZr5z8UpgOWZkxBF4iJftoA==} + /@types/yauzl/2.9.1: + resolution: {integrity: sha512-A1b8SU4D10uoPjwb0lnHmmu8wZhR9d+9o2PKBQT2jU5YPTKsxac6M2qGAdY7VcL+dHHhARVUDmeg0rOrcd9EjA==} dependencies: '@types/node': 15.0.1 dev: true @@ -1422,6 +1401,7 @@ packages: /cheap-watch/1.0.3: resolution: {integrity: sha512-xC5CruMhLzjPwJ5ecUxGu1uGmwJQykUhqd2QrCrYbwvsFYdRyviu6jG9+pccwDXJR/OpmOTOJ9yLFunVgQu9wg==} engines: {node: '>=8'} + dev: false /chokidar/3.5.1: resolution: {integrity: sha512-9+s+Od+W0VJJzawDma/gvBNQqkTiqYTWLuZoyAsivsI4AaWTCzHG06/TMjsf1cYe9Cb97UCEhjz7HvnPk2p/tw==} @@ -1491,6 +1471,7 @@ packages: /colorette/1.2.2: resolution: {integrity: sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w==} + dev: false /colors/1.4.0: resolution: {integrity: sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==} @@ -2070,7 +2051,7 @@ packages: get-stream: 5.2.0 yauzl: 2.10.0 optionalDependencies: - '@types/yauzl': 2.9.2 + '@types/yauzl': 2.9.1 transitivePeerDependencies: - supports-color dev: true @@ -2875,6 +2856,7 @@ packages: resolution: {integrity: sha512-FiB0kzdP0FFVGDKlRLEQ1BgDzU87dy5NnzjeW9YZNt+/c3+q82EQDUwniSAUxp/F0gFNI1ZhKU1FqYsMuqZVnw==} engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} hasBin: true + dev: false /natural-compare/1.4.0: resolution: {integrity: sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=} @@ -3240,6 +3222,7 @@ packages: colorette: 1.2.2 nanoid: 3.1.23 source-map-js: 0.6.2 + dev: false /preferred-pm/3.0.3: resolution: {integrity: sha512-+wZgbxNES/KlJs9q40F/1sfOd/j7f1O9JaHcW5Dsn3aUUOZg3L2bjpVUcKV2jvtElYfoTuQiNeMfQJ4kwUAhCQ==} @@ -3425,6 +3408,7 @@ packages: /require-relative/0.8.7: resolution: {integrity: sha1-eZlTn8ngR6N5KPoZb44VY9q9Nt4=} + dev: false /resolve-from/4.0.0: resolution: {integrity: sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==} @@ -3593,6 +3577,7 @@ packages: /source-map-js/0.6.2: resolution: {integrity: sha512-/3GptzWzu0+0MBQFrDKzw/DvvMTUORvgY6k6jd/VS6iCR4RDTKWH6v6WPwQoUO8667uQEf9Oe38DxAYWY5F/Ug==} engines: {node: '>=0.10.0'} + dev: false /source-map/0.7.3: resolution: {integrity: sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==} @@ -4102,19 +4087,6 @@ packages: fsevents: 2.3.2 dev: false - /vite/2.5.3: - resolution: {integrity: sha512-1wMDnjflvtTTkMov8O/Xb5+w1/VW/Gw8oCf8f6dqgHn8lMOEqq0SaPtFEQeikFcOKCfSbiU0nEi0LDIx6DNsaQ==} - engines: {node: '>=12.2.0'} - hasBin: true - dependencies: - esbuild: 0.12.20 - postcss: 8.3.6 - resolve: 1.20.0 - rollup: 2.55.0 - optionalDependencies: - fsevents: 2.3.2 - dev: true - /wcwidth/1.0.1: resolution: {integrity: sha1-8LDc+RW8X/FSivrbLA4XtTLaL+g=} dependencies: From e84a688306ba66341e045f91a8914a0ec7bb2c3e Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 13:40:40 +0700 Subject: [PATCH 17/23] adapter node tests --- packages/adapter-node/src/kit-middleware.js | 3 +-- packages/adapter-node/src/middlewares.js | 10 ++++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/adapter-node/src/kit-middleware.js b/packages/adapter-node/src/kit-middleware.js index 82fa0898fd8d..a37858e099d9 100644 --- a/packages/adapter-node/src/kit-middleware.js +++ b/packages/adapter-node/src/kit-middleware.js @@ -4,8 +4,7 @@ import { getRawBody } from '@sveltejs/kit/node'; * @param {import('@sveltejs/kit').App} app * @return {import('polka').Middleware} */ -export function create_kit_middleware({ init, render }) { - init(); +export function create_kit_middleware({ render }) { return async (req, res) => { const parsed = new URL(req.url || '', 'http://localhost'); diff --git a/packages/adapter-node/src/middlewares.js b/packages/adapter-node/src/middlewares.js index c21a53c14f6c..ca3c5ae66d85 100644 --- a/packages/adapter-node/src/middlewares.js +++ b/packages/adapter-node/src/middlewares.js @@ -1,11 +1,13 @@ -import app from '@sveltejs/kit/app'; -import { create_kit_middleware } from './kit-middleware.js'; - import fs from 'fs'; import { dirname, join } from 'path'; -import sirv from 'sirv'; import { fileURLToPath } from 'url'; +import sirv from 'sirv'; +import app from '@sveltejs/kit/app'; +import { create_kit_middleware } from './kit-middleware.js'; + +app.init(); + const __dirname = dirname(fileURLToPath(import.meta.url)); /** @type {import('polka').Middleware} */ const noop_handler = (_req, _res, next) => next(); From fa203536767a983d1d5a9681e5debfe6bdc6266d Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 13:50:47 +0700 Subject: [PATCH 18/23] revert constants changes --- packages/kit/src/core/adapt/prerender.js | 8 +++++--- packages/kit/src/core/adapt/test/index.js | 13 +++++++++---- packages/kit/src/core/adapt/utils.js | 6 +++--- packages/kit/src/core/constants.js | 3 --- packages/kit/src/core/preview/index.js | 6 +++--- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/kit/src/core/adapt/prerender.js b/packages/kit/src/core/adapt/prerender.js index ed1f290c5d1f..4ebb65484fe1 100644 --- a/packages/kit/src/core/adapt/prerender.js +++ b/packages/kit/src/core/adapt/prerender.js @@ -6,7 +6,7 @@ import { __fetch_polyfill } from '../../install-fetch.js'; import { mkdirp } from '../../utils/filesystem.js'; import { get_single_valued_header } from '../../utils/http.js'; -import { SERVER_OUTPUT } from '../constants.js'; +import { SVELTE_KIT } from '../constants.js'; /** * @typedef {import('types/config').PrerenderErrorHandler} PrerenderErrorHandler @@ -102,12 +102,14 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a __fetch_polyfill(); + const dir = resolve_path(cwd, `${SVELTE_KIT}/output`); + const seen = new Set(); - const server_root = resolve_path(cwd, SERVER_OUTPUT); + const server_root = resolve_path(dir); /** @type {import('types/app').App} */ - const app = await import(pathToFileURL(`${server_root}/app.js`).href); + const app = await import(pathToFileURL(`${server_root}/server/app.js`).href); app.init({ paths: config.kit.paths, diff --git a/packages/kit/src/core/adapt/test/index.js b/packages/kit/src/core/adapt/test/index.js index b8b915580b9f..484be0291dd5 100644 --- a/packages/kit/src/core/adapt/test/index.js +++ b/packages/kit/src/core/adapt/test/index.js @@ -5,7 +5,7 @@ import rimraf from 'rimraf'; import glob from 'tiny-glob/sync.js'; import { get_utils } from '../utils.js'; import { fileURLToPath } from 'url'; -import { CLIENT_OUTPUT, SERVER_OUTPUT } from '../../constants.js'; +import { SVELTE_KIT } from '../../constants.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = join(__filename, '..'); @@ -63,14 +63,19 @@ suite('copy files', () => { rimraf.sync(dest); utils.copy_client_files(dest); - assert.equal(glob('**', { cwd: `${cwd}/${CLIENT_OUTPUT}` }), glob('**', { cwd: dest })); + assert.equal( + glob('**', { cwd: `${cwd}/${SVELTE_KIT}/output/client` }), + glob('**', { cwd: dest }) + ); rimraf.sync(dest); utils.copy_server_files(dest); - assert.equal(glob('**', { cwd: `${cwd}/${SERVER_OUTPUT}` }), glob('**', { cwd: dest })); + assert.equal( + glob('**', { cwd: `${cwd}/${SVELTE_KIT}/output/server` }), + glob('**', { cwd: dest }) + ); }); - suite('prerender', async () => { const cwd = join(__dirname, 'fixtures/prerender'); const prerendered_files = join(__dirname, 'fixtures/prerender/build'); diff --git a/packages/kit/src/core/adapt/utils.js b/packages/kit/src/core/adapt/utils.js index 69cf30b737b3..12623819b611 100644 --- a/packages/kit/src/core/adapt/utils.js +++ b/packages/kit/src/core/adapt/utils.js @@ -1,4 +1,4 @@ -import { CLIENT_OUTPUT, SERVER_OUTPUT } from '../constants.js'; +import { SVELTE_KIT } from '../constants.js'; import { copy, rimraf, mkdirp } from '../../utils/filesystem.js'; import { prerender } from './prerender.js'; @@ -19,11 +19,11 @@ export function get_utils({ cwd, config, build_data, log }) { copy, copy_client_files(dest) { - copy(`${cwd}/${CLIENT_OUTPUT}`, dest, (file) => file[0] !== '.'); + copy(`${cwd}/${SVELTE_KIT}/output/client`, dest, (file) => file[0] !== '.'); }, copy_server_files(dest) { - copy(`${cwd}/${SERVER_OUTPUT}`, dest, (file) => file[0] !== '.'); + copy(`${cwd}/${SVELTE_KIT}/output/server`, dest, (file) => file[0] !== '.'); }, copy_static_files(dest) { diff --git a/packages/kit/src/core/constants.js b/packages/kit/src/core/constants.js index f8bbbe027387..565b216bfa66 100644 --- a/packages/kit/src/core/constants.js +++ b/packages/kit/src/core/constants.js @@ -4,6 +4,3 @@ export const SVELTE_KIT = '.svelte-kit'; // asset path so that we can serve local assets while still // verifying that requests are correctly prefixed export const SVELTE_KIT_ASSETS = '/_svelte_kit_assets'; - -export const CLIENT_OUTPUT = `${SVELTE_KIT}/output/client`; -export const SERVER_OUTPUT = `${SVELTE_KIT}/output/server`; diff --git a/packages/kit/src/core/preview/index.js b/packages/kit/src/core/preview/index.js index c3448e4effeb..a859ba209700 100644 --- a/packages/kit/src/core/preview/index.js +++ b/packages/kit/src/core/preview/index.js @@ -5,7 +5,7 @@ import { getRawBody } from '../node/index.js'; import { join, resolve } from 'path'; import { get_server } from '../server/index.js'; import { __fetch_polyfill } from '../../install-fetch.js'; -import { CLIENT_OUTPUT, SERVER_OUTPUT, SVELTE_KIT_ASSETS } from '../constants.js'; +import { SVELTE_KIT, SVELTE_KIT_ASSETS } from '../constants.js'; /** @param {string} dir */ const mutable = (dir) => @@ -32,7 +32,7 @@ export async function preview({ }) { __fetch_polyfill(); - const app_file = resolve(cwd, `${SERVER_OUTPUT}/app.js`); + const app_file = resolve(cwd, `${SVELTE_KIT}/output/server/app.js`); /** @type {import('types/app').App} */ const app = await import(pathToFileURL(app_file).href); @@ -45,7 +45,7 @@ export async function preview({ return next(); }; - const assets_handler = sirv(resolve(cwd, CLIENT_OUTPUT), { + const assets_handler = sirv(resolve(cwd, `${SVELTE_KIT}/output/client`), { maxAge: 31536000, immutable: true }); From 88d541ad1e7d9e634de9d50fe6890c8422c73241 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 13:57:34 +0700 Subject: [PATCH 19/23] remove promise wrapper from types --- packages/kit/types/ambient-modules.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/types/ambient-modules.d.ts b/packages/kit/types/ambient-modules.d.ts index ef1191f6ee0b..f2502da6f061 100644 --- a/packages/kit/types/ambient-modules.d.ts +++ b/packages/kit/types/ambient-modules.d.ts @@ -144,8 +144,8 @@ declare module '@sveltejs/kit/adapter' { declare module '@sveltejs/kit/app' { import { App } from '@sveltejs/kit'; - export function init(): Promise; - export function render(): Promise; + export function init(): App['init']; + export function render(): App['render']; const app: { init: typeof init; render: typeof render; From e1af57cf62814c04e92d2d7658caac7284ede9e4 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 13:58:32 +0700 Subject: [PATCH 20/23] update rest of adapter import paths --- .../adapter-cloudflare-workers/files/entry.js | 3 +-- packages/adapter-cloudflare-workers/index.js | 11 +++++++---- packages/adapter-netlify/files/entry.js | 3 +-- packages/adapter-netlify/index.js | 15 +++++++++------ packages/adapter-node/src/kit-middleware.js | 3 ++- packages/adapter-vercel/files/entry.js | 4 +--- packages/adapter-vercel/index.js | 3 +++ 7 files changed, 24 insertions(+), 18 deletions(-) diff --git a/packages/adapter-cloudflare-workers/files/entry.js b/packages/adapter-cloudflare-workers/files/entry.js index f7a7e397a620..ee28c237f214 100644 --- a/packages/adapter-cloudflare-workers/files/entry.js +++ b/packages/adapter-cloudflare-workers/files/entry.js @@ -1,5 +1,4 @@ -// TODO hardcoding the relative location makes this brittle -import { init, render } from '../output/server/app.js'; +import { init, render } from '@sveltejs/kit/app'; import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler'; init(); diff --git a/packages/adapter-cloudflare-workers/index.js b/packages/adapter-cloudflare-workers/index.js index 91909812030d..60bd73d6dc3c 100644 --- a/packages/adapter-cloudflare-workers/index.js +++ b/packages/adapter-cloudflare-workers/index.js @@ -1,9 +1,11 @@ -import fs from 'fs'; import { execSync } from 'child_process'; -import esbuild from 'esbuild'; -import toml from '@iarna/toml'; +import fs from 'fs'; import { fileURLToPath } from 'url'; +import toml from '@iarna/toml'; +import { appResolver } from '@sveltejs/kit/adapter'; +import esbuild from 'esbuild'; + /** * @typedef {import('esbuild').BuildOptions} BuildOptions */ @@ -40,7 +42,8 @@ export default function (options) { outfile: `${entrypoint}/index.js`, bundle: true, target: 'es2020', - platform: 'browser' + platform: 'browser', + plugins: [appResolver()] }; const build_options = diff --git a/packages/adapter-netlify/files/entry.js b/packages/adapter-netlify/files/entry.js index e5e7fd718c77..0878b798cde8 100644 --- a/packages/adapter-netlify/files/entry.js +++ b/packages/adapter-netlify/files/entry.js @@ -1,5 +1,4 @@ -// TODO hardcoding the relative location makes this brittle -import { init, render } from '../output/server/app.js'; +import { init, render } from '@sveltejs/kit/app'; init(); diff --git a/packages/adapter-netlify/index.js b/packages/adapter-netlify/index.js index 2368d0f2d1cd..573255829262 100644 --- a/packages/adapter-netlify/index.js +++ b/packages/adapter-netlify/index.js @@ -1,8 +1,10 @@ -import { appendFileSync, existsSync, readFileSync, writeFileSync } from 'fs'; +import fs from 'fs'; import { join, resolve } from 'path'; import { fileURLToPath } from 'url'; -import esbuild from 'esbuild'; + import toml from '@iarna/toml'; +import { appResolver } from '@sveltejs/kit/adapter'; +import esbuild from 'esbuild'; /** * @typedef {import('esbuild').BuildOptions} BuildOptions @@ -34,6 +36,7 @@ export default function (options) { outfile: '.netlify/functions-internal/__render.js', bundle: true, inject: [join(files, 'shims.js')], + plugins: [appResolver()], platform: 'node' }; @@ -42,7 +45,7 @@ export default function (options) { await esbuild.build(build_options); - writeFileSync(join('.netlify', 'package.json'), JSON.stringify({ type: 'commonjs' })); + fs.writeFileSync(join('.netlify', 'package.json'), JSON.stringify({ type: 'commonjs' })); utils.log.minor('Prerendering static pages...'); await utils.prerender({ @@ -57,7 +60,7 @@ export default function (options) { const redirectPath = join(publish, '_redirects'); utils.copy('_redirects', redirectPath); - appendFileSync(redirectPath, '\n\n/* /.netlify/functions/__render 200'); + fs.appendFileSync(redirectPath, '\n\n/* /.netlify/functions/__render 200'); } }; } @@ -65,12 +68,12 @@ export default function (options) { * @param {import('@sveltejs/kit').AdapterUtils} utils **/ function get_publish_directory(utils) { - if (existsSync('netlify.toml')) { + if (fs.existsSync('netlify.toml')) { /** @type {{ build?: { publish?: string }} & toml.JsonMap } */ let netlify_config; try { - netlify_config = toml.parse(readFileSync('netlify.toml', 'utf-8')); + netlify_config = toml.parse(fs.readFileSync('netlify.toml', 'utf-8')); } catch (err) { err.message = `Error parsing netlify.toml: ${err.message}`; throw err; diff --git a/packages/adapter-node/src/kit-middleware.js b/packages/adapter-node/src/kit-middleware.js index a37858e099d9..830d1c1e6160 100644 --- a/packages/adapter-node/src/kit-middleware.js +++ b/packages/adapter-node/src/kit-middleware.js @@ -19,7 +19,8 @@ export function create_kit_middleware({ render }) { const rendered = await render({ method: req.method, - headers: req.headers, // TODO: what about repeated headers, i.e. string[] + // @ts-expect-error - TODO: what about repeated headers, i.e. string[] + headers: req.headers, path: parsed.pathname, query: parsed.searchParams, rawBody: body diff --git a/packages/adapter-vercel/files/entry.js b/packages/adapter-vercel/files/entry.js index 35d6e3170a7c..9014c03f3e3b 100644 --- a/packages/adapter-vercel/files/entry.js +++ b/packages/adapter-vercel/files/entry.js @@ -1,8 +1,6 @@ +import { init, render } from '@sveltejs/kit/app'; import { getRawBody } from '@sveltejs/kit/node'; -// TODO hardcoding the relative location makes this brittle -import { init, render } from '../output/server/app.js'; - init(); export default async (req, res) => { diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index d5c53689f143..cae2ce0d34a5 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -1,6 +1,8 @@ import { writeFileSync } from 'fs'; import { join } from 'path'; import { fileURLToPath } from 'url'; + +import { appResolver } from '@sveltejs/kit/adapter'; import esbuild from 'esbuild'; /** @@ -37,6 +39,7 @@ export default function (options) { outfile: join(dirs.lambda, 'index.js'), bundle: true, inject: [join(files, 'shims.js')], + plugins: [appResolver()], platform: 'node' }; From d44975b11c8aced8fa8287faef665df12803cdb4 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Thu, 2 Sep 2021 21:53:52 +0700 Subject: [PATCH 21/23] remove accidental type as callback --- packages/adapter-node/src/middlewares.js | 2 +- packages/kit/src/core/adapt/test/index.js | 1 + packages/kit/types/ambient-modules.d.ts | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/adapter-node/src/middlewares.js b/packages/adapter-node/src/middlewares.js index ca3c5ae66d85..a3ed333eec04 100644 --- a/packages/adapter-node/src/middlewares.js +++ b/packages/adapter-node/src/middlewares.js @@ -2,8 +2,8 @@ import fs from 'fs'; import { dirname, join } from 'path'; import { fileURLToPath } from 'url'; -import sirv from 'sirv'; import app from '@sveltejs/kit/app'; +import sirv from 'sirv'; import { create_kit_middleware } from './kit-middleware.js'; app.init(); diff --git a/packages/kit/src/core/adapt/test/index.js b/packages/kit/src/core/adapt/test/index.js index 484be0291dd5..d1b3cd94093a 100644 --- a/packages/kit/src/core/adapt/test/index.js +++ b/packages/kit/src/core/adapt/test/index.js @@ -76,6 +76,7 @@ suite('copy files', () => { glob('**', { cwd: dest }) ); }); + suite('prerender', async () => { const cwd = join(__dirname, 'fixtures/prerender'); const prerendered_files = join(__dirname, 'fixtures/prerender/build'); diff --git a/packages/kit/types/ambient-modules.d.ts b/packages/kit/types/ambient-modules.d.ts index f2502da6f061..0445f5aa972d 100644 --- a/packages/kit/types/ambient-modules.d.ts +++ b/packages/kit/types/ambient-modules.d.ts @@ -144,8 +144,8 @@ declare module '@sveltejs/kit/adapter' { declare module '@sveltejs/kit/app' { import { App } from '@sveltejs/kit'; - export function init(): App['init']; - export function render(): App['render']; + export const init: App['init']; + export const render: App['render']; const app: { init: typeof init; render: typeof render; From 9dff930693015480e18a33a42dd1184188d70db1 Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Fri, 3 Sep 2021 13:39:06 +0700 Subject: [PATCH 22/23] some wording --- documentation/docs/80-adapter-api.md | 12 ++++++------ packages/adapter-cloudflare-workers/files/entry.js | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/documentation/docs/80-adapter-api.md b/documentation/docs/80-adapter-api.md index c9b33ecadf6f..8176bab201e7 100644 --- a/documentation/docs/80-adapter-api.md +++ b/documentation/docs/80-adapter-api.md @@ -23,17 +23,17 @@ export default function (options) { The types for `Adapter` and its parameters are available in [types/config.d.ts](https://github.com/sveltejs/kit/blob/master/packages/kit/types/config.d.ts). -Within the `adapt` method, an adapter should +Within the `adapt` method, an adapter should: -- Clear out the build directory +- Clear out the build directory before anything else - Import `{ appResolver }` from `@sveltejs/kit/adapter` - Pass in `appResolver()` to esbuild plugins for build options - Import `app` from `@sveltejs/kit/app` -- Calls `app.init()` -- Converts from the platform's request to a [SvelteKit request](#hooks-handle), calls `app.render()`, and converts from a [SvelteKit response](#hooks-handle) to the platform's -- Globally shims `fetch` to work on the target platform. SvelteKit provides a `@sveltejs/kit/install-fetch` helper for platforms that can use `node-fetch` +- Call `app.init()` as early as possible +- Convert from the platform's request to a [SvelteKit request](#hooks-handle), call `app.render()`, and convert from a [SvelteKit response](#hooks-handle) to the platform's +- Globally shim `fetch` to work on the target platform. SvelteKit provides a `@sveltejs/kit/install-fetch` helper for platforms that can use `node-fetch` - Bundle the output to avoid needing to install dependencies on the target platform, if desired -- Call `utils.prerender()` +- Prerender static pages by calling `utils.prerender()` - Put the user's static files and the generated JS/CSS in the correct location for the target platform If possible, we recommend putting the adapter output under the `build/` directory with any intermediate output placed under `'.svelte-kit/' + adapterName`. diff --git a/packages/adapter-cloudflare-workers/files/entry.js b/packages/adapter-cloudflare-workers/files/entry.js index ee28c237f214..6317c1d6f5e7 100644 --- a/packages/adapter-cloudflare-workers/files/entry.js +++ b/packages/adapter-cloudflare-workers/files/entry.js @@ -1,5 +1,5 @@ -import { init, render } from '@sveltejs/kit/app'; import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler'; +import { init, render } from '@sveltejs/kit/app'; init(); From 15f614918f45440d0b8d1466f226f87b04b5b30c Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Mon, 13 Sep 2021 14:50:11 +0700 Subject: [PATCH 23/23] fix merge conflict --- packages/adapter-node/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index 37f6e3b41149..fccc8cb5a2db 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -70,7 +70,9 @@ export default function ({ } }; - const build_options = esbuild_config ? await esbuild_config(defaultOptions) : defaultOptions; + const build_options = esbuild_config + ? await esbuild_config(default_options) + : default_options; await esbuild.build(build_options); utils.log.minor('Building SvelteKit reference server');