Skip to content

Commit 55a0675

Browse files
committed
fix: honour package.pattern over internally set exclusion list
1 parent a233a1a commit 55a0675

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

src/pack.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,29 @@ function setFunctionArtifactPath(this: EsbuildServerlessPlugin, func, artifactPa
3535
}
3636
}
3737

38-
const excludedFilesDefault = ['package-lock.json', 'pnpm-lock.yaml', 'yarn.lock', 'package.json'];
38+
const excludedFilesDefault = [
39+
'!package-lock.json',
40+
'!pnpm-lock.yaml',
41+
'!yarn.lock',
42+
'!package.json',
43+
];
3944

4045
export const filterFilesForZipPackage = ({
41-
files,
46+
allPatternFilesToIncludedFiles,
4247
functionAlias,
43-
includedFiles,
4448
excludedFiles,
4549
hasExternals,
4650
isGoogleProvider,
4751
depWhiteList,
4852
}: {
49-
files: IFiles;
53+
allPatternFilesToIncludedFiles: IFiles;
5054
functionAlias: string;
51-
includedFiles: string[];
5255
excludedFiles: string[];
5356
hasExternals: boolean;
5457
isGoogleProvider: boolean;
5558
depWhiteList: string[];
5659
}) => {
57-
return files.filter(({ localPath }) => {
58-
// if file is present in patterns it must be included
59-
if (includedFiles.find((file) => file === localPath)) {
60-
return true;
61-
}
62-
60+
return allPatternFilesToIncludedFiles.filter(({ localPath }) => {
6361
// exclude non individual files based on file path (and things that look derived, e.g. foo.js => foo.js.map)
6462
if (excludedFiles.find((p) => localPath.startsWith(`${p}.`))) return false;
6563

@@ -96,17 +94,16 @@ export async function pack(this: EsbuildServerlessPlugin) {
9694
'Packaging failed: cannot package function individually when using Google provider'
9795
);
9896

99-
// get a list of all path in build
100-
const files: IFiles = globby
101-
.sync('**', {
97+
// get a list of all paths in build that we want
98+
const patternBasedFilesToIncluded: IFiles = globby
99+
.sync(['**', ...excludedFiles, ...(this.serverless.service.package.patterns ?? [])], {
102100
cwd: this.buildDirPath,
103101
dot: true,
104102
onlyFiles: true,
105103
})
106-
.filter((p) => !excludedFiles.includes(p))
107104
.map((localPath) => ({ localPath, rootPath: path.join(this.buildDirPath, localPath) }));
108105

109-
if (isEmpty(files)) {
106+
if (isEmpty(patternBasedFilesToIncluded)) {
110107
console.log('Packaging: No files found. Skipping esbuild.');
111108
return;
112109
}
@@ -120,7 +117,7 @@ export async function pack(this: EsbuildServerlessPlugin) {
120117
const filesPathList = pipe<IFiles, IFiles, IFiles>(
121118
reject(test(/^__only_[^/]+$/)) as (x: IFiles) => IFiles,
122119
map(over(lensProp('localPath'), replace(/^__only_[^/]+\//, '')))
123-
)(files);
120+
)(patternBasedFilesToIncluded);
124121

125122
const startZip = Date.now();
126123
await zip(artifactPath, filesPathList, this.buildOptions.nativeZip);
@@ -157,18 +154,18 @@ export async function pack(this: EsbuildServerlessPlugin) {
157154
? await packager.getProdDependencies(this.buildDirPath)
158155
: {};
159156

160-
const packageFiles = await globby(this.serverless.service.package.patterns);
161-
162157
// package each function
163158
await Promise.all(
164159
buildResults.map(async ({ func, functionAlias, bundlePath }) => {
165160
const excludedFiles = bundlePathList
166161
.filter((p) => !bundlePath.startsWith(p))
167162
.map(trimExtension);
168163

169-
const functionFiles = await globby(func.package.patterns);
164+
const functionFiles = globby
165+
.sync(func.package.patterns)
166+
.map((localPath) => ({ localPath, rootPath: path.join(this.buildDirPath, localPath) }));
170167

171-
const includedFiles = [...packageFiles, ...functionFiles];
168+
const allPatternFilesToIncludedFiles = [...patternBasedFilesToIncluded, ...functionFiles];
172169

173170
// allowed external dependencies in the final zip
174171
let depWhiteList = [];
@@ -187,9 +184,8 @@ export async function pack(this: EsbuildServerlessPlugin) {
187184

188185
// filter files
189186
const filesPathList = filterFilesForZipPackage({
190-
files,
187+
allPatternFilesToIncludedFiles,
191188
functionAlias,
192-
includedFiles,
193189
excludedFiles,
194190
hasExternals,
195191
isGoogleProvider,

src/tests/pack.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('filterFilesForZipPackage', () => {
1717
it('should filter out files for another zip package', () => {
1818
expect(
1919
filterFilesForZipPackage({
20-
files: [
20+
allPatternFilesToIncludedFiles: [
2121
{
2222
localPath:
2323
'__only_service-otherFnName/bin/imagemagick/include/ImageMagick/magick/method-attribute.h',
@@ -35,7 +35,6 @@ describe('filterFilesForZipPackage', () => {
3535
functionAlias: 'fnAlias',
3636
isGoogleProvider: false,
3737
hasExternals: false,
38-
includedFiles: [],
3938
excludedFiles: [],
4039
})
4140
).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)