1- import { posix } from 'path'
2-
1+ import glob from 'globby'
32import { 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'
74
85import { HANDLER_FUNCTION_NAME } from '../constants'
96import { getDependenciesOfFile } from '../helpers/files'
107
118// Generate a file full of require.resolve() calls for all the pages in the
129// build. This is used by the nft bundler to find all the pages.
1310
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+ }
1715
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' , {
1920 cwd : root ,
2021 dot : true ,
2122 } )
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+ }
2527
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.
2838 exports.resolvePages = () => {
2939 try {
3040 ${ pageFiles . join ( '\n ' ) }
@@ -33,31 +43,21 @@ export const getPageResolver = async ({ publish, target }: { publish: string; ta
3343 `
3444}
3545
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 ( {
4153 functionsDir,
4254 sourceFiles,
4355} : {
4456 functionsDir : string
4557 sourceFiles : Array < string >
4658} ) => {
47- const dependencies = await Promise . all ( sourceFiles . map ( ( sourceFile ) => getDependenciesOfFile ( sourceFile ) ) )
4859 // We don't need the actual name, just the relative path.
4960 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 } )
6363}
0 commit comments