Unverified Commit b5607ae7 authored by Maxi Gimenez's avatar Maxi Gimenez Committed by GitHub

chore: hot-reloader, htmlescape, next-dev-server missing types (#12730)

parent 5610af01
......@@ -244,7 +244,7 @@ export default class HotReloader {
return { finished }
}
async clean() {
async clean(): Promise<void> {
return recursiveDelete(join(this.dir, this.config.distDir), /^cache/)
}
......@@ -299,7 +299,7 @@ export default class HotReloader {
])
}
async start() {
async start(): Promise<void> {
await this.clean()
const configs = await this.getWebpackConfig()
......@@ -312,7 +312,9 @@ export default class HotReloader {
this.stats = ((await this.waitUntilValid()) as any).stats[0]
}
async stop(webpackDevMiddleware?: WebpackDevMiddleware.WebpackDevMiddleware) {
async stop(
webpackDevMiddleware?: WebpackDevMiddleware.WebpackDevMiddleware
): Promise<void> {
const middleware = webpackDevMiddleware || this.webpackDevMiddleware
if (middleware) {
return new Promise((resolve, reject) => {
......@@ -324,7 +326,7 @@ export default class HotReloader {
}
}
async reload() {
async reload(): Promise<void> {
this.stats = null
await this.clean()
......@@ -349,7 +351,7 @@ export default class HotReloader {
webpackDevMiddleware: WebpackDevMiddleware.WebpackDevMiddleware
webpackHotMiddleware: NextHandleFunction & WebpackHotMiddleware.EventStream
onDemandEntries: any
}) {
}): void {
this.webpackDevMiddleware = webpackDevMiddleware
this.webpackHotMiddleware = webpackHotMiddleware
this.onDemandEntries = onDemandEntries
......@@ -510,7 +512,7 @@ export default class HotReloader {
waitUntilValid(
webpackDevMiddleware?: WebpackDevMiddleware.WebpackDevMiddleware
) {
): Promise<webpack.Stats> {
const middleware = webpackDevMiddleware || this.webpackDevMiddleware
return new Promise(resolve => {
middleware!.waitUntilValid(resolve)
......@@ -545,7 +547,7 @@ export default class HotReloader {
return []
}
send = (action: string, ...args: any[]) => {
send = (action: string, ...args: any[]): void => {
this.webpackHotMiddleware!.publish({ action, data: args })
}
......
......@@ -11,6 +11,6 @@ const ESCAPE_LOOKUP: { [match: string]: string } = {
const ESCAPE_REGEX = /[&><\u2028\u2029]/g
export function htmlEscapeJsonString(str: string) {
export function htmlEscapeJsonString(str: string): string {
return str.replace(ESCAPE_REGEX, match => ESCAPE_LOOKUP[match])
}
......@@ -104,11 +104,11 @@ export default class DevServer extends Server {
this.staticPathsWorker.getStderr().pipe(process.stderr)
}
protected currentPhase() {
protected currentPhase(): string {
return PHASE_DEVELOPMENT_SERVER
}
protected readBuildId() {
protected readBuildId(): string {
return 'development'
}
......@@ -158,7 +158,7 @@ export default class DevServer extends Server {
}
}
async startWatcher() {
async startWatcher(): Promise<void> {
if (this.webpackWatcher) {
return
}
......@@ -242,7 +242,7 @@ export default class DevServer extends Server {
})
}
async stopWatcher() {
async stopWatcher(): Promise<void> {
if (!this.webpackWatcher) {
return
}
......@@ -251,7 +251,7 @@ export default class DevServer extends Server {
this.webpackWatcher = null
}
async prepare() {
async prepare(): Promise<void> {
await verifyTypeScriptSetup(this.dir, this.pagesDir!)
await this.loadCustomRoutes()
......@@ -286,7 +286,7 @@ export default class DevServer extends Server {
)
}
protected async close() {
protected async close(): Promise<void> {
await this.stopWatcher()
if (this.hotReloader) {
await this.hotReloader.stop()
......@@ -319,7 +319,7 @@ export default class DevServer extends Server {
res: ServerResponse,
params: Params,
parsedUrl: UrlWithParsedQuery
) {
): Promise<boolean> {
const { pathname } = parsedUrl
const pathParts = params.path || []
const path = `/${pathParts.join('/')}`
......@@ -345,7 +345,7 @@ export default class DevServer extends Server {
req: IncomingMessage,
res: ServerResponse,
parsedUrl: UrlWithParsedQuery
) {
): Promise<void> {
await this.devReady
const { pathname } = parsedUrl
......@@ -383,7 +383,7 @@ export default class DevServer extends Server {
})
}
private async loadCustomRoutes() {
private async loadCustomRoutes(): Promise<void> {
const result = {
redirects: [],
rewrites: [],
......@@ -452,19 +452,19 @@ export default class DevServer extends Server {
}
// In development public files are not added to the router but handled as a fallback instead
protected generatePublicRoutes() {
protected generatePublicRoutes(): never[] {
return []
}
// In development dynamic routes cannot be known ahead of time
protected getDynamicRoutes() {
protected getDynamicRoutes(): never[] {
return []
}
_filterAmpDevelopmentScript(
html: string,
event: { line: number; col: number; code: string }
) {
): boolean {
if (event.code !== 'DISALLOWED_SCRIPT_TAG') {
return true
}
......@@ -494,7 +494,7 @@ export default class DevServer extends Server {
res: ServerResponse,
pathname: string,
query: { [key: string]: string }
) {
): Promise<string | null> {
const compilationErr = await this.getCompilationError(pathname)
if (compilationErr) {
res.statusCode = 500
......@@ -543,7 +543,7 @@ export default class DevServer extends Server {
res: ServerResponse,
pathname: string,
query: { [key: string]: string }
) {
): Promise<string | null> {
if (res.statusCode === 404 && (await this.hasPage('/404'))) {
await this.hotReloader!.ensurePage('/404')
} else {
......@@ -573,13 +573,17 @@ export default class DevServer extends Server {
}
}
sendHTML(req: IncomingMessage, res: ServerResponse, html: string) {
sendHTML(
req: IncomingMessage,
res: ServerResponse,
html: string
): Promise<void> {
// In dev, we should not cache pages for any reason.
res.setHeader('Cache-Control', 'no-store, must-revalidate')
return super.sendHTML(req, res, html)
}
protected setImmutableAssetCacheControl(res: ServerResponse) {
protected setImmutableAssetCacheControl(res: ServerResponse): void {
res.setHeader('Cache-Control', 'no-store, must-revalidate')
}
......@@ -587,12 +591,12 @@ export default class DevServer extends Server {
req: IncomingMessage,
res: ServerResponse,
pathParts: string[]
) {
): Promise<void> {
const p = join(this.publicDir, ...pathParts.map(encodeURIComponent))
return this.serveStatic(req, res, p)
}
async hasPublicFile(path: string) {
async hasPublicFile(path: string): Promise<boolean> {
try {
const info = await fs.promises.stat(join(this.publicDir, path))
return info.isFile()
......@@ -601,7 +605,7 @@ export default class DevServer extends Server {
}
}
async getCompilationError(page: string) {
async getCompilationError(page: string): Promise<any> {
const errors = await this.hotReloader!.getCompilationErrors(page)
if (errors.length === 0) return
......
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