Skip to content

Commit

Permalink
Revert "feat: 调整 showWebpackStats"
Browse files Browse the repository at this point in the history
This reverts commit 13692f5.
  • Loading branch information
imsunhao committed Jun 20, 2019
1 parent cbb386e commit 7d78247
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 50 deletions.
11 changes: 3 additions & 8 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getConfig } from 'src/utils'
import { compiler, showWebpackStats } from 'src/utils/compiler.webpack.ts'
import webpack from 'webpack'
import { compiler } from 'src/utils/compiler.webpack.ts'

export function serverBuild() {
const config = getConfig()
Expand All @@ -12,12 +11,8 @@ export function serverBuild() {
{
serverConfig,
clientConfig,
clientCompilerDone: ({ stats }: { stats: webpack.Stats }) => {
showWebpackStats(stats, { message: 'clientCompilerDone' })
},
serverCompilerDone: ({ stats }: { stats: webpack.Stats }) => {
showWebpackStats(stats, { message: 'serverCompilerDone' })
}
clientCompilerDone: () => {},
serverCompilerDone: () => {}
},
'production'
)
Expand Down
8 changes: 5 additions & 3 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { readFileSync } from 'fs'
import chokidar from 'chokidar'
import cookieParser from 'cookie-parser'
import { getConfig, BASE_RENDER_OPTIONS } from 'src/utils'
import { compiler, showWebpackStats } from 'src/utils/compiler.webpack.ts'
import { compiler } from 'src/utils/compiler.webpack.ts'
import consola from 'consola'

/**
Expand Down Expand Up @@ -75,7 +75,9 @@ export function serverDevRender(app: Express) {
}

function clientCompilerDone({ devMiddleware, stats }: any) {
showWebpackStats(stats, { isdev: true })
stats = stats.toJson()
stats.errors.forEach((err: any) => consola.error(err))
stats.warnings.forEach((err: any) => consola.info(err))
if (stats.errors.length) return

renderOptions.clientManifest = JSON.parse(
Expand All @@ -87,7 +89,7 @@ export function serverDevRender(app: Express) {
function serverCompilerDone({ err, mfs, stats }: any) {
if (err) throw err

showWebpackStats(stats, { isdev: true })
stats = stats.toJson()

renderOptions.bundle = JSON.parse(
readFile(mfs, 'vue-ssr-server-bundle.json')
Expand Down
69 changes: 30 additions & 39 deletions src/utils/compiler.webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,17 @@ import { readFileSync, existsSync } from 'fs'
import { Express } from 'express'
import { routerStackManagement } from 'src/utils'

const WARNINGS_FILTER = /export .* was not found in/

export function showWebpackStats(
stats: webpack.Stats,
{
isdev,
message,
tostring
}: { isdev?: boolean; message?: string; tostring?: boolean } = {}
) {
if (tostring) {
consola.log(
(stats as webpack.Stats).toString({
warningsFilter: WARNINGS_FILTER
})
)
} else if (stats.hasWarnings()) {
const statsJSON = (stats as webpack.Stats).toJson({
warningsFilter: WARNINGS_FILTER
function showError(stats: webpack.Stats, { isdev }: { isdev?: boolean } = {}) {
if (stats.hasWarnings()) {
stats.compilation.warnings.forEach(warning => {
consola.info(warning)
})
if (statsJSON.warnings.length) {
statsJSON.warnings.forEach((err: any) => consola.info(err))
}
}

if (stats.hasErrors()) {
stats.compilation.errors.forEach(error => {
consola.fatal(error)
})
if (!isdev) {
if (message) consola.fatal(message)
return process.exit(1)
}
if (!isdev) return process.exit(1)
}
}

Expand All @@ -63,7 +41,7 @@ function prodCompiler({
})

clientCompiler.run((err, stats) => {
showWebpackStats(stats, { tostring: true })
consola.log(stats.toString())
})

const serverCompiler = getCompiler(serverConfig)
Expand All @@ -77,7 +55,7 @@ function prodCompiler({
warnings: false
})
)
showWebpackStats(stats, { isdev: false })
showError(stats, { isdev: false })
})
}

Expand Down Expand Up @@ -127,9 +105,6 @@ function devCompiler({
publicPath: clientConfig.output.publicPath,
noInfo: true,
logLevel: 'warn',
stats: {
warningsFilter: WARNINGS_FILTER
},
writeToDisk: false
})

Expand Down Expand Up @@ -221,7 +196,9 @@ export function compilerConfig(
} else if (mode !== 'none') {
const compiler = getCompiler(webpackConfig)
compiler.plugin('done', stats => {
showWebpackStats(stats)
stats = stats.toJson()
stats.errors.forEach((err: any) => consola.error(err))
stats.warnings.forEach((err: any) => consola.info(err))
if (stats.errors.length) return
try {
const souce = readFileSync(path, 'utf-8')
Expand All @@ -233,7 +210,9 @@ export function compilerConfig(
done(config)
}
})
compiler.run((err, stats) => {})
compiler.run((err, stats) => {
showError(stats)
})
} else {
consola.fatal('compilerConfig: config path not find.', path)
return process.exit(1)
Expand All @@ -251,7 +230,13 @@ export function compilerDll(options: ConfigOptions.options): Promise<any> {

const compiler = getCompiler(webpackConfig)
compiler.plugin('done', stats => {
showWebpackStats(stats, { message: 'build dll fail!' })
stats = stats.toJson()
stats.errors.forEach((err: any) => consola.error(err))
stats.warnings.forEach((err: any) => consola.info(err))
if (stats.errors.length) {
consola.fatal('build dll fail!')
return process.exit(1)
}
done()
})

Expand All @@ -263,7 +248,7 @@ export function compilerDll(options: ConfigOptions.options): Promise<any> {
assets: true
})
)
showWebpackStats(stats)
showError(stats)
}
})
})
Expand Down Expand Up @@ -299,7 +284,13 @@ function prodCompilerExtensions(options: ConfigOptions.options) {

const compiler = getCompiler(webpackConfig)
compiler.plugin('done', stats => {
showWebpackStats(stats, { message: 'build extensions fail!' })
stats = stats.toJson()
stats.errors.forEach((err: any) => consola.error(err))
stats.warnings.forEach((err: any) => consola.info(err))
if (stats.errors.length) {
consola.fatal('build extensions fail!')
return process.exit(1)
}
done()
})

Expand All @@ -311,7 +302,7 @@ function prodCompilerExtensions(options: ConfigOptions.options) {
assets: true
})
)
showWebpackStats(stats)
showError(stats)
}
})
})
Expand Down Expand Up @@ -345,7 +336,7 @@ function devCompilerExtensions(options: ConfigOptions.options, app?: Express) {

compiler.plugin('done', function(this: any, stats) {
routerStackManagement.init(app)
showWebpackStats(stats, { isdev: true })
showError(stats, { isdev: true })

Object.keys(entrys).forEach(entry => {
const name = entry + '.js'
Expand Down

0 comments on commit 7d78247

Please sign in to comment.