Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'disposeContext' option in Configuration and EsbuildFunctionDefinitionHandler #515

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading