Skip to content

Commit 393d685

Browse files
committed
chore: refactor
1 parent 40d1c8b commit 393d685

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

helpers/copyUnstableIncludedDirs.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const path = require('path')
2+
const AdmZip = require('adm-zip')
3+
const getNetlifyFunctionName = require('../src/lib/helpers/getNetlifyFunctionName')
4+
5+
// This is UNSTABLE support for special use cases needing files served with their Next.js
6+
// pages that become Netlify functions. Eventually this will be supported internally.
7+
const copyUnstableIncludedDirs = ({ nextConfig, functionsDist }) => {
8+
for (const name in nextConfig.unstableNetlifyFunctionsSupport || {}) {
9+
const includeDirs = nextConfig.unstableNetlifyFunctionsSupport[name].includeDirs || []
10+
console.log('Processing included dirs for ', name)
11+
12+
const zipName = path.join(functionsDist, getNetlifyFunctionName(name) + '.zip')
13+
const zip = new AdmZip(zipName)
14+
includeDirs.forEach((includes) => {
15+
if (fs.lstatSync(includes).isDirectory()) {
16+
// We add the files at the root of the ZIP because process.cwd()
17+
// points to `/` in serverless functions
18+
zip.addLocalFolder(includes, includes)
19+
console.log(`Added ${includes} to ${zipName}`)
20+
}
21+
})
22+
zip.writeZip(zipName)
23+
}
24+
}
25+
26+
module.exports = copyUnstableIncludedDirs

index.js

+2-20
Original file line numberDiff line numberDiff line change
@@ -4,11 +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')
87

98
const validateNextUsage = require('./helpers/validateNextUsage')
109
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
1110
const getNextConfig = require('./helpers/getNextConfig')
11+
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
1212

1313
const pWriteFile = util.promisify(fs.writeFile)
1414

@@ -66,25 +66,7 @@ module.exports = {
6666
}
6767

6868
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-
}
69+
copyUnstableIncludedDirs({ nextConfig, functionsDist: FUNCTIONS_DIST })
8870
},
8971
}
9072

0 commit comments

Comments
 (0)