Skip to content

Commit

Permalink
[breaking] do a single bundling with Vite removing esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Oct 27, 2021
1 parent 52c0f99 commit c21ff65
Show file tree
Hide file tree
Showing 34 changed files with 142 additions and 397 deletions.
9 changes: 9 additions & 0 deletions .changeset/tidy-pets-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-vercel': patch
'@sveltejs/kit': patch
---

[breaking] do a single bundling with Vite removing esbuild
5 changes: 5 additions & 0 deletions documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const config = {
onError: 'fail'
},
router: true,
serverEntryPoint: null,
serviceWorker: {
files: (filepath) => !/\.DS_STORE/.test(filepath)
},
Expand Down Expand Up @@ -192,6 +193,10 @@ See [Prerendering](#ssr-and-javascript-prerender). An object containing zero or

Enables or disables the client-side [router](#ssr-and-javascript-router) app-wide.

### serverEntryPoint

A file path to a custom entry point for ther server. Passed to `vite.build.rollupOptions.input`. See [the Rollup docs](https://rollupjs.org/guide/en/#input) for more info.

### serviceWorker

An object containing zero or more of the following values:
Expand Down
1 change: 1 addition & 0 deletions documentation/docs/80-adapter-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function (options) {
/** @type {import('@sveltejs/kit').Adapter} */
return {
name: 'adapter-package-name',
serverEntryPoint: 'adapter-package-name/entry',
async adapt({ utils, config }) {
// adapter implementation
}
Expand Down
4 changes: 2 additions & 2 deletions examples/hn.svelte.dev/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import netlify from '@sveltejs/adapter-netlify';
import adapter from '@sveltejs/adapter-netlify';

export default {
kit: {
adapter: netlify(),
adapter: adapter(),
target: '#svelte'
}
};
31 changes: 0 additions & 31 deletions packages/adapter-cloudflare-workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,6 @@ npm run build && wrangler publish

More info on configuring a cloudflare worker site can be found [here](https://developers.cloudflare.com/workers/platform/sites/start-from-existing)

## 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
adapterCfw({
esbuild(options) {
return {
...options,
plugins: []
};
}
});
```

The default options for this version are as follows:

```js
const options = {
entryPoints: ['.svelte-kit/cloudflare-workers/entry.js'],
outfile: `${entrypoint}/index.js`,
bundle: true,
target: 'es2020',
platform: 'browser'
};
```

## Changelog

[The Changelog for this package is available on GitHub](https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare-workers/CHANGELOG.md).
5 changes: 3 additions & 2 deletions packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// TODO hardcoding the relative location makes this brittle
import { init, render } from '../output/server/app.js';
// $server-build doesn't exist until the app is built
// @ts-expect-error
import { init, render } from '$server-build';
import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler';

init();
Expand Down
21 changes: 1 addition & 20 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import fs from 'fs';
import { execSync } from 'child_process';
import esbuild from 'esbuild';
import toml from '@iarna/toml';
import { fileURLToPath } from 'url';

/**
* @typedef {import('esbuild').BuildOptions} BuildOptions
*/

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

Expand All @@ -34,20 +29,6 @@ export default function (options) {
utils.log.minor('Generating worker...');
utils.copy(`${files}/entry.js`, '.svelte-kit/cloudflare-workers/entry.js');

/** @type {BuildOptions} */
const default_options = {
entryPoints: ['.svelte-kit/cloudflare-workers/entry.js'],
outfile: `${entrypoint}/index.js`,
bundle: true,
target: 'es2020',
platform: 'browser'
};

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

await esbuild.build(build_options);

fs.writeFileSync(`${entrypoint}/package.json`, JSON.stringify({ main: 'index.js' }));

utils.log.info('Prerendering static pages...');
Expand Down
3 changes: 1 addition & 2 deletions packages/adapter-cloudflare-workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
},
"dependencies": {
"@iarna/toml": "^2.2.5",
"esbuild": "^0.13.4"
"@iarna/toml": "^2.2.5"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*"
Expand Down
32 changes: 0 additions & 32 deletions packages/adapter-netlify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,38 +66,6 @@ During compilation a required "catch all" redirect rule is automatically appende
node_bundler = "esbuild"
```

## 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
adapterNetlify({
esbuild(defaultOptions) {
return {
...defaultOptions,
plugins: []
};
}
});
```

The default options for this version are as follows:

```js
{
entryPoints: ['.svelte-kit/netlify/entry.js'],
// This is Netlify's internal functions directory, not the one for user functions.
outfile: '.netlify/functions-internal/__render.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-netlify/CHANGELOG.md).
6 changes: 4 additions & 2 deletions packages/adapter-netlify/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// TODO hardcoding the relative location makes this brittle
import { init, render } from '../output/server/app.js';
import '@sveltejs/kit/install-fetch';
// $server-build doesn't exist until the app is built
// @ts-expect-error
import { init, render } from '$server-build';

init();

Expand Down
1 change: 0 additions & 1 deletion packages/adapter-netlify/files/shims.js

This file was deleted.

27 changes: 3 additions & 24 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { appendFileSync, existsSync, readFileSync, writeFileSync } from 'fs';
import { join, resolve } from 'path';
import { fileURLToPath } from 'url';
import esbuild from 'esbuild';
import toml from '@iarna/toml';

/**
* @typedef {import('esbuild').BuildOptions} BuildOptions
*/

/** @type {import('.')} */
export default function (options) {
export default function () {
return {
name: '@sveltejs/adapter-netlify',
serverEntryPoint: '@sveltejs/adapter-netlify/entry',

async adapt({ utils }) {
// "build" is the default publish directory when Netlify detects SvelteKit
Expand All @@ -24,24 +20,7 @@ export default function (options) {
const files = fileURLToPath(new URL('./files', import.meta.url));

utils.log.minor('Generating serverless function...');
utils.copy(join(files, 'entry.js'), '.svelte-kit/netlify/entry.js');

/** @type {BuildOptions} */
const default_options = {
entryPoints: ['.svelte-kit/netlify/entry.js'],
// Any functions in ".netlify/functions-internal" are bundled in addition to user-defined Netlify functions.
// See https://github.com/netlify/build/pull/3213 for more details
outfile: '.netlify/functions-internal/__render.js',
bundle: true,
inject: [join(files, 'shims.js')],
platform: 'node'
};

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

await esbuild.build(build_options);

utils.copy(files, '.netlify');
writeFileSync(join('.netlify', 'package.json'), JSON.stringify({ type: 'commonjs' }));

utils.log.minor('Prerendering static pages...');
Expand Down
6 changes: 4 additions & 2 deletions packages/adapter-netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
".": {
"import": "./index.js"
},
"./entry": {
"import": "./files/entry.js"
},
"./package.json": "./package.json"
},
"main": "index.js",
Expand All @@ -26,8 +29,7 @@
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
},
"dependencies": {
"@iarna/toml": "^2.2.5",
"esbuild": "^0.13.4"
"@iarna/toml": "^2.2.5"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*"
Expand Down
57 changes: 7 additions & 50 deletions packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,13 @@ export default {
adapter: adapter({
// default options are shown
out: 'build',
precompress: false,
env: {
host: 'HOST',
port: 'PORT'
}
precompress: false
})
}
};
```

## Options

### entryPoint

The server entry point. Allows you to provide a [custom server implementation](#middleware). Defaults to the provided reference server.
## Build Options

### out

Expand All @@ -39,21 +31,23 @@ The directory to build the server to. It defaults to `build` — i.e. `node buil

Enables precompressing using gzip and brotli for assets and prerendered pages. It defaults to `false`.

### env
## Runtime Options

### Environment variables

By default, the server will accept connections on `0.0.0.0` using port 3000. These can be customised with the `PORT` and `HOST` environment variables:

```
HOST=127.0.0.1 PORT=4000 node build
```

You can specify different environment variables if necessary using the `env` option.
You can also specify `SOCKET_PATH` if you'd like to use a Unix domain socket or Windows named pipe.

## Middleware

The adapter exports a middleware `(req, res, next) => {}` that's compatible with [Express](https://github.com/expressjs/expressjs.com) / [Connect](https://github.com/senchalabs/connect) / [Polka](https://github.com/lukeed/polka). Additionally, it also exports a reference server implementation using this middleware with a plain Node HTTP server.

But you can use your favorite server framework to combine it with other middleware and server logic. You can import `kitMiddleware`, your ready-to-use SvelteKit middleware from the `build` directory. You can use [the `entryPoint` option](#entryPoint) to bundle your custom server entry point.
But you can use your favorite server framework to combine it with other middleware and server logic. You can import `kitMiddleware`, your ready-to-use SvelteKit middleware from the `build` directory. You can use [the `serverEntryPoint` option](https://kit.svelte.dev/docs#configuration-serverentrypoint) to bundle your custom server entry point.

```js
// src/server.js
Expand Down Expand Up @@ -83,43 +77,6 @@ app.listen(3000);

For using middleware in dev mode, [see the FAQ](https://kit.svelte.dev/faq#how-do-i-use-x-with-sveltekit-how-do-i-use-middleware).

## 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
adapterNode({
esbuild(defaultOptions) {
return {
...defaultOptions,
plugins: []
};
}
});
```

The default options for this version are as follows:

```js
{
entryPoints: ['.svelte-kit/node/index.js'],
outfile: 'pathTo/index.js',
bundle: true,
external: allProductionDependencies, // from package.json
format: 'esm',
platform: 'node',
target: 'node14',
inject: ['pathTo/shims.js'],
define: {
esbuild_app_dir: `"${config.kit.appDir}"`
}
}
```

## Changelog

[The Changelog for this package is available on GitHub](https://github.com/sveltejs/kit/blob/master/packages/adapter-node/CHANGELOG.md).
Expand Down
Loading

0 comments on commit c21ff65

Please sign in to comment.