Unverified Commit e9b2e25c authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Convert Dev Server to TypeScript (#8966)

* Convert Dev Server to TypeScript
This converts the Next.js Development Server to TypeScript in prep for upcoming changes.

* Convert remaining necessary

* Fix some type errors

* Adjust types
parent c9d9ff6a
......@@ -85,7 +85,7 @@ export default class Server {
}
private compression?: Middleware
router: Router
private dynamicRoutes?: Array<{ page: string; match: RouteMatch }>
protected dynamicRoutes?: Array<{ page: string; match: RouteMatch }>
public constructor({
dir = '.',
......@@ -166,7 +166,7 @@ export default class Server {
})
}
private currentPhase(): string {
protected currentPhase(): string {
return PHASE_PRODUCTION_SERVER
}
......@@ -212,13 +212,13 @@ export default class Server {
public async prepare(): Promise<void> {}
// Backwards compatibility
private async close(): Promise<void> {}
protected async close(): Promise<void> {}
private setImmutableAssetCacheControl(res: ServerResponse) {
protected setImmutableAssetCacheControl(res: ServerResponse) {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
}
private generateRoutes(): Route[] {
protected generateRoutes(): Route[] {
const routes: Route[] = [
{
match: route('/_next/static/:path*'),
......@@ -379,7 +379,7 @@ export default class Server {
* Resolves path to resolver function
* @param pathname path of request
*/
private resolveApiRequest(pathname: string) {
protected async resolveApiRequest(pathname: string): Promise<string | null> {
return getPagePath(
pathname,
this.distDir,
......@@ -388,7 +388,7 @@ export default class Server {
)
}
private generatePublicRoutes(): Route[] {
protected generatePublicRoutes(): Route[] {
const routes: Route[] = []
const publicFiles = recursiveReadDirSync(this.publicDir)
const serverBuildPath = join(
......@@ -414,7 +414,7 @@ export default class Server {
return routes
}
private getDynamicRoutes() {
protected getDynamicRoutes() {
const manifest = require(this.pagesManifest)
const dynamicRoutedPages = Object.keys(manifest).filter(isDynamicRoute)
return getSortedRoutes(dynamicRoutedPages).map(page => ({
......@@ -429,7 +429,7 @@ export default class Server {
}
}
private async run(
protected async run(
req: IncomingMessage,
res: ServerResponse,
parsedUrl: UrlWithParsedQuery
......@@ -453,7 +453,7 @@ export default class Server {
await this.render404(req, res, parsedUrl)
}
private async sendHTML(
protected async sendHTML(
req: IncomingMessage,
res: ServerResponse,
html: string
......@@ -848,7 +848,7 @@ export default class Server {
return true
}
private readBuildId(): string {
protected readBuildId(): string {
const buildIdFile = join(this.distDir, BUILD_ID_FILE)
try {
return fs.readFileSync(buildIdFile, 'utf8').trim()
......
......@@ -163,6 +163,8 @@
"@types/send": "0.14.4",
"@types/styled-jsx": "2.2.8",
"@types/text-table": "0.2.1",
"@types/webpack-dev-middleware": "2.0.3",
"@types/webpack-hot-middleware": "2.16.5",
"@types/webpack-sources": "0.1.5",
"@zeit/ncc": "0.18.5",
"arg": "4.1.0",
......
import React from 'react'
import Head from '../next-server/lib/head'
// This component is only rendered on the server side.
export default function ErrorDebug ({ error, info }) {
export default function ErrorDebug({
error,
info,
}: {
error: Error
info: any
}) {
return (
<div style={styles.errorDebug}>
<div style={styles.errorDebug as object}>
<Head>
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</Head>
<StackTrace error={error} info={info} />
</div>
)
}
const StackTrace = ({ error: { name, message, stack }, info }) => (
const StackTrace = ({
error: { name, message, stack },
info,
}: {
error: Error
info: any
}) => (
<div>
<div style={styles.heading}>{message || name}</div>
<pre style={styles.stack}>{stack}</pre>
{info && <pre style={styles.stack}>{info.componentStack}</pre>}
<div style={styles.heading as object}>{message || name}</div>
<pre style={styles.stack as object}>{stack}</pre>
{info && <pre style={styles.stack as object}>{info.componentStack}</pre>}
</div>
)
......@@ -33,7 +46,7 @@ export const styles = {
top: 0,
bottom: 0,
zIndex: 9999,
color: '#000000'
color: '#000000',
},
stack: {
......@@ -45,7 +58,7 @@ export const styles = {
margin: 0,
whiteSpace: 'pre-wrap',
wordWrap: 'break-word',
marginTop: '16px'
marginTop: '16px',
},
heading: {
......@@ -56,6 +69,6 @@ export const styles = {
lineHeight: '28px',
color: '#000000',
marginBottom: '0px',
marginTop: '0px'
}
marginTop: '0px',
},
}
......@@ -2,18 +2,19 @@ import url from 'url'
import launchEditor from 'launch-editor'
import fs from 'fs'
import path from 'path'
import { IncomingMessage, ServerResponse } from 'http'
export default function errorOverlayMiddleware (options) {
return (req, res, next) => {
if (req.url.startsWith('/_next/development/open-stack-frame-in-editor')) {
const query = url.parse(req.url, true).query
const lineNumber = parseInt(query.lineNumber, 10) || 1
const colNumber = parseInt(query.colNumber, 10) || 1
export default function errorOverlayMiddleware(options: { dir: string }) {
return (req: IncomingMessage, res: ServerResponse, next: Function) => {
if (req.url!.startsWith('/_next/development/open-stack-frame-in-editor')) {
const query = url.parse(req.url!, true).query
const lineNumber = parseInt(query.lineNumber as string, 10) || 1
const colNumber = parseInt(query.colNumber as string, 10) || 1
let resolvedFileName = query.fileName
if (!fs.existsSync(resolvedFileName)) {
resolvedFileName = path.join(options.dir, resolvedFileName)
if (!fs.existsSync(resolvedFileName as string)) {
resolvedFileName = path.join(options.dir, resolvedFileName as string)
}
launchEditor(`${resolvedFileName}:${lineNumber}:${colNumber}`)
......
declare module '@babel/plugin-transform-modules-commonjs'
declare module 'webpack/lib/GraphHelpers'
declare module 'webpack/lib/DynamicEntryPlugin'
declare module 'unfetch'
declare module 'launch-editor'
declare module 'styled-jsx/server'
declare module 'async-retry'
......@@ -93,3 +95,19 @@ declare module NodeJS {
crossOrigin?: string
}
}
declare module 'watchpack' {
import { EventEmitter } from 'events'
class Watchpack extends EventEmitter {
watch(files: string[], directories: string[], startTime?: number): void
close(): void
getTimeInfoEntries(): Map<
string,
{ safeTime: number; timestamp: number; accuracy?: number }
>
}
export default Watchpack
}
......@@ -2205,6 +2205,13 @@
resolved "https://registry.yarnpkg.com/@types/lru-cache/-/lru-cache-5.1.0.tgz#57f228f2b80c046b4a1bd5cac031f81f207f4f03"
integrity sha512-RaE0B+14ToE4l6UqdarKPnXwVDuigfFv+5j9Dze/Nqr23yyuqdNvzcZi3xB+3Agvi5R4EOgAksfv3lXX4vBt9w==
"@types/memory-fs@*":
version "0.3.2"
resolved "https://registry.yarnpkg.com/@types/memory-fs/-/memory-fs-0.3.2.tgz#5d4753f9b390cb077c8c8af97bc96463399ceccd"
integrity sha512-j5AcZo7dbMxHoOimcHEIh0JZe5e1b8q8AqGSpZJrYc7xOgCIP79cIjTdx5jSDLtySnQDwkDTqwlC7Xw7uXw7qg==
dependencies:
"@types/node" "*"
"@types/mime@*":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d"
......@@ -2411,6 +2418,24 @@
"@types/unist" "*"
"@types/vfile-message" "*"
"@types/webpack-dev-middleware@2.0.3":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@types/webpack-dev-middleware/-/webpack-dev-middleware-2.0.3.tgz#aefb145281b3326a052325d583d2339debbf1be3"
integrity sha512-DzNJJ6ah/6t1n8sfAgQyEbZ/OMmFcF9j9P3aesnm7G6/iBFR/qiGin8K89J0RmaWIBzhTMdDg3I5PmKmSv7N9w==
dependencies:
"@types/connect" "*"
"@types/memory-fs" "*"
"@types/webpack" "*"
loglevel "^1.6.2"
"@types/webpack-hot-middleware@2.16.5":
version "2.16.5"
resolved "https://registry.yarnpkg.com/@types/webpack-hot-middleware/-/webpack-hot-middleware-2.16.5.tgz#5271eada42f34670a7ae79ddb6f1c419a19c985f"
integrity sha512-41qSQeyRGZkWSi366jMQVsLo5fdLT8EgmvHNoBwcCtwZcHrQk6An6tD+ZfC0zMdNHzVEFlzQvT2mTte8zDxqNw==
dependencies:
"@types/connect" "*"
"@types/webpack" "*"
"@types/webpack-sources@0.1.5":
version "0.1.5"
resolved "https://registry.yarnpkg.com/@types/webpack-sources/-/webpack-sources-0.1.5.tgz#be47c10f783d3d6efe1471ff7f042611bd464a92"
......@@ -9227,6 +9252,11 @@ log-update@^2.3.0:
cli-cursor "^2.0.0"
wrap-ansi "^3.0.1"
loglevel@^1.6.2:
version "1.6.4"
resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.6.4.tgz#f408f4f006db8354d0577dcf6d33485b3cb90d56"
integrity sha512-p0b6mOGKcGa+7nnmKbpzR6qloPbrgLcnio++E+14Vo/XffOGwZtRpUhr8dTH/x2oCMmEoIU0Zwm3ZauhvYD17g==
lolex@1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/lolex/-/lolex-1.3.2.tgz#7c3da62ffcb30f0f5a80a2566ca24e45d8a01f31"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment