Skip to content

Commit

Permalink
chore: esbuild minify error tips (umijs#10580)
Browse files Browse the repository at this point in the history
  • Loading branch information
fz6m authored Feb 23, 2023
1 parent 1bc0060 commit aea8e0e
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions packages/bundler-webpack/src/build.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -78,7 +78,9 @@ export async function build(opts: IOpts): Promise<webpack.Stats> {
}
if (stats) {
const errorMsg = stats.toString('errors-only');
// console.error(errorMsg);

esbuildCompressErrorHelper(errorMsg);

reject(new Error(errorMsg));
}
} else {
Expand All @@ -102,3 +104,39 @@ export async function build(opts: IOpts): Promise<webpack.Stats> {
}
});
}

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();
}
}

0 comments on commit aea8e0e

Please sign in to comment.