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

fix: include esbuild config in adapter type definition #1954

Merged
merged 2 commits into from
Jul 19, 2021
Merged
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
8 changes: 8 additions & 0 deletions .changeset/tasty-cars-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-vercel': patch
---

fix: include esbuild config in adapter type definition
5 changes: 4 additions & 1 deletion packages/adapter-cloudflare-workers/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
declare function plugin(): import('@sveltejs/kit').Adapter;
type BuildOptions = import('esbuild').BuildOptions;
declare function plugin(options?: {
esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}): import('@sveltejs/kit').Adapter;

export = plugin;
14 changes: 9 additions & 5 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { fileURLToPath } from 'url';
/**
* @param {{
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
* }} options
* }} [options]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an intentional change? Why only on some adapters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • adapter-cloudflare-workers, adapter-vercel, adapter-netlify, and adapter-node all use esbuild, so this option should be available but not made mandatory.
  • adapter-begin is unmaintained
  • adapter-static doesn't use esbuild, so it wasn't affected.

If you're referring to the specific line, that's JSDoc's way of specifying an optional parameter so that you can do something like adapter: adapterCfw() without having to pass in an empty object.

Copy link
Contributor

@jthegedus jthegedus Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're referring to the specific line, that's JSDoc's way of specifying an optional parameter so that you can do something like adapter: adapterCfw() without having to pass in an empty object.

I was referring to the specific line, thanks for clarifying. I need to learn the JSDoc spec better.

**/
export default function (options = { esbuild: (opts) => opts }) {
export default function (options) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-cloudflare-workers',
Expand All @@ -38,13 +38,17 @@ export default function (options = { esbuild: (opts) => opts }) {
utils.log.minor('Generating worker...');
utils.copy(`${files}/entry.js`, '.svelte-kit/cloudflare-workers/entry.js');

const buildOptions = await options.esbuild({
/** @type {BuildOptions} */
const defaultOptions = {
entryPoints: ['.svelte-kit/cloudflare-workers/entry.js'],
outfile: `${entrypoint}/index.js`,
bundle: true,
target: 'es2020',
platform: 'browser' // TODO would be great if we could generate ESM and use type = "javascript"
});
platform: 'browser'
};

const buildOptions =
options && options.esbuild ? await options.esbuild(defaultOptions) : defaultOptions;

await esbuild.build(buildOptions);

Expand Down
5 changes: 4 additions & 1 deletion packages/adapter-netlify/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
declare function plugin(): import('@sveltejs/kit').Adapter;
type BuildOptions = import('esbuild').BuildOptions;
declare function plugin(options?: {
esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}): import('@sveltejs/kit').Adapter;

export = plugin;
12 changes: 8 additions & 4 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import toml from '@iarna/toml';
/**
* @param {{
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
* }} options
* }} [options]
**/
export default function (options = { esbuild: (opts) => opts }) {
export default function (options) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-netlify',
Expand All @@ -29,13 +29,17 @@ export default function (options = { esbuild: (opts) => opts }) {
utils.log.minor('Generating serverless function...');
utils.copy(join(files, 'entry.js'), '.svelte-kit/netlify/entry.js');

const buildOptions = await options.esbuild({
/** @type {BuildOptions} */
const defaultOptions = {
entryPoints: ['.svelte-kit/netlify/entry.js'],
outfile: join(functions, 'render/index.js'),
bundle: true,
inject: [join(files, 'shims.js')],
platform: 'node'
});
};

const buildOptions =
options && options.esbuild ? await options.esbuild(defaultOptions) : defaultOptions;

await esbuild.build(buildOptions);

Expand Down
2 changes: 2 additions & 0 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
type BuildOptions = import('esbuild').BuildOptions;
declare function plugin(options?: {
out?: string;
precompress?: boolean;
env?: {
host?: string;
port?: string;
};
esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}): import('@sveltejs/kit').Adapter;

export = plugin;
20 changes: 10 additions & 10 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ const pipe = promisify(pipeline);
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
* }} options
*/
export default function (
{
out = 'build',
precompress,
env: { host: host_env = 'HOST', port: port_env = 'PORT' } = {},
esbuild: esbuildConfig
} = { esbuild: (opts) => opts }
) {
export default function ({
out = 'build',
precompress,
env: { host: host_env = 'HOST', port: port_env = 'PORT' } = {},
esbuild: esbuildConfig
} = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-node',
Expand All @@ -63,7 +61,8 @@ export default function (
host_env
)}] || '0.0.0.0';\nexport const port = process.env[${JSON.stringify(port_env)}] || 3000;`
);
const buildOptions = await esbuildConfig({
/** @type {BuildOptions} */
const defaultOptions = {
entryPoints: ['.svelte-kit/node/index.js'],
outfile: join(out, 'index.js'),
bundle: true,
Expand All @@ -75,7 +74,8 @@ export default function (
define: {
esbuild_app_dir: '"' + config.kit.appDir + '"'
}
});
};
const buildOptions = esbuildConfig ? await esbuildConfig(defaultOptions) : defaultOptions;
await esbuild.build(buildOptions);

utils.log.minor('Prerendering static pages');
Expand Down
5 changes: 4 additions & 1 deletion packages/adapter-vercel/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
declare function plugin(): import('@sveltejs/kit').Adapter;
type BuildOptions = import('esbuild').BuildOptions;
declare function plugin(options?: {
esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}): import('@sveltejs/kit').Adapter;

export = plugin;
12 changes: 8 additions & 4 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import esbuild from 'esbuild';
/**
* @param {{
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
* }} options
* }} [options]
**/
export default function (options = { esbuild: (opts) => opts }) {
export default function (options) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-vercel',
Expand All @@ -36,13 +36,17 @@ export default function (options = { esbuild: (opts) => opts }) {
utils.log.minor('Generating serverless function...');
utils.copy(join(files, 'entry.js'), '.svelte-kit/vercel/entry.js');

const buildOptions = await options.esbuild({
/** @type {BuildOptions} */
const defaultOptions = {
entryPoints: ['.svelte-kit/vercel/entry.js'],
outfile: join(dirs.lambda, 'index.js'),
bundle: true,
inject: [join(files, 'shims.js')],
platform: 'node'
});
};

const buildOptions =
options && options.esbuild ? await options.esbuild(defaultOptions) : defaultOptions;

await esbuild.build(buildOptions);

Expand Down