Skip to content

Commit

Permalink
feat(resolveExtensions): added support for custom resolve extensions …
Browse files Browse the repository at this point in the history
…esbuild setting
  • Loading branch information
JohnGrisham committed Dec 1, 2023
1 parent 901dcfc commit 6a97354
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
6 changes: 4 additions & 2 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(data: T | T[]): T[] {
return Array.isArray(data) ? data : [data];
Expand All @@ -27,7 +28,8 @@ export function assertIsString(input: unknown, message = 'input is not a string'
export function extractFunctionEntries(
cwd: string,
provider: string,
functions: Record<string, Serverless.FunctionDefinitionHandler>
functions: Record<string, Serverless.FunctionDefinitionHandler>,
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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions src/tests/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 6a97354

Please sign in to comment.