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