-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathgetPageResolver.ts
63 lines (53 loc) · 1.99 KB
/
getPageResolver.ts
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
import { posix } from 'path'
import { outdent } from 'outdent'
import { relative, resolve } from 'pathe'
import slash from 'slash'
import glob from 'tiny-glob'
import { HANDLER_FUNCTION_NAME } from '../constants'
import { getDependenciesOfFile } from '../helpers/files'
// Generate a file full of require.resolve() calls for all the pages in the
// build. This is used by the nft bundler to find all the pages.
export const getPageResolver = async ({ publish, target }: { publish: string; target: string }) => {
const functionDir = posix.resolve(posix.join('.netlify', 'functions', HANDLER_FUNCTION_NAME))
const root = posix.resolve(slash(publish), target === 'server' ? 'server' : 'serverless', 'pages')
const pages = await glob('**/*.js', {
cwd: root,
dot: true,
})
const pageFiles = pages
.map((page) => `require.resolve('${posix.relative(functionDir, posix.join(root, slash(page)))}')`)
.sort()
return outdent`
// This file is purely to allow nft to know about these pages. It should be temporary.
exports.resolvePages = () => {
try {
${pageFiles.join('\n ')}
} catch {}
}
`
}
/**
* API routes only need the dependencies for a single entrypoint, so we use the
* NFT trace file to get the dependencies.
*/
export const getSinglePageResolver = async ({
functionsDir,
sourceFiles,
}: {
functionsDir: string
sourceFiles: Array<string>
}) => {
const dependencies = await Promise.all(sourceFiles.map((sourceFile) => getDependenciesOfFile(sourceFile)))
// We don't need the actual name, just the relative path.
const functionDir = resolve(functionsDir, 'functionName')
const deduped = [...new Set(dependencies.flat())]
const pageFiles = [...sourceFiles, ...deduped]
.map((file) => `require.resolve('${relative(functionDir, file)}')`)
.sort()
return outdent/* javascript */ `
// This file is purely to allow nft to know about these pages.
try {
${pageFiles.join('\n ')}
} catch {}
`
}