Skip to content

Commit c6075d7

Browse files
committed
feat(adpater-node): support all esbuild build options
This PR allows to change the options passed to esbuild build which allows to customize the server generation like target node version, sourcemap, inject and more. It contains one braking change: `out` is renamed to `outdir` to follow the esbuild API. It is possible to avoid a breaking change by making `out` an alias for `outdir`. I'm happy to adjust the PR.
1 parent 318cdd7 commit c6075d7

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

.changeset/stale-books-flash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-node': patch
3+
---
4+
5+
support all esbuild build options

packages/adapter-node/README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,24 @@ import adapter from '@sveltejs/adapter-node';
1313
export default {
1414
kit: {
1515
adapter: adapter({
16-
// default options are shown
17-
out: 'build'
16+
// default options are shown below
17+
outdir: 'build',
18+
outfile: join(outdir, 'index.js')
19+
bundle: true,
20+
format: 'esm',
21+
platform: 'node',
22+
target: 'node12',
23+
external: Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
1824
})
1925
}
2026
};
2127
```
2228

2329
## Options
2430

25-
### out
31+
All [esbuild build](https://esbuild.github.io/api/#build-api) options except for `entryPoints` are supported.
2632

27-
The directory to build the server to. It defaults to `build` — i.e. `node build` would start the server locally after it has been created.
33+
The [outdir](https://esbuild.github.io/api/#outdir) is the directory to build the server to. It defaults to `build` — i.e. `node build` would start the server locally after it has been created.
2834

2935
## Environment variables
3036

packages/adapter-node/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
declare function plugin(options?: { out?: string }): import('@sveltejs/kit').Adapter;
1+
declare function plugin(options?: import('esbuild').BuildOptions): import('@sveltejs/kit').Adapter;
22

33
export = plugin;

packages/adapter-node/index.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,50 @@ import { join } from 'path';
44
import { fileURLToPath } from 'url';
55

66
/**
7-
* @param {{
8-
* out?: string;
9-
* }} options
7+
* @param {import('esbuild').BuildOptions} options
108
*/
11-
export default function ({ out = 'build' } = {}) {
9+
export default function ({
10+
outdir = 'build',
11+
outfile = join(outdir, 'index.js'),
12+
bundle = true,
13+
format = 'esm',
14+
platform = 'node',
15+
target = 'node12',
16+
external = Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
17+
...esbuildOptions
18+
} = {}) {
1219
/** @type {import('@sveltejs/kit').Adapter} */
1320
const adapter = {
1421
name: '@sveltejs/adapter-node',
1522

1623
async adapt({ utils, config }) {
1724
utils.log.minor('Copying assets');
18-
const static_directory = join(out, 'assets');
25+
const static_directory = join(outdir, 'assets');
1926
utils.copy_client_files(static_directory);
2027
utils.copy_static_files(static_directory);
2128

2229
utils.log.minor('Building server');
2330
const files = fileURLToPath(new URL('./files', import.meta.url));
2431
utils.copy(files, '.svelte-kit/node');
2532
await esbuild.build({
33+
...esbuildOptions,
34+
outdir,
35+
outfile,
36+
bundle,
37+
format,
38+
platform,
39+
target,
40+
external,
2641
entryPoints: ['.svelte-kit/node/index.js'],
27-
outfile: join(out, 'index.js'),
28-
bundle: true,
29-
external: Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
30-
format: 'esm',
31-
platform: 'node',
32-
target: 'node12',
3342
define: {
43+
...esbuildOptions.define,
3444
esbuild_app_dir: '"' + config.kit.appDir + '"'
3545
}
3646
});
3747

3848
utils.log.minor('Prerendering static pages');
3949
await utils.prerender({
40-
dest: `${out}/prerendered`
50+
dest: `${outdir}/prerendered`
4151
});
4252
}
4353
};

0 commit comments

Comments
 (0)