next.ts 1.3 KB
Newer Older
1
import Server, { ServerConstructor } from '../next-server/server/next-server'
2 3
import { NON_STANDARD_NODE_ENV } from '../lib/constants'
import * as log from '../build/output/log'
4

5
type NextServerConstructor = ServerConstructor & {
6 7 8 9 10 11 12 13
  /**
   * Whether to launch Next.js in dev mode - @default false
   */
  dev?: boolean
}

// This file is used for when users run `require('next')`
function createServer(options: NextServerConstructor): Server {
14 15
  const standardEnv = ['production', 'development', 'test']

16 17 18 19 20 21
  if (options == null) {
    throw new Error(
      'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options'
    )
  }

22 23 24 25 26 27 28 29
  if (
    !(options as any).isNextDevCommand &&
    process.env.NODE_ENV &&
    !standardEnv.includes(process.env.NODE_ENV)
  ) {
    log.warn(NON_STANDARD_NODE_ENV)
  }

30
  if (options.dev) {
31 32 33 34 35 36
    if (typeof options.dev !== 'boolean') {
      console.warn(
        "Warning: 'dev' is not a boolean which could introduce unexpected behavior. https://err.sh/next.js/invalid-server-options"
      )
    }

37 38
    const DevServer = require('./next-dev-server').default
    return new DevServer(options)
39 40 41 42 43 44 45
  }

  return new Server(options)
}

// Support commonjs `require('next')`
module.exports = createServer
46
exports = module.exports
47 48 49

// Support `import next from 'next'`
export default createServer