From 70ec69337188f15c878b7aa72338830e278bfb6f Mon Sep 17 00:00:00 2001 From: David Tanner Date: Fri, 5 May 2023 10:11:58 -0600 Subject: [PATCH] feat: Merge the provided externals list with any we want to add here --- src/bundle.test.ts | 7 +++---- src/bundle.ts | 38 ++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/bundle.test.ts b/src/bundle.test.ts index f5daf59..340f18b 100644 --- a/src/bundle.test.ts +++ b/src/bundle.test.ts @@ -258,7 +258,7 @@ describe('AWS SDK bundling behavior', () => { await bundle({ entries: `${TEST_INPUT_DIR}/first-file.js`, outdir: TEST_OUTPUT_DIR, - node: 15, + node: 16, includeAwsSdk: true, }); @@ -268,7 +268,7 @@ describe('AWS SDK bundling behavior', () => { bundle: true, sourcemap: false, platform: 'node', - target: 'node15', + target: 'node16', external: [], resolveExtensions: ['.jsx', '.js', '.tsx', '.ts', '.json'], }), @@ -309,9 +309,8 @@ describe('AWS SDK bundling behavior', () => { * * We'll use these assumptions to make assertions below. */ - // Node 12, 14, 16 behavior - [12, 14, 16].forEach((node) => { + describe.each([12, 14, 16])('Node %#', (node) => { test(`does not bundle aws-sdk in node version ${node}`, async () => { const output = await bundleCode({ node, diff --git a/src/bundle.ts b/src/bundle.ts index 5e7f1c1..dfac6fb 100644 --- a/src/bundle.ts +++ b/src/bundle.ts @@ -6,7 +6,7 @@ import { promises as fs } from 'fs'; export type BundleOptions = { /** - * The entrypoint (or entrypoints) to bundle. This can be a glob pattern matching + * The entrypoint/s to bundle. This can be a glob pattern matching * multiple source files (or a list of glob patterns). */ entries: string | string[]; @@ -38,18 +38,37 @@ export type BundleOptions = { esbuild?: ESBuildOptions; }; +const addIfMissing = (arr: string[], str: string) => { + if (!arr.includes(str)) { + arr.push(str); + } +}; + export const bundle = async ({ entries, outdir, node: nodeVersion, cwd, includeAwsSdk = false, - esbuild, + esbuild: { external = [], ...esbuild } = {}, }: BundleOptions) => { const entryPoints = (typeof entries === 'string' ? [entries] : entries) .map((pattern) => glob.sync(pattern, cwd ? { cwd } : undefined)) .flat(); + if (!includeAwsSdk) { + /** + * Don't bundle the AWS SDK, since it is natively available + * in the Lambda environment. + * + * Node runtimes < 18 include the v2 sdk, while runtimes >= 18 include + * the v3 SDK. + * + * https://aws.amazon.com/blogs/compute/node-js-18-x-runtime-now-available-in-aws-lambda/ + */ + addIfMissing(external, nodeVersion >= 18 ? '@aws-sdk/*' : 'aws-sdk'); + } + const buildResult = await withTiming(() => build({ bundle: true, @@ -58,20 +77,7 @@ export const bundle = async ({ target: `node${nodeVersion}`, outdir, entryPoints, - /** - * Don't bundle the AWS SDK, since it is natively available - * in the Lambda environment. - * - * Node runtimes < 18 include the v2 sdk, while runtimes >= 18 include - * the v3 SDK. - * - * https://aws.amazon.com/blogs/compute/node-js-18-x-runtime-now-available-in-aws-lambda/ - */ - external: includeAwsSdk - ? [] - : nodeVersion >= 18 - ? ['@aws-sdk/*'] - : ['aws-sdk'], + external, /** * As of v0.14.44, esbuild by default prefers .ts over .js files. *