From aea8e0ee28a79b102647bf61497104ff5f1dab69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=92=B2=E5=A5=88Sakina?= <59400654+fz6m@users.noreply.github.com> Date: Thu, 23 Feb 2023 08:08:11 +0800 Subject: [PATCH] chore: esbuild minify error tips (#10580) --- packages/bundler-webpack/src/build.ts | 42 +++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/bundler-webpack/src/build.ts b/packages/bundler-webpack/src/build.ts index 8c8fab621a08..9e4b35707688 100644 --- a/packages/bundler-webpack/src/build.ts +++ b/packages/bundler-webpack/src/build.ts @@ -1,4 +1,4 @@ -import { rimraf } from '@umijs/utils'; +import { chalk, rimraf } from '@umijs/utils'; import { join, resolve } from 'path'; import webpack from '../compiled/webpack'; import { getConfig, IOpts as IConfigOpts } from './config/config'; @@ -78,7 +78,9 @@ export async function build(opts: IOpts): Promise { } if (stats) { const errorMsg = stats.toString('errors-only'); - // console.error(errorMsg); + + esbuildCompressErrorHelper(errorMsg); + reject(new Error(errorMsg)); } } else { @@ -102,3 +104,39 @@ export async function build(opts: IOpts): Promise { } }); } + +function esbuildCompressErrorHelper(errorMsg: string) { + if (typeof errorMsg !== 'string') return; + if ( + // https://github.com/evanw/esbuild/blob/a5f781ecd5edeb3fb6ae8d1045507ab850462614/internal/js_parser/js_parser_lower.go#L18 + errorMsg.includes('configured target environment') && + errorMsg.includes('es2015') + ) { + const terserRecommend = { + label: chalk.green('change jsMinifier'), + details: chalk.cyan(` jsMinifier: 'terser'`), + }; + const upgradeTargetRecommend = { + label: chalk.green('upgrade target'), + details: chalk.cyan(` jsMinifierOptions: { + target: ['chrome80', 'es2020'] + }`), + }; + const ieRecommend = { + details: `P.S. compatible with legacy browsers: https://umijs.org/blog/legacy-browser`, + }; + console.log(); + console.log(chalk.bgRed(' COMPRESSION ERROR ')); + console.log( + chalk.yellow( + `esbuild minify failed, please ${terserRecommend.label} or ${upgradeTargetRecommend.label}:`, + ), + ); + console.log('e.g. '); + console.log(terserRecommend.details); + console.log(' or'); + console.log(upgradeTargetRecommend.details); + console.log(ieRecommend.details); + console.log(); + } +}