Skip to content

Commit ef5b2d1

Browse files
authored
feat: avoid adding unnecessary package.patterns property to functions (#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 <torbjorn.vanheeswijck@aligent.com.au>
1 parent 4b032d0 commit ef5b2d1

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,17 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
358358
};
359359

360360
for (const fn of Object.values(this.functions)) {
361+
const patterns = [
362+
...new Set([
363+
...(fn.package?.include || []),
364+
...(fn.package?.exclude || []).map(concat('!')),
365+
...(fn.package?.patterns || []),
366+
]),
367+
];
368+
361369
fn.package = {
362370
...(fn.package || {}),
363-
patterns: [
364-
...new Set([
365-
...(fn.package?.include || []),
366-
...(fn.package?.exclude || []).map(concat('!')),
367-
...(fn.package?.patterns || []),
368-
]),
369-
],
371+
...(patterns.length && { patterns }),
370372
};
371373
}
372374
}

src/pack.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ export async function pack(this: EsbuildServerlessPlugin) {
179179

180180
const bundleExcludedFiles = bundlePathList.filter((item) => !bundlePath.startsWith(item)).map(trimExtension);
181181

182-
assert(func.package?.patterns);
182+
const functionPackagePatterns = func.package?.patterns || [];
183183

184-
const functionExclusionPatterns = func.package.patterns
184+
const functionExclusionPatterns = functionPackagePatterns
185185
.filter((pattern) => pattern.charAt(0) === '!')
186186
.map((pattern) => pattern.slice(1));
187187

188-
const functionFiles = await globby(func.package.patterns, { cwd: buildDirPath });
188+
const functionFiles = await globby(functionPackagePatterns, { cwd: buildDirPath });
189189
const functionExcludedFiles = (await globby(functionExclusionPatterns, { cwd: buildDirPath })).map(trimExtension);
190190

191191
const includedFiles = [...packageFiles, ...functionFiles];

src/tests/index.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ const packageService: Partial<Service> = {
3939
getFunction: mockGetFunction,
4040
};
4141

42+
const patternsService: Partial<Service> = {
43+
functions: {
44+
hello1: { handler: 'hello1.handler', events: [] },
45+
hello2: { handler: 'hello2.handler', events: [], package: {} },
46+
hello3: { handler: 'hello3.handler', events: [], package: { patterns: ['excluded-by-default.json'] } },
47+
},
48+
package: { patterns: ['!excluded-by-default.json'] },
49+
provider: mockProvider,
50+
getFunction: mockGetFunction,
51+
};
52+
4253
const mockServerlessConfig = (serviceOverride?: Partial<Service>): Serverless => {
4354
const service = {
4455
...packageIndividuallyService,
@@ -223,3 +234,39 @@ describe('Move Artifacts', () => {
223234
});
224235
});
225236
});
237+
238+
describe('Prepare', () => {
239+
describe('function package', () => {
240+
it('should set package patterns on functions only if supplied', () => {
241+
const plugin = new EsbuildServerlessPlugin(mockServerlessConfig(patternsService), mockOptions);
242+
243+
plugin.hooks.initialize?.();
244+
245+
plugin.prepare();
246+
247+
expect(plugin.functions).toMatchInlineSnapshot(`
248+
{
249+
"hello1": {
250+
"events": [],
251+
"handler": "hello1.handler",
252+
"package": {},
253+
},
254+
"hello2": {
255+
"events": [],
256+
"handler": "hello2.handler",
257+
"package": {},
258+
},
259+
"hello3": {
260+
"events": [],
261+
"handler": "hello3.handler",
262+
"package": {
263+
"patterns": [
264+
"excluded-by-default.json",
265+
],
266+
},
267+
},
268+
}
269+
`);
270+
});
271+
});
272+
});

src/tests/pack.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('pack', () => {
7575
handler: 'hello1.handler',
7676
events: [{ http: { path: 'hello', method: 'get' } }],
7777
name: 'serverless-example-dev-hello1',
78-
package: { patterns: [] },
78+
package: {},
7979
},
8080
functionAlias: 'hello1',
8181
},
@@ -85,7 +85,7 @@ describe('pack', () => {
8585
handler: 'hello2.handler',
8686
events: [{ http: { path: 'hello', method: 'get' } }],
8787
name: 'serverless-example-dev-hello2',
88-
package: { patterns: [] },
88+
package: {},
8989
},
9090
functionAlias: 'hello2',
9191
},
@@ -147,7 +147,7 @@ describe('pack', () => {
147147
handler: 'hello1.handler',
148148
events: [{ http: { path: 'hello', method: 'get' } }],
149149
name: 'serverless-example-dev-hello1',
150-
package: { patterns: [] },
150+
package: {},
151151
},
152152
functionAlias: 'hello1',
153153
},
@@ -157,7 +157,7 @@ describe('pack', () => {
157157
handler: 'hello2.handler',
158158
events: [{ http: { path: 'hello', method: 'get' } }],
159159
name: 'serverless-example-dev-hello2',
160-
package: { patterns: [] },
160+
package: {},
161161
},
162162
functionAlias: 'hello2',
163163
},

0 commit comments

Comments
 (0)