2018-01-30 15:40:52 +00:00
import path from 'path'
2018-03-30 14:59:42 +00:00
import promisify from '../../lib/promisify'
import globModule from 'glob'
2018-07-25 11:45:42 +00:00
import { CLIENT _STATIC _FILES _PATH } from '../../lib/constants'
2018-03-30 14:59:42 +00:00
const glob = promisify ( globModule )
2018-01-30 15:40:52 +00:00
2018-07-25 11:45:42 +00:00
export async function getPages ( dir , { nextPagesDir , dev , buildId , isServer , pageExtensions } ) {
2018-02-14 15:20:41 +00:00
const pageFiles = await getPagePaths ( dir , { dev , isServer , pageExtensions } )
2018-01-30 15:40:52 +00:00
2018-07-25 11:45:42 +00:00
return getPageEntries ( pageFiles , { nextPagesDir , buildId , isServer , pageExtensions } )
2018-01-30 15:40:52 +00:00
}
2018-03-30 13:08:09 +00:00
export async function getPagePaths ( dir , { dev , isServer , pageExtensions } ) {
2018-01-30 15:40:52 +00:00
let pages
if ( dev ) {
2018-04-12 08:33:22 +00:00
// In development we only compile _document.js, _error.js and _app.js when starting, since they're always needed. All other pages are compiled with on demand entries
2018-07-24 09:24:40 +00:00
pages = await glob ( isServer ? ` pages/+(_document|_app|_error).+( ${ pageExtensions } ) ` : ` pages/+(_app|_error).+( ${ pageExtensions } ) ` , { cwd : dir } )
2018-01-30 15:40:52 +00:00
} else {
2018-02-14 15:20:41 +00:00
// In production get all pages from the pages directory
pages = await glob ( isServer ? ` pages/**/*.+( ${ pageExtensions } ) ` : ` pages/**/!(_document)*.+( ${ pageExtensions } ) ` , { cwd : dir } )
2018-01-30 15:40:52 +00:00
}
return pages
}
// Convert page path into single entry
2018-07-25 11:45:42 +00:00
export function createEntry ( filePath , { buildId = '' , name , pageExtensions } = { } ) {
2018-01-30 15:40:52 +00:00
const parsedPath = path . parse ( filePath )
let entryName = name || filePath
// This makes sure we compile `pages/blog/index.js` to `pages/blog.js`.
// Excludes `pages/index.js` from this rule since we do want `/` to route to `pages/index.js`
if ( parsedPath . dir !== 'pages' && parsedPath . name === 'index' ) {
entryName = ` ${ parsedPath . dir } .js `
}
// Makes sure supported extensions are stripped off. The outputted file should always be `.js`
2018-02-14 15:20:41 +00:00
if ( pageExtensions ) {
2018-02-17 11:36:47 +00:00
entryName = entryName . replace ( new RegExp ( ` \\ .+( ${ pageExtensions } ) $ ` ) , '.js' )
2018-02-14 15:20:41 +00:00
}
2018-01-30 15:40:52 +00:00
return {
2018-07-25 11:45:42 +00:00
name : path . join ( CLIENT _STATIC _FILES _PATH , buildId , entryName ) ,
2018-01-30 15:40:52 +00:00
files : [ parsedPath . root ? filePath : ` ./ ${ filePath } ` ] // The entry always has to be an array.
}
}
// Convert page paths into entries
2018-07-25 11:45:42 +00:00
export function getPageEntries ( pagePaths , { nextPagesDir , buildId , isServer = false , pageExtensions } = { } ) {
2018-01-30 15:40:52 +00:00
const entries = { }
for ( const filePath of pagePaths ) {
2018-07-25 11:45:42 +00:00
const entry = createEntry ( filePath , { pageExtensions , buildId } )
2018-01-30 15:40:52 +00:00
entries [ entry . name ] = entry . files
}
2018-04-12 08:33:22 +00:00
const appPagePath = path . join ( nextPagesDir , '_app.js' )
2018-07-25 11:45:42 +00:00
const appPageEntry = createEntry ( appPagePath , { buildId , name : 'pages/_app.js' } ) // default app.js
2018-04-12 08:33:22 +00:00
if ( ! entries [ appPageEntry . name ] ) {
entries [ appPageEntry . name ] = appPageEntry . files
}
2018-01-30 15:40:52 +00:00
const errorPagePath = path . join ( nextPagesDir , '_error.js' )
2018-07-25 11:45:42 +00:00
const errorPageEntry = createEntry ( errorPagePath , { buildId , name : 'pages/_error.js' } ) // default error.js
2018-01-30 15:40:52 +00:00
if ( ! entries [ errorPageEntry . name ] ) {
entries [ errorPageEntry . name ] = errorPageEntry . files
}
if ( isServer ) {
const documentPagePath = path . join ( nextPagesDir , '_document.js' )
2018-07-25 11:45:42 +00:00
const documentPageEntry = createEntry ( documentPagePath , { buildId , name : 'pages/_document.js' } ) // default _document.js
2018-01-30 15:40:52 +00:00
if ( ! entries [ documentPageEntry . name ] ) {
entries [ documentPageEntry . name ] = documentPageEntry . files
}
}
return entries
}