Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adapter-node): support all esbuild build options #1692

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stale-books-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': patch
---

support all esbuild build options
29 changes: 27 additions & 2 deletions packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ import adapter from '@sveltejs/adapter-node';
export default {
kit: {
adapter: adapter({
// default options are shown
// default options are shown below
out: 'build',
precompress: false
precompress: false,
esbuildOptions: {
outdir: out,
bundle: true,
format: 'esm',
platform: 'node',
target: 'node12',
external: [
/* package.json#dependencies */
]
}
})
}
};
Expand All @@ -31,6 +41,21 @@ 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`.

### esbuildOptions

Any custom [esbuild build](https://esbuild.github.io/api/#build-api) options. It defaults to:

```js
{
outdir: out /* = 'build' */, // Unless a outfile is specified
bundle: true,
format: 'esm',
platform: 'node',
target: 'node12',
external: [ /* package.json#dependencies */ ]
}
```

## 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:
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare function plugin(options?: {
out?: string;
precompress?: boolean;
esbuildOptions?: import('esbuild').BuildOptions;
}): import('@sveltejs/kit').Adapter;

export = plugin;
38 changes: 28 additions & 10 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,26 @@ const pipe = promisify(pipeline);

/**
* @param {{
* out?: string;
* precompress?: boolean
* out?: string,
* precompress?: boolean,
* esbuildOptions?: import('esbuild').BuildOptions
* }} options
*/
export default function ({ out = 'build', precompress } = {}) {
export default function ({
out = 'build',
precompress,
esbuildOptions: {
outfile,
outdir = typeof outfile === 'undefined' ? out : undefined,
bundle = true,
format = 'esm',
platform = 'node',
target = 'node12',
external = Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
entryPoints = ['.svelte-kit/node/index.js'],
...esbuildOptions
} = {}
} = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-node',
Expand All @@ -35,14 +50,17 @@ export default function ({ out = 'build', precompress } = {}) {
const files = fileURLToPath(new URL('./files', import.meta.url));
utils.copy(files, '.svelte-kit/node');
await esbuild.build({
entryPoints: ['.svelte-kit/node/index.js'],
outfile: join(out, 'index.js'),
bundle: true,
external: Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
format: 'esm',
platform: 'node',
target: 'node12',
...esbuildOptions,
outdir,
outfile,
bundle,
format,
platform,
target,
external,
entryPoints,
define: {
...esbuildOptions.define,
esbuild_app_dir: '"' + config.kit.appDir + '"'
}
});
Expand Down