@sveltejs/adapter-vercel
Advanced tools
Comparing version 1.0.0-next.32 to 1.0.0-next.33
@@ -0,11 +1,11 @@ | ||
import { __fetch_polyfill } from '@sveltejs/kit/install-fetch'; | ||
import { getRawBody } from '@sveltejs/kit/node'; | ||
import { App } from 'APP'; | ||
import { manifest } from 'MANIFEST'; | ||
// TODO hardcoding the relative location makes this brittle | ||
import { init, render } from '../output/server/app.js'; | ||
__fetch_polyfill(); | ||
init(); | ||
const app = new App(manifest); | ||
export default async (req, res) => { | ||
const { pathname, searchParams } = new URL(req.url || '', 'http://localhost'); | ||
let body; | ||
@@ -20,7 +20,6 @@ | ||
const rendered = await render({ | ||
const rendered = await app.render({ | ||
url: req.url, | ||
method: req.method, | ||
headers: req.headers, | ||
path: pathname, | ||
query: searchParams, | ||
rawBody: body | ||
@@ -27,0 +26,0 @@ }); |
import { Adapter } from '@sveltejs/kit'; | ||
import { BuildOptions } from 'esbuild'; | ||
interface AdapterOptions { | ||
esbuild?: (options: BuildOptions) => Promise<BuildOptions> | BuildOptions; | ||
} | ||
declare function plugin(options?: AdapterOptions): Adapter; | ||
declare function plugin(): Adapter; | ||
export = plugin; |
96
index.js
import { writeFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { relative } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import esbuild from 'esbuild'; | ||
/** | ||
* @typedef {import('esbuild').BuildOptions} BuildOptions | ||
*/ | ||
// By writing to .output, we opt in to the Vercel filesystem API: | ||
// https://vercel.com/docs/file-system-api | ||
const VERCEL_OUTPUT = '.output'; | ||
/** @type {import('.')} **/ | ||
export default function (options) { | ||
export default function () { | ||
return { | ||
name: '@sveltejs/adapter-vercel', | ||
async adapt({ utils }) { | ||
const dir = '.vercel_build_output'; | ||
utils.rimraf(dir); | ||
async adapt(builder) { | ||
const tmp = builder.getBuildDirectory('vercel-tmp'); | ||
builder.rimraf(VERCEL_OUTPUT); | ||
builder.rimraf(tmp); | ||
builder.log.minor('Prerendering static pages...'); | ||
await builder.prerender({ | ||
dest: `${VERCEL_OUTPUT}/static` | ||
}); | ||
builder.log.minor('Generating serverless function...'); | ||
const files = fileURLToPath(new URL('./files', import.meta.url)); | ||
const relativePath = relative(tmp, builder.getServerDirectory()); | ||
const dirs = { | ||
static: join(dir, 'static'), | ||
lambda: join(dir, 'functions/node/render') | ||
}; | ||
builder.copy(files, tmp, { | ||
replace: { | ||
APP: `${relativePath}/app.js`, | ||
MANIFEST: './manifest.js' | ||
} | ||
}); | ||
// TODO ideally we'd have something like utils.tmpdir('vercel') | ||
// rather than hardcoding '.svelte-kit/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-kit/vercel/entry.js'); | ||
writeFileSync( | ||
`${tmp}/manifest.js`, | ||
`export const manifest = ${builder.generateManifest({ | ||
relativePath | ||
})};\n` | ||
); | ||
/** @type {BuildOptions} */ | ||
const default_options = { | ||
entryPoints: ['.svelte-kit/vercel/entry.js'], | ||
outfile: join(dirs.lambda, 'index.js'), | ||
await esbuild.build({ | ||
entryPoints: [`${tmp}/entry.js`], | ||
outfile: `${VERCEL_OUTPUT}/server/pages/__render.js`, | ||
target: 'node14', | ||
bundle: true, | ||
inject: [join(files, 'shims.js')], | ||
platform: 'node' | ||
}; | ||
}); | ||
const build_options = | ||
options && options.esbuild ? await options.esbuild(default_options) : default_options; | ||
writeFileSync( | ||
`${VERCEL_OUTPUT}/server/pages/package.json`, | ||
JSON.stringify({ type: 'commonjs' }) | ||
); | ||
await esbuild.build(build_options); | ||
builder.log.minor('Copying assets...'); | ||
builder.writeClient(`${VERCEL_OUTPUT}/static`); | ||
builder.writeStatic(`${VERCEL_OUTPUT}/static`); | ||
writeFileSync(join(dirs.lambda, 'package.json'), JSON.stringify({ type: 'commonjs' })); | ||
utils.log.minor('Prerendering static pages...'); | ||
await utils.prerender({ | ||
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.copy(join(files, 'routes.json'), join(dir, 'config/routes.json')); | ||
builder.log.minor('Writing manifests...'); | ||
writeFileSync( | ||
`${VERCEL_OUTPUT}/routes-manifest.json`, | ||
JSON.stringify({ | ||
version: 3, | ||
dynamicRoutes: [ | ||
{ | ||
page: '/__render', | ||
regex: '^/.*' | ||
} | ||
] | ||
}) | ||
); | ||
} | ||
}; | ||
} |
{ | ||
"name": "@sveltejs/adapter-vercel", | ||
"version": "1.0.0-next.32", | ||
"version": "1.0.0-next.33", | ||
"repository": { | ||
@@ -28,3 +28,3 @@ "type": "git", | ||
"devDependencies": { | ||
"@sveltejs/kit": "1.0.0-next.202" | ||
"@sveltejs/kit": "1.0.0-next.208" | ||
}, | ||
@@ -31,0 +31,0 @@ "scripts": { |
@@ -22,35 +22,4 @@ # adapter-vercel | ||
## Advanced Configuration | ||
### esbuild | ||
As an escape hatch, you may optionally specify a function which will receive the final esbuild options generated by this adapter and returns a modified esbuild configuration. The result of this function will be passed as-is to esbuild. The function can be async. | ||
For example, you may wish to add a plugin: | ||
```js | ||
adapterVercel({ | ||
esbuild(defaultOptions) { | ||
return { | ||
...defaultOptions, | ||
plugins: [] | ||
}; | ||
} | ||
}); | ||
``` | ||
The default options for this version are as follows: | ||
```js | ||
{ | ||
entryPoints: ['.svelte-kit/vercel/entry.js'], | ||
outfile: `pathToLambdaFolder/index.js`, | ||
bundle: true, | ||
inject: ['pathTo/shims.js'], | ||
platform: 'node' | ||
} | ||
``` | ||
## Changelog | ||
[The Changelog for this package is available on GitHub](https://github.com/sveltejs/kit/blob/master/packages/adapter-vercel/CHANGELOG.md). |
5145
6
25