提交 a372edae 编写于 作者: J Joe Haddad 提交者: Tim Neutkens

Default `target` in `loadConfig` instead of `next build` (#7521)

* Default `target` in `loadConfig` instead of `next build`
We should set the target from the environment variable during `loadConfig` instead of in `next build`. This ensures all other locations in Next.js that rely on `config` can read the correct value to toggle behaviors.

* Use object destructuring

* Add tests for builder target

* Run start with serverless
上级 a3082d67
......@@ -15,7 +15,7 @@ const defaultConfig: { [key: string]: any } = {
generateBuildId: () => null,
generateEtags: true,
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
target: 'server',
target: process.env.__NEXT_BUILDER_EXPERIMENTAL_TARGET || 'server',
poweredByHeader: true,
onDemandEntries: {
maxInactiveAge: 60 * 1000,
......
......@@ -67,7 +67,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
console.log()
const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
const target = process.env.__NEXT_BUILDER_EXPERIMENTAL_TARGET || config.target
const { target } = config
const buildId = debug
? 'unoptimized-build'
: await generateBuildId(config.generateBuildId, nanoid)
......
module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60
},
experimental: {
autoExport: true
}
}
const Comp = () => {
return <div>Hello Serverless</div>
}
Comp.getInitialProps = () => ({})
export default Comp
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import { existsSync } from 'fs'
import {
killApp,
findPort,
nextBuild,
nextStart,
renderViaHTTP
} from 'next-test-utils'
const appDir = join(__dirname, '../')
const serverlessDir = join(appDir, '.next/serverless/pages')
let appPort
let app
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Serverless', () => {
beforeAll(async () => {
await nextBuild(appDir, [], {
env: { __NEXT_BUILDER_EXPERIMENTAL_TARGET: 'serverless' }
})
appPort = await findPort()
app = await nextStart(appDir, appPort, {
env: { __NEXT_BUILDER_EXPERIMENTAL_TARGET: 'serverless' }
})
})
afterAll(() => killApp(app))
it('should render the page', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/Hello Serverless/)
})
it('should have rendered the index page to serverless build', () => {
expect(existsSync(join(serverlessDir, 'index.js'))).toBeTruthy()
})
it('should not output _app.js and _document.js to serverless build', () => {
expect(existsSync(join(serverlessDir, '_app.js'))).toBeFalsy()
expect(existsSync(join(serverlessDir, '_document.js'))).toBeFalsy()
})
})
......@@ -126,9 +126,9 @@ export function runNextCommand (argv, options = {}) {
})
}
export function runNextCommandDev (argv, stdOut) {
export function runNextCommandDev (argv, stdOut, opts = {}) {
const cwd = path.dirname(require.resolve('next/package'))
const env = { ...process.env, NODE_ENV: undefined }
const env = { ...process.env, NODE_ENV: undefined, ...opts.env }
return new Promise((resolve, reject) => {
const instance = spawn('node', ['dist/bin/next', ...argv], { cwd, env })
......@@ -164,16 +164,16 @@ export function launchApp (dir, port) {
return runNextCommandDev([dir, '-p', port])
}
export function nextBuild (dir, args = []) {
return runNextCommand(['build', dir, ...args])
export function nextBuild (dir, args = [], opts = {}) {
return runNextCommand(['build', dir, ...args], opts)
}
export function nextExport (dir, { outdir }) {
return runNextCommand(['export', dir, '--outdir', outdir])
}
export function nextStart (dir, port) {
return runNextCommandDev(['start', '-p', port, dir])
export function nextStart (dir, port, opts = {}) {
return runNextCommandDev(['start', '-p', port, dir], undefined, opts)
}
// Kill a launched app
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册