diff --git a/README.md b/README.md index 04125d0..35d3d7e 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ See [example folder](examples) for some example configurations. | `skipBuild` | Avoid rebuilding lambda artifacts in favor of reusing previous build artifacts. | `false` | | `skipBuildExcludeFns` | An array of lambda names that will always be rebuilt if `skipBuild` is set to `true` and bundling individually. This is helpful for dynamically generated functions like serverless-plugin-warmup. | `[]` | | `stripEntryResolveExtensions` | A boolean that determines if entrypoints using custom file extensions provided in the `resolveExtensions` ESbuild setting should be stripped of their custom extension upon packing the final bundle for that file. Example: `myLambda.custom.ts` would result in `myLambda.js` instead of `myLambda.custom.js`. - +| `disposeContext` | An option to disable the disposal of the context.(Functions can override the global `disposeContext` configuration by specifying their own `disposeContext` option in their individual configurations.) | `true` #### Default Esbuild Options The following `esbuild` options are automatically set. diff --git a/src/bundle.ts b/src/bundle.ts index 8b0cf67..41373e9 100644 --- a/src/bundle.ts +++ b/src/bundle.ts @@ -40,6 +40,7 @@ export async function bundle(this: EsbuildServerlessPlugin): Promise { 'skipBuild', 'skipBuildExcludeFns', 'stripEntryResolveExtensions', + 'disposeContext', ].reduce>((options, optionName) => { const { [optionName]: _, ...rest } = options; diff --git a/src/index.ts b/src/index.ts index e4e3c35..ff26361 100644 --- a/src/index.ts +++ b/src/index.ts @@ -234,6 +234,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin { for (const [functionAlias, fn] of Object.entries(functions)) { const currFn = fn as EsbuildFunctionDefinitionHandler; if (this.isFunctionDefinitionHandler(currFn) && this.isNodeFunction(currFn)) { + buildOptions.disposeContext = currFn.disposeContext ? currFn.disposeContext : buildOptions.disposeContext; // disposeContext configuration can be overridden per function if (buildOptions.skipBuild && !buildOptions.skipBuildExcludeFns?.includes(functionAlias)) { currFn.skipEsbuild = true; } @@ -321,6 +322,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin { skipBuild: false, skipBuildExcludeFns: [], stripEntryResolveExtensions: false, + disposeContext: true, // default true }; const providerRuntime = this.serverless.service.provider.runtime; @@ -526,7 +528,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin { async disposeContexts(): Promise { for (const { context } of Object.values(this.buildCache)) { if (context) { - await context.dispose(); + this.buildOptions?.disposeContext && (await context.dispose()); } } } diff --git a/src/types.ts b/src/types.ts index 6b5bf3f..f3c8115 100644 --- a/src/types.ts +++ b/src/types.ts @@ -48,9 +48,11 @@ export interface Configuration extends EsbuildOptions { skipBuild?: boolean; skipBuildExcludeFns: string[]; stripEntryResolveExtensions?: boolean; + disposeContext?: boolean; } export interface EsbuildFunctionDefinitionHandler extends Serverless.FunctionDefinitionHandler { + disposeContext?: boolean; skipEsbuild: boolean; }