Skip to content

Commit

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

export function serverBuild() {
const config = getConfig()
Expand All @@ -11,8 +12,12 @@ export function serverBuild() {
{
serverConfig,
clientConfig,
clientCompilerDone: () => {},
serverCompilerDone: () => {}
clientCompilerDone: ({ stats }: { stats: webpack.Stats }) => {
showWebpackStats(stats, { message: 'clientCompilerDone' })
},
serverCompilerDone: ({ stats }: { stats: webpack.Stats }) => {
showWebpackStats(stats, { message: 'serverCompilerDone' })
}
},
'production'
)
Expand Down
8 changes: 3 additions & 5 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 } from 'src/utils/compiler.webpack.ts'
import { compiler, showWebpackStats } from 'src/utils/compiler.webpack.ts'
import consola from 'consola'

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

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

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

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

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

function showError(stats: webpack.Stats, { isdev }: { isdev?: boolean } = {}) {
if (stats.hasWarnings()) {
stats.compilation.warnings.forEach(warning => {
consola.info(warning)
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
})
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) return process.exit(1)
if (!isdev) {
if (message) consola.fatal(message)
return process.exit(1)
}
}
}

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

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

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

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

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

const compiler = getCompiler(webpackConfig)
compiler.plugin('done', stats => {
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)
}
showWebpackStats(stats, { message: 'build dll fail!' })
done()
})

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

const compiler = getCompiler(webpackConfig)
compiler.plugin('done', stats => {
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)
}
showWebpackStats(stats, { message: 'build extensions fail!' })
done()
})

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

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

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

0 comments on commit 13692f5

Please sign in to comment.