1
1
import { posix } from 'path'
2
2
3
+ import glob from 'globby'
3
4
import { outdent } from 'outdent'
4
5
import { relative , resolve } from 'pathe'
5
6
import slash from 'slash'
6
- import glob from 'tiny-glob'
7
7
8
8
import { HANDLER_FUNCTION_NAME } from '../constants'
9
9
import { getDependenciesOfFile } from '../helpers/files'
10
10
11
11
// Generate a file full of require.resolve() calls for all the pages in the
12
12
// build. This is used by the nft bundler to find all the pages.
13
13
14
- export const getPageResolver = async ( { publish, target } : { publish : string ; target : string } ) => {
15
- const functionDir = posix . resolve ( posix . join ( '.netlify' , 'functions' , HANDLER_FUNCTION_NAME ) )
16
- const root = posix . resolve ( slash ( publish ) , target === 'server' ? 'server' : 'serverless' , 'pages' )
14
+ export const getUniqueDependencies = async ( sourceFiles : Array < string > ) => {
15
+ const dependencies = await Promise . all ( sourceFiles . map ( ( sourceFile ) => getDependenciesOfFile ( sourceFile ) ) )
16
+ return [ ...new Set ( [ ...sourceFiles , ...dependencies . flat ( ) ] ) ] . sort ( )
17
+ }
17
18
18
- const pages = await glob ( '**/*.js' , {
19
+ export const getAllPageDependencies = async ( publish : string ) => {
20
+ const root = posix . resolve ( slash ( publish ) , 'server' )
21
+
22
+ const pageFiles = await glob ( '{pages,app}/**/*.js' , {
19
23
cwd : root ,
24
+ absolute : true ,
20
25
dot : true ,
21
26
} )
22
- const pageFiles = pages
23
- . map ( ( page ) => `require.resolve('${ posix . relative ( functionDir , posix . join ( root , slash ( page ) ) ) } ')` )
24
- . sort ( )
25
27
26
- return outdent `
27
- // This file is purely to allow nft to know about these pages. It should be temporary.
28
+ return getUniqueDependencies ( pageFiles )
29
+ }
30
+
31
+ export const getResolverForDependencies = ( {
32
+ dependencies,
33
+ functionDir,
34
+ } : {
35
+ dependencies : string [ ]
36
+ functionDir : string
37
+ } ) => {
38
+ const pageFiles = dependencies . map ( ( file ) => `require.resolve('${ relative ( functionDir , file ) } ')` )
39
+ return outdent /* javascript */ `
40
+ // This file is purely to allow nft to know about these pages.
28
41
exports.resolvePages = () => {
29
42
try {
30
43
${ pageFiles . join ( '\n ' ) }
@@ -33,31 +46,21 @@ export const getPageResolver = async ({ publish, target }: { publish: string; ta
33
46
`
34
47
}
35
48
36
- /**
37
- * API routes only need the dependencies for a single entrypoint, so we use the
38
- * NFT trace file to get the dependencies.
39
- */
40
- export const getSinglePageResolver = async ( {
49
+ export const getResolverForPages = async ( publish : string ) => {
50
+ const functionDir = posix . resolve ( posix . join ( '.netlify' , 'functions' , HANDLER_FUNCTION_NAME ) )
51
+ const dependencies = await getAllPageDependencies ( publish )
52
+ return getResolverForDependencies ( { dependencies, functionDir } )
53
+ }
54
+
55
+ export const getResolverForSourceFiles = async ( {
41
56
functionsDir,
42
57
sourceFiles,
43
58
} : {
44
59
functionsDir : string
45
60
sourceFiles : Array < string >
46
61
} ) => {
47
- const dependencies = await Promise . all ( sourceFiles . map ( ( sourceFile ) => getDependenciesOfFile ( sourceFile ) ) )
48
62
// We don't need the actual name, just the relative path.
49
63
const functionDir = resolve ( functionsDir , 'functionName' )
50
-
51
- const deduped = [ ...new Set ( dependencies . flat ( ) ) ]
52
-
53
- const pageFiles = [ ...sourceFiles , ...deduped ]
54
- . map ( ( file ) => `require.resolve('${ relative ( functionDir , file ) } ')` )
55
- . sort ( )
56
-
57
- return outdent /* javascript */ `
58
- // This file is purely to allow nft to know about these pages.
59
- try {
60
- ${ pageFiles . join ( '\n ' ) }
61
- } catch {}
62
- `
64
+ const dependencies = await getUniqueDependencies ( sourceFiles )
65
+ return getResolverForDependencies ( { dependencies, functionDir } )
63
66
}
0 commit comments