mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
hot reload compilation errors
This commit is contained in:
parent
7ec46d512b
commit
7569ef6ca9
|
@ -68,7 +68,7 @@ export default class Router {
|
||||||
let props
|
let props
|
||||||
try {
|
try {
|
||||||
data = await this.fetchComponent(route)
|
data = await this.fetchComponent(route)
|
||||||
if (route !== this.route) {
|
if (route === this.route) {
|
||||||
props = await this.getInitialProps(data.Component, data.ctx)
|
props = await this.getInitialProps(data.Component, data.ctx)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { join } from 'path'
|
import { join, relative, sep } from 'path'
|
||||||
import WebpackDevServer from 'webpack-dev-server'
|
import WebpackDevServer from 'webpack-dev-server'
|
||||||
import webpack from './build/webpack'
|
import webpack from './build/webpack'
|
||||||
import read from './read'
|
import read from './read'
|
||||||
|
@ -10,7 +10,7 @@ export default class HotReloader {
|
||||||
this.stats = null
|
this.stats = null
|
||||||
this.compilationErrors = null
|
this.compilationErrors = null
|
||||||
this.prevAssets = null
|
this.prevAssets = null
|
||||||
this.prevEntryChunkNames = null
|
this.prevChunkHashes = null
|
||||||
}
|
}
|
||||||
|
|
||||||
async start () {
|
async start () {
|
||||||
|
@ -44,24 +44,33 @@ export default class HotReloader {
|
||||||
this.stats = stats
|
this.stats = stats
|
||||||
this.compilationErrors = null
|
this.compilationErrors = null
|
||||||
|
|
||||||
const entryChunkNames = new Set(stats.compilation.chunks
|
const chunkHashes = {}
|
||||||
.filter((c) => c.entry)
|
stats.compilation.chunks.map((c) => {
|
||||||
.map((c) => c.name))
|
chunkHashes[c.name] = c.hash
|
||||||
|
})
|
||||||
|
|
||||||
if (this.prevEntryChunkNames) {
|
if (this.prevChunkHashes) {
|
||||||
const added = diff(entryChunkNames, this.prevEntryChunkNames)
|
const names = new Set(Object.keys(chunkHashes))
|
||||||
const removed = diff(this.prevEntryChunkNames, entryChunkNames)
|
const prevNames = new Set(Object.keys(this.prevChunkHashes))
|
||||||
|
const added = diff(names, prevNames)
|
||||||
|
const removed = diff(prevNames, names)
|
||||||
|
const failed = new Set(stats.compilation.errors
|
||||||
|
.map((e) => e.module.reasons)
|
||||||
|
.reduce((a, b) => a.concat(b), [])
|
||||||
|
.map((r) => r.module.chunks)
|
||||||
|
.reduce((a, b) => a.concat(b), [])
|
||||||
|
.map((c) => c.name)
|
||||||
|
.filter((n) => chunkHashes[n] !== this.prevChunkHashes[n]))
|
||||||
|
|
||||||
for (const n of new Set([...added, ...removed])) {
|
const rootDir = join('bundles', 'pages')
|
||||||
const m = n.match(/^bundles\/pages(\/.+?)(?:\/index)?\.js$/)
|
|
||||||
if (!m) {
|
for (const n of new Set([...added, ...removed, ...failed])) {
|
||||||
console.error('Unexpected chunk name: ' + n)
|
const route = toRoute(relative(rootDir, n))
|
||||||
continue
|
this.send('reload', route)
|
||||||
}
|
|
||||||
this.send('reload', m[1])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.prevEntryChunkNames = entryChunkNames
|
|
||||||
|
this.prevChunkHashes = chunkHashes
|
||||||
})
|
})
|
||||||
|
|
||||||
this.server = new WebpackDevServer(compiler, {
|
this.server = new WebpackDevServer(compiler, {
|
||||||
|
@ -106,15 +115,19 @@ export default class HotReloader {
|
||||||
getCompilationErrors () {
|
getCompilationErrors () {
|
||||||
if (!this.compilationErrors) {
|
if (!this.compilationErrors) {
|
||||||
this.compilationErrors = new Map()
|
this.compilationErrors = new Map()
|
||||||
if (this.stats.hasErrors()) {
|
|
||||||
const entries = this.stats.compilation.entries
|
|
||||||
.filter((e) => e.context === this.dir)
|
|
||||||
.filter((e) => !!e.errors.length || !!e.dependenciesErrors.length)
|
|
||||||
|
|
||||||
for (const e of entries) {
|
if (this.stats.hasErrors()) {
|
||||||
const path = join(e.context, '.next', e.name)
|
const { compiler, errors } = this.stats.compilation
|
||||||
const errors = e.errors.concat(e.dependenciesErrors)
|
|
||||||
this.compilationErrors.set(path, errors)
|
for (const err of errors) {
|
||||||
|
for (const r of err.module.reasons) {
|
||||||
|
for (const c of r.module.chunks) {
|
||||||
|
// get the path of the bundle file
|
||||||
|
const path = join(compiler.outputPath, c.name)
|
||||||
|
const errors = this.compilationErrors.get(path) || []
|
||||||
|
this.compilationErrors.set(path, errors.concat([err]))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,3 +152,8 @@ function deleteCache (path) {
|
||||||
function diff (a, b) {
|
function diff (a, b) {
|
||||||
return new Set([...a].filter((v) => !b.has(v)))
|
return new Set([...a].filter((v) => !b.has(v)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toRoute (file) {
|
||||||
|
const f = sep === '\\' ? file.replace(/\\/g, '/') : file
|
||||||
|
return ('/' + f).replace(/(\/index)?\.js$/, '') || '/'
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue