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 1126f9b
Show file tree
Hide file tree
Showing 32 changed files with 133 additions and 392 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
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
51 changes: 6 additions & 45 deletions packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ export default {
adapter: adapter({
// default options are shown
out: 'build',
precompress: false,
env: {
host: 'HOST',
port: 'PORT'
}
precompress: false
})
}
};
```

## Options
## Build Options

### entryPoint

Expand All @@ -39,15 +35,17 @@ 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

Expand Down Expand Up @@ -83,43 +81,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 1126f9b

Please sign in to comment.