Skip to content

Commit

Permalink
feat: avoid adding unnecessary package.patterns property to functions (
Browse files Browse the repository at this point in the history
…#474)

* feat: passing test for existing function patterns implementation

* feat: tests and fixes for not including patterns in function package object

---------

Co-authored-by: Torbjorn van Heeswijck <[email protected]>
  • Loading branch information
tvhees and tvhees authored Jul 11, 2023
1 parent 4b032d0 commit ef5b2d1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
};
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
47 changes: 47 additions & 0 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ const packageService: Partial<Service> = {
getFunction: mockGetFunction,
};

const patternsService: Partial<Service> = {
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<Service>): Serverless => {
const service = {
...packageIndividuallyService,
Expand Down Expand Up @@ -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",
],
},
},
}
`);
});
});
});
8 changes: 4 additions & 4 deletions src/tests/pack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand All @@ -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',
},
Expand Down Expand Up @@ -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',
},
Expand All @@ -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',
},
Expand Down

0 comments on commit ef5b2d1

Please sign in to comment.