From 3ee4efe2e5dc8e9829503f796d01c1a0ab0902c0 Mon Sep 17 00:00:00 2001 From: Sascha Tandel Date: Wed, 16 Jun 2021 09:19:57 +0200 Subject: [PATCH] fix: use `esbuildOptions` parameter --- packages/adapter-node/README.md | 37 ++++++++++++++++++++++++-------- packages/adapter-node/index.d.ts | 6 +++++- packages/adapter-node/index.js | 34 ++++++++++++++++++----------- 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/packages/adapter-node/README.md b/packages/adapter-node/README.md index 39d7c6a37029..3fe2e26f23ab 100644 --- a/packages/adapter-node/README.md +++ b/packages/adapter-node/README.md @@ -14,14 +14,18 @@ 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 */ + ] + } }) } }; @@ -29,14 +33,29 @@ export default { ## 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: diff --git a/packages/adapter-node/index.d.ts b/packages/adapter-node/index.d.ts index 4eac1959e728..aaa150f5ff2a 100644 --- a/packages/adapter-node/index.d.ts +++ b/packages/adapter-node/index.d.ts @@ -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; diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index 5090f8f03df0..d62a8674ab8c 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -1,3 +1,4 @@ +import { readFileSync, statSync, createReadStream, createWriteStream } from 'fs'; import { join } from 'path'; import { fileURLToPath } from 'url'; import { pipeline } from 'stream'; @@ -9,17 +10,26 @@ 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 = { @@ -27,7 +37,7 @@ export default function ({ 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); @@ -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 + '"' @@ -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');