Skip to content

Commit 40d1c8b

Browse files
committed
feat: unstableNetlifyFunctionsSupport.includeDirs configuration
1 parent 60b3489 commit 40d1c8b

File tree

3 files changed

+44
-49
lines changed

3 files changed

+44
-49
lines changed

index.js

+28
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ const util = require('util')
44
const makeDir = require('make-dir')
55
const findUp = require('find-up')
66
const nextOnNetlify = require('./src/index.js')
7+
const AdmZip = require('adm-zip')
78

89
const validateNextUsage = require('./helpers/validateNextUsage')
910
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
11+
const getNextConfig = require('./helpers/getNextConfig')
1012

1113
const pWriteFile = util.promisify(fs.writeFile)
1214

@@ -58,6 +60,32 @@ module.exports = {
5860

5961
await nextOnNetlify({ functionsDir: FUNCTIONS_SRC, publishDir: PUBLISH_DIR })
6062
},
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+
},
6189
}
6290

6391
const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'

package-lock.json

+15-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"homepage": "https://github.com/netlify/netlify-plugin-nextjs#readme",
4545
"dependencies": {
4646
"@sls-next/lambda-at-edge": "^1.5.2",
47+
"adm-zip": "^0.5.4",
4748
"chalk": "^4.1.0",
4849
"chokidar": "^3.5.1",
4950
"commander": "^7.1.0",

0 commit comments

Comments
 (0)