1
1
import { cp , mkdir , readFile , rm , writeFile } from 'fs/promises'
2
2
import { join } from 'node:path'
3
+ import { relative } from 'path'
3
4
4
5
import { glob } from 'fast-glob'
5
6
@@ -8,15 +9,43 @@ import { PluginContext, SERVER_HANDLER_NAME } from '../plugin-context.js'
8
9
9
10
/** Copies the runtime dist folder to the lambda */
10
11
const 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
+
11
38
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 ) , {
15
43
recursive : true ,
16
44
force : true ,
17
45
} ) ,
18
- ) ,
19
- )
46
+ )
47
+ }
48
+ await Promise . all ( promises )
20
49
}
21
50
22
51
const writeHandlerManifest = async ( ctx : PluginContext ) => {
0 commit comments