Skip to content

Commit 176b51a

Browse files
committed
[breaking] do a single bundling with Vite removing esbuild
1 parent 0f523a9 commit 176b51a

File tree

36 files changed

+156
-305
lines changed

36 files changed

+156
-305
lines changed

.changeset/tidy-pets-tan.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
'@sveltejs/adapter-cloudflare-workers': patch
4+
'@sveltejs/adapter-netlify': patch
5+
'@sveltejs/adapter-node': patch
6+
'@sveltejs/adapter-vercel': patch
7+
'@sveltejs/kit': patch
8+
---
9+
10+
[breaking] do a single bundling with Vite making esbuild optional

documentation/docs/14-configuration.md

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const config = {
4747
onError: 'fail'
4848
},
4949
router: true,
50+
serverEntryPoint: null,
5051
serviceWorker: {
5152
files: (filepath) => !/\.DS_STORE/.test(filepath)
5253
},
@@ -195,6 +196,10 @@ See [Prerendering](#ssr-and-javascript-prerender). An object containing zero or
195196

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

199+
### serverEntryPoint
200+
201+
A file path to a custom entry point for the server. Passed to `vite.build.rollupOptions.input`. See [the Rollup docs](https://rollupjs.org/guide/en/#input) for more info.
202+
198203
### serviceWorker
199204

200205
An object containing zero or more of the following values:

documentation/docs/80-adapter-api.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default function (options) {
1212
/** @type {import('@sveltejs/kit').Adapter} */
1313
return {
1414
name: 'adapter-package-name',
15+
serverEntryPoint: 'adapter-package-name/entry',
1516
async adapt({ utils, config }) {
1617
// adapter implementation
1718
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import netlify from '@sveltejs/adapter-netlify';
1+
import adapter from '@sveltejs/adapter-netlify';
22

33
export default {
44
kit: {
5-
adapter: netlify(),
5+
adapter: adapter(),
66
target: '#svelte'
77
}
88
};

packages/adapter-cloudflare-workers/files/entry.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// TODO hardcoding the relative location makes this brittle
2-
import { init, render } from '../output/server/app.js';
1+
// $server-build doesn't exist until the app is built
2+
// @ts-expect-error
3+
import { init, render } from '$server-build';
34
import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler';
45

56
init();

packages/adapter-cloudflare-workers/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { fileURLToPath } from 'url';
1212
export default function (options) {
1313
return {
1414
name: '@sveltejs/adapter-cloudflare-workers',
15+
serverEntryPoint: '@sveltejs/adapter-cloudflare-workers/entry',
1516

1617
async adapt({ utils }) {
1718
const { site } = validate_config(utils);

packages/adapter-cloudflare-workers/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
".": {
1313
"import": "./index.js"
1414
},
15+
"./entry": {
16+
"import": "./files/entry.js"
17+
},
1518
"./package.json": "./package.json"
1619
},
1720
"main": "index.js",

packages/adapter-cloudflare/files/worker.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* global ASSETS */
2-
import { init, render } from '../output/server/app.js';
2+
// $server-build doesn't exist until the app is built
3+
// @ts-expect-error
4+
import { init, render } from '$server-build';
35

46
init();
57

packages/adapter-cloudflare/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as esbuild from 'esbuild';
99
export default function (options = {}) {
1010
return {
1111
name: '@sveltejs/adapter-cloudflare',
12+
serverEntryPoint: '@sveltejs/adapter-cloudflare/entry',
1213
async adapt({ utils, config }) {
1314
const files = fileURLToPath(new URL('./files', import.meta.url));
1415
const target_dir = join(process.cwd(), '.svelte-kit', 'cloudflare');

packages/adapter-cloudflare/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
".": {
1313
"import": "./index.js"
1414
},
15+
"./entry": {
16+
"import": "./files/worker.js"
17+
},
1518
"./package.json": "./package.json"
1619
},
1720
"types": "index.d.ts",

packages/adapter-netlify/README.md

-32
Original file line numberDiff line numberDiff line change
@@ -66,38 +66,6 @@ During compilation a required "catch all" redirect rule is automatically appende
6666
node_bundler = "esbuild"
6767
```
6868

69-
## Advanced Configuration
70-
71-
### esbuild
72-
73-
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.
74-
75-
For example, you may wish to add a plugin:
76-
77-
```js
78-
adapterNetlify({
79-
esbuild(defaultOptions) {
80-
return {
81-
...defaultOptions,
82-
plugins: []
83-
};
84-
}
85-
});
86-
```
87-
88-
The default options for this version are as follows:
89-
90-
```js
91-
{
92-
entryPoints: ['.svelte-kit/netlify/entry.js'],
93-
// This is Netlify's internal functions directory, not the one for user functions.
94-
outfile: '.netlify/functions-internal/__render.js',
95-
bundle: true,
96-
inject: ['pathTo/shims.js'],
97-
platform: 'node'
98-
}
99-
```
100-
10169
## Changelog
10270

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

packages/adapter-netlify/files/entry.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// TODO hardcoding the relative location makes this brittle
2-
import { init, render } from '../output/server/app.js';
1+
import '@sveltejs/kit/install-fetch';
2+
// $server-build doesn't exist until the app is built
3+
// @ts-expect-error
4+
import { init, render } from '$server-build';
35

46
init();
57

packages/adapter-netlify/files/shims.js

-1
This file was deleted.

packages/adapter-netlify/index.js

+5-29
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import { appendFileSync, existsSync, readFileSync, writeFileSync } from 'fs';
1+
import { appendFileSync, existsSync, readFileSync, renameSync } from 'fs';
22
import { join, resolve } from 'path';
3-
import { fileURLToPath } from 'url';
4-
import esbuild from 'esbuild';
53
import toml from '@iarna/toml';
64

7-
/**
8-
* @typedef {import('esbuild').BuildOptions} BuildOptions
9-
*/
10-
115
/** @type {import('.')} */
12-
export default function (options) {
6+
export default function () {
137
return {
148
name: '@sveltejs/adapter-netlify',
9+
serverEntryPoint: '@sveltejs/adapter-netlify/entry',
1510

1611
async adapt({ utils }) {
1712
// "build" is the default publish directory when Netlify detects SvelteKit
@@ -21,28 +16,9 @@ export default function (options) {
2116

2217
utils.rimraf(publish);
2318

24-
const files = fileURLToPath(new URL('./files', import.meta.url));
25-
2619
utils.log.minor('Generating serverless function...');
27-
utils.copy(join(files, 'entry.js'), '.svelte-kit/netlify/entry.js');
28-
29-
/** @type {BuildOptions} */
30-
const default_options = {
31-
entryPoints: ['.svelte-kit/netlify/entry.js'],
32-
// Any functions in ".netlify/functions-internal" are bundled in addition to user-defined Netlify functions.
33-
// See https://github.com./netlify/build/pull/3213 for more details
34-
outfile: '.netlify/functions-internal/__render.js',
35-
bundle: true,
36-
inject: [join(files, 'shims.js')],
37-
platform: 'node'
38-
};
39-
40-
const build_options =
41-
options && options.esbuild ? await options.esbuild(default_options) : default_options;
42-
43-
await esbuild.build(build_options);
44-
45-
writeFileSync(join('.netlify', 'package.json'), JSON.stringify({ type: 'commonjs' }));
20+
utils.copy('.svelte-kit/output/server/index.js', '.netlify/functions-internal/__render.js');
21+
utils.copy('.svelte-kit/output/server/chunks', '.netlify/functions-internal/chunks');
4622

4723
utils.log.minor('Prerendering static pages...');
4824
await utils.prerender({

packages/adapter-netlify/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
".": {
1313
"import": "./index.js"
1414
},
15+
"./entry": {
16+
"import": "./files/entry.js"
17+
},
1518
"./package.json": "./package.json"
1619
},
1720
"main": "index.js",
@@ -26,8 +29,7 @@
2629
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
2730
},
2831
"dependencies": {
29-
"@iarna/toml": "^2.2.5",
30-
"esbuild": "^0.13.4"
32+
"@iarna/toml": "^2.2.5"
3133
},
3234
"devDependencies": {
3335
"@sveltejs/kit": "workspace:*"

packages/adapter-node/README.md

+7-50
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,13 @@ export default {
1515
adapter: adapter({
1616
// default options are shown
1717
out: 'build',
18-
precompress: false,
19-
env: {
20-
host: 'HOST',
21-
port: 'PORT'
22-
}
18+
precompress: false
2319
})
2420
}
2521
};
2622
```
2723

28-
## Options
29-
30-
### entryPoint
31-
32-
The server entry point. Allows you to provide a [custom server implementation](#middleware). Defaults to the provided reference server.
24+
## Build Options
3325

3426
### out
3527

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

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

42-
### env
34+
## Runtime Options
35+
36+
### Environment variables
4337

4438
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:
4539

4640
```
4741
HOST=127.0.0.1 PORT=4000 node build
4842
```
4943

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

5246
## Middleware
5347

5448
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.
5549

56-
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.
50+
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.
5751

5852
```js
5953
// src/server.js
@@ -83,43 +77,6 @@ app.listen(3000);
8377

8478
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).
8579

86-
## Advanced Configuration
87-
88-
### esbuild
89-
90-
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.
91-
92-
For example, you may wish to add a plugin:
93-
94-
```js
95-
adapterNode({
96-
esbuild(defaultOptions) {
97-
return {
98-
...defaultOptions,
99-
plugins: []
100-
};
101-
}
102-
});
103-
```
104-
105-
The default options for this version are as follows:
106-
107-
```js
108-
{
109-
entryPoints: ['.svelte-kit/node/index.js'],
110-
outfile: 'pathTo/index.js',
111-
bundle: true,
112-
external: allProductionDependencies, // from package.json
113-
format: 'esm',
114-
platform: 'node',
115-
target: 'node14',
116-
inject: ['pathTo/shims.js'],
117-
define: {
118-
esbuild_app_dir: `"${config.kit.appDir}"`
119-
}
120-
}
121-
```
122-
12380
## Deploying
12481

12582
You will need the output directory (`build` by default), the project's `package.json`, and the production dependencies in `node_modules` to run the application. Production dependencies can be generated with `npm ci --prod`, you can also skip this step if your app doesn't have any dependencies. You can then start your app with

0 commit comments

Comments
 (0)