11import { cp , mkdir , readFile , rm , writeFile } from 'fs/promises'
22import { join } from 'node:path'
3+ import { relative } from 'path'
34
45import { glob } from 'fast-glob'
56
@@ -8,15 +9,43 @@ import { PluginContext, SERVER_HANDLER_NAME } from '../plugin-context.js'
89
910/** Copies the runtime dist folder to the lambda */
1011const copyHandlerDependencies = async ( ctx : PluginContext ) => {
12+ const promises : Promise < void > [ ] = [ ]
13+ const { included_files : includedFiles = [ ] } = ctx . netlifyConfig . functions ?. [ '*' ] || { }
14+ // if the user specified some files to include in the lambda
15+ // we need to copy them to the functions-internal folder
16+ if ( includedFiles . length !== 0 ) {
17+ const resolvedFiles = await Promise . all (
18+ includedFiles . map ( ( globPattern ) => glob ( globPattern , { cwd : process . cwd ( ) } ) ) ,
19+ )
20+ for ( const filePath of resolvedFiles . flat ( ) ) {
21+ promises . push (
22+ cp (
23+ join ( process . cwd ( ) , filePath ) ,
24+ // the serverHandlerDir is aware of the dist dir.
25+ // The distDir must not be the package path therefore we need to rely on the
26+ // serverHandlerDir instead of the serverHandlerRootDir
27+ // therefore we need to remove the package path from the filePath
28+ join ( ctx . serverHandlerDir , relative ( ctx . packagePath , filePath ) ) ,
29+ {
30+ recursive : true ,
31+ force : true ,
32+ } ,
33+ ) ,
34+ )
35+ }
36+ }
37+
1138 const fileList = await glob ( 'dist/**/*' , { cwd : ctx . pluginDir } )
12- await Promise . all (
13- [ ...fileList ] . map ( ( path ) =>
14- cp ( join ( ctx . pluginDir , path ) , join ( ctx . serverHandlerDir , '.netlify' , path ) , {
39+
40+ for ( const filePath of fileList ) {
41+ promises . push (
42+ cp ( join ( ctx . pluginDir , filePath ) , join ( ctx . serverHandlerDir , '.netlify' , filePath ) , {
1543 recursive : true ,
1644 force : true ,
1745 } ) ,
18- ) ,
19- )
46+ )
47+ }
48+ await Promise . all ( promises )
2049}
2150
2251const writeHandlerManifest = async ( ctx : PluginContext ) => {
0 commit comments