Add CONFIG_FILE constant, add types for server/config.js (#4529)
This was pulled from #4518, it can already be merged so it's easier to get it in.
This commit is contained in:
parent
905da6f92b
commit
fbaeba49b6
7 changed files with 47 additions and 43 deletions
5
bin/next
5
bin/next
|
@ -2,6 +2,7 @@
|
|||
import { join } from 'path'
|
||||
import { spawn } from 'cross-spawn'
|
||||
import pkg from '../../package.json'
|
||||
import {CONFIG_FILE} from '../lib/constants'
|
||||
|
||||
if (pkg.peerDependencies) {
|
||||
Object.keys(pkg.peerDependencies).forEach(dependency => {
|
||||
|
@ -89,9 +90,9 @@ let proc = startProcess()
|
|||
|
||||
if (cmd === 'dev') {
|
||||
const {watchFile} = require('fs')
|
||||
watchFile(`${process.cwd()}/next.config.js`, (cur, prev) => {
|
||||
watchFile(`${process.cwd()}/${CONFIG_FILE}`, (cur, prev) => {
|
||||
if (cur.size > 0 || prev.size > 0) {
|
||||
console.log('\n> Found a change in next.config.js, restarting the server...')
|
||||
console.log(`\n> Found a change in ${CONFIG_FILE}, restarting the server...`)
|
||||
// Don't listen to 'close' now since otherwise parent gets killed by listener
|
||||
proc.removeAllListeners('close')
|
||||
proc.kill()
|
||||
|
|
|
@ -5,3 +5,4 @@ export const PHASE_DEVELOPMENT_SERVER = 'phase-development-server'
|
|||
export const PAGES_MANIFEST = 'pages-manifest.json'
|
||||
export const BUILD_MANIFEST = 'build-manifest.json'
|
||||
export const SERVER_DIRECTORY = 'server'
|
||||
export const CONFIG_FILE = 'next.config.js'
|
||||
|
|
|
@ -2,7 +2,7 @@ import { join } from 'path'
|
|||
import promisify from '../lib/promisify'
|
||||
import fs from 'fs'
|
||||
import webpack from 'webpack'
|
||||
import getConfig from '../config'
|
||||
import loadConfig from '../config'
|
||||
import { PHASE_PRODUCTION_BUILD } from '../../lib/constants'
|
||||
import getBaseWebpackConfig from './webpack'
|
||||
|
||||
|
@ -10,7 +10,7 @@ const access = promisify(fs.access)
|
|||
const writeFile = promisify(fs.writeFile)
|
||||
|
||||
export default async function build (dir, conf = null) {
|
||||
const config = getConfig(PHASE_PRODUCTION_BUILD, dir, conf)
|
||||
const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
|
||||
const buildId = await config.generateBuildId() // defaults to a uuid
|
||||
|
||||
try {
|
||||
|
|
|
@ -1,10 +1,26 @@
|
|||
// @flow
|
||||
import findUp from 'find-up'
|
||||
import uuid from 'uuid'
|
||||
import {CONFIG_FILE} from '../lib/constants'
|
||||
|
||||
const cache = new Map()
|
||||
type WebpackConfig = *
|
||||
|
||||
const defaultConfig = {
|
||||
type WebpackDevMiddlewareConfig = *
|
||||
|
||||
export type NextConfig = {|
|
||||
webpack: null | (webpackConfig: WebpackConfig, {dir: string, dev: boolean, isServer: boolean, buildId: string, config: NextConfig, defaultLoaders: {}, totalPages: number}) => WebpackConfig,
|
||||
webpackDevMiddleware: null | (WebpackDevMiddlewareConfig: WebpackDevMiddlewareConfig) => WebpackDevMiddlewareConfig,
|
||||
poweredByHeader: boolean,
|
||||
distDir: string,
|
||||
assetPrefix: string,
|
||||
configOrigin: string,
|
||||
useFileSystemPublicRoutes: boolean,
|
||||
generateBuildId: () => string,
|
||||
generateEtags: boolean,
|
||||
pageExtensions: Array<string>
|
||||
|}
|
||||
|
||||
const defaultConfig: NextConfig = {
|
||||
webpack: null,
|
||||
webpackDevMiddleware: null,
|
||||
poweredByHeader: true,
|
||||
|
@ -17,37 +33,28 @@ const defaultConfig = {
|
|||
pageExtensions: ['jsx', 'js']
|
||||
}
|
||||
|
||||
export default function getConfig (phase: string, dir: string, customConfig?: ?Object) {
|
||||
if (!cache.has(dir)) {
|
||||
cache.set(dir, loadConfig(phase, dir, customConfig))
|
||||
}
|
||||
return cache.get(dir)
|
||||
}
|
||||
type PhaseFunction = (phase: string, options: {defaultConfig: NextConfig}) => NextConfig
|
||||
|
||||
export function loadConfig (phase: string, dir: string, customConfig?: ?Object) {
|
||||
if (customConfig && typeof customConfig === 'object') {
|
||||
export default function loadConfig (phase: string, dir: string, customConfig?: NextConfig): NextConfig {
|
||||
if (customConfig) {
|
||||
customConfig.configOrigin = 'server'
|
||||
return withDefaults(customConfig)
|
||||
return {...defaultConfig, ...customConfig}
|
||||
}
|
||||
const path: string = findUp.sync('next.config.js', {
|
||||
const path: string = findUp.sync(CONFIG_FILE, {
|
||||
cwd: dir
|
||||
})
|
||||
|
||||
let userConfig = {}
|
||||
|
||||
// If config file was found
|
||||
if (path && path.length) {
|
||||
// $FlowFixMe
|
||||
const userConfigModule = require(path)
|
||||
userConfig = userConfigModule.default || userConfigModule
|
||||
if (typeof userConfigModule === 'function') {
|
||||
userConfig = userConfigModule(phase, {defaultConfig})
|
||||
const userConfigInitial: NextConfig | PhaseFunction = userConfigModule.default || userConfigModule
|
||||
if (typeof userConfigInitial === 'function') {
|
||||
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfigInitial(phase, {defaultConfig})}
|
||||
}
|
||||
userConfig.configOrigin = 'next.config.js'
|
||||
|
||||
return {...defaultConfig, configOrigin: CONFIG_FILE, ...userConfigInitial}
|
||||
}
|
||||
|
||||
return withDefaults(userConfig)
|
||||
}
|
||||
|
||||
function withDefaults (config: Object) {
|
||||
return Object.assign({}, defaultConfig, config)
|
||||
return defaultConfig
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ import mkdirp from 'mkdirp-then'
|
|||
import walk from 'walk'
|
||||
import { extname, resolve, join, dirname, sep } from 'path'
|
||||
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
||||
import getConfig from './config'
|
||||
import {PHASE_EXPORT, SERVER_DIRECTORY, PAGES_MANIFEST} from '../lib/constants'
|
||||
import loadConfig from './config'
|
||||
import {PHASE_EXPORT, SERVER_DIRECTORY, PAGES_MANIFEST, CONFIG_FILE} from '../lib/constants'
|
||||
import { renderToHTML } from './render'
|
||||
import { getAvailableChunks } from './utils'
|
||||
import { setAssetPrefix } from '../lib/asset'
|
||||
|
@ -13,7 +13,7 @@ import * as envConfig from '../lib/runtime-config'
|
|||
|
||||
export default async function (dir, options, configuration) {
|
||||
dir = resolve(dir)
|
||||
const nextConfig = configuration || getConfig(PHASE_EXPORT, dir)
|
||||
const nextConfig = configuration || loadConfig(PHASE_EXPORT, dir)
|
||||
const nextDir = join(dir, nextConfig.distDir)
|
||||
|
||||
log(`> using build directory: ${nextDir}`)
|
||||
|
@ -73,9 +73,9 @@ export default async function (dir, options, configuration) {
|
|||
|
||||
await copyPages(nextDir, outDir, buildId)
|
||||
|
||||
// Get the exportPathMap from the `next.config.js`
|
||||
// Get the exportPathMap from the config file
|
||||
if (typeof nextConfig.exportPathMap !== 'function') {
|
||||
console.log('> No "exportPathMap" found in "next.config.js". Generating map from "./pages"')
|
||||
console.log(`> No "exportPathMap" found in "${CONFIG_FILE}". Generating map from "./pages"`)
|
||||
nextConfig.exportPathMap = async (defaultMap) => {
|
||||
return defaultMap
|
||||
}
|
||||
|
|
|
@ -14,15 +14,16 @@ import {
|
|||
} from './render'
|
||||
import Router from './router'
|
||||
import { getAvailableChunks, isInternalUrl } from './utils'
|
||||
import getConfig from './config'
|
||||
import loadConfig from './config'
|
||||
import {PHASE_PRODUCTION_SERVER, PHASE_DEVELOPMENT_SERVER} from '../lib/constants'
|
||||
// We need to go up one more level since we are in the `dist` directory
|
||||
import pkg from '../../package'
|
||||
import * as asset from '../lib/asset'
|
||||
import * as envConfig from '../lib/runtime-config'
|
||||
import { isResSent } from '../lib/utils'
|
||||
import isAsyncSupported from './lib/is-async-supported'
|
||||
|
||||
// We need to go up one more level since we are in the `dist` directory
|
||||
import pkg from '../../package'
|
||||
|
||||
const access = promisify(fs.access)
|
||||
|
||||
const blockedPages = {
|
||||
|
@ -39,7 +40,7 @@ export default class Server {
|
|||
this.router = new Router()
|
||||
this.http = null
|
||||
const phase = dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER
|
||||
this.nextConfig = getConfig(phase, this.dir, conf)
|
||||
this.nextConfig = loadConfig(phase, this.dir, conf)
|
||||
this.dist = this.nextConfig.distDir
|
||||
|
||||
this.hotReloader = dev ? this.getHotReloader(this.dir, { quiet, config: this.nextConfig }) : null
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* global describe, it, expect */
|
||||
|
||||
import {join} from 'path'
|
||||
import getConfig, {loadConfig} from '../../dist/server/config'
|
||||
import loadConfig from '../../dist/server/config'
|
||||
import {PHASE_DEVELOPMENT_SERVER} from '../../dist/lib/constants'
|
||||
|
||||
const pathToConfig = join(__dirname, '_resolvedata', 'without-function')
|
||||
|
@ -32,10 +32,4 @@ describe('config', () => {
|
|||
const config = loadConfig(PHASE_DEVELOPMENT_SERVER, null, null)
|
||||
expect(config.webpack).toBe(null)
|
||||
})
|
||||
|
||||
it('Should cache on getConfig', () => {
|
||||
const config = getConfig(PHASE_DEVELOPMENT_SERVER, pathToConfig)
|
||||
const config2 = getConfig(PHASE_DEVELOPMENT_SERVER, pathToConfig, {extraConfig: true}) // won't add extraConfig because it's cached.
|
||||
expect(config === config2).toBe(true)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue