Skip to content

Commit

Permalink
fix: use esbuildOptions parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
sastan committed Jun 22, 2021
1 parent fba2021 commit 3ee4efe
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
37 changes: 28 additions & 9 deletions packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,48 @@ export default {
kit: {
adapter: adapter({
// default options are shown below
outdir: 'build',
out: 'build',
precompress: false,
outfile: join(outdir, 'index.js')
bundle: true,
format: 'esm',
platform: 'node',
target: 'node12',
external: Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
esbuildOptions: {
outdir: out,
bundle: true,
format: 'esm',
platform: 'node',
target: 'node12',
external: [
/* package.json#dependencies */
]
}
})
}
};
```

## Options

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

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

### precompress

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

export = plugin;
34 changes: 22 additions & 12 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFileSync, statSync, createReadStream, createWriteStream } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';
import { pipeline } from 'stream';
Expand All @@ -9,25 +10,34 @@ import glob from 'tiny-glob';
const pipe = promisify(pipeline);

/**
* @param {{precompress?: boolean} & import('esbuild').BuildOptions} options
* @param {{
* out?: string,
* precompress?: boolean,
* esbuildOptions?: import('esbuild').BuildOptions
* }} options
*/
export default function ({
outdir = 'build',
outfile = join(outdir, 'index.js'),
bundle = true,
format = 'esm',
platform = 'node',
target = 'node12',
external = Object.keys(JSON.parse(readFileSync('package.json', 'utf8')).dependencies || {}),
...esbuildOptions
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',

async adapt({ utils, config }) {
utils.log.minor('Copying assets');
const static_directory = join(outdir, 'assets');
const static_directory = join(out, 'assets');
utils.copy_client_files(static_directory);
utils.copy_static_files(static_directory);

Expand All @@ -48,7 +58,7 @@ export default function ({
platform,
target,
external,
entryPoints: ['.svelte-kit/node/index.js'],
entryPoints,
define: {
...esbuildOptions.define,
esbuild_app_dir: '"' + config.kit.appDir + '"'
Expand All @@ -57,7 +67,7 @@ export default function ({

utils.log.minor('Prerendering static pages');
await utils.prerender({
dest: `${outdir}/prerendered`
dest: `${out}/prerendered`
});
if (precompress) {
utils.log.minor('Compressing prerendered pages');
Expand Down

0 comments on commit 3ee4efe

Please sign in to comment.