Skip to content

Commit

Permalink
fix: honour package.pattern over internally set exclusion list
Browse files Browse the repository at this point in the history
  • Loading branch information
codingnuclei committed Jul 18, 2022
1 parent a233a1a commit 3d3fb50
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
44 changes: 20 additions & 24 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 @@ -88,25 +86,24 @@ export const filterFilesForZipPackage = ({
export async function pack(this: EsbuildServerlessPlugin) {
// GOOGLE Provider requires a package.json and NO node_modules
const isGoogleProvider = this.serverless?.service?.provider?.name === 'google';
const excludedFiles = isGoogleProvider ? [] : excludedFilesDefault;

// Google provider cannot use individual packaging for now - this could be built in a future release
if (isGoogleProvider && this.serverless?.service?.package?.individually)
throw new Error(
'Packaging failed: cannot package function individually when using Google provider'
);

// get a list of all path in build
const files: IFiles = globby
.sync('**', {
const excludedFiles = isGoogleProvider ? [] : excludedFilesDefault;
// 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

0 comments on commit 3d3fb50

Please sign in to comment.