From 55a0675b9024a04bf8e150c8031f3ab15007613e Mon Sep 17 00:00:00 2001 From: Chris Williams Date: Mon, 18 Jul 2022 17:47:24 +0100 Subject: [PATCH] fix: honour package.pattern over internally set exclusion list --- src/pack.ts | 42 +++++++++++++++++++----------------------- src/tests/pack.test.ts | 3 +-- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/pack.ts b/src/pack.ts index 3737dc15..332a215f 100644 --- a/src/pack.ts +++ b/src/pack.ts @@ -35,31 +35,29 @@ function setFunctionArtifactPath(this: EsbuildServerlessPlugin, func, artifactPa } } -const excludedFilesDefault = ['package-lock.json', 'pnpm-lock.yaml', 'yarn.lock', 'package.json']; +const excludedFilesDefault = [ + '!package-lock.json', + '!pnpm-lock.yaml', + '!yarn.lock', + '!package.json', +]; export const filterFilesForZipPackage = ({ - files, + allPatternFilesToIncludedFiles, functionAlias, - includedFiles, excludedFiles, hasExternals, isGoogleProvider, depWhiteList, }: { - files: IFiles; + allPatternFilesToIncludedFiles: IFiles; functionAlias: string; - includedFiles: string[]; excludedFiles: string[]; hasExternals: boolean; isGoogleProvider: boolean; depWhiteList: string[]; }) => { - return files.filter(({ localPath }) => { - // if file is present in patterns it must be included - if (includedFiles.find((file) => file === localPath)) { - return true; - } - + return allPatternFilesToIncludedFiles.filter(({ localPath }) => { // exclude non individual files based on file path (and things that look derived, e.g. foo.js => foo.js.map) if (excludedFiles.find((p) => localPath.startsWith(`${p}.`))) return false; @@ -96,17 +94,16 @@ export async function pack(this: EsbuildServerlessPlugin) { 'Packaging failed: cannot package function individually when using Google provider' ); - // get a list of all path in build - const files: IFiles = globby - .sync('**', { + // get a list of all paths in build that we want + const patternBasedFilesToIncluded: IFiles = globby + .sync(['**', ...excludedFiles, ...(this.serverless.service.package.patterns ?? [])], { cwd: this.buildDirPath, dot: true, onlyFiles: true, }) - .filter((p) => !excludedFiles.includes(p)) .map((localPath) => ({ localPath, rootPath: path.join(this.buildDirPath, localPath) })); - if (isEmpty(files)) { + if (isEmpty(patternBasedFilesToIncluded)) { console.log('Packaging: No files found. Skipping esbuild.'); return; } @@ -120,7 +117,7 @@ export async function pack(this: EsbuildServerlessPlugin) { const filesPathList = pipe( reject(test(/^__only_[^/]+$/)) as (x: IFiles) => IFiles, map(over(lensProp('localPath'), replace(/^__only_[^/]+\//, ''))) - )(files); + )(patternBasedFilesToIncluded); const startZip = Date.now(); await zip(artifactPath, filesPathList, this.buildOptions.nativeZip); @@ -157,8 +154,6 @@ export async function pack(this: EsbuildServerlessPlugin) { ? await packager.getProdDependencies(this.buildDirPath) : {}; - const packageFiles = await globby(this.serverless.service.package.patterns); - // package each function await Promise.all( buildResults.map(async ({ func, functionAlias, bundlePath }) => { @@ -166,9 +161,11 @@ export async function pack(this: EsbuildServerlessPlugin) { .filter((p) => !bundlePath.startsWith(p)) .map(trimExtension); - const functionFiles = await globby(func.package.patterns); + const functionFiles = globby + .sync(func.package.patterns) + .map((localPath) => ({ localPath, rootPath: path.join(this.buildDirPath, localPath) })); - const includedFiles = [...packageFiles, ...functionFiles]; + const allPatternFilesToIncludedFiles = [...patternBasedFilesToIncluded, ...functionFiles]; // allowed external dependencies in the final zip let depWhiteList = []; @@ -187,9 +184,8 @@ export async function pack(this: EsbuildServerlessPlugin) { // filter files const filesPathList = filterFilesForZipPackage({ - files, + allPatternFilesToIncludedFiles, functionAlias, - includedFiles, excludedFiles, hasExternals, isGoogleProvider, diff --git a/src/tests/pack.test.ts b/src/tests/pack.test.ts index 668d5060..1ba36739 100644 --- a/src/tests/pack.test.ts +++ b/src/tests/pack.test.ts @@ -17,7 +17,7 @@ describe('filterFilesForZipPackage', () => { it('should filter out files for another zip package', () => { expect( filterFilesForZipPackage({ - files: [ + allPatternFilesToIncludedFiles: [ { localPath: '__only_service-otherFnName/bin/imagemagick/include/ImageMagick/magick/method-attribute.h', @@ -35,7 +35,6 @@ describe('filterFilesForZipPackage', () => { functionAlias: 'fnAlias', isGoogleProvider: false, hasExternals: false, - includedFiles: [], excludedFiles: [], }) ).toMatchInlineSnapshot(`