Skip to content

Commit

Permalink
Merge pull request #12 from lifeomic/mergeExternals
Browse files Browse the repository at this point in the history
Merge the provided externals list with any we want to add here
  • Loading branch information
DavidTanner authored May 5, 2023
2 parents 1f086dd + 70ec693 commit 78ef7e8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand All @@ -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'],
}),
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 22 additions & 16 deletions src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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,
Expand All @@ -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.
*
Expand Down

0 comments on commit 78ef7e8

Please sign in to comment.