Expose next function type (#7726)

* Expose next function type

* Add type testing for typescript custom server

* Move types test to integration folder
This commit is contained in:
Junyoung Choi 2019-07-04 11:38:58 +09:00 committed by JJ Kasper
parent e00a2c5d64
commit 12f1a63442
8 changed files with 95 additions and 0 deletions

View file

@ -13,6 +13,8 @@ import {
import { PageConfig } from 'next-server/types'
import next from 'next/dist/server/next'
// Extend the React types with missing properties
declare module 'react' {
// <html amp=""> support
@ -52,3 +54,5 @@ export {
NextApiRequest,
PageConfig,
}
export default next

View file

@ -0,0 +1,2 @@
server.js
pages/index.jsx

View file

@ -0,0 +1,2 @@
/// <reference types="next" />
/// <reference types="next/types/global" />

View file

@ -0,0 +1 @@
export default () => <p>hello world</p>

View file

@ -0,0 +1,17 @@
import next from 'next'
import http from 'http'
const dir = __dirname
const port = process.env.PORT || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev, dir })
const handleNextRequests = app.getRequestHandler()
app.prepare().then(() => {
const server = new http.Server((req, res) => {
handleNextRequests(req, res)
})
server.listen(port)
})

View file

@ -0,0 +1,14 @@
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import { buildTS } from 'next-test-utils'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '../')
describe('Custom Server TypeScript', () => {
it('should build server.ts correctly', async () => {
await buildTS([], appDir)
})
})

View file

@ -0,0 +1,29 @@
{
"compilerOptions": {
"esModuleInterop": true,
"module": "esnext",
"jsx": "preserve",
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
},
"exclude": [
"node_modules"
],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
]
}

View file

@ -176,6 +176,32 @@ export function nextStart (dir, port, opts = {}) {
return runNextCommandDev(['start', '-p', port, dir], undefined, opts)
}
export function buildTS (args = [], cwd, env = {}) {
cwd = cwd || path.dirname(require.resolve('next/package'))
env = { ...process.env, NODE_ENV: undefined, ...env }
return new Promise((resolve, reject) => {
const instance = spawn(
'node',
[require.resolve('typescript/lib/tsc'), ...args],
{ cwd, env }
)
let output = ''
const handleData = chunk => {
output += chunk.toString()
}
instance.stdout.on('data', handleData)
instance.stderr.on('data', handleData)
instance.on('exit', code => {
if (code) { return reject(new Error('exited with code: ' + code + '\n' + output)) }
resolve()
})
})
}
// Kill a launched app
export async function killApp (instance) {
await new Promise((resolve, reject) => {