Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: honour package.pattern over internally set exclusion list #339

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -120,7 +117,7 @@ export async function pack(this: EsbuildServerlessPlugin) {
const filesPathList = pipe<IFiles, IFiles, IFiles>(
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);
Expand Down Expand Up @@ -157,18 +154,18 @@ 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 }) => {
const excludedFiles = bundlePathList
.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 = [];
Expand All @@ -187,9 +184,8 @@ export async function pack(this: EsbuildServerlessPlugin) {

// filter files
const filesPathList = filterFilesForZipPackage({
files,
allPatternFilesToIncludedFiles,
functionAlias,
includedFiles,
excludedFiles,
hasExternals,
isGoogleProvider,
Expand Down
3 changes: 1 addition & 2 deletions src/tests/pack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -35,7 +35,6 @@ describe('filterFilesForZipPackage', () => {
functionAlias: 'fnAlias',
isGoogleProvider: false,
hasExternals: false,
includedFiles: [],
excludedFiles: [],
})
).toMatchInlineSnapshot(`
Expand Down