Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update adapter to work with SvelteKit next-208 #17

Merged
merged 4 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 2 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# svelte-adapter-azure-swa

:stop_sign: IMPORTANT: due to breaking changes to the adapter API introduced by [SvelteKit next-208](https://github.com/sveltejs/kit/blob/master/packages/kit/CHANGELOG.md#100-next208), this adapter will only work with SvelteKit versions up to next-206. You can track the work to support the latest SvelteKit version in [this issue](https://github.com/geoffrich/svelte-adapter-azure-swa/issues/6). For now, you will need to manually pin to a supported version to use this adapter. See [this comment](https://github.com/geoffrich/svelte-adapter-azure-swa/issues/14#issuecomment-1003452028) for more details.

| :warning: WARNING: this project is considered to be in BETA until SvelteKit is available for general use and the Adapter API is stable. Please report any issues you encounter. |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
> :warning: WARNING: this project is considered to be in BETA until SvelteKit is available for general use and the Adapter API is stable. Please report any issues you encounter.

Adapter for Svelte apps that creates an Azure Static Web App, using an Azure function for dynamic server rendering. If your app is purely static, you may be able to use [adapter-static](https://www.npmjs.com/package/@sveltejs/adapter-static) instead.

Expand Down Expand Up @@ -56,11 +53,9 @@ When deploying to Azure, you will need to properly [configure your build](https:

## Running locally with the Azure SWA CLI

**Local SWA debugging is currently broken** due to the following SWA CLI issues: [261](https://github.com/Azure/static-web-apps-cli/issues/261) and [286](https://github.com/Azure/static-web-apps-cli/issues/286)

You can debug using the [Azure Static Web Apps CLI](https://github.com/Azure/static-web-apps-cli). Note that the CLI is currently in preview and you may encounter issues.

To run the CLI, install `@azure/static-web-apps-cli` and add a `swa-cli.config.json` to your project (see sample below). Run `swa start` to start the emulator. See the CLI docs for more information on usage.
To run the CLI, install `@azure/static-web-apps-cli` and the [Azure Functions Core Tools](https://github.com/Azure/static-web-apps-cli#serve-both-the-static-app-and-api) and add a `swa-cli.config.json` to your project (see sample below). Run `npm run build` to build your project and `swa start` to start the emulator. See the [CLI docs](https://github.com/Azure/static-web-apps-cli) for more information on usage.

### Sample `swa-cli.config.json`

Expand All @@ -74,22 +69,3 @@ To run the CLI, install `@azure/static-web-apps-cli` and add a `swa-cli.config.j
}
}
```

## 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: []
};
}
});
```
17 changes: 9 additions & 8 deletions files/entry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// TODO hardcoding the relative location makes this brittle
// Update once https://github.com/sveltejs/kit/pull/2579 merged
import { init, render } from '../output/server/app.js';
import { __fetch_polyfill } from '@sveltejs/kit/install-fetch';
import { App } from 'APP';
import { manifest } from 'MANIFEST';

init();
__fetch_polyfill();

const app = new App(manifest);

/**
* @typedef {import('@azure/functions').AzureFunction} AzureFunction
Expand All @@ -18,16 +20,15 @@ export async function index(context) {
// because we proxy all requests to the render function, the original URL in the request is /api/__render
// this header contains the URL the user requested
const originalUrl = headers['x-ms-original-url'];
const { pathname, searchParams } = new URL(originalUrl);
const url = new URL(originalUrl);

const encoding = headers['content-encoding'] || 'utf-8';
const rawBody = typeof body === 'string' ? Buffer.from(body, encoding) : body;

const rendered = await render({
const rendered = await app.render({
url,
method,
headers,
path: pathname,
query: searchParams,
rawBody
});

Expand Down
1 change: 0 additions & 1 deletion files/shims.js

This file was deleted.

4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Adapter } from '@sveltejs/kit';

declare function plugin(): Adapter;
export = plugin;
64 changes: 40 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writeFileSync } from 'fs';
import { join } from 'path';
import { join, posix } from 'path';
import { fileURLToPath } from 'url';
import esbuild from 'esbuild';

Expand All @@ -8,54 +8,70 @@ import esbuild from 'esbuild';
*/

/** @type {import('.')} */
export default function (options) {
export default function () {
return {
name: 'adapter-azure-swa',

async adapt({ utils }) {
// implementation based on the vercel adapter
async adapt(builder) {
const tmp = builder.getBuildDirectory('azure-tmp');
const publish = 'build';
const staticDir = join(publish, 'static');
const apiDir = join('api', 'render');
const entry = '.svelte-kit/azure-swa/entry.js';
const entry = `${tmp}/entry.js`;
builder.log.minor(`Publishing to "${publish}"`);

utils.log.minor(`Publishing to "${publish}"`);
builder.rimraf(tmp);
builder.rimraf(publish);
builder.rimraf(apiDir);

utils.rimraf(publish);
utils.rimraf(apiDir);
builder.log.minor('Prerendering static pages...');
await builder.prerender({
dest: staticDir
});

const files = fileURLToPath(new URL('./files', import.meta.url));

utils.log.minor('Generating serverless function...');
utils.copy(join(files, 'entry.js'), entry);
utils.copy(
builder.log.minor('Generating serverless function...');

// use posix because of https://github.com/sveltejs/kit/pull/3200
const relativePath = posix.relative(tmp, builder.getServerDirectory());

builder.copy(join(files, 'entry.js'), entry, {
replace: {
APP: `${relativePath}/app.js`,
MANIFEST: './manifest.js'
}
});

writeFileSync(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({
relativePath
})};\n`
);

builder.copy(
join(files, 'staticwebapp.config.json'),
join(publish, 'staticwebapp.config.json')
);
utils.copy(join(files, 'api'), apiDir);

builder.copy(join(files, 'api'), apiDir);

/** @type {BuildOptions} */
const default_options = {
entryPoints: [entry],
outfile: join(apiDir, 'index.js'),
bundle: true,
inject: [join(files, 'shims.js')],
platform: 'node',
target: 'node12'
};

const build_options =
options && options.esbuild ? await options.esbuild(default_options) : default_options;

await esbuild.build(build_options);

utils.log.minor('Prerendering static pages...');
await utils.prerender({
dest: staticDir
});
await esbuild.build(default_options);

utils.log.minor('Copying assets...');
utils.copy_static_files(staticDir);
utils.copy_client_files(staticDir);
builder.log.minor('Copying assets...');
builder.writeStatic(staticDir);
builder.writeClient(staticDir);
}
};
}
Loading