rsnext/packages/next-server/server/config.ts
JJ Kasper cdd54afb0d Add auto static/dynamic (#7293)
* Add automatic exporting of pages with no getInitialProps

* Add support for exporting serverless to static
and serving the html files during next start

* Fix missing runtimeEnv when requiring page, re-add warning
when trying to export with serverless, and update tests

* Update flying-shuttle test

* revert un-used pagesManifest change

* remove query.amp RegExp test

* Fix windows backslashes not being replaced

* Re-enable serverless support for next start

* bump

* Fix getInitialProps check

* Fix incorrect error check

* Re-add check for reserved pages

* Fix static check

* Update to ignore /api pages and clean up some tests

* Re-add needed next.config for test and correct behavior

* Update RegExp for ignored pages for auto-static

* Add checking for custom getInitialProps in pages/_app

* Update isPageStatic logic to only use default export

* Re-add retrying to CircleCi

* Update query during dev to only have values
available during export for static pages

* Fix test

* Add warning when page without default export is
found and make sure to update pages-manifest
correctly in flying-shuttle mode

* Fix backslashes not being replaced

* Integrate auto-static with flying-shuttle
and make sure AMP is handled in flying-shuttle

* Add autoExport for opting in
2019-05-22 09:36:53 -07:00

86 lines
2.3 KiB
TypeScript

import os from 'os'
import findUp from 'find-up'
import { CONFIG_FILE } from '../lib/constants'
const targets = ['server', 'serverless']
const defaultConfig: {[key: string]: any} = {
env: [],
webpack: null,
webpackDevMiddleware: null,
distDir: '.next',
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true,
generateBuildId: () => null,
generateEtags: true,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
target: 'server',
poweredByHeader: true,
onDemandEntries: {
maxInactiveAge: 60 * 1000,
pagesBufferLength: 2,
},
experimental: {
cpus: Math.max(
1,
(Number(process.env.CIRCLE_NODE_TOTAL) ||
(os.cpus() || { length: 1 }).length) - 1,
),
autoExport: false,
ampBindInitData: false,
exportTrailingSlash: true,
terserLoader: false,
profiling: false,
flyingShuttle: false,
asyncToPromises: false,
},
}
function assignDefaults(userConfig: {[key: string]: any}) {
Object.keys(userConfig).forEach((key: string) => {
const maybeObject = userConfig[key]
if ((!!maybeObject) && (maybeObject.constructor === Object)) {
userConfig[key] = {
...(defaultConfig[key] || {}),
...userConfig[key],
}
}
})
return { ...defaultConfig, ...userConfig }
}
function normalizeConfig(phase: string, config: any) {
if (typeof config === 'function') {
config = config(phase, { defaultConfig })
if (typeof config.then === 'function') {
throw new Error(
'> Promise returned in next config. https://err.sh/zeit/next.js/promise-in-next-config.md',
)
}
}
return config
}
export default function loadConfig(phase: string, dir: string, customConfig: any) {
if (customConfig) {
return assignDefaults({ configOrigin: 'server', ...customConfig })
}
const path = findUp.sync(CONFIG_FILE, {
cwd: dir,
})
// If config file was found
if (path && path.length) {
const userConfigModule = require(path)
const userConfig = normalizeConfig(phase, userConfigModule.default || userConfigModule)
if (userConfig.target && !targets.includes(userConfig.target)) {
throw new Error(`Specified target is invalid. Provided: "${userConfig.target}" should be one of ${targets.join(', ')}`)
}
return assignDefaults({ configOrigin: CONFIG_FILE, ...userConfig })
}
return defaultConfig
}