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