@@ -4,9 +4,11 @@ const util = require('util')
4
4
const makeDir = require ( 'make-dir' )
5
5
const findUp = require ( 'find-up' )
6
6
const nextOnNetlify = require ( './src/index.js' )
7
+ const AdmZip = require ( 'adm-zip' )
7
8
8
9
const validateNextUsage = require ( './helpers/validateNextUsage' )
9
10
const doesNotNeedPlugin = require ( './helpers/doesNotNeedPlugin' )
11
+ const getNextConfig = require ( './helpers/getNextConfig' )
10
12
11
13
const pWriteFile = util . promisify ( fs . writeFile )
12
14
@@ -58,6 +60,32 @@ module.exports = {
58
60
59
61
await nextOnNetlify ( { functionsDir : FUNCTIONS_SRC , publishDir : PUBLISH_DIR } )
60
62
} ,
63
+ async onPostBuild ( { netlifyConfig, packageJson, constants : { FUNCTIONS_DIST } , utils } ) {
64
+ if ( await doesNotNeedPlugin ( { netlifyConfig, packageJson, utils } ) ) {
65
+ return
66
+ }
67
+
68
+ const nextConfig = await getNextConfig ( utils . failBuild )
69
+ const getNetlifyFunctionName = require ( './src/lib/helpers/getNetlifyFunctionName' )
70
+
71
+ // any functions defined in the config need special handling
72
+ for ( const name in nextConfig . unstableNetlifyFunctionsSupport || { } ) {
73
+ const includeDirs = nextConfig . unstableNetlifyFunctionsSupport [ name ] . includeDirs || [ ]
74
+ console . log ( 'Processing included dirs for ' , name )
75
+
76
+ const zipName = path . join ( FUNCTIONS_DIST , getNetlifyFunctionName ( name ) + '.zip' )
77
+ const zip = new AdmZip ( zipName )
78
+ includeDirs . forEach ( ( includes ) => {
79
+ if ( fs . lstatSync ( includes ) . isDirectory ( ) ) {
80
+ // we add the files at the root of the ZIP because process.cwd()
81
+ // points to `/` in serverless functions
82
+ zip . addLocalFolder ( includes , includes )
83
+ console . log ( `Added ${ includes } to ${ zipName } ` )
84
+ }
85
+ } )
86
+ zip . writeZip ( zipName )
87
+ }
88
+ } ,
61
89
}
62
90
63
91
const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
0 commit comments