-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathindex.js
94 lines (73 loc) · 2.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const { normalize } = require('path')
const chokidar = require('chokidar')
const debounceFn = require('debounce-fn')
const execa = require('execa')
const getNextConfig = require('../helpers/getNextConfig')
const { NETLIFY_PUBLISH_PATH, NETLIFY_FUNCTIONS_PATH, SRC_FILES } = require('./lib/config')
const { logTitle } = require('./lib/helpers/logger')
const copyNextAssets = require('./lib/steps/copyNextAssets')
const copyPublicFiles = require('./lib/steps/copyPublicFiles')
const prepareFolders = require('./lib/steps/prepareFolders')
const setupHeaders = require('./lib/steps/setupHeaders')
const setupImageFunction = require('./lib/steps/setupImageFunction')
const setupPages = require('./lib/steps/setupPages')
const setupRedirects = require('./lib/steps/setupRedirects')
const build = async (functionsPath, publishPath, nextRoot) => {
const trackNextOnNetlifyFiles = prepareFolders({
functionsPath,
publishPath,
})
// If we're in a monorepo we need to move into the Next site root
const oldCwd = process.cwd()
if (nextRoot !== oldCwd) {
process.chdir(nextRoot)
}
copyPublicFiles(publishPath)
await copyNextAssets(publishPath)
await setupPages({ functionsPath, publishPath })
const { images } = await getNextConfig()
await setupImageFunction(functionsPath, images)
await setupRedirects(publishPath)
setupHeaders(publishPath)
// ...and move back after
if (nextRoot !== oldCwd) {
process.chdir(oldCwd)
}
trackNextOnNetlifyFiles()
logTitle('✅ Success! All done!')
}
const watch = (functionsPath, publishPath, nextRoot) => {
logTitle(`👀 Watching source code for changes`)
const runBuild = debounceFn(
async () => {
try {
execa.sync('next', ['build'], { stdio: 'inherit', preferLocal: true, cwd: nextRoot })
await build(functionsPath, publishPath, nextRoot)
} catch (error) {
console.log(error)
}
},
{
wait: 3000,
},
)
chokidar.watch(SRC_FILES).on('all', runBuild)
}
// options param:
// {
// functionsDir: string to path
// publishDir: string to path
// watch: { directory: string to path }
// }
//
const nextOnNetlify = async (options = {}) => {
const functionsPath = normalize(options.functionsDir || NETLIFY_FUNCTIONS_PATH)
const publishPath = normalize(options.publishDir || NETLIFY_PUBLISH_PATH)
const nextRoot = normalize(options.nextRoot || process.cwd())
if (options.watch) {
watch(functionsPath, publishPath, nextRoot)
} else {
await build(functionsPath, publishPath, nextRoot)
}
}
module.exports = nextOnNetlify