Unverified Commit 5e3bf6e5 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Convert render.js to typescript (#5869)

* Convert render.js to typescript

* Compile tsx files too

* Remove internal renderErrorToHTML function

* Interopt component result

* requirePage doesn’t need async

* Move out enhancing logic into it’s own function

* Remove buildManifest from renderPage

* Move render into it’s own function

* Change let to const

* Move renderDocument into it’s own function
parent 81a2a6c4
...@@ -23,7 +23,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE ...@@ -23,7 +23,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
import React from 'react' import React from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
export default class Capture extends React.Component { type Props = {
report: (moduleName: string) => void
}
export default class Capture extends React.Component<Props> {
static propTypes = { static propTypes = {
report: PropTypes.func.isRequired report: PropTypes.func.isRequired
}; };
......
...@@ -315,4 +315,4 @@ Loadable.preloadReady = (webpackIds) => { ...@@ -315,4 +315,4 @@ Loadable.preloadReady = (webpackIds) => {
}) })
} }
module.exports = Loadable export default Loadable
...@@ -42,6 +42,8 @@ ...@@ -42,6 +42,8 @@
"devDependencies": { "devDependencies": {
"@taskr/clear": "1.1.0", "@taskr/clear": "1.1.0",
"@taskr/watch": "1.1.0", "@taskr/watch": "1.1.0",
"@types/react": "16.7.13",
"@types/react-dom": "16.0.11",
"@types/send": "0.14.4", "@types/send": "0.14.4",
"taskr": "1.1.0", "taskr": "1.1.0",
"typescript": "3.1.6" "typescript": "3.1.6"
......
type ManifestItem = { export type ManifestItem = {
id: number|string, id: number|string,
name: string, name: string,
file: string, file: string,
......
...@@ -4,10 +4,7 @@ import { resolve, join, sep } from 'path' ...@@ -4,10 +4,7 @@ import { resolve, join, sep } from 'path'
import { parse as parseUrl, UrlWithParsedQuery } from 'url' import { parse as parseUrl, UrlWithParsedQuery } from 'url'
import { parse as parseQs, ParsedUrlQuery } from 'querystring' import { parse as parseQs, ParsedUrlQuery } from 'querystring'
import fs from 'fs' import fs from 'fs'
import { import {renderToHTML} from './render'
renderToHTML,
renderErrorToHTML
} from './render'
import {sendHTML} from './send-html' import {sendHTML} from './send-html'
import {serveStatic} from './serve-static' import {serveStatic} from './serve-static'
import Router, {route, Route} from './router' import Router, {route, Route} from './router'
...@@ -246,11 +243,14 @@ export default class Server { ...@@ -246,11 +243,14 @@ export default class Server {
public async renderError (err: Error|null, req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery = {}): Promise<void> { public async renderError (err: Error|null, req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery = {}): Promise<void> {
res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate') res.setHeader('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate')
const html = await this.renderErrorToHTML(err, req, res, pathname, query) const html = await this.renderErrorToHTML(err, req, res, pathname, query)
if(html === null) {
return
}
return this.sendHTML(req, res, html) return this.sendHTML(req, res, html)
} }
public async renderErrorToHTML (err: Error|null, req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery = {}) { public async renderErrorToHTML (err: Error|null, req: IncomingMessage, res: ServerResponse, pathname: string, query: ParsedUrlQuery = {}) {
return renderErrorToHTML(err, req, res, pathname, query, this.renderOpts) return renderToHTML(req, res, '/_error', query, {...this.renderOpts, err})
} }
public async render404 (req: IncomingMessage, res: ServerResponse, parsedUrl?: UrlWithParsedQuery): Promise<void> { public async render404 (req: IncomingMessage, res: ServerResponse, parsedUrl?: UrlWithParsedQuery): Promise<void> {
......
...@@ -45,7 +45,7 @@ export function getPagePath (page: string, distDir: string): string { ...@@ -45,7 +45,7 @@ export function getPagePath (page: string, distDir: string): string {
return join(serverBuildPath, pagesManifest[page]) return join(serverBuildPath, pagesManifest[page])
} }
export async function requirePage (page: string, distDir: string): Promise<any> { export function requirePage (page: string, distDir: string): any {
const pagePath = getPagePath(page, distDir) const pagePath = getPagePath(page, distDir)
return require(pagePath) return require(pagePath)
} }
const notifier = require('node-notifier') const notifier = require('node-notifier')
export async function lib (task, opts) { export async function lib (task, opts) {
await task.source(opts.src || 'lib/**/*.+(js|ts)').typescript({module: 'commonjs'}).target('dist/lib') await task.source(opts.src || 'lib/**/*.+(js|ts|tsx)').typescript({module: 'commonjs'}).target('dist/lib')
notify('Compiled lib files') notify('Compiled lib files')
} }
export async function server (task, opts) { export async function server (task, opts) {
await task.source(opts.src || 'server/**/*.+(js|ts)').typescript({module: 'commonjs'}).target('dist/server') await task.source(opts.src || 'server/**/*.+(js|ts|tsx)').typescript({module: 'commonjs'}).target('dist/server')
notify('Compiled server files') notify('Compiled server files')
} }
...@@ -17,8 +17,8 @@ export async function build (task) { ...@@ -17,8 +17,8 @@ export async function build (task) {
export default async function (task) { export default async function (task) {
await task.clear('dist') await task.clear('dist')
await task.start('build') await task.start('build')
await task.watch('server/**/*.+(js|ts)', 'server') await task.watch('server/**/*.+(js|ts|tsx)', 'server')
await task.watch('lib/**/*.+(js|ts)', 'lib') await task.watch('lib/**/*.+(js|ts|tsx)', 'lib')
} }
export async function release (task) { export async function release (task) {
......
...@@ -14,9 +14,9 @@ export default class Document extends Component { ...@@ -14,9 +14,9 @@ export default class Document extends Component {
} }
static getInitialProps ({ renderPage }) { static getInitialProps ({ renderPage }) {
const { html, head, buildManifest } = renderPage() const { html, head } = renderPage()
const styles = flush() const styles = flush()
return { html, head, styles, buildManifest } return { html, head, styles }
} }
getChildContext () { getChildContext () {
......
...@@ -5,13 +5,12 @@ import Head from 'next-server/head' ...@@ -5,13 +5,12 @@ import Head from 'next-server/head'
// This component is rendered through dev-error-overlay on the client side. // This component is rendered through dev-error-overlay on the client side.
// On the server side it's rendered directly // On the server side it's rendered directly
export default function ErrorDebug ({error, info}) { export default function ErrorDebug ({error, info}) {
const { name, message, module } = error const { name, message } = error
return ( return (
<div style={styles.errorDebug}> <div style={styles.errorDebug}>
<Head> <Head>
<meta name='viewport' content='width=device-width, initial-scale=1.0' /> <meta name='viewport' content='width=device-width, initial-scale=1.0' />
</Head> </Head>
{module ? <h1 style={styles.heading}>Error in {module.rawRequest}</h1> : null}
{ {
name === 'ModuleBuildError' && message name === 'ModuleBuildError' && message
? <pre style={styles.stack} dangerouslySetInnerHTML={{ __html: ansiHTML(encodeHtml(message)) }} /> ? <pre style={styles.stack} dangerouslySetInnerHTML={{ __html: ansiHTML(encodeHtml(message)) }} />
......
...@@ -10,33 +10,33 @@ export async function bin (task, opts) { ...@@ -10,33 +10,33 @@ export async function bin (task, opts) {
} }
export async function lib (task, opts) { export async function lib (task, opts) {
await task.source(opts.src || 'lib/**/*.+(js|ts)').typescript({module: 'commonjs'}).target('dist/lib') await task.source(opts.src || 'lib/**/*.+(js|ts|tsx)').typescript({module: 'commonjs'}).target('dist/lib')
notify('Compiled lib files') notify('Compiled lib files')
} }
export async function server (task, opts) { export async function server (task, opts) {
await task.source(opts.src || 'server/**/*.+(js|ts)').typescript({module: 'commonjs'}).target('dist/server') await task.source(opts.src || 'server/**/*.+(js|ts|tsx)').typescript({module: 'commonjs'}).target('dist/server')
notify('Compiled server files') notify('Compiled server files')
} }
export async function nextbuild (task, opts) { export async function nextbuild (task, opts) {
await task.source(opts.src || 'build/**/*.+(js|ts)').typescript({module: 'commonjs'}).target('dist/build') await task.source(opts.src || 'build/**/*.+(js|ts|tsx)').typescript({module: 'commonjs'}).target('dist/build')
notify('Compiled build files') notify('Compiled build files')
} }
export async function client (task, opts) { export async function client (task, opts) {
await task.source(opts.src || 'client/**/*.+(js|ts)').typescript().target('dist/client') await task.source(opts.src || 'client/**/*.+(js|ts|tsx)').typescript().target('dist/client')
notify('Compiled client files') notify('Compiled client files')
} }
// export is a reserved keyword for functions // export is a reserved keyword for functions
export async function nextbuildstatic (task, opts) { export async function nextbuildstatic (task, opts) {
await task.source(opts.src || 'export/**/*.+(js|ts)').typescript({module: 'commonjs'}).target('dist/export') await task.source(opts.src || 'export/**/*.+(js|ts|tsx)').typescript({module: 'commonjs'}).target('dist/export')
notify('Compiled export files') notify('Compiled export files')
} }
export async function pages (task, opts) { export async function pages (task, opts) {
await task.source(opts.src || 'pages/**/*.+(js|ts)').typescript().target('dist/pages') await task.source(opts.src || 'pages/**/*.+(js|ts|tsx)').typescript().target('dist/pages')
} }
export async function build (task) { export async function build (task) {
...@@ -47,12 +47,12 @@ export default async function (task) { ...@@ -47,12 +47,12 @@ export default async function (task) {
await task.clear('dist') await task.clear('dist')
await task.start('build') await task.start('build')
await task.watch('bin/*', 'bin') await task.watch('bin/*', 'bin')
await task.watch('pages/**/*.+(js|ts)', 'pages') await task.watch('pages/**/*.+(js|ts|tsx)', 'pages')
await task.watch('server/**/*.+(js|ts)', 'server') await task.watch('server/**/*.+(js|ts|tsx)', 'server')
await task.watch('build/**/*.+(js|ts)', 'nextbuild') await task.watch('build/**/*.+(js|ts|tsx)', 'nextbuild')
await task.watch('export/**/*.+(js|ts)', 'nextexport') await task.watch('export/**/*.+(js|ts|tsx)', 'nextexport')
await task.watch('client/**/*.+(js|ts)', 'client') await task.watch('client/**/*.+(js|ts|tsx)', 'client')
await task.watch('lib/**/*.+(js|ts)', 'lib') await task.watch('lib/**/*.+(js|ts|tsx)', 'lib')
} }
export async function release (task) { export async function release (task) {
......
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