diff --git a/src/build.ts b/src/build.ts index c582bc6..2859375 100644 --- a/src/build.ts +++ b/src/build.ts @@ -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() @@ -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' ) diff --git a/src/render.ts b/src/render.ts index 79a8cf1..1997773 100644 --- a/src/render.ts +++ b/src/render.ts @@ -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' /** @@ -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( @@ -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') diff --git a/src/utils/compiler.webpack.ts b/src/utils/compiler.webpack.ts index 5f82dfc..87a9753 100644 --- a/src/utils/compiler.webpack.ts +++ b/src/utils/compiler.webpack.ts @@ -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) + } } } @@ -41,7 +63,7 @@ function prodCompiler({ }) clientCompiler.run((err, stats) => { - consola.log(stats.toString()) + showWebpackStats(stats, { tostring: true }) }) const serverCompiler = getCompiler(serverConfig) @@ -55,7 +77,7 @@ function prodCompiler({ warnings: false }) ) - showError(stats, { isdev: false }) + showWebpackStats(stats, { isdev: false }) }) } @@ -105,6 +127,9 @@ function devCompiler({ publicPath: clientConfig.output.publicPath, noInfo: true, logLevel: 'warn', + stats: { + warningsFilter: WARNINGS_FILTER + }, writeToDisk: false }) @@ -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') @@ -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) @@ -230,13 +251,7 @@ export function compilerDll(options: ConfigOptions.options): Promise { 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() }) @@ -248,7 +263,7 @@ export function compilerDll(options: ConfigOptions.options): Promise { assets: true }) ) - showError(stats) + showWebpackStats(stats) } }) }) @@ -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() }) @@ -302,7 +311,7 @@ function prodCompilerExtensions(options: ConfigOptions.options) { assets: true }) ) - showError(stats) + showWebpackStats(stats) } }) }) @@ -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'