Skip to content

Commit

Permalink
Merge pull request #7 from lifeomic/allow-overriding-external
Browse files Browse the repository at this point in the history
feat: Allow opting out from excluding the aws sdk
  • Loading branch information
jkdowdle authored Apr 17, 2023
2 parents 08fa7e6 + 8013e9f commit 5de8c83
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
preset: 'ts-jest',
clearMocks: true,
collectCoverage: true,
coverageThreshold: {
global: {
Expand Down
10 changes: 9 additions & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import * as yargs from 'yargs';
import { bundle } from '../bundle';

const main = async () => {
const { entries, outdir, node } = await yargs(process.argv.slice(2))
const { entries, outdir, node, includeAwsSdk } = await yargs(
process.argv.slice(2),
)
.option('entries', {
type: 'string',
description: 'The lambda entrypoints to bundle. Can be a glob pattern.',
Expand All @@ -20,6 +22,11 @@ const main = async () => {
description: 'The Node version to target.',
demandOption: true,
})
.option('include-aws-sdk', {
type: 'boolean',
description: 'Allow opting out from excluding the aws sdk',
default: false,
})
.strict()
.parse();

Expand All @@ -28,6 +35,7 @@ const main = async () => {
outdir: path.resolve(process.cwd(), outdir),
node,
cwd: process.cwd(),
includeAwsSdk,
});
};

Expand Down
22 changes: 22 additions & 0 deletions src/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,28 @@ test('bundle works with an array of entries', async () => {
});

describe('AWS SDK bundling behavior', () => {
test('allows for ignoring AWS SDK exclusion behavior', async () => {
writeTestInputFile('first-file.js', `export const TMP = 'TMP';`);
await bundle({
entries: `${TEST_INPUT_DIR}/first-file.js`,
outdir: TEST_OUTPUT_DIR,
node: 15,
includeAwsSdk: true,
});

expect(build).toHaveBeenCalledTimes(1);
expect(build).toHaveBeenCalledWith(
expect.objectContaining({
bundle: true,
sourcemap: false,
platform: 'node',
target: 'node15',
external: [],
resolveExtensions: ['.jsx', '.js', '.tsx', '.ts', '.json'],
}),
);
});

/** Bundles the code, returning the output. */
const bundleCode = async (params: {
node: number;
Expand Down
12 changes: 11 additions & 1 deletion src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export type BundleOptions = {

cwd?: string;

/**
* Allows for opting out from excluding the aws sdk
*/
includeAwsSdk?: boolean;

/**
* Override options to pass to esbuild. These override any options generated
* by other internal settings.
Expand All @@ -38,6 +43,7 @@ export const bundle = async ({
outdir,
node: nodeVersion,
cwd,
includeAwsSdk = false,
esbuild,
}: BundleOptions) => {
const entryPoints = (typeof entries === 'string' ? [entries] : entries)
Expand All @@ -61,7 +67,11 @@ export const bundle = async ({
*
* https://aws.amazon.com/blogs/compute/node-js-18-x-runtime-now-available-in-aws-lambda/
*/
external: nodeVersion >= 18 ? ['@aws-sdk/*'] : ['aws-sdk'],
external: includeAwsSdk
? []
: nodeVersion >= 18
? ['@aws-sdk/*']
: ['aws-sdk'],
/**
* As of v0.14.44, esbuild by default prefers .ts over .js files.
*
Expand Down

0 comments on commit 5de8c83

Please sign in to comment.