@@ -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
4045export 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
@@ -88,25 +86,24 @@ export const filterFilesForZipPackage = ({
8886export async function pack ( this : EsbuildServerlessPlugin ) {
8987 // GOOGLE Provider requires a package.json and NO node_modules
9088 const isGoogleProvider = this . serverless ?. service ?. provider ?. name === 'google' ;
91- const excludedFiles = isGoogleProvider ? [ ] : excludedFilesDefault ;
9289
9390 // Google provider cannot use individual packaging for now - this could be built in a future release
9491 if ( isGoogleProvider && this . serverless ?. service ?. package ?. individually )
9592 throw new Error (
9693 'Packaging failed: cannot package function individually when using Google provider'
9794 ) ;
9895
99- // get a list of all path in build
100- const files : IFiles = globby
101- . sync ( '**' , {
96+ const excludedFiles = isGoogleProvider ? [ ] : excludedFilesDefault ;
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 ( / ^ _ _ o n l y _ [ ^ / ] + $ / ) ) as ( x : IFiles ) => IFiles ,
122119 map ( over ( lensProp ( 'localPath' ) , replace ( / ^ _ _ o n l y _ [ ^ / ] + \/ / , '' ) ) )
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,
0 commit comments