diff --git a/src/index.ts b/src/index.ts index fd450047..438f6d36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -358,15 +358,17 @@ class EsbuildServerlessPlugin implements ServerlessPlugin { }; for (const fn of Object.values(this.functions)) { + const patterns = [ + ...new Set([ + ...(fn.package?.include || []), + ...(fn.package?.exclude || []).map(concat('!')), + ...(fn.package?.patterns || []), + ]), + ]; + fn.package = { ...(fn.package || {}), - patterns: [ - ...new Set([ - ...(fn.package?.include || []), - ...(fn.package?.exclude || []).map(concat('!')), - ...(fn.package?.patterns || []), - ]), - ], + ...(patterns.length && { patterns }), }; } } diff --git a/src/pack.ts b/src/pack.ts index 01ff27dc..6d9af2c1 100644 --- a/src/pack.ts +++ b/src/pack.ts @@ -179,13 +179,13 @@ export async function pack(this: EsbuildServerlessPlugin) { const bundleExcludedFiles = bundlePathList.filter((item) => !bundlePath.startsWith(item)).map(trimExtension); - assert(func.package?.patterns); + const functionPackagePatterns = func.package?.patterns || []; - const functionExclusionPatterns = func.package.patterns + const functionExclusionPatterns = functionPackagePatterns .filter((pattern) => pattern.charAt(0) === '!') .map((pattern) => pattern.slice(1)); - const functionFiles = await globby(func.package.patterns, { cwd: buildDirPath }); + const functionFiles = await globby(functionPackagePatterns, { cwd: buildDirPath }); const functionExcludedFiles = (await globby(functionExclusionPatterns, { cwd: buildDirPath })).map(trimExtension); const includedFiles = [...packageFiles, ...functionFiles]; diff --git a/src/tests/index.test.ts b/src/tests/index.test.ts index d9fbbc65..7a15ce8d 100644 --- a/src/tests/index.test.ts +++ b/src/tests/index.test.ts @@ -39,6 +39,17 @@ const packageService: Partial = { getFunction: mockGetFunction, }; +const patternsService: Partial = { + functions: { + hello1: { handler: 'hello1.handler', events: [] }, + hello2: { handler: 'hello2.handler', events: [], package: {} }, + hello3: { handler: 'hello3.handler', events: [], package: { patterns: ['excluded-by-default.json'] } }, + }, + package: { patterns: ['!excluded-by-default.json'] }, + provider: mockProvider, + getFunction: mockGetFunction, +}; + const mockServerlessConfig = (serviceOverride?: Partial): Serverless => { const service = { ...packageIndividuallyService, @@ -223,3 +234,39 @@ describe('Move Artifacts', () => { }); }); }); + +describe('Prepare', () => { + describe('function package', () => { + it('should set package patterns on functions only if supplied', () => { + const plugin = new EsbuildServerlessPlugin(mockServerlessConfig(patternsService), mockOptions); + + plugin.hooks.initialize?.(); + + plugin.prepare(); + + expect(plugin.functions).toMatchInlineSnapshot(` + { + "hello1": { + "events": [], + "handler": "hello1.handler", + "package": {}, + }, + "hello2": { + "events": [], + "handler": "hello2.handler", + "package": {}, + }, + "hello3": { + "events": [], + "handler": "hello3.handler", + "package": { + "patterns": [ + "excluded-by-default.json", + ], + }, + }, + } + `); + }); + }); +}); diff --git a/src/tests/pack.test.ts b/src/tests/pack.test.ts index 1c121804..f03c6f4a 100644 --- a/src/tests/pack.test.ts +++ b/src/tests/pack.test.ts @@ -75,7 +75,7 @@ describe('pack', () => { handler: 'hello1.handler', events: [{ http: { path: 'hello', method: 'get' } }], name: 'serverless-example-dev-hello1', - package: { patterns: [] }, + package: {}, }, functionAlias: 'hello1', }, @@ -85,7 +85,7 @@ describe('pack', () => { handler: 'hello2.handler', events: [{ http: { path: 'hello', method: 'get' } }], name: 'serverless-example-dev-hello2', - package: { patterns: [] }, + package: {}, }, functionAlias: 'hello2', }, @@ -147,7 +147,7 @@ describe('pack', () => { handler: 'hello1.handler', events: [{ http: { path: 'hello', method: 'get' } }], name: 'serverless-example-dev-hello1', - package: { patterns: [] }, + package: {}, }, functionAlias: 'hello1', }, @@ -157,7 +157,7 @@ describe('pack', () => { handler: 'hello2.handler', events: [{ http: { path: 'hello', method: 'get' } }], name: 'serverless-example-dev-hello2', - package: { patterns: [] }, + package: {}, }, functionAlias: 'hello2', },