From 6a97354977db600b6b6b23f1db0bbad33f111372 Mon Sep 17 00:00:00 2001 From: JohnGrisham Date: Fri, 1 Dec 2023 10:55:24 -0600 Subject: [PATCH] feat(resolveExtensions): added support for custom resolve extensions esbuild setting --- src/constants.ts | 1 + src/helper.ts | 6 ++++-- src/index.ts | 7 ++++++- src/tests/helper.test.ts | 29 +++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index a1d29242..0e4b6413 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -2,3 +2,4 @@ export const SERVERLESS_FOLDER = '.serverless'; export const BUILD_FOLDER = '.build'; export const WORK_FOLDER = '.esbuild'; export const ONLY_PREFIX = '__only_'; +export const DEFAULT_EXTENSIONS = ['.ts', '.js', '.jsx', '.tsx']; diff --git a/src/helper.ts b/src/helper.ts index af3b66f7..c0fee898 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -11,6 +11,7 @@ import type Serverless from 'serverless'; import type ServerlessPlugin from 'serverless/classes/Plugin'; import type { Configuration, DependencyMap, FunctionEntry } from './types'; import type { EsbuildFunctionDefinitionHandler } from './types'; +import { DEFAULT_EXTENSIONS } from './constants'; export function asArray(data: T | T[]): T[] { return Array.isArray(data) ? data : [data]; @@ -27,7 +28,8 @@ export function assertIsString(input: unknown, message = 'input is not a string' export function extractFunctionEntries( cwd: string, provider: string, - functions: Record + functions: Record, + resolveExtensions?: string[] ): FunctionEntry[] { // The Google provider will use the entrypoint not from the definition of the // handler function, but instead from the package.json:main field, or via a @@ -69,7 +71,7 @@ export function extractFunctionEntries( // replace only last instance to allow the same name for file and handler const fileName = handler.substring(0, fnNameLastAppearanceIndex); - const extensions = ['.ts', '.js', '.jsx', '.tsx']; + const extensions = resolveExtensions ?? DEFAULT_EXTENSIONS; for (const extension of extensions) { // Check if the .{extension} files exists. If so return that to watch diff --git a/src/index.ts b/src/index.ts index f8a7ba99..82ad92de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -345,7 +345,12 @@ class EsbuildServerlessPlugin implements ServerlessPlugin { } get functionEntries() { - return extractFunctionEntries(this.serviceDirPath, this.serverless.service.provider.name, this.functions); + return extractFunctionEntries( + this.serviceDirPath, + this.serverless.service.provider.name, + this.functions, + this.buildOptions?.resolveExtensions + ); } watch(): void { diff --git a/src/tests/helper.test.ts b/src/tests/helper.test.ts index cbec499d..d58fb6cf 100644 --- a/src/tests/helper.test.ts +++ b/src/tests/helper.test.ts @@ -135,6 +135,35 @@ describe('extractFunctionEntries', () => { ]); }); + it('should allow resolve extensions custom Esbuild setting', () => { + jest.mocked(fs.existsSync).mockReturnValue(true); + const functionDefinitions = { + function1: { + events: [], + handler: './file1.handler', + }, + function2: { + events: [], + handler: './file2.handler', + }, + }; + + const fileNames = extractFunctionEntries(cwd, 'aws', functionDefinitions, ['.custom.ts']); + + expect(fileNames).toStrictEqual([ + { + entry: 'file1.custom.ts', + func: functionDefinitions.function1, + functionAlias: 'function1', + }, + { + entry: 'file2.custom.ts', + func: functionDefinitions.function2, + functionAlias: 'function2', + }, + ]); + }); + it('should not return entries for handlers which have skipEsbuild set to true', async () => { jest.mocked(fs.existsSync).mockReturnValue(true); const functionDefinitions = {