Skip to content

Commit

Permalink
fix: Implement 'disposeContext' option in Configuration and EsbuildFu…
Browse files Browse the repository at this point in the history
…nctionDefinitionHandler (#515)

* fix(buildOptions): add disposeContext option to improve handling

* fix(buildOptions): disposeContext removed from the list on bundle.ts
fix(buildOptions): Override disposeContext if a function also has it's own configuration for it.

* Update README.md

---------

Co-authored-by: Ogeday Oztoprak <[email protected]>
  • Loading branch information
0gebey and ogedayoztoprak committed Dec 18, 2023
1 parent d32199f commit fb41719
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function bundle(this: EsbuildServerlessPlugin): Promise<void> {
'skipBuild',
'skipBuildExcludeFns',
'stripEntryResolveExtensions',
'disposeContext',
].reduce<Record<string, any>>((options, optionName) => {
const { [optionName]: _, ...rest } = options;

Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -321,6 +322,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
skipBuild: false,
skipBuildExcludeFns: [],
stripEntryResolveExtensions: false,
disposeContext: true, // default true
};

const providerRuntime = this.serverless.service.provider.runtime;
Expand Down Expand Up @@ -526,7 +528,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
async disposeContexts(): Promise<void> {
for (const { context } of Object.values(this.buildCache)) {
if (context) {
await context.dispose();
this.buildOptions?.disposeContext && (await context.dispose());
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit fb41719

Please sign in to comment.