@sveltejs/adapter-cloudflare
Advanced tools
Comparing version 1.0.0-next.4 to 1.0.0-next.5
@@ -1,6 +0,8 @@ | ||
/* global ASSETS */ | ||
import { init, render } from '../output/server/app.js'; | ||
import { App } from '../output/server/app.js'; | ||
import { manifest, prerendered } from './manifest.js'; | ||
init(); | ||
const app = new App(manifest); | ||
const prefix = `/${manifest.appDir}/`; | ||
export default { | ||
@@ -10,6 +12,11 @@ async fetch(req, env) { | ||
// check generated asset_set for static files | ||
let pathname = url.pathname.substring(1); | ||
// static assets | ||
if (url.pathname.startsWith(prefix)) return env.ASSETS.fetch(req); | ||
// prerendered pages and index.html files | ||
const pathname = url.pathname.replace(/\/$/, ''); | ||
let file = pathname.substring(1); | ||
try { | ||
pathname = decodeURIComponent(pathname); | ||
file = decodeURIComponent(file); | ||
} catch (err) { | ||
@@ -19,12 +26,15 @@ // ignore | ||
if (ASSETS.has(pathname)) { | ||
if ( | ||
manifest.assets.has(file) || | ||
manifest.assets.has(file + '/index.html') || | ||
prerendered.has(pathname || '/') | ||
) { | ||
return env.ASSETS.fetch(req); | ||
} | ||
// dynamically-generated pages | ||
try { | ||
const rendered = await render({ | ||
host: url.host || '', | ||
path: url.pathname || '', | ||
query: url.searchParams || '', | ||
rawBody: await read(req), | ||
const rendered = await app.render({ | ||
url, | ||
rawBody: new Uint8Array(await req.arrayBuffer()), | ||
headers: Object.fromEntries(req.headers), | ||
@@ -37,3 +47,3 @@ method: req.method | ||
status: rendered.status, | ||
headers: makeHeaders(rendered.headers) | ||
headers: make_headers(rendered.headers) | ||
}); | ||
@@ -52,7 +62,3 @@ } | ||
async function read(request) { | ||
return new Uint8Array(await request.arrayBuffer()); | ||
} | ||
function makeHeaders(headers) { | ||
function make_headers(headers) { | ||
const result = new Headers(); | ||
@@ -59,0 +65,0 @@ for (const header in headers) { |
import { Adapter } from '@sveltejs/kit'; | ||
import { BuildOptions } from 'esbuild'; | ||
export default function (options?: BuildOptions): Adapter; | ||
declare function plugin(): Adapter; | ||
export = plugin; |
53
index.js
@@ -1,40 +0,39 @@ | ||
import { join } from 'path'; | ||
import { writeFileSync } from 'fs'; | ||
import { relative } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import * as esbuild from 'esbuild'; | ||
/** | ||
* @param {esbuild.BuildOptions} [options] | ||
*/ | ||
/** @type {import('.')} */ | ||
export default function (options = {}) { | ||
return { | ||
name: '@sveltejs/adapter-cloudflare', | ||
async adapt({ utils, config }) { | ||
async adapt(builder) { | ||
const files = fileURLToPath(new URL('./files', import.meta.url)); | ||
const target_dir = join(process.cwd(), '.svelte-kit', 'cloudflare'); | ||
utils.rimraf(target_dir); | ||
const dest = builder.getBuildDirectory('cloudflare'); | ||
const tmp = builder.getBuildDirectory('cloudflare-tmp'); | ||
const static_files = utils | ||
.copy(config.kit.files.assets, target_dir) | ||
.map((f) => f.replace(`${target_dir}/`, '')); | ||
builder.rimraf(dest); | ||
builder.rimraf(tmp); | ||
builder.mkdirp(tmp); | ||
const client_files = utils | ||
.copy(`${process.cwd()}/.svelte-kit/output/client`, target_dir) | ||
.map((f) => f.replace(`${target_dir}/`, '')); | ||
builder.writeStatic(dest); | ||
builder.writeClient(dest); | ||
// returns nothing, very sad | ||
// TODO(future) get/save output | ||
await utils.prerender({ | ||
dest: `${target_dir}/` | ||
}); | ||
const { paths } = await builder.prerender({ dest }); | ||
const static_assets = [...static_files, ...client_files]; | ||
const assets = `const ASSETS = new Set(${JSON.stringify(static_assets)});\n`; | ||
const relativePath = relative(tmp, builder.getServerDirectory()); | ||
const worker = readFileSync(join(files, 'worker.js'), { encoding: 'utf-8' }); | ||
writeFileSync( | ||
`${tmp}/manifest.js`, | ||
`export const manifest = ${builder.generateManifest({ | ||
relativePath | ||
})};\n\nexport const prerendered = new Set(${JSON.stringify(paths)});\n` | ||
); | ||
const target_worker = join(target_dir, '_worker.js'); | ||
builder.copy(`${files}/worker.js`, `${tmp}/_worker.js`, { | ||
replace: { | ||
APP: `${relativePath}/app.js` | ||
} | ||
}); | ||
writeFileSync(target_worker, assets + worker); | ||
await esbuild.build({ | ||
@@ -44,4 +43,4 @@ target: 'es2020', | ||
...options, | ||
entryPoints: [target_worker], | ||
outfile: target_worker, | ||
entryPoints: [`${tmp}/_worker.js`], | ||
outfile: `${dest}/_worker.js`, | ||
allowOverwrite: true, | ||
@@ -48,0 +47,0 @@ format: 'esm', |
{ | ||
"name": "@sveltejs/adapter-cloudflare", | ||
"version": "1.0.0-next.4", | ||
"version": "1.0.0-next.5", | ||
"repository": { | ||
@@ -29,3 +29,3 @@ "type": "git", | ||
"devDependencies": { | ||
"@sveltejs/kit": "1.0.0-next.203" | ||
"@sveltejs/kit": "1.0.0-next.208" | ||
}, | ||
@@ -39,4 +39,3 @@ "publishConfig": { | ||
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore" | ||
}, | ||
"readme": "# adapter-cloudflare\n\n[Adapter](https://kit.svelte.dev/docs#adapters) for building SvelteKit\napplications on Cloudflare Pages with Workers integration.\n\n_**Comparisons**_\n\n- `adapter-cloudflare` – supports all SvelteKit features; builds for\n [Cloudflare Pages](https://blog.cloudflare.com/cloudflare-pages-goes-full-stack/)\n- `adapter-cloudflare-workers` – supports all SvelteKit features; builds for\n Cloudflare Workers\n- `adapter-static` – only produces client-side static assets; compatible with\n Cloudflare Pages\n\n> **Note:** Cloudflare Pages' new Workers integration is currently in beta.<br/>\n> Compared to `adapter-cloudflare-workers`, this adapter will be the preferred approach for most users since building on top of Pages unlocks automatic builds and deploys, preview deployments, instant rollbacks, etc.<br/>\n> From SvelteKit's perspective, there is no difference and no functionality loss when migrating to/from the Workers and the Pages adapters.\n\n## Installation\n\n```sh\n$ npm i --save-dev @sveltejs/adapter-cloudflare@next\n```\n\n## Usage\n\nYou can include these changes in your `svelte.config.js` configuration file:\n\n```js\nimport cloudflare from '@sveltejs/adapter-cloudflare';\n\nexport default {\n\tkit: {\n\t\ttarget: '#svelte',\n\t\tadapter: cloudflare({\n\t\t\t// any esbuild options\n\t\t})\n\t}\n};\n```\n\n### Options\n\nThe adapter optionally accepts all\n[`esbuild.build`](https://esbuild.github.io/api/#build-api) configuration.\n\nThese are the default options, of which, all but `target` and `platform` are\nenforced:\n\n```js\ntarget: 'es2020',\nplatform: 'browser',\nentryPoints: '< input >',\noutfile: '<output>/_worker.js',\nallowOverwrite: true,\nformat: 'esm',\nbundle: true,\n```\n\n## Deployment\n\nPlease follow the [Get Started Guide](https://developers.cloudflare.com/pages/get-started) for Cloudflare Pages to begin.\n\nWhen configuring your project settings, you must use the following settings:\n\n- **Framework preset** – None\n- **Build command** – `npm run build` or `svelte-kit build`\n- **Build output directory** – `.svelte-kit/cloudflare`\n- **Environment variables**\n - `NODE_VERSION`: `16` or `14`\n\n> **Important:** You need to add a `NODE_VERSION` environment variable to both the \"production\" and \"preview\" environments. You can add this during project setup or later in the Pages project settings. SvelteKit requires Node `14.13` or later, so you should use `14` or `16` as the `NODE_VERSION` value.\n\n## Changelog\n\n[The Changelog for this package is available on\nGitHub](https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare/CHANGELOG.md).\n" | ||
} | ||
} |
# adapter-cloudflare | ||
[Adapter](https://kit.svelte.dev/docs#adapters) for building SvelteKit | ||
applications on Cloudflare Pages with Workers integration. | ||
[Adapter](https://kit.svelte.dev/docs#adapters) for building SvelteKit applications on [Cloudflare Pages](https://developers.cloudflare.com/pages/) with [Workers integration](https://developers.cloudflare.com/pages/platform/functions). | ||
@@ -30,3 +29,3 @@ _**Comparisons**_ | ||
```js | ||
import cloudflare from '@sveltejs/adapter-cloudflare'; | ||
import adapter from '@sveltejs/adapter-cloudflare'; | ||
@@ -36,5 +35,3 @@ export default { | ||
target: '#svelte', | ||
adapter: cloudflare({ | ||
// any esbuild options | ||
}) | ||
adapter: adapter() | ||
} | ||
@@ -44,20 +41,2 @@ }; | ||
### Options | ||
The adapter optionally accepts all | ||
[`esbuild.build`](https://esbuild.github.io/api/#build-api) configuration. | ||
These are the default options, of which, all but `target` and `platform` are | ||
enforced: | ||
```js | ||
target: 'es2020', | ||
platform: 'browser', | ||
entryPoints: '< input >', | ||
outfile: '<output>/_worker.js', | ||
allowOverwrite: true, | ||
format: 'esm', | ||
bundle: true, | ||
``` | ||
## Deployment | ||
@@ -79,3 +58,2 @@ | ||
[The Changelog for this package is available on | ||
GitHub](https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare/CHANGELOG.md). | ||
[The Changelog for this package is available on GitHub](https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare/CHANGELOG.md). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107
7297
56