Skip to content

Commit e699579

Browse files
authored
feat(adapters): expose esbuild configuration (#1914)
1 parent f07baa7 commit e699579

File tree

9 files changed

+194
-14
lines changed

9 files changed

+194
-14
lines changed

Diff for: .changeset/dull-numbers-hunt.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@sveltejs/adapter-cloudflare-workers': patch
3+
'@sveltejs/adapter-netlify': patch
4+
'@sveltejs/adapter-node': patch
5+
'@sveltejs/adapter-vercel': patch
6+
---
7+
8+
feat(adapters): expose esbuild configuration

Diff for: packages/adapter-cloudflare-workers/README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ SvelteKit adapter that creates a Cloudflare Workers site using a function for dy
44

55
This is very experimental; the adapter API isn't at all fleshed out, and things will definitely change.
66

7-
## Configuration
7+
## Basic Configuration
88

99
This adapter expects to find a [wrangler.toml](https://developers.cloudflare.com/workers/platform/sites/configuration) file in the project root. It will determine where to write static assets and the worker based on the `site.bucket` and `site.entry-point` settings.
1010

@@ -26,6 +26,37 @@ It's recommended that you add the `build` and `workers-site` folders (or whichev
2626

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

29+
## Advanced Configuration
30+
31+
### esbuild
32+
33+
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.
34+
35+
For example, you may wish to add a plugin:
36+
37+
```js
38+
adapterCfw({
39+
esbuild(defaultOptions) {
40+
return {
41+
...defaultOptions,
42+
plugins: []
43+
};
44+
}
45+
});
46+
```
47+
48+
The default options for this version are as follows:
49+
50+
```js
51+
{
52+
entryPoints: ['.svelte-kit/cloudflare-workers/entry.js'],
53+
outfile: `${entrypoint}/index.js`,
54+
bundle: true,
55+
target: 'es2020',
56+
platform: 'browser'
57+
}
58+
```
59+
2960
## Changelog
3061

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

Diff for: packages/adapter-cloudflare-workers/index.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ import esbuild from 'esbuild';
44
import toml from '@iarna/toml';
55
import { fileURLToPath } from 'url';
66

7-
export default function () {
7+
/**
8+
* @typedef {import('esbuild').BuildOptions} BuildOptions
9+
*/
10+
11+
/**
12+
* @param {{
13+
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
14+
* }} options
15+
**/
16+
export default function (options = { esbuild: (opts) => opts }) {
817
/** @type {import('@sveltejs/kit').Adapter} */
918
const adapter = {
1019
name: '@sveltejs/adapter-cloudflare-workers',
@@ -29,14 +38,16 @@ export default function () {
2938
utils.log.minor('Generating worker...');
3039
utils.copy(`${files}/entry.js`, '.svelte-kit/cloudflare-workers/entry.js');
3140

32-
await esbuild.build({
41+
const buildOptions = await options.esbuild({
3342
entryPoints: ['.svelte-kit/cloudflare-workers/entry.js'],
3443
outfile: `${entrypoint}/index.js`,
3544
bundle: true,
3645
target: 'es2020',
37-
platform: 'node' // TODO would be great if we could generate ESM and use type = "javascript"
46+
platform: 'browser' // TODO would be great if we could generate ESM and use type = "javascript"
3847
});
3948

49+
await esbuild.build(buildOptions);
50+
4051
fs.writeFileSync(`${entrypoint}/package.json`, JSON.stringify({ main: 'index.js' }));
4152

4253
utils.log.info('Prerendering static pages...');

Diff for: packages/adapter-netlify/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,37 @@ During compilation a required "catch all" redirect rule is automatically appende
5353
2. Netlify's build bot parses your HTML files at deploy time, which means your form must be [prerendered](https://kit.svelte.dev/docs#ssr-and-javascript-prerender) as HTML. You can either add `export const prerender = true` to your `contact.svelte` to prerender just that page or set the `kit.prerender.force: true` option to prerender all pages.
5454
3. If your Netlify form has a [custom success message](https://docs.netlify.com/forms/setup/#success-messages) like `<form netlify ... action="/success">` then ensure the corresponding `/routes/success.svelte` exists and is prerendered.
5555

56+
## Advanced Configuration
57+
58+
### esbuild
59+
60+
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.
61+
62+
For example, you may wish to add a plugin:
63+
64+
```js
65+
adapterNetlify({
66+
esbuild(defaultOptions) {
67+
return {
68+
...defaultOptions,
69+
plugins: []
70+
};
71+
}
72+
});
73+
```
74+
75+
The default options for this version are as follows:
76+
77+
```js
78+
{
79+
entryPoints: ['.svelte-kit/netlify/entry.js'],
80+
outfile: `pathToFunctionsFolder/render/index.js`,
81+
bundle: true,
82+
inject: ['pathTo/shims.js'],
83+
platform: 'node'
84+
}
85+
```
86+
5687
## Changelog
5788

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

Diff for: packages/adapter-netlify/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ import { fileURLToPath } from 'url';
44
import esbuild from 'esbuild';
55
import toml from '@iarna/toml';
66

7-
export default function () {
7+
/**
8+
* @typedef {import('esbuild').BuildOptions} BuildOptions
9+
*/
10+
11+
/**
12+
* @param {{
13+
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
14+
* }} options
15+
**/
16+
export default function (options = { esbuild: (opts) => opts }) {
817
/** @type {import('@sveltejs/kit').Adapter} */
918
const adapter = {
1019
name: '@sveltejs/adapter-netlify',
@@ -20,14 +29,16 @@ export default function () {
2029
utils.log.minor('Generating serverless function...');
2130
utils.copy(join(files, 'entry.js'), '.svelte-kit/netlify/entry.js');
2231

23-
await esbuild.build({
32+
const buildOptions = await options.esbuild({
2433
entryPoints: ['.svelte-kit/netlify/entry.js'],
2534
outfile: join(functions, 'render/index.js'),
2635
bundle: true,
2736
inject: [join(files, 'shims.js')],
2837
platform: 'node'
2938
});
3039

40+
await esbuild.build(buildOptions);
41+
3142
writeFileSync(join(functions, 'package.json'), JSON.stringify({ type: 'commonjs' }));
3243

3344
utils.log.info('Prerendering static pages...');

Diff for: packages/adapter-node/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,43 @@ HOST=127.0.0.1 PORT=4000 node build
4545

4646
You can specify different environment variables if necessary using the `env` option.
4747

48+
## Advanced Configuration
49+
50+
### esbuild
51+
52+
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.
53+
54+
For example, you may wish to add a plugin:
55+
56+
```js
57+
adapterNode({
58+
esbuild(defaultOptions) {
59+
return {
60+
...defaultOptions,
61+
plugins: []
62+
};
63+
}
64+
});
65+
```
66+
67+
The default options for this version are as follows:
68+
69+
```js
70+
{
71+
entryPoints: ['.svelte-kit/node/index.js'],
72+
outfile: 'pathTo/index.js',
73+
bundle: true,
74+
external: allProductionDependencies, // from package.json
75+
format: 'esm',
76+
platform: 'node',
77+
target: 'node12',
78+
inject: ['pathTo/shims.js'],
79+
define: {
80+
esbuild_app_dir: `"${config.kit.appDir}"`
81+
}
82+
}
83+
```
84+
4885
## Changelog
4986

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

Diff for: packages/adapter-node/index.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import zlib from 'zlib';
1616

1717
const pipe = promisify(pipeline);
1818

19+
/**
20+
* @typedef {import('esbuild').BuildOptions} BuildOptions
21+
*/
22+
1923
/**
2024
* @param {{
2125
* out?: string;
@@ -24,13 +28,17 @@ const pipe = promisify(pipeline);
2428
* host?: string;
2529
* port?: string;
2630
* };
31+
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
2732
* }} options
2833
*/
29-
export default function ({
30-
out = 'build',
31-
precompress,
32-
env: { host: host_env = 'HOST', port: port_env = 'PORT' } = {}
33-
} = {}) {
34+
export default function (
35+
{
36+
out = 'build',
37+
precompress,
38+
env: { host: host_env = 'HOST', port: port_env = 'PORT' } = {},
39+
esbuild: esbuildConfig
40+
} = { esbuild: (opts) => opts }
41+
) {
3442
/** @type {import('@sveltejs/kit').Adapter} */
3543
const adapter = {
3644
name: '@sveltejs/adapter-node',
@@ -55,7 +63,7 @@ export default function ({
5563
host_env
5664
)}] || '0.0.0.0';\nexport const port = process.env[${JSON.stringify(port_env)}] || 3000;`
5765
);
58-
await esbuild.build({
66+
const buildOptions = await esbuildConfig({
5967
entryPoints: ['.svelte-kit/node/index.js'],
6068
outfile: join(out, 'index.js'),
6169
bundle: true,
@@ -68,6 +76,7 @@ export default function ({
6876
esbuild_app_dir: '"' + config.kit.appDir + '"'
6977
}
7078
});
79+
await esbuild.build(buildOptions);
7180

7281
utils.log.minor('Prerendering static pages');
7382
await utils.prerender({

Diff for: packages/adapter-vercel/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@ export default {
1919
};
2020
```
2121

22+
## Advanced Configuration
23+
24+
### esbuild
25+
26+
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.
27+
28+
For example, you may wish to add a plugin:
29+
30+
```js
31+
adapterVercel({
32+
esbuild(defaultOptions) {
33+
return {
34+
...defaultOptions,
35+
plugins: []
36+
};
37+
}
38+
});
39+
```
40+
41+
The default options for this version are as follows:
42+
43+
```js
44+
{
45+
entryPoints: ['.svelte-kit/vercel/entry.js'],
46+
outfile: `pathToLambdaFolder/index.js`,
47+
bundle: true,
48+
inject: ['pathTo/shims.js'],
49+
platform: 'node'
50+
}
51+
```
52+
2253
## Changelog
2354

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

Diff for: packages/adapter-vercel/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import { join } from 'path';
33
import { fileURLToPath } from 'url';
44
import esbuild from 'esbuild';
55

6-
export default function () {
6+
/**
7+
* @typedef {import('esbuild').BuildOptions} BuildOptions
8+
*/
9+
10+
/**
11+
* @param {{
12+
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
13+
* }} options
14+
**/
15+
export default function (options = { esbuild: (opts) => opts }) {
716
/** @type {import('@sveltejs/kit').Adapter} */
817
const adapter = {
918
name: '@sveltejs/adapter-vercel',
@@ -27,14 +36,16 @@ export default function () {
2736
utils.log.minor('Generating serverless function...');
2837
utils.copy(join(files, 'entry.js'), '.svelte-kit/vercel/entry.js');
2938

30-
await esbuild.build({
39+
const buildOptions = await options.esbuild({
3140
entryPoints: ['.svelte-kit/vercel/entry.js'],
3241
outfile: join(dirs.lambda, 'index.js'),
3342
bundle: true,
3443
inject: [join(files, 'shims.js')],
3544
platform: 'node'
3645
});
3746

47+
await esbuild.build(buildOptions);
48+
3849
writeFileSync(join(dirs.lambda, 'package.json'), JSON.stringify({ type: 'commonjs' }));
3950

4051
utils.log.minor('Prerendering static pages...');

0 commit comments

Comments
 (0)